| 
									
										
										
										
											2015-02-11 16:03:19 +08:00
										 |  |  | // Copyright (c) 2015 GitHub, Inc.
 | 
					
						
							|  |  |  | // Use of this source code is governed by the MIT license that can be
 | 
					
						
							|  |  |  | // found in the LICENSE file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "atom/common/api/atom_api_native_image.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 21:59:08 +08:00
										 |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 19:34:21 +08:00
										 |  |  | #include "atom/common/asar/asar_util.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-11 18:41:06 +08:00
										 |  |  | #include "atom/common/native_mate_converters/file_path_converter.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-11 16:49:43 +08:00
										 |  |  | #include "atom/common/native_mate_converters/gfx_converter.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-12 14:27:53 +08:00
										 |  |  | #include "atom/common/native_mate_converters/gurl_converter.h"
 | 
					
						
							| 
									
										
										
										
											2015-08-20 19:26:20 +03:00
										 |  |  | #include "atom/common/node_includes.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-12 14:32:51 +08:00
										 |  |  | #include "base/base64.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-11 18:41:06 +08:00
										 |  |  | #include "base/strings/string_util.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-02 15:16:49 +08:00
										 |  |  | #include "base/strings/pattern.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-11 16:03:19 +08:00
										 |  |  | #include "native_mate/dictionary.h"
 | 
					
						
							|  |  |  | #include "native_mate/object_template_builder.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-12 14:27:53 +08:00
										 |  |  | #include "net/base/data_url.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-11 18:41:06 +08:00
										 |  |  | #include "ui/base/layout.h"
 | 
					
						
							|  |  |  | #include "ui/gfx/codec/jpeg_codec.h"
 | 
					
						
							|  |  |  | #include "ui/gfx/codec/png_codec.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-11 16:49:43 +08:00
										 |  |  | #include "ui/gfx/geometry/size.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-11 18:41:06 +08:00
										 |  |  | #include "ui/gfx/image/image_skia.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-11 17:47:54 +08:00
										 |  |  | #include "ui/gfx/image/image_util.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-11 16:03:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-08 17:58:05 +03:00
										 |  |  | #if defined(OS_WIN)
 | 
					
						
							| 
									
										
										
										
											2015-08-08 21:57:29 +03:00
										 |  |  | #include "atom/common/asar/archive.h"
 | 
					
						
							| 
									
										
										
										
											2015-08-20 19:26:20 +03:00
										 |  |  | #include "base/win/scoped_gdi_object.h"
 | 
					
						
							| 
									
										
										
										
											2015-08-08 17:58:05 +03:00
										 |  |  | #include "ui/gfx/icon_util.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-02-11 16:03:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace atom { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace api { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 18:41:06 +08:00
										 |  |  | struct ScaleFactorPair { | 
					
						
							|  |  |  |   const char* name; | 
					
						
							|  |  |  |   float scale; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ScaleFactorPair kScaleFactorPairs[] = { | 
					
						
							|  |  |  |   // The "@2x" is put as first one to make scale matching faster.
 | 
					
						
							|  |  |  |   { "@2x"    , 2.0f }, | 
					
						
							|  |  |  |   { "@3x"    , 3.0f }, | 
					
						
							|  |  |  |   { "@1x"    , 1.0f }, | 
					
						
							|  |  |  |   { "@4x"    , 4.0f }, | 
					
						
							|  |  |  |   { "@5x"    , 5.0f }, | 
					
						
							|  |  |  |   { "@1.25x" , 1.25f }, | 
					
						
							|  |  |  |   { "@1.33x" , 1.33f }, | 
					
						
							|  |  |  |   { "@1.4x"  , 1.4f }, | 
					
						
							|  |  |  |   { "@1.5x"  , 1.5f }, | 
					
						
							|  |  |  |   { "@1.8x"  , 1.8f }, | 
					
						
							|  |  |  |   { "@2.5x"  , 2.5f }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | float GetScaleFactorFromPath(const base::FilePath& path) { | 
					
						
							|  |  |  |   std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // We don't try to convert string to float here because it is very very
 | 
					
						
							|  |  |  |   // expensive.
 | 
					
						
							|  |  |  |   for (unsigned i = 0; i < arraysize(kScaleFactorPairs); ++i) { | 
					
						
							| 
									
										
										
										
											2015-12-07 19:56:23 +08:00
										 |  |  |     if (base::EndsWith(filename, kScaleFactorPairs[i].name, | 
					
						
							|  |  |  |                        base::CompareCase::INSENSITIVE_ASCII)) | 
					
						
							| 
									
										
										
										
											2015-02-11 18:41:06 +08:00
										 |  |  |       return kScaleFactorPairs[i].scale; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 1.0f; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool AddImageSkiaRep(gfx::ImageSkia* image, | 
					
						
							| 
									
										
										
										
											2015-02-12 15:19:05 +08:00
										 |  |  |                      const unsigned char* data, | 
					
						
							|  |  |  |                      size_t size, | 
					
						
							| 
									
										
										
										
											2015-02-11 18:41:06 +08:00
										 |  |  |                      double scale_factor) { | 
					
						
							|  |  |  |   scoped_ptr<SkBitmap> decoded(new SkBitmap()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Try PNG first.
 | 
					
						
							|  |  |  |   if (!gfx::PNGCodec::Decode(data, size, decoded.get())) | 
					
						
							|  |  |  |     // Try JPEG.
 | 
					
						
							|  |  |  |     decoded.reset(gfx::JPEGCodec::Decode(data, size)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!decoded) | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-05 15:49:37 +08:00
										 |  |  |   image->AddRepresentation(gfx::ImageSkiaRep(*decoded, scale_factor)); | 
					
						
							| 
									
										
										
										
											2015-02-11 18:41:06 +08:00
										 |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 15:19:05 +08:00
										 |  |  | bool AddImageSkiaRep(gfx::ImageSkia* image, | 
					
						
							|  |  |  |                      const base::FilePath& path, | 
					
						
							|  |  |  |                      double scale_factor) { | 
					
						
							|  |  |  |   std::string file_contents; | 
					
						
							| 
									
										
										
										
											2015-02-12 19:34:21 +08:00
										 |  |  |   if (!asar::ReadFileToString(path, &file_contents)) | 
					
						
							| 
									
										
										
										
											2015-02-12 15:19:05 +08:00
										 |  |  |     return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const unsigned char* data = | 
					
						
							|  |  |  |       reinterpret_cast<const unsigned char*>(file_contents.data()); | 
					
						
							|  |  |  |   size_t size = file_contents.size(); | 
					
						
							|  |  |  |   return AddImageSkiaRep(image, data, size, scale_factor); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 18:41:06 +08:00
										 |  |  | bool PopulateImageSkiaRepsFromPath(gfx::ImageSkia* image, | 
					
						
							|  |  |  |                                    const base::FilePath& path) { | 
					
						
							|  |  |  |   bool succeed = false; | 
					
						
							|  |  |  |   std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe()); | 
					
						
							| 
									
										
										
										
											2015-09-02 15:16:49 +08:00
										 |  |  |   if (base::MatchPattern(filename, "*@*x")) | 
					
						
							| 
									
										
										
										
											2015-02-11 18:41:06 +08:00
										 |  |  |     // Don't search for other representations if the DPI has been specified.
 | 
					
						
							|  |  |  |     return AddImageSkiaRep(image, path, GetScaleFactorFromPath(path)); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     succeed |= AddImageSkiaRep(image, path, 1.0f); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (const ScaleFactorPair& pair : kScaleFactorPairs) | 
					
						
							|  |  |  |     succeed |= AddImageSkiaRep(image, | 
					
						
							|  |  |  |                                path.InsertBeforeExtensionASCII(pair.name), | 
					
						
							|  |  |  |                                pair.scale); | 
					
						
							|  |  |  |   return succeed; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-02-12 12:24:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(OS_MACOSX)
 | 
					
						
							| 
									
										
										
										
											2015-07-26 21:58:48 -07:00
										 |  |  | bool IsTemplateFilename(const base::FilePath& path) { | 
					
						
							| 
									
										
										
										
											2015-09-02 15:16:49 +08:00
										 |  |  |   return (base::MatchPattern(path.value(), "*Template.*") || | 
					
						
							|  |  |  |           base::MatchPattern(path.value(), "*Template@*x.*")); | 
					
						
							| 
									
										
										
										
											2015-02-12 12:24:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-02-11 18:41:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-21 12:06:38 +08:00
										 |  |  | #if defined(OS_WIN)
 | 
					
						
							|  |  |  | bool ReadImageSkiaFromICO(gfx::ImageSkia* image, const base::FilePath& path) { | 
					
						
							|  |  |  |   // If file is in asar archive, we extract it to a temp file so LoadImage can
 | 
					
						
							|  |  |  |   // load it.
 | 
					
						
							|  |  |  |   base::FilePath asar_path, relative_path; | 
					
						
							|  |  |  |   base::FilePath image_path(path); | 
					
						
							|  |  |  |   if (asar::GetAsarArchivePath(image_path, &asar_path, &relative_path)) { | 
					
						
							|  |  |  |     std::shared_ptr<asar::Archive> archive = | 
					
						
							|  |  |  |         asar::GetOrCreateAsarArchive(asar_path); | 
					
						
							|  |  |  |     if (archive) | 
					
						
							|  |  |  |       archive->CopyFileOut(relative_path, &image_path); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Load the icon from file.
 | 
					
						
							|  |  |  |   base::win::ScopedHICON icon(static_cast<HICON>( | 
					
						
							|  |  |  |       LoadImage(NULL, image_path.value().c_str(), IMAGE_ICON, 0, 0, | 
					
						
							|  |  |  |                 LR_DEFAULTSIZE | LR_LOADFROMFILE))); | 
					
						
							|  |  |  |   if (!icon) | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Convert the icon from the Windows specific HICON to gfx::ImageSkia.
 | 
					
						
							|  |  |  |   scoped_ptr<SkBitmap> bitmap(IconUtil::CreateSkBitmapFromHICON(icon)); | 
					
						
							|  |  |  |   image->AddRepresentation(gfx::ImageSkiaRep(*bitmap, 1.0f)); | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 16:03:19 +08:00
										 |  |  | v8::Persistent<v8::ObjectTemplate> template_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 19:22:41 +08:00
										 |  |  | NativeImage::NativeImage() {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 16:03:19 +08:00
										 |  |  | NativeImage::NativeImage(const gfx::Image& image) : image_(image) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NativeImage::~NativeImage() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | mate::ObjectTemplateBuilder NativeImage::GetObjectTemplateBuilder( | 
					
						
							|  |  |  |     v8::Isolate* isolate) { | 
					
						
							|  |  |  |   if (template_.IsEmpty()) | 
					
						
							|  |  |  |     template_.Reset(isolate, mate::ObjectTemplateBuilder(isolate) | 
					
						
							|  |  |  |         .SetMethod("toPng", &NativeImage::ToPNG) | 
					
						
							| 
									
										
										
										
											2015-02-11 17:47:54 +08:00
										 |  |  |         .SetMethod("toJpeg", &NativeImage::ToJPEG) | 
					
						
							| 
									
										
										
										
											2015-11-13 16:03:40 +08:00
										 |  |  |         .SetMethod("toDataURL", &NativeImage::ToDataURL) | 
					
						
							|  |  |  |         .SetMethod("toDataUrl", &NativeImage::ToDataURL)  // deprecated.
 | 
					
						
							| 
									
										
										
										
											2015-02-11 16:49:43 +08:00
										 |  |  |         .SetMethod("isEmpty", &NativeImage::IsEmpty) | 
					
						
							|  |  |  |         .SetMethod("getSize", &NativeImage::GetSize) | 
					
						
							| 
									
										
										
										
											2015-04-08 13:50:03 +05:30
										 |  |  |         .SetMethod("setTemplateImage", &NativeImage::SetTemplateImage) | 
					
						
							| 
									
										
										
										
											2015-07-26 21:58:48 -07:00
										 |  |  |         .SetMethod("isTemplateImage", &NativeImage::IsTemplateImage) | 
					
						
							| 
									
										
										
										
											2015-02-11 16:03:19 +08:00
										 |  |  |         .Build()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return mate::ObjectTemplateBuilder( | 
					
						
							|  |  |  |       isolate, v8::Local<v8::ObjectTemplate>::New(isolate, template_)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  | v8::Local<v8::Value> NativeImage::ToPNG(v8::Isolate* isolate) { | 
					
						
							| 
									
										
										
										
											2015-02-11 16:03:19 +08:00
										 |  |  |   scoped_refptr<base::RefCountedMemory> png = image_.As1xPNGBytes(); | 
					
						
							| 
									
										
										
										
											2015-08-04 16:50:33 +08:00
										 |  |  |   return node::Buffer::Copy(isolate, | 
					
						
							|  |  |  |                             reinterpret_cast<const char*>(png->front()), | 
					
						
							|  |  |  |                             static_cast<size_t>(png->size())).ToLocalChecked(); | 
					
						
							| 
									
										
										
										
											2015-02-11 16:03:19 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  | v8::Local<v8::Value> NativeImage::ToJPEG(v8::Isolate* isolate, int quality) { | 
					
						
							| 
									
										
										
										
											2015-02-11 17:47:54 +08:00
										 |  |  |   std::vector<unsigned char> output; | 
					
						
							|  |  |  |   gfx::JPEG1xEncodedDataFromImage(image_, quality, &output); | 
					
						
							| 
									
										
										
										
											2015-08-04 16:50:33 +08:00
										 |  |  |   return node::Buffer::Copy( | 
					
						
							|  |  |  |       isolate, | 
					
						
							|  |  |  |       reinterpret_cast<const char*>(&output.front()), | 
					
						
							|  |  |  |       static_cast<size_t>(output.size())).ToLocalChecked(); | 
					
						
							| 
									
										
										
										
											2015-02-11 17:47:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 14:32:51 +08:00
										 |  |  | std::string NativeImage::ToDataURL() { | 
					
						
							|  |  |  |   scoped_refptr<base::RefCountedMemory> png = image_.As1xPNGBytes(); | 
					
						
							|  |  |  |   std::string data_url; | 
					
						
							|  |  |  |   data_url.insert(data_url.end(), png->front(), png->front() + png->size()); | 
					
						
							|  |  |  |   base::Base64Encode(data_url, &data_url); | 
					
						
							|  |  |  |   data_url.insert(0, "data:image/png;base64,"); | 
					
						
							|  |  |  |   return data_url; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 16:49:43 +08:00
										 |  |  | bool NativeImage::IsEmpty() { | 
					
						
							|  |  |  |   return image_.IsEmpty(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | gfx::Size NativeImage::GetSize() { | 
					
						
							|  |  |  |   return image_.Size(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 11:53:24 +08:00
										 |  |  | #if !defined(OS_MACOSX)
 | 
					
						
							|  |  |  | void NativeImage::SetTemplateImage(bool setAsTemplate) { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-29 11:48:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-26 21:58:48 -07:00
										 |  |  | bool NativeImage::IsTemplateImage() { | 
					
						
							| 
									
										
										
										
											2015-07-29 12:36:01 +08:00
										 |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2015-07-26 21:58:48 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-04-13 11:53:24 +08:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 13:55:45 +08:00
										 |  |  | // static
 | 
					
						
							|  |  |  | mate::Handle<NativeImage> NativeImage::CreateEmpty(v8::Isolate* isolate) { | 
					
						
							|  |  |  |   return mate::CreateHandle(isolate, new NativeImage); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 16:03:19 +08:00
										 |  |  | // static
 | 
					
						
							| 
									
										
										
										
											2015-02-11 18:02:59 +08:00
										 |  |  | mate::Handle<NativeImage> NativeImage::Create( | 
					
						
							|  |  |  |     v8::Isolate* isolate, const gfx::Image& image) { | 
					
						
							| 
									
										
										
										
											2015-02-11 16:03:19 +08:00
										 |  |  |   return mate::CreateHandle(isolate, new NativeImage(image)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 18:02:59 +08:00
										 |  |  | // static
 | 
					
						
							|  |  |  | mate::Handle<NativeImage> NativeImage::CreateFromPNG( | 
					
						
							| 
									
										
										
										
											2015-02-12 14:27:53 +08:00
										 |  |  |     v8::Isolate* isolate, const char* buffer, size_t length) { | 
					
						
							| 
									
										
										
										
											2015-02-11 18:02:59 +08:00
										 |  |  |   gfx::Image image = gfx::Image::CreateFrom1xPNGBytes( | 
					
						
							| 
									
										
										
										
											2015-02-12 14:27:53 +08:00
										 |  |  |       reinterpret_cast<const unsigned char*>(buffer), length); | 
					
						
							| 
									
										
										
										
											2015-02-11 18:02:59 +08:00
										 |  |  |   return Create(isolate, image); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // static
 | 
					
						
							|  |  |  | mate::Handle<NativeImage> NativeImage::CreateFromJPEG( | 
					
						
							| 
									
										
										
										
											2015-02-12 14:27:53 +08:00
										 |  |  |     v8::Isolate* isolate, const char* buffer, size_t length) { | 
					
						
							| 
									
										
										
										
											2015-02-11 18:02:59 +08:00
										 |  |  |   gfx::Image image = gfx::ImageFrom1xJPEGEncodedData( | 
					
						
							| 
									
										
										
										
											2015-02-12 14:27:53 +08:00
										 |  |  |       reinterpret_cast<const unsigned char*>(buffer), length); | 
					
						
							| 
									
										
										
										
											2015-02-11 18:02:59 +08:00
										 |  |  |   return Create(isolate, image); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 18:41:06 +08:00
										 |  |  | // static
 | 
					
						
							|  |  |  | mate::Handle<NativeImage> NativeImage::CreateFromPath( | 
					
						
							|  |  |  |     v8::Isolate* isolate, const base::FilePath& path) { | 
					
						
							|  |  |  |   gfx::ImageSkia image_skia; | 
					
						
							| 
									
										
										
										
											2015-08-21 12:06:38 +08:00
										 |  |  |   if (path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) { | 
					
						
							| 
									
										
										
										
											2015-08-08 17:58:05 +03:00
										 |  |  | #if defined(OS_WIN)
 | 
					
						
							| 
									
										
										
										
											2015-08-21 12:06:38 +08:00
										 |  |  |     ReadImageSkiaFromICO(&image_skia, path); | 
					
						
							| 
									
										
										
										
											2015-08-08 17:58:05 +03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2015-08-21 12:06:38 +08:00
										 |  |  |     PopulateImageSkiaRepsFromPath(&image_skia, path); | 
					
						
							| 
									
										
										
										
											2015-08-08 17:58:05 +03:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-02-12 12:24:18 +08:00
										 |  |  |   gfx::Image image(image_skia); | 
					
						
							| 
									
										
										
										
											2015-04-13 11:53:24 +08:00
										 |  |  |   mate::Handle<NativeImage> handle = Create(isolate, image); | 
					
						
							| 
									
										
										
										
											2015-02-12 12:24:18 +08:00
										 |  |  | #if defined(OS_MACOSX)
 | 
					
						
							| 
									
										
										
										
											2015-07-26 21:58:48 -07:00
										 |  |  |   if (IsTemplateFilename(path)) | 
					
						
							| 
									
										
										
										
											2015-04-13 11:53:24 +08:00
										 |  |  |     handle->SetTemplateImage(true); | 
					
						
							| 
									
										
										
										
											2015-02-12 12:24:18 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-04-13 11:53:24 +08:00
										 |  |  |   return handle; | 
					
						
							| 
									
										
										
										
											2015-02-11 18:41:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 15:19:05 +08:00
										 |  |  | // static
 | 
					
						
							|  |  |  | mate::Handle<NativeImage> NativeImage::CreateFromBuffer( | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  |     mate::Arguments* args, v8::Local<v8::Value> buffer) { | 
					
						
							| 
									
										
										
										
											2015-02-12 15:19:05 +08:00
										 |  |  |   double scale_factor = 1.; | 
					
						
							|  |  |  |   args->GetNext(&scale_factor); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   gfx::ImageSkia image_skia; | 
					
						
							|  |  |  |   AddImageSkiaRep(&image_skia, | 
					
						
							|  |  |  |                   reinterpret_cast<unsigned char*>(node::Buffer::Data(buffer)), | 
					
						
							|  |  |  |                   node::Buffer::Length(buffer), | 
					
						
							|  |  |  |                   scale_factor); | 
					
						
							|  |  |  |   return Create(args->isolate(), gfx::Image(image_skia)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-12 14:27:53 +08:00
										 |  |  | // static
 | 
					
						
							|  |  |  | mate::Handle<NativeImage> NativeImage::CreateFromDataURL( | 
					
						
							|  |  |  |     v8::Isolate* isolate, const GURL& url) { | 
					
						
							|  |  |  |   std::string mime_type, charset, data; | 
					
						
							|  |  |  |   if (net::DataURL::Parse(url, &mime_type, &charset, &data)) { | 
					
						
							|  |  |  |     if (mime_type == "image/png") | 
					
						
							|  |  |  |       return CreateFromPNG(isolate, data.c_str(), data.size()); | 
					
						
							|  |  |  |     else if (mime_type == "image/jpeg") | 
					
						
							|  |  |  |       return CreateFromJPEG(isolate, data.c_str(), data.size()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return CreateEmpty(isolate); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 16:03:19 +08:00
										 |  |  | }  // namespace api
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace atom
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-22 19:11:22 +08:00
										 |  |  | void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused, | 
					
						
							|  |  |  |                 v8::Local<v8::Context> context, void* priv) { | 
					
						
							| 
									
										
										
										
											2015-02-11 18:09:51 +08:00
										 |  |  |   mate::Dictionary dict(context->GetIsolate(), exports); | 
					
						
							| 
									
										
										
										
											2015-02-12 13:55:45 +08:00
										 |  |  |   dict.SetMethod("createEmpty", &atom::api::NativeImage::CreateEmpty); | 
					
						
							| 
									
										
										
										
											2015-02-11 18:41:06 +08:00
										 |  |  |   dict.SetMethod("createFromPath", &atom::api::NativeImage::CreateFromPath); | 
					
						
							| 
									
										
										
										
											2015-02-12 15:19:05 +08:00
										 |  |  |   dict.SetMethod("createFromBuffer", &atom::api::NativeImage::CreateFromBuffer); | 
					
						
							| 
									
										
										
										
											2015-11-13 16:03:40 +08:00
										 |  |  |   dict.SetMethod("createFromDataURL", | 
					
						
							| 
									
										
										
										
											2015-02-12 14:27:53 +08:00
										 |  |  |                  &atom::api::NativeImage::CreateFromDataURL); | 
					
						
							| 
									
										
										
										
											2015-02-11 16:03:19 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_native_image, Initialize) |