2018-10-03 22:02:46 +08:00
|
|
|
|
// Copyright (c) 2016-2018 Easy2D - 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.
|
|
|
|
|
|
|
2018-09-06 23:26:32 +08:00
|
|
|
|
#include "..\e2dobject.h"
|
2018-09-05 13:33:39 +08:00
|
|
|
|
#include "..\e2dmodule.h"
|
|
|
|
|
|
#include "..\e2dtool.h"
|
2017-10-18 22:13:20 +08:00
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
std::map<size_t, ID2D1Bitmap*> easy2d::Image::bitmap_cache_;
|
2017-10-18 22:13:20 +08:00
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
easy2d::Image::Image()
|
2018-09-04 22:42:34 +08:00
|
|
|
|
: bitmap_(nullptr)
|
|
|
|
|
|
, crop_rect_()
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
easy2d::Image::Image(const Resource& res)
|
2018-09-04 22:42:34 +08:00
|
|
|
|
: bitmap_(nullptr)
|
|
|
|
|
|
, crop_rect_()
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-10-06 10:25:29 +08:00
|
|
|
|
this->Load(res);
|
2017-10-18 22:13:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
easy2d::Image::Image(const Resource& res, const Rect& crop_rect)
|
2018-09-04 22:42:34 +08:00
|
|
|
|
: bitmap_(nullptr)
|
|
|
|
|
|
, crop_rect_()
|
2018-04-27 00:16:14 +08:00
|
|
|
|
{
|
2018-10-06 10:25:29 +08:00
|
|
|
|
this->Load(res);
|
2018-09-04 22:42:34 +08:00
|
|
|
|
this->Crop(crop_rect);
|
2018-04-27 00:16:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
easy2d::Image::Image(const String & file_name)
|
2018-09-04 22:42:34 +08:00
|
|
|
|
: bitmap_(nullptr)
|
|
|
|
|
|
, crop_rect_()
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-10-06 10:25:29 +08:00
|
|
|
|
this->Load(file_name);
|
2017-10-18 22:13:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
easy2d::Image::Image(const String & file_name, const Rect & crop_rect)
|
2018-09-04 22:42:34 +08:00
|
|
|
|
: bitmap_(nullptr)
|
|
|
|
|
|
, crop_rect_()
|
2018-04-27 00:16:14 +08:00
|
|
|
|
{
|
2018-10-06 10:25:29 +08:00
|
|
|
|
this->Load(file_name);
|
2018-09-04 22:42:34 +08:00
|
|
|
|
this->Crop(crop_rect);
|
2018-04-27 00:16:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
easy2d::Image::~Image()
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
SafeRelease(bitmap_);
|
2017-10-18 22:13:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
bool easy2d::Image::Load(const Resource& res)
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-10-06 10:25:29 +08:00
|
|
|
|
if (!Image::CacheBitmap(res))
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-10-16 15:42:15 +08:00
|
|
|
|
E2D_WARNING("Load Image from file failed!");
|
2018-04-27 00:16:14 +08:00
|
|
|
|
return false;
|
2017-10-18 22:13:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-07 17:56:03 +08:00
|
|
|
|
this->SetBitmap(bitmap_cache_.at(res.id));
|
2018-04-27 00:16:14 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
bool easy2d::Image::Load(const String & file_name)
|
2018-04-27 00:16:14 +08:00
|
|
|
|
{
|
2018-10-16 15:42:15 +08:00
|
|
|
|
E2D_WARNING_IF(file_name.IsEmpty(), "Image Load failed! Invalid file name.");
|
2018-08-19 20:40:44 +08:00
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (file_name.IsEmpty())
|
2018-08-19 20:40:44 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
2018-10-06 10:25:29 +08:00
|
|
|
|
if (!Image::CacheBitmap(file_name))
|
2018-08-19 20:40:44 +08:00
|
|
|
|
{
|
2018-10-16 15:42:15 +08:00
|
|
|
|
E2D_WARNING("Load Image from file failed!");
|
2018-08-19 20:40:44 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
this->SetBitmap(bitmap_cache_.at(file_name.GetHash()));
|
2018-08-19 20:40:44 +08:00
|
|
|
|
return true;
|
2017-10-18 22:13:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
void easy2d::Image::Crop(const Rect& crop_rect)
|
2018-02-03 22:04:43 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (bitmap_)
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-09-16 16:07:51 +08:00
|
|
|
|
auto bitmap_size = bitmap_->GetSize();
|
|
|
|
|
|
crop_rect_.origin.x = std::min(std::max(crop_rect.origin.x, 0.f), bitmap_size.width);
|
|
|
|
|
|
crop_rect_.origin.y = std::min(std::max(crop_rect.origin.y, 0.f), bitmap_size.height);
|
|
|
|
|
|
crop_rect_.size.width = std::min(std::max(crop_rect.size.width, 0.f), bitmap_size.width - crop_rect.origin.x);
|
|
|
|
|
|
crop_rect_.size.height = std::min(std::max(crop_rect.size.height, 0.f), bitmap_size.height - crop_rect.origin.y);
|
2017-10-18 22:13:20 +08:00
|
|
|
|
}
|
2018-02-03 22:04:43 +08:00
|
|
|
|
}
|
2017-10-18 22:13:20 +08:00
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
float easy2d::Image::GetWidth() const
|
2018-02-03 22:04:43 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return crop_rect_.size.width;
|
2018-02-03 22:04:43 +08:00
|
|
|
|
}
|
2017-10-18 22:13:20 +08:00
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
float easy2d::Image::GetHeight() const
|
2018-02-03 22:04:43 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return crop_rect_.size.height;
|
2018-02-03 22:04:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
easy2d::Size easy2d::Image::GetSize() const
|
2018-02-03 22:04:43 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return crop_rect_.size;
|
2017-10-18 22:13:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
float easy2d::Image::GetSourceWidth() const
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (bitmap_)
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return bitmap_->GetSize().width;
|
2017-10-18 22:13:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
float easy2d::Image::GetSourceHeight() const
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (bitmap_)
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return bitmap_->GetSize().height;
|
2017-10-18 22:13:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
easy2d::Size easy2d::Image::GetSourceSize() const
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-09-16 16:07:51 +08:00
|
|
|
|
Size source_size;
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (bitmap_)
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-09-16 16:07:51 +08:00
|
|
|
|
auto bitmap_size = bitmap_->GetSize();
|
|
|
|
|
|
source_size.width = bitmap_size.width;
|
|
|
|
|
|
source_size.height = bitmap_size.height;
|
2017-10-18 22:13:20 +08:00
|
|
|
|
}
|
2018-09-16 16:07:51 +08:00
|
|
|
|
return std::move(source_size);
|
2017-10-18 22:13:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
float easy2d::Image::GetCropX() const
|
2018-02-03 22:04:43 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return crop_rect_.origin.x;
|
2018-02-03 22:04:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
float easy2d::Image::GetCropY() const
|
2018-02-03 22:04:43 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return crop_rect_.origin.y;
|
2018-02-03 22:04:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
easy2d::Point easy2d::Image::GetCropPos() const
|
2018-02-03 22:04:43 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
return crop_rect_.origin;
|
2018-02-03 22:04:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
const easy2d::Rect & easy2d::Image::GetCropRect() const
|
2018-09-16 16:07:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
return crop_rect_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
ID2D1Bitmap * easy2d::Image::GetBitmap() const
|
2018-10-03 18:04:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
return bitmap_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
bool easy2d::Image::CacheBitmap(const Resource& res)
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-09-07 17:56:03 +08:00
|
|
|
|
if (bitmap_cache_.find(res.id) != bitmap_cache_.end())
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-03 18:04:04 +08:00
|
|
|
|
IWICImagingFactory *imaging_factory = Device::GetGraphics()->GetImagingFactory();
|
|
|
|
|
|
ID2D1HwndRenderTarget* render_target = Device::GetGraphics()->GetRenderTarget();
|
2018-09-07 23:47:21 +08:00
|
|
|
|
IWICBitmapDecoder *decoder = nullptr;
|
2018-09-07 17:56:03 +08:00
|
|
|
|
IWICBitmapFrameDecode *source = nullptr;
|
|
|
|
|
|
IWICStream *stream = nullptr;
|
|
|
|
|
|
IWICFormatConverter *converter = nullptr;
|
|
|
|
|
|
ID2D1Bitmap *bitmap = nullptr;
|
|
|
|
|
|
HRSRC res_handle = nullptr;
|
|
|
|
|
|
HGLOBAL res_data_handle = nullptr;
|
|
|
|
|
|
void *image_file = nullptr;
|
|
|
|
|
|
DWORD image_file_size = 0;
|
2018-08-19 20:40:44 +08:00
|
|
|
|
|
|
|
|
|
|
// <20><>λ<EFBFBD><CEBB>Դ
|
2018-09-07 17:56:03 +08:00
|
|
|
|
res_handle = ::FindResourceW(
|
2018-09-07 23:47:21 +08:00
|
|
|
|
HINST_THISCOMPONENT,
|
|
|
|
|
|
MAKEINTRESOURCE(res.id),
|
2018-09-04 22:42:34 +08:00
|
|
|
|
(LPCWSTR)res.type
|
2018-08-19 20:40:44 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
2018-09-07 17:56:03 +08:00
|
|
|
|
HRESULT hr = res_handle ? S_OK : E_FAIL;
|
2018-08-19 20:40:44 +08:00
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
|
2018-09-07 17:56:03 +08:00
|
|
|
|
res_data_handle = ::LoadResource(HINST_THISCOMPONENT, res_handle);
|
2018-08-19 20:40:44 +08:00
|
|
|
|
|
2018-09-07 17:56:03 +08:00
|
|
|
|
hr = res_data_handle ? S_OK : E_FAIL;
|
2018-08-19 20:40:44 +08:00
|
|
|
|
}
|
2017-10-18 22:13:20 +08:00
|
|
|
|
|
2018-08-19 20:40:44 +08:00
|
|
|
|
if (SUCCEEDED(hr))
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-08-19 20:40:44 +08:00
|
|
|
|
// <20><>ȡ<EFBFBD>ļ<EFBFBD>ָ<EFBFBD>룬<EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
|
2018-09-07 17:56:03 +08:00
|
|
|
|
image_file = ::LockResource(res_data_handle);
|
2017-10-18 22:13:20 +08:00
|
|
|
|
|
2018-09-07 17:56:03 +08:00
|
|
|
|
hr = image_file ? S_OK : E_FAIL;
|
2018-04-27 00:16:14 +08:00
|
|
|
|
}
|
2018-08-19 20:40:44 +08:00
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
|
2018-09-07 17:56:03 +08:00
|
|
|
|
image_file_size = ::SizeofResource(HINST_THISCOMPONENT, res_handle);
|
2018-08-19 20:40:44 +08:00
|
|
|
|
|
2018-09-07 17:56:03 +08:00
|
|
|
|
hr = image_file_size ? S_OK : E_FAIL;
|
2018-08-19 20:40:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> WIC <20><>
|
2018-09-07 17:56:03 +08:00
|
|
|
|
hr = imaging_factory->CreateStream(&stream);
|
2018-08-19 20:40:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
2018-09-07 17:56:03 +08:00
|
|
|
|
hr = stream->InitializeFromMemory(
|
|
|
|
|
|
reinterpret_cast<BYTE*>(image_file),
|
|
|
|
|
|
image_file_size
|
2018-08-19 20:40:44 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
2018-04-27 00:16:14 +08:00
|
|
|
|
{
|
2018-08-19 20:40:44 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD>
|
2018-09-07 17:56:03 +08:00
|
|
|
|
hr = imaging_factory->CreateDecoderFromStream(
|
|
|
|
|
|
stream,
|
2018-05-17 15:22:14 +08:00
|
|
|
|
nullptr,
|
2018-04-27 00:16:14 +08:00
|
|
|
|
WICDecodeMetadataCacheOnLoad,
|
2018-09-07 17:56:03 +08:00
|
|
|
|
&decoder
|
2018-04-27 00:16:14 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-09-07 17:56:03 +08:00
|
|
|
|
hr = decoder->GetFrame(0, &source);
|
2018-04-27 00:16:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD>
|
2018-09-07 17:56:03 +08:00
|
|
|
|
hr = imaging_factory->CreateFormatConverter(&converter);
|
2018-04-27 00:16:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
// ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD> 32bppPBGRA
|
2018-09-07 17:56:03 +08:00
|
|
|
|
hr = converter->Initialize(
|
|
|
|
|
|
source,
|
2018-04-27 00:16:14 +08:00
|
|
|
|
GUID_WICPixelFormat32bppPBGRA,
|
|
|
|
|
|
WICBitmapDitherTypeNone,
|
2018-05-17 15:22:14 +08:00
|
|
|
|
nullptr,
|
2018-04-27 00:16:14 +08:00
|
|
|
|
0.f,
|
|
|
|
|
|
WICBitmapPaletteTypeMedianCut
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><> WIC λͼ<CEBB><CDBC><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> Direct2D λͼ
|
2018-09-07 17:56:03 +08:00
|
|
|
|
hr = render_target->CreateBitmapFromWicBitmap(
|
|
|
|
|
|
converter,
|
2018-08-19 20:40:44 +08:00
|
|
|
|
nullptr,
|
2018-09-07 17:56:03 +08:00
|
|
|
|
&bitmap
|
2018-08-19 20:40:44 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-09-07 17:56:03 +08:00
|
|
|
|
bitmap_cache_.insert(std::make_pair(res.id, bitmap));
|
2018-08-19 20:40:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
|
2018-09-07 17:56:03 +08:00
|
|
|
|
SafeRelease(decoder);
|
|
|
|
|
|
SafeRelease(source);
|
|
|
|
|
|
SafeRelease(stream);
|
|
|
|
|
|
SafeRelease(converter);
|
2018-08-19 20:40:44 +08:00
|
|
|
|
|
|
|
|
|
|
return SUCCEEDED(hr);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
bool easy2d::Image::CacheBitmap(const String & file_name)
|
2018-08-19 20:40:44 +08:00
|
|
|
|
{
|
2018-09-16 16:26:42 +08:00
|
|
|
|
size_t hash = file_name.GetHash();
|
|
|
|
|
|
if (bitmap_cache_.find(hash) != bitmap_cache_.end())
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
2018-09-07 17:56:03 +08:00
|
|
|
|
File image_file;
|
|
|
|
|
|
if (!image_file.Open(file_name))
|
2018-08-19 20:40:44 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
2018-09-07 17:56:03 +08:00
|
|
|
|
// <20>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8> File::AddSearchPath <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
// Ĭ<><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫͨ<D2AA><CDA8> File::GetPath <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
|
|
|
|
|
|
String image_file_path = image_file.GetPath();
|
2018-08-19 20:40:44 +08:00
|
|
|
|
|
2018-10-06 10:25:29 +08:00
|
|
|
|
Graphics * graphics_device = Device::GetGraphics();
|
|
|
|
|
|
IWICImagingFactory *imaging_factory = graphics_device->GetImagingFactory();
|
|
|
|
|
|
ID2D1HwndRenderTarget* render_target = graphics_device->GetRenderTarget();
|
2018-09-07 23:47:21 +08:00
|
|
|
|
IWICBitmapDecoder *decoder = nullptr;
|
2018-09-07 17:56:03 +08:00
|
|
|
|
IWICBitmapFrameDecode *source = nullptr;
|
|
|
|
|
|
IWICStream *stream = nullptr;
|
|
|
|
|
|
IWICFormatConverter *converter = nullptr;
|
|
|
|
|
|
ID2D1Bitmap *bitmap = nullptr;
|
2018-08-19 20:40:44 +08:00
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-09-07 17:56:03 +08:00
|
|
|
|
HRESULT hr = imaging_factory->CreateDecoderFromFilename(
|
|
|
|
|
|
(LPCWSTR)image_file_path,
|
2018-08-19 20:40:44 +08:00
|
|
|
|
nullptr,
|
|
|
|
|
|
GENERIC_READ,
|
|
|
|
|
|
WICDecodeMetadataCacheOnLoad,
|
2018-09-07 17:56:03 +08:00
|
|
|
|
&decoder
|
2018-08-19 20:40:44 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2018-09-07 17:56:03 +08:00
|
|
|
|
hr = decoder->GetFrame(0, &source);
|
2018-08-19 20:40:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD>
|
2018-09-07 17:56:03 +08:00
|
|
|
|
hr = imaging_factory->CreateFormatConverter(&converter);
|
2018-08-19 20:40:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
// ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD> 32bppPBGRA
|
2018-09-07 17:56:03 +08:00
|
|
|
|
hr = converter->Initialize(
|
|
|
|
|
|
source,
|
2018-08-19 20:40:44 +08:00
|
|
|
|
GUID_WICPixelFormat32bppPBGRA,
|
|
|
|
|
|
WICBitmapDitherTypeNone,
|
|
|
|
|
|
nullptr,
|
|
|
|
|
|
0.f,
|
|
|
|
|
|
WICBitmapPaletteTypeMedianCut
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><> WIC λͼ<CEBB><CDBC><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> Direct2D λͼ
|
2018-09-07 17:56:03 +08:00
|
|
|
|
hr = render_target->CreateBitmapFromWicBitmap(
|
|
|
|
|
|
converter,
|
2018-05-17 15:22:14 +08:00
|
|
|
|
nullptr,
|
2018-09-07 17:56:03 +08:00
|
|
|
|
&bitmap
|
2018-04-27 00:16:14 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
2018-09-07 17:56:03 +08:00
|
|
|
|
bitmap_cache_.insert(std::make_pair(hash, bitmap));
|
2018-04-27 00:16:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <20>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
|
2018-09-07 17:56:03 +08:00
|
|
|
|
SafeRelease(decoder);
|
|
|
|
|
|
SafeRelease(source);
|
|
|
|
|
|
SafeRelease(stream);
|
|
|
|
|
|
SafeRelease(converter);
|
2018-04-27 00:16:14 +08:00
|
|
|
|
|
|
|
|
|
|
return SUCCEEDED(hr);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
void easy2d::Image::ClearCache()
|
2017-10-18 22:13:20 +08:00
|
|
|
|
{
|
2018-09-04 22:42:34 +08:00
|
|
|
|
if (bitmap_cache_.empty())
|
2018-07-21 22:57:21 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
for (const auto& bitmap : bitmap_cache_)
|
2018-04-27 00:16:14 +08:00
|
|
|
|
{
|
2018-07-21 22:57:21 +08:00
|
|
|
|
bitmap.second->Release();
|
2018-04-27 00:16:14 +08:00
|
|
|
|
}
|
2018-09-04 22:42:34 +08:00
|
|
|
|
bitmap_cache_.clear();
|
2018-05-14 00:36:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-16 14:13:15 +08:00
|
|
|
|
void easy2d::Image::SetBitmap(ID2D1Bitmap * bitmap)
|
2018-05-14 00:36:01 +08:00
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
if (bitmap_ == bitmap)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (bitmap_)
|
|
|
|
|
|
{
|
|
|
|
|
|
bitmap_->Release();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-14 00:36:01 +08:00
|
|
|
|
if (bitmap)
|
|
|
|
|
|
{
|
2018-09-07 00:28:54 +08:00
|
|
|
|
bitmap->AddRef();
|
|
|
|
|
|
|
2018-09-04 22:42:34 +08:00
|
|
|
|
bitmap_ = bitmap;
|
|
|
|
|
|
crop_rect_.origin.x = crop_rect_.origin.y = 0;
|
|
|
|
|
|
crop_rect_.size.width = bitmap_->GetSize().width;
|
|
|
|
|
|
crop_rect_.size.height = bitmap_->GetSize().height;
|
2018-05-14 00:36:01 +08:00
|
|
|
|
}
|
2017-10-18 22:13:20 +08:00
|
|
|
|
}
|