| 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 "core/fxcodec/codec/codec_int.h" | 9 #include "core/fxcodec/codec/codec_int.h" |
| 10 #include "core/fxcodec/include/fx_codec.h" | 10 #include "core/fxcodec/include/fx_codec.h" |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 | 469 |
| 470 int nlines = jpeg_read_scanlines(&cinfo, &m_pScanlineBuf, 1); | 470 int nlines = jpeg_read_scanlines(&cinfo, &m_pScanlineBuf, 1); |
| 471 if (nlines < 1) { | 471 if (nlines < 1) { |
| 472 return nullptr; | 472 return nullptr; |
| 473 } | 473 } |
| 474 return m_pScanlineBuf; | 474 return m_pScanlineBuf; |
| 475 } | 475 } |
| 476 uint32_t CCodec_JpegDecoder::GetSrcOffset() { | 476 uint32_t CCodec_JpegDecoder::GetSrcOffset() { |
| 477 return (uint32_t)(m_SrcSize - src.bytes_in_buffer); | 477 return (uint32_t)(m_SrcSize - src.bytes_in_buffer); |
| 478 } | 478 } |
| 479 ICodec_ScanlineDecoder* CCodec_JpegModule::CreateDecoder( | 479 CCodec_ScanlineDecoder* CCodec_JpegModule::CreateDecoder( |
| 480 const uint8_t* src_buf, | 480 const uint8_t* src_buf, |
| 481 uint32_t src_size, | 481 uint32_t src_size, |
| 482 int width, | 482 int width, |
| 483 int height, | 483 int height, |
| 484 int nComps, | 484 int nComps, |
| 485 FX_BOOL ColorTransform) { | 485 FX_BOOL ColorTransform) { |
| 486 if (!src_buf || src_size == 0) { | 486 if (!src_buf || src_size == 0) { |
| 487 return NULL; | 487 return NULL; |
| 488 } | 488 } |
| 489 CCodec_JpegDecoder* pDecoder = new CCodec_JpegDecoder; | 489 CCodec_JpegDecoder* pDecoder = new CCodec_JpegDecoder; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 cinfo->src->bytes_in_buffer -= num; | 544 cinfo->src->bytes_in_buffer -= num; |
| 545 } | 545 } |
| 546 } | 546 } |
| 547 }; | 547 }; |
| 548 static void* jpeg_alloc_func(unsigned int size) { | 548 static void* jpeg_alloc_func(unsigned int size) { |
| 549 return FX_Alloc(char, size); | 549 return FX_Alloc(char, size); |
| 550 } | 550 } |
| 551 static void jpeg_free_func(void* p) { | 551 static void jpeg_free_func(void* p) { |
| 552 FX_Free(p); | 552 FX_Free(p); |
| 553 } | 553 } |
| 554 void* CCodec_JpegModule::Start() { | 554 FXJPEG_Context* CCodec_JpegModule::Start() { |
| 555 FXJPEG_Context* p = FX_Alloc(FXJPEG_Context, 1); | 555 FXJPEG_Context* p = FX_Alloc(FXJPEG_Context, 1); |
| 556 p->m_AllocFunc = jpeg_alloc_func; | 556 p->m_AllocFunc = jpeg_alloc_func; |
| 557 p->m_FreeFunc = jpeg_free_func; | 557 p->m_FreeFunc = jpeg_free_func; |
| 558 p->m_ErrMgr.error_exit = _error_fatal1; | 558 p->m_ErrMgr.error_exit = _error_fatal1; |
| 559 p->m_ErrMgr.emit_message = _error_do_nothing1; | 559 p->m_ErrMgr.emit_message = _error_do_nothing1; |
| 560 p->m_ErrMgr.output_message = _error_do_nothing; | 560 p->m_ErrMgr.output_message = _error_do_nothing; |
| 561 p->m_ErrMgr.format_message = _error_do_nothing2; | 561 p->m_ErrMgr.format_message = _error_do_nothing2; |
| 562 p->m_ErrMgr.reset_error_mgr = _error_do_nothing; | 562 p->m_ErrMgr.reset_error_mgr = _error_do_nothing; |
| 563 p->m_SrcMgr.init_source = _src_do_nothing; | 563 p->m_SrcMgr.init_source = _src_do_nothing; |
| 564 p->m_SrcMgr.term_source = _src_do_nothing; | 564 p->m_SrcMgr.term_source = _src_do_nothing; |
| 565 p->m_SrcMgr.skip_input_data = _src_skip_data1; | 565 p->m_SrcMgr.skip_input_data = _src_skip_data1; |
| 566 p->m_SrcMgr.fill_input_buffer = _src_fill_buffer; | 566 p->m_SrcMgr.fill_input_buffer = _src_fill_buffer; |
| 567 p->m_SrcMgr.resync_to_restart = _src_resync; | 567 p->m_SrcMgr.resync_to_restart = _src_resync; |
| 568 p->m_Info.client_data = p; | 568 p->m_Info.client_data = p; |
| 569 p->m_Info.err = &p->m_ErrMgr; | 569 p->m_Info.err = &p->m_ErrMgr; |
| 570 if (setjmp(p->m_JumpMark) == -1) { | 570 if (setjmp(p->m_JumpMark) == -1) { |
| 571 return 0; | 571 return 0; |
| 572 } | 572 } |
| 573 jpeg_create_decompress(&p->m_Info); | 573 jpeg_create_decompress(&p->m_Info); |
| 574 p->m_Info.src = &p->m_SrcMgr; | 574 p->m_Info.src = &p->m_SrcMgr; |
| 575 p->m_SkipSize = 0; | 575 p->m_SkipSize = 0; |
| 576 return p; | 576 return p; |
| 577 } | 577 } |
| 578 void CCodec_JpegModule::Finish(void* pContext) { | 578 |
| 579 FXJPEG_Context* p = (FXJPEG_Context*)pContext; | 579 void CCodec_JpegModule::Finish(FXJPEG_Context* ctx) { |
| 580 jpeg_destroy_decompress(&p->m_Info); | 580 jpeg_destroy_decompress(&ctx->m_Info); |
| 581 p->m_FreeFunc(p); | 581 ctx->m_FreeFunc(ctx); |
| 582 } | 582 } |
| 583 void CCodec_JpegModule::Input(void* pContext, | 583 |
| 584 void CCodec_JpegModule::Input(FXJPEG_Context* ctx, |
| 584 const unsigned char* src_buf, | 585 const unsigned char* src_buf, |
| 585 uint32_t src_size) { | 586 uint32_t src_size) { |
| 586 FXJPEG_Context* p = (FXJPEG_Context*)pContext; | 587 if (ctx->m_SkipSize) { |
| 587 if (p->m_SkipSize) { | 588 if (ctx->m_SkipSize > src_size) { |
| 588 if (p->m_SkipSize > src_size) { | 589 ctx->m_SrcMgr.bytes_in_buffer = 0; |
| 589 p->m_SrcMgr.bytes_in_buffer = 0; | 590 ctx->m_SkipSize -= src_size; |
| 590 p->m_SkipSize -= src_size; | |
| 591 return; | 591 return; |
| 592 } | 592 } |
| 593 src_size -= p->m_SkipSize; | 593 src_size -= ctx->m_SkipSize; |
| 594 src_buf += p->m_SkipSize; | 594 src_buf += ctx->m_SkipSize; |
| 595 p->m_SkipSize = 0; | 595 ctx->m_SkipSize = 0; |
| 596 } | 596 } |
| 597 p->m_SrcMgr.next_input_byte = src_buf; | 597 ctx->m_SrcMgr.next_input_byte = src_buf; |
| 598 p->m_SrcMgr.bytes_in_buffer = src_size; | 598 ctx->m_SrcMgr.bytes_in_buffer = src_size; |
| 599 } | 599 } |
| 600 | 600 |
| 601 #ifdef PDF_ENABLE_XFA | 601 #ifdef PDF_ENABLE_XFA |
| 602 int CCodec_JpegModule::ReadHeader(void* pContext, | 602 int CCodec_JpegModule::ReadHeader(FXJPEG_Context* ctx, |
| 603 int* width, | 603 int* width, |
| 604 int* height, | 604 int* height, |
| 605 int* nComps, | 605 int* nComps, |
| 606 CFX_DIBAttribute* pAttribute) { | 606 CFX_DIBAttribute* pAttribute) { |
| 607 #else // PDF_ENABLE_XFA | 607 #else // PDF_ENABLE_XFA |
| 608 int CCodec_JpegModule::ReadHeader(void* pContext, | 608 int CCodec_JpegModule::ReadHeader(FXJPEG_Context* ctx, |
| 609 int* width, | 609 int* width, |
| 610 int* height, | 610 int* height, |
| 611 int* nComps) { | 611 int* nComps) { |
| 612 #endif // PDF_ENABLE_XFA | 612 #endif // PDF_ENABLE_XFA |
| 613 FXJPEG_Context* p = (FXJPEG_Context*)pContext; | 613 if (setjmp(ctx->m_JumpMark) == -1) |
| 614 if (setjmp(p->m_JumpMark) == -1) { | |
| 615 return 1; | 614 return 1; |
| 616 } | 615 |
| 617 int ret = jpeg_read_header(&p->m_Info, true); | 616 int ret = jpeg_read_header(&ctx->m_Info, true); |
| 618 if (ret == JPEG_SUSPENDED) { | 617 if (ret == JPEG_SUSPENDED) |
| 619 return 2; | 618 return 2; |
| 620 } | 619 if (ret != JPEG_HEADER_OK) |
| 621 if (ret != JPEG_HEADER_OK) { | |
| 622 return 1; | 620 return 1; |
| 623 } | 621 |
| 624 *width = p->m_Info.image_width; | 622 *width = ctx->m_Info.image_width; |
| 625 *height = p->m_Info.image_height; | 623 *height = ctx->m_Info.image_height; |
| 626 *nComps = p->m_Info.num_components; | 624 *nComps = ctx->m_Info.num_components; |
| 627 #ifdef PDF_ENABLE_XFA | 625 #ifdef PDF_ENABLE_XFA |
| 628 _JpegLoadAttribute(&p->m_Info, pAttribute); | 626 _JpegLoadAttribute(&ctx->m_Info, pAttribute); |
| 629 #endif | 627 #endif |
| 630 return 0; | 628 return 0; |
| 631 } | 629 } |
| 632 int CCodec_JpegModule::StartScanline(void* pContext, int down_scale) { | 630 |
| 633 FXJPEG_Context* p = (FXJPEG_Context*)pContext; | 631 int CCodec_JpegModule::StartScanline(FXJPEG_Context* ctx, int down_scale) { |
| 634 if (setjmp(p->m_JumpMark) == -1) { | 632 if (setjmp(ctx->m_JumpMark) == -1) |
| 635 return 0; | 633 return 0; |
| 636 } | 634 |
| 637 p->m_Info.scale_denom = down_scale; | 635 ctx->m_Info.scale_denom = down_scale; |
| 638 return jpeg_start_decompress(&p->m_Info); | 636 return jpeg_start_decompress(&ctx->m_Info); |
| 639 } | 637 } |
| 640 FX_BOOL CCodec_JpegModule::ReadScanline(void* pContext, | 638 |
| 639 FX_BOOL CCodec_JpegModule::ReadScanline(FXJPEG_Context* ctx, |
| 641 unsigned char* dest_buf) { | 640 unsigned char* dest_buf) { |
| 642 FXJPEG_Context* p = (FXJPEG_Context*)pContext; | 641 if (setjmp(ctx->m_JumpMark) == -1) |
| 643 if (setjmp(p->m_JumpMark) == -1) { | |
| 644 return FALSE; | 642 return FALSE; |
| 645 } | 643 |
| 646 int nlines = jpeg_read_scanlines(&p->m_Info, &dest_buf, 1); | 644 int nlines = jpeg_read_scanlines(&ctx->m_Info, &dest_buf, 1); |
| 647 return nlines == 1; | 645 return nlines == 1; |
| 648 } | 646 } |
| 649 uint32_t CCodec_JpegModule::GetAvailInput(void* pContext, | 647 |
| 648 uint32_t CCodec_JpegModule::GetAvailInput(FXJPEG_Context* ctx, |
| 650 uint8_t** avail_buf_ptr) { | 649 uint8_t** avail_buf_ptr) { |
| 651 if (avail_buf_ptr) { | 650 if (avail_buf_ptr) { |
| 652 *avail_buf_ptr = NULL; | 651 *avail_buf_ptr = NULL; |
| 653 if (((FXJPEG_Context*)pContext)->m_SrcMgr.bytes_in_buffer > 0) { | 652 if (ctx->m_SrcMgr.bytes_in_buffer > 0) { |
| 654 *avail_buf_ptr = | 653 *avail_buf_ptr = (uint8_t*)ctx->m_SrcMgr.next_input_byte; |
| 655 (uint8_t*)((FXJPEG_Context*)pContext)->m_SrcMgr.next_input_byte; | |
| 656 } | 654 } |
| 657 } | 655 } |
| 658 return (uint32_t)((FXJPEG_Context*)pContext)->m_SrcMgr.bytes_in_buffer; | 656 return (uint32_t)ctx->m_SrcMgr.bytes_in_buffer; |
| 659 } | 657 } |
| OLD | NEW |