blob: 20d36842f05fadcca7a4b63622d207eafa4c4ee0 [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
Mike Kleinbe28ee22017-02-06 12:46:20 -050028int main(int argc, char** argv) {
robertphillips9c4909b2015-10-19 06:39:17 -070029 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 }
halcanary9d524f22016-03-29 09:03:52 -070036 return kError;
robertphillips9c4909b2015-10-19 06:39:17 -070037 }
38
reed9ce9d672016-03-17 10:51:11 -070039 sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_in[0]));
msarettf7a840a2016-03-09 10:09:02 -080040 if (nullptr == data) {
robertphillips9c4909b2015-10-19 06:39:17 -070041 if (!FLAGS_quiet) {
42 SkDebugf("Couldn't open file: %s\n", FLAGS_in[0]);
43 }
44 return kError;
45 }
46
reed9ce9d672016-03-17 10:51:11 -070047 sk_sp<SkImage> image(SkImage::MakeFromEncoded(data));
msarettf7a840a2016-03-09 10:09:02 -080048 if (!image) {
robertphillips9c4909b2015-10-19 06:39:17 -070049 if (!FLAGS_quiet) {
msarettf7a840a2016-03-09 10:09:02 -080050 SkDebugf("Couldn't create image for: %s.\n", FLAGS_in[0]);
robertphillips9c4909b2015-10-19 06:39:17 -070051 }
52 return kError;
53 }
54
55 SkBitmap src;
msarettf7a840a2016-03-09 10:09:02 -080056 if (!image->asLegacyBitmap(&src, SkImage::kRW_LegacyBitmapMode)) {
robertphillips9c4909b2015-10-19 06:39:17 -070057 if (!FLAGS_quiet) {
msarettf7a840a2016-03-09 10:09:02 -080058 SkDebugf("Couldn't create bitmap for: %s.\n", FLAGS_in[0]);
59 }
robertphillips9c4909b2015-10-19 06:39:17 -070060 return kError;
61 }
62
63 SkBitmap dst = sk_tool_utils::slow_blur(src, (float) FLAGS_sigma);
64
Hal Canarydb683012016-11-23 08:55:18 -070065 if (!sk_tool_utils::EncodeImageToFile(FLAGS_out[0], dst, SkEncodedImageFormat::kPNG, 100)) {
robertphillips9c4909b2015-10-19 06:39:17 -070066 if (!FLAGS_quiet) {
67 SkDebugf("Couldn't write to file: %s\n", FLAGS_out[0]);
68 }
halcanary9d524f22016-03-29 09:03:52 -070069 return kError;
robertphillips9c4909b2015-10-19 06:39:17 -070070 }
71
72 return kSuccess;
73}