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 "SkColorPriv.h" | |
| 9 #include "SkCommandLineFlags.h" | 10 #include "SkCommandLineFlags.h" |
| 11 #include "SkData.h" | |
| 10 #include "SkGraphics.h" | 12 #include "SkGraphics.h" |
| 11 #include "SkImageDecoder.h" | 13 #include "SkImageDecoder.h" |
| 12 #include "SkImageEncoder.h" | 14 #include "SkImageEncoder.h" |
| 13 #include "SkOSFile.h" | 15 #include "SkOSFile.h" |
| 14 #include "SkStream.h" | 16 #include "SkStream.h" |
| 15 #include "SkTArray.h" | 17 #include "SkTArray.h" |
| 16 #include "SkTemplates.h" | 18 #include "SkTemplates.h" |
| 17 | 19 |
| 18 | |
| 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::kBMP_Type, SkImageDecoder::kBMP_Format, ".bmp" }, |
| 31 if (!stream.isValid()) { | 32 { SkImageEncoder::kGIF_Type, SkImageDecoder::kGIF_Format, ".gif" }, |
| 32 invalids.push_back().set(srcPath); | 33 { SkImageEncoder::kICO_Type, SkImageDecoder::kICO_Format, ".ico" }, |
| 33 return false; | 34 { SkImageEncoder::kJPEG_Type, SkImageDecoder::kJPEG_Format, ".jpg" }, |
| 35 { SkImageEncoder::kPNG_Type, SkImageDecoder::kPNG_Format, ".png" }, | |
| 36 { SkImageEncoder::kWBMP_Type, SkImageDecoder::kWBMP_Format, ".wbmp" }, | |
| 37 { SkImageEncoder::kWEBP_Type, SkImageDecoder::kWEBP_Format, ".webp" } | |
| 38 }; | |
| 39 | |
| 40 static SkImageEncoder::Type format_to_type(SkImageDecoder::Format format) { | |
| 41 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { | |
| 42 if (gFormats[i].fFormat == format) { | |
| 43 return gFormats[i].fType; | |
| 44 } | |
| 34 } | 45 } |
| 35 | 46 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 } | 47 } |
| 54 | 48 |
| 55 /////////////////////////////////////////////////////////////////////////////// | 49 static const char* suffix_for_type(SkImageEncoder::Type type) { |
| 50 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { | |
| 51 if (gFormats[i].fType == type) { | |
| 52 return gFormats[i].fSuffix; | |
| 53 } | |
| 54 } | |
| 55 return ""; | |
| 56 } | |
| 56 | 57 |
| 57 static void make_outname(SkString* dst, const char outDir[], const char src[]) { | 58 static SkImageEncoder::Type guess_type_from_suffix(const char suffix[]) { |
| 59 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormats); i++) { | |
| 60 if (strcmp(suffix, gFormats[i].fSuffix) == 0) { | |
| 61 return gFormats[i].fType; | |
| 62 } | |
| 63 } | |
| 64 return SkImageEncoder::kUnknown_Type; | |
| 65 } | |
| 66 | |
| 67 static void make_outname(SkString* dst, const char outDir[], const char src[], | |
| 68 const char suffix[]) { | |
| 58 dst->set(outDir); | 69 dst->set(outDir); |
| 59 const char* start = strrchr(src, '/'); | 70 const char* start = strrchr(src, '/'); |
| 60 if (start) { | 71 if (start) { |
| 61 start += 1; // skip the actual last '/' | 72 start += 1; // skip the actual last '/' |
| 62 } else { | 73 } else { |
| 63 start = src; | 74 start = src; |
| 64 } | 75 } |
| 65 dst->append(start); | 76 dst->append(start); |
| 66 if (!dst->endsWith(".png")) { | 77 if (!dst->endsWith(suffix)) { |
| 67 const char* cstyleDst = dst->c_str(); | 78 const char* cstyleDst = dst->c_str(); |
| 68 const char* dot = strrchr(cstyleDst, '.'); | 79 const char* dot = strrchr(cstyleDst, '.'); |
| 69 if (dot != NULL) { | 80 if (dot != NULL) { |
| 70 int32_t index = SkToS32(dot - cstyleDst); | 81 int32_t index = SkToS32(dot - cstyleDst); |
| 71 dst->remove(index, dst->size() - index); | 82 dst->remove(index, dst->size() - index); |
| 72 } | 83 } |
| 73 dst->append(".png"); | 84 dst->append(suffix); |
| 74 } | 85 } |
| 75 } | 86 } |
| 76 | 87 |
| 88 // Store the names of the filenames to report later which ones failed, succeeded , and were | |
| 89 // invalid. | |
| 90 static SkTArray<SkString, false> gInvalidStreams; | |
| 91 static SkTArray<SkString, false> gMissingCodecs; | |
| 92 static SkTArray<SkString, false> gDecodeFailures; | |
| 93 static SkTArray<SkString, false> gEncodeFailures; | |
| 94 static SkTArray<SkString, false> gSuccessfulDecodes; | |
| 95 | |
| 96 static bool write_bitmap(const char outName[], SkBitmap* bm) { | |
| 97 SkBitmap* bmPtr; | |
|
epoger
2013/04/23 16:03:41
or, rather than creating this additional SkBitmap*
scroggo
2013/04/24 18:00:05
Done.
| |
| 98 SkBitmap bitmap8888; | |
| 99 if (SkBitmap::kARGB_8888_Config == bm->config()) { | |
| 100 bmPtr = bm; | |
| 101 } else { | |
| 102 if (!bm->copyTo(&bitmap8888, SkBitmap::kARGB_8888_Config)) { | |
| 103 return false; | |
| 104 } | |
| 105 bmPtr = &bitmap8888; | |
| 106 } | |
| 107 // FIXME: This forces all pixels to be opaque, like the many implementations | |
| 108 // of force_all_opaque. These should be unified if they cannot be eliminated . | |
| 109 SkAutoLockPixels lock(*bmPtr); | |
| 110 for (int y = 0; y < bmPtr->height(); y++) { | |
| 111 for (int x = 0; x < bmPtr->width(); x++) { | |
| 112 *bmPtr->getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT); | |
| 113 } | |
| 114 } | |
| 115 return SkImageEncoder::EncodeFile(outName, *bmPtr, SkImageEncoder::kPNG_Type , 100); | |
| 116 } | |
| 117 | |
| 118 static void decodeFileAndWrite(const char srcPath[], const SkString* writePath) { | |
| 119 SkBitmap bitmap; | |
| 120 SkFILEStream stream(srcPath); | |
| 121 if (!stream.isValid()) { | |
| 122 gInvalidStreams.push_back().set(srcPath); | |
| 123 return; | |
| 124 } | |
| 125 | |
| 126 SkImageDecoder* codec = SkImageDecoder::Factory(&stream); | |
| 127 if (NULL == codec) { | |
| 128 gMissingCodecs.push_back().set(srcPath); | |
| 129 return; | |
| 130 } | |
| 131 | |
| 132 SkAutoTDelete<SkImageDecoder> ad(codec); | |
| 133 | |
| 134 stream.rewind(); | |
| 135 if (!codec->decode(&stream, &bitmap, SkBitmap::kARGB_8888_Config, | |
| 136 SkImageDecoder::kDecodePixels_Mode)) { | |
| 137 gDecodeFailures.push_back().set(srcPath); | |
| 138 return; | |
| 139 } | |
| 140 | |
| 141 gSuccessfulDecodes.push_back().printf("%s [%d %d]", srcPath, bitmap.width(), bitmap.height()); | |
| 142 | |
| 143 if (FLAGS_reencode) { | |
| 144 // Encode to the format the file was originally in, or PNG if the encode r for the same | |
| 145 // format is unavailable. | |
| 146 SkImageDecoder::Format format = codec->getFormat(); | |
| 147 if (SkImageDecoder::kMultiple_Formats == format | |
| 148 || SkImageDecoder::kUnknown_Format == format) { | |
| 149 if (stream.rewind()) { | |
| 150 format = codec->getFormat(&stream); | |
| 151 } | |
| 152 } else { | |
| 153 SkASSERT(!stream.rewind() || codec->getFormat(&stream) == format); | |
| 154 } | |
| 155 SkImageEncoder::Type type = format_to_type(format); | |
| 156 if (SkImageEncoder::kUnknown_Type == type) { | |
| 157 const char* dot = strrchr(srcPath, '.'); | |
| 158 if (NULL != dot) { | |
| 159 type = guess_type_from_suffix(dot); | |
| 160 } | |
| 161 if (SkImageEncoder::kUnknown_Type == type) { | |
| 162 SkDebugf("Could not determine type for '%s'\n", srcPath); | |
| 163 type = SkImageEncoder::kPNG_Type; | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 SkImageEncoder* encoder = SkImageEncoder::Create(type); | |
| 168 if (NULL == encoder) { | |
| 169 type = SkImageEncoder::kPNG_Type; | |
| 170 encoder = SkImageEncoder::Create(type); | |
| 171 SkASSERT(encoder); | |
| 172 } | |
| 173 SkAutoTDelete<SkImageEncoder> ade(encoder); | |
| 174 // Encode to a stream. | |
| 175 SkDynamicMemoryWStream wStream; | |
| 176 if (!encoder->encodeStream(&wStream, bitmap, 100)) { | |
| 177 gEncodeFailures.push_back().printf("Failed to reencode %s to type '% s'", srcPath, | |
| 178 suffix_for_type(type)); | |
| 179 return; | |
| 180 } | |
| 181 | |
| 182 SkAutoTUnref<SkData> data(wStream.copyToData()); | |
| 183 if (writePath != NULL && type != SkImageEncoder::kPNG_Type) { | |
| 184 // Write the encoded data to a file. Do not write to PNG, which will be written later, | |
| 185 // regardless of the input format. | |
| 186 SkString outPath; | |
| 187 make_outname(&outPath, writePath->c_str(), srcPath, suffix_for_type( type)); | |
| 188 SkFILEWStream file(outPath.c_str()); | |
| 189 if(file.write(data->data(), data->size())) { | |
| 190 gSuccessfulDecodes.push_back().appendf("\twrote %s", outPath.c_s tr()); | |
| 191 } else { | |
| 192 gEncodeFailures.push_back().printf("Failed to write %s", outPath .c_str()); | |
| 193 } | |
| 194 } | |
| 195 // Ensure that the reencoded data can still be decoded. | |
| 196 SkMemoryStream memStream(data); | |
| 197 SkBitmap redecodedBitmap; | |
| 198 if (!SkImageDecoder::DecodeStream(&memStream, &redecodedBitmap)) { | |
| 199 gDecodeFailures.push_back().printf("Failed to redecode %s after reen coding to '%s'", | |
| 200 srcPath, suffix_for_type(type)); | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 if (writePath != NULL) { | |
| 205 SkString outPath; | |
| 206 make_outname(&outPath, writePath->c_str(), srcPath, ".png"); | |
| 207 if (write_bitmap(outPath.c_str(), &bitmap)) { | |
| 208 gSuccessfulDecodes.push_back().appendf("\twrote %s", outPath.c_str() ); | |
| 209 } else { | |
| 210 gEncodeFailures.push_back().set(outPath); | |
| 211 } | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 /////////////////////////////////////////////////////////////////////////////// | |
| 216 | |
| 77 // If strings is not empty, print title, followed by each string on its own line starting | 217 // If strings is not empty, print title, followed by each string on its own line starting |
| 78 // with a tab. | 218 // with a tab. |
| 79 static void print_strings(const char* title, const SkTArray<SkString, false>& st rings) { | 219 static void print_strings(const char* title, const SkTArray<SkString, false>& st rings) { |
| 80 if (strings.count() > 0) { | 220 if (strings.count() > 0) { |
| 81 SkDebugf("%s:\n", title); | 221 SkDebugf("%s:\n", title); |
| 82 for (int i = 0; i < strings.count(); i++) { | 222 for (int i = 0; i < strings.count(); i++) { |
| 83 SkDebugf("\t%s\n", strings[i].c_str()); | 223 SkDebugf("\t%s\n", strings[i].c_str()); |
| 84 } | 224 } |
| 85 SkDebugf("\n"); | 225 SkDebugf("\n"); |
| 86 } | 226 } |
| 87 } | 227 } |
| 88 | 228 |
| 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); | 229 int tool_main(int argc, char** argv); |
| 102 int tool_main(int argc, char** argv) { | 230 int tool_main(int argc, char** argv) { |
| 103 SkCommandLineFlags::SetUsage("Decode files, and optionally write the results to files."); | 231 SkCommandLineFlags::SetUsage("Decode files, and optionally write the results to files."); |
| 104 SkCommandLineFlags::Parse(argc, argv); | 232 SkCommandLineFlags::Parse(argc, argv); |
| 105 | 233 |
| 106 if (FLAGS_readPath.count() < 1) { | 234 if (FLAGS_readPath.count() < 1) { |
| 107 SkDebugf("Folder(s) or image(s) to decode are required.\n"); | 235 SkDebugf("Folder(s) or image(s) to decode are required.\n"); |
| 108 return -1; | 236 return -1; |
| 109 } | 237 } |
| 110 | 238 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 141 decodeFileAndWrite(fullname.c_str(), outDirPtr); | 269 decodeFileAndWrite(fullname.c_str(), outDirPtr); |
| 142 } while (iter.next(&filename)); | 270 } while (iter.next(&filename)); |
| 143 } else { | 271 } else { |
| 144 decodeFileAndWrite(FLAGS_readPath[i], outDirPtr); | 272 decodeFileAndWrite(FLAGS_readPath[i], outDirPtr); |
| 145 } | 273 } |
| 146 } | 274 } |
| 147 | 275 |
| 148 // Add some space, since codecs may print warnings without newline. | 276 // Add some space, since codecs may print warnings without newline. |
| 149 SkDebugf("\n\n"); | 277 SkDebugf("\n\n"); |
| 150 | 278 |
| 151 print_strings("Invalid files", invalids); | 279 print_strings("Invalid files", gInvalidStreams); |
| 152 print_strings("Missing codec", nocodecs); | 280 print_strings("Missing codec", gMissingCodecs); |
| 153 print_strings("Failed to decode", failures); | 281 print_strings("Failed to decode", gDecodeFailures); |
| 154 print_strings("Decoded", successes); | 282 print_strings("Failed to encode", gEncodeFailures); |
| 283 print_strings("Decoded", gSuccessfulDecodes); | |
| 155 | 284 |
| 156 return 0; | 285 return 0; |
| 157 } | 286 } |
| 158 | 287 |
| 159 void forceLinking(); | 288 void forceLinking(); |
| 160 | 289 |
| 161 void forceLinking() { | 290 void forceLinking() { |
| 162 // This function leaks, but that is okay because it is not intended | 291 // 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 | 292 // to be called. It is only here so that the linker will include the |
| 164 // decoders. | 293 // decoders. |
| 165 SkDEBUGCODE(SkImageDecoder *creator = ) CreateJPEGImageDecoder(); | 294 SkDEBUGCODE(SkImageDecoder *creator = ) CreateJPEGImageDecoder(); |
| 166 SkASSERT(creator); | 295 SkASSERT(creator); |
| 167 SkDEBUGCODE(creator = ) CreateWEBPImageDecoder(); | 296 SkDEBUGCODE(creator = ) CreateWEBPImageDecoder(); |
| 168 SkASSERT(creator); | 297 SkASSERT(creator); |
| 169 #ifdef SK_BUILD_FOR_UNIX | 298 #ifdef SK_BUILD_FOR_UNIX |
| 170 SkDEBUGCODE(creator = ) CreateGIFImageDecoder(); | 299 SkDEBUGCODE(creator = ) CreateGIFImageDecoder(); |
| 171 SkASSERT(creator); | 300 SkASSERT(creator); |
| 172 #endif | 301 #endif |
| 173 } | 302 } |
| 174 | 303 |
| 175 #if !defined SK_BUILD_FOR_IOS | 304 #if !defined SK_BUILD_FOR_IOS |
| 176 int main(int argc, char * const argv[]) { | 305 int main(int argc, char * const argv[]) { |
| 177 return tool_main(argc, (char**) argv); | 306 return tool_main(argc, (char**) argv); |
| 178 } | 307 } |
| 179 #endif | 308 #endif |
| OLD | NEW |