Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Member Functions | List of all members
SkPngEncoderMgr Class Referencefinal
Inheritance diagram for SkPngEncoderMgr:
SkNoncopyable

Public Member Functions

bool setHeader (const SkImageInfo &srcInfo, const SkPngEncoder::Options &options)
 
bool setColorSpace (const SkImageInfo &info, const SkPngEncoder::Options &options)
 
bool writeInfo (const SkImageInfo &srcInfo)
 
void chooseProc (const SkImageInfo &srcInfo)
 
png_structp pngPtr ()
 
png_infop infoPtr ()
 
int pngBytesPerPixel () const
 
transform_scanline_proc proc () const
 
 ~SkPngEncoderMgr ()
 

Static Public Member Functions

static std::unique_ptr< SkPngEncoderMgrMake (SkWStream *stream)
 

Detailed Description

Definition at line 72 of file SkPngEncoderImpl.cpp.

Constructor & Destructor Documentation

◆ ~SkPngEncoderMgr()

SkPngEncoderMgr::~SkPngEncoderMgr ( )
inline

Definition at line 90 of file SkPngEncoderImpl.cpp.

90{ png_destroy_write_struct(&fPngPtr, &fInfoPtr); }

Member Function Documentation

◆ chooseProc()

void SkPngEncoderMgr::chooseProc ( const SkImageInfo srcInfo)

Definition at line 445 of file SkPngEncoderImpl.cpp.

445{ fProc = choose_proc(srcInfo); }
static transform_scanline_proc choose_proc(const SkImageInfo &info)

◆ infoPtr()

png_infop SkPngEncoderMgr::infoPtr ( )
inline

Definition at line 86 of file SkPngEncoderImpl.cpp.

86{ return fInfoPtr; }

◆ Make()

std::unique_ptr< SkPngEncoderMgr > SkPngEncoderMgr::Make ( SkWStream stream)
static

Definition at line 101 of file SkPngEncoderImpl.cpp.

101 {
102 png_structp pngPtr =
103 png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, sk_error_fn, nullptr);
104 if (!pngPtr) {
105 return nullptr;
106 }
107
108 png_infop infoPtr = png_create_info_struct(pngPtr);
109 if (!infoPtr) {
110 png_destroy_write_struct(&pngPtr, nullptr);
111 return nullptr;
112 }
113
114 png_set_write_fn(pngPtr, (void*)stream, sk_write_fn, nullptr);
115 return std::unique_ptr<SkPngEncoderMgr>(new SkPngEncoderMgr(pngPtr, infoPtr));
116}
static void sk_error_fn(png_structp png_ptr, png_const_charp msg)
static void sk_write_fn(png_structp png_ptr, png_bytep data, png_size_t len)
png_structp pngPtr()

◆ pngBytesPerPixel()

int SkPngEncoderMgr::pngBytesPerPixel ( ) const
inline

Definition at line 87 of file SkPngEncoderImpl.cpp.

87{ return fPngBytesPerPixel; }

◆ pngPtr()

png_structp SkPngEncoderMgr::pngPtr ( )
inline

Definition at line 85 of file SkPngEncoderImpl.cpp.

85{ return fPngPtr; }

◆ proc()

transform_scanline_proc SkPngEncoderMgr::proc ( ) const
inline

Definition at line 88 of file SkPngEncoderImpl.cpp.

88{ return fProc; }

◆ setColorSpace()

bool SkPngEncoderMgr::setColorSpace ( const SkImageInfo info,
const SkPngEncoder::Options options 
)

Definition at line 415 of file SkPngEncoderImpl.cpp.

415 {
416 if (setjmp(png_jmpbuf(fPngPtr))) {
417 return false;
418 }
419
420 if (info.colorSpace() && info.colorSpace()->isSRGB()) {
421 png_set_sRGB(fPngPtr, fInfoPtr, PNG_sRGB_INTENT_PERCEPTUAL);
422 } else {
423 set_icc(fPngPtr, fInfoPtr, info, options.fICCProfile, options.fICCProfileDescription);
424 }
425
426 return true;
427}
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
static void set_icc(png_structp png_ptr, png_infop info_ptr, const SkImageInfo &info, const skcms_ICCProfile *profile, const char *profile_description)

