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 "SkBitmap.h"
9#include "SkCommandLineFlags.h"
10#include "SkCommonFlags.h"
11#include "SkData.h"
12#include "SkImage.h"
13#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 main(int argc, char** argv) {
29    SkCommandLineFlags::SetUsage("Brute force blur of an image.");
30    SkCommandLineFlags::Parse(argc, argv);
31
32    if (FLAGS_sigma <= 0) {
33        if (!FLAGS_quiet) {
34            SkDebugf("Sigma must be greater than zero (it is %f).\n", FLAGS_sigma);
35        }
36        return kError;
37    }
38
39    sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_in[0]));
40    if (nullptr == data) {
41        if (!FLAGS_quiet) {
42            SkDebugf("Couldn't open file: %s\n", FLAGS_in[0]);
43        }
44        return kError;
45    }
46
47    sk_sp<SkImage> image(SkImage::MakeFromEncoded(data));
48    if (!image) {
49        if (!FLAGS_quiet) {
50            SkDebugf("Couldn't create image for: %s.\n", FLAGS_in[0]);
51        }
52        return kError;
53    }
54
55    SkBitmap src;
56    if (!image->asLegacyBitmap(&src, SkImage::kRW_LegacyBitmapMode)) {
57        if (!FLAGS_quiet) {
58            SkDebugf("Couldn't create bitmap for: %s.\n", FLAGS_in[0]);
59        }
60        return kError;
61    }
62
63    SkBitmap dst = sk_tool_utils::slow_blur(src, (float) FLAGS_sigma);
64
65    if (!sk_tool_utils::EncodeImageToFile(FLAGS_out[0], dst, SkEncodedImageFormat::kPNG, 100)) {
66        if (!FLAGS_quiet) {
67            SkDebugf("Couldn't write to file: %s\n", FLAGS_out[0]);
68        }
69        return kError;
70    }
71
72    return kSuccess;
73}
74