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