epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
| 9 | #include "SkGraphics.h" |
| 10 | #include "SkImageDecoder.h" |
| 11 | #include "SkImageEncoder.h" |
| 12 | #include "SkStream.h" |
| 13 | #include "SkTemplates.h" |
| 14 | |
| 15 | static 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.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 41 | static void show_help() { |
| 42 | SkDebugf("usage: skiamge [-o out-dir] inputfiles...\n"); |
| 43 | } |
| 44 | |
| 45 | static 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 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 57 | int tool_main(int argc, char** argv); |
| 58 | int tool_main(int argc, char** argv) { |
reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 59 | SkAutoGraphics ag; |
| 60 | int i, outDirIndex = 0; |
| 61 | SkString outDir; |
| 62 | |
| 63 | for (i = 1; i < argc; i++) { |
| 64 | if (!strcmp(argv[i], "-help")) { |
| 65 | show_help(); |
| 66 | return 0; |
| 67 | } |
| 68 | if (!strcmp(argv[i], "-o")) { |
| 69 | if (i == argc-1) { |
| 70 | SkDebugf("ERROR: -o needs a following filename\n"); |
| 71 | return -1; |
| 72 | } |
| 73 | outDirIndex = i; |
| 74 | outDir.set(argv[i+1]); |
| 75 | if (outDir.c_str()[outDir.size() - 1] != '/') { |
| 76 | outDir.append("/"); |
| 77 | } |
| 78 | i += 1; // skip the out dir name |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | for (i = 1; i < argc; i++) { |
| 83 | if (i == outDirIndex) { |
| 84 | i += 1; // skip this and the next entry |
| 85 | continue; |
| 86 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 87 | |
reed@android.com | af45979 | 2009-04-24 19:52:53 +0000 | [diff] [blame] | 88 | SkBitmap bitmap; |
| 89 | if (decodeFile(&bitmap, argv[i])) { |
| 90 | if (outDirIndex) { |
| 91 | SkString outPath; |
| 92 | make_outname(&outPath, outDir.c_str(), argv[i]); |
| 93 | SkDebugf(" writing %s\n", outPath.c_str()); |
| 94 | SkImageEncoder::EncodeFile(outPath.c_str(), bitmap, |
| 95 | SkImageEncoder::kPNG_Type, 100); |
| 96 | } else { |
| 97 | SkDebugf(" decoded %s [%d %d]\n", argv[i], bitmap.width(), |
| 98 | bitmap.height()); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
caryclark@google.com | 5987f58 | 2012-10-02 18:33:14 +0000 | [diff] [blame] | 106 | #if !defined SK_BUILD_FOR_IOS |
| 107 | int main(int argc, char * const argv[]) { |
| 108 | return tool_main(argc, (char**) argv); |
| 109 | } |
| 110 | #endif |