imgblur.cpp revision 9d524f22bfde5dc3dc8f48e1be39bdebd3bb0304
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 "SkForceLinking.h" 13f7a840aaba84ceffb3f19cf629d314db84f91d38msarett#include "SkImage.h" 149c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips#include "SkStream.h" 159c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips#include "SkTypes.h" 169c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips 179c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips#include "sk_tool_utils.h" 189c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips 19f7a840aaba84ceffb3f19cf629d314db84f91d38msarett__SK_FORCE_IMAGE_DECODER_LINKING; 20f7a840aaba84ceffb3f19cf629d314db84f91d38msarett 219c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillipsDEFINE_string(in, "input.png", "Input image"); 229c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillipsDEFINE_string(out, "blurred.png", "Output image"); 239c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillipsDEFINE_double(sigma, 1, "Sigma to be used for blur (> 0.0f)"); 249c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips 259c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips 269c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips// This tool just performs a blur on an input image 279c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips// Return codes: 289c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillipsstatic const int kSuccess = 0; 299c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillipsstatic const int kError = 1; 309c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips 319c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillipsint tool_main(int argc, char** argv); 329c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillipsint tool_main(int argc, char** argv) { 339c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips SkCommandLineFlags::SetUsage("Brute force blur of an image."); 349c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips SkCommandLineFlags::Parse(argc, argv); 359c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips 369c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips if (FLAGS_sigma <= 0) { 379c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips if (!FLAGS_quiet) { 389c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips SkDebugf("Sigma must be greater than zero (it is %f).\n", FLAGS_sigma); 399c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips } 409d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanary return kError; 419c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips } 429c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips 439ce9d6772df650ceb0511f275e1a83dffa78ff72reed sk_sp<SkData> data(SkData::MakeFromFileName(FLAGS_in[0])); 44f7a840aaba84ceffb3f19cf629d314db84f91d38msarett if (nullptr == data) { 459c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips if (!FLAGS_quiet) { 469c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips SkDebugf("Couldn't open file: %s\n", FLAGS_in[0]); 479c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips } 489c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips return kError; 499c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips } 509c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips 519ce9d6772df650ceb0511f275e1a83dffa78ff72reed sk_sp<SkImage> image(SkImage::MakeFromEncoded(data)); 52f7a840aaba84ceffb3f19cf629d314db84f91d38msarett if (!image) { 539c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips if (!FLAGS_quiet) { 54f7a840aaba84ceffb3f19cf629d314db84f91d38msarett SkDebugf("Couldn't create image for: %s.\n", FLAGS_in[0]); 559c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips } 569c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips return kError; 579c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips } 589c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips 599c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips SkBitmap src; 60f7a840aaba84ceffb3f19cf629d314db84f91d38msarett if (!image->asLegacyBitmap(&src, SkImage::kRW_LegacyBitmapMode)) { 619c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips if (!FLAGS_quiet) { 62f7a840aaba84ceffb3f19cf629d314db84f91d38msarett SkDebugf("Couldn't create bitmap for: %s.\n", FLAGS_in[0]); 63f7a840aaba84ceffb3f19cf629d314db84f91d38msarett } 649c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips return kError; 659c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips } 669c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips 679c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips SkBitmap dst = sk_tool_utils::slow_blur(src, (float) FLAGS_sigma); 689c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips 699c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips if (!SkImageEncoder::EncodeFile(FLAGS_out[0], dst, SkImageEncoder::kPNG_Type, 100)) { 709c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips if (!FLAGS_quiet) { 719c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips SkDebugf("Couldn't write to file: %s\n", FLAGS_out[0]); 729c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips } 739d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanary return kError; 749c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips } 759c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips 769c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips return kSuccess; 779c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips} 789c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips 799c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips#if !defined SK_BUILD_FOR_IOS 809c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillipsint main(int argc, char * const argv[]) { 819c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips return tool_main(argc, (char**) argv); 829c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips} 839c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3crobertphillips#endif 84