| 
									
										
										
										
											2018-05-14 00:36:01 +08:00
										 |  |  |  | #include "..\e2dcommon.h"
 | 
					
						
							|  |  |  |  | #include "..\e2dbase.h"
 | 
					
						
							| 
									
										
										
										
											2018-05-24 15:47:38 +08:00
										 |  |  |  | #include "..\e2dtool.h"
 | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | std::map<size_t, ID2D1Bitmap*> e2d::Image::_bitmapCache; | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Image::Image() | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	: _bitmap(nullptr) | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 	, _cropRect() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | e2d::Image::Image(const Resource& res) | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	: _bitmap(nullptr) | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 	, _cropRect() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	this->open(res); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | e2d::Image::Image(const Resource& res, const Rect& cropRect) | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	: _bitmap(nullptr) | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 	, _cropRect() | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	this->open(res); | 
					
						
							|  |  |  |  | 	this->crop(cropRect); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | e2d::Image::Image(const String & fileName) | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	: _bitmap(nullptr) | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 	, _cropRect() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	this->open(fileName); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | e2d::Image::Image(const String & fileName, const Rect & cropRect) | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	: _bitmap(nullptr) | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 	, _cropRect() | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	this->open(fileName); | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 	this->crop(cropRect); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | e2d::Image::~Image() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | bool e2d::Image::open(const Resource& res) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 	if (!Image::preload(res)) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-05-24 12:24:39 +08:00
										 |  |  |  | 		WARN("Load Image from file failed!"); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	this->_setBitmap(_bitmapCache.at(res.resNameId)); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	return true; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | bool e2d::Image::open(const String & fileName) | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | 	WARN_IF(fileName.isEmpty(), "Image open failed! Invalid file name."); | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | 	if (fileName.isEmpty()) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 		return false; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (!Image::preload(fileName)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		WARN("Load Image from file failed!"); | 
					
						
							|  |  |  |  | 		return false; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	this->_setBitmap(_bitmapCache.at(fileName.hash())); | 
					
						
							|  |  |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | void e2d::Image::crop(const Rect& cropRect) | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	if (_bitmap) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | 		_cropRect.origin.x = std::min(std::max(cropRect.origin.x, 0.f), this->getSourceWidth()); | 
					
						
							|  |  |  |  | 		_cropRect.origin.y = std::min(std::max(cropRect.origin.y, 0.f), this->getSourceHeight()); | 
					
						
							|  |  |  |  | 		_cropRect.size.width = std::min(std::max(cropRect.size.width, 0.f), this->getSourceWidth() - cropRect.origin.x); | 
					
						
							|  |  |  |  | 		_cropRect.size.height = std::min(std::max(cropRect.size.height, 0.f), this->getSourceHeight() - cropRect.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-08-23 16:37:51 +08:00
										 |  |  |  | float e2d::Image::getWidth() const | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 	return _cropRect.size.width; | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | float e2d::Image::getHeight() const | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 	return _cropRect.size.height; | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | e2d::Size e2d::Image::getSize() const | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 	return _cropRect.size; | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | float e2d::Image::getSourceWidth() const | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	if (_bitmap) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 		return _bitmap->GetSize().width; | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 	else | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return 0; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | float e2d::Image::getSourceHeight() const | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	if (_bitmap) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 		return _bitmap->GetSize().height; | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 	else | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return 0; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | e2d::Size e2d::Image::getSourceSize() const | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	if (_bitmap) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | 		return Size(getSourceWidth(), getSourceHeight()); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 	else | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | 		return Size(); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | float e2d::Image::getCropX() const | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 	return _cropRect.origin.x; | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | float e2d::Image::getCropY() const | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 	return _cropRect.origin.y; | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | e2d::Point e2d::Image::getCropPos() const | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 	return _cropRect.origin; | 
					
						
							| 
									
										
										
										
											2018-02-03 22:04:43 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | bool e2d::Image::preload(const Resource& res) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	if (_bitmapCache.find(res.resNameId) != _bitmapCache.end()) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		return true; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 00:06:10 +08:00
										 |  |  |  | 	IWICImagingFactory *pImagingFactory = Renderer::getImagingFactory(); | 
					
						
							|  |  |  |  | 	ID2D1HwndRenderTarget* pRenderTarget = Renderer::getInstance()->getRenderTarget(); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 	IWICBitmapDecoder *pDecoder = nullptr; | 
					
						
							|  |  |  |  | 	IWICBitmapFrameDecode *pSource = nullptr; | 
					
						
							|  |  |  |  | 	IWICStream *pStream = nullptr; | 
					
						
							|  |  |  |  | 	IWICFormatConverter *pConverter = nullptr; | 
					
						
							|  |  |  |  | 	ID2D1Bitmap *pBitmap = nullptr; | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	HRSRC imageResHandle = nullptr; | 
					
						
							|  |  |  |  | 	HGLOBAL imageResDataHandle = nullptr; | 
					
						
							|  |  |  |  | 	void *pImageFile = nullptr; | 
					
						
							|  |  |  |  | 	DWORD imageFileSize = 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// <20><>λ<EFBFBD><CEBB>Դ
 | 
					
						
							|  |  |  |  | 	imageResHandle = ::FindResourceW( | 
					
						
							|  |  |  |  | 		HINST_THISCOMPONENT,  | 
					
						
							|  |  |  |  | 		MAKEINTRESOURCE(res.resNameId),  | 
					
						
							|  |  |  |  | 		(LPCWSTR)res.resType | 
					
						
							|  |  |  |  | 	); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	HRESULT hr = imageResHandle ? S_OK : E_FAIL; | 
					
						
							|  |  |  |  | 	if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
 | 
					
						
							|  |  |  |  | 		imageResDataHandle = ::LoadResource(HINST_THISCOMPONENT, imageResHandle); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		hr = imageResDataHandle ? S_OK : E_FAIL; | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											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>Դ
 | 
					
						
							|  |  |  |  | 		pImageFile = ::LockResource(imageResDataHandle); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 		hr = pImageFile ? 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>С
 | 
					
						
							|  |  |  |  | 		imageFileSize = ::SizeofResource(HINST_THISCOMPONENT, imageResHandle); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		hr = imageFileSize ? S_OK : E_FAIL; | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD><EFBFBD> WIC <20><>
 | 
					
						
							|  |  |  |  | 		hr = pImagingFactory->CreateStream(&pStream); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 		hr = pStream->InitializeFromMemory( | 
					
						
							|  |  |  |  | 			reinterpret_cast<BYTE*>(pImageFile), | 
					
						
							|  |  |  |  | 			imageFileSize | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	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>
 | 
					
						
							|  |  |  |  | 		hr = pImagingFactory->CreateDecoderFromStream( | 
					
						
							|  |  |  |  | 			pStream, | 
					
						
							| 
									
										
										
										
											2018-05-17 15:22:14 +08:00
										 |  |  |  | 			nullptr, | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 			WICDecodeMetadataCacheOnLoad, | 
					
						
							|  |  |  |  | 			&pDecoder | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 		hr = pDecoder->GetFrame(0, &pSource); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD>
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  |  | 		hr = pImagingFactory->CreateFormatConverter(&pConverter); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		// ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD> 32bppPBGRA
 | 
					
						
							|  |  |  |  | 		hr = pConverter->Initialize( | 
					
						
							|  |  |  |  | 			pSource, | 
					
						
							|  |  |  |  | 			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-08-19 20:40:44 +08:00
										 |  |  |  | 		hr = pRenderTarget->CreateBitmapFromWicBitmap( | 
					
						
							|  |  |  |  | 			pConverter, | 
					
						
							|  |  |  |  | 			nullptr, | 
					
						
							|  |  |  |  | 			&pBitmap | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		_bitmapCache.insert(std::make_pair(res.resNameId, pBitmap)); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// <20>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
 | 
					
						
							|  |  |  |  | 	SafeRelease(pDecoder); | 
					
						
							|  |  |  |  | 	SafeRelease(pSource); | 
					
						
							|  |  |  |  | 	SafeRelease(pStream); | 
					
						
							|  |  |  |  | 	SafeRelease(pConverter); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	return SUCCEEDED(hr); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool e2d::Image::preload(const String & fileName) | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-23 16:58:32 +08:00
										 |  |  |  | 	String actualFilePath = File(fileName).getPath(); | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | 	if (actualFilePath.isEmpty()) | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 		return false; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	size_t hash = actualFilePath.hash(); | 
					
						
							|  |  |  |  | 	if (_bitmapCache.find(hash) != _bitmapCache.end()) | 
					
						
							|  |  |  |  | 		return true; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-28 00:06:10 +08:00
										 |  |  |  | 	IWICImagingFactory *pImagingFactory = Renderer::getImagingFactory(); | 
					
						
							|  |  |  |  | 	ID2D1HwndRenderTarget* pRenderTarget = Renderer::getInstance()->getRenderTarget(); | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 	IWICBitmapDecoder *pDecoder = nullptr; | 
					
						
							|  |  |  |  | 	IWICBitmapFrameDecode *pSource = nullptr; | 
					
						
							|  |  |  |  | 	IWICStream *pStream = nullptr; | 
					
						
							|  |  |  |  | 	IWICFormatConverter *pConverter = nullptr; | 
					
						
							|  |  |  |  | 	ID2D1Bitmap *pBitmap = nullptr; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 	HRESULT hr = pImagingFactory->CreateDecoderFromFilename( | 
					
						
							|  |  |  |  | 		(LPCWSTR)actualFilePath, | 
					
						
							|  |  |  |  | 		nullptr, | 
					
						
							|  |  |  |  | 		GENERIC_READ, | 
					
						
							|  |  |  |  | 		WICDecodeMetadataCacheOnLoad, | 
					
						
							|  |  |  |  | 		&pDecoder | 
					
						
							|  |  |  |  | 	); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 		hr = pDecoder->GetFrame(0, &pSource); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD><EFBFBD>ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD>
 | 
					
						
							|  |  |  |  | 		hr = pImagingFactory->CreateFormatConverter(&pConverter); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		// ͼƬ<CDBC><C6AC>ʽת<CABD><D7AA><EFBFBD><EFBFBD> 32bppPBGRA
 | 
					
						
							|  |  |  |  | 		hr = pConverter->Initialize( | 
					
						
							|  |  |  |  | 			pSource, | 
					
						
							|  |  |  |  | 			GUID_WICPixelFormat32bppPBGRA, | 
					
						
							|  |  |  |  | 			WICBitmapDitherTypeNone, | 
					
						
							|  |  |  |  | 			nullptr, | 
					
						
							|  |  |  |  | 			0.f, | 
					
						
							|  |  |  |  | 			WICBitmapPaletteTypeMedianCut | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		// <20><> WIC λͼ<CEBB><CDBC><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> Direct2D λͼ
 | 
					
						
							|  |  |  |  | 		hr = pRenderTarget->CreateBitmapFromWicBitmap( | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 			pConverter, | 
					
						
							| 
									
										
										
										
											2018-05-17 15:22:14 +08:00
										 |  |  |  | 			nullptr, | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 			&pBitmap | 
					
						
							|  |  |  |  | 		); | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if (SUCCEEDED(hr)) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-08-19 20:40:44 +08:00
										 |  |  |  | 		_bitmapCache.insert(std::make_pair(hash, pBitmap)); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// <20>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
 | 
					
						
							| 
									
										
										
										
											2018-05-22 12:24:43 +08:00
										 |  |  |  | 	SafeRelease(pDecoder); | 
					
						
							|  |  |  |  | 	SafeRelease(pSource); | 
					
						
							|  |  |  |  | 	SafeRelease(pStream); | 
					
						
							|  |  |  |  | 	SafeRelease(pConverter); | 
					
						
							| 
									
										
										
										
											2018-04-27 00:16:14 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	return SUCCEEDED(hr); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-07 16:37:12 +08:00
										 |  |  |  | void e2d::Image::clearCache() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-21 22:57:21 +08:00
										 |  |  |  | 	if (_bitmapCache.empty()) | 
					
						
							|  |  |  |  | 		return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-12 14:30:28 +08:00
										 |  |  |  | 	for (const auto& bitmap : _bitmapCache) | 
					
						
							| 
									
										
										
										
											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-07-08 02:48:04 +08:00
										 |  |  |  | 	_bitmapCache.clear(); | 
					
						
							| 
									
										
										
										
											2018-05-14 00:36:01 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void e2d::Image::_setBitmap(ID2D1Bitmap * bitmap) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | 	if (bitmap) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		_bitmap = bitmap; | 
					
						
							| 
									
										
										
										
											2018-05-22 23:36:46 +08:00
										 |  |  |  | 		_cropRect.origin.x = _cropRect.origin.y = 0; | 
					
						
							|  |  |  |  | 		_cropRect.size.width = _bitmap->GetSize().width; | 
					
						
							|  |  |  |  | 		_cropRect.size.height = _bitmap->GetSize().height; | 
					
						
							| 
									
										
										
										
											2018-05-14 00:36:01 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-23 16:37:51 +08:00
										 |  |  |  | ID2D1Bitmap * e2d::Image::getBitmap() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-09 00:34:15 +08:00
										 |  |  |  | 	return _bitmap; | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  |  | } |