| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2018-11-12 02:10:35 +08:00
										 |  |  | #include "logs.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | #include "../utils/File.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-23 14:55:03 +08:00
										 |  |  | #include "../utils/string.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 00:21:59 +08:00
										 |  |  | 	Image::Image() | 
					
						
							|  |  |  | 		: bitmap_(nullptr) | 
					
						
							|  |  |  | 		, crop_rect_() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 	Image::Image(Resource const& res) | 
					
						
							| 
									
										
										
										
											2018-11-18 20:26:41 +08:00
										 |  |  | 		: Image() | 
					
						
							| 
									
										
										
										
											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-17 17:15:32 +08:00
										 |  |  | 	Image::Image(Resource const& res, Rect const& crop_rect) | 
					
						
							| 
									
										
										
										
											2018-11-18 20:26:41 +08:00
										 |  |  | 		: Image() | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 	Image::Image(D2DBitmapPtr const & bitmap) | 
					
						
							| 
									
										
										
										
											2018-11-18 20:26:41 +08:00
										 |  |  | 		: Image() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		SetBitmap(bitmap); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	Image::~Image() | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 	bool Image::Load(Resource const& res) | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 		HRESULT hr = S_OK; | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 		D2DBitmapPtr bitmap; | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-07 23:54:19 +08:00
										 |  |  | 		if (res.IsFileType()) | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 			if (!File(res.GetFileName()).Exists()) | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2019-02-09 00:03:24 +08:00
										 |  |  | 				E2D_WARNING_LOG(L"Image file '%s' not found!", res.GetFileName().c_str()); | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 				return false; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 			hr = RenderSystem::Instance()->CreateBitmapFromFile(bitmap, res.GetFileName()); | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-01-24 12:21:01 +08:00
										 |  |  | 			hr = RenderSystem::Instance()->CreateBitmapFromResource(bitmap, res); | 
					
						
							| 
									
										
										
										
											2018-11-12 20:46:54 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (FAILED(hr)) | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-02-07 21:31:30 +08:00
										 |  |  | 			E2D_ERROR_HR_LOG(hr, L"Load image file failed!"); | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-18 20:26:41 +08:00
										 |  |  | 		SetBitmap(bitmap); | 
					
						
							| 
									
										
										
										
											2017-10-18 22:13:20 +08:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-30 21:20:23 +08:00
										 |  |  | 	bool Image::IsValid() const | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return !!bitmap_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 17:15:32 +08:00
										 |  |  | 	void Image::Crop(Rect const& 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); | 
					
						
							| 
									
										
										
										
											2018-11-25 15:00:15 +08:00
										 |  |  | 			crop_rect_.size.x = std::min(std::max(crop_rect.size.x, 0.f), bitmap_size.width - crop_rect.origin.x); | 
					
						
							|  |  |  | 			crop_rect_.size.y = std::min(std::max(crop_rect.size.y, 0.f), bitmap_size.height - crop_rect.origin.y); | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											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-25 15:00:15 +08:00
										 |  |  | 		return crop_rect_.size.x; | 
					
						
							| 
									
										
										
										
											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-25 15:00:15 +08:00
										 |  |  | 		return crop_rect_.size.y; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-25 15:00:15 +08:00
										 |  |  | 		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; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-25 15:00:15 +08:00
										 |  |  | 		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-17 17:15:32 +08:00
										 |  |  | 	Rect const& 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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 	D2DBitmapPtr const& 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-17 23:24:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 	void Image::SetBitmap(D2DBitmapPtr const & bitmap) | 
					
						
							| 
									
										
										
										
											2018-11-18 20:26:41 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (bitmap) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			bitmap_ = bitmap; | 
					
						
							|  |  |  | 			crop_rect_.origin.x = crop_rect_.origin.y = 0; | 
					
						
							| 
									
										
										
										
											2018-11-25 15:00:15 +08:00
										 |  |  | 			crop_rect_.size.x = bitmap_->GetSize().width; | 
					
						
							|  |  |  | 			crop_rect_.size.y = bitmap_->GetSize().height; | 
					
						
							| 
									
										
										
										
											2018-11-18 20:26:41 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | } |