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
8#include "SkCommandLineFlags.h"
9#include "SkCommonFlags.h"
10#include "SkImageDecoder.h"
11#include "SkStream.h"
12#include "SkTypes.h"
13
14#include "sk_tool_utils.h"
15
16DEFINE_string(in, "input.png", "Input image");
17DEFINE_string(out, "blurred.png", "Output image");
18DEFINE_double(sigma, 1, "Sigma to be used for blur (> 0.0f)");
19
20
21// This tool just performs a blur on an input image
22// Return codes:
23static const int kSuccess = 0;
24static const int kError = 1;
25
26int tool_main(int argc, char** argv);
27int tool_main(int argc, char** argv) {
28    SkCommandLineFlags::SetUsage("Brute force blur of an image.");
29    SkCommandLineFlags::Parse(argc, argv);
30
31    if (FLAGS_sigma <= 0) {
32        if (!FLAGS_quiet) {
33            SkDebugf("Sigma must be greater than zero (it is %f).\n", FLAGS_sigma);
34        }
35        return kError;
36    }
37
38    SkFILEStream inputStream(FLAGS_in[0]);
39    if (!inputStream.isValid()) {
40        if (!FLAGS_quiet) {
41            SkDebugf("Couldn't open file: %s\n", FLAGS_in[0]);
42        }
43        return kError;
44    }
45
46    SkAutoTDelete<SkImageDecoder> codec(SkImageDecoder::Factory(&inputStream));
47    if (!codec) {
48        if (!FLAGS_quiet) {
49            SkDebugf("Couldn't create codec for: %s.\n", FLAGS_in[0]);
50        }
51        return kError;
52    }
53
54    SkBitmap src;
55
56    inputStream.rewind();
57    SkImageDecoder::Result res = codec->decode(&inputStream, &src,
58                                               kN32_SkColorType,
59                                               SkImageDecoder::kDecodePixels_Mode);
60    if (SkImageDecoder::kSuccess != res) {
61        if (!FLAGS_quiet) {
62            SkDebugf("Couldn't decode image: %s.\n", FLAGS_in[0]);
63        }
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        }
73        return kError;
74    }
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
84