blob: 23ba815db35f76fb12b0807783a718a357bf530f [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"
msarettf7a840a2016-03-09 10:09:02 -080012#include "SkImage.h"
robertphillips9c4909b2015-10-19 06:39:17 -070013#include "SkStream.h"
14#include "SkTypes.h"
15
16#include "sk_tool_utils.h"
17
18DEFINE_string(in, "input.png", "Input image");
19DEFINE_string(out, "blurred.png", "Output image");
20DEFINE_double(sigma, 1, "Sigma to be used for blur (> 0.0f)");
21
22
23// This tool just performs a blur on an input image
24// Return codes:
25static const int kSuccess = 0;
26static const int kError = 1;
27
28int tool_main(int argc, char** argv);
29int tool_main(int argc, char** argv) {
30 SkCommandLineFlags::SetUsage("Brute force blur of an image.");
31 SkCommandLineFlags::Parse(argc, argv);
32
33 if (FLAGS_sigma <= 0) {
34 if (!FLAGS_quiet) {
35 SkDebugf("Sigma must be greater than zero (it is %f).\n", FLAGS_sigma);
36 }
halcanary9d524f22016-03-29 09:03:52 -070037 return kError;
robertphillips9c4909b2015-10-19 06:39:17 -070038 }
39
reed9ce9d672016-03-17 10:51:11 -070040 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_in[0]));
msarettf7a840a2016-03-09 10:09:02 -080041 if (nullptr == data) {
robertphillips9c4909b2015-10-19 06:39:17 -070042 if (!FLAGS_quiet) {
43 SkDebugf("Couldn't open file: %s\n", FLAGS_in[0]);
44 }
45 return kError;
46 }
47
reed9ce9d672016-03-17 10:51:11 -070048 sk_sp<SkImage> image(SkImage::MakeFromEncoded(data));
msarettf7a840a2016-03-09 10:09:02 -080049 if (!image) {
robertphillips9c4909b2015-10-19 06:39:17 -070050 if (!FLAGS_quiet) {
msarettf7a840a2016-03-09 10:09:02 -080051 SkDebugf("Couldn't create image for: %s.\n", FLAGS_in[0]);
robertphillips9c4909b2015-10-19 06:39:17 -070052 }
53 return kError;
54 }
55
56 SkBitmap src;
msarettf7a840a2016-03-09 10:09:02 -080057 if (!image->asLegacyBitmap(&src, SkImage::kRW_LegacyBitmapMode)) {
robertphillips9c4909b2015-10-19 06:39:17 -070058 if (!FLAGS_quiet) {
msarettf7a840a2016-03-09 10:09:02 -080059 SkDebugf("Couldn't create bitmap for: %s.\n", FLAGS_in[0]);
60 }
robertphillips9c4909b2015-10-19 06:39:17 -070061 return kError;
62 }
63
64 SkBitmap dst = sk_tool_utils::slow_blur(src, (float) FLAGS_sigma);
65
Hal Canarydb683012016-11-23 08:55:18 -070066 if (!sk_tool_utils::EncodeImageToFile(FLAGS_out[0], dst, SkEncodedImageFormat::kPNG, 100)) {
robertphillips9c4909b2015-10-19 06:39:17 -070067 if (!FLAGS_quiet) {
68 SkDebugf("Couldn't write to file: %s\n", FLAGS_out[0]);
69 }
halcanary9d524f22016-03-29 09:03:52 -070070 return kError;
robertphillips9c4909b2015-10-19 06:39:17 -070071 }
72
73 return kSuccess;
74}
75
76#if !defined SK_BUILD_FOR_IOS
77int main(int argc, char * const argv[]) {
78 return tool_main(argc, (char**) argv);
79}
80#endif