robertphillips | 5ce341f | 2015-09-18 09:04:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkCommandLineFlags.h" |
| 9 | #include "SkImageDecoder.h" |
| 10 | #include "SkStream.h" |
| 11 | |
| 12 | DEFINE_bool(header, false, "Print an extra row of the min-max values"); |
| 13 | DEFINE_string2(label, l, "label", "Label printed as the first value"); |
| 14 | |
| 15 | DEFINE_string2(image, i, "", "Input image"); |
| 16 | DEFINE_int32_2(row, r, -1, "Row to extract"); |
| 17 | DEFINE_int32_2(column, c, -1, "Column to extract"); |
| 18 | |
| 19 | DEFINE_int32_2(min, n, 0, "Minimum row/column to extract - inclusive"); |
| 20 | DEFINE_int32_2(max, m, 100, "Maximum row/column to extract - inclusive"); |
| 21 | |
| 22 | DEFINE_int32(rgb, 0, "Color channel to print (0->b, 1->g, 2->r, 3->a)"); |
| 23 | |
| 24 | DEFINE_bool2(quiet, q, false, "Quiet"); |
| 25 | DEFINE_bool2(reverse, v, false, "Iterate from max to min"); |
| 26 | |
| 27 | |
| 28 | // This tool just loads a single image and prints out a comma-separated row or column |
| 29 | // Return codes: |
| 30 | static const int kSuccess = 0; |
| 31 | static const int kError = 1; |
| 32 | |
| 33 | int tool_main(int argc, char** argv); |
| 34 | int tool_main(int argc, char** argv) { |
| 35 | SkCommandLineFlags::SetUsage("Print out a row or column of an image."); |
| 36 | SkCommandLineFlags::Parse(argc, argv); |
| 37 | |
| 38 | if (FLAGS_rgb > 3 || FLAGS_rgb < 0) { |
| 39 | if (!FLAGS_quiet) { |
| 40 | SkDebugf("Channel (--rgb) must be between 0 and 3 (inclusive) - value is %d.\n", |
| 41 | FLAGS_rgb); |
| 42 | } |
| 43 | return kError; |
| 44 | } |
| 45 | |
| 46 | if (FLAGS_row >= 0 && FLAGS_column >= 0) { |
| 47 | if (!FLAGS_quiet) { |
| 48 | SkDebugf("Only one of '-c' or '-r' can be specified at at time.\n"); |
| 49 | } |
| 50 | return kError; |
| 51 | } |
| 52 | |
| 53 | if (FLAGS_row < 0 && FLAGS_column < 0) { |
| 54 | if (!FLAGS_quiet) { |
| 55 | SkDebugf("At least one of '-c' or '-r' need to be specified.\n"); |
| 56 | } |
| 57 | return kError; |
| 58 | } |
| 59 | |
| 60 | SkFILEStream inputStream(FLAGS_image[0]); |
| 61 | if (!inputStream.isValid()) { |
| 62 | if (!FLAGS_quiet) { |
| 63 | SkDebugf("Couldn't open file: %s\n", FLAGS_image[0]); |
| 64 | } |
| 65 | return kError; |
| 66 | } |
| 67 | |
| 68 | SkAutoTDelete<SkImageDecoder> codec(SkImageDecoder::Factory(&inputStream)); |
| 69 | if (!codec) { |
| 70 | if (!FLAGS_quiet) { |
| 71 | SkDebugf("Couldn't create codec for: %s.\n", FLAGS_image[0]); |
| 72 | } |
| 73 | return kError; |
| 74 | } |
| 75 | |
| 76 | SkBitmap bitmap; |
| 77 | |
| 78 | inputStream.rewind(); |
| 79 | codec->decode(&inputStream, &bitmap, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode); |
| 80 | |
| 81 | int top, bottom, left, right; |
| 82 | |
| 83 | if (-1 != FLAGS_row) { |
| 84 | SkASSERT(-1 == FLAGS_column); |
| 85 | |
| 86 | top = bottom = SkTPin(FLAGS_row, 0, bitmap.height()-1); |
| 87 | FLAGS_min = left = SkTPin(FLAGS_min, 0, bitmap.width()-1); |
| 88 | FLAGS_max = right = SkTPin(FLAGS_max, left, bitmap.width()-1); |
| 89 | } else { |
| 90 | SkASSERT(-1 != FLAGS_column); |
| 91 | left = right = SkTPin(FLAGS_column, 0, bitmap.width()-1); |
| 92 | FLAGS_min = top = SkTPin(FLAGS_min, 0, bitmap.height()-1); |
| 93 | FLAGS_max = bottom = SkTPin(FLAGS_max, top, bitmap.height()-1); |
| 94 | } |
| 95 | |
| 96 | if (FLAGS_header) { |
| 97 | SkDebugf("header"); |
| 98 | if (FLAGS_reverse) { |
| 99 | for (int i = FLAGS_max; i >= FLAGS_min; --i) { |
| 100 | SkDebugf(", %d", i); |
| 101 | } |
| 102 | } else { |
| 103 | for (int i = FLAGS_min; i <= FLAGS_max; ++i) { |
| 104 | SkDebugf(", %d", i); |
| 105 | } |
| 106 | } |
| 107 | SkDebugf("\n"); |
| 108 | } |
| 109 | |
| 110 | SkDebugf("%s", FLAGS_label[0]); |
| 111 | if (FLAGS_reverse) { |
| 112 | for (int y = bottom; y >= top; --y) { |
| 113 | for (int x = right; x >= left; --x) { |
| 114 | SkColor c = bitmap.getColor(x, y); |
| 115 | |
| 116 | SkDebugf(", %d", ((c) >> (FLAGS_rgb*8)) & 0xFF); |
| 117 | } |
| 118 | } |
| 119 | } else { |
| 120 | for (int y = top; y <= bottom; ++y) { |
| 121 | for (int x = left; x <= right; ++x) { |
| 122 | SkColor c = bitmap.getColor(x, y); |
| 123 | |
| 124 | SkDebugf(", %d", ((c) >> (FLAGS_rgb*8)) & 0xFF); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | SkDebugf("\n"); |
| 130 | |
| 131 | return kSuccess; |
| 132 | } |
| 133 | |
| 134 | #if !defined SK_BUILD_FOR_IOS |
| 135 | int main(int argc, char * const argv[]) { |
| 136 | return tool_main(argc, (char**) argv); |
| 137 | } |
| 138 | #endif |