reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 1 | #include "SkBitmap.h" |
| 2 | #include "SkGraphics.h" |
| 3 | #include "SkImageDecoder.h" |
| 4 | #include "SkImageEncoder.h" |
| 5 | #include "SkStream.h" |
| 6 | #include "SkTemplates.h" |
| 7 | |
| 8 | static 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 | |
reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 34 | static void show_help() { |
| 35 | SkDebugf("usage: skiamge [-o out-dir] inputfiles...\n"); |
| 36 | } |
| 37 | |
| 38 | static void make_outname(SkString* dst, const char outDir[], const char src[]) { |
| 39 | dst->set(outDir); |
| 40 | const char* start = strrchr(src, '/'); |
| 41 | if (start) { |
| 42 | start += 1; // skip the actual last '/' |
| 43 | } else { |
| 44 | start = src; |
| 45 | } |
| 46 | dst->append(start); |
| 47 | dst->append(".png"); |
| 48 | } |
| 49 | |
| 50 | int main (int argc, char * const argv[]) { |
| 51 | SkAutoGraphics ag; |
| 52 | int i, outDirIndex = 0; |
| 53 | SkString outDir; |
| 54 | |
| 55 | for (i = 1; i < argc; i++) { |
| 56 | if (!strcmp(argv[i], "-help")) { |
| 57 | show_help(); |
| 58 | return 0; |
| 59 | } |
| 60 | if (!strcmp(argv[i], "-o")) { |
| 61 | if (i == argc-1) { |
| 62 | SkDebugf("ERROR: -o needs a following filename\n"); |
| 63 | return -1; |
| 64 | } |
| 65 | outDirIndex = i; |
| 66 | outDir.set(argv[i+1]); |
| 67 | if (outDir.c_str()[outDir.size() - 1] != '/') { |
| 68 | outDir.append("/"); |
| 69 | } |
| 70 | i += 1; // skip the out dir name |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | for (i = 1; i < argc; i++) { |
| 75 | if (i == outDirIndex) { |
| 76 | i += 1; // skip this and the next entry |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | SkBitmap bitmap; |
| 81 | if (decodeFile(&bitmap, argv[i])) { |
| 82 | if (outDirIndex) { |
| 83 | SkString outPath; |
| 84 | make_outname(&outPath, outDir.c_str(), argv[i]); |
| 85 | SkDebugf(" writing %s\n", outPath.c_str()); |
| 86 | SkImageEncoder::EncodeFile(outPath.c_str(), bitmap, |
| 87 | SkImageEncoder::kPNG_Type, 100); |
| 88 | } else { |
| 89 | SkDebugf(" decoded %s [%d %d]\n", argv[i], bitmap.width(), |
| 90 | bitmap.height()); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |