1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkBitmap.h"
#include "SkCommandLineFlags.h"
#include "SkGraphics.h"
#include "SkImageDecoder.h"
#include "SkImageEncoder.h"
#include "SkOSFile.h"
#include "SkStream.h"
#include "SkTArray.h"
#include "SkTemplates.h"
DEFINE_string2(readPath, r, "", "Folder(s) and files to decode images. Required.");
DEFINE_string2(writePath, w, "", "Write rendered images into this directory.");
// Store the names of the filenames to report later which ones failed, succeeded, and were
// invalid.
static SkTArray<SkString, false> invalids;
static SkTArray<SkString, false> nocodecs;
static SkTArray<SkString, false> failures;
static SkTArray<SkString, false> successes;
static bool decodeFile(SkBitmap* bitmap, const char srcPath[]) {
SkFILEStream stream(srcPath);
if (!stream.isValid()) {
invalids.push_back().set(srcPath);
return false;
}
SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
if (NULL == codec) {
nocodecs.push_back().set(srcPath);
return false;
}
SkAutoTDelete<SkImageDecoder> ad(codec);
stream.rewind();
if (!codec->decode(&stream, bitmap, SkBitmap::kARGB_8888_Config,
SkImageDecoder::kDecodePixels_Mode)) {
failures.push_back().set(srcPath);
return false;
}
successes.push_back().printf("%s [%d %d]", srcPath, bitmap->width(), bitmap->height());
return true;
}
///////////////////////////////////////////////////////////////////////////////
static void make_outname(SkString* dst, const char outDir[], const char src[]) {
dst->set(outDir);
const char* start = strrchr(src, '/');
if (start) {
start += 1; // skip the actual last '/'
} else {
start = src;
}
dst->append(start);
if (!dst->endsWith(".png")) {
const char* cstyleDst = dst->c_str();
const char* dot = strrchr(cstyleDst, '.');
if (dot != NULL) {
int32_t index = SkToS32(dot - cstyleDst);
dst->remove(index, dst->size() - index);
}
dst->append(".png");
}
}
// If strings is not empty, print title, followed by each string on its own line starting
// with a tab.
static void print_strings(const char* title, const SkTArray<SkString, false>& strings) {
if (strings.count() > 0) {
SkDebugf("%s:\n", title);
for (int i = 0; i < strings.count(); i++) {
SkDebugf("\t%s\n", strings[i].c_str());
}
SkDebugf("\n");
}
}
static void decodeFileAndWrite(const char filePath[], const SkString* writePath) {
SkBitmap bitmap;
if (decodeFile(&bitmap, filePath)) {
if (writePath != NULL) {
SkString outPath;
make_outname(&outPath, writePath->c_str(), filePath);
successes.push_back().appendf("\twrote %s", outPath.c_str());
SkImageEncoder::EncodeFile(outPath.c_str(), bitmap, SkImageEncoder::kPNG_Type, 100);
}
}
}
int tool_main(int argc, char** argv);
int tool_main(int argc, char** argv) {
SkCommandLineFlags::SetUsage("Decode files, and optionally write the results to files.");
SkCommandLineFlags::Parse(argc, argv);
if (FLAGS_readPath.count() < 1) {
SkDebugf("Folder(s) or image(s) to decode are required.\n");
return -1;
}
SkAutoGraphics ag;
SkString outDir;
SkString* outDirPtr;
if (FLAGS_writePath.count() == 1) {
outDir.set(FLAGS_writePath[0]);
if (outDir.c_str()[outDir.size() - 1] != '/') {
outDir.append("/");
}
outDirPtr = &outDir;
} else {
outDirPtr = NULL;
}
for (int i = 0; i < FLAGS_readPath.count(); i++) {
if (strlen(FLAGS_readPath[i]) < 1) {
break;
}
SkOSFile::Iter iter(FLAGS_readPath[i]);
SkString filename;
if (iter.next(&filename)) {
SkString directory(FLAGS_readPath[i]);
if (directory[directory.size() - 1] != '/') {
directory.append("/");
}
do {
SkString fullname(directory);
fullname.append(filename);
decodeFileAndWrite(fullname.c_str(), outDirPtr);
} while (iter.next(&filename));
} else {
decodeFileAndWrite(FLAGS_readPath[i], outDirPtr);
}
}
// Add some space, since codecs may print warnings without newline.
SkDebugf("\n\n");
print_strings("Invalid files", invalids);
print_strings("Missing codec", nocodecs);
print_strings("Failed to decode", failures);
print_strings("Decoded", successes);
return 0;
}
void forceLinking();
void forceLinking() {
// This function leaks, but that is okay because it is not intended
// to be called. It is only here so that the linker will include the
// decoders.
SkDEBUGCODE(SkImageDecoder *creator = ) CreateJPEGImageDecoder();
SkASSERT(creator);
SkDEBUGCODE(creator = ) CreateWEBPImageDecoder();
SkASSERT(creator);
#ifdef SK_BUILD_FOR_UNIX
SkDEBUGCODE(creator = ) CreateGIFImageDecoder();
SkASSERT(creator);
#endif
}
#if !defined SK_BUILD_FOR_IOS
int main(int argc, char * const argv[]) {
return tool_main(argc, (char**) argv);
}
#endif
|