Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Namespaces | Functions
SkJpegEncoderImpl.cpp File Reference
#include "src/encode/SkJpegEncoderImpl.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/SkStream.h"
#include "include/core/SkYUVAInfo.h"
#include "include/core/SkYUVAPixmaps.h"
#include "include/encode/SkEncoder.h"
#include "include/encode/SkJpegEncoder.h"
#include "include/private/base/SkAssert.h"
#include "include/private/base/SkNoncopyable.h"
#include "include/private/base/SkTemplates.h"
#include "src/base/SkMSAN.h"
#include "src/codec/SkJpegConstants.h"
#include "src/codec/SkJpegPriv.h"
#include "src/encode/SkImageEncoderFns.h"
#include "src/encode/SkImageEncoderPriv.h"
#include "src/encode/SkJPEGWriteUtility.h"
#include "src/image/SkImage_Base.h"
#include <csetjmp>
#include <cstdint>
#include <cstring>
#include <memory>
#include <utility>
#include "jpeglib.h"

Go to the source code of this file.

Classes

class  SkJpegEncoderMgr
 

Namespaces

namespace  SkJpegEncoder
 

Functions

static void yuva_copy_row (const SkYUVAPixmaps *src, int row, uint8_t *dst)
 
static std::unique_ptr< SkEncoderMake (SkWStream *dst, const SkPixmap *src, const SkYUVAPixmaps *srcYUVA, const SkColorSpace *srcYUVAColorSpace, const SkJpegEncoder::Options &options)
 
SK_API bool SkJpegEncoder::Encode (SkWStream *dst, const SkPixmap &src, const Options &options)
 
SK_API bool SkJpegEncoder::Encode (SkWStream *dst, const SkYUVAPixmaps &src, const SkColorSpace *srcColorSpace, const Options &options)
 
SK_API sk_sp< SkDataSkJpegEncoder::Encode (GrDirectContext *ctx, const SkImage *img, const Options &options)
 
SK_API std::unique_ptr< SkEncoderSkJpegEncoder::Make (SkWStream *dst, const SkPixmap &src, const Options &options)
 
SK_API std::unique_ptr< SkEncoderSkJpegEncoder::Make (SkWStream *dst, const SkYUVAPixmaps &src, const SkColorSpace *srcColorSpace, const Options &options)
 

Function Documentation

◆ Make()

static std::unique_ptr< SkEncoder > Make ( SkWStream dst,
const SkPixmap src,
const SkYUVAPixmaps srcYUVA,
const SkColorSpace srcYUVAColorSpace,
const SkJpegEncoder::Options options 
)
static

Definition at line 259 of file SkJpegEncoderImpl.cpp.

