commit-bot@chromium.org | c735598 | 2014-01-02 19:42:15 +0000 | [diff] [blame] | 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" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 15 | #include "SkPictureRecorder.h" |
commit-bot@chromium.org | c735598 | 2014-01-02 19:42:15 +0000 | [diff] [blame] | 16 | #include "SkScalar.h" |
| 17 | #include "SkStream.h" |
| 18 | |
bungeman | 60e0fee | 2015-08-26 05:15:46 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | |
commit-bot@chromium.org | c735598 | 2014-01-02 19:42:15 +0000 | [diff] [blame] | 21 | // Flags used by this file, alphabetically: |
| 22 | DEFINE_int32(blue, 128, "Value of blue color channel in image, 0-255."); |
commit-bot@chromium.org | 238771c | 2014-01-15 16:33:31 +0000 | [diff] [blame] | 23 | DEFINE_int32(border, 4, "Width of the black border around the image."); |
commit-bot@chromium.org | c735598 | 2014-01-02 19:42:15 +0000 | [diff] [blame] | 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 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 30 | // Create a 'width' by 'height' skp with a 'border'-wide black border around |
| 31 | // a 'color' rectangle. |
| 32 | static void make_skp(SkScalar width, SkScalar height, SkScalar border, SkColor color, |
commit-bot@chromium.org | c735598 | 2014-01-02 19:42:15 +0000 | [diff] [blame] | 33 | const char *writePath) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 34 | SkPictureRecorder recorder; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | SkCanvas* canvas = recorder.beginRecording(width, height, nullptr, 0); |
commit-bot@chromium.org | c735598 | 2014-01-02 19:42:15 +0000 | [diff] [blame] | 36 | SkPaint paint; |
| 37 | paint.setStyle(SkPaint::kFill_Style); |
commit-bot@chromium.org | 238771c | 2014-01-15 16:33:31 +0000 | [diff] [blame] | 38 | paint.setColor(SK_ColorBLACK); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 39 | SkRect r = SkRect::MakeWH(width, height); |
| 40 | canvas->drawRect(r, paint); |
commit-bot@chromium.org | 238771c | 2014-01-15 16:33:31 +0000 | [diff] [blame] | 41 | paint.setColor(color); |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 42 | r.inset(border, border); |
| 43 | canvas->drawRect(r, paint); |
commit-bot@chromium.org | c735598 | 2014-01-02 19:42:15 +0000 | [diff] [blame] | 44 | SkFILEWStream stream(writePath); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 45 | recorder.finishRecordingAsPicture()->serialize(&stream); |
commit-bot@chromium.org | c735598 | 2014-01-02 19:42:15 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | int tool_main(int argc, char** argv); |
| 49 | int tool_main(int argc, char** argv) { |
| 50 | SkCommandLineFlags::SetUsage("Creates a simple .skp file for testing."); |
| 51 | SkCommandLineFlags::Parse(argc, argv); |
| 52 | |
| 53 | // Validate flags. |
| 54 | if ((FLAGS_blue < 0) || (FLAGS_blue > 255)) { |
| 55 | SkDebugf("--blue must be within range [0,255]\n"); |
| 56 | exit(-1); |
| 57 | } |
| 58 | if ((FLAGS_green < 0) || (FLAGS_green > 255)) { |
| 59 | SkDebugf("--green must be within range [0,255]\n"); |
| 60 | exit(-1); |
| 61 | } |
| 62 | if (FLAGS_height <= 0) { |
| 63 | SkDebugf("--height must be >0\n"); |
| 64 | exit(-1); |
| 65 | } |
| 66 | if ((FLAGS_red < 0) || (FLAGS_red > 255)) { |
| 67 | SkDebugf("--red must be within range [0,255]\n"); |
| 68 | exit(-1); |
| 69 | } |
| 70 | if (FLAGS_width <= 0) { |
| 71 | SkDebugf("--width must be >0\n"); |
| 72 | exit(-1); |
| 73 | } |
| 74 | if (FLAGS_writePath.isEmpty()) { |
| 75 | SkDebugf("--writePath must be nonempty\n"); |
| 76 | exit(-1); |
| 77 | } |
| 78 | |
| 79 | SkColor color = SkColorSetRGB(FLAGS_red, FLAGS_green, FLAGS_blue); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 80 | make_skp(SkIntToScalar(FLAGS_width), |
| 81 | SkIntToScalar(FLAGS_height), |
| 82 | SkIntToScalar(FLAGS_border), |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 83 | color, FLAGS_writePath[0]); |
commit-bot@chromium.org | c735598 | 2014-01-02 19:42:15 +0000 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | #if !defined SK_BUILD_FOR_IOS |
| 88 | int main(int argc, char * const argv[]) { |
| 89 | return tool_main(argc, (char**) argv); |
| 90 | } |
| 91 | #endif |