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 SkImageDecoder::Format guess_format_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].fFormat; | |
62 } | |
63 } | |
64 return SkImageDecoder::kUnknown_Format; | |
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 bitmap8888; | |
98 if (SkBitmap::kARGB_8888_Config != bm->config()) { | |
99 if (!bm->copyTo(&bitmap8888, SkBitmap::kARGB_8888_Config)) { | |
100 return false; | |
101 } | |
102 bm = &bitmap8888; | |
103 } | |
104 // FIXME: This forces all pixels to be opaque, like the many implementations | |
105 // of force_all_opaque. These should be unified if they cannot be eliminated . | |
106 SkAutoLockPixels lock(*bm); | |
107 for (int y = 0; y < bm->height(); y++) { | |
108 for (int x = 0; x < bm->width(); x++) { | |
109 *bm->getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT); | |
110 } | |
111 } | |
112 return SkImageEncoder::EncodeFile(outName, *bm, SkImageEncoder::kPNG_Type, 1 00); | |
113 } | |
114 | |
115 static void decodeFileAndWrite(const char srcPath[], const SkString* writePath) { | |
116 SkBitmap bitmap; | |
117 SkFILEStream stream(srcPath); | |
118 if (!stream.isValid()) { | |
119 gInvalidStreams.push_back().set(srcPath); | |
120 return; | |
121 } | |
122 | |
123 SkImageDecoder* codec = SkImageDecoder::Factory(&stream); | |
124 if (NULL == codec) { | |
125 gMissingCodecs.push_back().set(srcPath); | |
126 return; | |
127 } | |
128 | |
129 SkAutoTDelete<SkImageDecoder> ad(codec); | |
130 | |
131 stream.rewind(); | |
132 if (!codec->decode(&stream, &bitmap, SkBitmap::kARGB_8888_Config, | |
133 SkImageDecoder::kDecodePixels_Mode)) { | |
134 gDecodeFailures.push_back().set(srcPath); | |
135 return; | |
136 } | |
137 | |
138 gSuccessfulDecodes.push_back().printf("%s [%d %d]", srcPath, bitmap.width(), bitmap.height()); | |
139 | |
140 if (FLAGS_reencode) { | |
141 // Encode to the format the file was originally in, or PNG if the encode r for the same | |
142 // format is unavailable. | |
143 SkImageDecoder::Format format = codec->getFormat(); | |
144 if (SkImageDecoder::kUnknown_Format == format) { | |
145 if (stream.rewind()) { | |
146 format = SkImageDecoder::GetFormat(&stream); | |
147 } | |
148 if (SkImageDecoder::kUnknown_Format == format) { | |
149 const char* dot = strrchr(srcPath, '.'); | |
150 if (NULL != dot) { | |
151 format = guess_format_from_suffix(dot); | |
epoger
2013/04/25 14:38:43
Since there are two possible ways skimage can figu
| |
152 } | |
153 if (SkImageDecoder::kUnknown_Format == format) { | |
154 SkDebugf("Could not determine type for '%s'\n", srcPath); | |
155 format = SkImageDecoder::kPNG_Format; | |
156 } | |
157 | |
158 } | |
159 } else { | |
160 SkASSERT(!stream.rewind() || SkImageDecoder::GetFormat(&stream) == f ormat); | |
161 } | |
162 SkImageEncoder::Type type = format_to_type(format); | |
163 // format should never be kUnknown_Format, so type should never be kUnkn own_Type. | |
164 SkASSERT(type != SkImageEncoder::kUnknown_Type); | |
165 | |
166 SkImageEncoder* encoder = SkImageEncoder::Create(type); | |
167 if (NULL == encoder) { | |
168 type = SkImageEncoder::kPNG_Type; | |
169 encoder = SkImageEncoder::Create(type); | |
170 SkASSERT(encoder); | |
171 } | |
172 SkAutoTDelete<SkImageEncoder> ade(encoder); | |
173 // Encode to a stream. | |
174 SkDynamicMemoryWStream wStream; | |
175 if (!encoder->encodeStream(&wStream, bitmap, 100)) { | |
176 gEncodeFailures.push_back().printf("Failed to reencode %s to type '% s'", srcPath, | |
177 suffix_for_type(type)); | |
178 return; | |
179 } | |
180 | |
181 SkAutoTUnref<SkData> data(wStream.copyToData()); | |
182 if (writePath != NULL && type != SkImageEncoder::kPNG_Type) { | |
183 // Write the encoded data to a file. Do not write to PNG, which will be written later, | |
184 // regardless of the input format. | |
185 SkString outPath; | |
186 make_outname(&outPath, writePath->c_str(), srcPath, suffix_for_type( type)); | |
187 SkFILEWStream file(outPath.c_str()); | |
188 if(file.write(data->data(), data->size())) { | |
189 gSuccessfulDecodes.push_back().appendf("\twrote %s", outPath.c_s tr()); | |
190 } else { | |
191 gEncodeFailures.push_back().printf("Failed to write %s", outPath .c_str()); | |
192 } | |
193 } | |
194 // Ensure that the reencoded data can still be decoded. | |
195 SkMemoryStream memStream(data); | |
196 SkBitmap redecodedBitmap; | |
197 SkImageDecoder::Format formatOnSecondDecode; | |
198 if (SkImageDecoder::DecodeStream(&memStream, &redecodedBitmap, SkBitmap: :kNo_Config, | |
199 SkImageDecoder::kDecodePixels_Mode, | |
200 &formatOnSecondDecode)) { | |
201 SkASSERT(format_to_type(formatOnSecondDecode) == type); | |
202 } else { | |
203 gDecodeFailures.push_back().printf("Failed to redecode %s after reen coding to '%s'", | |
204 srcPath, suffix_for_type(type)); | |
205 } | |
206 } | |
207 | |
208 if (writePath != NULL) { | |
209 SkString outPath; | |
210 make_outname(&outPath, writePath->c_str(), srcPath, ".png"); | |
211 if (write_bitmap(outPath.c_str(), &bitmap)) { | |
212 gSuccessfulDecodes.push_back().appendf("\twrote %s", outPath.c_str() ); | |
213 } else { | |
214 gEncodeFailures.push_back().set(outPath); | |
215 } | |
216 } | |
217 } | |
218 | |
219 /////////////////////////////////////////////////////////////////////////////// | |
220 | |
77 // If strings is not empty, print title, followed by each string on its own line starting | 221 // If strings is not empty, print title, followed by each string on its own line starting |
78 // with a tab. | 222 // with a tab. |
79 static void print_strings(const char* title, const SkTArray<SkString, false>& st rings) { | 223 // @return bool True if strings had at least one entry. |
224 static bool print_strings(const char* title, const SkTArray<SkString, false>& st rings) { | |
80 if (strings.count() > 0) { | 225 if (strings.count() > 0) { |
81 SkDebugf("%s:\n", title); | 226 SkDebugf("%s:\n", title); |
82 for (int i = 0; i < strings.count(); i++) { | 227 for (int i = 0; i < strings.count(); i++) { |
83 SkDebugf("\t%s\n", strings[i].c_str()); | 228 SkDebugf("\t%s\n", strings[i].c_str()); |
84 } | 229 } |
85 SkDebugf("\n"); | 230 SkDebugf("\n"); |
231 return true; | |
86 } | 232 } |
87 } | 233 return false; |
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 } | |
99 } | 234 } |
100 | 235 |
101 int tool_main(int argc, char** argv); | 236 int tool_main(int argc, char** argv); |
102 int tool_main(int argc, char** argv) { | 237 int tool_main(int argc, char** argv) { |
103 SkCommandLineFlags::SetUsage("Decode files, and optionally write the results to files."); | 238 SkCommandLineFlags::SetUsage("Decode files, and optionally write the results to files."); |
104 SkCommandLineFlags::Parse(argc, argv); | 239 SkCommandLineFlags::Parse(argc, argv); |
105 | 240 |
106 if (FLAGS_readPath.count() < 1) { | 241 if (FLAGS_readPath.count() < 1) { |
107 SkDebugf("Folder(s) or image(s) to decode are required.\n"); | 242 SkDebugf("Folder(s) or image(s) to decode are required.\n"); |
108 return -1; | 243 return -1; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 decodeFileAndWrite(fullname.c_str(), outDirPtr); | 276 decodeFileAndWrite(fullname.c_str(), outDirPtr); |
142 } while (iter.next(&filename)); | 277 } while (iter.next(&filename)); |
143 } else { | 278 } else { |
144 decodeFileAndWrite(FLAGS_readPath[i], outDirPtr); | 279 decodeFileAndWrite(FLAGS_readPath[i], outDirPtr); |
145 } | 280 } |
146 } | 281 } |
147 | 282 |
148 // Add some space, since codecs may print warnings without newline. | 283 // Add some space, since codecs may print warnings without newline. |
149 SkDebugf("\n\n"); | 284 SkDebugf("\n\n"); |
150 | 285 |
151 print_strings("Invalid files", invalids); | 286 bool failed = print_strings("Invalid files", gInvalidStreams); |
152 print_strings("Missing codec", nocodecs); | 287 failed |= print_strings("Missing codec", gMissingCodecs); |
153 print_strings("Failed to decode", failures); | 288 failed |= print_strings("Failed to decode", gDecodeFailures); |
154 print_strings("Decoded", successes); | 289 failed |= print_strings("Failed to encode", gEncodeFailures); |
290 print_strings("Decoded", gSuccessfulDecodes); | |
155 | 291 |
156 return 0; | 292 return failed ? -1 : 0; |
157 } | 293 } |
158 | 294 |
159 void forceLinking(); | 295 void forceLinking(); |
160 | 296 |
161 void forceLinking() { | 297 void forceLinking() { |
162 // This function leaks, but that is okay because it is not intended | 298 // 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 | 299 // to be called. It is only here so that the linker will include the |
164 // decoders. | 300 // decoders. |
165 SkDEBUGCODE(SkImageDecoder *creator = ) CreateJPEGImageDecoder(); | 301 SkDEBUGCODE(SkImageDecoder *creator = ) CreateJPEGImageDecoder(); |
166 SkASSERT(creator); | 302 SkASSERT(creator); |
167 SkDEBUGCODE(creator = ) CreateWEBPImageDecoder(); | 303 SkDEBUGCODE(creator = ) CreateWEBPImageDecoder(); |
168 SkASSERT(creator); | 304 SkASSERT(creator); |
169 #ifdef SK_BUILD_FOR_UNIX | 305 #ifdef SK_BUILD_FOR_UNIX |
170 SkDEBUGCODE(creator = ) CreateGIFImageDecoder(); | 306 SkDEBUGCODE(creator = ) CreateGIFImageDecoder(); |
171 SkASSERT(creator); | 307 SkASSERT(creator); |
172 #endif | 308 #endif |
173 } | 309 } |
174 | 310 |
175 #if !defined SK_BUILD_FOR_IOS | 311 #if !defined SK_BUILD_FOR_IOS |
176 int main(int argc, char * const argv[]) { | 312 int main(int argc, char * const argv[]) { |
177 return tool_main(argc, (char**) argv); | 313 return tool_main(argc, (char**) argv); |
178 } | 314 } |
179 #endif | 315 #endif |
OLD | NEW |