Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include <setjmp.h> | 7 #include <setjmp.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 extern "C" { | 75 extern "C" { |
| 76 static void _error_do_nothing1(j_common_ptr cinfo, int) {} | 76 static void _error_do_nothing1(j_common_ptr cinfo, int) {} |
| 77 }; | 77 }; |
| 78 extern "C" { | 78 extern "C" { |
| 79 static void _error_do_nothing2(j_common_ptr cinfo, char*) {} | 79 static void _error_do_nothing2(j_common_ptr cinfo, char*) {} |
| 80 }; | 80 }; |
| 81 | 81 |
| 82 #define JPEG_MARKER_ICC (JPEG_APP0 + 2) | 82 #define JPEG_MARKER_ICC (JPEG_APP0 + 2) |
| 83 #define JPEG_MARKER_MAXSIZE 0xFFFF | 83 #define JPEG_MARKER_MAXSIZE 0xFFFF |
| 84 | 84 |
| 85 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_DESKTOP_ | |
| 86 extern "C" { | |
| 87 static void _dest_do_nothing(j_compress_ptr cinfo) {} | |
| 88 }; | |
| 89 extern "C" { | |
|
dsinclair
2017/01/05 17:13:43
Can drop this extern "C" and combine with the othe
rbpotter
2017/01/05 22:58:18
Done.
| |
| 90 static boolean _dest_empty(j_compress_ptr cinfo) { | |
| 91 return false; | |
| 92 } | |
| 93 }; | |
| 94 #endif | |
| 95 | |
| 85 #ifdef PDF_ENABLE_XFA | 96 #ifdef PDF_ENABLE_XFA |
| 86 static void JpegLoadAttribute(struct jpeg_decompress_struct* pInfo, | 97 static void JpegLoadAttribute(struct jpeg_decompress_struct* pInfo, |
| 87 CFX_DIBAttribute* pAttribute) { | 98 CFX_DIBAttribute* pAttribute) { |
| 88 if (!pAttribute) | 99 if (!pAttribute) |
| 89 return; | 100 return; |
| 90 | 101 |
| 91 pAttribute->m_nXDPI = pInfo->X_density; | 102 pAttribute->m_nXDPI = pInfo->X_density; |
| 92 pAttribute->m_nYDPI = pInfo->Y_density; | 103 pAttribute->m_nYDPI = pInfo->Y_density; |
| 93 pAttribute->m_wDPIUnit = pInfo->density_unit; | 104 pAttribute->m_wDPIUnit = pInfo->density_unit; |
| 94 } | 105 } |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 473 uint32_t CCodec_JpegModule::GetAvailInput(FXJPEG_Context* ctx, | 484 uint32_t CCodec_JpegModule::GetAvailInput(FXJPEG_Context* ctx, |
| 474 uint8_t** avail_buf_ptr) { | 485 uint8_t** avail_buf_ptr) { |
| 475 if (avail_buf_ptr) { | 486 if (avail_buf_ptr) { |
| 476 *avail_buf_ptr = nullptr; | 487 *avail_buf_ptr = nullptr; |
| 477 if (ctx->m_SrcMgr.bytes_in_buffer > 0) { | 488 if (ctx->m_SrcMgr.bytes_in_buffer > 0) { |
| 478 *avail_buf_ptr = (uint8_t*)ctx->m_SrcMgr.next_input_byte; | 489 *avail_buf_ptr = (uint8_t*)ctx->m_SrcMgr.next_input_byte; |
| 479 } | 490 } |
| 480 } | 491 } |
| 481 return (uint32_t)ctx->m_SrcMgr.bytes_in_buffer; | 492 return (uint32_t)ctx->m_SrcMgr.bytes_in_buffer; |
| 482 } | 493 } |
| 494 | |
| 495 #if _FX_OS_ == _FX_WIN32_DESKTOP_ || _FX_OS_ == _FX_WIN64_DESKTOP_ | |
| 496 #define JPEG_BLOCK_SIZE 1048576 | |
| 497 bool CCodec_JpegModule::JpegEncode(const CFX_DIBSource* pSource, | |
| 498 uint8_t*& dest_buf, | |
| 499 FX_STRSIZE& dest_size) { | |
| 500 struct jpeg_error_mgr jerr; | |
| 501 jerr.error_exit = _error_do_nothing; | |
| 502 jerr.emit_message = _error_do_nothing1; | |
| 503 jerr.output_message = _error_do_nothing; | |
| 504 jerr.format_message = _error_do_nothing2; | |
| 505 jerr.reset_error_mgr = _error_do_nothing; | |
| 506 | |
| 507 struct jpeg_compress_struct cinfo; | |
| 508 memset(&cinfo, 0, sizeof(cinfo)); | |
| 509 cinfo.err = &jerr; | |
| 510 jpeg_create_compress(&cinfo); | |
| 511 int Bpp = pSource->GetBPP() / 8; | |
| 512 uint32_t nComponents = Bpp >= 3 ? (pSource->IsCmykImage() ? 4 : 3) : 1; | |
| 513 uint32_t pitch = pSource->GetPitch(); | |
| 514 uint32_t width = pdfium::base::checked_cast<uint32_t>(pSource->GetWidth()); | |
| 515 uint32_t height = pdfium::base::checked_cast<uint32_t>(pSource->GetHeight()); | |
| 516 FX_SAFE_UINT32 safe_buf_len = width; | |
| 517 safe_buf_len *= height; | |
| 518 safe_buf_len *= nComponents; | |
| 519 safe_buf_len += 1024; | |
| 520 if (!safe_buf_len.IsValid()) | |
| 521 return false; | |
| 522 | |
| 523 uint32_t dest_buf_length = safe_buf_len.ValueOrDie(); | |
| 524 dest_buf = FX_TryAlloc(uint8_t, dest_buf_length); | |
| 525 const int MIN_TRY_BUF_LEN = 1024; | |
| 526 while (!dest_buf && dest_buf_length > MIN_TRY_BUF_LEN) { | |
| 527 dest_buf_length >>= 1; | |
| 528 dest_buf = FX_TryAlloc(uint8_t, dest_buf_length); | |
| 529 } | |
| 530 if (!dest_buf) | |
| 531 return false; | |
| 532 | |
| 533 struct jpeg_destination_mgr dest; | |
| 534 dest.init_destination = _dest_do_nothing; | |
| 535 dest.term_destination = _dest_do_nothing; | |
| 536 dest.empty_output_buffer = _dest_empty; | |
| 537 dest.next_output_byte = dest_buf; | |
| 538 dest.free_in_buffer = dest_buf_length; | |
| 539 cinfo.dest = &dest; | |
| 540 cinfo.image_width = width; | |
| 541 cinfo.image_height = height; | |
| 542 cinfo.input_components = nComponents; | |
| 543 if (nComponents == 1) { | |
| 544 cinfo.in_color_space = JCS_GRAYSCALE; | |
| 545 } else if (nComponents == 3) { | |
| 546 cinfo.in_color_space = JCS_RGB; | |
| 547 } else { | |
| 548 cinfo.in_color_space = JCS_CMYK; | |
| 549 } | |
| 550 uint8_t* line_buf = nullptr; | |
| 551 if (nComponents > 1) | |
| 552 line_buf = FX_Alloc2D(uint8_t, width, nComponents); | |
| 553 | |
| 554 jpeg_set_defaults(&cinfo); | |
| 555 jpeg_start_compress(&cinfo, TRUE); | |
| 556 JSAMPROW row_pointer[1]; | |
| 557 JDIMENSION row; | |
| 558 while (cinfo.next_scanline < cinfo.image_height) { | |
| 559 const uint8_t* src_scan = pSource->GetScanline(cinfo.next_scanline); | |
| 560 if (nComponents > 1) { | |
| 561 uint8_t* dest_scan = line_buf; | |
| 562 if (nComponents == 3) { | |
| 563 for (uint32_t i = 0; i < width; i++) { | |
| 564 dest_scan[0] = src_scan[2]; | |
| 565 dest_scan[1] = src_scan[1]; | |
| 566 dest_scan[2] = src_scan[0]; | |
| 567 dest_scan += 3; | |
| 568 src_scan += Bpp; | |
| 569 } | |
| 570 } else { | |
| 571 for (uint32_t i = 0; i < pitch; i++) { | |
| 572 *dest_scan++ = ~*src_scan++; | |
| 573 } | |
| 574 } | |
| 575 row_pointer[0] = line_buf; | |
| 576 } else { | |
| 577 row_pointer[0] = (uint8_t*)src_scan; | |
| 578 } | |
| 579 row = cinfo.next_scanline; | |
| 580 jpeg_write_scanlines(&cinfo, row_pointer, 1); | |
| 581 if (cinfo.next_scanline == row) { | |
| 582 dest_buf = | |
| 583 FX_Realloc(uint8_t, dest_buf, dest_buf_length + JPEG_BLOCK_SIZE); | |
| 584 dest.next_output_byte = dest_buf + dest_buf_length - dest.free_in_buffer; | |
| 585 dest_buf_length += JPEG_BLOCK_SIZE; | |
| 586 dest.free_in_buffer += JPEG_BLOCK_SIZE; | |
| 587 } | |
| 588 } | |
| 589 jpeg_finish_compress(&cinfo); | |
| 590 jpeg_destroy_compress(&cinfo); | |
| 591 FX_Free(line_buf); | |
| 592 dest_size = dest_buf_length - (FX_STRSIZE)dest.free_in_buffer; | |
| 593 | |
| 594 return true; | |
| 595 } | |
| 596 #endif | |
| OLD | NEW |