• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdlib.h>
20 
21 // Flags used by this file, alphabetically:
22 DEFINE_int32(blue, 128, "Value of blue color channel in image, 0-255.");
23 DEFINE_int32(border, 4, "Width of the black border around the image.");
24 DEFINE_int32(green, 128, "Value of green color channel in image, 0-255.");
25 DEFINE_int32(height, 200, "Height of canvas to create.");
26 DEFINE_int32(red, 128, "Value of red color channel in image, 0-255.");
27 DEFINE_int32(width, 300, "Width of canvas to create.");
28 DEFINE_string(writePath, "", "Filepath to write the SKP into.");
29 
30 // Create a 'width' by 'height' skp with a 'border'-wide black border around
31 // a 'color' rectangle.
make_skp(SkScalar width,SkScalar height,SkScalar border,SkColor color,const char * writePath)32 static void make_skp(SkScalar width, SkScalar height, SkScalar border, SkColor color,
33                      const char *writePath) {
34     SkPictureRecorder recorder;
35     SkCanvas* canvas = recorder.beginRecording(width, height, nullptr, 0);
36     SkPaint paint;
37     paint.setStyle(SkPaint::kFill_Style);
38     paint.setColor(SK_ColorBLACK);
39     SkRect r = SkRect::MakeWH(width, height);
40     canvas->drawRect(r, paint);
41     paint.setColor(color);
42     r.inset(border, border);
43     canvas->drawRect(r, paint);
44     SkFILEWStream stream(writePath);
45     recorder.finishRecordingAsPicture()->serialize(&stream);
46 }
47 
main(int argc,char ** argv)48 int main(int argc, char** argv) {
49     SkCommandLineFlags::SetUsage("Creates a simple .skp file for testing.");
50     SkCommandLineFlags::Parse(argc, argv);
51 
52     // Validate flags.
53     if ((FLAGS_blue < 0) || (FLAGS_blue > 255)) {
54         SkDebugf("--blue must be within range [0,255]\n");
55         exit(-1);
56     }
57     if ((FLAGS_green < 0) || (FLAGS_green > 255)) {
58         SkDebugf("--green must be within range [0,255]\n");
59         exit(-1);
60     }
61     if (FLAGS_height <= 0) {
62         SkDebugf("--height must be >0\n");
63         exit(-1);
64     }
65     if ((FLAGS_red < 0) || (FLAGS_red > 255)) {
66         SkDebugf("--red must be within range [0,255]\n");
67         exit(-1);
68     }
69     if (FLAGS_width <= 0) {
70         SkDebugf("--width must be >0\n");
71         exit(-1);
72     }
73     if (FLAGS_writePath.isEmpty()) {
74         SkDebugf("--writePath must be nonempty\n");
75         exit(-1);
76     }
77 
78     SkColor color = SkColorSetRGB(FLAGS_red, FLAGS_green, FLAGS_blue);
79     make_skp(SkIntToScalar(FLAGS_width),
80              SkIntToScalar(FLAGS_height),
81              SkIntToScalar(FLAGS_border),
82              color, FLAGS_writePath[0]);
83     return 0;
84 }
85