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 | |
| 19 | // Flags used by this file, alphabetically: |
| 20 | 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] | 21 | 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] | 22 | DEFINE_int32(green, 128, "Value of green color channel in image, 0-255."); |
| 23 | DEFINE_int32(height, 200, "Height of canvas to create."); |
| 24 | DEFINE_int32(red, 128, "Value of red color channel in image, 0-255."); |
| 25 | DEFINE_int32(width, 300, "Width of canvas to create."); |
| 26 | DEFINE_string(writePath, "", "Filepath to write the SKP into."); |
| 27 | |
commit-bot@chromium.org | 238771c | 2014-01-15 16:33:31 +0000 | [diff] [blame] | 28 | static void skpmaker(int width, int height, int border, SkColor color, |
commit-bot@chromium.org | c735598 | 2014-01-02 19:42:15 +0000 | [diff] [blame] | 29 | const char *writePath) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 30 | SkPictureRecorder recorder; |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 31 | SkCanvas* canvas = recorder.beginRecording(width, height, NULL, 0); |
commit-bot@chromium.org | c735598 | 2014-01-02 19:42:15 +0000 | [diff] [blame] | 32 | SkPaint paint; |
| 33 | paint.setStyle(SkPaint::kFill_Style); |
commit-bot@chromium.org | 238771c | 2014-01-15 16:33:31 +0000 | [diff] [blame] | 34 | paint.setColor(SK_ColorBLACK); |
commit-bot@chromium.org | 7e1a31d | 2014-01-15 16:04:45 +0000 | [diff] [blame] | 35 | canvas->drawRectCoords(0, 0, SkIntToScalar(width), SkIntToScalar(height), paint); |
commit-bot@chromium.org | 238771c | 2014-01-15 16:33:31 +0000 | [diff] [blame] | 36 | paint.setColor(color); |
| 37 | canvas->drawRectCoords(SkIntToScalar(border), SkIntToScalar(border), |
| 38 | SkIntToScalar(width - border*2), SkIntToScalar(height - border*2), |
| 39 | paint); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 40 | SkAutoTUnref<SkPicture> pict(recorder.endRecording()); |
commit-bot@chromium.org | c735598 | 2014-01-02 19:42:15 +0000 | [diff] [blame] | 41 | SkFILEWStream stream(writePath); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 42 | pict->serialize(&stream); |
commit-bot@chromium.org | c735598 | 2014-01-02 19:42:15 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | int tool_main(int argc, char** argv); |
| 46 | int 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.org | 238771c | 2014-01-15 16:33:31 +0000 | [diff] [blame] | 77 | skpmaker(FLAGS_width, FLAGS_height, FLAGS_border, color, FLAGS_writePath[0]); |
commit-bot@chromium.org | c735598 | 2014-01-02 19:42:15 +0000 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | #if !defined SK_BUILD_FOR_IOS |
| 82 | int main(int argc, char * const argv[]) { |
| 83 | return tool_main(argc, (char**) argv); |
| 84 | } |
| 85 | #endif |