blob: 3a0ae00f3bb1432b376d84a307579da8f96c7ed6 [file] [log] [blame]
robertphillips9c4909b2015-10-19 06:39:17 -07001/*
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
msarettf7a840a2016-03-09 10:09:02 -08008#include "SkBitmap.h"
robertphillips9c4909b2015-10-19 06:39:17 -07009#include "SkCommandLineFlags.h"
10#include "SkCommonFlags.h"
msarettf7a840a2016-03-09 10:09:02 -080011#include "SkData.h"
12#include "SkForceLinking.h"
13#include "SkImage.h"
robertphillips9c4909b2015-10-19 06:39:17 -070014#include "SkStream.h"
15#include "SkTypes.h"
16
17#include "sk_tool_utils.h"
18
msarettf7a840a2016-03-09 10:09:02 -080019__SK_FORCE_IMAGE_DECODER_LINKING;
20
robertphillips9c4909b2015-10-19 06:39:17 -070021DEFINE_string(in, "input.png", "Input image");
22DEFINE_string(out, "blurred.png", "Output image");
23DEFINE_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:
28static const int kSuccess = 0;
29static const int kError = 1;
30
31int tool_main(int argc, char** argv);
32int 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 }
halcanary9d524f22016-03-29 09:03:52 -070040 return kError;
robertphillips9c4909b2015-10-19 06:39:17 -070041 }
42
reed9ce9d672016-03-17 10:51:11 -070043 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_in[0]));
msarettf7a840a2016-03-09 10:09:02 -080044 if (nullptr == data) {
robertphillips9c4909b2015-10-19 06:39:17 -070045 if (!FLAGS_quiet) {
46 SkDebugf("Couldn't open file: %s\n", FLAGS_in[0]);
47 }
48 return kError;
49 }
50
reed9ce9d672016-03-17 10:51:11 -070051 sk_sp<SkImage> image(SkImage::MakeFromEncoded(data));
msarettf7a840a2016-03-09 10:09:02 -080052 if (!image) {
robertphillips9c4909b2015-10-19 06:39:17 -070053 if (!FLAGS_quiet) {
msarettf7a840a2016-03-09 10:09:02 -080054 SkDebugf("Couldn't create image for: %s.\n", FLAGS_in[0]);
robertphillips9c4909b2015-10-19 06:39:17 -070055 }
56 return kError;
57 }
58
59 SkBitmap src;
msarettf7a840a2016-03-09 10:09:02 -080060 if (!image->asLegacyBitmap(&src, SkImage::kRW_LegacyBitmapMode)) {
robertphillips9c4909b2015-10-19 06:39:17 -070061 if (!FLAGS_quiet) {
msarettf7a840a2016-03-09 10:09:02 -080062 SkDebugf("Couldn't create bitmap for: %s.\n", FLAGS_in[0]);
63 }
robertphillips9c4909b2015-10-19 06:39:17 -070064 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 }
halcanary9d524f22016-03-29 09:03:52 -070073 return kError;
robertphillips9c4909b2015-10-19 06:39:17 -070074 }
75
76 return kSuccess;
77}
78
79#if !defined SK_BUILD_FOR_IOS
80int main(int argc, char * const argv[]) {
81 return tool_main(argc, (char**) argv);
82}
83#endif