blob: 51aeef78301787980f23741a341dcabde1f1f377 [file] [log] [blame]
robertphillips5ce341f2015-09-18 09:04:43 -07001/*
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
msarettf7a840a2016-03-09 10:09:02 -08008#include "SkBitmap.h"
robertphillips5ce341f2015-09-18 09:04:43 -07009#include "SkCommandLineFlags.h"
msarettf7a840a2016-03-09 10:09:02 -080010#include "SkData.h"
11#include "SkImage.h"
robertphillips5ce341f2015-09-18 09:04:43 -070012#include "SkStream.h"
13
14DEFINE_bool(header, false, "Print an extra row of the min-max values");
15DEFINE_string2(label, l, "label", "Label printed as the first value");
16
17DEFINE_string2(image, i, "", "Input image");
18DEFINE_int32_2(row, r, -1, "Row to extract");
19DEFINE_int32_2(column, c, -1, "Column to extract");
20
21DEFINE_int32_2(min, n, 0, "Minimum row/column to extract - inclusive");
22DEFINE_int32_2(max, m, 100, "Maximum row/column to extract - inclusive");
23
24DEFINE_int32(rgb, 0, "Color channel to print (0->b, 1->g, 2->r, 3->a)");
25
26DEFINE_bool2(quiet, q, false, "Quiet");
27DEFINE_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:
32static const int kSuccess = 0;
33static const int kError = 1;
34
Mike Kleinbe28ee22017-02-06 12:46:20 -050035int main(int argc, char** argv) {
robertphillips5ce341f2015-09-18 09:04:43 -070036 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 }
halcanary9d524f22016-03-29 09:03:52 -070044 return kError;
robertphillips5ce341f2015-09-18 09:04:43 -070045 }
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 }
halcanary9d524f22016-03-29 09:03:52 -070051 return kError;
robertphillips5ce341f2015-09-18 09:04:43 -070052 }
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 }
halcanary9d524f22016-03-29 09:03:52 -070058 return kError;
robertphillips5ce341f2015-09-18 09:04:43 -070059 }
60
reed9ce9d672016-03-17 10:51:11 -070061 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_image[0]));
msarettf7a840a2016-03-09 10:09:02 -080062 if (nullptr == data) {
robertphillips5ce341f2015-09-18 09:04:43 -070063 if (!FLAGS_quiet) {
64 SkDebugf("Couldn't open file: %s\n", FLAGS_image[0]);
65 }
66 return kError;
67 }
68
reed9ce9d672016-03-17 10:51:11 -070069 sk_sp<SkImage> image(SkImage::MakeFromEncoded(data));
msarettf7a840a2016-03-09 10:09:02 -080070 if (!image) {
robertphillips5ce341f2015-09-18 09:04:43 -070071 if (!FLAGS_quiet) {
msarettf7a840a2016-03-09 10:09:02 -080072 SkDebugf("Couldn't create image for: %s.\n", FLAGS_image[0]);
robertphillips5ce341f2015-09-18 09:04:43 -070073 }
74 return kError;
75 }
76
77 SkBitmap bitmap;
msarettf7a840a2016-03-09 10:09:02 -080078 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 }
robertphillips5ce341f2015-09-18 09:04:43 -070084
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 }
halcanary9d524f22016-03-29 09:03:52 -0700130 }
robertphillips5ce341f2015-09-18 09:04:43 -0700131 }
132
133 SkDebugf("\n");
134
135 return kSuccess;
136}