418 lines
9.2 KiB
C++
418 lines
9.2 KiB
C++
// 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"
|
||
#include "Renderer.h"
|
||
#include "../base/Logger.h"
|
||
|
||
namespace kiwano
|
||
{
|
||
GifImage::GifImage()
|
||
: frames_count_(0)
|
||
, width_in_pixels_(0)
|
||
, height_in_pixels_(0)
|
||
, bg_color_{}
|
||
{
|
||
}
|
||
|
||
GifImage::GifImage(String const& file_path)
|
||
{
|
||
Load(file_path);
|
||
}
|
||
|
||
GifImage::GifImage(Resource const& res)
|
||
: GifImage()
|
||
{
|
||
Load(res);
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
bool GifImage::Load(Resource const& res)
|
||
{
|
||
Renderer::GetInstance()->CreateGifImage(*this, res);
|
||
|
||
if (IsValid())
|
||
{
|
||
if (FAILED(GetGlobalMetadata()))
|
||
{
|
||
SetDecoder(nullptr);
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool GifImage::IsValid() const
|
||
{
|
||
return decoder_ != nullptr;
|
||
}
|
||
|
||
HRESULT GifImage::GetGlobalMetadata()
|
||
{
|
||
UInt32 width = 0;
|
||
UInt32 height = 0;
|
||
|
||
PROPVARIANT prop_val;
|
||
::PropVariantInit(&prop_val);
|
||
|
||
ComPtr<IWICMetadataQueryReader> metadata_reader;
|
||
|
||
// <20><>ȡ֡<C8A1><D6A1><EFBFBD><EFBFBD>
|
||
HRESULT hr = decoder_ ? S_OK : E_FAIL;
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
hr = decoder_->GetFrameCount(&frames_count_);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
hr = decoder_->GetMetadataQueryReader(&metadata_reader);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ɫ
|
||
if (FAILED(GetBackgroundColor(metadata_reader.get())))
|
||
{
|
||
// <20><><EFBFBD><EFBFBD>δ<EFBFBD>ܻ<EFBFBD><DCBB><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ<CEAA><CDB8>
|
||
bg_color_ = Color(0, 0.f);
|
||
}
|
||
}
|
||
|
||
// <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
|
||
Float32 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>(height / pixel_asp_ratio);
|
||
}
|
||
else
|
||
{
|
||
width_in_pixels_ = static_cast<UInt32>(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);
|
||
return hr;
|
||
}
|
||
|
||
HRESULT GifImage::GetBackgroundColor(ComPtr<IWICMetadataQueryReader> metadata_reader)
|
||
{
|
||
UChar bg_index = 0;
|
||
WICColor bgcolors[256];
|
||
UInt32 colors_copied = 0;
|
||
ComPtr<IWICPalette> wic_palette;
|
||
|
||
PROPVARIANT prop_val;
|
||
PropVariantInit(&prop_val);
|
||
|
||
HRESULT hr = metadata_reader->GetMetadataByName(L"/logscrdesc/GlobalColorTableFlag", &prop_val);
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
hr = (prop_val.vt != VT_BOOL || !prop_val.boolVal) ? E_FAIL : S_OK;
|
||
::PropVariantClear(&prop_val);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
hr = metadata_reader->GetMetadataByName(L"/logscrdesc/BackgroundColorIndex", &prop_val);
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
hr = (prop_val.vt != VT_UI1) ? E_FAIL : S_OK;
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
bg_index = prop_val.bVal;
|
||
}
|
||
::PropVariantClear(&prop_val);
|
||
}
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
auto factory = Renderer::GetInstance()->GetD2DDeviceResources()->GetWICImagingFactory();
|
||
hr = factory->CreatePalette(&wic_palette);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
hr = decoder_->CopyPalette(wic_palette.get());
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
hr = wic_palette->GetColors(
|
||
ARRAYSIZE(bgcolors),
|
||
bgcolors,
|
||
&colors_copied);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
hr = (bg_index >= colors_copied) ? E_FAIL : S_OK;
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// ת<><D7AA>Ϊ ARGB <20><>ʽ
|
||
Float32 alpha = (bgcolors[bg_index] >> 24) / 255.f;
|
||
bg_color_ = Color(bgcolors[bg_index], alpha);
|
||
}
|
||
return hr;
|
||
}
|
||
|
||
HRESULT GifImage::GetRawFrame(UInt32 frame_index, Image& raw_frame, Rect& frame_rect, Duration& delay, DisposalType& disposal_type)
|
||
{
|
||
ComPtr<IWICFormatConverter> converter;
|
||
ComPtr<IWICBitmapFrameDecode> wic_frame;
|
||
ComPtr<IWICMetadataQueryReader> metadata_reader;
|
||
|
||
PROPVARIANT prop_val;
|
||
PropVariantInit(&prop_val);
|
||
|
||
// 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);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
hr = converter->Initialize(
|
||
wic_frame.get(),
|
||
GUID_WICPixelFormat32bppPBGRA,
|
||
WICBitmapDitherTypeNone,
|
||
nullptr,
|
||
0.f,
|
||
WICBitmapPaletteTypeCustom);
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
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);
|
||
}
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
// Get Metadata Query Reader from the frame
|
||
hr = wic_frame->GetMetadataQueryReader(&metadata_reader);
|
||
}
|
||
|
||
// Get the Metadata for the current frame
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
hr = metadata_reader->GetMetadataByName(L"/imgdesc/Left", &prop_val);
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
hr = (prop_val.vt == VT_UI2 ? S_OK : E_FAIL);
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
frame_rect.left_top.x = static_cast<Float32>(prop_val.uiVal);
|
||
}
|
||
PropVariantClear(&prop_val);
|
||
}
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
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))
|
||
{
|
||
frame_rect.left_top.y = static_cast<Float32>(prop_val.uiVal);
|
||
}
|
||
PropVariantClear(&prop_val);
|
||
}
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
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))
|
||
{
|
||
frame_rect.right_bottom.x = frame_rect.left_top.x + static_cast<Float32>(prop_val.uiVal);
|
||
}
|
||
PropVariantClear(&prop_val);
|
||
}
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
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))
|
||
{
|
||
frame_rect.right_bottom.y = frame_rect.left_top.y + static_cast<Float32>(prop_val.uiVal);
|
||
}
|
||
PropVariantClear(&prop_val);
|
||
}
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
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))
|
||
{
|
||
UInt32 udelay = 0;
|
||
hr = UIntMult(prop_val.uiVal, 10, &udelay);
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
delay.SetMilliseconds(static_cast<long>(udelay));
|
||
}
|
||
}
|
||
PropVariantClear(&prop_val);
|
||
}
|
||
else
|
||
{
|
||
delay = 0;
|
||
}
|
||
}
|
||
|
||
if (SUCCEEDED(hr))
|
||
{
|
||
hr = metadata_reader->GetMetadataByName(L"/grctlext/Disposal", &prop_val);
|
||
|
||
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;
|
||
}
|
||
}
|
||
|
||
::PropVariantClear(&prop_val);
|
||
return hr;
|
||
}
|
||
|
||
}
|