| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 7 | |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 9 | #include "SkCommandLineFlags.h" |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 10 | #include "SkGraphics.h" |
| 11 | #include "SkImageDecoder.h" |
| 12 | #include "SkImageEncoder.h" |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 13 | #include "SkOSFile.h" |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 14 | #include "SkStream.h" |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 15 | #include "SkTArray.h" |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 16 | #include "SkTemplates.h" |
| 17 | |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 18 | |
| 19 | DEFINE_string2(readPath, r, "", "Folder(s) and files to decode images. Required."); |
| 20 | DEFINE_string2(writePath, w, "", "Write rendered images into this directory."); |
| 21 | |
| 22 | // Store the names of the filenames to report later which ones failed, succeeded, and were |
| 23 | // invalid. |
| 24 | static SkTArray<SkString, false> invalids; |
| 25 | static SkTArray<SkString, false> nocodecs; |
| 26 | static SkTArray<SkString, false> failures; |
| 27 | static SkTArray<SkString, false> successes; |
| 28 | |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 29 | static bool decodeFile(SkBitmap* bitmap, const char srcPath[]) { |
| 30 | SkFILEStream stream(srcPath); |
| 31 | if (!stream.isValid()) { |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 32 | invalids.push_back().set(srcPath); |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 33 | return false; |
| 34 | } |
| 35 | |
| 36 | SkImageDecoder* codec = SkImageDecoder::Factory(&stream); |
| 37 | if (NULL == codec) { |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 38 | nocodecs.push_back().set(srcPath); |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 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)) { |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 47 | failures.push_back().set(srcPath); |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 48 | return false; |
| 49 | } |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 50 | |
| 51 | successes.push_back().printf("%s [%d %d]", srcPath, bitmap->width(), bitmap->height()); |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 52 | return true; |
| 53 | } |
| 54 | |
| 55 | /////////////////////////////////////////////////////////////////////////////// |
| 56 | |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 57 | static void make_outname(SkString* dst, const char outDir[], const char src[]) { |
| 58 | dst->set(outDir); |
| 59 | const char* start = strrchr(src, '/'); |
| 60 | if (start) { |
| 61 | start += 1; // skip the actual last '/' |
| 62 | } else { |
| 63 | start = src; |
| 64 | } |
| 65 | dst->append(start); |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 66 | if (!dst->endsWith(".png")) { |
| 67 | const char* cstyleDst = dst->c_str(); |
| 68 | const char* dot = strrchr(cstyleDst, '.'); |
| 69 | if (dot != NULL) { |
| 70 | int32_t index = SkToS32(dot - cstyleDst); |
| 71 | dst->remove(index, dst->size() - index); |
| 72 | } |
| 73 | dst->append(".png"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // If strings is not empty, print title, followed by each string on its own line starting |
| 78 | // with a tab. |
| 79 | static void print_strings(const char* title, const SkTArray<SkString, false>& strings) { |
| 80 | if (strings.count() > 0) { |
| 81 | SkDebugf("%s:\n", title); |
| 82 | for (int i = 0; i < strings.count(); i++) { |
| 83 | SkDebugf("\t%s\n", strings[i].c_str()); |
| 84 | } |
| 85 | SkDebugf("\n"); |
| 86 | } |
| 87 | } |
| 88 | |
| 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 | } |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 101 | int tool_main(int argc, char** argv); |
| 102 | int tool_main(int argc, char** argv) { |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 103 | SkCommandLineFlags::SetUsage("Decode files, and optionally write the results to files."); |
| 104 | SkCommandLineFlags::Parse(argc, argv); |
| 105 | |
| 106 | if (FLAGS_readPath.count() < 1) { |
| 107 | SkDebugf("Folder(s) or image(s) to decode are required.\n"); |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 112 | SkAutoGraphics ag; |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 113 | |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 114 | SkString outDir; |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 115 | SkString* outDirPtr; |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 116 | |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 117 | if (FLAGS_writePath.count() == 1) { |
| 118 | outDir.set(FLAGS_writePath[0]); |
| 119 | if (outDir.c_str()[outDir.size() - 1] != '/') { |
| 120 | outDir.append("/"); |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 121 | } |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 122 | outDirPtr = &outDir; |
| 123 | } else { |
| 124 | outDirPtr = NULL; |
| 125 | } |
| 126 | |
| 127 | for (int i = 0; i < FLAGS_readPath.count(); i++) { |
| 128 | if (strlen(FLAGS_readPath[i]) < 1) { |
| 129 | break; |
| 130 | } |
| 131 | SkOSFile::Iter iter(FLAGS_readPath[i]); |
| 132 | SkString filename; |
| 133 | if (iter.next(&filename)) { |
| 134 | SkString directory(FLAGS_readPath[i]); |
| 135 | if (directory[directory.size() - 1] != '/') { |
| 136 | directory.append("/"); |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 137 | } |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 138 | do { |
| 139 | SkString fullname(directory); |
| 140 | fullname.append(filename); |
| 141 | decodeFileAndWrite(fullname.c_str(), outDirPtr); |
| 142 | } while (iter.next(&filename)); |
| 143 | } else { |
| 144 | decodeFileAndWrite(FLAGS_readPath[i], outDirPtr); |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 148 | // Add some space, since codecs may print warnings without newline. |
| 149 | SkDebugf("\n\n"); |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 150 | |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 151 | print_strings("Invalid files", invalids); |
| 152 | print_strings("Missing codec", nocodecs); |
| 153 | print_strings("Failed to decode", failures); |
| 154 | print_strings("Decoded", successes); |
| reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 159 | void forceLinking(); |
| 160 | |
| 161 | void forceLinking() { |
| scroggo@google.com | 9c5f969 | 2013-04-11 18:32:01 +0000 | [diff] [blame] | 162 | // 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 |
| 164 | // decoders. |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 165 | SkDEBUGCODE(SkImageDecoder *creator = ) CreateJPEGImageDecoder(); |
| 166 | SkASSERT(creator); |
| scroggo@google.com | aaec851 | 2013-04-15 22:05:03 +0000 | [diff] [blame] | 167 | SkDEBUGCODE(creator = ) CreateWEBPImageDecoder(); |
| 168 | SkASSERT(creator); |
| scroggo@google.com | 9c5f969 | 2013-04-11 18:32:01 +0000 | [diff] [blame] | 169 | #ifdef SK_BUILD_FOR_UNIX |
| 170 | SkDEBUGCODE(creator = ) CreateGIFImageDecoder(); |
| 171 | SkASSERT(creator); |
| 172 | #endif |
| scroggo@google.com | b41ff95 | 2013-04-11 15:53:35 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 175 | #if !defined SK_BUILD_FOR_IOS |
| 176 | int main(int argc, char * const argv[]) { |
| 177 | return tool_main(argc, (char**) argv); |
| 178 | } |
| 179 | #endif |