Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkCommandLineFlags.h" | 9 #include "SkCommandLineFlags.h" |
| 10 #include "SkData.h" | |
| 10 #include "SkGraphics.h" | 11 #include "SkGraphics.h" |
| 11 #include "SkImageDecoder.h" | 12 #include "SkImageDecoder.h" |
| 12 #include "SkImageEncoder.h" | 13 #include "SkImageEncoder.h" |
| 13 #include "SkOSFile.h" | 14 #include "SkOSFile.h" |
| 14 #include "SkStream.h" | 15 #include "SkStream.h" |
| 15 #include "SkTArray.h" | 16 #include "SkTArray.h" |
| 16 #include "SkTemplates.h" | 17 #include "SkTemplates.h" |
| 17 | 18 #include "picture_utils.h" |
| 18 | 19 |
| 19 DEFINE_string2(readPath, r, "", "Folder(s) and files to decode images. Required. "); | 20 DEFINE_string2(readPath, r, "", "Folder(s) and files to decode images. Required. "); |
| 20 DEFINE_string2(writePath, w, "", "Write rendered images into this directory."); | 21 DEFINE_string2(writePath, w, "", "Write rendered images into this directory."); |
| 22 DEFINE_bool(reencode, true, "Reencode the images to test encoding."); | |
| 21 | 23 |
| 22 // Store the names of the filenames to report later which ones failed, succeeded , and were | 24 struct Format { |
| 23 // invalid. | 25 SkImageEncoder::Type fType; |
| 24 static SkTArray<SkString, false> invalids; | 26 SkImageDecoder::Format fFormat; |
| 25 static SkTArray<SkString, false> nocodecs; | 27 const char* fSuffix; |
| 26 static SkTArray<SkString, false> failures; | 28 }; |
| 27 static SkTArray<SkString, false> successes; | |
| 28 | 29 |
| 29 static bool decodeFile(SkBitmap* bitmap, const char srcPath[]) { | 30 static const Format gFormats[] = { |
| 30 SkFILEStream stream(srcPath); | 31 { SkImageEncoder::kUnknown_Type, SkImageDecoder::kUnknown_Format, "" }, |
| 31 if (!stream.isValid()) { | 32 { SkImageEncoder::kBMP_Type, SkImageDecoder::kBMP_Format, ".bmp" }, |
| 32 invalids.push_back().set(srcPath); | 33 { SkImageEncoder::kGIF_Type, SkImageDecoder::kGIF_Format, ".gif" }, |
| 33 return false; | 34 { SkImageEncoder::kICO_Type, SkImageDecoder::kICO_Format, ".ico" }, |
| 35 { SkImageEncoder::kJPEG_Type, SkImageDecoder::kJPEG_Format, ".jpg" }, | |
| 36 { SkImageEncoder::kPNG_Type, SkImageDecoder::kPNG_Format, ".png" }, | |
| 37 { SkImageEncoder::kWBMP_Type, SkImageDecoder::kWBMP_Format, ".wbmp" }, | |
| 38 { SkImageEncoder::kWEBP_Type, SkImageDecoder::kWEBP_Format, ".webp" } | |
| 39 }; | |
| 40 | |
| 41 static SkImageEncoder::Type format_to_type(SkImageDecoder::Format format) { | |
| 42 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { | |
| 43 if (gFormats[i].fFormat == format) { | |
| 44 return gFormats[i].fType; | |
| 45 } | |
| 34 } | 46 } |
| 35 | 47 return SkImageEncoder::kUnknown_Type; |
| 36 SkImageDecoder* codec = SkImageDecoder::Factory(&stream); | |
| 37 if (NULL == codec) { | |
| 38 nocodecs.push_back().set(srcPath); | |
| 39 return false; | |
| 40 } | |
| 41 | |
| 42 SkAutoTDelete<SkImageDecoder> ad(codec); | |
| 43 | |
| 44 stream.rewind(); | |
| 45 if (!codec->decode(&stream, bitmap, SkBitmap::kARGB_8888_Config, | |
| 46 SkImageDecoder::kDecodePixels_Mode)) { | |
| 47 failures.push_back().set(srcPath); | |
| 48 return false; | |
| 49 } | |
| 50 | |
| 51 successes.push_back().printf("%s [%d %d]", srcPath, bitmap->width(), bitmap- >height()); | |
| 52 return true; | |
| 53 } | 48 } |
| 54 | 49 |
| 55 /////////////////////////////////////////////////////////////////////////////// | 50 static const char* suffix_for_type(SkImageEncoder::Type type) { |
| 51 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { | |
| 52 if (gFormats[i].fType == type) { | |
| 53 return gFormats[i].fSuffix; | |
| 54 } | |
| 55 } | |
| 56 return ""; | |
| 57 } | |
| 56 | 58 |
| 57 static void make_outname(SkString* dst, const char outDir[], const char src[]) { | 59 static SkImageEncoder::Type guess_type_from_suffix(const char suffix[]) { |
| 60 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { | |
| 61 if (SkImageEncoder::kUnknown_Type == gFormats[i].fType) { | |
| 62 continue; | |
| 63 } | |
| 64 if (strcmp(suffix, gFormats[i].fSuffix) == 0) { | |
| 65 return gFormats[i].fType; | |
| 66 } | |
| 67 } | |
| 68 return SkImageEncoder::kUnknown_Type; | |
| 69 } | |
| 70 | |
| 71 static void make_outname(SkString* dst, const char outDir[], const char src[], | |
| 72 const char suffix[]) { | |
| 58 dst->set(outDir); | 73 dst->set(outDir); |
| 59 const char* start = strrchr(src, '/'); | 74 const char* start = strrchr(src, '/'); |
| 60 if (start) { | 75 if (start) { |
| 61 start += 1; // skip the actual last '/' | 76 start += 1; // skip the actual last '/' |
| 62 } else { | 77 } else { |
| 63 start = src; | 78 start = src; |
| 64 } | 79 } |
| 65 dst->append(start); | 80 dst->append(start); |
| 66 if (!dst->endsWith(".png")) { | 81 if (!dst->endsWith(suffix)) { |
| 67 const char* cstyleDst = dst->c_str(); | 82 const char* cstyleDst = dst->c_str(); |
| 68 const char* dot = strrchr(cstyleDst, '.'); | 83 const char* dot = strrchr(cstyleDst, '.'); |
| 69 if (dot != NULL) { | 84 if (dot != NULL) { |
| 70 int32_t index = SkToS32(dot - cstyleDst); | 85 int32_t index = SkToS32(dot - cstyleDst); |
| 71 dst->remove(index, dst->size() - index); | 86 dst->remove(index, dst->size() - index); |
| 72 } | 87 } |
| 73 dst->append(".png"); | 88 dst->append(suffix); |
| 74 } | 89 } |
| 75 } | 90 } |
| 76 | 91 |
| 92 // Store the names of the filenames to report later which ones failed, succeeded , and were | |
| 93 // invalid. | |
| 94 static SkTArray<SkString, false> gInvalidStreams; | |
| 95 static SkTArray<SkString, false> gMissingCodecs; | |
| 96 static SkTArray<SkString, false> gDecodeFailures; | |
| 97 static SkTArray<SkString, false> gEncodeFailures; | |
| 98 static SkTArray<SkString, false> gSuccessfulDecodes; | |
| 99 | |
| 100 static void decodeFileAndWrite(const char srcPath[], const SkString* writePath) { | |
| 101 SkBitmap bitmap; | |
| 102 SkFILEStream stream(srcPath); | |
| 103 if (!stream.isValid()) { | |
| 104 gInvalidStreams.push_back().set(srcPath); | |
| 105 return; | |
| 106 } | |
| 107 | |
| 108 SkImageDecoder* codec = SkImageDecoder::Factory(&stream); | |
| 109 if (NULL == codec) { | |
| 110 gMissingCodecs.push_back().set(srcPath); | |
| 111 return; | |
| 112 } | |
| 113 | |
| 114 SkAutoTDelete<SkImageDecoder> ad(codec); | |
| 115 | |
| 116 stream.rewind(); | |
| 117 if (!codec->decode(&stream, &bitmap, SkBitmap::kARGB_8888_Config, | |
| 118 SkImageDecoder::kDecodePixels_Mode)) { | |
| 119 gDecodeFailures.push_back().set(srcPath); | |
| 120 return; | |
| 121 } | |
| 122 | |
| 123 gSuccessfulDecodes.push_back().printf("%s [%d %d]", srcPath, bitmap.width(), bitmap.height()); | |
| 124 | |
| 125 if (FLAGS_reencode) { | |
| 126 // Encode to the format the file was originally in, or PNG if the encode r for the same | |
| 127 // format is unavailable. | |
| 128 SkImageEncoder::Type type = format_to_type(codec->getFormat()); | |
| 129 if (SkImageEncoder::kUnknown_Type == type) { | |
| 130 const char* dot = strrchr(srcPath, '.'); | |
| 131 if (NULL != dot) { | |
| 132 type = guess_type_from_suffix(dot); | |
| 133 } | |
| 134 if (SkImageEncoder::kUnknown_Type == type) { | |
| 135 SkDebugf("Could not determine type for '%s'\n", srcPath); | |
| 136 type = SkImageEncoder::kPNG_Type; | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 SkImageEncoder* encoder = SkImageEncoder::Create(type); | |
| 141 if (NULL == encoder) { | |
| 142 type = SkImageEncoder::kPNG_Type; | |
| 143 encoder = SkImageEncoder::Create(type); | |
| 144 SkASSERT(encoder); | |
| 145 } | |
| 146 SkAutoTDelete<SkImageEncoder> ade(encoder); | |
| 147 // Encode to a stream. | |
| 148 SkDynamicMemoryWStream wStream; | |
| 149 if (!encoder->encodeStream(&wStream, bitmap, 100)) { | |
| 150 gEncodeFailures.push_back().printf("Failed to reencode %s to type '% s'", srcPath, | |
| 151 suffix_for_type(type)); | |
| 152 return; | |
| 153 } | |
| 154 | |
| 155 SkAutoTUnref<SkData> data(wStream.copyToData()); | |
| 156 if (writePath != NULL && type != SkImageEncoder::kPNG_Type) { | |
| 157 // Write the encoded data to a file. Do not write to PNG, which will be written later, | |
| 158 // regardless of the input format. | |
| 159 SkString outPath; | |
| 160 make_outname(&outPath, writePath->c_str(), srcPath, suffix_for_type( type)); | |
| 161 SkFILEWStream file(outPath.c_str()); | |
| 162 if(file.write(data->data(), data->size())) { | |
| 163 gSuccessfulDecodes.push_back().appendf("\twrote %s", outPath.c_s tr()); | |
| 164 } else { | |
| 165 gEncodeFailures.push_back().printf("Failed to write %s", outPath .c_str()); | |
| 166 } | |
| 167 } | |
| 168 // Ensure that the reencoded data can still be decoded. | |
| 169 SkMemoryStream memStream(data); | |
| 170 SkBitmap redecodedBitmap; | |
| 171 if (!SkImageDecoder::DecodeStream(&memStream, &redecodedBitmap)) { | |
| 172 gDecodeFailures.push_back().printf("Failed to redecode %s after reen coding to '%s'", | |
| 173 srcPath, suffix_for_type(type)); | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 if (writePath != NULL) { | |
| 178 SkString outPath; | |
| 179 make_outname(&outPath, writePath->c_str(), srcPath, ".png"); | |
| 180 sk_tools::force_all_opaque(bitmap); | |
| 181 if (SkImageEncoder::EncodeFile(outPath.c_str(), bitmap, SkImageEncoder:: kPNG_Type, 100)) { | |
| 182 gSuccessfulDecodes.push_back().appendf("\twrote %s", outPath.c_str() ); | |
| 183 } else { | |
| 184 gEncodeFailures.push_back().set(outPath); | |
| 185 } | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 /////////////////////////////////////////////////////////////////////////////// | |
| 190 | |
| 77 // If strings is not empty, print title, followed by each string on its own line starting | 191 // If strings is not empty, print title, followed by each string on its own line starting |
| 78 // with a tab. | 192 // with a tab. |
| 79 static void print_strings(const char* title, const SkTArray<SkString, false>& st rings) { | 193 static void print_strings(const char* title, const SkTArray<SkString, false>& st rings) { |
| 80 if (strings.count() > 0) { | 194 if (strings.count() > 0) { |
| 81 SkDebugf("%s:\n", title); | 195 SkDebugf("%s:\n", title); |
| 82 for (int i = 0; i < strings.count(); i++) { | 196 for (int i = 0; i < strings.count(); i++) { |
| 83 SkDebugf("\t%s\n", strings[i].c_str()); | 197 SkDebugf("\t%s\n", strings[i].c_str()); |
| 84 } | 198 } |
| 85 SkDebugf("\n"); | 199 SkDebugf("\n"); |
| 86 } | 200 } |
| 87 } | 201 } |
| 88 | 202 |
| 89 static void decodeFileAndWrite(const char filePath[], const SkString* writePath) { | |
| 90 SkBitmap bitmap; | |
| 91 if (decodeFile(&bitmap, filePath)) { | |
| 92 if (writePath != NULL) { | |
| 93 SkString outPath; | |
| 94 make_outname(&outPath, writePath->c_str(), filePath); | |
| 95 successes.push_back().appendf("\twrote %s", outPath.c_str()); | |
| 96 SkImageEncoder::EncodeFile(outPath.c_str(), bitmap, SkImageEncoder:: kPNG_Type, 100); | |
| 97 } | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 int tool_main(int argc, char** argv); | 203 int tool_main(int argc, char** argv); |
| 102 int tool_main(int argc, char** argv) { | 204 int tool_main(int argc, char** argv) { |
| 103 SkCommandLineFlags::SetUsage("Decode files, and optionally write the results to files."); | 205 SkCommandLineFlags::SetUsage("Decode files, and optionally write the results to files."); |
| 104 SkCommandLineFlags::Parse(argc, argv); | 206 SkCommandLineFlags::Parse(argc, argv); |
| 105 | 207 |
| 106 if (FLAGS_readPath.count() < 1) { | 208 if (FLAGS_readPath.count() < 1) { |
| 107 SkDebugf("Folder(s) or image(s) to decode are required.\n"); | 209 SkDebugf("Folder(s) or image(s) to decode are required.\n"); |
| 108 return -1; | 210 return -1; |
| 109 } | 211 } |
| 110 | 212 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 141 decodeFileAndWrite(fullname.c_str(), outDirPtr); | 243 decodeFileAndWrite(fullname.c_str(), outDirPtr); |
| 142 } while (iter.next(&filename)); | 244 } while (iter.next(&filename)); |
| 143 } else { | 245 } else { |
| 144 decodeFileAndWrite(FLAGS_readPath[i], outDirPtr); | 246 decodeFileAndWrite(FLAGS_readPath[i], outDirPtr); |
| 145 } | 247 } |
| 146 } | 248 } |
| 147 | 249 |
| 148 // Add some space, since codecs may print warnings without newline. | 250 // Add some space, since codecs may print warnings without newline. |
| 149 SkDebugf("\n\n"); | 251 SkDebugf("\n\n"); |
| 150 | 252 |
| 151 print_strings("Invalid files", invalids); | 253 print_strings("Invalid files", gInvalidStreams); |
|
epoger
2013/04/19 21:35:16
I have to give the previous author style points fo
scroggo
2013/04/19 22:12:01
Haha, that would be me. Thanks ;)
| |
| 152 print_strings("Missing codec", nocodecs); | 254 print_strings("Missing codec", gMissingCodecs); |
| 153 print_strings("Failed to decode", failures); | 255 print_strings("Failed to decode", gDecodeFailures); |
| 154 print_strings("Decoded", successes); | 256 print_strings("Failed to encode", gEncodeFailures); |
| 257 print_strings("Decoded", gSuccessfulDecodes); | |
| 155 | 258 |
| 156 return 0; | 259 return 0; |
| 157 } | 260 } |
| 158 | 261 |
| 159 void forceLinking(); | 262 void forceLinking(); |
| 160 | 263 |
| 161 void forceLinking() { | 264 void forceLinking() { |
| 162 // This function leaks, but that is okay because it is not intended | 265 // This function leaks, but that is okay because it is not intended |
| 163 // to be called. It is only here so that the linker will include the | 266 // to be called. It is only here so that the linker will include the |
| 164 // decoders. | 267 // decoders. |
| 165 SkDEBUGCODE(SkImageDecoder *creator = ) CreateJPEGImageDecoder(); | 268 SkDEBUGCODE(SkImageDecoder *creator = ) CreateJPEGImageDecoder(); |
| 166 SkASSERT(creator); | 269 SkASSERT(creator); |
| 167 SkDEBUGCODE(creator = ) CreateWEBPImageDecoder(); | 270 SkDEBUGCODE(creator = ) CreateWEBPImageDecoder(); |
| 168 SkASSERT(creator); | 271 SkASSERT(creator); |
| 169 #ifdef SK_BUILD_FOR_UNIX | 272 #ifdef SK_BUILD_FOR_UNIX |
| 170 SkDEBUGCODE(creator = ) CreateGIFImageDecoder(); | 273 SkDEBUGCODE(creator = ) CreateGIFImageDecoder(); |
| 171 SkASSERT(creator); | 274 SkASSERT(creator); |
| 172 #endif | 275 #endif |
| 173 } | 276 } |
| 174 | 277 |
| 175 #if !defined SK_BUILD_FOR_IOS | 278 #if !defined SK_BUILD_FOR_IOS |
| 176 int main(int argc, char * const argv[]) { | 279 int main(int argc, char * const argv[]) { |
| 177 return tool_main(argc, (char**) argv); | 280 return tool_main(argc, (char**) argv); |
| 178 } | 281 } |
| 179 #endif | 282 #endif |
| OLD | NEW |