robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
msarett | f7a840a | 2016-03-09 10:09:02 -0800 | [diff] [blame] | 8 | #include "SkBitmap.h" |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 9 | #include "SkCommandLineFlags.h" |
| 10 | #include "SkCommonFlags.h" |
msarett | f7a840a | 2016-03-09 10:09:02 -0800 | [diff] [blame] | 11 | #include "SkData.h" |
| 12 | #include "SkForceLinking.h" |
| 13 | #include "SkImage.h" |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 14 | #include "SkStream.h" |
| 15 | #include "SkTypes.h" |
| 16 | |
| 17 | #include "sk_tool_utils.h" |
| 18 | |
msarett | f7a840a | 2016-03-09 10:09:02 -0800 | [diff] [blame] | 19 | __SK_FORCE_IMAGE_DECODER_LINKING; |
| 20 | |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 21 | DEFINE_string(in, "input.png", "Input image"); |
| 22 | DEFINE_string(out, "blurred.png", "Output image"); |
| 23 | DEFINE_double(sigma, 1, "Sigma to be used for blur (> 0.0f)"); |
| 24 | |
| 25 | |
| 26 | // This tool just performs a blur on an input image |
| 27 | // Return codes: |
| 28 | static const int kSuccess = 0; |
| 29 | static const int kError = 1; |
| 30 | |
| 31 | int tool_main(int argc, char** argv); |
| 32 | int tool_main(int argc, char** argv) { |
| 33 | SkCommandLineFlags::SetUsage("Brute force blur of an image."); |
| 34 | SkCommandLineFlags::Parse(argc, argv); |
| 35 | |
| 36 | if (FLAGS_sigma <= 0) { |
| 37 | if (!FLAGS_quiet) { |
| 38 | SkDebugf("Sigma must be greater than zero (it is %f).\n", FLAGS_sigma); |
| 39 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 40 | return kError; |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 41 | } |
| 42 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 43 | sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_in[0])); |
msarett | f7a840a | 2016-03-09 10:09:02 -0800 | [diff] [blame] | 44 | if (nullptr == data) { |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 45 | if (!FLAGS_quiet) { |
| 46 | SkDebugf("Couldn't open file: %s\n", FLAGS_in[0]); |
| 47 | } |
| 48 | return kError; |
| 49 | } |
| 50 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 51 | sk_sp<SkImage> image(SkImage::MakeFromEncoded(data)); |
msarett | f7a840a | 2016-03-09 10:09:02 -0800 | [diff] [blame] | 52 | if (!image) { |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 53 | if (!FLAGS_quiet) { |
msarett | f7a840a | 2016-03-09 10:09:02 -0800 | [diff] [blame] | 54 | SkDebugf("Couldn't create image for: %s.\n", FLAGS_in[0]); |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 55 | } |
| 56 | return kError; |
| 57 | } |
| 58 | |
| 59 | SkBitmap src; |
msarett | f7a840a | 2016-03-09 10:09:02 -0800 | [diff] [blame] | 60 | if (!image->asLegacyBitmap(&src, SkImage::kRW_LegacyBitmapMode)) { |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 61 | if (!FLAGS_quiet) { |
msarett | f7a840a | 2016-03-09 10:09:02 -0800 | [diff] [blame] | 62 | SkDebugf("Couldn't create bitmap for: %s.\n", FLAGS_in[0]); |
| 63 | } |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 64 | return kError; |
| 65 | } |
| 66 | |
| 67 | SkBitmap dst = sk_tool_utils::slow_blur(src, (float) FLAGS_sigma); |
| 68 | |
| 69 | if (!SkImageEncoder::EncodeFile(FLAGS_out[0], dst, SkImageEncoder::kPNG_Type, 100)) { |
| 70 | if (!FLAGS_quiet) { |
| 71 | SkDebugf("Couldn't write to file: %s\n", FLAGS_out[0]); |
| 72 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 73 | return kError; |
robertphillips | 9c4909b | 2015-10-19 06:39:17 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | return kSuccess; |
| 77 | } |
| 78 | |
| 79 | #if !defined SK_BUILD_FOR_IOS |
| 80 | int main(int argc, char * const argv[]) { |
| 81 | return tool_main(argc, (char**) argv); |
| 82 | } |
| 83 | #endif |