263 {
264 // Exactly one of |src| or |srcYUVA| should be specified.
265 if (srcYUVA) {
266 SkASSERT(!src);
267 if (!srcYUVA->isValid()) {
268 return nullptr;
269 }
270 } else {
271 SkASSERT(src);
272 if (!src || !SkPixmapIsValid(*src)) {
273 return nullptr;
274 }
275 }
276
277 std::unique_ptr<SkJpegEncoderMgr> encoderMgr = SkJpegEncoderMgr::Make(dst);
278
279 skjpeg_error_mgr::AutoPushJmpBuf jmp(encoderMgr->errorMgr());
280 if (setjmp(jmp)) {
281 return nullptr;
282 }
283
284 if (srcYUVA) {
285 if (!encoderMgr->setParams(srcYUVA->pixmapsInfo(), options)) {
286 return nullptr;
287 }
288 } else {
289 if (!encoderMgr->setParams(src->info(), options)) {
290 return nullptr;
291 }
292 }
293
294 jpeg_set_quality(encoderMgr->cinfo(), options.fQuality, TRUE);
295 jpeg_start_compress(encoderMgr->cinfo(), TRUE);
296
297 // Write XMP metadata. This will only write the standard XMP segment.
298 // TODO(ccameron): Split this into a standard and extended XMP segment if needed.
299 if (options.xmpMetadata) {
301 s.write(kXMPStandardSig, sizeof(kXMPStandardSig));
302 s.write(options.xmpMetadata->data(), options.xmpMetadata->size());
303 auto data = s.detachAsData();
304 jpeg_write_marker(encoderMgr->cinfo(), kXMPMarker, data->bytes(), data->size());
305 }
306
307 // Write the ICC profile.
308 // TODO(ccameron): This limits ICC profile size to a single segment's parameters (less than
309 // 64k). Split larger profiles into more segments.
310 sk_sp<SkData> icc = icc_from_color_space(srcYUVA ? srcYUVAColorSpace : src->colorSpace(),
311 options.fICCProfile,
312 options.fICCProfileDescription);
313 if (icc) {
314 // Create a contiguous block of memory with the icc signature followed by the profile.
316 uint8_t* ptr = (uint8_t*)markerData->writable_data();
317 memcpy(ptr, kICCSig, sizeof(kICCSig));
318 ptr += sizeof(kICCSig);
319 *ptr++ = 1; // This is the first marker.
320 *ptr++ = 1; // Out of one total markers.
321 memcpy(ptr, icc->data(), icc->size());
322
323 jpeg_write_marker(encoderMgr->cinfo(), kICCMarker, markerData->bytes(), markerData->size());
324 }
325
326 if (srcYUVA) {
327 return std::make_unique<SkJpegEncoderImpl>(std::move(encoderMgr), srcYUVA);
328 }
329 return std::make_unique<SkJpegEncoderImpl>(std::move(encoderMgr), *src);
330}
const char * options
#define SkASSERT(cond)
Definition SkAssert.h:116
static sk_sp< SkData > icc_from_color_space(const SkColorSpace *cs, const skcms_ICCProfile *profile, const char *profile_description)
static bool SkPixmapIsValid(const SkPixmap &src)
static constexpr uint8_t kICCSig[]
static constexpr uint32_t kICCMarker
static constexpr uint32_t kXMPMarker
static constexpr uint8_t kXMPStandardSig[]
static constexpr uint32_t kICCMarkerHeaderSize
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition SkData.cpp:116
static std::unique_ptr< SkJpegEncoderMgr > Make(SkWStream *stream)
SkYUVAPixmapInfo pixmapsInfo() const
bool isValid() const
struct MyStruct s
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switches.h:41

◆ yuva_copy_row()

static void yuva_copy_row ( const SkYUVAPixmaps src,
int  row,
uint8_t *  dst 
)
static

Definition at line 183 of file SkJpegEncoderImpl.cpp.

183 {
184 int width = src->plane(0).width();
185 switch (src->yuvaInfo().planeConfig()) {
187 auto [ssWidthU, ssHeightU] = src->yuvaInfo().planeSubsamplingFactors(1);
188 auto [ssWidthV, ssHeightV] = src->yuvaInfo().planeSubsamplingFactors(2);
189 const uint8_t* srcY = reinterpret_cast<const uint8_t*>(src->plane(0).addr(0, row));
190 const uint8_t* srcU =
191 reinterpret_cast<const uint8_t*>(src->plane(1).addr(0, row / ssHeightU));
192 const uint8_t* srcV =
193 reinterpret_cast<const uint8_t*>(src->plane(2).addr(0, row / ssHeightV));
194 for (int col = 0; col < width; ++col) {
195 dst[3 * col + 0] = srcY[col];
196 dst[3 * col + 1] = srcU[col / ssWidthU];
197 dst[3 * col + 2] = srcV[col / ssWidthV];
198 }
199 break;
200 }
202 auto [ssWidthUV, ssHeightUV] = src->yuvaInfo().planeSubsamplingFactors(1);
203 const uint8_t* srcY = reinterpret_cast<const uint8_t*>(src->plane(0).addr(0, row));
204 const uint8_t* srcUV =
205 reinterpret_cast<const uint8_t*>(src->plane(1).addr(0, row / ssHeightUV));
206 for (int col = 0; col < width; ++col) {
207 dst[3 * col + 0] = srcY[col];
208 dst[3 * col + 1] = srcUV[2 * (col / ssWidthUV) + 0];
209 dst[3 * col + 2] = srcUV[2 * (col / ssWidthUV) + 1];
210 }
211 break;
212 }
213 default:
214 break;
215 }
216}
@ kY_U_V
Plane 0: Y, Plane 1: U, Plane 2: V.
@ kY_UV
Plane 0: Y, Plane 1: UV.
dst
Definition cp.py:12
int32_t width