Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Namespaces | Typedefs | Functions
SkWebpEncoderImpl.cpp File Reference
#include "include/encode/SkWebpEncoder.h"
#include "include/core/SkAlphaType.h"
#include "include/core/SkBitmap.h"
#include "include/core/SkColorType.h"
#include "include/core/SkData.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPixmap.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkSpan.h"
#include "include/core/SkStream.h"
#include "include/encode/SkEncoder.h"
#include "include/private/base/SkTemplates.h"
#include "src/core/SkImageInfoPriv.h"
#include "src/encode/SkImageEncoderFns.h"
#include "src/encode/SkImageEncoderPriv.h"
#include "src/image/SkImage_Base.h"
#include <cstddef>
#include <cstdint>
#include <memory>
#include "webp/encode.h"
#include "webp/mux.h"
#include "webp/mux_types.h"

Go to the source code of this file.

Namespaces

namespace  SkWebpEncoder
 

Typedefs

using WebPPictureImportProc = int(*)(WebPPicture *picture, const uint8_t *pixels, int stride)
 

Functions

static int stream_writer (const uint8_t *data, size_t data_size, const WebPPicture *const picture)
 
static bool preprocess_webp_picture (WebPPicture *pic, WebPConfig *webp_config, const SkPixmap &pixmap, const SkWebpEncoder::Options &opts)
 
SK_API bool SkWebpEncoder::Encode (SkWStream *dst, const SkPixmap &src, const Options &options)
 
SK_API bool SkWebpEncoder::EncodeAnimated (SkWStream *dst, SkSpan< const SkEncoder::Frame > src, const Options &options)
 
SK_API sk_sp< SkDataSkWebpEncoder::Encode (GrDirectContext *ctx, const SkImage *img, const Options &options)
 

Typedef Documentation

◆ WebPPictureImportProc

using WebPPictureImportProc = int (*)(WebPPicture* picture, const uint8_t* pixels, int stride)

Definition at line 52 of file SkWebpEncoderImpl.cpp.

Function Documentation

◆ preprocess_webp_picture()

static bool preprocess_webp_picture ( WebPPicture *  pic,
WebPConfig *  webp_config,
const SkPixmap pixmap,
const SkWebpEncoder::Options opts 
)
static

Definition at line 54 of file SkWebpEncoderImpl.cpp.

57 {
58 if (!SkPixmapIsValid(pixmap)) {
59 return false;
60 }
61
62 if (SkColorTypeIsAlphaOnly(pixmap.colorType())) {
63 // Maintain the existing behavior of not supporting encoding alpha-only images.
64 // TODO: Support encoding alpha only to an image with alpha but no color?
65 return false;
66 }
67
68 if (nullptr == pixmap.addr()) {
69 return false;
70 }
71
72 pic->width = pixmap.width();
73 pic->height = pixmap.height();
74
75 // Set compression, method, and pixel format.
76 // libwebp recommends using BGRA for lossless and YUV for lossy.
77 // The choices of |webp_config.method| currently just match Chrome's defaults. We
78 // could potentially expose this decision to the client.
80 webp_config->lossless = 0;
81#ifndef SK_WEBP_ENCODER_USE_DEFAULT_METHOD
82 webp_config->method = 3;
83#endif
84 pic->use_argb = 0;
85 } else {
86 webp_config->lossless = 1;
87 webp_config->method = 0;
88 pic->use_argb = 1;
89 }
90
91 {
92 const SkColorType ct = pixmap.colorType();
93 const bool premul = pixmap.alphaType() == kPremul_SkAlphaType;
94
95 SkBitmap tmpBm;
96 WebPPictureImportProc importProc = nullptr;
97 const SkPixmap* src = &pixmap;
98 if (ct == kRGB_888x_SkColorType) {
99 importProc = WebPPictureImportRGBX;
100 } else if (!premul && ct == kRGBA_8888_SkColorType) {
101 importProc = WebPPictureImportRGBA;
102 }
103#ifdef WebPPictureImportBGRA
104 else if (!premul && ct == kBGRA_8888_SkColorType) {
105 importProc = WebPPictureImportBGRA;
106 }
107#endif
108 else {
109 importProc = WebPPictureImportRGBA;
110 auto info = pixmap.info()
113 if (!tmpBm.tryAllocPixels(info) ||
114 !pixmap.readPixels(tmpBm.info(), tmpBm.getPixels(), tmpBm.rowBytes())) {
115 return false;
116 }
117 src = &tmpBm.pixmap();
118 }
119
120 if (!importProc(pic, reinterpret_cast<const uint8_t*>(src->addr()), src->rowBytes())) {
121 return false;
122 }
123 }
124
125 return true;
126}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
kUnpremul_SkAlphaType
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
SkColorType
Definition SkColorType.h:19
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition SkColorType.h:26
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
@ kRGB_888x_SkColorType
pixel with 8 bits each for red, green, blue; in 32-bit word
Definition SkColorType.h:25
static bool SkPixmapIsValid(const SkPixmap &src)
static bool SkColorTypeIsAlphaOnly(SkColorType ct)
int(*)(WebPPicture *picture, const uint8_t *pixels, int stride) WebPPictureImportProc
static uint32_t premul(uint32_t color)
const SkPixmap & pixmap() const
Definition SkBitmap.h:133
size_t rowBytes() const
Definition SkBitmap.h:238
void * getPixels() const
Definition SkBitmap.h:283
const SkImageInfo & info() const
Definition SkBitmap.h:139
bool tryAllocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:271
int width() const
Definition SkPixmap.h:160
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes) const
Definition SkPixmap.h:592
SkColorType colorType() const
Definition SkPixmap.h:173
const SkImageInfo & info() const
Definition SkPixmap.h:135
const void * addr() const
Definition SkPixmap.h:153
int height() const
Definition SkPixmap.h:166
SkAlphaType alphaType() const
Definition SkPixmap.h:175
SkImageInfo makeAlphaType(SkAlphaType newAlphaType) const
SkImageInfo makeColorType(SkColorType newColorType) const

◆ stream_writer()

static int stream_writer ( const uint8_t *  data,
size_t  data_size,
const WebPPicture *const  picture 
)
static

Definition at line 47 of file SkWebpEncoderImpl.cpp.

47 {
48 SkWStream* const stream = (SkWStream*)picture->custom_ptr;
49 return stream->write(data, data_size) ? 1 : 0;
50}
sk_sp< const SkPicture > picture
Definition SkRecords.h:299