SkEncoder: Rename files, change webp API, for consistency

Bug: skia:
Change-Id: I3dd6feb3d5661dcad3d2388b4d01fa9d3bbb15bb
Reviewed-on: https://skia-review.googlesource.com/15631
Reviewed-by: Leon Scroggins <scroggo@google.com>
Commit-Queue: Matt Sarett <msarett@google.com>
This commit is contained in:
Matt Sarett 2017-05-05 14:02:13 -04:00 committed by Skia Commit-Bot
parent 76fcb10f47
commit 04c3731de8
11 changed files with 78 additions and 38 deletions

View File

@ -470,8 +470,8 @@ optional("jpeg") {
"src/codec/SkJpegCodec.cpp",
"src/codec/SkJpegDecoderMgr.cpp",
"src/codec/SkJpegUtility.cpp",
"src/images/SkJPEGImageEncoder.cpp",
"src/images/SkJPEGWriteUtility.cpp",
"src/images/SkJpegEncoder.cpp",
]
}
@ -501,7 +501,7 @@ optional("png") {
sources = [
"src/codec/SkIcoCodec.cpp",
"src/codec/SkPngCodec.cpp",
"src/images/SkPNGImageEncoder.cpp",
"src/images/SkPngEncoder.cpp",
]
}
@ -547,7 +547,7 @@ optional("webp") {
sources = [
"src/codec/SkWebpAdapterCodec.cpp",
"src/codec/SkWebpCodec.cpp",
"src/images/SkWEBPImageEncoder.cpp",
"src/images/SkWebpEncoder.cpp",
]
}

View File

@ -15,6 +15,7 @@
#include "SkJpegEncoder.h"
#include "SkPngEncoder.h"
#include "SkUnPreMultiply.h"
#include "SkWebpEncoder.h"
namespace skiagm {
@ -80,8 +81,12 @@ static sk_sp<SkData> encode_data(SkEncodedImageFormat type, const SkBitmap& bitm
bool success = SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options());
return success ? buf.detachAsData() : nullptr;
}
case SkEncodedImageFormat::kWEBP:
return SkEncodeImageAsWEBP(&buf, src, 100) ? buf.detachAsData() : nullptr;
case SkEncodedImageFormat::kWEBP: {
SkWebpEncoder::Options options;
options.fUnpremulBehavior = SkTransferFunctionBehavior::kIgnore;
bool success = SkWebpEncoder::Encode(&buf, src, options);
return success ? buf.detachAsData() : nullptr;
}
default:
SkASSERT(false);
return nullptr;

View File

