epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 8 | #include "SkImageEncoderPriv.h" |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 9 | |
| 10 | #ifdef SK_HAS_PNG_LIBRARY |
| 11 | |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 12 | #include "SkColorTable.h" |
Matt Sarett | 5df93de | 2017-03-22 21:52:47 +0000 | [diff] [blame] | 13 | #include "SkImageEncoderFns.h" |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 14 | #include "SkImageInfoPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkStream.h" |
Matt Sarett | 74de1f6 | 2017-03-21 13:03:26 -0400 | [diff] [blame] | 16 | #include "SkString.h" |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 17 | #include "SkPngEncoder.h" |
Mike Reed | d6cb11e | 2017-11-30 15:33:04 -0500 | [diff] [blame] | 18 | #include "SkPngPriv.h" |
djsollen | b2a6fe7 | 2015-04-03 12:35:27 -0700 | [diff] [blame] | 19 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "png.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 22 | static_assert(PNG_FILTER_NONE == (int)SkPngEncoder::FilterFlag::kNone, "Skia libpng filter err."); |
| 23 | static_assert(PNG_FILTER_SUB == (int)SkPngEncoder::FilterFlag::kSub, "Skia libpng filter err."); |
| 24 | static_assert(PNG_FILTER_UP == (int)SkPngEncoder::FilterFlag::kUp, "Skia libpng filter err."); |
| 25 | static_assert(PNG_FILTER_AVG == (int)SkPngEncoder::FilterFlag::kAvg, "Skia libpng filter err."); |
| 26 | static_assert(PNG_FILTER_PAETH == (int)SkPngEncoder::FilterFlag::kPaeth, "Skia libpng filter err."); |
| 27 | static_assert(PNG_ALL_FILTERS == (int)SkPngEncoder::FilterFlag::kAll, "Skia libpng filter err."); |
| 28 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 29 | static constexpr bool kSuppressPngEncodeWarnings = true; |
commit-bot@chromium.org | a936e37 | 2013-03-14 14:42:18 +0000 | [diff] [blame] | 30 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | static void sk_error_fn(png_structp png_ptr, png_const_charp msg) { |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 32 | if (!kSuppressPngEncodeWarnings) { |
| 33 | SkDebugf("libpng encode error: %s\n", msg); |
scroggo | 04a0d3f | 2015-12-10 08:54:36 -0800 | [diff] [blame] | 34 | } |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 35 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | longjmp(png_jmpbuf(png_ptr), 1); |
| 37 | } |
| 38 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | static void sk_write_fn(png_structp png_ptr, png_bytep data, png_size_t len) { |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 40 | SkWStream* stream = (SkWStream*)png_get_io_ptr(png_ptr); |
| 41 | if (!stream->write(data, len)) { |
| 42 | png_error(png_ptr, "sk_write_fn cannot write to stream"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 46 | class SkPngEncoderMgr final : SkNoncopyable { |
| 47 | public: |
| 48 | |
| 49 | /* |
| 50 | * Create the decode manager |
| 51 | * Does not take ownership of stream |
| 52 | */ |
| 53 | static std::unique_ptr<SkPngEncoderMgr> Make(SkWStream* stream); |
| 54 | |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 55 | bool setHeader(const SkImageInfo& srcInfo, const SkPngEncoder::Options& options); |
Matt Sarett | 1950e0a | 2017-06-12 16:17:30 -0400 | [diff] [blame] | 56 | bool setColorSpace(const SkImageInfo& info); |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 57 | bool writeInfo(const SkImageInfo& srcInfo); |
| 58 | void chooseProc(const SkImageInfo& srcInfo, SkTransferFunctionBehavior unpremulBehavior); |
| 59 | |
| 60 | png_structp pngPtr() { return fPngPtr; } |
| 61 | png_infop infoPtr() { return fInfoPtr; } |
| 62 | int pngBytesPerPixel() const { return fPngBytesPerPixel; } |
| 63 | transform_scanline_proc proc() const { return fProc; } |
| 64 | |
| 65 | ~SkPngEncoderMgr() { |
| 66 | png_destroy_write_struct(&fPngPtr, &fInfoPtr); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | |
| 71 | SkPngEncoderMgr(png_structp pngPtr, png_infop infoPtr) |
| 72 | : fPngPtr(pngPtr) |
| 73 | , fInfoPtr(infoPtr) |
| 74 | {} |
| 75 | |
| 76 | png_structp fPngPtr; |
| 77 | png_infop fInfoPtr; |
| 78 | int fPngBytesPerPixel; |
| 79 | transform_scanline_proc fProc; |
| 80 | }; |
| 81 | |
| 82 | std::unique_ptr<SkPngEncoderMgr> SkPngEncoderMgr::Make(SkWStream* stream) { |
| 83 | png_structp pngPtr = |
| 84 | png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, sk_error_fn, nullptr); |
| 85 | if (!pngPtr) { |
| 86 | return nullptr; |
| 87 | } |
| 88 | |
| 89 | png_infop infoPtr = png_create_info_struct(pngPtr); |
| 90 | if (!infoPtr) { |
| 91 | png_destroy_write_struct(&pngPtr, nullptr); |
| 92 | return nullptr; |
| 93 | } |
| 94 | |
| 95 | png_set_write_fn(pngPtr, (void*)stream, sk_write_fn, nullptr); |
| 96 | return std::unique_ptr<SkPngEncoderMgr>(new SkPngEncoderMgr(pngPtr, infoPtr)); |
| 97 | } |
| 98 | |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 99 | bool SkPngEncoderMgr::setHeader(const SkImageInfo& srcInfo, const SkPngEncoder::Options& options) { |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 100 | if (setjmp(png_jmpbuf(fPngPtr))) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | int pngColorType; |
| 105 | png_color_8 sigBit; |
| 106 | int bitDepth = 8; |
| 107 | switch (srcInfo.colorType()) { |
| 108 | case kRGBA_F16_SkColorType: |
Mike Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 109 | case kRGBA_F32_SkColorType: |
Mike Klein | ce4cf72 | 2018-05-10 11:29:15 -0400 | [diff] [blame] | 110 | SkASSERT(srcInfo.colorSpace()); |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 111 | sigBit.red = 16; |
| 112 | sigBit.green = 16; |
| 113 | sigBit.blue = 16; |
| 114 | sigBit.alpha = 16; |
| 115 | bitDepth = 16; |
| 116 | pngColorType = srcInfo.isOpaque() ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA; |
| 117 | fPngBytesPerPixel = 8; |
| 118 | break; |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 119 | case kGray_8_SkColorType: |
| 120 | sigBit.gray = 8; |
| 121 | pngColorType = PNG_COLOR_TYPE_GRAY; |
| 122 | fPngBytesPerPixel = 1; |
| 123 | SkASSERT(srcInfo.isOpaque()); |
| 124 | break; |
| 125 | case kRGBA_8888_SkColorType: |
| 126 | case kBGRA_8888_SkColorType: |
| 127 | sigBit.red = 8; |
| 128 | sigBit.green = 8; |
| 129 | sigBit.blue = 8; |
| 130 | sigBit.alpha = 8; |
| 131 | pngColorType = srcInfo.isOpaque() ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA; |
| 132 | fPngBytesPerPixel = srcInfo.isOpaque() ? 3 : 4; |
| 133 | break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 134 | case kRGB_888x_SkColorType: |
| 135 | sigBit.red = 8; |
| 136 | sigBit.green = 8; |
| 137 | sigBit.blue = 8; |
| 138 | pngColorType = PNG_COLOR_TYPE_RGB; |
| 139 | fPngBytesPerPixel = 3; |
| 140 | SkASSERT(srcInfo.isOpaque()); |
| 141 | break; |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 142 | case kARGB_4444_SkColorType: |
| 143 | if (kUnpremul_SkAlphaType == srcInfo.alphaType()) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | sigBit.red = 4; |
| 148 | sigBit.green = 4; |
| 149 | sigBit.blue = 4; |
| 150 | sigBit.alpha = 4; |
| 151 | pngColorType = srcInfo.isOpaque() ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA; |
| 152 | fPngBytesPerPixel = srcInfo.isOpaque() ? 3 : 4; |
| 153 | break; |
| 154 | case kRGB_565_SkColorType: |
| 155 | sigBit.red = 5; |
| 156 | sigBit.green = 6; |
| 157 | sigBit.blue = 5; |
| 158 | pngColorType = PNG_COLOR_TYPE_RGB; |
| 159 | fPngBytesPerPixel = 3; |
| 160 | SkASSERT(srcInfo.isOpaque()); |
| 161 | break; |
Mike Reed | d6cb11e | 2017-11-30 15:33:04 -0500 | [diff] [blame] | 162 | case kAlpha_8_SkColorType: // store as gray+alpha, but ignore gray |
| 163 | sigBit.gray = kGraySigBit_GrayAlphaIsJustAlpha; |
| 164 | sigBit.alpha = 8; |
| 165 | pngColorType = PNG_COLOR_TYPE_GRAY_ALPHA; |
| 166 | fPngBytesPerPixel = 2; |
| 167 | break; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 168 | case kRGBA_1010102_SkColorType: |
| 169 | bitDepth = 16; |
| 170 | sigBit.red = 10; |
| 171 | sigBit.green = 10; |
| 172 | sigBit.blue = 10; |
| 173 | sigBit.alpha = 2; |
| 174 | pngColorType = srcInfo.isOpaque() ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA; |
| 175 | fPngBytesPerPixel = 8; |
| 176 | break; |
| 177 | case kRGB_101010x_SkColorType: |
| 178 | bitDepth = 16; |
| 179 | sigBit.red = 10; |
| 180 | sigBit.green = 10; |
| 181 | sigBit.blue = 10; |
| 182 | pngColorType = PNG_COLOR_TYPE_RGB; |
| 183 | fPngBytesPerPixel = 6; |
| 184 | break; |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 185 | default: |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | png_set_IHDR(fPngPtr, fInfoPtr, srcInfo.width(), srcInfo.height(), |
| 190 | bitDepth, pngColorType, |
| 191 | PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, |
| 192 | PNG_FILTER_TYPE_BASE); |
| 193 | png_set_sBIT(fPngPtr, fInfoPtr, &sigBit); |
| 194 | |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 195 | int filters = (int)options.fFilterFlags & (int)SkPngEncoder::FilterFlag::kAll; |
| 196 | SkASSERT(filters == (int)options.fFilterFlags); |
| 197 | png_set_filter(fPngPtr, PNG_FILTER_TYPE_BASE, filters); |
| 198 | |
| 199 | int zlibLevel = SkTMin(SkTMax(0, options.fZLibLevel), 9); |
| 200 | SkASSERT(zlibLevel == options.fZLibLevel); |
| 201 | png_set_compression_level(fPngPtr, zlibLevel); |
Yuqian Li | d0dbee6 | 2017-06-09 11:35:58 -0400 | [diff] [blame] | 202 | |
| 203 | // Set comments in tEXt chunk |
| 204 | const sk_sp<SkDataTable>& comments = options.fComments; |
| 205 | if (comments != nullptr) { |
| 206 | std::vector<png_text> png_texts(comments->count()); |
| 207 | std::vector<SkString> clippedKeys; |
| 208 | for (int i = 0; i < comments->count() / 2; ++i) { |
| 209 | const char* keyword; |
| 210 | const char* originalKeyword = comments->atStr(2 * i); |
| 211 | const char* text = comments->atStr(2 * i + 1); |
| 212 | if (strlen(originalKeyword) <= PNG_KEYWORD_MAX_LENGTH) { |
| 213 | keyword = originalKeyword; |
| 214 | } else { |
| 215 | SkDEBUGFAILF("PNG tEXt keyword should be no longer than %d.", |
| 216 | PNG_KEYWORD_MAX_LENGTH); |
| 217 | clippedKeys.emplace_back(originalKeyword, PNG_KEYWORD_MAX_LENGTH); |
| 218 | keyword = clippedKeys.back().c_str(); |
| 219 | } |
| 220 | // It seems safe to convert png_const_charp to png_charp for key/text, |
| 221 | // and we don't have to provide text_length and other fields as we're providing |
| 222 | // 0-terminated c_str with PNG_TEXT_COMPRESSION_NONE (no compression, no itxt). |
| 223 | png_texts[i].compression = PNG_TEXT_COMPRESSION_NONE; |
| 224 | png_texts[i].key = (png_charp)keyword; |
| 225 | png_texts[i].text = (png_charp)text; |
| 226 | } |
| 227 | png_set_text(fPngPtr, fInfoPtr, png_texts.data(), png_texts.size()); |
| 228 | } |
| 229 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 230 | return true; |
Matt Sarett | 687b656 | 2017-03-21 10:06:45 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Matt Sarett | 7abfb5e | 2017-04-05 17:36:04 -0400 | [diff] [blame] | 233 | static transform_scanline_proc choose_proc(const SkImageInfo& info, |
| 234 | SkTransferFunctionBehavior unpremulBehavior) { |
| 235 | const bool isSRGBTransferFn = |
| 236 | (SkTransferFunctionBehavior::kRespect == unpremulBehavior) && info.gammaCloseToSRGB(); |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 237 | switch (info.colorType()) { |
| 238 | case kRGBA_8888_SkColorType: |
| 239 | switch (info.alphaType()) { |
| 240 | case kOpaque_SkAlphaType: |
| 241 | return transform_scanline_RGBX; |
| 242 | case kUnpremul_SkAlphaType: |
| 243 | return transform_scanline_memcpy; |
| 244 | case kPremul_SkAlphaType: |
Matt Sarett | 7abfb5e | 2017-04-05 17:36:04 -0400 | [diff] [blame] | 245 | return isSRGBTransferFn ? transform_scanline_srgbA : |
| 246 | transform_scanline_rgbA; |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 247 | default: |
| 248 | SkASSERT(false); |
| 249 | return nullptr; |
| 250 | } |
| 251 | case kBGRA_8888_SkColorType: |
| 252 | switch (info.alphaType()) { |
| 253 | case kOpaque_SkAlphaType: |
| 254 | return transform_scanline_BGRX; |
| 255 | case kUnpremul_SkAlphaType: |
| 256 | return transform_scanline_BGRA; |
| 257 | case kPremul_SkAlphaType: |
Matt Sarett | 7abfb5e | 2017-04-05 17:36:04 -0400 | [diff] [blame] | 258 | return isSRGBTransferFn ? transform_scanline_sbgrA : |
| 259 | transform_scanline_bgrA; |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 260 | default: |
| 261 | SkASSERT(false); |
| 262 | return nullptr; |
| 263 | } |
| 264 | case kRGB_565_SkColorType: |
| 265 | return transform_scanline_565; |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 266 | case kRGB_888x_SkColorType: |
| 267 | return transform_scanline_888x; |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 268 | case kARGB_4444_SkColorType: |
| 269 | switch (info.alphaType()) { |
| 270 | case kOpaque_SkAlphaType: |
| 271 | return transform_scanline_444; |
| 272 | case kPremul_SkAlphaType: |
| 273 | // 4444 is assumed to be legacy premul. |
| 274 | return transform_scanline_4444; |
| 275 | default: |
| 276 | SkASSERT(false); |
| 277 | return nullptr; |
| 278 | } |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 279 | case kGray_8_SkColorType: |
| 280 | return transform_scanline_memcpy; |
Matt Sarett | 1da27ef | 2017-01-19 17:14:07 -0500 | [diff] [blame] | 281 | case kRGBA_F16_SkColorType: |
| 282 | switch (info.alphaType()) { |
| 283 | case kOpaque_SkAlphaType: |
| 284 | case kUnpremul_SkAlphaType: |
| 285 | return transform_scanline_F16; |
| 286 | case kPremul_SkAlphaType: |
| 287 | return transform_scanline_F16_premul; |
| 288 | default: |
| 289 | SkASSERT(false); |
| 290 | return nullptr; |
| 291 | } |
Mike Klein | 3785471 | 2018-06-26 11:43:06 -0400 | [diff] [blame] | 292 | case kRGBA_F32_SkColorType: |
| 293 | switch (info.alphaType()) { |
| 294 | case kOpaque_SkAlphaType: |
| 295 | case kUnpremul_SkAlphaType: |
| 296 | return transform_scanline_F32; |
| 297 | case kPremul_SkAlphaType: |
| 298 | return transform_scanline_F32_premul; |
| 299 | default: |
| 300 | SkASSERT(false); |
| 301 | return nullptr; |
| 302 | } |
Mike Klein | ac568a9 | 2018-01-25 09:09:32 -0500 | [diff] [blame] | 303 | case kRGBA_1010102_SkColorType: |
| 304 | switch (info.alphaType()) { |
| 305 | case kOpaque_SkAlphaType: |
| 306 | case kUnpremul_SkAlphaType: |
| 307 | return transform_scanline_1010102; |
| 308 | case kPremul_SkAlphaType: |
| 309 | return transform_scanline_1010102_premul; |
| 310 | default: |
| 311 | SkASSERT(false); |
| 312 | return nullptr; |
| 313 | } |
| 314 | case kRGB_101010x_SkColorType: |
| 315 | return transform_scanline_101010x; |
Mike Reed | d6cb11e | 2017-11-30 15:33:04 -0500 | [diff] [blame] | 316 | case kAlpha_8_SkColorType: |
| 317 | return transform_scanline_A8_to_GrayAlpha; |
Matt Sarett | 84014f0 | 2017-01-10 11:28:54 -0500 | [diff] [blame] | 318 | default: |
| 319 | SkASSERT(false); |
| 320 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 321 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Matt Sarett | 1950e0a | 2017-06-12 16:17:30 -0400 | [diff] [blame] | 324 | static void set_icc(png_structp png_ptr, png_infop info_ptr, const SkImageInfo& info) { |
| 325 | sk_sp<SkData> icc = icc_from_color_space(info); |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 326 | if (!icc) { |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | #if PNG_LIBPNG_VER_MAJOR > 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 5) |
| 331 | const char* name = "Skia"; |
| 332 | png_const_bytep iccPtr = icc->bytes(); |
| 333 | #else |
| 334 | SkString str("Skia"); |
| 335 | char* name = str.writable_str(); |
| 336 | png_charp iccPtr = (png_charp) icc->writable_data(); |
| 337 | #endif |
| 338 | png_set_iCCP(png_ptr, info_ptr, name, 0, iccPtr, icc->size()); |
| 339 | } |
| 340 | |
Matt Sarett | 1950e0a | 2017-06-12 16:17:30 -0400 | [diff] [blame] | 341 | bool SkPngEncoderMgr::setColorSpace(const SkImageInfo& info) { |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 342 | if (setjmp(png_jmpbuf(fPngPtr))) { |
| 343 | return false; |
| 344 | } |
| 345 | |
Matt Sarett | 1950e0a | 2017-06-12 16:17:30 -0400 | [diff] [blame] | 346 | if (info.colorSpace() && info.colorSpace()->isSRGB()) { |
| 347 | png_set_sRGB(fPngPtr, fInfoPtr, PNG_sRGB_INTENT_PERCEPTUAL); |
| 348 | } else { |
| 349 | set_icc(fPngPtr, fInfoPtr, info); |
Matt Sarett | 0e032be | 2017-03-15 17:50:08 -0400 | [diff] [blame] | 350 | } |
| 351 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 352 | return true; |
| 353 | } |
| 354 | |
| 355 | bool SkPngEncoderMgr::writeInfo(const SkImageInfo& srcInfo) { |
| 356 | if (setjmp(png_jmpbuf(fPngPtr))) { |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | png_write_info(fPngPtr, fInfoPtr); |
| 361 | if (kRGBA_F16_SkColorType == srcInfo.colorType() && |
| 362 | kOpaque_SkAlphaType == srcInfo.alphaType()) |
| 363 | { |
Matt Sarett | 1da27ef | 2017-01-19 17:14:07 -0500 | [diff] [blame] | 364 | // For kOpaque, kRGBA_F16, we will keep the row as RGBA and tell libpng |
| 365 | // to skip the alpha channel. |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 366 | png_set_filler(fPngPtr, 0, PNG_FILLER_AFTER); |
Matt Sarett | 1da27ef | 2017-01-19 17:14:07 -0500 | [diff] [blame] | 367 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 368 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 369 | return true; |
| 370 | } |
| 371 | |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 372 | void SkPngEncoderMgr::chooseProc(const SkImageInfo& srcInfo, |
| 373 | SkTransferFunctionBehavior unpremulBehavior) { |
| 374 | fProc = choose_proc(srcInfo, unpremulBehavior); |
| 375 | } |
| 376 | |
Matt Sarett | 6a4dc66 | 2017-05-11 09:32:59 -0400 | [diff] [blame] | 377 | std::unique_ptr<SkEncoder> SkPngEncoder::Make(SkWStream* dst, const SkPixmap& src, |
| 378 | const Options& options) { |
Brian Osman | e1adc3a | 2018-06-04 09:21:17 -0400 | [diff] [blame] | 379 | if (!SkPixmapIsValid(src)) { |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 380 | return nullptr; |
| 381 | } |
| 382 | |
| 383 | std::unique_ptr<SkPngEncoderMgr> encoderMgr = SkPngEncoderMgr::Make(dst); |
| 384 | if (!encoderMgr) { |
| 385 | return nullptr; |
| 386 | } |
| 387 | |
Matt Sarett | be4c9b0 | 2017-05-08 12:11:44 -0400 | [diff] [blame] | 388 | if (!encoderMgr->setHeader(src.info(), options)) { |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 389 | return nullptr; |
| 390 | } |
| 391 | |
Matt Sarett | 1950e0a | 2017-06-12 16:17:30 -0400 | [diff] [blame] | 392 | if (!encoderMgr->setColorSpace(src.info())) { |
Matt Sarett | c367d03 | 2017-05-05 11:13:26 -0400 | [diff] [blame] | 393 | return nullptr; |
| 394 | } |
| 395 | |
| 396 | if (!encoderMgr->writeInfo(src.info())) { |
| 397 | return nullptr; |
| 398 | } |
| 399 | |
| 400 | encoderMgr->chooseProc(src.info(), options.fUnpremulBehavior); |
| 401 | |
| 402 | return std::unique_ptr<SkPngEncoder>(new SkPngEncoder(std::move(encoderMgr), src)); |
| 403 | } |
| 404 | |
| 405 | SkPngEncoder::SkPngEncoder(std::unique_ptr<SkPngEncoderMgr> encoderMgr, const SkPixmap& src) |
| 406 | : INHERITED(src, encoderMgr->pngBytesPerPixel() * src.width()) |
| 407 | , fEncoderMgr(std::move(encoderMgr)) |
| 408 | {} |
| 409 | |
| 410 | SkPngEncoder::~SkPngEncoder() {} |
| 411 | |
| 412 | bool SkPngEncoder::onEncodeRows(int numRows) { |
| 413 | if (setjmp(png_jmpbuf(fEncoderMgr->pngPtr()))) { |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | const void* srcRow = fSrc.addr(0, fCurrRow); |
| 418 | for (int y = 0; y < numRows; y++) { |
| 419 | fEncoderMgr->proc()((char*) fStorage.get(), (const char*) srcRow, fSrc.width(), |
| 420 | SkColorTypeBytesPerPixel(fSrc.colorType()), nullptr); |
| 421 | |
| 422 | png_bytep rowPtr = (png_bytep) fStorage.get(); |
| 423 | png_write_rows(fEncoderMgr->pngPtr(), &rowPtr, 1); |
| 424 | srcRow = SkTAddOffset<const void>(srcRow, fSrc.rowBytes()); |
| 425 | } |
| 426 | |
| 427 | fCurrRow += numRows; |
| 428 | if (fCurrRow == fSrc.height()) { |
| 429 | png_write_end(fEncoderMgr->pngPtr(), fEncoderMgr->infoPtr()); |
| 430 | } |
| 431 | |
| 432 | return true; |
| 433 | } |
| 434 | |
| 435 | bool SkPngEncoder::Encode(SkWStream* dst, const SkPixmap& src, const Options& options) { |
| 436 | auto encoder = SkPngEncoder::Make(dst, src, options); |
| 437 | return encoder.get() && encoder->encodeRows(src.height()); |
| 438 | } |
| 439 | |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 440 | #endif |