blob: 390e5ca299abf576f388fac3e34c47e8be641408 [file] [log] [blame]
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +00001/*
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"
robertphillips@google.com770963f2014-04-18 18:04:41 +000015#include "SkPictureRecorder.h"
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +000016#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.");
commit-bot@chromium.org238771c2014-01-15 16:33:31 +000021DEFINE_int32(border, 4, "Width of the black border around the image.");
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +000022DEFINE_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
commit-bot@chromium.org238771c2014-01-15 16:33:31 +000028static void skpmaker(int width, int height, int border, SkColor color,
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +000029 const char *writePath) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +000030 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +000031 SkCanvas* canvas = recorder.beginRecording(width, height, NULL, 0);
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +000032 SkPaint paint;
33 paint.setStyle(SkPaint::kFill_Style);
commit-bot@chromium.org238771c2014-01-15 16:33:31 +000034 paint.setColor(SK_ColorBLACK);
commit-bot@chromium.org7e1a31d2014-01-15 16:04:45 +000035 canvas->drawRectCoords(0, 0, SkIntToScalar(width), SkIntToScalar(height), paint);
commit-bot@chromium.org238771c2014-01-15 16:33:31 +000036 paint.setColor(color);
37 canvas->drawRectCoords(SkIntToScalar(border), SkIntToScalar(border),
38 SkIntToScalar(width - border*2), SkIntToScalar(height - border*2),
39 paint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000040 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +000041 SkFILEWStream stream(writePath);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000042 pict->serialize(&stream);
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +000043}
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);
commit-bot@chromium.org238771c2014-01-15 16:33:31 +000077 skpmaker(FLAGS_width, FLAGS_height, FLAGS_border, color, FLAGS_writePath[0]);
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +000078 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