19c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips/*
29c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips * Copyright 2015 Google Inc.
39c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips *
49c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips * Use of this source code is governed by a BSD-style license that can be
59c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips * found in the LICENSE file.
69c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips */
79c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips
8f7a840aaba84ceffb3f19cf629d314db84f91d38msarett#include "SkBitmap.h"
99c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips#include "SkCommandLineFlags.h"
109c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips#include "SkCommonFlags.h"
11f7a840aaba84ceffb3f19cf629d314db84f91d38msarett#include "SkData.h"
12f7a840aaba84ceffb3f19cf629d314db84f91d38msarett#include "SkImage.h"
139c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips#include "SkStream.h"
149c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips#include "SkTypes.h"
159c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips
169c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips#include "sk_tool_utils.h"
179c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips
189c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillipsDEFINE_string(in, "input.png", "Input image");
199c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillipsDEFINE_string(out, "blurred.png", "Output image");
209c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillipsDEFINE_double(sigma, 1, "Sigma to be used for blur (> 0.0f)");
219c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips
229c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips
239c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips// This tool just performs a blur on an input image
249c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips// Return codes:
259c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillipsstatic const int kSuccess = 0;
269c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillipsstatic const int kError = 1;
279c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips
28be28ee2974474800323ce4fabf62a839018be591Mike Kleinint main(int argc, char** argv) {
299c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips    SkCommandLineFlags::SetUsage("Brute force blur of an image.");
309c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips    SkCommandLineFlags::Parse(argc, argv);
319c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips
329c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips    if (FLAGS_sigma <= 0) {
339c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips        if (!FLAGS_quiet) {
349c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips            SkDebugf("Sigma must be greater than zero (it is %f).\n", FLAGS_sigma);
359c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips        }
369d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanary        return kError;
379c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips    }
389c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips
399ce9d6772df650ceb0511f275e1a83dffa78ff72reed    sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_in[0]));
40f7a840aaba84ceffb3f19cf629d314db84f91d38msarett    if (nullptr == data) {
419c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips        if (!FLAGS_quiet) {
429c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips            SkDebugf("Couldn't open file: %s\n", FLAGS_in[0]);
439c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips        }
449c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips        return kError;
459c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips    }
469c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips
479ce9d6772df650ceb0511f275e1a83dffa78ff72reed    sk_sp<SkImage> image(SkImage::MakeFromEncoded(data));
48f7a840aaba84ceffb3f19cf629d314db84f91d38msarett    if (!image) {
499c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips        if (!FLAGS_quiet) {
50f7a840aaba84ceffb3f19cf629d314db84f91d38msarett            SkDebugf("Couldn't create image for: %s.\n", FLAGS_in[0]);
519c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips        }
529c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips        return kError;
539c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips    }
549c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips
559c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips    SkBitmap src;
56f7a840aaba84ceffb3f19cf629d314db84f91d38msarett    if (!image->asLegacyBitmap(&src, SkImage::kRW_LegacyBitmapMode)) {
579c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips        if (!FLAGS_quiet) {
58f7a840aaba84ceffb3f19cf629d314db84f91d38msarett            SkDebugf("Couldn't create bitmap for: %s.\n", FLAGS_in[0]);
59f7a840aaba84ceffb3f19cf629d314db84f91d38msarett        }
609c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips        return kError;
619c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips    }
629c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips
639c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips    SkBitmap dst = sk_tool_utils::slow_blur(src, (float) FLAGS_sigma);
649c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips
65db6830162eca5b94e61d9825ec93306fc615d204Hal Canary    if (!sk_tool_utils::EncodeImageToFile(FLAGS_out[0], dst, SkEncodedImageFormat::kPNG, 100)) {
669c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips        if (!FLAGS_quiet) {
679c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips            SkDebugf("Couldn't write to file: %s\n", FLAGS_out[0]);
689c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips        }
699d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanary        return kError;
709c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips    }
719c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips
729c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips    return kSuccess;
739c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips}
74