OLD | NEW |
1 | |
2 /* | 1 /* |
3 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
4 * | 3 * |
5 * 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 |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
| 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkCommandLineFlags.h" |
9 #include "SkGraphics.h" | 10 #include "SkGraphics.h" |
10 #include "SkImageDecoder.h" | 11 #include "SkImageDecoder.h" |
11 #include "SkImageEncoder.h" | 12 #include "SkImageEncoder.h" |
| 13 #include "SkOSFile.h" |
12 #include "SkStream.h" | 14 #include "SkStream.h" |
| 15 #include "SkTArray.h" |
13 #include "SkTemplates.h" | 16 #include "SkTemplates.h" |
14 | 17 |
| 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 |
15 static bool decodeFile(SkBitmap* bitmap, const char srcPath[]) { | 29 static bool decodeFile(SkBitmap* bitmap, const char srcPath[]) { |
16 SkFILEStream stream(srcPath); | 30 SkFILEStream stream(srcPath); |
17 if (!stream.isValid()) { | 31 if (!stream.isValid()) { |
18 SkDebugf("ERROR: bad filename <%s>\n", srcPath); | 32 invalids.push_back().set(srcPath); |
19 return false; | 33 return false; |
20 } | 34 } |
21 | 35 |
22 SkImageDecoder* codec = SkImageDecoder::Factory(&stream); | 36 SkImageDecoder* codec = SkImageDecoder::Factory(&stream); |
23 if (NULL == codec) { | 37 if (NULL == codec) { |
24 SkDebugf("ERROR: no codec found for <%s>\n", srcPath); | 38 nocodecs.push_back().set(srcPath); |
25 return false; | 39 return false; |
26 } | 40 } |
27 | 41 |
28 SkAutoTDelete<SkImageDecoder> ad(codec); | 42 SkAutoTDelete<SkImageDecoder> ad(codec); |
29 | 43 |
30 stream.rewind(); | 44 stream.rewind(); |
31 if (!codec->decode(&stream, bitmap, SkBitmap::kARGB_8888_Config, | 45 if (!codec->decode(&stream, bitmap, SkBitmap::kARGB_8888_Config, |
32 SkImageDecoder::kDecodePixels_Mode)) { | 46 SkImageDecoder::kDecodePixels_Mode)) { |
33 SkDebugf("ERROR: codec failed for <%s>\n", srcPath); | 47 failures.push_back().set(srcPath); |
34 return false; | 48 return false; |
35 } | 49 } |
| 50 |
| 51 successes.push_back().printf("%s [%d %d]", srcPath, bitmap->width(), bitmap-
>height()); |
36 return true; | 52 return true; |
37 } | 53 } |
38 | 54 |
39 /////////////////////////////////////////////////////////////////////////////// | 55 /////////////////////////////////////////////////////////////////////////////// |
40 | 56 |
41 static void show_help() { | |
42 SkDebugf("usage: skiamge [-o out-dir] inputfiles...\n"); | |
43 } | |
44 | |
45 static void make_outname(SkString* dst, const char outDir[], const char src[]) { | 57 static void make_outname(SkString* dst, const char outDir[], const char src[]) { |
46 dst->set(outDir); | 58 dst->set(outDir); |
47 const char* start = strrchr(src, '/'); | 59 const char* start = strrchr(src, '/'); |
48 if (start) { | 60 if (start) { |
49 start += 1; // skip the actual last '/' | 61 start += 1; // skip the actual last '/' |
50 } else { | 62 } else { |
51 start = src; | 63 start = src; |
52 } | 64 } |
53 dst->append(start); | 65 dst->append(start); |
54 dst->append(".png"); | 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>& st
rings) { |
| 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 } |
55 } | 99 } |
56 | 100 |
57 int tool_main(int argc, char** argv); | 101 int tool_main(int argc, char** argv); |
58 int tool_main(int argc, char** argv) { | 102 int tool_main(int argc, char** argv) { |
| 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 |
59 SkAutoGraphics ag; | 112 SkAutoGraphics ag; |
60 int i, outDirIndex = 0; | 113 |
61 SkString outDir; | 114 SkString outDir; |
| 115 SkString* outDirPtr; |
62 | 116 |
63 for (i = 1; i < argc; i++) { | 117 if (FLAGS_writePath.count() == 1) { |
64 if (!strcmp(argv[i], "-help")) { | 118 outDir.set(FLAGS_writePath[0]); |
65 show_help(); | 119 if (outDir.c_str()[outDir.size() - 1] != '/') { |
66 return 0; | 120 outDir.append("/"); |
67 } | 121 } |
68 if (!strcmp(argv[i], "-o")) { | 122 outDirPtr = &outDir; |
69 if (i == argc-1) { | 123 } else { |
70 SkDebugf("ERROR: -o needs a following filename\n"); | 124 outDirPtr = NULL; |
71 return -1; | 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("/"); |
72 } | 137 } |
73 outDirIndex = i; | 138 do { |
74 outDir.set(argv[i+1]); | 139 SkString fullname(directory); |
75 if (outDir.c_str()[outDir.size() - 1] != '/') { | 140 fullname.append(filename); |
76 outDir.append("/"); | 141 decodeFileAndWrite(fullname.c_str(), outDirPtr); |
77 } | 142 } while (iter.next(&filename)); |
78 i += 1; // skip the out dir name | 143 } else { |
| 144 decodeFileAndWrite(FLAGS_readPath[i], outDirPtr); |
79 } | 145 } |
80 } | 146 } |
81 | 147 |
82 for (i = 1; i < argc; i++) { | 148 // Add some space, since codecs may print warnings without newline. |
83 if (i == outDirIndex) { | 149 SkDebugf("\n\n"); |
84 i += 1; // skip this and the next entry | |
85 continue; | |
86 } | |
87 | 150 |
88 SkBitmap bitmap; | 151 print_strings("Invalid files", invalids); |
89 if (decodeFile(&bitmap, argv[i])) { | 152 print_strings("Missing codec", nocodecs); |
90 if (outDirIndex) { | 153 print_strings("Failed to decode", failures); |
91 SkString outPath; | 154 print_strings("Decoded", successes); |
92 make_outname(&outPath, outDir.c_str(), argv[i]); | |
93 SkDebugf(" writing %s\n", outPath.c_str()); | |
94 SkImageEncoder::EncodeFile(outPath.c_str(), bitmap, | |
95 SkImageEncoder::kPNG_Type, 100); | |
96 } else { | |
97 SkDebugf(" decoded %s [%d %d]\n", argv[i], bitmap.width(), | |
98 bitmap.height()); | |
99 } | |
100 } | |
101 } | |
102 | 155 |
103 return 0; | 156 return 0; |
104 } | 157 } |
105 | 158 |
| 159 void forceLinking(); |
| 160 |
| 161 void forceLinking() { |
| 162 SkDEBUGCODE(SkImageDecoder *creator = ) CreateJPEGImageDecoder(); |
| 163 SkASSERT(creator); |
| 164 } |
| 165 |
106 #if !defined SK_BUILD_FOR_IOS | 166 #if !defined SK_BUILD_FOR_IOS |
107 int main(int argc, char * const argv[]) { | 167 int main(int argc, char * const argv[]) { |
108 return tool_main(argc, (char**) argv); | 168 return tool_main(argc, (char**) argv); |
109 } | 169 } |
110 #endif | 170 #endif |
OLD | NEW |