Simplify the image_converter.cc
This commit is contained in:
parent
f972c38bc8
commit
40679ae82c
2 changed files with 30 additions and 45 deletions
|
@ -30,6 +30,8 @@ ScaleFactorPair kScaleFactorPairs[] = {
|
||||||
{ "@2x" , 2.0f },
|
{ "@2x" , 2.0f },
|
||||||
{ "@3x" , 3.0f },
|
{ "@3x" , 3.0f },
|
||||||
{ "@1x" , 1.0f },
|
{ "@1x" , 1.0f },
|
||||||
|
{ "@4x" , 4.0f },
|
||||||
|
{ "@5x" , 5.0f },
|
||||||
{ "@1.25x" , 1.25f },
|
{ "@1.25x" , 1.25f },
|
||||||
{ "@1.33x" , 1.33f },
|
{ "@1.33x" , 1.33f },
|
||||||
{ "@1.4x" , 1.4f },
|
{ "@1.4x" , 1.4f },
|
||||||
|
@ -41,10 +43,6 @@ ScaleFactorPair kScaleFactorPairs[] = {
|
||||||
float GetScaleFactorFromPath(const base::FilePath& path) {
|
float GetScaleFactorFromPath(const base::FilePath& path) {
|
||||||
std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe());
|
std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe());
|
||||||
|
|
||||||
// There is no scale info in the file path.
|
|
||||||
if (!EndsWith(filename, "x", true))
|
|
||||||
return 1.0f;
|
|
||||||
|
|
||||||
// We don't try to convert string to float here because it is very very
|
// We don't try to convert string to float here because it is very very
|
||||||
// expensive.
|
// expensive.
|
||||||
for (unsigned i = 0; i < arraysize(kScaleFactorPairs); ++i) {
|
for (unsigned i = 0; i < arraysize(kScaleFactorPairs); ++i) {
|
||||||
|
@ -55,27 +53,9 @@ float GetScaleFactorFromPath(const base::FilePath& path) {
|
||||||
return 1.0f;
|
return 1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AppendIfExists(std::vector<base::FilePath>* paths,
|
bool AddImageSkiaRep(gfx::ImageSkia* image,
|
||||||
const base::FilePath& path) {
|
const base::FilePath& path,
|
||||||
if (base::PathExists(path))
|
double scale_factor) {
|
||||||
paths->push_back(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
void PopulatePossibleFilePaths(std::vector<base::FilePath>* paths,
|
|
||||||
const base::FilePath& path) {
|
|
||||||
AppendIfExists(paths, path);
|
|
||||||
|
|
||||||
std::string filename(path.BaseName().RemoveExtension().AsUTF8Unsafe());
|
|
||||||
if (MatchPattern(filename, "*@*x"))
|
|
||||||
return;
|
|
||||||
|
|
||||||
for (unsigned i = 0; i < arraysize(kScaleFactorPairs); ++i)
|
|
||||||
AppendIfExists(paths,
|
|
||||||
path.InsertBeforeExtensionASCII(kScaleFactorPairs[i].name));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool AddImageSkiaRepFromPath(gfx::ImageSkia* image,
|
|
||||||
const base::FilePath& path) {
|
|
||||||
std::string file_contents;
|
std::string file_contents;
|
||||||
if (!base::ReadFileToString(path, &file_contents))
|
if (!base::ReadFileToString(path, &file_contents))
|
||||||
return false;
|
return false;
|
||||||
|
@ -90,13 +70,28 @@ bool AddImageSkiaRepFromPath(gfx::ImageSkia* image,
|
||||||
// Try JPEG.
|
// Try JPEG.
|
||||||
decoded.reset(gfx::JPEGCodec::Decode(data, size));
|
decoded.reset(gfx::JPEGCodec::Decode(data, size));
|
||||||
|
|
||||||
if (decoded) {
|
if (!decoded)
|
||||||
image->AddRepresentation(gfx::ImageSkiaRep(
|
return false;
|
||||||
*decoded.release(), GetScaleFactorFromPath(path)));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -105,20 +100,10 @@ bool Converter<gfx::ImageSkia>::FromV8(v8::Isolate* isolate,
|
||||||
v8::Handle<v8::Value> val,
|
v8::Handle<v8::Value> val,
|
||||||
gfx::ImageSkia* out) {
|
gfx::ImageSkia* out) {
|
||||||
base::FilePath path;
|
base::FilePath path;
|
||||||
if (Converter<base::FilePath>::FromV8(isolate, val, &path)) {
|
if (!Converter<base::FilePath>::FromV8(isolate, val, &path))
|
||||||
std::vector<base::FilePath> paths;
|
return false;
|
||||||
PopulatePossibleFilePaths(&paths, path);
|
|
||||||
if (paths.empty())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < paths.size(); ++i) {
|
return PopulateImageSkiaRepsFromPath(out, path);
|
||||||
if (!AddImageSkiaRepFromPath(out, paths[i]))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Converter<gfx::Image>::FromV8(v8::Isolate* isolate,
|
bool Converter<gfx::Image>::FromV8(v8::Isolate* isolate,
|
||||||
|
|
2
vendor/native_mate
vendored
2
vendor/native_mate
vendored
|
@ -1 +1 @@
|
||||||
Subproject commit 4a1d11b2bea0907c66bf986ab635e161a1a16385
|
Subproject commit be2934d9b5925cb017238c1b385340c2f9cfdcb7
|
Loading…
Add table
Add a link
Reference in a new issue