Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/jp2.c

Issue 758593002: Update to openjpeg r2944 (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: tab Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 opj_event_mgr_t * p_manager ); 102 opj_event_mgr_t * p_manager );
103 103
104 static OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2, 104 static OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2,
105 OPJ_BYTE * p_cdef_header_data, 105 OPJ_BYTE * p_cdef_header_data,
106 OPJ_UINT 32 p_cdef_header_size, 106 OPJ_UINT 32 p_cdef_header_size,
107 opj_even t_mgr_t * p_manager ); 107 opj_even t_mgr_t * p_manager );
108 108
109 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color); 109 static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color);
110 110
111 /** 111 /**
112 * Writes the Channel Definition box.
113 *
114 * @param jp2 jpeg2000 file codec.
115 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
116 *
117 * @return the data being copied.
118 */
119 static OPJ_BYTE * opj_jp2_write_cdef( opj_jp2_t *jp2,
120 OPJ_UINT32 * p_ nb_bytes_written );
121
122 /**
112 * Writes the Colour Specification box. 123 * Writes the Colour Specification box.
113 * 124 *
114 * @param jp2 jpeg2000 file codec. 125 * @param jp2 jpeg2000 file codec.
115 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function. 126 * @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
116 * 127 *
117 * @return the data being copied. 128 * @return the data being copied.
118 */ 129 */
119 static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2, 130 static OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
120 OPJ_ UINT32 * p_nb_bytes_written ); 131 OPJ_ UINT32 * p_nb_bytes_written );
121 132
(...skipping 551 matching lines...) Expand 10 before | Expand all | Expand 10 after
673 } 684 }
674 685
675 /* read info for each component */ 686 /* read info for each component */
676 for (i = 0; i < jp2->numcomps; ++i) { 687 for (i = 0; i < jp2->numcomps; ++i) {
677 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */ 688 opj_read_bytes(p_bpc_header_data,&jp2->comps[i].bpcc ,1); /* read each BPCC component */
678 ++p_bpc_header_data; 689 ++p_bpc_header_data;
679 } 690 }
680 691
681 return OPJ_TRUE; 692 return OPJ_TRUE;
682 } 693 }
694 static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2, OPJ_UINT32 * p_nb_bytes_wri tten)
695 {
696 /* room for 8 bytes for box, 2 for n */
697 OPJ_UINT32 l_cdef_size = 10;
698 OPJ_BYTE * l_cdef_data,* l_current_cdef_ptr;
699 OPJ_UINT32 l_value;
700 OPJ_UINT16 i;
701
702 /* preconditions */
703 assert(jp2 != 00);
704 assert(p_nb_bytes_written != 00);
705 assert(jp2->color.jp2_cdef != 00);
706 assert(jp2->color.jp2_cdef->info != 00);
707 assert(jp2->color.jp2_cdef->n > 0U);
708
709 l_cdef_size += 6 * jp2->color.jp2_cdef->n;
710
711 l_cdef_data = (OPJ_BYTE *) opj_malloc(l_cdef_size);
712 if (l_cdef_data == 00) {
713 return 00;
714 }
715
716 l_current_cdef_ptr = l_cdef_data;
717
718 opj_write_bytes(l_current_cdef_ptr,l_cdef_size,4); /* write box size */
719 l_current_cdef_ptr += 4;
720
721 opj_write_bytes(l_current_cdef_ptr,JP2_CDEF,4); /* BPCC */
722 l_current_cdef_ptr += 4;
723
724 l_value = jp2->color.jp2_cdef->n;
725 opj_write_bytes(l_current_cdef_ptr,l_value,2); /* N */
726 l_current_cdef_ptr += 2;
727
728 for (i = 0U; i < jp2->color.jp2_cdef->n; ++i) {
729 l_value = jp2->color.jp2_cdef->info[i].cn;
730 opj_write_bytes(l_current_cdef_ptr,l_value,2); /* Cni */
731 l_current_cdef_ptr += 2;
732 l_value = jp2->color.jp2_cdef->info[i].typ;
733 opj_write_bytes(l_current_cdef_ptr,l_value,2); /* Typi */
734 l_current_cdef_ptr += 2;
735 l_value = jp2->color.jp2_cdef->info[i].asoc;
736 opj_write_bytes(l_current_cdef_ptr,l_value,2); /* Asoci */
737 l_current_cdef_ptr += 2;
738 }
739 *p_nb_bytes_written = l_cdef_size;
740
741 return l_cdef_data;
742 }
683 743
684 OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2, 744 OPJ_BYTE * opj_jp2_write_colr( opj_jp2_t *jp2,
685 OPJ_UINT32 * p_nb_by tes_written 745 OPJ_UINT32 * p_nb_by tes_written
686 ) 746 )
687 { 747 {
688 /* room for 8 bytes for box 3 for common data and variable upon profile* / 748 /* room for 8 bytes for box 3 for common data and variable upon profile* /
689 OPJ_UINT32 l_colr_size = 11; 749 OPJ_UINT32 l_colr_size = 11;
690 OPJ_BYTE * l_colr_data,* l_current_colr_ptr; 750 OPJ_BYTE * l_colr_data,* l_current_colr_ptr;
691 » 751
692 /* preconditions */ 752 /* preconditions */
693 assert(jp2 != 00); 753 assert(jp2 != 00);
694 assert(p_nb_bytes_written != 00); 754 assert(p_nb_bytes_written != 00);
695 assert(jp2->meth == 1 || jp2->meth == 2); 755 assert(jp2->meth == 1 || jp2->meth == 2);
696 756
697 switch (jp2->meth) { 757 switch (jp2->meth) {
698 case 1 : 758 case 1 :
699 l_colr_size += 4; /* EnumCS */ 759 l_colr_size += 4; /* EnumCS */
700 break; 760 break;
701 case 2 : 761 case 2 :
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 opj_jp2_cdef_info_t *info = color->jp2_cdef->info; 825 opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
766 OPJ_UINT16 n = color->jp2_cdef->n; 826 OPJ_UINT16 n = color->jp2_cdef->n;
767 OPJ_UINT32 nr_channels = image->numcomps; /* FIXME image->numcom ps == jp2->numcomps before color is applied ??? */ 827 OPJ_UINT32 nr_channels = image->numcomps; /* FIXME image->numcom ps == jp2->numcomps before color is applied ??? */
768 828
769 /* cdef applies to cmap channels if any */ 829 /* cdef applies to cmap channels if any */
770 if (color->jp2_pclr && color->jp2_pclr->cmap) { 830 if (color->jp2_pclr && color->jp2_pclr->cmap) {
771 nr_channels = (OPJ_UINT32)color->jp2_pclr->nr_channels; 831 nr_channels = (OPJ_UINT32)color->jp2_pclr->nr_channels;
772 } 832 }
773 833
774 for (i = 0; i < n; i++) { 834 for (i = 0; i < n; i++) {
775 » » » if (info[i].cn >= image->numcomps) { 835 » » » if (info[i].cn >= nr_channels) {
776 » » » » opj_event_msg(p_manager, EVT_ERROR, "Invalid com ponent index %d (>= %d).\n", info[i].cn, image->numcomps); 836 » » » » opj_event_msg(p_manager, EVT_ERROR, "Invalid com ponent index %d (>= %d).\n", info[i].cn, nr_channels);
777 return OPJ_FALSE; 837 return OPJ_FALSE;
778 } 838 }
779 » » » if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) > = image->numcomps) { 839 » » » if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) > = nr_channels) {
780 » » » » opj_event_msg(p_manager, EVT_ERROR, "Invalid com ponent index %d (>= %d).\n", info[i].asoc - 1, image->numcomps); 840 » » » » opj_event_msg(p_manager, EVT_ERROR, "Invalid com ponent index %d (>= %d).\n", info[i].asoc - 1, nr_channels);
781 return OPJ_FALSE; 841 return OPJ_FALSE;
782 } 842 }
783 } 843 }
784 844
785 /* issue 397 */ 845 /* issue 397 */
786 /* ISO 15444-1 states that if cdef is present, it shall contain a complete list of channel definitions. */ 846 /* ISO 15444-1 states that if cdef is present, it shall contain a complete list of channel definitions. */
787 while (nr_channels > 0) 847 while (nr_channels > 0)
788 { 848 {
789 for(i = 0; i < n; ++i) { 849 for(i = 0; i < n; ++i) {
790 if ((OPJ_UINT32)info[i].cn == (nr_channels - 1U) ) { 850 if ((OPJ_UINT32)info[i].cn == (nr_channels - 1U) ) {
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
975 1035
976 if(jp2->color.jp2_pclr) 1036 if(jp2->color.jp2_pclr)
977 return OPJ_FALSE; 1037 return OPJ_FALSE;
978 1038
979 if (p_pclr_header_size < 3) 1039 if (p_pclr_header_size < 3)
980 return OPJ_FALSE; 1040 return OPJ_FALSE;
981 1041
982 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */ 1042 opj_read_bytes(p_pclr_header_data, &l_value , 2); /* NE */
983 p_pclr_header_data += 2; 1043 p_pclr_header_data += 2;
984 nr_entries = (OPJ_UINT16) l_value; 1044 nr_entries = (OPJ_UINT16) l_value;
1045 if ((nr_entries == 0U) || (nr_entries > 1024U)) {
1046 opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports % d entries\n", (int)nr_entries);
1047 return OPJ_FALSE;
1048 }
985 1049
986 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */ 1050 opj_read_bytes(p_pclr_header_data, &l_value , 1); /* NPC */
987 ++p_pclr_header_data; 1051 ++p_pclr_header_data;
988 nr_channels = (OPJ_UINT16) l_value; 1052 nr_channels = (OPJ_UINT16) l_value;
1053 if (nr_channels == 0U) {
1054 opj_event_msg(p_manager, EVT_ERROR, "Invalid PCLR box. Reports 0 palette columns\n");
1055 return OPJ_FALSE;
1056 }
989 1057
990 » if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels || nr_channels == 0 || nr_entries >= (OPJ_UINT32)-1 / nr_channels) 1058 » if (p_pclr_header_size < 3 + (OPJ_UINT32)nr_channels)
991 return OPJ_FALSE; 1059 return OPJ_FALSE;
992 1060
993 entries = (OPJ_UINT32*) opj_malloc((size_t)nr_channels * nr_entries * si zeof(OPJ_UINT32)); 1061 entries = (OPJ_UINT32*) opj_malloc((size_t)nr_channels * nr_entries * si zeof(OPJ_UINT32));
994 if (!entries) 1062 if (!entries)
995 return OPJ_FALSE; 1063 return OPJ_FALSE;
996 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels); 1064 channel_size = (OPJ_BYTE*) opj_malloc(nr_channels);
997 if (!channel_size) 1065 if (!channel_size)
998 { 1066 {
999 opj_free(entries); 1067 opj_free(entries);
1000 return OPJ_FALSE; 1068 return OPJ_FALSE;
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
1113 } 1181 }
1114 1182
1115 void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color) 1183 void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color)
1116 { 1184 {
1117 opj_jp2_cdef_info_t *info; 1185 opj_jp2_cdef_info_t *info;
1118 OPJ_UINT16 i, n, cn, asoc, acn; 1186 OPJ_UINT16 i, n, cn, asoc, acn;
1119 1187
1120 info = color->jp2_cdef->info; 1188 info = color->jp2_cdef->info;
1121 n = color->jp2_cdef->n; 1189 n = color->jp2_cdef->n;
1122 1190
1123 for(i = 0; i < n; ++i) 1191 » for(i = 0; i < n; ++i)
1124 { 1192 » {
1125 /* WATCH: acn = asoc - 1 ! */ 1193 » » /* WATCH: acn = asoc - 1 ! */
1126 asoc = info[i].asoc; 1194 » » asoc = info[i].asoc;
1127 if(asoc == 0 || asoc == 65535) 1195 » » cn = info[i].cn;
1128 {
1129 if (i < image->numcomps)
1130 image->comps[i].alpha = info[i].typ;
1131 continue;
1132 }
1133 1196
1134 cn = info[i].cn; 1197 » » if( cn >= image->numcomps)
1135 acn = (OPJ_UINT16)(asoc - 1); 1198 » » {
1136 if( cn >= image->numcomps || acn >= image->numcomps ) 1199 » » » fprintf(stderr, "cn=%d, numcomps=%d\n", cn, image->numco mps);
1137 { 1200 » » » continue;
1138 fprintf(stderr, "cn=%d, acn=%d, numcomps=%d\n", cn, acn, image->numcomps); 1201 » » }
1139 continue; 1202 » » if(asoc == 0 || asoc == 65535)
1140 } 1203 » » {
1204 » » » image->comps[cn].alpha = info[i].typ;
1205 » » » continue;
1206 » » }
1141 1207
1142 » » if(cn != acn) 1208 » » acn = (OPJ_UINT16)(asoc - 1);
1209 » » if( acn >= image->numcomps )
1210 » » {
1211 » » » fprintf(stderr, "acn=%d, numcomps=%d\n", acn, image->num comps);
1212 » » » continue;
1213 » » }
1214
1215 » » /* Swap only if color channel */
1216 » » if((cn != acn) && (info[i].typ == 0))
1143 { 1217 {
1144 opj_image_comp_t saved; 1218 opj_image_comp_t saved;
1219 OPJ_UINT16 j;
1145 1220
1146 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_ t)); 1221 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_ t));
1147 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj _image_comp_t)); 1222 memcpy(&image->comps[cn], &image->comps[acn], sizeof(opj _image_comp_t));
1148 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp _t)); 1223 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp _t));
1149 1224
1150 » » » info[i].asoc = (OPJ_UINT16)(cn + 1); 1225 » » » /* Swap channels in following channel definitions, don't bother with j <= i that are already processed */
1151 » » » info[acn].asoc = (OPJ_UINT16)(info[acn].cn + 1); 1226 » » » for (j = i + 1; j < n ; ++j)
1227 » » » {
1228 » » » » if (info[j].cn == cn) {
1229 » » » » » info[j].cn = acn;
1230 » » » » }
1231 » » » » else if (info[j].cn == acn) {
1232 » » » » » info[j].cn = cn;
1233 » » » » }
1234 » » » » /* asoc is related to color index. Do not update . */
1235 » » » }
1152 } 1236 }
1153 1237
1154 image->comps[cn].alpha = info[i].typ; 1238 image->comps[cn].alpha = info[i].typ;
1155 } 1239 }
1156 1240
1157 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info); 1241 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info);
1158 1242
1159 opj_free(color->jp2_cdef); color->jp2_cdef = NULL; 1243 opj_free(color->jp2_cdef); color->jp2_cdef = NULL;
1160 1244
1161 }/* jp2_apply_cdef() */ 1245 }/* jp2_apply_cdef() */
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
1334 p_image->color_space = OPJ_CLRSPC_SRGB; 1418 p_image->color_space = OPJ_CLRSPC_SRGB;
1335 else if (jp2->enumcs == 17) 1419 else if (jp2->enumcs == 17)
1336 p_image->color_space = OPJ_CLRSPC_GRAY; 1420 p_image->color_space = OPJ_CLRSPC_GRAY;
1337 else if (jp2->enumcs == 18) 1421 else if (jp2->enumcs == 18)
1338 p_image->color_space = OPJ_CLRSPC_SYCC; 1422 p_image->color_space = OPJ_CLRSPC_SYCC;
1339 else if (jp2->enumcs == 24) 1423 else if (jp2->enumcs == 24)
1340 p_image->color_space = OPJ_CLRSPC_EYCC; 1424 p_image->color_space = OPJ_CLRSPC_EYCC;
1341 else 1425 else
1342 p_image->color_space = OPJ_CLRSPC_UNKNOWN; 1426 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
1343 1427
1344 /* Apply the color space if needed */
1345 if(jp2->color.jp2_cdef) {
1346 opj_jp2_apply_cdef(p_image, &(jp2->color));
1347 }
1348
1349 if(jp2->color.jp2_pclr) { 1428 if(jp2->color.jp2_pclr) {
1350 /* Part 1, I.5.3.4: Either both or none : */ 1429 /* Part 1, I.5.3.4: Either both or none : */
1351 if( !jp2->color.jp2_pclr->cmap) 1430 if( !jp2->color.jp2_pclr->cmap)
1352 opj_jp2_free_pclr(&(jp2->color)); 1431 opj_jp2_free_pclr(&(jp2->color));
1353 else 1432 else
1354 opj_jp2_apply_pclr(p_image, &(jp2->color)); 1433 opj_jp2_apply_pclr(p_image, &(jp2->color));
1355 } 1434 }
1356 1435
1436 /* Apply the color space if needed */
1437 if(jp2->color.jp2_cdef) {
1438 opj_jp2_apply_cdef(p_image, &(jp2->color));
1439 }
1440
1357 if(jp2->color.icc_profile_buf) { 1441 if(jp2->color.icc_profile_buf) {
1358 p_image->icc_profile_buf = jp2->color.icc_profile_buf; 1442 p_image->icc_profile_buf = jp2->color.icc_profile_buf;
1359 p_image->icc_profile_len = jp2->color.icc_profile_len; 1443 p_image->icc_profile_len = jp2->color.icc_profile_len;
1360 jp2->color.icc_profile_buf = NULL; 1444 jp2->color.icc_profile_buf = NULL;
1361 } 1445 }
1362 } 1446 }
1363 1447
1364 return OPJ_TRUE; 1448 return OPJ_TRUE;
1365 } 1449 }
1366 1450
1367 OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2, 1451 OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
1368 opj_stream_private_t *stream, 1452 opj_stream_private_t *stream,
1369 opj_event_mgr_t * p_manager 1453 opj_event_mgr_t * p_manager
1370 ) 1454 )
1371 { 1455 {
1372 » opj_jp2_img_header_writer_handler_t l_writers [3]; 1456 » opj_jp2_img_header_writer_handler_t l_writers [4];
1373 opj_jp2_img_header_writer_handler_t * l_current_writer; 1457 opj_jp2_img_header_writer_handler_t * l_current_writer;
1374 1458
1375 OPJ_INT32 i, l_nb_pass; 1459 OPJ_INT32 i, l_nb_pass;
1376 /* size of data for super box*/ 1460 /* size of data for super box*/
1377 OPJ_UINT32 l_jp2h_size = 8; 1461 OPJ_UINT32 l_jp2h_size = 8;
1378 OPJ_BOOL l_result = OPJ_TRUE; 1462 OPJ_BOOL l_result = OPJ_TRUE;
1379 1463
1380 /* to store the data of the super box */ 1464 /* to store the data of the super box */
1381 OPJ_BYTE l_jp2h_data [8]; 1465 OPJ_BYTE l_jp2h_data [8];
1382 1466
1383 /* preconditions */ 1467 /* preconditions */
1384 assert(stream != 00); 1468 assert(stream != 00);
1385 assert(jp2 != 00); 1469 assert(jp2 != 00);
1386 assert(p_manager != 00); 1470 assert(p_manager != 00);
1387 1471
1388 memset(l_writers,0,sizeof(l_writers)); 1472 memset(l_writers,0,sizeof(l_writers));
1389 1473
1390 if (jp2->bpc == 255) { 1474 if (jp2->bpc == 255) {
1391 l_nb_pass = 3; 1475 l_nb_pass = 3;
1392 l_writers[0].handler = opj_jp2_write_ihdr; 1476 l_writers[0].handler = opj_jp2_write_ihdr;
1393 l_writers[1].handler = opj_jp2_write_bpcc; 1477 l_writers[1].handler = opj_jp2_write_bpcc;
1394 l_writers[2].handler = opj_jp2_write_colr; 1478 l_writers[2].handler = opj_jp2_write_colr;
1395 } 1479 }
1396 else { 1480 else {
1397 l_nb_pass = 2; 1481 l_nb_pass = 2;
1398 l_writers[0].handler = opj_jp2_write_ihdr; 1482 l_writers[0].handler = opj_jp2_write_ihdr;
1399 l_writers[1].handler = opj_jp2_write_colr; 1483 l_writers[1].handler = opj_jp2_write_colr;
1400 } 1484 }
1401 1485
1486 if (jp2->color.jp2_cdef != NULL) {
1487 l_writers[l_nb_pass].handler = opj_jp2_write_cdef;
1488 l_nb_pass++;
1489 }
1490
1402 /* write box header */ 1491 /* write box header */
1403 /* write JP2H type */ 1492 /* write JP2H type */
1404 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4); 1493 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4);
1405 1494
1406 l_current_writer = l_writers; 1495 l_current_writer = l_writers;
1407 for (i=0;i<l_nb_pass;++i) { 1496 for (i=0;i<l_nb_pass;++i) {
1408 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_cur rent_writer->m_size)); 1497 l_current_writer->m_data = l_current_writer->handler(jp2,&(l_cur rent_writer->m_size));
1409 if (l_current_writer->m_data == 00) { 1498 if (l_current_writer->m_data == 00) {
1410 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory t o hold JP2 Header data\n"); 1499 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory t o hold JP2 Header data\n");
1411 l_result = OPJ_FALSE; 1500 l_result = OPJ_FALSE;
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
1591 1680
1592 /* ----------------------------------------------------------------------- */ 1681 /* ----------------------------------------------------------------------- */
1593 /* JP2 encoder interface */ 1682 /* JP2 encoder interface */
1594 /* ----------------------------------------------------------------------- */ 1683 /* ----------------------------------------------------------------------- */
1595 1684
1596 OPJ_BOOL opj_jp2_setup_encoder( opj_jp2_t *jp2, 1685 OPJ_BOOL opj_jp2_setup_encoder( opj_jp2_t *jp2,
1597 opj_cparameters_t *parameters, 1686 opj_cparameters_t *parameters,
1598 opj_image_t *image, 1687 opj_image_t *image,
1599 opj_event_mgr_t * p_manager) 1688 opj_event_mgr_t * p_manager)
1600 { 1689 {
1601 OPJ_UINT32 i; 1690 » OPJ_UINT32 i;
1602 OPJ_UINT32 depth_0; 1691 OPJ_UINT32 depth_0;
1603 OPJ_UINT32 sign; 1692 OPJ_UINT32 sign;
1693 OPJ_UINT32 alpha_count;
1694 OPJ_UINT32 color_channels = 0U;
1695 OPJ_UINT32 alpha_channel = 0U;
1696
1604 1697
1605 if(!jp2 || !parameters || !image) 1698 if(!jp2 || !parameters || !image)
1606 return OPJ_FALSE; 1699 return OPJ_FALSE;
1607 1700
1608 /* setup the J2K codec */ 1701 /* setup the J2K codec */
1609 /* ------------------- */ 1702 /* ------------------- */
1610 1703
1611 /* Check if number of components respects standard */ 1704 /* Check if number of components respects standard */
1612 if (image->numcomps < 1 || image->numcomps > 16384) { 1705 if (image->numcomps < 1 || image->numcomps > 16384) {
1613 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of component s specified while setting up JP2 encoder\n"); 1706 opj_event_msg(p_manager, EVT_ERROR, "Invalid number of component s specified while setting up JP2 encoder\n");
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1674 else { 1767 else {
1675 jp2->meth = 1; 1768 jp2->meth = 1;
1676 if (image->color_space == 1) 1769 if (image->color_space == 1)
1677 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2-1 */ 1770 jp2->enumcs = 16; /* sRGB as defined by IEC 61966-2-1 */
1678 else if (image->color_space == 2) 1771 else if (image->color_space == 2)
1679 jp2->enumcs = 17; /* greyscale */ 1772 jp2->enumcs = 17; /* greyscale */
1680 else if (image->color_space == 3) 1773 else if (image->color_space == 3)
1681 jp2->enumcs = 18; /* YUV */ 1774 jp2->enumcs = 18; /* YUV */
1682 } 1775 }
1683 1776
1777 /* Channel Definition box */
1778 /* FIXME not provided by parameters */
1779 /* We try to do what we can... */
1780 alpha_count = 0U;
1781 for (i = 0; i < image->numcomps; i++) {
1782 if (image->comps[i].alpha != 0) {
1783 alpha_count++;
1784 alpha_channel = i;
1785 }
1786 }
1787 if (alpha_count == 1U) { /* no way to deal with more than 1 alpha channe l */
1788 switch (jp2->enumcs) {
1789 case 16:
1790 case 18:
1791 color_channels = 3;
1792 break;
1793 case 17:
1794 color_channels = 1;
1795 break;
1796 default:
1797 alpha_count = 0U;
1798 break;
1799 }
1800 if (alpha_count == 0U) {
1801 opj_event_msg(p_manager, EVT_WARNING, "Alpha channel spe cified but unknown enumcs. No cdef box will be created.\n");
1802 } else if (image->numcomps < (color_channels+1)) {
1803 opj_event_msg(p_manager, EVT_WARNING, "Alpha channel spe cified but not enough image components for an automatic cdef box creation.\n");
1804 alpha_count = 0U;
1805 } else if ((OPJ_UINT32)alpha_channel < color_channels) {
1806 opj_event_msg(p_manager, EVT_WARNING, "Alpha channel pos ition conflicts with color channel. No cdef box will be created.\n");
1807 alpha_count = 0U;
1808 }
1809 } else if (alpha_count > 1) {
1810 opj_event_msg(p_manager, EVT_WARNING, "Multiple alpha channels s pecified. No cdef box will be created.\n");
1811 }
1812 if (alpha_count == 1U) { /* if here, we know what we can do */
1813 jp2->color.jp2_cdef = (opj_jp2_cdef_t*)opj_malloc(sizeof(opj_jp2 _cdef_t));
1814 if(!jp2->color.jp2_cdef) {
1815 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory t o setup the JP2 encoder\n");
1816 return OPJ_FALSE;
1817 }
1818 /* no memset needed, all values will be overwritten except if jp 2->color.jp2_cdef->info allocation fails, */
1819 /* in which case jp2->color.jp2_cdef->info will be NULL => valid for destruction */
1820 jp2->color.jp2_cdef->info = (opj_jp2_cdef_info_t*) opj_malloc(im age->numcomps * sizeof(opj_jp2_cdef_info_t));
1821 if (!jp2->color.jp2_cdef->info) {
1822 /* memory will be freed by opj_jp2_destroy */
1823 opj_event_msg(p_manager, EVT_ERROR, "Not enough memory t o setup the JP2 encoder\n");
1824 return OPJ_FALSE;
1825 }
1826 jp2->color.jp2_cdef->n = (OPJ_UINT16) image->numcomps; /* cast i s valid : image->numcomps [1,16384] */
1827 for (i = 0U; i < color_channels; i++) {
1828 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)i; /* cast is valid : image->numcomps [1,16384] */
1829 jp2->color.jp2_cdef->info[i].typ = 0U;
1830 jp2->color.jp2_cdef->info[i].asoc = (OPJ_UINT16)(i+1U); /* No overflow + cast is valid : image->numcomps [1,16384] */
1831 }
1832 for (; i < image->numcomps; i++) {
1833 if (image->comps[i].alpha != 0) { /* we'll be here exact ly once */
1834 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)i; /* cast is valid : image->numcomps [1,16384] */
1835 jp2->color.jp2_cdef->info[i].typ = 1U; /* Opacit y channel */
1836 jp2->color.jp2_cdef->info[i].asoc = 0U; /* Apply alpha channel to the whole image */
1837 } else {
1838 /* Unknown channel */
1839 jp2->color.jp2_cdef->info[i].cn = (OPJ_UINT16)i; /* cast is valid : image->numcomps [1,16384] */
1840 jp2->color.jp2_cdef->info[i].typ = 65535U;
1841 jp2->color.jp2_cdef->info[i].asoc = 65535U;
1842 }
1843 }
1844 }
1684 1845
1685 jp2->precedence = 0; /* PRECEDENCE */ 1846 jp2->precedence = 0; /* PRECEDENCE */
1686 jp2->approx = 0; /* APPROX */ 1847 jp2->approx = 0; /* APPROX */
1687 1848
1688 jp2->jpip_on = parameters->jpip_on; 1849 jp2->jpip_on = parameters->jpip_on;
1689 1850
1690 return OPJ_TRUE; 1851 return OPJ_TRUE;
1691 } 1852 }
1692 1853
1693 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2, 1854 OPJ_BOOL opj_jp2_encode(opj_jp2_t *jp2,
(...skipping 850 matching lines...) Expand 10 before | Expand all | Expand 10 after
2544 /* Set Image Color Space */ 2705 /* Set Image Color Space */
2545 if (p_jp2->enumcs == 16) 2706 if (p_jp2->enumcs == 16)
2546 p_image->color_space = OPJ_CLRSPC_SRGB; 2707 p_image->color_space = OPJ_CLRSPC_SRGB;
2547 else if (p_jp2->enumcs == 17) 2708 else if (p_jp2->enumcs == 17)
2548 p_image->color_space = OPJ_CLRSPC_GRAY; 2709 p_image->color_space = OPJ_CLRSPC_GRAY;
2549 else if (p_jp2->enumcs == 18) 2710 else if (p_jp2->enumcs == 18)
2550 p_image->color_space = OPJ_CLRSPC_SYCC; 2711 p_image->color_space = OPJ_CLRSPC_SYCC;
2551 else 2712 else
2552 p_image->color_space = OPJ_CLRSPC_UNKNOWN; 2713 p_image->color_space = OPJ_CLRSPC_UNKNOWN;
2553 2714
2554 /* Apply the color space if needed */
2555 if(p_jp2->color.jp2_cdef) {
2556 opj_jp2_apply_cdef(p_image, &(p_jp2->color));
2557 }
2558
2559 if(p_jp2->color.jp2_pclr) { 2715 if(p_jp2->color.jp2_pclr) {
2560 /* Part 1, I.5.3.4: Either both or none : */ 2716 /* Part 1, I.5.3.4: Either both or none : */
2561 if( !p_jp2->color.jp2_pclr->cmap) 2717 if( !p_jp2->color.jp2_pclr->cmap)
2562 opj_jp2_free_pclr(&(p_jp2->color)); 2718 opj_jp2_free_pclr(&(p_jp2->color));
2563 else 2719 else
2564 opj_jp2_apply_pclr(p_image, &(p_jp2->color)); 2720 opj_jp2_apply_pclr(p_image, &(p_jp2->color));
2565 } 2721 }
2566 2722
2723 /* Apply the color space if needed */
2724 if(p_jp2->color.jp2_cdef) {
2725 opj_jp2_apply_cdef(p_image, &(p_jp2->color));
2726 }
2727
2567 if(p_jp2->color.icc_profile_buf) { 2728 if(p_jp2->color.icc_profile_buf) {
2568 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf; 2729 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf;
2569 p_image->icc_profile_len = p_jp2->color.icc_profile_len; 2730 p_image->icc_profile_len = p_jp2->color.icc_profile_len;
2570 p_jp2->color.icc_profile_buf = NULL; 2731 p_jp2->color.icc_profile_buf = NULL;
2571 } 2732 }
2572 2733
2573 return OPJ_TRUE; 2734 return OPJ_TRUE;
2574 } 2735 }
2575 2736
2576 /* ----------------------------------------------------------------------- */ 2737 /* ----------------------------------------------------------------------- */
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
2820 len = opj_stream_tell(cio)-lenp; 2981 len = opj_stream_tell(cio)-lenp;
2821 opj_stream_skip(cio, lenp, p_manager); 2982 opj_stream_skip(cio, lenp, p_manager);
2822 opj_write_bytes(l_data_header,len,4);/* L */ 2983 opj_write_bytes(l_data_header,len,4);/* L */
2823 opj_stream_write_data(cio,l_data_header,4,p_manager); 2984 opj_stream_write_data(cio,l_data_header,4,p_manager);
2824 opj_stream_seek(cio, lenp+len,p_manager); 2985 opj_stream_seek(cio, lenp+len,p_manager);
2825 2986
2826 return len; 2987 return len;
2827 } 2988 }
2828 #endif 2989 #endif
2829 #endif /* USE_JPIP */ 2990 #endif /* USE_JPIP */
OLDNEW
« no previous file with comments | « core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/j2k.c ('k') | core/src/fxcodec/fx_libopenjpeg/libopenjpeg20/pi.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698