| 
									
										
										
										
											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-11-08 21:39:26 +08:00
										 |  |  |  | #include "Image.h"
 | 
					
						
							|  |  |  |  | #include "render.h"
 | 
					
						
							|  |  |  |  | #include "../utils/File.h"
 | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | namespace easy2d | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	namespace | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		std::map<size_t, ID2D1Bitmap*> bitmap_cache_; | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	Image::Image() | 
					
						
							|  |  |  |  | 		: bitmap_(nullptr) | 
					
						
							|  |  |  |  | 		, crop_rect_() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	Image::Image(Resource& res) | 
					
						
							|  |  |  |  | 		: bitmap_(nullptr) | 
					
						
							|  |  |  |  | 		, crop_rect_() | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		this->Load(res); | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	Image::Image(Resource& res, const Rect& crop_rect) | 
					
						
							|  |  |  |  | 		: bitmap_(nullptr) | 
					
						
							|  |  |  |  | 		, crop_rect_() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		this->Load(res); | 
					
						
							|  |  |  |  | 		this->Crop(crop_rect); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	Image::Image(const String & file_name) | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		: bitmap_(nullptr) | 
					
						
							|  |  |  |  | 		, crop_rect_() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		this->Load(file_name); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	Image::Image(const String & file_name, const Rect & crop_rect) | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		: bitmap_(nullptr) | 
					
						
							|  |  |  |  | 		, crop_rect_() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		this->Load(file_name); | 
					
						
							|  |  |  |  | 		this->Crop(crop_rect); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	Image::~Image() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		SafeRelease(bitmap_); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	bool Image::Load(Resource& res) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		if (!Image::CacheBitmap(res)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			E2D_WARNING("Load Image from file failed!"); | 
					
						
							|  |  |  |  | 			return false; | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		this->SetBitmap(bitmap_cache_.at(res.GetHashCode())); | 
					
						
							|  |  |  |  | 		return true; | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	bool Image::Load(const String & file_name) | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		E2D_WARNING_IF(file_name.empty(), "Image Load failed! Invalid file name."); | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		if (file_name.empty()) | 
					
						
							|  |  |  |  | 			return false; | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		if (!Image::CacheBitmap(file_name)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			E2D_WARNING("Load Image from file failed!"); | 
					
						
							|  |  |  |  | 			return false; | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-09-16 16:07:51 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		this->SetBitmap(bitmap_cache_.at(std::hash<String>{}(file_name))); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 		return true; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	void Image::Crop(const Rect& crop_rect) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		if (bitmap_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			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); | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	float Image::GetWidth() const | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		return crop_rect_.size.width; | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	float Image::GetHeight() const | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		return crop_rect_.size.height; | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	Size Image::GetSize() const | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		return crop_rect_.size; | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	float Image::GetSourceWidth() const | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		if (bitmap_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return bitmap_->GetSize().width; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		else | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return 0; | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	float Image::GetSourceHeight() const | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		if (bitmap_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return bitmap_->GetSize().height; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		else | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return 0; | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	Size Image::GetSourceSize() const | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		if (bitmap_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			auto bitmap_size = bitmap_->GetSize(); | 
					
						
							|  |  |  |  | 			return Size{ bitmap_size.width, bitmap_size.height }; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		return Size{}; | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	float Image::GetCropX() const | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		return crop_rect_.origin.x; | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	float Image::GetCropY() const | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		return crop_rect_.origin.y; | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	Point Image::GetCropPos() const | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		return crop_rect_.origin; | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	const Rect & Image::GetCropRect() const | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		return crop_rect_; | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	ID2D1Bitmap * Image::GetBitmap() const | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		return bitmap_; | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	bool Image::CacheBitmap(Resource& res) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		size_t hash_code = res.GetHashCode(); | 
					
						
							|  |  |  |  | 		if (bitmap_cache_.find(hash_code) != bitmap_cache_.end()) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			return true; | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		HRESULT hr; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		HINSTANCE				hinstance = GetModuleHandle(nullptr); | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		IWICImagingFactory*		imaging_factory = render::D2D.WICImagingFactory; | 
					
						
							|  |  |  |  | 		ID2D1HwndRenderTarget*	render_target = render::D2D.HwndRenderTarget; | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		IWICBitmapDecoder*		decoder = nullptr; | 
					
						
							|  |  |  |  | 		IWICBitmapFrameDecode*	source = nullptr; | 
					
						
							|  |  |  |  | 		IWICStream*				stream = nullptr; | 
					
						
							|  |  |  |  | 		IWICFormatConverter*	converter = nullptr; | 
					
						
							|  |  |  |  | 		ID2D1Bitmap*			bitmap = nullptr; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
 | 
					
						
							|  |  |  |  | 		hr = res.Load() ? S_OK : E_FAIL; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			// <20><><EFBFBD><EFBFBD> WIC <20><>
 | 
					
						
							|  |  |  |  | 			hr = imaging_factory->CreateStream(&stream); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 			hr = stream->InitializeFromMemory( | 
					
						
							|  |  |  |  | 				static_cast<WICInProcPointer>(res.GetData()), | 
					
						
							|  |  |  |  | 				res.GetDataSize() | 
					
						
							|  |  |  |  | 			); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 			hr = imaging_factory->CreateDecoderFromStream( | 
					
						
							|  |  |  |  | 				stream, | 
					
						
							|  |  |  |  | 				nullptr, | 
					
						
							|  |  |  |  | 				WICDecodeMetadataCacheOnLoad, | 
					
						
							|  |  |  |  | 				&decoder | 
					
						
							|  |  |  |  | 			); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 			hr = decoder->GetFrame(0, &source); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			// <20><><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 			hr = imaging_factory->CreateFormatConverter(&converter); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			// ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD> 32bppPBGRA
 | 
					
						
							|  |  |  |  | 			hr = converter->Initialize( | 
					
						
							|  |  |  |  | 				source, | 
					
						
							|  |  |  |  | 				GUID_WICPixelFormat32bppPBGRA, | 
					
						
							|  |  |  |  | 				WICBitmapDitherTypeNone, | 
					
						
							|  |  |  |  | 				nullptr, | 
					
						
							|  |  |  |  | 				0.f, | 
					
						
							|  |  |  |  | 				WICBitmapPaletteTypeMedianCut | 
					
						
							|  |  |  |  | 			); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			// <20><> WIC λͼ<CEBB><CDBC><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> Direct2D λͼ
 | 
					
						
							|  |  |  |  | 			hr = render_target->CreateBitmapFromWicBitmap( | 
					
						
							|  |  |  |  | 				converter, | 
					
						
							|  |  |  |  | 				nullptr, | 
					
						
							|  |  |  |  | 				&bitmap | 
					
						
							|  |  |  |  | 			); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			bitmap_cache_.insert(std::make_pair(hash_code, bitmap)); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		// <20>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
 | 
					
						
							|  |  |  |  | 		SafeRelease(decoder); | 
					
						
							|  |  |  |  | 		SafeRelease(source); | 
					
						
							|  |  |  |  | 		SafeRelease(stream); | 
					
						
							|  |  |  |  | 		SafeRelease(converter); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		return SUCCEEDED(hr); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 	bool Image::CacheBitmap(const String & file_name) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		size_t hash_code = std::hash<String>{}(file_name); | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		if (bitmap_cache_.find(hash_code) != bitmap_cache_.end()) | 
					
						
							|  |  |  |  | 			return true; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		File image_file; | 
					
						
							|  |  |  |  | 		if (!image_file.Open(file_name)) | 
					
						
							|  |  |  |  | 			return false; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		// <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>
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		String image_file_path = image_file.GetPath(); | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  |  | 		IWICImagingFactory*		imaging_factory = render::D2D.WICImagingFactory; | 
					
						
							|  |  |  |  | 		ID2D1HwndRenderTarget*	render_target = render::D2D.HwndRenderTarget; | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		IWICBitmapDecoder*		decoder = nullptr; | 
					
						
							|  |  |  |  | 		IWICBitmapFrameDecode*	source = nullptr; | 
					
						
							|  |  |  |  | 		IWICStream*				stream = nullptr; | 
					
						
							|  |  |  |  | 		IWICFormatConverter*	converter = nullptr; | 
					
						
							|  |  |  |  | 		ID2D1Bitmap*			bitmap = nullptr; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 		HRESULT hr = imaging_factory->CreateDecoderFromFilename( | 
					
						
							|  |  |  |  | 			image_file_path.c_str(), | 
					
						
							|  |  |  |  | 			nullptr, | 
					
						
							|  |  |  |  | 			GENERIC_READ, | 
					
						
							|  |  |  |  | 			WICDecodeMetadataCacheOnLoad, | 
					
						
							|  |  |  |  | 			&decoder | 
					
						
							|  |  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2018-05-14 00:36:01 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 			hr = decoder->GetFrame(0, &source); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			// <20><><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 			hr = imaging_factory->CreateFormatConverter(&converter); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			// ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD> 32bppPBGRA
 | 
					
						
							|  |  |  |  | 			hr = converter->Initialize( | 
					
						
							|  |  |  |  | 				source, | 
					
						
							|  |  |  |  | 				GUID_WICPixelFormat32bppPBGRA, | 
					
						
							|  |  |  |  | 				WICBitmapDitherTypeNone, | 
					
						
							|  |  |  |  | 				nullptr, | 
					
						
							|  |  |  |  | 				0.f, | 
					
						
							|  |  |  |  | 				WICBitmapPaletteTypeMedianCut | 
					
						
							|  |  |  |  | 			); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			// <20><> WIC λͼ<CEBB><CDBC><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> Direct2D λͼ
 | 
					
						
							|  |  |  |  | 			hr = render_target->CreateBitmapFromWicBitmap( | 
					
						
							|  |  |  |  | 				converter, | 
					
						
							|  |  |  |  | 				nullptr, | 
					
						
							|  |  |  |  | 				&bitmap | 
					
						
							|  |  |  |  | 			); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			bitmap_cache_.insert(std::make_pair(hash_code, bitmap)); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		// <20>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
 | 
					
						
							|  |  |  |  | 		SafeRelease(decoder); | 
					
						
							|  |  |  |  | 		SafeRelease(source); | 
					
						
							|  |  |  |  | 		SafeRelease(stream); | 
					
						
							|  |  |  |  | 		SafeRelease(converter); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		return SUCCEEDED(hr); | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-07 00:28:54 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	void Image::ClearCache() | 
					
						
							| 
									
										
										
										
											2018-09-07 00:28:54 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		if (bitmap_cache_.empty()) | 
					
						
							|  |  |  |  | 			return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		for (const auto& bitmap : bitmap_cache_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			bitmap.second->Release(); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 		bitmap_cache_.clear(); | 
					
						
							| 
									
										
										
										
											2018-09-07 00:28:54 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 	void Image::SetBitmap(ID2D1Bitmap * bitmap) | 
					
						
							| 
									
										
										
										
											2018-05-14 00:36:01 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | 		if (bitmap_ == bitmap) | 
					
						
							|  |  |  |  | 			return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (bitmap_) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			bitmap_->Release(); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if (bitmap) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							|  |  |  |  | 			bitmap->AddRef(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			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
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  |  | } |