From 8852b128ece9183317857c016631af7de27ac031 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 6 Mar 2017 16:29:39 -0800 Subject: [PATCH] Add GetScaleFactorFromOptions helper --- atom/common/api/atom_api_native_image.cc | 29 +++++++++++------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/atom/common/api/atom_api_native_image.cc b/atom/common/api/atom_api_native_image.cc index e63fff3a0c9c..98eac6b2ff68 100644 --- a/atom/common/api/atom_api_native_image.cc +++ b/atom/common/api/atom_api_native_image.cc @@ -76,6 +76,15 @@ float GetScaleFactorFromPath(const base::FilePath& path) { return 1.0f; } +// Get the scale factor from options object at the first argument +float GetScaleFactorFromOptions(mate::Arguments* args) { + float scale_factor = 1.0f; + mate::Dictionary options; + if (args->GetNext(&options)) + options.Get("scaleFactor", &scale_factor); + return scale_factor; +} + bool AddImageSkiaRep(gfx::ImageSkia* image, const unsigned char* data, size_t size, @@ -231,10 +240,7 @@ HICON NativeImage::GetHICON(int size) { #endif v8::Local NativeImage::ToPNG(mate::Arguments* args) { - float scale_factor = 1.0f; - mate::Dictionary options; - if (args->GetNext(&options)) - options.Get("scaleFactor", &scale_factor); + float scale_factor = GetScaleFactorFromOptions(args); if (scale_factor == 1.0f) { // Use raw 1x PNG bytes when available @@ -257,10 +263,7 @@ v8::Local NativeImage::ToPNG(mate::Arguments* args) { } v8::Local NativeImage::ToBitmap(mate::Arguments* args) { - float scale_factor = 1.0f; - mate::Dictionary options; - if (args->GetNext(&options)) - options.Get("scaleFactor", &scale_factor); + float scale_factor = GetScaleFactorFromOptions(args); const SkBitmap bitmap = image_.AsImageSkia().GetRepresentation(scale_factor).sk_bitmap(); @@ -282,10 +285,7 @@ v8::Local NativeImage::ToJPEG(v8::Isolate* isolate, int quality) { } std::string NativeImage::ToDataURL(mate::Arguments* args) { - float scale_factor = 1.0f; - mate::Dictionary options; - if (args->GetNext(&options)) - options.Get("scaleFactor", &scale_factor); + float scale_factor = GetScaleFactorFromOptions(args); if (scale_factor == 1.0f) { // Use raw 1x PNG bytes when available @@ -299,10 +299,7 @@ std::string NativeImage::ToDataURL(mate::Arguments* args) { } v8::Local NativeImage::GetBitmap(mate::Arguments* args) { - float scale_factor = 1.0f; - mate::Dictionary options; - if (args->GetNext(&options)) - options.Get("scaleFactor", &scale_factor); + float scale_factor = GetScaleFactorFromOptions(args); const SkBitmap bitmap = image_.AsImageSkia().GetRepresentation(scale_factor).sk_bitmap();