| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							|  |  |  |  | // 
 | 
					
						
							|  |  |  |  | // Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
					
						
							|  |  |  |  | // of this software and associated documentation files (the "Software"), to deal
 | 
					
						
							|  |  |  |  | // in the Software without restriction, including without limitation the rights
 | 
					
						
							|  |  |  |  | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
					
						
							|  |  |  |  | // copies of the Software, and to permit persons to whom the Software is
 | 
					
						
							|  |  |  |  | // furnished to do so, subject to the following conditions:
 | 
					
						
							|  |  |  |  | // 
 | 
					
						
							|  |  |  |  | // The above copyright notice and this permission notice shall be included in
 | 
					
						
							|  |  |  |  | // all copies or substantial portions of the Software.
 | 
					
						
							|  |  |  |  | // 
 | 
					
						
							|  |  |  |  | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
					
						
							|  |  |  |  | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
					
						
							|  |  |  |  | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
					
						
							|  |  |  |  | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
					
						
							|  |  |  |  | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
					
						
							|  |  |  |  | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					
						
							|  |  |  |  | // THE SOFTWARE.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #include "GifSprite.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-14 21:52:49 +08:00
										 |  |  |  | #include "../base/Logger.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | #include "../renderer/ImageCache.h"
 | 
					
						
							|  |  |  |  | #include "../renderer/Renderer.h"
 | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace kiwano | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	GifSprite::GifSprite() | 
					
						
							|  |  |  |  | 		: animating_(false) | 
					
						
							|  |  |  |  | 		, next_index_(0) | 
					
						
							|  |  |  |  | 		, total_loop_count_(1) | 
					
						
							|  |  |  |  | 		, loop_count_(0) | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 		, disposal_type_(DisposalType::Unknown) | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	GifSprite::GifSprite(Resource const& res) | 
					
						
							|  |  |  |  | 		: GifSprite() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		Load(res); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	GifSprite::GifSprite(GifImagePtr image) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		Load(image); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	bool GifSprite::Load(Resource const& res) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 		GifImagePtr image = ImageCache::GetInstance()->AddGifImage(res); | 
					
						
							|  |  |  |  | 		return Load(image); | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	bool GifSprite::Load(GifImagePtr image) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (image && image_ != image) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			image_ = image; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			next_index_ = 0; | 
					
						
							|  |  |  |  | 			loop_count_ = 0; | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 			disposal_type_ = DisposalType::None; | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 			SetSize( | 
					
						
							|  |  |  |  | 				static_cast<float>(image_->GetWidthInPixels()), | 
					
						
							|  |  |  |  | 				static_cast<float>(image_->GetHeightInPixels()) | 
					
						
							|  |  |  |  | 			); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 			if (!frame_rt_.IsValid()) | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 				Renderer::GetInstance()->CreateImageRenderTarget(frame_rt_); | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (image_->GetFramesCount() > 0) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				ComposeNextFrame(); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 			return true; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		return false; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 	void GifSprite::OnRender(Renderer* renderer) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (frame_.IsValid() && renderer->CheckVisibility(size_, transform_matrix_)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			PrepareRender(renderer); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			renderer->DrawImage(frame_); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 	void GifSprite::Update(Duration dt) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-14 00:28:25 +08:00
										 |  |  |  | 		Actor::Update(dt); | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (image_ && animating_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			frame_elapsed_ += dt; | 
					
						
							|  |  |  |  | 			if (frame_delay_ <= frame_elapsed_) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				frame_delay_ -= frame_elapsed_; | 
					
						
							|  |  |  |  | 				frame_elapsed_ = 0; | 
					
						
							|  |  |  |  | 				ComposeNextFrame(); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	void GifSprite::RestartAnimation() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		animating_ = true; | 
					
						
							|  |  |  |  | 		next_index_ = 0; | 
					
						
							|  |  |  |  | 		loop_count_ = 0; | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 		disposal_type_ = DisposalType::None; | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	void GifSprite::ComposeNextFrame() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 		if (frame_rt_.IsValid()) | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			do | 
					
						
							|  |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 				DisposeCurrentFrame(); | 
					
						
							|  |  |  |  | 				OverlayNextFrame(); | 
					
						
							|  |  |  |  | 			} while (frame_delay_.IsZero() && !IsLastFrame()); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			animating_ = (!EndOfAnimation() && image_->GetFramesCount() > 1); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	void GifSprite::DisposeCurrentFrame() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		switch (disposal_type_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 		case DisposalType::Unknown: | 
					
						
							|  |  |  |  | 		case DisposalType::None: | 
					
						
							|  |  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 		case DisposalType::Background: | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			ClearCurrentFrameArea(); | 
					
						
							|  |  |  |  | 			break; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		case DisposalType::Previous: | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			RestoreSavedFrame(); | 
					
						
							|  |  |  |  | 			break; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		default: | 
					
						
							|  |  |  |  | 			ThrowIfFailed(E_FAIL); | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 	void GifSprite::OverlayNextFrame() | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 		Image raw_image; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		HRESULT hr = image_->GetRawFrame(next_index_, raw_image, frame_rect_, frame_delay_, disposal_type_); | 
					
						
							|  |  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 			if (disposal_type_ == DisposalType::Previous) | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 				SaveComposedFrame(); | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 			} | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 			frame_rt_.BeginDraw(); | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (next_index_ == 0) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				// <20><><EFBFBD>»<EFBFBD><C2BB>Ʊ<EFBFBD><C6B1><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 				frame_rt_.Clear(image_->GetBackgroundColor()); | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 				loop_count_++; | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 			frame_rt_.DrawImage(raw_image, nullptr, &frame_rect_); | 
					
						
							|  |  |  |  | 			frame_rt_.EndDraw(); | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 			Image frame_to_render; | 
					
						
							|  |  |  |  | 			frame_rt_.GetOutput(frame_to_render); | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 			hr = frame_to_render.IsValid() ? S_OK : E_FAIL; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				frame_ = frame_to_render; | 
					
						
							|  |  |  |  | 				next_index_ = (++next_index_) % image_->GetFramesCount(); | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (IsLastFrame() && loop_cb_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			loop_cb_(loop_count_ - 1); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (EndOfAnimation() && done_cb_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			done_cb_(); | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-16 00:50:54 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		ThrowIfFailed(hr); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	void GifSprite::SaveComposedFrame() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		Image frame_to_be_saved; | 
					
						
							|  |  |  |  | 		frame_rt_.GetOutput(frame_to_be_saved); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		HRESULT hr = frame_to_be_saved.IsValid() ? S_OK : E_FAIL; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			if (!saved_frame_.IsValid()) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				auto size = frame_to_be_saved.GetSizeInPixels(); | 
					
						
							|  |  |  |  | 				auto prop = D2D1::BitmapProperties(frame_to_be_saved.GetPixelFormat()); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				ComPtr<ID2D1Bitmap> saved_bitmap; | 
					
						
							|  |  |  |  | 				hr = frame_rt_.GetRenderTarget()->CreateBitmap(D2D1::SizeU(size.x, size.y), prop, &saved_bitmap); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 				{ | 
					
						
							|  |  |  |  | 					saved_frame_.SetBitmap(saved_bitmap); | 
					
						
							|  |  |  |  | 				} | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			saved_frame_.CopyFrom(frame_to_be_saved); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		ThrowIfFailed(hr); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	void GifSprite::RestoreSavedFrame() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		HRESULT hr = saved_frame_.IsValid() ? S_OK : E_FAIL; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			Image frame_to_copy_to; | 
					
						
							|  |  |  |  | 			frame_rt_.GetOutput(frame_to_copy_to); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			hr = frame_to_copy_to.IsValid() ? S_OK : E_FAIL; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 			{ | 
					
						
							|  |  |  |  | 				frame_to_copy_to.CopyFrom(saved_frame_); | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		ThrowIfFailed(hr); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	void GifSprite::ClearCurrentFrameArea() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		frame_rt_.BeginDraw(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		frame_rt_.PushClipRect(frame_rect_); | 
					
						
							|  |  |  |  | 		frame_rt_.Clear(image_->GetBackgroundColor()); | 
					
						
							|  |  |  |  | 		frame_rt_.PopClipRect(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		return frame_rt_.EndDraw(); | 
					
						
							| 
									
										
										
										
											2019-07-31 16:22:33 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | } |