OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2013 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "SkBitmap.h" |
| 9 #include "SkColorPriv.h" |
| 10 #include "SkCommandLineFlags.h" |
| 11 #include "SkData.h" |
| 12 #include "SkForceLinking.h" |
| 13 #include "SkGraphics.h" |
| 14 #include "SkImageDecoder.h" |
| 15 #include "SkImageEncoder.h" |
| 16 #include "SkOSFile.h" |
| 17 #include "SkRandom.h" |
| 18 #include "SkStream.h" |
| 19 #include "SkTArray.h" |
| 20 #include "SkTemplates.h" |
| 21 |
| 22 __SK_FORCE_IMAGE_DECODER_LINKING; |
| 23 |
| 24 DEFINE_string2(readPath, r, "", "Folder(s) and files to decode images. Required.
"); |
| 25 |
| 26 struct Format { |
| 27 SkImageEncoder::Type fType; |
| 28 SkImageDecoder::Format fFormat; |
| 29 const char* fSuffix; |
| 30 }; |
| 31 |
| 32 static const Format gFormats[] = { |
| 33 { SkImageEncoder::kBMP_Type, SkImageDecoder::kBMP_Format, ".bmp" }, |
| 34 { SkImageEncoder::kGIF_Type, SkImageDecoder::kGIF_Format, ".gif" }, |
| 35 { SkImageEncoder::kICO_Type, SkImageDecoder::kICO_Format, ".ico" }, |
| 36 { SkImageEncoder::kJPEG_Type, SkImageDecoder::kJPEG_Format, ".jpg" }, |
| 37 { SkImageEncoder::kPNG_Type, SkImageDecoder::kPNG_Format, ".png" }, |
| 38 { SkImageEncoder::kWBMP_Type, SkImageDecoder::kWBMP_Format, ".wbmp" }, |
| 39 { SkImageEncoder::kWEBP_Type, SkImageDecoder::kWEBP_Format, ".webp" } |
| 40 }; |
| 41 |
| 42 SkISize opaqueSize(const SkBitmap& bm) { |
| 43 int width = 1; |
| 44 int height = 1; |
| 45 for (int y = 0 ; y < bm.height(); y++) { |
| 46 for (int x = 0 ; x < bm.height(); x++) { |
| 47 SkColor color = bm.getColor(x, y); |
| 48 if (SkColorGetA(color) != 0) { |
| 49 height = y + 1; |
| 50 width = width > (x + 1) ? width : x + 1; |
| 51 } |
| 52 } |
| 53 } |
| 54 |
| 55 return SkISize::Make(width, height); |
| 56 } |
| 57 |
| 58 static void setup_bitmap(SkBitmap* bitmap, int width, int height) { |
| 59 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 60 |
| 61 bitmap->allocPixels(); |
| 62 } |
| 63 |
| 64 |
| 65 static bool write_bitmap(const char outName[], const SkBitmap& bm) { |
| 66 SkISize size = opaqueSize(bm); |
| 67 SkBitmap dst; |
| 68 setup_bitmap(&dst, size.width(), size.height()); |
| 69 |
| 70 for (int y = 0 ; y < dst.height(); y++) { |
| 71 for (int x = 0 ; x < dst.width(); x++) { |
| 72 SkColor color = bm.getColor(x, y); |
| 73 if (SkColorGetA(color) != 0xff) { |
| 74 int a = SkColorGetA(color); |
| 75 int r = SkColorGetR(color); |
| 76 int g = SkColorGetG(color); |
| 77 int b = SkColorGetB(color); |
| 78 if (a == 0) { |
| 79 r = g = b = 0; |
| 80 } else { |
| 81 r = (r * a) / 255; |
| 82 g = (g * a) / 255; |
| 83 b = (b * a) / 255; |
| 84 a = 255; |
| 85 } |
| 86 color = SkColorSetARGB((U8CPU)a, (U8CPU)r, (U8CPU)g, (U8CPU)b); |
| 87 } |
| 88 *dst.getAddr32(x, y) = color; |
| 89 } |
| 90 } |
| 91 |
| 92 return SkImageEncoder::EncodeFile(outName, dst, SkImageEncoder::kPNG_Type, 1
00); |
| 93 } |
| 94 |
| 95 static void decodeFileAndWrite(const char srcPath[]) { |
| 96 SkBitmap bitmap; |
| 97 SkFILEStream stream(srcPath); |
| 98 if (!stream.isValid()) { |
| 99 return; |
| 100 } |
| 101 |
| 102 SkImageDecoder* codec = SkImageDecoder::Factory(&stream); |
| 103 if (NULL == codec) { |
| 104 return; |
| 105 } |
| 106 |
| 107 SkAutoTDelete<SkImageDecoder> ad(codec); |
| 108 |
| 109 stream.rewind(); |
| 110 |
| 111 if (!codec->decode(&stream, &bitmap, SkBitmap::kARGB_8888_Config, |
| 112 SkImageDecoder::kDecodePixels_Mode)) { |
| 113 return; |
| 114 } |
| 115 |
| 116 write_bitmap(srcPath, bitmap); |
| 117 } |
| 118 |
| 119 /** |
| 120 * Return true if the filename represents an image. |
| 121 */ |
| 122 static bool is_image_file(const char* filename) { |
| 123 const char* gImageExtensions[] = { |
| 124 ".png", ".PNG", ".jpg", ".JPG", ".jpeg", ".JPEG", ".bmp", ".BMP", |
| 125 ".webp", ".WEBP", ".ico", ".ICO", ".wbmp", ".WBMP", ".gif", ".GIF" |
| 126 }; |
| 127 for (size_t i = 0; i < SK_ARRAY_COUNT(gImageExtensions); ++i) { |
| 128 if (SkStrEndsWith(filename, gImageExtensions[i])) { |
| 129 return true; |
| 130 } |
| 131 } |
| 132 return false; |
| 133 } |
| 134 |
| 135 int tool_main(int argc, char** argv); |
| 136 int tool_main(int argc, char** argv) { |
| 137 SkCommandLineFlags::SetUsage("Decode files, and optionally write the results
to files."); |
| 138 SkCommandLineFlags::Parse(argc, argv); |
| 139 |
| 140 if (FLAGS_readPath.count() < 1) { |
| 141 SkDebugf("Folder(s) or image(s) to decode are required.\n"); |
| 142 return -1; |
| 143 } |
| 144 |
| 145 |
| 146 SkAutoGraphics ag; |
| 147 |
| 148 for (int i = 0; i < FLAGS_readPath.count(); i++) { |
| 149 const char* readPath = FLAGS_readPath[i]; |
| 150 if (strlen(readPath) < 1) { |
| 151 break; |
| 152 } |
| 153 if (sk_isdir(readPath)) { |
| 154 const char* dir = readPath; |
| 155 SkOSFile::Iter iter(dir); |
| 156 SkString filename; |
| 157 while (iter.next(&filename)) { |
| 158 if (!is_image_file(filename.c_str())) { |
| 159 continue; |
| 160 } |
| 161 SkString fullname = SkOSPath::SkPathJoin(dir, filename.c_str()); |
| 162 decodeFileAndWrite(fullname.c_str()); |
| 163 } |
| 164 } else if (sk_exists(readPath) && is_image_file(readPath)) { |
| 165 decodeFileAndWrite(readPath); |
| 166 } |
| 167 } |
| 168 |
| 169 return 0; |
| 170 } |
| 171 |
| 172 #if !defined SK_BUILD_FOR_IOS |
| 173 int main(int argc, char * const argv[]) { |
| 174 return tool_main(argc, (char**) argv); |
| 175 } |
| 176 #endif |
OLD | NEW |