blob: af9b4adea27b9dc1534ab5e654d535e836a2fcc3 [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.comb41ff952013-04-11 15:53:35 +00009#include "SkCommandLineFlags.h"
reed@android.comaf459792009-04-24 19:52:53 +000010#include "SkGraphics.h"
11#include "SkImageDecoder.h"
12#include "SkImageEncoder.h"
scroggo@google.comb41ff952013-04-11 15:53:35 +000013#include "SkOSFile.h"
reed@android.comaf459792009-04-24 19:52:53 +000014#include "SkStream.h"
scroggo@google.comb41ff952013-04-11 15:53:35 +000015#include "SkTArray.h"
reed@android.comaf459792009-04-24 19:52:53 +000016#include "SkTemplates.h"
17
scroggo@google.comb41ff952013-04-11 15:53:35 +000018
19DEFINE_string2(readPath, r, "", "Folder(s) and files to decode images. Required.");
20DEFINE_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.
24static SkTArray<SkString, false> invalids;
25static SkTArray<SkString, false> nocodecs;
26static SkTArray<SkString, false> failures;
27static SkTArray<SkString, false> successes;
28
reed@android.comaf459792009-04-24 19:52:53 +000029static bool decodeFile(SkBitmap* bitmap, const char srcPath[]) {
30 SkFILEStream stream(srcPath);
31 if (!stream.isValid()) {
scroggo@google.comb41ff952013-04-11 15:53:35 +000032 invalids.push_back().set(srcPath);
reed@android.comaf459792009-04-24 19:52:53 +000033 return false;
34 }
35
36 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
37 if (NULL == codec) {
scroggo@google.comb41ff952013-04-11 15:53:35 +000038 nocodecs.push_back().set(srcPath);
reed@android.comaf459792009-04-24 19:52:53 +000039 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.comb41ff952013-04-11 15:53:35 +000047 failures.push_back().set(srcPath);
reed@android.comaf459792009-04-24 19:52:53 +000048 return false;
49 }
scroggo@google.comb41ff952013-04-11 15:53:35 +000050
51 successes.push_back().printf("%s [%d %d]", srcPath, bitmap->width(), bitmap->height());
reed@android.comaf459792009-04-24 19:52:53 +000052 return true;
53}
54
55///////////////////////////////////////////////////////////////////////////////
56
reed@android.comaf459792009-04-24 19:52:53 +000057static 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.comb41ff952013-04-11 15:53:35 +000066 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.
79static 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
89static 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.comaf459792009-04-24 19:52:53 +000099}
100
caryclark@google.com5987f582012-10-02 18:33:14 +0000101int tool_main(int argc, char** argv);
102int tool_main(int argc, char** argv) {
scroggo@google.comb41ff952013-04-11 15:53:35 +0000103 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.comaf459792009-04-24 19:52:53 +0000112 SkAutoGraphics ag;
scroggo@google.comb41ff952013-04-11 15:53:35 +0000113
reed@android.comaf459792009-04-24 19:52:53 +0000114 SkString outDir;
scroggo@google.comb41ff952013-04-11 15:53:35 +0000115 SkString* outDirPtr;
reed@android.comaf459792009-04-24 19:52:53 +0000116
scroggo@google.comb41ff952013-04-11 15:53:35 +0000117 if (FLAGS_writePath.count() == 1) {
118 outDir.set(FLAGS_writePath[0]);
119 if (outDir.c_str()[outDir.size() - 1] != '/') {
120 outDir.append("/");
reed@android.comaf459792009-04-24 19:52:53 +0000121 }
scroggo@google.comb41ff952013-04-11 15:53:35 +0000122 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.comaf459792009-04-24 19:52:53 +0000137 }
scroggo@google.comb41ff952013-04-11 15:53:35 +0000138 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.comaf459792009-04-24 19:52:53 +0000145 }
146 }
147
scroggo@google.comb41ff952013-04-11 15:53:35 +0000148 // Add some space, since codecs may print warnings without newline.
149 SkDebugf("\n\n");
rmistry@google.comd6176b02012-08-23 18:14:13 +0000150
scroggo@google.comb41ff952013-04-11 15:53:35 +0000151 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.comaf459792009-04-24 19:52:53 +0000155
156 return 0;
157}
158
scroggo@google.comb41ff952013-04-11 15:53:35 +0000159void forceLinking();
160
161void forceLinking() {
scroggo@google.com9c5f9692013-04-11 18:32:01 +0000162 // 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.comb41ff952013-04-11 15:53:35 +0000165 SkDEBUGCODE(SkImageDecoder *creator = ) CreateJPEGImageDecoder();
166 SkASSERT(creator);
scroggo@google.comaaec8512013-04-15 22:05:03 +0000167 SkDEBUGCODE(creator = ) CreateWEBPImageDecoder();
168 SkASSERT(creator);
scroggo@google.com9c5f9692013-04-11 18:32:01 +0000169#ifdef SK_BUILD_FOR_UNIX
170 SkDEBUGCODE(creator = ) CreateGIFImageDecoder();
171 SkASSERT(creator);
172#endif
scroggo@google.comb41ff952013-04-11 15:53:35 +0000173}
174
caryclark@google.com5987f582012-10-02 18:33:14 +0000175#if !defined SK_BUILD_FOR_IOS
176int main(int argc, char * const argv[]) {
177 return tool_main(argc, (char**) argv);
178}
179#endif