@ -18,6 +18,7 @@
#include "SkPngEncoder.h"
#include "SkPM4f.h"
#include "SkSRGB.h"
#include "SkWebpEncoder.h"
namespace skiagm {
@ -119,10 +120,10 @@ static sk_sp<SkData> encode_data(const SkBitmap& bitmap, SkEncodedImageFormat fo
SkDynamicMemoryWStream buf;
SkPngEncoder::Options pngOptions;
SkEncodeOptions options;
SkWebpEncoder::Options webpOptions;
if (bitmap.colorSpace()) {
pngOptions.fUnpremulBehavior = SkTransferFunctionBehavior::kRespect;
options.fUnpremulBehavior = SkTransferFunctionBehavior::kRespect;
webpOptions.fUnpremulBehavior = SkTransferFunctionBehavior::kRespect;
}
switch (format) {
@ -130,7 +131,7 @@ static sk_sp<SkData> encode_data(const SkBitmap& bitmap, SkEncodedImageFormat fo
SkAssertResult(SkPngEncoder::Encode(&buf, src, pngOptions));
break;
case SkEncodedImageFormat::kWEBP:
SkAssertResult(SkEncodeImageAsWEBP(&buf, src, options));
SkAssertResult(SkWebpEncoder::Encode(&buf, src, webpOptions));
break;
case SkEncodedImageFormat::kJPEG:
SkAssertResult(SkJpegEncoder::Encode(&buf, src, SkJpegEncoder::Options()));

View File

@ -8,6 +8,7 @@
#include "SkImageEncoderPriv.h"
#include "SkJpegEncoder.h"
#include "SkPngEncoder.h"
#include "SkWebpEncoder.h"
bool SkEncodeImage(SkWStream* dst, const SkPixmap& src,
SkEncodedImageFormat format, int quality) {
@ -28,8 +29,12 @@ bool SkEncodeImage(SkWStream* dst, const SkPixmap& src,
opts.fUnpremulBehavior = SkTransferFunctionBehavior::kIgnore;
return SkPngEncoder::Encode(dst, src, opts);
}
case SkEncodedImageFormat::kWEBP:
return SkEncodeImageAsWEBP(dst, src, quality);
case SkEncodedImageFormat::kWEBP: {
SkWebpEncoder::Options opts;
opts.fQuality = quality;
opts.fUnpremulBehavior = SkTransferFunctionBehavior::kIgnore;
return SkWebpEncoder::Encode(dst, src, opts);
}
default:
return false;
}

View File

@ -31,17 +31,6 @@ static inline bool SkPixmapIsValid(const SkPixmap& src,
return true;
}
struct SkEncodeOptions {
SkTransferFunctionBehavior fUnpremulBehavior = SkTransferFunctionBehavior::kIgnore;
};
#ifdef SK_HAS_WEBP_LIBRARY
bool SkEncodeImageAsWEBP(SkWStream*, const SkPixmap&, const SkEncodeOptions&);
bool SkEncodeImageAsWEBP(SkWStream*, const SkPixmap&, int quality);
#else
#define SkEncodeImageAsWEBP(...) false
#endif
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
bool SkEncodeImageWithCG(SkWStream*, const SkPixmap&, SkEncodedImageFormat);
#else

View File

@ -20,6 +20,11 @@ public:
// Add options for png filters and zlib compression.
struct Options {
/**
* If the input is premultiplied, this controls the unpremultiplication behavior.
* The encoder can convert to linear before unpremultiplying or ignore the transfer
* function and unpremultiply the input as is.
*/
SkTransferFunctionBehavior fUnpremulBehavior = SkTransferFunctionBehavior::kRespect;
};

View File

@ -25,6 +25,7 @@
#include "SkTemplates.h"
#include "SkUnPreMultiply.h"
#include "SkUtils.h"
#include "SkWebpEncoder.h"
// A WebP encoder only, on top of (subset of) libwebp
// For more information on WebP image format, and libwebp library, see:
@ -123,13 +124,10 @@ static int stream_writer(const uint8_t* data, size_t data_size,
return stream->write(data, data_size) ? 1 : 0;
}
static bool do_encode(SkWStream* stream, const SkPixmap& pixmap, const SkEncodeOptions& opts,
int quality) {
if (SkTransferFunctionBehavior::kRespect == opts.fUnpremulBehavior) {
if (!pixmap.colorSpace() || (!pixmap.colorSpace()->gammaCloseToSRGB() &&
!pixmap.colorSpace()->gammaIsLinear())) {
return false;
}
static bool do_encode(SkWStream* stream, const SkPixmap& pixmap, const SkWebpEncoder::Options& opts)
{
if (!SkPixmapIsValid(pixmap, opts.fUnpremulBehavior)) {
return false;
}
const transform_scanline_proc proc = choose_proc(pixmap.info(), opts.fUnpremulBehavior);
@ -166,7 +164,7 @@ static bool do_encode(SkWStream* stream, const SkPixmap& pixmap, const SkEncodeO
}
WebPConfig webp_config;
if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)) {
if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, opts.fQuality)) {
return false;
}
@ -238,12 +236,8 @@ static bool do_encode(SkWStream* stream, const SkPixmap& pixmap, const SkEncodeO
return true;
}
bool SkEncodeImageAsWEBP(SkWStream* stream, const SkPixmap& src, int quality) {
return do_encode(stream, src, SkEncodeOptions(), quality);
}
bool SkEncodeImageAsWEBP(SkWStream* stream, const SkPixmap& src, const SkEncodeOptions& opts) {
return do_encode(stream, src, opts, 100);
bool SkWebpEncoder::Encode(SkWStream* dst, const SkPixmap& src, const Options& opts) {
return do_encode(dst, src, opts);
}
#endif

View File

@ -0,0 +1,40 @@
/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkWebpEncoder_DEFINED
#define SkWebpEncoder_DEFINED
#include "SkEncoder.h"
class SkWStream;
namespace SkWebpEncoder {
struct Options {
/**
* |fQuality| must be in [0.0f, 100.0f] where 0.0f corresponds to the lowest quality.
*/
float fQuality = 100.0f;
/**
* If the input is premultiplied, this controls the unpremultiplication behavior.
* The encoder can convert to linear before unpremultiplying or ignore the transfer
* function and unpremultiply the input as is.
*/
SkTransferFunctionBehavior fUnpremulBehavior = SkTransferFunctionBehavior::kRespect;
};
/**
* Encode the |src| pixels to the |dst| stream.
* |options| may be used to control the encoding behavior.
*
* Returns true on success. Returns false on an invalid or unsupported |src|.
*/
bool Encode(SkWStream* dst, const SkPixmap& src, const Options& options);
};
#endif

View File

@ -26,6 +26,7 @@
#include "SkRandom.h"
#include "SkStream.h"
#include "SkStreamPriv.h"
#include "SkWebpEncoder.h"
#include "Test.h"
#include "png.h"
@ -1529,9 +1530,9 @@ static void encode_format(SkDynamicMemoryWStream* stream, const SkPixmap& pixmap
SkTransferFunctionBehavior unpremulBehavior,
SkEncodedImageFormat format) {
SkPngEncoder::Options pngOptions;
SkEncodeOptions options;
SkWebpEncoder::Options webpOptions;
pngOptions.fUnpremulBehavior = unpremulBehavior;
options.fUnpremulBehavior = unpremulBehavior;
webpOptions.fUnpremulBehavior = unpremulBehavior;
switch (format) {
case SkEncodedImageFormat::kPNG:
SkPngEncoder::Encode(stream, pixmap, pngOptions);
@ -1540,7 +1541,7 @@ static void encode_format(SkDynamicMemoryWStream* stream, const SkPixmap& pixmap
SkJpegEncoder::Encode(stream, pixmap, SkJpegEncoder::Options());
break;
case SkEncodedImageFormat::kWEBP:
SkEncodeImageAsWEBP(stream, pixmap, options);
SkWebpEncoder::Encode(stream, pixmap, webpOptions);
break;
default:
SkASSERT(false);