| 
									
										
										
										
											2019-04-14 22:37:05 +08:00
										 |  |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-04-14 22:37:05 +08:00
										 |  |  |  | // 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:
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-04-14 22:37:05 +08:00
										 |  |  |  | // The above copyright notice and this permission notice shall be included in
 | 
					
						
							|  |  |  |  | // all copies or substantial portions of the Software.
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-04-14 22:37:05 +08:00
										 |  |  |  | // 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.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | #include <kiwano/core/Logger.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-17 16:55:47 +08:00
										 |  |  |  | #include <kiwano/render/GifImage.h>
 | 
					
						
							|  |  |  |  | #include <kiwano/render/Renderer.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-14 22:37:05 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace kiwano | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-06 16:54:47 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | GifImagePtr GifImage::Create(String const& file_path) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     GifImagePtr ptr = new (std::nothrow) GifImage; | 
					
						
							|  |  |  |  |     if (ptr) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         if (!ptr->Load(file_path)) | 
					
						
							|  |  |  |  |             return nullptr; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     return ptr; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | GifImagePtr GifImage::Create(Resource const& res) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     GifImagePtr ptr = new (std::nothrow) GifImage; | 
					
						
							|  |  |  |  |     if (ptr) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         if (!ptr->Load(res)) | 
					
						
							|  |  |  |  |             return nullptr; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  |     return ptr; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | GifImage::GifImage() | 
					
						
							|  |  |  |  |     : frames_count_(0) | 
					
						
							|  |  |  |  |     , width_in_pixels_(0) | 
					
						
							|  |  |  |  |     , height_in_pixels_(0) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool GifImage::Load(String const& file_path) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     Renderer::Instance().CreateGifImage(*this, file_path); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (IsValid()) | 
					
						
							|  |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-07 20:50:27 +08:00
										 |  |  |  |         if (GetGlobalMetadata()) | 
					
						
							|  |  |  |  |             return true; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         SetDecoder(nullptr); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  |     return false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool GifImage::Load(Resource const& res) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     Renderer::Instance().CreateGifImage(*this, res); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (IsValid()) | 
					
						
							|  |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-07 20:50:27 +08:00
										 |  |  |  |         if (GetGlobalMetadata()) | 
					
						
							|  |  |  |  |             return true; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         SetDecoder(nullptr); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  |     return false; | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-14 22:37:05 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | bool GifImage::IsValid() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     return decoder_ != nullptr; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | GifImage::Frame GifImage::GetFrame(uint32_t index) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     Frame frame; | 
					
						
							|  |  |  |  |     Renderer::Instance().CreateGifImageFrame(frame, *this, index); | 
					
						
							|  |  |  |  |     return frame; | 
					
						
							| 
									
										
										
										
											2019-04-14 22:37:05 +08:00
										 |  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 20:50:27 +08:00
										 |  |  |  | #if defined(KGE_WIN32)
 | 
					
						
							|  |  |  |  | bool GifImage::GetGlobalMetadata() | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     HRESULT hr = decoder_ ? S_OK : E_FAIL; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         hr = decoder_->GetFrameCount(&frames_count_); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         ComPtr<IWICMetadataQueryReader> metadata_reader; | 
					
						
							|  |  |  |  |         hr = decoder_->GetMetadataQueryReader(&metadata_reader); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             uint32_t width  = 0; | 
					
						
							|  |  |  |  |             uint32_t height = 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             PROPVARIANT prop_val; | 
					
						
							|  |  |  |  |             ::PropVariantInit(&prop_val); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             // <20><>ȡȫ<C8A1><C8AB> frame <20><>С
 | 
					
						
							|  |  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 // <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |                 hr = metadata_reader->GetMetadataByName(L"/logscrdesc/Width", &prop_val); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                 if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL); | 
					
						
							|  |  |  |  |                     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  |                     { | 
					
						
							|  |  |  |  |                         width = prop_val.uiVal; | 
					
						
							|  |  |  |  |                     } | 
					
						
							|  |  |  |  |                     ::PropVariantClear(&prop_val); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 // <20><>ȡ<EFBFBD>߶<EFBFBD>
 | 
					
						
							|  |  |  |  |                 hr = metadata_reader->GetMetadataByName(L"/logscrdesc/Height", &prop_val); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                 if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL); | 
					
						
							|  |  |  |  |                     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  |                     { | 
					
						
							|  |  |  |  |                         height = prop_val.uiVal; | 
					
						
							|  |  |  |  |                     } | 
					
						
							|  |  |  |  |                     ::PropVariantClear(&prop_val); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD><DDBA><EFBFBD>
 | 
					
						
							|  |  |  |  |                 hr = metadata_reader->GetMetadataByName(L"/logscrdesc/PixelAspectRatio", &prop_val); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                 if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     hr = (prop_val.vt == VT_UI1 ? S_OK : E_FAIL); | 
					
						
							|  |  |  |  |                     if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  |                     { | 
					
						
							|  |  |  |  |                         if (prop_val.bVal != 0) | 
					
						
							|  |  |  |  |                         { | 
					
						
							|  |  |  |  |                             // <20><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  |                             // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1<><31>4<EFBFBD><34><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 4<><34>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ 1/64
 | 
					
						
							|  |  |  |  |                             float pixel_asp_ratio = (prop_val.bVal + 15.f) / 64.f; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                             // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>س<EFBFBD><D8B3><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>Ⱥ߶ȣ<DFB6>ֻ<EFBFBD><D6BB>Сͼ<D0A1><CDBC>
 | 
					
						
							|  |  |  |  |                             if (pixel_asp_ratio > 1.f) | 
					
						
							|  |  |  |  |                             { | 
					
						
							|  |  |  |  |                                 width_in_pixels_  = width; | 
					
						
							|  |  |  |  |                                 height_in_pixels_ = static_cast<uint32_t>(height / pixel_asp_ratio); | 
					
						
							|  |  |  |  |                             } | 
					
						
							|  |  |  |  |                             else | 
					
						
							|  |  |  |  |                             { | 
					
						
							|  |  |  |  |                                 width_in_pixels_  = static_cast<uint32_t>(width * pixel_asp_ratio); | 
					
						
							|  |  |  |  |                                 height_in_pixels_ = height; | 
					
						
							|  |  |  |  |                             } | 
					
						
							|  |  |  |  |                         } | 
					
						
							|  |  |  |  |                         else | 
					
						
							|  |  |  |  |                         { | 
					
						
							|  |  |  |  |                             // ֵΪ 0, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>Ϊ 1
 | 
					
						
							|  |  |  |  |                             width_in_pixels_  = width; | 
					
						
							|  |  |  |  |                             height_in_pixels_ = height; | 
					
						
							|  |  |  |  |                         } | 
					
						
							|  |  |  |  |                     } | 
					
						
							|  |  |  |  |                     ::PropVariantClear(&prop_val); | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  |             ::PropVariantClear(&prop_val); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-07 20:50:27 +08:00
										 |  |  |  |     return SUCCEEDED(hr); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-07 20:50:27 +08:00
										 |  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:09:55 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | }  // namespace kiwano
 |