blob: 8fa969a6f072f2a87dc55ca0088ae5d719d58774 [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
bungeman60e0fee2015-08-26 05:15:46 -070019#include <stdlib.h>
20
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +000021// Flags used by this file, alphabetically:
22DEFINE_int32(blue, 128, "Value of blue color channel in image, 0-255.");
commit-bot@chromium.org238771c2014-01-15 16:33:31 +000023DEFINE_int32(border, 4, "Width of the black border around the image.");
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +000024DEFINE_int32(green, 128, "Value of green color channel in image, 0-255.");
25DEFINE_int32(height, 200, "Height of canvas to create.");
26DEFINE_int32(red, 128, "Value of red color channel in image, 0-255.");
27DEFINE_int32(width, 300, "Width of canvas to create.");
28DEFINE_string(writePath, "", "Filepath to write the SKP into.");
29
robertphillipsa8d7f0b2014-08-29 08:03:56 -070030// Create a 'width' by 'height' skp with a 'border'-wide black border around
31// a 'color' rectangle.
32static void make_skp(SkScalar width, SkScalar height, SkScalar border, SkColor color,
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +000033 const char *writePath) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +000034 SkPictureRecorder recorder;
halcanary96fcdcc2015-08-27 07:41:13 -070035 SkCanvas* canvas = recorder.beginRecording(width, height, nullptr, 0);
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +000036 SkPaint paint;
37 paint.setStyle(SkPaint::kFill_Style);
commit-bot@chromium.org238771c2014-01-15 16:33:31 +000038 paint.setColor(SK_ColorBLACK);
robertphillipsa8d7f0b2014-08-29 08:03:56 -070039 SkRect r = SkRect::MakeWH(width, height);
40 canvas->drawRect(r, paint);
commit-bot@chromium.org238771c2014-01-15 16:33:31 +000041 paint.setColor(color);
robertphillipsa8d7f0b2014-08-29 08:03:56 -070042 r.inset(border, border);
43 canvas->drawRect(r, paint);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000044 SkAutoTUnref<SkPicture> pict(recorder.endRecording());
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +000045 SkFILEWStream stream(writePath);
robertphillips@google.com84b18c72014-04-13 19:09:42 +000046 pict->serialize(&stream);
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +000047}
48
49int tool_main(int argc, char** argv);
50int tool_main(int argc, char** argv) {
51 SkCommandLineFlags::SetUsage("Creates a simple .skp file for testing.");
52 SkCommandLineFlags::Parse(argc, argv);
53
54 // Validate flags.
55 if ((FLAGS_blue < 0) || (FLAGS_blue > 255)) {
56 SkDebugf("--blue must be within range [0,255]\n");
57 exit(-1);
58 }
59 if ((FLAGS_green < 0) || (FLAGS_green > 255)) {
60 SkDebugf("--green must be within range [0,255]\n");
61 exit(-1);
62 }
63 if (FLAGS_height <= 0) {
64 SkDebugf("--height must be >0\n");
65 exit(-1);
66 }
67 if ((FLAGS_red < 0) || (FLAGS_red > 255)) {
68 SkDebugf("--red must be within range [0,255]\n");
69 exit(-1);
70 }
71 if (FLAGS_width <= 0) {
72 SkDebugf("--width must be >0\n");
73 exit(-1);
74 }
75 if (FLAGS_writePath.isEmpty()) {
76 SkDebugf("--writePath must be nonempty\n");
77 exit(-1);
78 }
79
80 SkColor color = SkColorSetRGB(FLAGS_red, FLAGS_green, FLAGS_blue);
robertphillipsa8d7f0b2014-08-29 08:03:56 -070081 make_skp(SkIntToScalar(FLAGS_width),
82 SkIntToScalar(FLAGS_height),
83 SkIntToScalar(FLAGS_border),
84 color, FLAGS_writePath[0]);
commit-bot@chromium.orgc7355982014-01-02 19:42:15 +000085 return 0;
86}
87
88#if !defined SK_BUILD_FOR_IOS
89int main(int argc, char * const argv[]) {
90 return tool_main(argc, (char**) argv);
91}
92#endif