OLD | NEW |
1 /* | 1 /* |
2 * The copyright in this software is being made available under the 2-clauses | 2 * The copyright in this software is being made available under the 2-clauses |
3 * BSD License, included below. This software may be subject to other third | 3 * BSD License, included below. This software may be subject to other third |
4 * party and contributor rights, including patent rights, and no such rights | 4 * party and contributor rights, including patent rights, and no such rights |
5 * are granted under this license. | 5 * are granted under this license. |
6 * | 6 * |
7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium | 7 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium |
8 * Copyright (c) 2002-2014, Professor Benoit Macq | 8 * Copyright (c) 2002-2014, Professor Benoit Macq |
9 * Copyright (c) 2001-2003, David Janssens | 9 * Copyright (c) 2001-2003, David Janssens |
10 * Copyright (c) 2002-2003, Yannick Verschueren | 10 * Copyright (c) 2002-2003, Yannick Verschueren |
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 } | 530 } |
531 | 531 |
532 opj_read_bytes(p_image_header_data,&(jp2->h),4);
/* HEIGHT */ | 532 opj_read_bytes(p_image_header_data,&(jp2->h),4);
/* HEIGHT */ |
533 p_image_header_data += 4; | 533 p_image_header_data += 4; |
534 opj_read_bytes(p_image_header_data,&(jp2->w),4);
/* WIDTH */ | 534 opj_read_bytes(p_image_header_data,&(jp2->w),4);
/* WIDTH */ |
535 p_image_header_data += 4; | 535 p_image_header_data += 4; |
536 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */ | 536 opj_read_bytes(p_image_header_data,&(jp2->numcomps),2); /* NC */ |
537 p_image_header_data += 2; | 537 p_image_header_data += 2; |
538 | 538 |
539 /* allocate memory for components */ | 539 /* allocate memory for components */ |
540 » jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp
2_comps_t)); | 540 » jp2->comps = (opj_jp2_comps_t*) opj_calloc(jp2->numcomps, sizeof(opj_jp2
_comps_t)); |
541 if (jp2->comps == 0) { | 541 if (jp2->comps == 0) { |
542 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle
image header (ihdr)\n"); | 542 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle
image header (ihdr)\n"); |
543 return OPJ_FALSE; | 543 return OPJ_FALSE; |
544 } | 544 } |
545 memset(jp2->comps,0,jp2->numcomps * sizeof(opj_jp2_comps_t)); | |
546 | 545 |
547 opj_read_bytes(p_image_header_data,&(jp2->bpc),1);
/* BPC */ | 546 opj_read_bytes(p_image_header_data,&(jp2->bpc),1);
/* BPC */ |
548 ++ p_image_header_data; | 547 ++ p_image_header_data; |
549 | 548 |
550 opj_read_bytes(p_image_header_data,&(jp2->C),1);
/* C */ | 549 opj_read_bytes(p_image_header_data,&(jp2->C),1);
/* C */ |
551 ++ p_image_header_data; | 550 ++ p_image_header_data; |
552 | 551 |
553 /* Should be equal to 7 cf. chapter about image header box of the norm *
/ | 552 /* Should be equal to 7 cf. chapter about image header box of the norm *
/ |
554 if (jp2->C != 7){ | 553 if (jp2->C != 7){ |
555 opj_event_msg(p_manager, EVT_INFO, "JP2 IHDR box: compression ty
pe indicate that the file is not a conforming JP2 file (%d) \n", jp2->C); | 554 opj_event_msg(p_manager, EVT_INFO, "JP2 IHDR box: compression ty
pe indicate that the file is not a conforming JP2 file (%d) \n", jp2->C); |
(...skipping 11 matching lines...) Expand all Loading... |
567 OPJ_UINT32 * p_nb_bytes_written | 566 OPJ_UINT32 * p_nb_bytes_written |
568 ) | 567 ) |
569 { | 568 { |
570 OPJ_BYTE * l_ihdr_data,* l_current_ihdr_ptr; | 569 OPJ_BYTE * l_ihdr_data,* l_current_ihdr_ptr; |
571 | 570 |
572 /* preconditions */ | 571 /* preconditions */ |
573 assert(jp2 != 00); | 572 assert(jp2 != 00); |
574 assert(p_nb_bytes_written != 00); | 573 assert(p_nb_bytes_written != 00); |
575 | 574 |
576 /* default image header is 22 bytes wide */ | 575 /* default image header is 22 bytes wide */ |
577 » l_ihdr_data = (OPJ_BYTE *) opj_malloc(22); | 576 » l_ihdr_data = (OPJ_BYTE *) opj_calloc(1,22); |
578 if (l_ihdr_data == 00) { | 577 if (l_ihdr_data == 00) { |
579 return 00; | 578 return 00; |
580 } | 579 } |
581 memset(l_ihdr_data,0,22); | |
582 | 580 |
583 l_current_ihdr_ptr = l_ihdr_data; | 581 l_current_ihdr_ptr = l_ihdr_data; |
584 | 582 |
585 opj_write_bytes(l_current_ihdr_ptr,22,4);
/* write box size */ | 583 opj_write_bytes(l_current_ihdr_ptr,22,4);
/* write box size */ |
586 l_current_ihdr_ptr+=4; | 584 l_current_ihdr_ptr+=4; |
587 | 585 |
588 opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR
*/ | 586 opj_write_bytes(l_current_ihdr_ptr,JP2_IHDR, 4); /* IHDR
*/ |
589 l_current_ihdr_ptr+=4; | 587 l_current_ihdr_ptr+=4; |
590 | 588 |
591 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */ | 589 opj_write_bytes(l_current_ihdr_ptr,jp2->h, 4); /* HEIGHT */ |
(...skipping 28 matching lines...) Expand all Loading... |
620 { | 618 { |
621 OPJ_UINT32 i; | 619 OPJ_UINT32 i; |
622 /* room for 8 bytes for box and 1 byte for each component */ | 620 /* room for 8 bytes for box and 1 byte for each component */ |
623 OPJ_UINT32 l_bpcc_size = 8 + jp2->numcomps; | 621 OPJ_UINT32 l_bpcc_size = 8 + jp2->numcomps; |
624 OPJ_BYTE * l_bpcc_data,* l_current_bpcc_ptr; | 622 OPJ_BYTE * l_bpcc_data,* l_current_bpcc_ptr; |
625 | 623 |
626 /* preconditions */ | 624 /* preconditions */ |
627 assert(jp2 != 00); | 625 assert(jp2 != 00); |
628 assert(p_nb_bytes_written != 00); | 626 assert(p_nb_bytes_written != 00); |
629 | 627 |
630 » l_bpcc_data = (OPJ_BYTE *) opj_malloc(l_bpcc_size); | 628 » l_bpcc_data = (OPJ_BYTE *) opj_calloc(1,l_bpcc_size); |
631 if (l_bpcc_data == 00) { | 629 if (l_bpcc_data == 00) { |
632 return 00; | 630 return 00; |
633 } | 631 } |
634 memset(l_bpcc_data,0,l_bpcc_size); | |
635 | 632 |
636 l_current_bpcc_ptr = l_bpcc_data; | 633 l_current_bpcc_ptr = l_bpcc_data; |
637 | 634 |
638 opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4);
/* write box size */ | 635 opj_write_bytes(l_current_bpcc_ptr,l_bpcc_size,4);
/* write box size */ |
639 l_current_bpcc_ptr += 4; | 636 l_current_bpcc_ptr += 4; |
640 | 637 |
641 opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4);
/* BPCC */ | 638 opj_write_bytes(l_current_bpcc_ptr,JP2_BPCC,4);
/* BPCC */ |
642 l_current_bpcc_ptr += 4; | 639 l_current_bpcc_ptr += 4; |
643 | 640 |
644 for (i = 0; i < jp2->numcomps; ++i) { | 641 for (i = 0; i < jp2->numcomps; ++i) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
702 l_colr_size += 4; /* EnumCS */ | 699 l_colr_size += 4; /* EnumCS */ |
703 break; | 700 break; |
704 case 2 : | 701 case 2 : |
705 assert(jp2->color.icc_profile_len); /* ICC profile */ | 702 assert(jp2->color.icc_profile_len); /* ICC profile */ |
706 l_colr_size += jp2->color.icc_profile_len; | 703 l_colr_size += jp2->color.icc_profile_len; |
707 break; | 704 break; |
708 default : | 705 default : |
709 return 00; | 706 return 00; |
710 } | 707 } |
711 | 708 |
712 » l_colr_data = (OPJ_BYTE *) opj_malloc(l_colr_size); | 709 » l_colr_data = (OPJ_BYTE *) opj_calloc(1,l_colr_size); |
713 if (l_colr_data == 00) { | 710 if (l_colr_data == 00) { |
714 return 00; | 711 return 00; |
715 } | 712 } |
716 memset(l_colr_data,0,l_colr_size); | |
717 | 713 |
718 l_current_colr_ptr = l_colr_data; | 714 l_current_colr_ptr = l_colr_data; |
719 | 715 |
720 opj_write_bytes(l_current_colr_ptr,l_colr_size,4);
/* write box size */ | 716 opj_write_bytes(l_current_colr_ptr,l_colr_size,4);
/* write box size */ |
721 l_current_colr_ptr += 4; | 717 l_current_colr_ptr += 4; |
722 | 718 |
723 opj_write_bytes(l_current_colr_ptr,JP2_COLR,4);
/* BPCC */ | 719 opj_write_bytes(l_current_colr_ptr,JP2_COLR,4);
/* BPCC */ |
724 l_current_colr_ptr += 4; | 720 l_current_colr_ptr += 4; |
725 | 721 |
726 opj_write_bytes(l_current_colr_ptr, jp2->meth,1);
/* METH */ | 722 opj_write_bytes(l_current_colr_ptr, jp2->meth,1);
/* METH */ |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 | 848 |
853 channel_size = color->jp2_pclr->channel_size; | 849 channel_size = color->jp2_pclr->channel_size; |
854 channel_sign = color->jp2_pclr->channel_sign; | 850 channel_sign = color->jp2_pclr->channel_sign; |
855 entries = color->jp2_pclr->entries; | 851 entries = color->jp2_pclr->entries; |
856 cmap = color->jp2_pclr->cmap; | 852 cmap = color->jp2_pclr->cmap; |
857 nr_channels = color->jp2_pclr->nr_channels; | 853 nr_channels = color->jp2_pclr->nr_channels; |
858 | 854 |
859 old_comps = image->comps; | 855 old_comps = image->comps; |
860 new_comps = (opj_image_comp_t*) | 856 new_comps = (opj_image_comp_t*) |
861 opj_malloc(nr_channels * sizeof(opj_image_comp_t)); | 857 opj_malloc(nr_channels * sizeof(opj_image_comp_t)); |
862 | 858 » if (!new_comps) { |
| 859 » » /* FIXME no error code for opj_jp2_apply_pclr */ |
| 860 » » /* FIXME event manager error callback */ |
| 861 » » return; |
| 862 » } |
863 for(i = 0; i < nr_channels; ++i) { | 863 for(i = 0; i < nr_channels; ++i) { |
864 pcol = cmap[i].pcol; cmp = cmap[i].cmp; | 864 pcol = cmap[i].pcol; cmp = cmap[i].cmp; |
865 | 865 |
866 /* Direct use */ | 866 /* Direct use */ |
867 if(cmap[i].mtyp == 0){ | 867 if(cmap[i].mtyp == 0){ |
868 assert( pcol == 0 ); | 868 assert( pcol == 0 ); |
869 new_comps[i] = old_comps[cmp]; | 869 new_comps[i] = old_comps[cmp]; |
870 } else { | 870 } else { |
871 assert( i == pcol ); | 871 assert( i == pcol ); |
872 new_comps[pcol] = old_comps[cmp]; | 872 new_comps[pcol] = old_comps[cmp]; |
873 } | 873 } |
874 | 874 |
875 /* Palette mapping: */ | 875 /* Palette mapping: */ |
876 new_comps[i].data = (OPJ_INT32*) | 876 new_comps[i].data = (OPJ_INT32*) |
877 opj_malloc(old_comps[cmp].w * old_comps[cmp].h *
sizeof(OPJ_INT32)); | 877 opj_malloc(old_comps[cmp].w * old_comps[cmp].h *
sizeof(OPJ_INT32)); |
| 878 if (!new_comps[i].data) { |
| 879 opj_free(new_comps); |
| 880 new_comps = NULL; |
| 881 /* FIXME no error code for opj_jp2_apply_pclr */ |
| 882 /* FIXME event manager error callback */ |
| 883 return; |
| 884 } |
878 new_comps[i].prec = channel_size[i]; | 885 new_comps[i].prec = channel_size[i]; |
879 new_comps[i].sgnd = channel_sign[i]; | 886 new_comps[i].sgnd = channel_sign[i]; |
880 } | 887 } |
881 | 888 |
882 top_k = color->jp2_pclr->nr_entries - 1; | 889 top_k = color->jp2_pclr->nr_entries - 1; |
883 | 890 |
884 for(i = 0; i < nr_channels; ++i) { | 891 for(i = 0; i < nr_channels; ++i) { |
885 /* Palette mapping: */ | 892 /* Palette mapping: */ |
886 cmp = cmap[i].cmp; pcol = cmap[i].pcol; | 893 cmp = cmap[i].cmp; pcol = cmap[i].pcol; |
887 src = old_comps[cmp].data; | 894 src = old_comps[cmp].data; |
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1229 opj_read_bytes(p_colr_header_data,&jp2->meth ,1);
/* METH */ | 1236 opj_read_bytes(p_colr_header_data,&jp2->meth ,1);
/* METH */ |
1230 ++p_colr_header_data; | 1237 ++p_colr_header_data; |
1231 | 1238 |
1232 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECE
DENCE */ | 1239 opj_read_bytes(p_colr_header_data,&jp2->precedence ,1); /* PRECE
DENCE */ |
1233 ++p_colr_header_data; | 1240 ++p_colr_header_data; |
1234 | 1241 |
1235 opj_read_bytes(p_colr_header_data,&jp2->approx ,1);
/* APPROX */ | 1242 opj_read_bytes(p_colr_header_data,&jp2->approx ,1);
/* APPROX */ |
1236 ++p_colr_header_data; | 1243 ++p_colr_header_data; |
1237 | 1244 |
1238 if (jp2->meth == 1) { | 1245 if (jp2->meth == 1) { |
| 1246 if (p_colr_header_size < 7) { |
| 1247 opj_event_msg(p_manager, EVT_ERROR, "Bad COLR header box
(bad size: %d)\n", p_colr_header_size); |
| 1248 return OPJ_FALSE; |
| 1249 } |
1239 if (p_colr_header_size > 7) { | 1250 if (p_colr_header_size > 7) { |
1240 /* testcase Altona_Technical_v20_x4.pdf */ | 1251 /* testcase Altona_Technical_v20_x4.pdf */ |
1241 opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header b
ox (bad size: %d)\n", p_colr_header_size); | 1252 opj_event_msg(p_manager, EVT_WARNING, "Bad COLR header b
ox (bad size: %d)\n", p_colr_header_size); |
1242 } | 1253 } |
1243 | 1254 |
1244 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4);
/* EnumCS */ | 1255 opj_read_bytes(p_colr_header_data,&jp2->enumcs ,4);
/* EnumCS */ |
1245 | 1256 |
1246 p_colr_header_data += 4; | |
1247 | |
1248 if(jp2->enumcs == 14)/* CIELab */ | |
1249 { | |
1250 OPJ_UINT32 *cielab; | |
1251 OPJ_UINT32 rl, ol, ra, oa, rb, ob, il; | |
1252 | |
1253 cielab = (OPJ_UINT32*)opj_malloc(9 * sizeof(OPJ_UINT32))
; | |
1254 cielab[0] = 14; /* enumcs */ | |
1255 | |
1256 if(p_colr_header_size == 7)/* default values */ | |
1257 { | |
1258 rl = ra = rb = ol = oa = ob = 0; | |
1259 il = 0x00443530; /* D50 */ | |
1260 cielab[1] = 0x44454600;/* DEF */ | |
1261 } | |
1262 else if(p_colr_header_size == 35) | |
1263 { | |
1264 opj_read_bytes(p_colr_header_data, &rl, 4); | |
1265 p_colr_header_data += 4; | |
1266 opj_read_bytes(p_colr_header_data, &ol, 4); | |
1267 p_colr_header_data += 4; | |
1268 opj_read_bytes(p_colr_header_data, &ra, 4); | |
1269 p_colr_header_data += 4; | |
1270 opj_read_bytes(p_colr_header_data, &oa, 4); | |
1271 p_colr_header_data += 4; | |
1272 opj_read_bytes(p_colr_header_data, &rb, 4); | |
1273 p_colr_header_data += 4; | |
1274 opj_read_bytes(p_colr_header_data, &ob, 4); | |
1275 p_colr_header_data += 4; | |
1276 opj_read_bytes(p_colr_header_data, &il, 4); | |
1277 p_colr_header_data += 4; | |
1278 cielab[1] = 0; | |
1279 } | |
1280 cielab[2] = rl; cielab[4] = ra; cielab[6] = rb; | |
1281 cielab[3] = ol; cielab[5] = oa; cielab[7] = ob; | |
1282 cielab[8] = il; | |
1283 | |
1284 jp2->color.icc_profile_buf = (unsigned char*)cielab; | |
1285 jp2->color.icc_profile_len = 0; | |
1286 } | |
1287 | |
1288 jp2->color.jp2_has_colr = 1; | 1257 jp2->color.jp2_has_colr = 1; |
1289 } | 1258 } |
1290 else if (jp2->meth == 2) { | 1259 else if (jp2->meth == 2) { |
1291 /* ICC profile */ | 1260 /* ICC profile */ |
1292 OPJ_INT32 it_icc_value = 0; | 1261 OPJ_INT32 it_icc_value = 0; |
1293 OPJ_INT32 icc_len = (OPJ_INT32)p_colr_header_size - 3; | 1262 OPJ_INT32 icc_len = (OPJ_INT32)p_colr_header_size - 3; |
1294 | 1263 |
1295 jp2->color.icc_profile_len = (OPJ_UINT32)icc_len; | 1264 jp2->color.icc_profile_len = (OPJ_UINT32)icc_len; |
1296 » » jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_malloc((size_t)icc_
len); | 1265 » » jp2->color.icc_profile_buf = (OPJ_BYTE*) opj_calloc(1,(size_t)ic
c_len); |
1297 if (!jp2->color.icc_profile_buf) | 1266 if (!jp2->color.icc_profile_buf) |
1298 { | 1267 { |
1299 jp2->color.icc_profile_len = 0; | 1268 jp2->color.icc_profile_len = 0; |
1300 return OPJ_FALSE; | 1269 return OPJ_FALSE; |
1301 } | 1270 } |
1302 memset(jp2->color.icc_profile_buf, 0, (size_t)icc_len * sizeof(O
PJ_BYTE)); | |
1303 | 1271 |
1304 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value) | 1272 for (it_icc_value = 0; it_icc_value < icc_len; ++it_icc_value) |
1305 { | 1273 { |
1306 opj_read_bytes(p_colr_header_data,&l_value,1);
/* icc values */ | 1274 opj_read_bytes(p_colr_header_data,&l_value,1);
/* icc values */ |
1307 ++p_colr_header_data; | 1275 ++p_colr_header_data; |
1308 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_
value; | 1276 jp2->color.icc_profile_buf[it_icc_value] = (OPJ_BYTE) l_
value; |
1309 } | 1277 } |
1310 | 1278 |
1311 jp2->color.jp2_has_colr = 1; | 1279 jp2->color.jp2_has_colr = 1; |
1312 } | 1280 } |
1313 else if (jp2->meth > 2) | 1281 else if (jp2->meth > 2) |
1314 { | 1282 { |
1315 /*» ISO/IEC 15444-1:2004 (E), Table I.9 ?Legal METH values: | 1283 /*» ISO/IEC 15444-1:2004 (E), Table I.9 Â Legal METH values: |
1316 conforming JP2 reader shall ignore the entire Colour Specification box.*
/ | 1284 conforming JP2 reader shall ignore the entire Colour Specification box.*
/ |
1317 opj_event_msg(p_manager, EVT_INFO, "COLR BOX meth value is not a regular
value (%d), " | 1285 opj_event_msg(p_manager, EVT_INFO, "COLR BOX meth value is not a regular
value (%d), " |
1318 "so we will ignore the entire Colour Specification box. \n", jp2->me
th); | 1286 "so we will ignore the entire Colour Specification box. \n", jp2->me
th); |
1319 } | 1287 } |
1320 return OPJ_TRUE; | 1288 return OPJ_TRUE; |
1321 } | 1289 } |
1322 | 1290 |
1323 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2, | 1291 OPJ_BOOL opj_jp2_decode(opj_jp2_t *jp2, |
1324 opj_stream_private_t *p_stream, | 1292 opj_stream_private_t *p_stream, |
1325 opj_image_t* p_image, | 1293 opj_image_t* p_image, |
(...skipping 28 matching lines...) Expand all Loading... |
1354 /* Apply the color space if needed */ | 1322 /* Apply the color space if needed */ |
1355 if(jp2->color.jp2_cdef) { | 1323 if(jp2->color.jp2_cdef) { |
1356 opj_jp2_apply_cdef(p_image, &(jp2->color)); | 1324 opj_jp2_apply_cdef(p_image, &(jp2->color)); |
1357 } | 1325 } |
1358 | 1326 |
1359 if(jp2->color.jp2_pclr) { | 1327 if(jp2->color.jp2_pclr) { |
1360 /* Part 1, I.5.3.4: Either both or none : */ | 1328 /* Part 1, I.5.3.4: Either both or none : */ |
1361 if( !jp2->color.jp2_pclr->cmap) | 1329 if( !jp2->color.jp2_pclr->cmap) |
1362 opj_jp2_free_pclr(&(jp2->color)); | 1330 opj_jp2_free_pclr(&(jp2->color)); |
1363 else | 1331 else |
1364 if(!p_image->useColorSpace) | |
1365 opj_jp2_apply_pclr(p_image, &(jp2->color)); | 1332 opj_jp2_apply_pclr(p_image, &(jp2->color)); |
1366 } | 1333 } |
1367 | 1334 |
1368 if(jp2->color.icc_profile_buf) { | 1335 if(jp2->color.icc_profile_buf) { |
1369 p_image->icc_profile_buf = jp2->color.icc_profile_buf; | 1336 p_image->icc_profile_buf = jp2->color.icc_profile_buf; |
1370 p_image->icc_profile_len = jp2->color.icc_profile_len; | 1337 p_image->icc_profile_len = jp2->color.icc_profile_len; |
1371 jp2->color.icc_profile_buf = NULL; | 1338 jp2->color.icc_profile_buf = NULL; |
1372 } | 1339 } |
1373 } | 1340 } |
1374 | 1341 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1480 OPJ_UINT32 i; | 1447 OPJ_UINT32 i; |
1481 OPJ_UINT32 l_ftyp_size = 16 + 4 * jp2->numcl; | 1448 OPJ_UINT32 l_ftyp_size = 16 + 4 * jp2->numcl; |
1482 OPJ_BYTE * l_ftyp_data, * l_current_data_ptr; | 1449 OPJ_BYTE * l_ftyp_data, * l_current_data_ptr; |
1483 OPJ_BOOL l_result; | 1450 OPJ_BOOL l_result; |
1484 | 1451 |
1485 /* preconditions */ | 1452 /* preconditions */ |
1486 assert(cio != 00); | 1453 assert(cio != 00); |
1487 assert(jp2 != 00); | 1454 assert(jp2 != 00); |
1488 assert(p_manager != 00); | 1455 assert(p_manager != 00); |
1489 | 1456 |
1490 » l_ftyp_data = (OPJ_BYTE *) opj_malloc(l_ftyp_size); | 1457 » l_ftyp_data = (OPJ_BYTE *) opj_calloc(1,l_ftyp_size); |
1491 | 1458 |
1492 if (l_ftyp_data == 00) { | 1459 if (l_ftyp_data == 00) { |
1493 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle
ftyp data\n"); | 1460 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle
ftyp data\n"); |
1494 return OPJ_FALSE; | 1461 return OPJ_FALSE; |
1495 } | 1462 } |
1496 | 1463 |
1497 memset(l_ftyp_data,0,l_ftyp_size); | |
1498 | |
1499 l_current_data_ptr = l_ftyp_data; | 1464 l_current_data_ptr = l_ftyp_data; |
1500 | 1465 |
1501 opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */ | 1466 opj_write_bytes(l_current_data_ptr, l_ftyp_size,4); /* box size */ |
1502 l_current_data_ptr += 4; | 1467 l_current_data_ptr += 4; |
1503 | 1468 |
1504 opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */ | 1469 opj_write_bytes(l_current_data_ptr, JP2_FTYP,4); /* FTYP */ |
1505 l_current_data_ptr += 4; | 1470 l_current_data_ptr += 4; |
1506 | 1471 |
1507 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */ | 1472 opj_write_bytes(l_current_data_ptr, jp2->brand,4); /* BR */ |
1508 l_current_data_ptr += 4; | 1473 l_current_data_ptr += 4; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1599 | 1564 |
1600 /* further JP2 initializations go here */ | 1565 /* further JP2 initializations go here */ |
1601 jp2->color.jp2_has_colr = 0; | 1566 jp2->color.jp2_has_colr = 0; |
1602 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR
_CMAP_CDEF_FLAG; | 1567 jp2->ignore_pclr_cmap_cdef = parameters->flags & OPJ_DPARAMETERS_IGNORE_PCLR
_CMAP_CDEF_FLAG; |
1603 } | 1568 } |
1604 | 1569 |
1605 /* ----------------------------------------------------------------------- */ | 1570 /* ----------------------------------------------------------------------- */ |
1606 /* JP2 encoder interface */ | 1571 /* JP2 encoder interface */ |
1607 /* ----------------------------------------------------------------------- */ | 1572 /* ----------------------------------------------------------------------- */ |
1608 | 1573 |
1609 void opj_jp2_setup_encoder(» opj_jp2_t *jp2, | 1574 OPJ_BOOL opj_jp2_setup_encoder(»opj_jp2_t *jp2, |
1610 opj_cparameters_t *parameters, | 1575 opj_cparameters_t *parameters, |
1611 opj_image_t *image, | 1576 opj_image_t *image, |
1612 opj_event_mgr_t * p_manager) | 1577 opj_event_mgr_t * p_manager) |
1613 { | 1578 { |
1614 OPJ_UINT32 i; | 1579 OPJ_UINT32 i; |
1615 OPJ_UINT32 depth_0; | 1580 OPJ_UINT32 depth_0; |
1616 OPJ_UINT32 sign; | 1581 OPJ_UINT32 sign; |
1617 | 1582 |
1618 if(!jp2 || !parameters || !image) | 1583 if(!jp2 || !parameters || !image) |
1619 » » return; | 1584 » » return OPJ_FALSE; |
1620 | 1585 |
1621 /* setup the J2K codec */ | 1586 /* setup the J2K codec */ |
1622 /* ------------------- */ | 1587 /* ------------------- */ |
1623 | 1588 |
1624 /* Check if number of components respects standard */ | 1589 /* Check if number of components respects standard */ |
1625 if (image->numcomps < 1 || image->numcomps > 16384) { | 1590 if (image->numcomps < 1 || image->numcomps > 16384) { |
1626 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of component
s specified while setting up JP2 encoder\n"); | 1591 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of component
s specified while setting up JP2 encoder\n"); |
1627 » » return; | 1592 » » return OPJ_FALSE; |
1628 } | 1593 } |
1629 | 1594 |
1630 » opj_j2k_setup_encoder(jp2->j2k, parameters, image, p_manager ); | 1595 » if (opj_j2k_setup_encoder(jp2->j2k, parameters, image, p_manager ) == OP
J_FALSE) { |
| 1596 » » return OPJ_FALSE; |
| 1597 » } |
1631 | 1598 |
1632 /* setup the JP2 codec */ | 1599 /* setup the JP2 codec */ |
1633 /* ------------------- */ | 1600 /* ------------------- */ |
1634 | 1601 |
1635 /* Profile box */ | 1602 /* Profile box */ |
1636 | 1603 |
1637 jp2->brand = JP2_JP2; /* BR */ | 1604 jp2->brand = JP2_JP2; /* BR */ |
1638 jp2->minversion = 0; /* MinV */ | 1605 jp2->minversion = 0; /* MinV */ |
1639 jp2->numcl = 1; | 1606 jp2->numcl = 1; |
1640 jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32)); | 1607 jp2->cl = (OPJ_UINT32*) opj_malloc(jp2->numcl * sizeof(OPJ_UINT32)); |
1641 if (!jp2->cl){ | 1608 » if (!jp2->cl){ |
1642 jp2->cl = NULL; | 1609 » » jp2->cl = NULL; |
1643 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP
2 encoder\n"); | 1610 » » opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setu
p the JP2 encoder\n"); |
1644 return; | 1611 » » return OPJ_FALSE; |
1645 } | 1612 » } |
1646 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */ | 1613 jp2->cl[0] = JP2_JP2; /* CL0 : JP2 */ |
1647 | 1614 |
1648 /* Image Header box */ | 1615 /* Image Header box */ |
1649 | 1616 |
1650 jp2->numcomps = image->numcomps; /* NC */ | 1617 jp2->numcomps = image->numcomps; /* NC */ |
1651 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp
2_comps_t)); | 1618 jp2->comps = (opj_jp2_comps_t*) opj_malloc(jp2->numcomps * sizeof(opj_jp
2_comps_t)); |
1652 if (!jp2->comps) { | 1619 » if (!jp2->comps) { |
1653 jp2->comps = NULL; | 1620 » » jp2->comps = NULL; |
1654 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setup the JP
2 encoder\n"); | 1621 » » opj_event_msg(p_manager, EVT_ERROR, "Not enough memory when setu
p the JP2 encoder\n"); |
1655 return; | 1622 » » /* Memory of jp2->cl will be freed by opj_jp2_destroy */ |
1656 } | 1623 » » return OPJ_FALSE; |
| 1624 » } |
1657 | 1625 |
1658 jp2->h = image->y1 - image->y0; /* HEIGHT */ | 1626 jp2->h = image->y1 - image->y0; /* HEIGHT */ |
1659 jp2->w = image->x1 - image->x0; /* WIDTH */ | 1627 jp2->w = image->x1 - image->x0; /* WIDTH */ |
1660 /* BPC */ | 1628 /* BPC */ |
1661 depth_0 = image->comps[0].prec - 1; | 1629 depth_0 = image->comps[0].prec - 1; |
1662 sign = image->comps[0].sgnd; | 1630 sign = image->comps[0].sgnd; |
1663 jp2->bpc = depth_0 + (sign << 7); | 1631 jp2->bpc = depth_0 + (sign << 7); |
1664 for (i = 1; i < image->numcomps; i++) { | 1632 for (i = 1; i < image->numcomps; i++) { |
1665 OPJ_UINT32 depth = image->comps[i].prec - 1; | 1633 OPJ_UINT32 depth = image->comps[i].prec - 1; |
1666 sign = image->comps[i].sgnd; | 1634 sign = image->comps[i].sgnd; |
(...skipping 22 matching lines...) Expand all Loading... |
1689 jp2->enumcs = 17; /* greyscale */ | 1657 jp2->enumcs = 17; /* greyscale */ |
1690 else if (image->color_space == 3) | 1658 else if (image->color_space == 3) |
1691 jp2->enumcs = 18; /* YUV */ | 1659 jp2->enumcs = 18; /* YUV */ |
1692 } | 1660 } |
1693 | 1661 |
1694 | 1662 |
1695 jp2->precedence = 0; /* PRECEDENCE */ | 1663 jp2->precedence = 0; /* PRECEDENCE */ |
1696 jp2->approx = 0; /* APPROX */ | 1664 jp2->approx = 0; /* APPROX */ |
1697 | 1665 |
1698 jp2->jpip_on = parameters->jpip_on; | 1666 jp2->jpip_on = parameters->jpip_on; |
| 1667 |
| 1668 return OPJ_TRUE; |
1699 } | 1669 } |
1700 | 1670 |
1701 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2, | 1671 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2, |
1702 opj_stream_private_t *stream, | 1672 opj_stream_private_t *stream, |
1703 opj_event_mgr_t * p_manager) | 1673 opj_event_mgr_t * p_manager) |
1704 { | 1674 { |
1705 return opj_j2k_encode(jp2->j2k, stream, p_manager); | 1675 return opj_j2k_encode(jp2->j2k, stream, p_manager); |
1706 } | 1676 } |
1707 | 1677 |
1708 OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2, | 1678 OPJ_BOOL opj_jp2_end_decompress(opj_jp2_t *jp2, |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1839 const opj_jp2_header_handler_t * l_current_handler; | 1809 const opj_jp2_header_handler_t * l_current_handler; |
1840 OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE; | 1810 OPJ_UINT32 l_last_data_size = OPJ_BOX_SIZE; |
1841 OPJ_UINT32 l_current_data_size; | 1811 OPJ_UINT32 l_current_data_size; |
1842 OPJ_BYTE * l_current_data = 00; | 1812 OPJ_BYTE * l_current_data = 00; |
1843 | 1813 |
1844 /* preconditions */ | 1814 /* preconditions */ |
1845 assert(stream != 00); | 1815 assert(stream != 00); |
1846 assert(jp2 != 00); | 1816 assert(jp2 != 00); |
1847 assert(p_manager != 00); | 1817 assert(p_manager != 00); |
1848 | 1818 |
1849 » l_current_data = (OPJ_BYTE*)opj_malloc(l_last_data_size); | 1819 » l_current_data = (OPJ_BYTE*)opj_calloc(1,l_last_data_size); |
1850 | 1820 |
1851 if (l_current_data == 00) { | 1821 if (l_current_data == 00) { |
1852 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle
jpeg2000 file header\n"); | 1822 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle
jpeg2000 file header\n"); |
1853 return OPJ_FALSE; | 1823 return OPJ_FALSE; |
1854 } | 1824 } |
1855 memset(l_current_data, 0 , l_last_data_size); | |
1856 | 1825 |
1857 while (opj_jp2_read_boxhdr(&box,&l_nb_bytes_read,stream,p_manager)) { | 1826 while (opj_jp2_read_boxhdr(&box,&l_nb_bytes_read,stream,p_manager)) { |
1858 /* is it the codestream box ? */ | 1827 /* is it the codestream box ? */ |
1859 if (box.type == JP2_JP2C) { | 1828 if (box.type == JP2_JP2C) { |
1860 if (jp2->jp2_state & JP2_STATE_HEADER) { | 1829 if (jp2->jp2_state & JP2_STATE_HEADER) { |
1861 jp2->jp2_state |= JP2_STATE_CODESTREAM; | 1830 jp2->jp2_state |= JP2_STATE_CODESTREAM; |
1862 opj_free(l_current_data); | 1831 opj_free(l_current_data); |
1863 return OPJ_TRUE; | 1832 return OPJ_TRUE; |
1864 } | 1833 } |
1865 else { | 1834 else { |
1866 opj_event_msg(p_manager, EVT_ERROR, "bad placed
jpeg codestream\n"); | 1835 opj_event_msg(p_manager, EVT_ERROR, "bad placed
jpeg codestream\n"); |
1867 opj_free(l_current_data); | 1836 opj_free(l_current_data); |
1868 return OPJ_FALSE; | 1837 return OPJ_FALSE; |
1869 } | 1838 } |
1870 } | 1839 } |
1871 else if (box.length == 0) { | 1840 else if (box.length == 0) { |
1872 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box o
f undefined sizes\n"); | 1841 opj_event_msg(p_manager, EVT_ERROR, "Cannot handle box o
f undefined sizes\n"); |
1873 opj_free(l_current_data); | 1842 opj_free(l_current_data); |
1874 return OPJ_FALSE; | 1843 return OPJ_FALSE; |
1875 } | 1844 } |
1876 /* testcase 1851.pdf.SIGSEGV.ce9.948 */ | 1845 /* testcase 1851.pdf.SIGSEGV.ce9.948 */ |
1877 » » /* testcase K-5787457125613568 */ | 1846 » » else if»(box.length < l_nb_bytes_read) { |
1878 » » else if»(box.length < l_nb_bytes_read || box.length > opj_stream
_get_number_byte_left(stream)) { | |
1879 opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d
(%x)\n", box.length, box.type); | 1847 opj_event_msg(p_manager, EVT_ERROR, "invalid box size %d
(%x)\n", box.length, box.type); |
1880 opj_free(l_current_data); | 1848 opj_free(l_current_data); |
1881 return OPJ_FALSE; | 1849 return OPJ_FALSE; |
1882 } | 1850 } |
1883 | 1851 |
1884 l_current_handler = opj_jp2_find_handler(box.type); | 1852 l_current_handler = opj_jp2_find_handler(box.type); |
1885 l_current_data_size = box.length - l_nb_bytes_read; | 1853 l_current_data_size = box.length - l_nb_bytes_read; |
1886 | 1854 |
1887 if (l_current_handler != 00) { | 1855 if (l_current_handler != 00) { |
1888 if (l_current_data_size > l_last_data_size) { | 1856 if (l_current_data_size > l_last_data_size) { |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2116 | 2084 |
2117 /* the number of remaining bytes should be a multiple of 4 */ | 2085 /* the number of remaining bytes should be a multiple of 4 */ |
2118 if ((l_remaining_bytes & 0x3) != 0) { | 2086 if ((l_remaining_bytes & 0x3) != 0) { |
2119 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature B
ox size\n"); | 2087 opj_event_msg(p_manager, EVT_ERROR, "Error with FTYP signature B
ox size\n"); |
2120 return OPJ_FALSE; | 2088 return OPJ_FALSE; |
2121 } | 2089 } |
2122 | 2090 |
2123 /* div by 4 */ | 2091 /* div by 4 */ |
2124 jp2->numcl = l_remaining_bytes >> 2; | 2092 jp2->numcl = l_remaining_bytes >> 2; |
2125 if (jp2->numcl) { | 2093 if (jp2->numcl) { |
2126 » » jp2->cl = (OPJ_UINT32 *) opj_malloc(jp2->numcl * sizeof(OPJ_UINT
32)); | 2094 » » jp2->cl = (OPJ_UINT32 *) opj_calloc(jp2->numcl, sizeof(OPJ_UINT3
2)); |
2127 if (jp2->cl == 00) { | 2095 if (jp2->cl == 00) { |
2128 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory w
ith FTYP Box\n"); | 2096 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory w
ith FTYP Box\n"); |
2129 return OPJ_FALSE; | 2097 return OPJ_FALSE; |
2130 } | 2098 } |
2131 memset(jp2->cl,0,jp2->numcl * sizeof(OPJ_UINT32)); | |
2132 } | 2099 } |
2133 | 2100 |
2134 for (i = 0; i < jp2->numcl; ++i) | 2101 for (i = 0; i < jp2->numcl; ++i) |
2135 { | 2102 { |
2136 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi *
/ | 2103 opj_read_bytes(p_header_data,&jp2->cl[i],4); /* CLi *
/ |
2137 p_header_data += 4; | 2104 p_header_data += 4; |
2138 } | 2105 } |
2139 | 2106 |
2140 jp2->jp2_state |= JP2_STATE_FILE_TYPE; | 2107 jp2->jp2_state |= JP2_STATE_FILE_TYPE; |
2141 | 2108 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2218 opj_event_msg(p_manager, EVT_ERROR, "Stream error while
reading JP2 Header box\n"); | 2185 opj_event_msg(p_manager, EVT_ERROR, "Stream error while
reading JP2 Header box\n"); |
2219 return OPJ_FALSE; | 2186 return OPJ_FALSE; |
2220 } | 2187 } |
2221 | 2188 |
2222 if (box.length > p_header_size) { | 2189 if (box.length > p_header_size) { |
2223 opj_event_msg(p_manager, EVT_ERROR, "Stream error while
reading JP2 Header box: box length is inconsistent.\n"); | 2190 opj_event_msg(p_manager, EVT_ERROR, "Stream error while
reading JP2 Header box: box length is inconsistent.\n"); |
2224 return OPJ_FALSE; | 2191 return OPJ_FALSE; |
2225 } | 2192 } |
2226 | 2193 |
2227 l_current_handler = opj_jp2_img_find_handler(box.type); | 2194 l_current_handler = opj_jp2_img_find_handler(box.type); |
2228 //BUGID:0055999 | |
2229 //test file: fuzz-signal_sigsegv_6b88de_1123_2509.pdf | |
2230 if (box.length < l_box_size) return OPJ_FALSE; | |
2231 l_current_data_size = box.length - l_box_size; | 2195 l_current_data_size = box.length - l_box_size; |
2232 p_header_data += l_box_size; | 2196 p_header_data += l_box_size; |
2233 | 2197 |
2234 if (l_current_handler != 00) { | 2198 if (l_current_handler != 00) { |
2235 if (! l_current_handler->handler(jp2,p_header_data,l_cur
rent_data_size,p_manager)) { | 2199 if (! l_current_handler->handler(jp2,p_header_data,l_cur
rent_data_size,p_manager)) { |
2236 return OPJ_FALSE; | 2200 return OPJ_FALSE; |
2237 } | 2201 } |
2238 } | 2202 } |
2239 else { | 2203 else { |
2240 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN; | 2204 jp2->jp2_img_state |= JP2_IMG_STATE_UNKNOWN; |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2567 | 2531 |
2568 return OPJ_TRUE; | 2532 return OPJ_TRUE; |
2569 } | 2533 } |
2570 | 2534 |
2571 /* ----------------------------------------------------------------------- */ | 2535 /* ----------------------------------------------------------------------- */ |
2572 /* JP2 encoder interface */ | 2536 /* JP2 encoder interface */ |
2573 /* ----------------------------------------------------------------------- */ | 2537 /* ----------------------------------------------------------------------- */ |
2574 | 2538 |
2575 opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder) | 2539 opj_jp2_t* opj_jp2_create(OPJ_BOOL p_is_decoder) |
2576 { | 2540 { |
2577 » opj_jp2_t *jp2 = (opj_jp2_t*)opj_malloc(sizeof(opj_jp2_t)); | 2541 » opj_jp2_t *jp2 = (opj_jp2_t*)opj_calloc(1,sizeof(opj_jp2_t)); |
2578 if (jp2) { | 2542 if (jp2) { |
2579 memset(jp2,0,sizeof(opj_jp2_t)); | |
2580 | 2543 |
2581 /* create the J2K codec */ | 2544 /* create the J2K codec */ |
2582 if (! p_is_decoder) { | 2545 if (! p_is_decoder) { |
2583 jp2->j2k = opj_j2k_create_compress(); | 2546 jp2->j2k = opj_j2k_create_compress(); |
2584 } | 2547 } |
2585 else { | 2548 else { |
2586 jp2->j2k = opj_j2k_create_decompress(); | 2549 jp2->j2k = opj_j2k_create_decompress(); |
2587 } | 2550 } |
2588 | 2551 |
2589 if (jp2->j2k == 00) { | 2552 if (jp2->j2k == 00) { |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2816 len = opj_stream_tell(cio)-lenp; | 2779 len = opj_stream_tell(cio)-lenp; |
2817 opj_stream_skip(cio, lenp, p_manager); | 2780 opj_stream_skip(cio, lenp, p_manager); |
2818 opj_write_bytes(l_data_header,len,4);/* L */ | 2781 opj_write_bytes(l_data_header,len,4);/* L */ |
2819 opj_stream_write_data(cio,l_data_header,4,p_manager); | 2782 opj_stream_write_data(cio,l_data_header,4,p_manager); |
2820 opj_stream_seek(cio, lenp+len,p_manager); | 2783 opj_stream_seek(cio, lenp+len,p_manager); |
2821 | 2784 |
2822 return len; | 2785 return len; |
2823 } | 2786 } |
2824 #endif | 2787 #endif |
2825 #endif /* USE_JPIP */ | 2788 #endif /* USE_JPIP */ |
OLD | NEW |