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 "SkBitmap.h"
9#include "SkCommandLineFlags.h"
10#include "SkData.h"
11#include "SkImage.h"
12#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 main(int argc, char** argv) {
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        }
44        return kError;
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        }
51        return kError;
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        }
58        return kError;
59    }
60
61    sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_image[0]));
62    if (nullptr == data) {
63        if (!FLAGS_quiet) {
64            SkDebugf("Couldn't open file: %s\n", FLAGS_image[0]);
65        }
66        return kError;
67    }
68
69    sk_sp<SkImage> image(SkImage::MakeFromEncoded(data));
70    if (!image) {
71        if (!FLAGS_quiet) {
72            SkDebugf("Couldn't create image for: %s.\n", FLAGS_image[0]);
73        }
74        return kError;
75    }
76
77    SkBitmap bitmap;
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    }
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            }
130        }
131    }
132
133    SkDebugf("\n");
134
135    return kSuccess;
136}
137