blob: 7906386cdce2fb7f25999863f0c215826a7349ac [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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.comb41ff952013-04-11 15:53:35 +00007
reed@android.comaf459792009-04-24 19:52:53 +00008#include "SkBitmap.h"
scroggo@google.com39edf4c2013-04-25 17:33:51 +00009#include "SkColorPriv.h"
scroggo@google.comb41ff952013-04-11 15:53:35 +000010#include "SkCommandLineFlags.h"
scroggo@google.com39edf4c2013-04-25 17:33:51 +000011#include "SkData.h"
reed@android.comaf459792009-04-24 19:52:53 +000012#include "SkGraphics.h"
13#include "SkImageDecoder.h"
14#include "SkImageEncoder.h"
scroggo@google.comb41ff952013-04-11 15:53:35 +000015#include "SkOSFile.h"
reed@android.comaf459792009-04-24 19:52:53 +000016#include "SkStream.h"
scroggo@google.comb41ff952013-04-11 15:53:35 +000017#include "SkTArray.h"
reed@android.comaf459792009-04-24 19:52:53 +000018#include "SkTemplates.h"
19
scroggo@google.comb41ff952013-04-11 15:53:35 +000020DEFINE_string2(readPath, r, "", "Folder(s) and files to decode images. Required.");
21DEFINE_string2(writePath, w, "", "Write rendered images into this directory.");
scroggo@google.com39edf4c2013-04-25 17:33:51 +000022DEFINE_bool(reencode, true, "Reencode the images to test encoding.");
scroggo@google.comb41ff952013-04-11 15:53:35 +000023
scroggo@google.com39edf4c2013-04-25 17:33:51 +000024struct Format {
25 SkImageEncoder::Type fType;
26 SkImageDecoder::Format fFormat;
27 const char* fSuffix;
28};
scroggo@google.comb41ff952013-04-11 15:53:35 +000029
scroggo@google.com39edf4c2013-04-25 17:33:51 +000030static const Format gFormats[] = {
31 { SkImageEncoder::kBMP_Type, SkImageDecoder::kBMP_Format, ".bmp" },
32 { SkImageEncoder::kGIF_Type, SkImageDecoder::kGIF_Format, ".gif" },
33 { SkImageEncoder::kICO_Type, SkImageDecoder::kICO_Format, ".ico" },
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
40static 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 }
reed@android.comaf459792009-04-24 19:52:53 +000045 }
scroggo@google.com39edf4c2013-04-25 17:33:51 +000046 return SkImageEncoder::kUnknown_Type;
reed@android.comaf459792009-04-24 19:52:53 +000047}
48
scroggo@google.com39edf4c2013-04-25 17:33:51 +000049static 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}
reed@android.comaf459792009-04-24 19:52:53 +000057
scroggo@google.com39edf4c2013-04-25 17:33:51 +000058static 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
67static void make_outname(SkString* dst, const char outDir[], const char src[],
68 const char suffix[]) {
reed@android.comaf459792009-04-24 19:52:53 +000069 dst->set(outDir);
70 const char* start = strrchr(src, '/');
71 if (start) {
72 start += 1; // skip the actual last '/'
73 } else {
74 start = src;
75 }
76 dst->append(start);
scroggo@google.com39edf4c2013-04-25 17:33:51 +000077 if (!dst->endsWith(suffix)) {
scroggo@google.comb41ff952013-04-11 15:53:35 +000078 const char* cstyleDst = dst->c_str();
79 const char* dot = strrchr(cstyleDst, '.');
80 if (dot != NULL) {
81 int32_t index = SkToS32(dot - cstyleDst);
82 dst->remove(index, dst->size() - index);
83 }
scroggo@google.com39edf4c2013-04-25 17:33:51 +000084 dst->append(suffix);
scroggo@google.comb41ff952013-04-11 15:53:35 +000085 }
86}
87
scroggo@google.com39edf4c2013-04-25 17:33:51 +000088// Store the names of the filenames to report later which ones failed, succeeded, and were
89// invalid.
90static SkTArray<SkString, false> gInvalidStreams;
91static SkTArray<SkString, false> gMissingCodecs;
92static SkTArray<SkString, false> gDecodeFailures;
93static SkTArray<SkString, false> gEncodeFailures;
94static SkTArray<SkString, false> gSuccessfulDecodes;
95
96static 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, 100);
113}
114
115static 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 encoder 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::GetStreamFormat(&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);
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::GetStreamFormat(&stream) == format);
161 }
162 SkImageEncoder::Type type = format_to_type(format);
163 // format should never be kUnknown_Format, so type should never be kUnknown_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_str());
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 reencoding 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
scroggo@google.comb41ff952013-04-11 15:53:35 +0000221// If strings is not empty, print title, followed by each string on its own line starting
222// with a tab.
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000223// @return bool True if strings had at least one entry.
224static bool print_strings(const char* title, const SkTArray<SkString, false>& strings) {
scroggo@google.comb41ff952013-04-11 15:53:35 +0000225 if (strings.count() > 0) {
226 SkDebugf("%s:\n", title);
227 for (int i = 0; i < strings.count(); i++) {
228 SkDebugf("\t%s\n", strings[i].c_str());
229 }
230 SkDebugf("\n");
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000231 return true;
scroggo@google.comb41ff952013-04-11 15:53:35 +0000232 }
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000233 return false;
reed@android.comaf459792009-04-24 19:52:53 +0000234}
235
caryclark@google.com5987f582012-10-02 18:33:14 +0000236int tool_main(int argc, char** argv);
237int tool_main(int argc, char** argv) {
scroggo@google.comb41ff952013-04-11 15:53:35 +0000238 SkCommandLineFlags::SetUsage("Decode files, and optionally write the results to files.");
239 SkCommandLineFlags::Parse(argc, argv);
240
241 if (FLAGS_readPath.count() < 1) {
242 SkDebugf("Folder(s) or image(s) to decode are required.\n");
243 return -1;
244 }
245
246
reed@android.comaf459792009-04-24 19:52:53 +0000247 SkAutoGraphics ag;
scroggo@google.comb41ff952013-04-11 15:53:35 +0000248
reed@android.comaf459792009-04-24 19:52:53 +0000249 SkString outDir;
scroggo@google.comb41ff952013-04-11 15:53:35 +0000250 SkString* outDirPtr;
reed@android.comaf459792009-04-24 19:52:53 +0000251
scroggo@google.comb41ff952013-04-11 15:53:35 +0000252 if (FLAGS_writePath.count() == 1) {
253 outDir.set(FLAGS_writePath[0]);
254 if (outDir.c_str()[outDir.size() - 1] != '/') {
255 outDir.append("/");
reed@android.comaf459792009-04-24 19:52:53 +0000256 }
scroggo@google.comb41ff952013-04-11 15:53:35 +0000257 outDirPtr = &outDir;
258 } else {
259 outDirPtr = NULL;
260 }
261
262 for (int i = 0; i < FLAGS_readPath.count(); i++) {
263 if (strlen(FLAGS_readPath[i]) < 1) {
264 break;
265 }
266 SkOSFile::Iter iter(FLAGS_readPath[i]);
267 SkString filename;
268 if (iter.next(&filename)) {
269 SkString directory(FLAGS_readPath[i]);
270 if (directory[directory.size() - 1] != '/') {
271 directory.append("/");
reed@android.comaf459792009-04-24 19:52:53 +0000272 }
scroggo@google.comb41ff952013-04-11 15:53:35 +0000273 do {
274 SkString fullname(directory);
275 fullname.append(filename);
276 decodeFileAndWrite(fullname.c_str(), outDirPtr);
277 } while (iter.next(&filename));
278 } else {
279 decodeFileAndWrite(FLAGS_readPath[i], outDirPtr);
reed@android.comaf459792009-04-24 19:52:53 +0000280 }
281 }
282
scroggo@google.comb41ff952013-04-11 15:53:35 +0000283 // Add some space, since codecs may print warnings without newline.
284 SkDebugf("\n\n");
rmistry@google.comd6176b02012-08-23 18:14:13 +0000285
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000286 bool failed = print_strings("Invalid files", gInvalidStreams);
287 failed |= print_strings("Missing codec", gMissingCodecs);
288 failed |= print_strings("Failed to decode", gDecodeFailures);
289 failed |= print_strings("Failed to encode", gEncodeFailures);
290 print_strings("Decoded", gSuccessfulDecodes);
reed@android.comaf459792009-04-24 19:52:53 +0000291
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000292 return failed ? -1 : 0;
reed@android.comaf459792009-04-24 19:52:53 +0000293}
294
caryclark@google.com5987f582012-10-02 18:33:14 +0000295#if !defined SK_BUILD_FOR_IOS
296int main(int argc, char * const argv[]) {
297 return tool_main(argc, (char**) argv);
298}
299#endif