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