Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Tom Sepez | 73c9f3b | 2017-02-27 10:12:59 -0800 | [diff] [blame^] | 7 | #include "core/fxcodec/codec/ccodec_pngmodule.h" |
| 8 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
Dan Sinclair | 764ec51 | 2016-03-14 13:35:12 -0400 | [diff] [blame] | 11 | #include "core/fxcodec/codec/codec_int.h" |
dsinclair | 8a4e286 | 2016-09-29 13:43:30 -0700 | [diff] [blame] | 12 | #include "core/fxcodec/fx_codec.h" |
dsinclair | 74a34fc | 2016-09-29 16:41:42 -0700 | [diff] [blame] | 13 | #include "core/fxge/fx_dib.h" |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 14 | |
| 15 | extern "C" { |
| 16 | #undef FAR |
| 17 | #include "third_party/libpng16/png.h" |
| 18 | } |
| 19 | |
| 20 | static void _png_error_data(png_structp png_ptr, png_const_charp error_msg) { |
| 21 | if (png_get_error_ptr(png_ptr)) { |
| 22 | FXSYS_strncpy((char*)png_get_error_ptr(png_ptr), error_msg, |
| 23 | PNG_ERROR_SIZE - 1); |
| 24 | } |
| 25 | longjmp(png_jmpbuf(png_ptr), 1); |
| 26 | } |
| 27 | static void _png_warning_data(png_structp png_ptr, png_const_charp error_msg) {} |
| 28 | static void _png_load_bmp_attribute(png_structp png_ptr, |
| 29 | png_infop info_ptr, |
| 30 | CFX_DIBAttribute* pAttribute) { |
| 31 | if (pAttribute) { |
| 32 | #if defined(PNG_pHYs_SUPPORTED) |
| 33 | pAttribute->m_nXDPI = png_get_x_pixels_per_meter(png_ptr, info_ptr); |
| 34 | pAttribute->m_nYDPI = png_get_y_pixels_per_meter(png_ptr, info_ptr); |
| 35 | png_uint_32 res_x, res_y; |
| 36 | int unit_type; |
| 37 | png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y, &unit_type); |
| 38 | switch (unit_type) { |
| 39 | case PNG_RESOLUTION_METER: |
| 40 | pAttribute->m_wDPIUnit = FXCODEC_RESUNIT_METER; |
| 41 | break; |
| 42 | default: |
| 43 | pAttribute->m_wDPIUnit = FXCODEC_RESUNIT_NONE; |
| 44 | } |
| 45 | #endif |
| 46 | #if defined(PNG_iCCP_SUPPORTED) |
| 47 | png_charp icc_name; |
| 48 | png_bytep icc_profile; |
| 49 | png_uint_32 icc_proflen; |
| 50 | int compress_type; |
| 51 | png_get_iCCP(png_ptr, info_ptr, &icc_name, &compress_type, &icc_profile, |
| 52 | &icc_proflen); |
| 53 | #endif |
| 54 | int bTime = 0; |
| 55 | #if defined(PNG_tIME_SUPPORTED) |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 56 | png_timep t = nullptr; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 57 | png_get_tIME(png_ptr, info_ptr, &t); |
| 58 | if (t) { |
| 59 | FXSYS_memset(pAttribute->m_strTime, 0, sizeof(pAttribute->m_strTime)); |
| 60 | FXSYS_snprintf((FX_CHAR*)pAttribute->m_strTime, |
weili | 12367cb | 2016-06-03 11:22:16 -0700 | [diff] [blame] | 61 | sizeof(pAttribute->m_strTime), "%4u:%2u:%2u %2u:%2u:%2u", |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 62 | t->year, t->month, t->day, t->hour, t->minute, t->second); |
| 63 | pAttribute->m_strTime[sizeof(pAttribute->m_strTime) - 1] = 0; |
| 64 | bTime = 1; |
| 65 | } |
| 66 | #endif |
| 67 | #if defined(PNG_TEXT_SUPPORTED) |
| 68 | int i; |
| 69 | FX_STRSIZE len; |
| 70 | const FX_CHAR* buf; |
| 71 | int num_text; |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 72 | png_textp text = nullptr; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 73 | png_get_text(png_ptr, info_ptr, &text, &num_text); |
| 74 | for (i = 0; i < num_text; i++) { |
| 75 | len = FXSYS_strlen(text[i].key); |
| 76 | buf = "Time"; |
| 77 | if (!FXSYS_memcmp(buf, text[i].key, std::min(len, FXSYS_strlen(buf)))) { |
| 78 | if (!bTime) { |
| 79 | FXSYS_memset(pAttribute->m_strTime, 0, sizeof(pAttribute->m_strTime)); |
| 80 | FXSYS_memcpy( |
| 81 | pAttribute->m_strTime, text[i].text, |
| 82 | std::min(sizeof(pAttribute->m_strTime) - 1, text[i].text_length)); |
| 83 | } |
| 84 | } else { |
| 85 | buf = "Author"; |
| 86 | if (!FXSYS_memcmp(buf, text[i].key, std::min(len, FXSYS_strlen(buf)))) { |
tsepez | f172290 | 2016-04-08 12:13:50 -0700 | [diff] [blame] | 87 | pAttribute->m_strAuthor = |
| 88 | CFX_ByteString(reinterpret_cast<uint8_t*>(text[i].text), |
| 89 | static_cast<FX_STRSIZE>(text[i].text_length)); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | } |
| 93 | #endif |
| 94 | } |
| 95 | } |
| 96 | struct FXPNG_Context { |
| 97 | png_structp png_ptr; |
| 98 | png_infop info_ptr; |
| 99 | void* parent_ptr; |
| 100 | void* child_ptr; |
| 101 | |
| 102 | void* (*m_AllocFunc)(unsigned int); |
| 103 | void (*m_FreeFunc)(void*); |
| 104 | }; |
| 105 | extern "C" { |
| 106 | static void* _png_alloc_func(unsigned int size) { |
| 107 | return FX_Alloc(char, size); |
| 108 | } |
| 109 | static void _png_free_func(void* p) { |
Lei Zhang | 5eca305 | 2016-02-22 20:32:21 -0800 | [diff] [blame] | 110 | FX_Free(p); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 111 | } |
| 112 | }; |
| 113 | static void _png_get_header_func(png_structp png_ptr, png_infop info_ptr) { |
| 114 | FXPNG_Context* p = (FXPNG_Context*)png_get_progressive_ptr(png_ptr); |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 115 | if (!p) |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 116 | return; |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 117 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 118 | CCodec_PngModule* pModule = (CCodec_PngModule*)p->parent_ptr; |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 119 | if (!pModule) |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 120 | return; |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 121 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 122 | png_uint_32 width = 0, height = 0; |
| 123 | int bpc = 0, color_type = 0, color_type1 = 0, pass = 0; |
| 124 | double gamma = 1.0; |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 125 | png_get_IHDR(png_ptr, info_ptr, &width, &height, &bpc, &color_type, nullptr, |
| 126 | nullptr, nullptr); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 127 | color_type1 = color_type; |
| 128 | if (bpc > 8) { |
| 129 | png_set_strip_16(png_ptr); |
| 130 | } else if (bpc < 8) { |
| 131 | png_set_expand_gray_1_2_4_to_8(png_ptr); |
| 132 | } |
| 133 | bpc = 8; |
| 134 | if (color_type == PNG_COLOR_TYPE_PALETTE) { |
| 135 | png_set_palette_to_rgb(png_ptr); |
| 136 | } |
| 137 | pass = png_set_interlace_handling(png_ptr); |
| 138 | if (!pModule->ReadHeaderCallback(p->child_ptr, width, height, bpc, pass, |
| 139 | &color_type, &gamma)) { |
| 140 | png_error(p->png_ptr, "Read Header Callback Error"); |
| 141 | } |
| 142 | int intent; |
| 143 | if (png_get_sRGB(png_ptr, info_ptr, &intent)) { |
| 144 | png_set_gamma(png_ptr, gamma, 0.45455); |
| 145 | } else { |
| 146 | double image_gamma; |
| 147 | if (png_get_gAMA(png_ptr, info_ptr, &image_gamma)) { |
| 148 | png_set_gamma(png_ptr, gamma, image_gamma); |
| 149 | } else { |
| 150 | png_set_gamma(png_ptr, gamma, 0.45455); |
| 151 | } |
| 152 | } |
| 153 | switch (color_type) { |
| 154 | case PNG_COLOR_TYPE_GRAY: |
| 155 | case PNG_COLOR_TYPE_GRAY_ALPHA: { |
| 156 | if (color_type1 & PNG_COLOR_MASK_COLOR) { |
| 157 | png_set_rgb_to_gray(png_ptr, 1, 0.299, 0.587); |
| 158 | } |
| 159 | } break; |
| 160 | case PNG_COLOR_TYPE_PALETTE: |
| 161 | if (color_type1 != PNG_COLOR_TYPE_PALETTE) { |
| 162 | png_error(p->png_ptr, "Not Support Output Palette Now"); |
| 163 | } |
| 164 | case PNG_COLOR_TYPE_RGB: |
| 165 | case PNG_COLOR_TYPE_RGB_ALPHA: |
| 166 | if (!(color_type1 & PNG_COLOR_MASK_COLOR)) { |
| 167 | png_set_gray_to_rgb(png_ptr); |
| 168 | } |
| 169 | png_set_bgr(png_ptr); |
| 170 | break; |
| 171 | } |
| 172 | if (!(color_type & PNG_COLOR_MASK_ALPHA)) { |
| 173 | png_set_strip_alpha(png_ptr); |
| 174 | } |
| 175 | if (color_type & PNG_COLOR_MASK_ALPHA && |
| 176 | !(color_type1 & PNG_COLOR_MASK_ALPHA)) { |
| 177 | png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); |
| 178 | } |
| 179 | png_read_update_info(png_ptr, info_ptr); |
| 180 | } |
| 181 | static void _png_get_end_func(png_structp png_ptr, png_infop info_ptr) {} |
| 182 | static void _png_get_row_func(png_structp png_ptr, |
| 183 | png_bytep new_row, |
| 184 | png_uint_32 row_num, |
| 185 | int pass) { |
| 186 | FXPNG_Context* p = (FXPNG_Context*)png_get_progressive_ptr(png_ptr); |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 187 | if (!p) |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 188 | return; |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 189 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 190 | CCodec_PngModule* pModule = (CCodec_PngModule*)p->parent_ptr; |
thestig | 4997b22 | 2016-06-07 10:46:22 -0700 | [diff] [blame] | 191 | uint8_t* src_buf = nullptr; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 192 | if (!pModule->AskScanlineBufCallback(p->child_ptr, row_num, src_buf)) { |
| 193 | png_error(png_ptr, "Ask Scanline buffer Callback Error"); |
| 194 | } |
Lei Zhang | 5eca305 | 2016-02-22 20:32:21 -0800 | [diff] [blame] | 195 | if (src_buf) { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 196 | png_progressive_combine_row(png_ptr, src_buf, new_row); |
| 197 | } |
| 198 | pModule->FillScanlineBufCompletedCallback(p->child_ptr, pass, row_num); |
| 199 | } |
dsinclair | d55e11e | 2016-04-12 11:21:22 -0700 | [diff] [blame] | 200 | |
| 201 | FXPNG_Context* CCodec_PngModule::Start(void* pModule) { |
tsepez | dd1bfe4 | 2017-01-16 06:07:54 -0800 | [diff] [blame] | 202 | FXPNG_Context* p = FX_Alloc(FXPNG_Context, 1); |
dsinclair | d55e11e | 2016-04-12 11:21:22 -0700 | [diff] [blame] | 203 | if (!p) |
| 204 | return nullptr; |
| 205 | |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 206 | p->m_AllocFunc = _png_alloc_func; |
| 207 | p->m_FreeFunc = _png_free_func; |
dsinclair | d55e11e | 2016-04-12 11:21:22 -0700 | [diff] [blame] | 208 | p->png_ptr = nullptr; |
| 209 | p->info_ptr = nullptr; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 210 | p->parent_ptr = (void*)this; |
| 211 | p->child_ptr = pModule; |
dsinclair | d55e11e | 2016-04-12 11:21:22 -0700 | [diff] [blame] | 212 | p->png_ptr = |
| 213 | png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); |
| 214 | if (!p->png_ptr) { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 215 | FX_Free(p); |
dsinclair | d55e11e | 2016-04-12 11:21:22 -0700 | [diff] [blame] | 216 | return nullptr; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 217 | } |
| 218 | p->info_ptr = png_create_info_struct(p->png_ptr); |
dsinclair | d55e11e | 2016-04-12 11:21:22 -0700 | [diff] [blame] | 219 | if (!p->info_ptr) { |
| 220 | png_destroy_read_struct(&(p->png_ptr), nullptr, nullptr); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 221 | FX_Free(p); |
dsinclair | d55e11e | 2016-04-12 11:21:22 -0700 | [diff] [blame] | 222 | return nullptr; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 223 | } |
| 224 | if (setjmp(png_jmpbuf(p->png_ptr))) { |
Lei Zhang | 5eca305 | 2016-02-22 20:32:21 -0800 | [diff] [blame] | 225 | if (p) { |
dsinclair | d55e11e | 2016-04-12 11:21:22 -0700 | [diff] [blame] | 226 | png_destroy_read_struct(&(p->png_ptr), &(p->info_ptr), nullptr); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 227 | FX_Free(p); |
| 228 | } |
dsinclair | d55e11e | 2016-04-12 11:21:22 -0700 | [diff] [blame] | 229 | return nullptr; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 230 | } |
| 231 | png_set_progressive_read_fn(p->png_ptr, p, _png_get_header_func, |
| 232 | _png_get_row_func, _png_get_end_func); |
| 233 | png_set_error_fn(p->png_ptr, m_szLastError, (png_error_ptr)_png_error_data, |
| 234 | (png_error_ptr)_png_warning_data); |
| 235 | return p; |
| 236 | } |
dsinclair | d55e11e | 2016-04-12 11:21:22 -0700 | [diff] [blame] | 237 | |
| 238 | void CCodec_PngModule::Finish(FXPNG_Context* ctx) { |
| 239 | if (ctx) { |
| 240 | png_destroy_read_struct(&(ctx->png_ptr), &(ctx->info_ptr), nullptr); |
| 241 | ctx->m_FreeFunc(ctx); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 242 | } |
| 243 | } |
dsinclair | d55e11e | 2016-04-12 11:21:22 -0700 | [diff] [blame] | 244 | |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 245 | bool CCodec_PngModule::Input(FXPNG_Context* ctx, |
| 246 | const uint8_t* src_buf, |
| 247 | uint32_t src_size, |
| 248 | CFX_DIBAttribute* pAttribute) { |
dsinclair | d55e11e | 2016-04-12 11:21:22 -0700 | [diff] [blame] | 249 | if (setjmp(png_jmpbuf(ctx->png_ptr))) { |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 250 | if (pAttribute && |
| 251 | 0 == FXSYS_strcmp(m_szLastError, "Read Header Callback Error")) { |
dsinclair | d55e11e | 2016-04-12 11:21:22 -0700 | [diff] [blame] | 252 | _png_load_bmp_attribute(ctx->png_ptr, ctx->info_ptr, pAttribute); |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 253 | } |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 254 | return false; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 255 | } |
dsinclair | d55e11e | 2016-04-12 11:21:22 -0700 | [diff] [blame] | 256 | png_process_data(ctx->png_ptr, ctx->info_ptr, (uint8_t*)src_buf, src_size); |
tsepez | 12f3e4a | 2016-11-02 15:17:29 -0700 | [diff] [blame] | 257 | return true; |
Lei Zhang | 9429368 | 2016-01-27 18:27:56 -0800 | [diff] [blame] | 258 | } |