aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/imgblur.cpp
blob: 3a0ae00f3bb1432b376d84a307579da8f96c7ed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkBitmap.h"
#include "SkCommandLineFlags.h"
#include "SkCommonFlags.h"
#include "SkData.h"
#include "SkForceLinking.h"
#include "SkImage.h"
#include "SkStream.h"
#include "SkTypes.h"

#include "sk_tool_utils.h"

__SK_FORCE_IMAGE_DECODER_LINKING;

DEFINE_string(in, "input.png", "Input image");
DEFINE_string(out, "blurred.png", "Output image");
DEFINE_double(sigma, 1, "Sigma to be used for blur (> 0.0f)");


// This tool just performs a blur on an input image
// Return codes:
static const int kSuccess = 0;
static const int kError = 1;

int tool_main(int argc, char** argv);
int tool_main(int argc, char** argv) {
    SkCommandLineFlags::SetUsage("Brute force blur of an image.");
    SkCommandLineFlags::Parse(argc, argv);

    if (FLAGS_sigma <= 0) {
        if (!FLAGS_quiet) {
            SkDebugf("Sigma must be greater than zero (it is %f).\n", FLAGS_sigma);
        }
        return kError;
    }

    sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_in[0]));
    if (nullptr == data) {
        if (!FLAGS_quiet) {
            SkDebugf("Couldn't open file: %s\n", FLAGS_in[0]);
        }
        return kError;
    }

    sk_sp<SkImage> image(SkImage::MakeFromEncoded(data));
    if (!image) {
        if (!FLAGS_quiet) {
            SkDebugf("Couldn't create image for: %s.\n", FLAGS_in[0]);
        }
        return kError;
    }

    SkBitmap src;
    if (!image->asLegacyBitmap(&src, SkImage::kRW_LegacyBitmapMode)) {
        if (!FLAGS_quiet) {
            SkDebugf("Couldn't create bitmap for: %s.\n", FLAGS_in[0]);
        }
        return kError;
    }

    SkBitmap dst = sk_tool_utils::slow_blur(src, (float) FLAGS_sigma);

    if (!SkImageEncoder::EncodeFile(FLAGS_out[0], dst, SkImageEncoder::kPNG_Type, 100)) {
        if (!FLAGS_quiet) {
            SkDebugf("Couldn't write to file: %s\n", FLAGS_out[0]);
        }
        return kError;
    }

    return kSuccess;
}

#if !defined SK_BUILD_FOR_IOS
int main(int argc, char * const argv[]) {
    return tool_main(argc, (char**) argv);
}
#endif