blob: 2fdc819aa98bb4a7681bde5117dfcaa938490d9b [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
35int tool_main(int argc, char** argv);
36int tool_main(int argc, char** argv) {
37 SkCommandLineFlags::SetUsage("Print out a row or column of an image.");
38 SkCommandLineFlags::Parse(argc, argv);
39
40 if (FLAGS_rgb > 3 || FLAGS_rgb < 0) {
41 if (!FLAGS_quiet) {
42 SkDebugf("Channel (--rgb) must be between 0 and 3 (inclusive) - value is %d.\n",
43 FLAGS_rgb);
44 }
halcanary9d524f22016-03-29 09:03:52 -070045 return kError;
robertphillips5ce341f2015-09-18 09:04:43 -070046 }
47
48 if (FLAGS_row >= 0 && FLAGS_column >= 0) {
49 if (!FLAGS_quiet) {
50 SkDebugf("Only one of '-c' or '-r' can be specified at at time.\n");
51 }
halcanary9d524f22016-03-29 09:03:52 -070052 return kError;
robertphillips5ce341f2015-09-18 09:04:43 -070053 }
54
55 if (FLAGS_row < 0 && FLAGS_column < 0) {
56 if (!FLAGS_quiet) {
57 SkDebugf("At least one of '-c' or '-r' need to be specified.\n");
58 }
halcanary9d524f22016-03-29 09:03:52 -070059 return kError;
robertphillips5ce341f2015-09-18 09:04:43 -070060 }
61
reed9ce9d672016-03-17 10:51:11 -070062 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_image[0]));
msarettf7a840a2016-03-09 10:09:02 -080063 if (nullptr == data) {
robertphillips5ce341f2015-09-18 09:04:43 -070064 if (!FLAGS_quiet) {
65 SkDebugf("Couldn't open file: %s\n", FLAGS_image[0]);
66 }
67 return kError;
68 }
69
reed9ce9d672016-03-17 10:51:11 -070070 sk_sp<SkImage> image(SkImage::MakeFromEncoded(data));
msarettf7a840a2016-03-09 10:09:02 -080071 if (!image) {
robertphillips5ce341f2015-09-18 09:04:43 -070072 if (!FLAGS_quiet) {
msarettf7a840a2016-03-09 10:09:02 -080073 SkDebugf("Couldn't create image for: %s.\n", FLAGS_image[0]);
robertphillips5ce341f2015-09-18 09:04:43 -070074 }
75 return kError;
76 }
77
78 SkBitmap bitmap;
msarettf7a840a2016-03-09 10:09:02 -080079 if (!image->asLegacyBitmap(&bitmap, SkImage::kRW_LegacyBitmapMode)) {
80 if (!FLAGS_quiet) {
81 SkDebugf("Couldn't create bitmap for: %s.\n", FLAGS_image[0]);
82 }
83 return kError;
84 }
robertphillips5ce341f2015-09-18 09:04:43 -070085
86 int top, bottom, left, right;
87
88 if (-1 != FLAGS_row) {
89 SkASSERT(-1 == FLAGS_column);
90
91 top = bottom = SkTPin(FLAGS_row, 0, bitmap.height()-1);
92 FLAGS_min = left = SkTPin(FLAGS_min, 0, bitmap.width()-1);
93 FLAGS_max = right = SkTPin(FLAGS_max, left, bitmap.width()-1);
94 } else {
95 SkASSERT(-1 != FLAGS_column);
96 left = right = SkTPin(FLAGS_column, 0, bitmap.width()-1);
97 FLAGS_min = top = SkTPin(FLAGS_min, 0, bitmap.height()-1);
98 FLAGS_max = bottom = SkTPin(FLAGS_max, top, bitmap.height()-1);
99 }
100
101 if (FLAGS_header) {
102 SkDebugf("header");
103 if (FLAGS_reverse) {
104 for (int i = FLAGS_max; i >= FLAGS_min; --i) {
105 SkDebugf(", %d", i);
106 }
107 } else {
108 for (int i = FLAGS_min; i <= FLAGS_max; ++i) {
109 SkDebugf(", %d", i);
110 }
111 }
112 SkDebugf("\n");
113 }
114
115 SkDebugf("%s", FLAGS_label[0]);
116 if (FLAGS_reverse) {
117 for (int y = bottom; y >= top; --y) {
118 for (int x = right; x >= left; --x) {
119 SkColor c = bitmap.getColor(x, y);
120
121 SkDebugf(", %d", ((c) >> (FLAGS_rgb*8)) & 0xFF);
122 }
123 }
124 } else {
125 for (int y = top; y <= bottom; ++y) {
126 for (int x = left; x <= right; ++x) {
127 SkColor c = bitmap.getColor(x, y);
128
129 SkDebugf(", %d", ((c) >> (FLAGS_rgb*8)) & 0xFF);
130 }
halcanary9d524f22016-03-29 09:03:52 -0700131 }
robertphillips5ce341f2015-09-18 09:04:43 -0700132 }
133
134 SkDebugf("\n");
135
136 return kSuccess;
137}
138
139#if !defined SK_BUILD_FOR_IOS
140int main(int argc, char * const argv[]) {
141 return tool_main(argc, (char**) argv);
142}
143#endif