2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-05-30 15:05:24 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/common/native_mate_converters/image_converter.h"
|
|
|
|
|
2014-06-01 02:20:06 +00:00
|
|
|
#include <string>
|
2014-08-06 03:20:00 +00:00
|
|
|
#include <vector>
|
2014-06-01 02:20:06 +00:00
|
|
|
|
2014-05-30 15:05:24 +00:00
|
|
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
2015-01-10 01:45:50 +00:00
|
|
|
#include "base/files/file_util.h"
|
2014-06-23 05:09:06 +00:00
|
|
|
#include "base/strings/string_util.h"
|
2014-05-30 15:05:24 +00:00
|
|
|
#include "ui/gfx/codec/jpeg_codec.h"
|
|
|
|
#include "ui/gfx/codec/png_codec.h"
|
2015-01-03 01:13:26 +00:00
|
|
|
#include "ui/gfx/image/image.h"
|
2014-05-30 15:05:24 +00:00
|
|
|
#include "ui/gfx/image/image_skia.h"
|
2014-06-28 14:33:00 +00:00
|
|
|
#include "ui/base/layout.h"
|
2014-05-30 15:05:24 +00:00
|
|
|
|
2015-01-03 02:34:13 +00:00
|
|
|
#if !defined(OS_MACOSX)
|
|
|
|
|
2014-05-30 15:05:24 +00:00
|
|
|
namespace mate {
|
|
|
|
|
2014-06-23 05:09:06 +00:00
|
|
|
namespace {
|
|
|
|
|
2014-08-06 03:20:00 +00: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 },
|
2015-01-03 02:10:29 +00:00
|
|
|
{ "@4x" , 4.0f },
|
|
|
|
{ "@5x" , 5.0f },
|
2014-08-06 03:20:00 +00:00
|
|
|
{ "@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) {
|
2014-06-23 05:09:06 +00:00
|
|
|
std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe());
|
2014-08-06 03:20:00 +00:00
|
|
|
|
|
|
|
// We don't try to convert string to float here because it is very very
|
|
|
|
// expensive.
|
|
|
|
for (unsigned i = 0; i < arraysize(kScaleFactorPairs); ++i) {
|
|
|
|
if (EndsWith(filename, kScaleFactorPairs[i].name, true))
|
|
|
|
return kScaleFactorPairs[i].scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
2015-01-03 02:10:29 +00:00
|
|
|
bool AddImageSkiaRep(gfx::ImageSkia* image,
|
|
|
|
const base::FilePath& path,
|
|
|
|
double scale_factor) {
|
2014-08-06 03:20:00 +00:00
|
|
|
std::string file_contents;
|
|
|
|
if (!base::ReadFileToString(path, &file_contents))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const unsigned char* data =
|
|
|
|
reinterpret_cast<const unsigned char*>(file_contents.data());
|
|
|
|
size_t size = file_contents.size();
|
|
|
|
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));
|
|
|
|
|
2015-01-03 02:10:29 +00:00
|
|
|
if (!decoded)
|
|
|
|
return false;
|
2014-08-06 03:20:00 +00:00
|
|
|
|
2015-01-03 02:10:29 +00:00
|
|
|
image->AddRepresentation(gfx::ImageSkiaRep(*decoded.release(), scale_factor));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PopulateImageSkiaRepsFromPath(gfx::ImageSkia* image,
|
|
|
|
const base::FilePath& path) {
|
|
|
|
bool succeed = false;
|
|
|
|
std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe());
|
|
|
|
if (MatchPattern(filename, "*@*x"))
|
|
|
|
// 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;
|
2014-06-23 05:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-05-30 15:05:24 +00:00
|
|
|
bool Converter<gfx::ImageSkia>::FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Handle<v8::Value> val,
|
|
|
|
gfx::ImageSkia* out) {
|
2015-01-05 22:41:33 +00:00
|
|
|
if (val->IsNull())
|
|
|
|
return true;
|
|
|
|
|
2014-05-30 15:05:24 +00:00
|
|
|
base::FilePath path;
|
2015-01-03 02:10:29 +00:00
|
|
|
if (!Converter<base::FilePath>::FromV8(isolate, val, &path))
|
|
|
|
return false;
|
2014-05-30 15:05:24 +00:00
|
|
|
|
2015-01-03 02:10:29 +00:00
|
|
|
return PopulateImageSkiaRepsFromPath(out, path);
|
2014-05-30 15:05:24 +00:00
|
|
|
}
|
|
|
|
|
2015-01-03 01:13:26 +00:00
|
|
|
bool Converter<gfx::Image>::FromV8(v8::Isolate* isolate,
|
|
|
|
v8::Handle<v8::Value> val,
|
|
|
|
gfx::Image* out) {
|
|
|
|
gfx::ImageSkia image;
|
|
|
|
if (!ConvertFromV8(isolate, val, &image))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*out = gfx::Image(image);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:05:24 +00:00
|
|
|
} // namespace mate
|
2015-01-03 02:34:13 +00:00
|
|
|
|
|
|
|
#endif // !defined(OS_MACOSX)
|