blob: 575078c62ef81971f11e8a966847aec6fbfe49d0 [file] [log] [blame]
reed@android.comaf459792009-04-24 19:52:53 +00001#include "SkBitmap.h"
2#include "SkGraphics.h"
3#include "SkImageDecoder.h"
4#include "SkImageEncoder.h"
5#include "SkStream.h"
6#include "SkTemplates.h"
7
8static bool decodeFile(SkBitmap* bitmap, const char srcPath[]) {
9 SkFILEStream stream(srcPath);
10 if (!stream.isValid()) {
11 SkDebugf("ERROR: bad filename <%s>\n", srcPath);
12 return false;
13 }
14
15 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
16 if (NULL == codec) {
17 SkDebugf("ERROR: no codec found for <%s>\n", srcPath);
18 return false;
19 }
20
21 SkAutoTDelete<SkImageDecoder> ad(codec);
22
23 stream.rewind();
24 if (!codec->decode(&stream, bitmap, SkBitmap::kARGB_8888_Config,
25 SkImageDecoder::kDecodePixels_Mode)) {
26 SkDebugf("ERROR: codec failed for <%s>\n", srcPath);
27 return false;
28 }
29 return true;
30}
31
32///////////////////////////////////////////////////////////////////////////////
33
34class SkAutoGraphics {
35public:
36 SkAutoGraphics() {
37 SkGraphics::Init();
38 }
39 ~SkAutoGraphics() {
40 SkGraphics::Term();
41 }
42};
43
44static void show_help() {
45 SkDebugf("usage: skiamge [-o out-dir] inputfiles...\n");
46}
47
48static void make_outname(SkString* dst, const char outDir[], const char src[]) {
49 dst->set(outDir);
50 const char* start = strrchr(src, '/');
51 if (start) {
52 start += 1; // skip the actual last '/'
53 } else {
54 start = src;
55 }
56 dst->append(start);
57 dst->append(".png");
58}
59
60int main (int argc, char * const argv[]) {
61 SkAutoGraphics ag;
62 int i, outDirIndex = 0;
63 SkString outDir;
64
65 for (i = 1; i < argc; i++) {
66 if (!strcmp(argv[i], "-help")) {
67 show_help();
68 return 0;
69 }
70 if (!strcmp(argv[i], "-o")) {
71 if (i == argc-1) {
72 SkDebugf("ERROR: -o needs a following filename\n");
73 return -1;
74 }
75 outDirIndex = i;
76 outDir.set(argv[i+1]);
77 if (outDir.c_str()[outDir.size() - 1] != '/') {
78 outDir.append("/");
79 }
80 i += 1; // skip the out dir name
81 }
82 }
83
84 for (i = 1; i < argc; i++) {
85 if (i == outDirIndex) {
86 i += 1; // skip this and the next entry
87 continue;
88 }
89
90 SkBitmap bitmap;
91 if (decodeFile(&bitmap, argv[i])) {
92 if (outDirIndex) {
93 SkString outPath;
94 make_outname(&outPath, outDir.c_str(), argv[i]);
95 SkDebugf(" writing %s\n", outPath.c_str());
96 SkImageEncoder::EncodeFile(outPath.c_str(), bitmap,
97 SkImageEncoder::kPNG_Type, 100);
98 } else {
99 SkDebugf(" decoded %s [%d %d]\n", argv[i], bitmap.width(),
100 bitmap.height());
101 }
102 }
103 }
104
105 return 0;
106}
107