Magic_Game/src/kiwano/renderer/GifImage.cpp

418 lines
9.1 KiB
C++
Raw Normal View History

2019-04-14 22:37:05 +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 "GifImage.h"
2019-08-16 00:50:54 +08:00
#include "Renderer.h"
2019-08-14 21:52:49 +08:00
#include "../base/Logger.h"
2019-04-14 22:37:05 +08:00
namespace kiwano
{
GifImage::GifImage()
2019-07-31 16:22:33 +08:00
: frames_count_(0)
2019-04-14 22:37:05 +08:00
, width_in_pixels_(0)
, height_in_pixels_(0)
, bg_color_{}
{
}
2019-08-18 17:49:13 +08:00
GifImage::GifImage(String const& file_path)
{
Load(file_path);
}
2019-04-14 22:37:05 +08:00
GifImage::GifImage(Resource const& res)
: GifImage()
{
Load(res);
}
2019-08-18 17:49:13 +08:00
bool GifImage::Load(String const& file_path)
{
Renderer::GetInstance()->CreateGifImage(*this, file_path);
if (IsValid())
{
if (FAILED(GetGlobalMetadata()))
{
SetDecoder(nullptr);
return false;
}
return true;
}
return false;
}
2019-04-14 22:37:05 +08:00
bool GifImage::Load(Resource const& res)
{
2019-08-16 00:50:54 +08:00
Renderer::GetInstance()->CreateGifImage(*this, res);
2019-04-14 22:37:05 +08:00
2019-08-16 00:50:54 +08:00
if (IsValid())
2019-04-14 22:37:05 +08:00
{
2019-08-16 00:50:54 +08:00
if (FAILED(GetGlobalMetadata()))
2019-04-14 22:37:05 +08:00
{
2019-08-16 00:50:54 +08:00
SetDecoder(nullptr);
2019-04-14 22:37:05 +08:00
return false;
}
2019-08-16 00:50:54 +08:00
return true;
2019-04-14 22:37:05 +08:00
}
2019-08-16 00:50:54 +08:00
return false;
2019-04-14 22:37:05 +08:00
}
2019-08-16 00:50:54 +08:00
bool GifImage::IsValid() const
2019-04-14 22:37:05 +08:00
{
2019-08-16 00:50:54 +08:00
return decoder_ != nullptr;
2019-04-14 22:37:05 +08:00
}
HRESULT GifImage::GetGlobalMetadata()
{
2019-08-18 22:49:44 +08:00
UInt32 width = 0;
UInt32 height = 0;
2019-04-14 22:37:05 +08:00
PROPVARIANT prop_val;
::PropVariantInit(&prop_val);
ComPtr<IWICMetadataQueryReader> metadata_reader;
// <20><>ȡ֡<C8A1><D6A1><EFBFBD><EFBFBD>
2019-08-16 00:50:54 +08:00
HRESULT hr = decoder_ ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
hr = decoder_->GetFrameCount(&frames_count_);
}
2019-04-14 22:37:05 +08:00
if (SUCCEEDED(hr))
{
2019-07-31 16:22:33 +08:00
hr = decoder_->GetMetadataQueryReader(&metadata_reader);
2019-04-14 22:37:05 +08:00
}
if (SUCCEEDED(hr))
{
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ɫ
2019-08-13 21:16:38 +08:00
if (FAILED(GetBackgroundColor(metadata_reader.get())))
2019-04-14 22:37:05 +08:00
{
// <20><><EFBFBD><EFBFBD>δ<EFBFBD>ܻ<EFBFBD><DCBB><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ͸<CEAA><CDB8>
2019-08-16 00:50:54 +08:00
bg_color_ = Color(0, 0.f);
2019-04-14 22:37:05 +08:00
}
}
// <20><>ȡȫ<C8A1><C8AB> frame <20><>С
if (SUCCEEDED(hr))
{
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
2019-08-16 00:50:54 +08:00
hr = metadata_reader->GetMetadataByName(L"/logscrdesc/Width", &prop_val);
2019-04-14 22:37:05 +08:00
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>
2019-08-16 00:50:54 +08:00
hr = metadata_reader->GetMetadataByName(L"/logscrdesc/Height", &prop_val);
2019-04-14 22:37:05 +08:00
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>
2019-08-16 00:50:54 +08:00
hr = metadata_reader->GetMetadataByName(L"/logscrdesc/PixelAspectRatio", &prop_val);
2019-04-14 22:37:05 +08:00
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
2019-08-18 22:49:44 +08:00
Float32 pixel_asp_ratio = (prop_val.bVal + 15.f) / 64.f;
2019-04-14 22:37:05 +08:00
// <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;
2019-08-18 22:49:44 +08:00
height_in_pixels_ = static_cast<UInt32>(height / pixel_asp_ratio);
2019-04-14 22:37:05 +08:00
}
else
{
2019-08-18 22:49:44 +08:00
width_in_pixels_ = static_cast<UInt32>(width * pixel_asp_ratio);
2019-04-14 22:37:05 +08:00
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);
return hr;
}
2019-08-16 00:50:54 +08:00
HRESULT GifImage::GetBackgroundColor(ComPtr<IWICMetadataQueryReader> metadata_reader)
2019-04-14 22:37:05 +08:00
{
2019-08-19 09:28:59 +08:00
UChar bg_index = 0;
2019-08-16 00:50:54 +08:00
WICColor bgcolors[256];
2019-08-18 22:49:44 +08:00
UInt32 colors_copied = 0;
2019-08-16 00:50:54 +08:00
ComPtr<IWICPalette> wic_palette;
2019-04-14 22:37:05 +08:00
2019-08-16 00:50:54 +08:00
PROPVARIANT prop_val;
PropVariantInit(&prop_val);
2019-04-14 22:37:05 +08:00
2019-08-16 00:50:54 +08:00
HRESULT hr = metadata_reader->GetMetadataByName(L"/logscrdesc/GlobalColorTableFlag", &prop_val);
2019-04-14 22:37:05 +08:00
2019-08-16 00:50:54 +08:00
if (SUCCEEDED(hr))
{
hr = (prop_val.vt != VT_BOOL || !prop_val.boolVal) ? E_FAIL : S_OK;
::PropVariantClear(&prop_val);
}
2019-04-14 22:37:05 +08:00
if (SUCCEEDED(hr))
{
2019-08-16 00:50:54 +08:00
hr = metadata_reader->GetMetadataByName(L"/logscrdesc/BackgroundColorIndex", &prop_val);
2019-04-14 22:37:05 +08:00
2019-08-16 00:50:54 +08:00
if (SUCCEEDED(hr))
{
hr = (prop_val.vt != VT_UI1) ? E_FAIL : S_OK;
if (SUCCEEDED(hr))
{
bg_index = prop_val.bVal;
}
::PropVariantClear(&prop_val);
2019-04-14 22:37:05 +08:00
}
}
if (SUCCEEDED(hr))
{
2019-08-16 00:50:54 +08:00
auto factory = Renderer::GetInstance()->GetD2DDeviceResources()->GetWICImagingFactory();
hr = factory->CreatePalette(&wic_palette);
2019-04-14 22:37:05 +08:00
}
2019-08-16 00:50:54 +08:00
if (SUCCEEDED(hr))
{
hr = decoder_->CopyPalette(wic_palette.get());
}
2019-04-14 22:37:05 +08:00
if (SUCCEEDED(hr))
{
2019-08-16 00:50:54 +08:00
hr = wic_palette->GetColors(
ARRAYSIZE(bgcolors),
bgcolors,
&colors_copied);
2019-04-14 22:37:05 +08:00
}
if (SUCCEEDED(hr))
{
2019-08-16 00:50:54 +08:00
hr = (bg_index >= colors_copied) ? E_FAIL : S_OK;
2019-04-14 22:37:05 +08:00
}
2019-08-16 00:50:54 +08:00
if (SUCCEEDED(hr))
{
// ת<><D7AA>Ϊ ARGB <20><>ʽ
2019-08-18 22:49:44 +08:00
Float32 alpha = (bgcolors[bg_index] >> 24) / 255.f;
2019-08-16 00:50:54 +08:00
bg_color_ = Color(bgcolors[bg_index], alpha);
}
2019-04-14 22:37:05 +08:00
return hr;
}
2019-08-18 22:49:44 +08:00
HRESULT GifImage::GetRawFrame(UInt32 frame_index, Image& raw_frame, Rect& frame_rect, Duration& delay, DisposalType& disposal_type)
2019-04-14 22:37:05 +08:00
{
2019-08-16 00:50:54 +08:00
ComPtr<IWICFormatConverter> converter;
ComPtr<IWICBitmapFrameDecode> wic_frame;
ComPtr<IWICMetadataQueryReader> metadata_reader;
2019-04-14 22:37:05 +08:00
PROPVARIANT prop_val;
PropVariantInit(&prop_val);
2019-08-16 00:50:54 +08:00
// Retrieve the current frame
HRESULT hr = decoder_->GetFrame(frame_index, &wic_frame);
if (SUCCEEDED(hr))
{
// Format convert to 32bppPBGRA which D2D expects
auto factory = Renderer::GetInstance()->GetD2DDeviceResources()->GetWICImagingFactory();
hr = factory->CreateFormatConverter(&converter);
}
2019-04-14 22:37:05 +08:00
2019-08-16 00:50:54 +08:00
if (SUCCEEDED(hr))
{
hr = converter->Initialize(
wic_frame.get(),
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
nullptr,
0.f,
WICBitmapPaletteTypeCustom);
}
2019-04-14 22:37:05 +08:00
if (SUCCEEDED(hr))
{
2019-08-16 00:50:54 +08:00
auto ctx = Renderer::GetInstance()->GetD2DDeviceResources()->GetDeviceContext();
// Create a D2DBitmap from IWICBitmapSource
ComPtr<ID2D1Bitmap> raw_bitmap;
hr = ctx->CreateBitmapFromWicBitmap(
converter.get(),
nullptr,
&raw_bitmap
);
if (SUCCEEDED(hr))
{
raw_frame.SetBitmap(raw_bitmap);
}
2019-04-14 22:37:05 +08:00
}
if (SUCCEEDED(hr))
{
2019-08-16 00:50:54 +08:00
// Get Metadata Query Reader from the frame
hr = wic_frame->GetMetadataQueryReader(&metadata_reader);
}
2019-04-14 22:37:05 +08:00
2019-08-16 00:50:54 +08:00
// Get the Metadata for the current frame
if (SUCCEEDED(hr))
{
hr = metadata_reader->GetMetadataByName(L"/imgdesc/Left", &prop_val);
2019-04-14 22:37:05 +08:00
if (SUCCEEDED(hr))
{
2019-08-16 00:50:54 +08:00
hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL);
2019-04-14 22:37:05 +08:00
if (SUCCEEDED(hr))
{
2019-08-18 22:49:44 +08:00
frame_rect.origin.x = static_cast<Float32>(prop_val.uiVal);
2019-04-14 22:37:05 +08:00
}
2019-08-16 00:50:54 +08:00
PropVariantClear(&prop_val);
2019-04-14 22:37:05 +08:00
}
}
if (SUCCEEDED(hr))
{
2019-08-16 00:50:54 +08:00
hr = metadata_reader->GetMetadataByName(L"/imgdesc/Top", &prop_val);
if (SUCCEEDED(hr))
{
hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL);
if (SUCCEEDED(hr))
{
2019-08-18 22:49:44 +08:00
frame_rect.origin.y = static_cast<Float32>(prop_val.uiVal);
2019-08-16 00:50:54 +08:00
}
PropVariantClear(&prop_val);
}
2019-04-14 22:37:05 +08:00
}
if (SUCCEEDED(hr))
{
2019-08-16 00:50:54 +08:00
hr = metadata_reader->GetMetadataByName(L"/imgdesc/Width", &prop_val);
if (SUCCEEDED(hr))
{
hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL);
if (SUCCEEDED(hr))
{
2019-08-18 22:49:44 +08:00
frame_rect.size.x = static_cast<Float32>(prop_val.uiVal);
2019-08-16 00:50:54 +08:00
}
PropVariantClear(&prop_val);
}
2019-04-14 22:37:05 +08:00
}
if (SUCCEEDED(hr))
{
2019-08-16 00:50:54 +08:00
hr = metadata_reader->GetMetadataByName(L"/imgdesc/Height", &prop_val);
if (SUCCEEDED(hr))
{
hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL);
if (SUCCEEDED(hr))
{
2019-08-18 22:49:44 +08:00
frame_rect.size.y = static_cast<Float32>(prop_val.uiVal);
2019-08-16 00:50:54 +08:00
}
PropVariantClear(&prop_val);
}
2019-04-14 22:37:05 +08:00
}
if (SUCCEEDED(hr))
{
2019-08-16 00:50:54 +08:00
hr = metadata_reader->GetMetadataByName(L"/grctlext/Delay", &prop_val);
if (SUCCEEDED(hr))
{
hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL);
if (SUCCEEDED(hr))
{
2019-08-18 22:49:44 +08:00
UInt32 udelay = 0;
2019-08-16 00:50:54 +08:00
hr = UIntMult(prop_val.uiVal, 10, &udelay);
if (SUCCEEDED(hr))
{
delay.SetMilliseconds(static_cast<long>(udelay));
}
}
PropVariantClear(&prop_val);
}
else
{
delay = 0;
}
2019-04-14 22:37:05 +08:00
}
if (SUCCEEDED(hr))
{
2019-08-16 00:50:54 +08:00
hr = metadata_reader->GetMetadataByName(L"/grctlext/Disposal", &prop_val);
2019-04-14 22:37:05 +08:00
2019-08-16 00:50:54 +08:00
if (SUCCEEDED(hr))
{
hr = (prop_val.vt == VT_UI1) ? S_OK : E_FAIL;
if (SUCCEEDED(hr))
{
disposal_type = DisposalType(prop_val.bVal);
}
::PropVariantClear(&prop_val);
}
else
{
// <20><>ȡ DisposalType ʧ<>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC>ֻ<EFBFBD><D6BB>һ֡<D2BB><D6A1>ͼƬ
disposal_type = DisposalType::Unknown;
}
2019-04-14 22:37:05 +08:00
}
2019-08-16 00:50:54 +08:00
::PropVariantClear(&prop_val);
2019-04-14 22:37:05 +08:00
return hr;
}
}