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 "../../../include/fxcodec/fx_codec.h" | 7 #include "../../../include/fxcodec/fx_codec.h" |
8 #include "codec_int.h" | 8 #include "codec_int.h" |
9 #include "../fx_libopenjpeg/libopenjpeg20/openjpeg.h" | 9 #include "../fx_libopenjpeg/libopenjpeg20/openjpeg.h" |
10 #include "../lcms2/include/fx_lcms2.h" | 10 #include "../lcms2/include/fx_lcms2.h" |
11 static void fx_error_callback(const char *msg, void *client_data) | 11 static void fx_error_callback(const char *msg, void *client_data) |
12 { | 12 { |
13 (void)client_data; | 13 (void)client_data; |
14 } | 14 } |
15 static void fx_warning_callback(const char *msg, void *client_data) | 15 static void fx_warning_callback(const char *msg, void *client_data) |
16 { | 16 { |
17 (void)client_data; | 17 (void)client_data; |
18 } | 18 } |
19 static void fx_info_callback(const char *msg, void *client_data) | 19 static void fx_info_callback(const char *msg, void *client_data) |
20 { | 20 { |
21 (void)client_data; | 21 (void)client_data; |
22 } | 22 } |
23 struct DecodeData { | 23 struct DecodeData { |
Tom Sepez
2015/03/16 17:51:20
So if I'm making a unit test, this declaration mov
| |
24 unsigned char* src_data; | 24 unsigned char* src_data; |
25 OPJ_SIZE_T src_size; | 25 OPJ_SIZE_T src_size; |
26 OPJ_SIZE_T offset; | 26 OPJ_SIZE_T offset; |
27 }; | 27 }; |
28 static OPJ_SIZE_T opj_read_from_memory (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void* p_user_data) | 28 static OPJ_SIZE_T opj_read_from_memory (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void* p_user_data) |
Tom Sepez
2015/03/16 17:51:20
So if I'm making a unit test, these are not longer
| |
29 { | 29 { |
30 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); | 30 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); |
31 if (srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL | | srcData->offset >= srcData->src_size) { | 31 if (srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL | | srcData->offset >= srcData->src_size) { |
32 return -1; | 32 return -1; |
33 } | 33 } |
34 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset; | 34 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset; |
35 OPJ_SIZE_T readlength = p_nb_bytes < bufferLength ? p_nb_bytes : bufferLengt h; | 35 OPJ_SIZE_T readlength = p_nb_bytes < bufferLength ? p_nb_bytes : bufferLengt h; |
36 memcpy(p_buffer, &srcData->src_data[srcData->offset], readlength); | 36 memcpy(p_buffer, &srcData->src_data[srcData->offset], readlength); |
37 srcData->offset += readlength; | 37 srcData->offset += readlength; |
38 return readlength; | 38 return readlength; |
39 } | 39 } |
40 static OPJ_SIZE_T opj_write_from_memory (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void* p_user_data) | 40 static OPJ_SIZE_T opj_write_from_memory (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void* p_user_data) |
41 { | 41 { |
42 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); | 42 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); |
43 if (srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL | | srcData->offset >= srcData->src_size) { | 43 if (srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL | | srcData->offset >= srcData->src_size) { |
44 return -1; | 44 return -1; |
45 } | 45 } |
46 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset; | 46 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset; |
47 OPJ_SIZE_T writeLength = p_nb_bytes < bufferLength ? p_nb_bytes : bufferLeng th; | 47 OPJ_SIZE_T writeLength = p_nb_bytes < bufferLength ? p_nb_bytes : bufferLeng th; |
48 memcpy(&srcData->src_data[srcData->offset], p_buffer, writeLength); | 48 memcpy(&srcData->src_data[srcData->offset], p_buffer, writeLength); |
49 srcData->offset += writeLength; | 49 srcData->offset += writeLength; |
50 return writeLength; | 50 return writeLength; |
51 } | 51 } |
52 static OPJ_OFF_T opj_skip_from_memory (OPJ_OFF_T p_nb_bytes, void* p_user_data) | 52 static OPJ_OFF_T opj_skip_from_memory (OPJ_OFF_T p_nb_bytes, void* p_user_data) |
53 { | 53 { |
54 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); | 54 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); |
55 if (srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL | | srcData->offset >= srcData->src_size) { | 55 if (srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL) { |
56 return -1; | 56 return -1; |
57 } | 57 } |
58 if (srcData->offset >= srcData->src_size) { | |
59 srcData->offset = srcData->src_size; | |
60 return p_nb_bytes; | |
61 } | |
58 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset; | 62 OPJ_SIZE_T bufferLength = srcData->src_size - srcData->offset; |
59 OPJ_SIZE_T skipLength = p_nb_bytes < bufferLength ? p_nb_bytes : bufferLengt h; | 63 OPJ_SIZE_T skipLength = p_nb_bytes < bufferLength ? p_nb_bytes : bufferLengt h; |
60 srcData->offset += skipLength; | 64 srcData->offset += skipLength; |
61 return skipLength; | 65 return skipLength; |
62 } | 66 } |
63 static OPJ_BOOL opj_seek_from_memory (OPJ_OFF_T p_nb_bytes, void* p_user_data) | 67 static OPJ_BOOL opj_seek_from_memory (OPJ_OFF_T p_nb_bytes, void* p_user_data) |
64 { | 68 { |
65 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); | 69 DecodeData* srcData = static_cast<DecodeData*>(p_user_data); |
66 if (srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL | | srcData->offset >= srcData->src_size) { | 70 if (srcData == NULL || srcData->src_size == 0 || srcData->src_data == NULL) { |
67 return OPJ_FALSE; | 71 return OPJ_FALSE; |
68 } | 72 } |
73 if (srcData->offset >= srcData->src_size) { | |
74 return OPJ_TRUE; | |
75 } | |
69 if (p_nb_bytes >= srcData->src_size) { | 76 if (p_nb_bytes >= srcData->src_size) { |
70 return OPJ_FALSE; | 77 return OPJ_FALSE; |
71 } | 78 } |
72 srcData->offset = p_nb_bytes; | 79 srcData->offset = p_nb_bytes; |
73 return OPJ_TRUE; | 80 return OPJ_TRUE; |
74 } | 81 } |
75 opj_stream_t* fx_opj_stream_create_memory_stream (DecodeData* data, OPJ_SIZE _T p_size, OPJ_BOOL p_is_read_stream) | 82 opj_stream_t* fx_opj_stream_create_memory_stream (DecodeData* data, OPJ_SIZE _T p_size, OPJ_BOOL p_is_read_stream) |
76 { | 83 { |
77 opj_stream_t* l_stream = 00; | 84 opj_stream_t* l_stream = 00; |
78 if (!data || ! data->src_data || data->src_size <= 0 ) { | 85 if (!data || ! data->src_data || data->src_size <= 0 ) { |
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
581 if (!src_data || src_size < sizeof(szJP2Header)) { | 588 if (!src_data || src_size < sizeof(szJP2Header)) { |
582 return FALSE; | 589 return FALSE; |
583 } | 590 } |
584 image = NULL; | 591 image = NULL; |
585 m_SrcData = src_data; | 592 m_SrcData = src_data; |
586 m_SrcSize = src_size; | 593 m_SrcSize = src_size; |
587 DecodeData srcData; | 594 DecodeData srcData; |
588 srcData.offset = 0; | 595 srcData.offset = 0; |
589 srcData.src_size = src_size; | 596 srcData.src_size = src_size; |
590 srcData.src_data = const_cast<unsigned char*>(src_data); | 597 srcData.src_data = const_cast<unsigned char*>(src_data); |
591 l_stream = fx_opj_stream_create_memory_stream(&srcData, OPJ_J2K_STREAM_CHUNK _SIZE, 1); | 598 l_stream = fx_opj_stream_create_memory_stream(&srcData, OPJ_J2K_STREAM_CHUNK _SIZE, 1); |
Tom Sepez
2015/03/16 17:24:52
Doesn't srcData go out of scope and get destroyed
| |
592 if (l_stream == NULL) { | 599 if (l_stream == NULL) { |
593 return FALSE; | 600 return FALSE; |
594 } | 601 } |
595 opj_dparameters_t parameters; | 602 opj_dparameters_t parameters; |
596 opj_set_default_decoder_parameters(¶meters); | 603 opj_set_default_decoder_parameters(¶meters); |
597 parameters.decod_format = 0; | 604 parameters.decod_format = 0; |
598 parameters.cod_format = 3; | 605 parameters.cod_format = 3; |
599 if(FXSYS_memcmp32(m_SrcData, szJP2Header, sizeof(szJP2Header)) == 0) { | 606 if(FXSYS_memcmp32(m_SrcData, szJP2Header, sizeof(szJP2Header)) == 0) { |
600 l_codec = opj_create_decompress(OPJ_CODEC_JP2); | 607 l_codec = opj_create_decompress(OPJ_CODEC_JP2); |
601 parameters.decod_format = 1; | 608 parameters.decod_format = 1; |
(...skipping 20 matching lines...) Expand all Loading... | |
622 image->useColorSpace = 0; | 629 image->useColorSpace = 0; |
623 } | 630 } |
624 */ | 631 */ |
625 if (!parameters.nb_tile_to_decode) { | 632 if (!parameters.nb_tile_to_decode) { |
626 if (!opj_set_decode_area(l_codec, image, parameters.DA_x0, | 633 if (!opj_set_decode_area(l_codec, image, parameters.DA_x0, |
627 parameters.DA_y0, parameters.DA_x1, paramete rs.DA_y1)) { | 634 parameters.DA_y0, parameters.DA_x1, paramete rs.DA_y1)) { |
628 opj_image_destroy(image); | 635 opj_image_destroy(image); |
629 image = NULL; | 636 image = NULL; |
630 return FALSE; | 637 return FALSE; |
631 } | 638 } |
632 if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec ,» l_stream))) { | 639 if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec , l_stream))) { |
633 opj_image_destroy(image); | 640 opj_image_destroy(image); |
634 image = NULL; | 641 image = NULL; |
635 return FALSE; | 642 return FALSE; |
636 } | 643 } |
637 } else { | 644 } else { |
638 if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_inde x)) { | 645 if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_inde x)) { |
639 return FALSE; | 646 return FALSE; |
640 } | 647 } |
641 } | 648 } |
642 opj_stream_destroy(l_stream); | 649 opj_stream_destroy(l_stream); |
643 l_stream = NULL; | 650 l_stream = NULL; |
644 if( image->color_space != OPJ_CLRSPC_SYCC | 651 if( image->color_space != OPJ_CLRSPC_SYCC |
645 && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy | 652 && image->numcomps == 3 && image->comps[0].dx == image->comps[0].dy |
646 && image->comps[1].dx != 1 ) { | 653 && image->comps[1].dx != 1 ) { |
647 image->color_space = OPJ_CLRSPC_SYCC; | 654 image->color_space = OPJ_CLRSPC_SYCC; |
648 } else if (image->numcomps <= 2) { | 655 } else if (image->numcomps <= 2) { |
649 image->color_space = OPJ_CLRSPC_GRAY; | 656 image->color_space = OPJ_CLRSPC_GRAY; |
650 } | 657 } |
651 if(image->color_space == OPJ_CLRSPC_SYCC) { | 658 if(image->color_space == OPJ_CLRSPC_SYCC) { |
652 color_sycc_to_rgb(image); | 659 color_sycc_to_rgb(image); |
653 } | 660 } |
654 //if(image->icc_profile_buf && !image->useColorSpace) { | 661 //if(image->icc_profile_buf && !image->useColorSpace) { |
Tom Sepez
2015/03/16 17:24:52
nit: kill dead code while we're at it.
| |
655 if(image->icc_profile_buf) { | 662 if(image->icc_profile_buf) { |
656 FX_Free(image->icc_profile_buf); | 663 FX_Free(image->icc_profile_buf); |
657 image->icc_profile_buf = NULL; | 664 image->icc_profile_buf = NULL; |
658 image->icc_profile_len = 0; | 665 image->icc_profile_len = 0; |
659 } | 666 } |
660 if(!image) { | 667 if(!image) { |
661 return FALSE; | 668 return FALSE; |
662 } | 669 } |
663 return TRUE; | 670 return TRUE; |
664 } | 671 } |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
785 FX_BOOL CCodec_JpxModule::Decode(void* ctx, FX_LPBYTE dest_data, int pitch, FX_B OOL bTranslateColor, FX_LPBYTE offsets) | 792 FX_BOOL CCodec_JpxModule::Decode(void* ctx, FX_LPBYTE dest_data, int pitch, FX_B OOL bTranslateColor, FX_LPBYTE offsets) |
786 { | 793 { |
787 CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx; | 794 CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx; |
788 return pDecoder->Decode(dest_data, pitch, bTranslateColor, offsets); | 795 return pDecoder->Decode(dest_data, pitch, bTranslateColor, offsets); |
789 } | 796 } |
790 void CCodec_JpxModule::DestroyDecoder(void* ctx) | 797 void CCodec_JpxModule::DestroyDecoder(void* ctx) |
791 { | 798 { |
792 CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx; | 799 CJPX_Decoder* pDecoder = (CJPX_Decoder*)ctx; |
793 delete pDecoder; | 800 delete pDecoder; |
794 } | 801 } |
OLD | NEW |