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 } |
36 return true; | 50 return true; |
djsollen
2013/04/11 14:17:44
add the srcPath to successes here so that all the
scroggo
2013/04/11 15:32:20
Done. decodeAndWriteFile still adds a string stati
| |
37 } | 51 } |
38 | 52 |
39 /////////////////////////////////////////////////////////////////////////////// | 53 /////////////////////////////////////////////////////////////////////////////// |
40 | 54 |
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[]) { | 55 static void make_outname(SkString* dst, const char outDir[], const char src[]) { |
46 dst->set(outDir); | 56 dst->set(outDir); |
47 const char* start = strrchr(src, '/'); | 57 const char* start = strrchr(src, '/'); |
48 if (start) { | 58 if (start) { |
49 start += 1; // skip the actual last '/' | 59 start += 1; // skip the actual last '/' |
50 } else { | 60 } else { |
51 start = src; | 61 start = src; |
52 } | 62 } |
53 dst->append(start); | 63 dst->append(start); |
54 dst->append(".png"); | 64 if (!dst->endsWith(".png")) { |
65 const char* cstyleDst = dst->c_str(); | |
66 const char* dot = strrchr(cstyleDst, '.'); | |
67 if (dot != NULL) { | |
68 int index = dot - cstyleDst; | |
69 dst->remove(index, dst->size() - index); | |
70 } | |
71 dst->append(".png"); | |
72 } | |
55 } | 73 } |
56 | 74 |
75 // If strings is not empty, print title, followed by each string on its own line starting | |
76 // with a tab. | |
77 static void print_strings(const char* title, const SkTArray<SkString, false>& st rings) { | |
78 if (strings.count() > 0) { | |
79 SkDebugf("%s:\n", title); | |
80 for (int i = 0; i < strings.count(); i++) { | |
81 SkDebugf("\t%s\n", strings[i].c_str()); | |
82 } | |
83 SkDebugf("\n"); | |
84 } | |
85 } | |
86 | |
87 void decodeFileAndWrite(const char filePath[], const SkString* writePath); | |
djsollen
2013/04/11 14:17:44
why not just move the function here?
scroggo
2013/04/11 15:32:20
I was hoping to make the diff simpler, but it come
| |
88 | |
57 int tool_main(int argc, char** argv); | 89 int tool_main(int argc, char** argv); |
58 int tool_main(int argc, char** argv) { | 90 int tool_main(int argc, char** argv) { |
91 SkCommandLineFlags::SetUsage("Decode files, and optionally write the results to files."); | |
92 SkCommandLineFlags::Parse(argc, argv); | |
93 | |
94 if (FLAGS_readPath.count() < 1) { | |
95 SkDebugf("Folder(s) or image(s) to decode are required.\n"); | |
96 return -1; | |
97 } | |
98 | |
99 | |
59 SkAutoGraphics ag; | 100 SkAutoGraphics ag; |
60 int i, outDirIndex = 0; | 101 |
61 SkString outDir; | 102 SkString outDir; |
103 SkString* outDirPtr; | |
62 | 104 |
63 for (i = 1; i < argc; i++) { | 105 if (FLAGS_writePath.count() == 1) { |
64 if (!strcmp(argv[i], "-help")) { | 106 outDir.set(FLAGS_writePath[0]); |
65 show_help(); | 107 if (outDir.c_str()[outDir.size() - 1] != '/') { |
66 return 0; | 108 outDir.append("/"); |
67 } | 109 } |
68 if (!strcmp(argv[i], "-o")) { | 110 outDirPtr = &outDir; |
69 if (i == argc-1) { | 111 } else { |
70 SkDebugf("ERROR: -o needs a following filename\n"); | 112 outDirPtr = NULL; |
71 return -1; | 113 } |
114 | |
115 for (int i = 0; i < FLAGS_readPath.count(); i++) { | |
116 if (strlen(FLAGS_readPath[i]) < 1) { | |
117 break; | |
118 } | |
119 SkOSFile::Iter iter(FLAGS_readPath[i]); | |
120 SkString filename; | |
121 if (iter.next(&filename)) { | |
122 SkString directory(FLAGS_readPath[i]); | |
123 if (directory[directory.size() - 1] != '/') { | |
124 directory.append("/"); | |
72 } | 125 } |
73 outDirIndex = i; | 126 do { |
74 outDir.set(argv[i+1]); | 127 SkString fullname(directory); |
75 if (outDir.c_str()[outDir.size() - 1] != '/') { | 128 fullname.append(filename); |
76 outDir.append("/"); | 129 decodeFileAndWrite(fullname.c_str(), outDirPtr); |
77 } | 130 } while (iter.next(&filename)); |
78 i += 1; // skip the out dir name | 131 } else { |
132 decodeFileAndWrite(FLAGS_readPath[i], outDirPtr); | |
79 } | 133 } |
80 } | 134 } |
81 | 135 |
82 for (i = 1; i < argc; i++) { | 136 // Add some space, since codecs may print warnings without newline. |
83 if (i == outDirIndex) { | 137 SkDebugf("\n\n"); |
84 i += 1; // skip this and the next entry | |
85 continue; | |
86 } | |
87 | 138 |
88 SkBitmap bitmap; | 139 print_strings("Invalid files", invalids); |
89 if (decodeFile(&bitmap, argv[i])) { | 140 print_strings("Missing codec", nocodecs); |
90 if (outDirIndex) { | 141 print_strings("Failed to decode", failures); |
91 SkString outPath; | 142 print_strings("Decoded", successes); |
92 make_outname(&outPath, outDir.c_str(), argv[i]); | 143 |
93 SkDebugf(" writing %s\n", outPath.c_str()); | 144 return 0; |
94 SkImageEncoder::EncodeFile(outPath.c_str(), bitmap, | 145 } |
95 SkImageEncoder::kPNG_Type, 100); | 146 |
96 } else { | 147 void decodeFileAndWrite(const char filePath[], const SkString* writePath) { |
97 SkDebugf(" decoded %s [%d %d]\n", argv[i], bitmap.width(), | 148 SkBitmap bitmap; |
98 bitmap.height()); | 149 if (decodeFile(&bitmap, filePath)) { |
99 } | 150 SkString& result = successes.push_back(); |
151 result.printf("%s [%d %d]", filePath, bitmap.width(), bitmap.height()); | |
djsollen
2013/04/11 14:17:44
move these two lines in decodeFile
scroggo
2013/04/11 15:32:20
Done.
| |
152 if (writePath != NULL) { | |
153 SkString outPath; | |
154 make_outname(&outPath, writePath->c_str(), filePath); | |
155 result.appendf("\n\t\twrote %s", outPath.c_str()); | |
156 SkImageEncoder::EncodeFile(outPath.c_str(), bitmap, SkImageEncoder:: kPNG_Type, 100); | |
100 } | 157 } |
101 } | 158 } |
159 } | |
102 | 160 |
103 return 0; | 161 void forceLinking(); |
djsollen
2013/04/11 14:17:44
this isn't needed.
robertphillips
2013/04/11 14:29:53
I think it was added elsewhere to silence some iOS
scroggo
2013/04/11 15:32:20
Cary added these to fix warnings on Mac in https:/
| |
162 | |
163 void forceLinking() { | |
164 SkDEBUGCODE(SkImageDecoder *creator = ) CreateJPEGImageDecoder(); | |
165 SkASSERT(creator); | |
104 } | 166 } |
105 | 167 |
106 #if !defined SK_BUILD_FOR_IOS | 168 #if !defined SK_BUILD_FOR_IOS |
107 int main(int argc, char * const argv[]) { | 169 int main(int argc, char * const argv[]) { |
108 return tool_main(argc, (char**) argv); | 170 return tool_main(argc, (char**) argv); |
109 } | 171 } |
110 #endif | 172 #endif |
OLD | NEW |