◆ setHeader()

bool SkPngEncoderMgr::setHeader ( const SkImageInfo srcInfo,
const SkPngEncoder::Options options 
)

Definition at line 118 of file SkPngEncoderImpl.cpp.

118 {
119 if (setjmp(png_jmpbuf(fPngPtr))) {
120 return false;
121 }
122
123 int pngColorType;
124 png_color_8 sigBit;
125 int bitDepth = 8;
126 switch (srcInfo.colorType()) {
130 sigBit.red = 16;
131 sigBit.green = 16;
132 sigBit.blue = 16;
133 sigBit.alpha = 16;
134 bitDepth = 16;
135 pngColorType = srcInfo.isOpaque() ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
136 fPngBytesPerPixel = 8;
137 break;
139 sigBit.gray = 8;
140 pngColorType = PNG_COLOR_TYPE_GRAY;
141 fPngBytesPerPixel = 1;
142 SkASSERT(srcInfo.isOpaque());
143 break;
146 sigBit.red = 8;
147 sigBit.green = 8;
148 sigBit.blue = 8;
149 sigBit.alpha = 8;
150 pngColorType = srcInfo.isOpaque() ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
151 fPngBytesPerPixel = srcInfo.isOpaque() ? 3 : 4;
152 break;
154 sigBit.red = 8;
155 sigBit.green = 8;
156 sigBit.blue = 8;
157 pngColorType = PNG_COLOR_TYPE_RGB;
158 fPngBytesPerPixel = 3;
159 SkASSERT(srcInfo.isOpaque());
160 break;
162 if (kUnpremul_SkAlphaType == srcInfo.alphaType()) {
163 return false;
164 }
165
166 sigBit.red = 4;
167 sigBit.green = 4;
168 sigBit.blue = 4;
169 sigBit.alpha = 4;
170 pngColorType = srcInfo.isOpaque() ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
171 fPngBytesPerPixel = srcInfo.isOpaque() ? 3 : 4;
172 break;
174 sigBit.red = 5;
175 sigBit.green = 6;
176 sigBit.blue = 5;
177 pngColorType = PNG_COLOR_TYPE_RGB;
178 fPngBytesPerPixel = 3;
179 SkASSERT(srcInfo.isOpaque());
180 break;
181 case kAlpha_8_SkColorType: // store as gray+alpha, but ignore gray
183 sigBit.alpha = 8;
184 pngColorType = PNG_COLOR_TYPE_GRAY_ALPHA;
185 fPngBytesPerPixel = 2;
186 break;
188 bitDepth = 16;
189 sigBit.red = 10;
190 sigBit.green = 10;
191 sigBit.blue = 10;
192 sigBit.alpha = 2;
193 pngColorType = srcInfo.isOpaque() ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
194 fPngBytesPerPixel = 8;
195 break;
198 bitDepth = 16;
199 sigBit.red = 10;
200 sigBit.green = 10;
201 sigBit.blue = 10;
202 pngColorType = PNG_COLOR_TYPE_RGB;
203 fPngBytesPerPixel = 6;
204 break;
206 bitDepth = 16;
207 sigBit.red = 10;
208 sigBit.green = 10;
209 sigBit.blue = 10;
210 sigBit.alpha = 10;
211 pngColorType = srcInfo.isOpaque() ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA;
212 fPngBytesPerPixel = 8;
213 break;
214 default:
215 return false;
216 }
217
218 png_set_IHDR(fPngPtr,
219 fInfoPtr,
220 srcInfo.width(),
221 srcInfo.height(),
222 bitDepth,
223 pngColorType,
224 PNG_INTERLACE_NONE,
225 PNG_COMPRESSION_TYPE_BASE,
226 PNG_FILTER_TYPE_BASE);
227 png_set_sBIT(fPngPtr, fInfoPtr, &sigBit);
228
229 int filters = (int)options.fFilterFlags & (int)SkPngEncoder::FilterFlag::kAll;
230 SkASSERT(filters == (int)options.fFilterFlags);
231 png_set_filter(fPngPtr, PNG_FILTER_TYPE_BASE, filters);
232
233 int zlibLevel = std::min(std::max(0, options.fZLibLevel), 9);
234 SkASSERT(zlibLevel == options.fZLibLevel);
235 png_set_compression_level(fPngPtr, zlibLevel);
236
237 // Set comments in tEXt chunk
238 const sk_sp<SkDataTable>& comments = options.fComments;
239 if (comments != nullptr) {
240 std::vector<png_text> png_texts(comments->count());
241 std::vector<SkString> clippedKeys;
242 for (int i = 0; i < comments->count() / 2; ++i) {
243 const char* keyword;
244 const char* originalKeyword = comments->atStr(2 * i);
245 const char* text = comments->atStr(2 * i + 1);
246 if (strlen(originalKeyword) <= PNG_KEYWORD_MAX_LENGTH) {
247 keyword = originalKeyword;
248 } else {
249 SkDEBUGFAILF("PNG tEXt keyword should be no longer than %d.",
250 PNG_KEYWORD_MAX_LENGTH);
251 clippedKeys.emplace_back(originalKeyword, PNG_KEYWORD_MAX_LENGTH);
252 keyword = clippedKeys.back().c_str();
253 }
254 // It seems safe to convert png_const_charp to png_charp for key/text,
255 // and we don't have to provide text_length and other fields as we're providing
256 // 0-terminated c_str with PNG_TEXT_COMPRESSION_NONE (no compression, no itxt).
257 png_texts[i].compression = PNG_TEXT_COMPRESSION_NONE;
258 png_texts[i].key = const_cast<png_charp>(keyword);
259 png_texts[i].text = const_cast<png_charp>(text);
260 }
261 png_set_text(fPngPtr, fInfoPtr, png_texts.data(), png_texts.size());
262 }
263
264 return true;
265}
kUnpremul_SkAlphaType
#define SkDEBUGFAILF(fmt,...)
Definition: SkAssert.h:119
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition: SkColorType.h:23
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition: SkColorType.h:38
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition: SkColorType.h:21
@ kRGB_101010x_SkColorType
pixel with 10 bits each for red, green, blue; in 32-bit word
Definition: SkColorType.h:29
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition: SkColorType.h:35
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition: SkColorType.h:22
@ kBGRA_10101010_XR_SkColorType
pixel with 10 bits each for blue, green, red, alpha; in 64-bit word, extended range
Definition: SkColorType.h:32
@ 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
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
Definition: SkColorType.h:40
@ kRGBA_1010102_SkColorType
10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
Definition: SkColorType.h:27
@ kBGR_101010x_XR_SkColorType
pixel with 10 bits each for blue, green, red; in 32-bit word, extended range
Definition: SkColorType.h:31
@ kRGBA_F16Norm_SkColorType
pixel with half floats in [0,1] for red, green, blue, alpha;
Definition: SkColorType.h:36
static constexpr int kGraySigBit_GrayAlphaIsJustAlpha
Definition: SkPngPriv.h:17
int count() const
Definition: SkDataTable.h:33
const char * atStr(int index) const
Definition: SkDataTable.h:59
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
std::u16string text
bool isOpaque() const
Definition: SkImageInfo.h:415
int width() const
Definition: SkImageInfo.h:365
SkAlphaType alphaType() const
Definition: SkImageInfo.h:375
SkColorType colorType() const
Definition: SkImageInfo.h:373
int height() const
Definition: SkImageInfo.h:371

◆ writeInfo()

bool SkPngEncoderMgr::writeInfo ( const SkImageInfo srcInfo)

Definition at line 429 of file SkPngEncoderImpl.cpp.

429 {
430 if (setjmp(png_jmpbuf(fPngPtr))) {
431 return false;
432 }
433
434 png_write_info(fPngPtr, fInfoPtr);
435 if (kRGBA_F16_SkColorType == srcInfo.colorType() &&
436 kOpaque_SkAlphaType == srcInfo.alphaType()) {
437 // For kOpaque, kRGBA_F16, we will keep the row as RGBA and tell libpng
438 // to skip the alpha channel.
439 png_set_filler(fPngPtr, 0, PNG_FILLER_AFTER);
440 }
441
442 return true;
443}
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28

The documentation for this class was generated from the following file: