1/*
2 * Copyright 2014 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 * Simple tool to generate SKP files for testing.
8 */
9
10#include "SkCanvas.h"
11#include "SkColor.h"
12#include "SkCommandLineFlags.h"
13#include "SkPaint.h"
14#include "SkPicture.h"
15#include "SkPictureRecorder.h"
16#include "SkScalar.h"
17#include "SkStream.h"
18
19// Flags used by this file, alphabetically:
20DEFINE_int32(blue, 128, "Value of blue color channel in image, 0-255.");
21DEFINE_int32(border, 4, "Width of the black border around the image.");
22DEFINE_int32(green, 128, "Value of green color channel in image, 0-255.");
23DEFINE_int32(height, 200, "Height of canvas to create.");
24DEFINE_int32(red, 128, "Value of red color channel in image, 0-255.");
25DEFINE_int32(width, 300, "Width of canvas to create.");
26DEFINE_string(writePath, "", "Filepath to write the SKP into.");
27
28static void skpmaker(int width, int height, int border, SkColor color,
29                     const char *writePath) {
30    SkPictureRecorder recorder;
31    SkCanvas* canvas = recorder.beginRecording(width, height, NULL, 0);
32    SkPaint paint;
33    paint.setStyle(SkPaint::kFill_Style);
34    paint.setColor(SK_ColorBLACK);
35    canvas->drawRectCoords(0, 0, SkIntToScalar(width), SkIntToScalar(height), paint);
36    paint.setColor(color);
37    canvas->drawRectCoords(SkIntToScalar(border), SkIntToScalar(border),
38                           SkIntToScalar(width - border*2), SkIntToScalar(height - border*2),
39                           paint);
40    SkAutoTUnref<SkPicture> pict(recorder.endRecording());
41    SkFILEWStream stream(writePath);
42    pict->serialize(&stream);
43}
44
45int tool_main(int argc, char** argv);
46int tool_main(int argc, char** argv) {
47    SkCommandLineFlags::SetUsage("Creates a simple .skp file for testing.");
48    SkCommandLineFlags::Parse(argc, argv);
49
50    // Validate flags.
51    if ((FLAGS_blue < 0) || (FLAGS_blue > 255)) {
52        SkDebugf("--blue must be within range [0,255]\n");
53        exit(-1);
54    }
55    if ((FLAGS_green < 0) || (FLAGS_green > 255)) {
56        SkDebugf("--green must be within range [0,255]\n");
57        exit(-1);
58    }
59    if (FLAGS_height <= 0) {
60        SkDebugf("--height must be >0\n");
61        exit(-1);
62    }
63    if ((FLAGS_red < 0) || (FLAGS_red > 255)) {
64        SkDebugf("--red must be within range [0,255]\n");
65        exit(-1);
66    }
67    if (FLAGS_width <= 0) {
68        SkDebugf("--width must be >0\n");
69        exit(-1);
70    }
71    if (FLAGS_writePath.isEmpty()) {
72        SkDebugf("--writePath must be nonempty\n");
73        exit(-1);
74    }
75
76    SkColor color = SkColorSetRGB(FLAGS_red, FLAGS_green, FLAGS_blue);
77    skpmaker(FLAGS_width, FLAGS_height, FLAGS_border, color, FLAGS_writePath[0]);
78    return 0;
79}
80
81#if !defined SK_BUILD_FOR_IOS
82int main(int argc, char * const argv[]) {
83    return tool_main(argc, (char**) argv);
84}
85#endif
86