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 688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
699 OPJ_UINT32 l_value; | 699 OPJ_UINT32 l_value; |
700 OPJ_UINT16 i; | 700 OPJ_UINT16 i; |
701 | 701 |
702 /* preconditions */ | 702 /* preconditions */ |
703 assert(jp2 != 00); | 703 assert(jp2 != 00); |
704 assert(p_nb_bytes_written != 00); | 704 assert(p_nb_bytes_written != 00); |
705 assert(jp2->color.jp2_cdef != 00); | 705 assert(jp2->color.jp2_cdef != 00); |
706 assert(jp2->color.jp2_cdef->info != 00); | 706 assert(jp2->color.jp2_cdef->info != 00); |
707 assert(jp2->color.jp2_cdef->n > 0U); | 707 assert(jp2->color.jp2_cdef->n > 0U); |
708 | 708 |
709 » l_cdef_size += 6 * jp2->color.jp2_cdef->n; | 709 » l_cdef_size += 6U * jp2->color.jp2_cdef->n; |
710 | 710 |
711 l_cdef_data = (OPJ_BYTE *) opj_malloc(l_cdef_size); | 711 l_cdef_data = (OPJ_BYTE *) opj_malloc(l_cdef_size); |
712 if (l_cdef_data == 00) { | 712 if (l_cdef_data == 00) { |
713 return 00; | 713 return 00; |
714 } | 714 } |
715 | 715 |
716 l_current_cdef_ptr = l_cdef_data; | 716 l_current_cdef_ptr = l_cdef_data; |
717 | 717 » |
718 opj_write_bytes(l_current_cdef_ptr,l_cdef_size,4);
/* write box size */ | 718 opj_write_bytes(l_current_cdef_ptr,l_cdef_size,4);
/* write box size */ |
719 l_current_cdef_ptr += 4; | 719 l_current_cdef_ptr += 4; |
720 | 720 |
721 opj_write_bytes(l_current_cdef_ptr,JP2_CDEF,4);
/* BPCC */ | 721 opj_write_bytes(l_current_cdef_ptr,JP2_CDEF,4);
/* BPCC */ |
722 l_current_cdef_ptr += 4; | 722 l_current_cdef_ptr += 4; |
723 | 723 |
724 l_value = jp2->color.jp2_cdef->n; | 724 l_value = jp2->color.jp2_cdef->n; |
725 opj_write_bytes(l_current_cdef_ptr,l_value,2);
/* N */ | 725 opj_write_bytes(l_current_cdef_ptr,l_value,2);
/* N */ |
726 l_current_cdef_ptr += 2; | 726 l_current_cdef_ptr += 2; |
727 | 727 |
(...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1177 | 1177 |
1178 jp2->color.jp2_pclr->cmap = cmap; | 1178 jp2->color.jp2_pclr->cmap = cmap; |
1179 | 1179 |
1180 return OPJ_TRUE; | 1180 return OPJ_TRUE; |
1181 } | 1181 } |
1182 | 1182 |
1183 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) |
1184 { | 1184 { |
1185 opj_jp2_cdef_info_t *info; | 1185 opj_jp2_cdef_info_t *info; |
1186 OPJ_UINT16 i, n, cn, asoc, acn; | 1186 OPJ_UINT16 i, n, cn, asoc, acn; |
1187 | 1187 » |
1188 info = color->jp2_cdef->info; | 1188 info = color->jp2_cdef->info; |
1189 n = color->jp2_cdef->n; | 1189 n = color->jp2_cdef->n; |
1190 | 1190 » |
1191 for(i = 0; i < n; ++i) | 1191 for(i = 0; i < n; ++i) |
1192 { | 1192 { |
1193 /* WATCH: acn = asoc - 1 ! */ | 1193 /* WATCH: acn = asoc - 1 ! */ |
1194 asoc = info[i].asoc; | 1194 asoc = info[i].asoc; |
1195 cn = info[i].cn; | 1195 cn = info[i].cn; |
1196 | 1196 » » |
1197 if( cn >= image->numcomps) | 1197 if( cn >= image->numcomps) |
1198 { | 1198 { |
1199 fprintf(stderr, "cn=%d, numcomps=%d\n", cn, image->numco
mps); | 1199 fprintf(stderr, "cn=%d, numcomps=%d\n", cn, image->numco
mps); |
1200 continue; | 1200 continue; |
1201 } | 1201 } |
1202 if(asoc == 0 || asoc == 65535) | 1202 if(asoc == 0 || asoc == 65535) |
1203 { | 1203 { |
1204 image->comps[cn].alpha = info[i].typ; | 1204 image->comps[cn].alpha = info[i].typ; |
1205 continue; | 1205 continue; |
1206 } | 1206 } |
1207 | 1207 » » |
1208 acn = (OPJ_UINT16)(asoc - 1); | 1208 acn = (OPJ_UINT16)(asoc - 1); |
1209 if( acn >= image->numcomps ) | 1209 if( acn >= image->numcomps ) |
1210 { | 1210 { |
1211 fprintf(stderr, "acn=%d, numcomps=%d\n", acn, image->num
comps); | 1211 fprintf(stderr, "acn=%d, numcomps=%d\n", acn, image->num
comps); |
1212 continue; | 1212 continue; |
1213 } | 1213 } |
1214 | 1214 » » |
1215 /* Swap only if color channel */ | 1215 /* Swap only if color channel */ |
1216 if((cn != acn) && (info[i].typ == 0)) | 1216 if((cn != acn) && (info[i].typ == 0)) |
1217 { | 1217 { |
1218 opj_image_comp_t saved; | 1218 opj_image_comp_t saved; |
1219 OPJ_UINT16 j; | 1219 OPJ_UINT16 j; |
1220 | 1220 » » » |
1221 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_
t)); | 1221 memcpy(&saved, &image->comps[cn], sizeof(opj_image_comp_
t)); |
1222 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)); |
1223 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp
_t)); | 1223 memcpy(&image->comps[acn], &saved, sizeof(opj_image_comp
_t)); |
1224 | 1224 » » » |
1225 /* Swap channels in following channel definitions, don't
bother with j <= i that are already processed */ | 1225 /* Swap channels in following channel definitions, don't
bother with j <= i that are already processed */ |
1226 » » » for (j = i + 1; j < n ; ++j) | 1226 » » » for (j = (OPJ_UINT16)(i + 1U); j < n ; ++j) |
1227 { | 1227 { |
1228 if (info[j].cn == cn) { | 1228 if (info[j].cn == cn) { |
1229 info[j].cn = acn; | 1229 info[j].cn = acn; |
1230 } | 1230 } |
1231 else if (info[j].cn == acn) { | 1231 else if (info[j].cn == acn) { |
1232 info[j].cn = cn; | 1232 info[j].cn = cn; |
1233 } | 1233 } |
1234 /* asoc is related to color index. Do not update
. */ | 1234 /* asoc is related to color index. Do not update
. */ |
1235 } | 1235 } |
1236 } | 1236 } |
1237 | 1237 » » |
1238 image->comps[cn].alpha = info[i].typ; | 1238 image->comps[cn].alpha = info[i].typ; |
1239 } | 1239 } |
1240 | 1240 » |
1241 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info); | 1241 if(color->jp2_cdef->info) opj_free(color->jp2_cdef->info); |
1242 | 1242 » |
1243 opj_free(color->jp2_cdef); color->jp2_cdef = NULL; | 1243 opj_free(color->jp2_cdef); color->jp2_cdef = NULL; |
1244 | 1244 » |
1245 }/* jp2_apply_cdef() */ | 1245 }/* jp2_apply_cdef() */ |
1246 | 1246 |
1247 OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2, | 1247 OPJ_BOOL opj_jp2_read_cdef( opj_jp2_t * jp2, |
1248 OPJ_BYTE * p_cdef_header_data, | 1248 OPJ_BYTE * p_cdef_header_data, |
1249 OPJ_UINT32 p_cdef_header
_size, | 1249 OPJ_UINT32 p_cdef_header
_size, |
1250 opj_event_mgr_t * p_mana
ger | 1250 opj_event_mgr_t * p_mana
ger |
1251 ) | 1251 ) |
1252 { | 1252 { |
1253 opj_jp2_cdef_info_t *cdef_info; | 1253 opj_jp2_cdef_info_t *cdef_info; |
1254 OPJ_UINT16 i; | 1254 OPJ_UINT16 i; |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1480 else { | 1480 else { |
1481 l_nb_pass = 2; | 1481 l_nb_pass = 2; |
1482 l_writers[0].handler = opj_jp2_write_ihdr; | 1482 l_writers[0].handler = opj_jp2_write_ihdr; |
1483 l_writers[1].handler = opj_jp2_write_colr; | 1483 l_writers[1].handler = opj_jp2_write_colr; |
1484 } | 1484 } |
1485 | 1485 |
1486 if (jp2->color.jp2_cdef != NULL) { | 1486 if (jp2->color.jp2_cdef != NULL) { |
1487 l_writers[l_nb_pass].handler = opj_jp2_write_cdef; | 1487 l_writers[l_nb_pass].handler = opj_jp2_write_cdef; |
1488 l_nb_pass++; | 1488 l_nb_pass++; |
1489 } | 1489 } |
1490 | 1490 » |
1491 /* write box header */ | 1491 /* write box header */ |
1492 /* write JP2H type */ | 1492 /* write JP2H type */ |
1493 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4); | 1493 opj_write_bytes(l_jp2h_data+4,JP2_JP2H,4); |
1494 | 1494 |
1495 l_current_writer = l_writers; | 1495 l_current_writer = l_writers; |
1496 for (i=0;i<l_nb_pass;++i) { | 1496 for (i=0;i<l_nb_pass;++i) { |
1497 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)); |
1498 if (l_current_writer->m_data == 00) { | 1498 if (l_current_writer->m_data == 00) { |
1499 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"); |
1500 l_result = OPJ_FALSE; | 1500 l_result = OPJ_FALSE; |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1686 opj_cparameters_t *parameters, | 1686 opj_cparameters_t *parameters, |
1687 opj_image_t *image, | 1687 opj_image_t *image, |
1688 opj_event_mgr_t * p_manager) | 1688 opj_event_mgr_t * p_manager) |
1689 { | 1689 { |
1690 OPJ_UINT32 i; | 1690 OPJ_UINT32 i; |
1691 OPJ_UINT32 depth_0; | 1691 OPJ_UINT32 depth_0; |
1692 OPJ_UINT32 sign; | 1692 OPJ_UINT32 sign; |
1693 OPJ_UINT32 alpha_count; | 1693 OPJ_UINT32 alpha_count; |
1694 OPJ_UINT32 color_channels = 0U; | 1694 OPJ_UINT32 color_channels = 0U; |
1695 OPJ_UINT32 alpha_channel = 0U; | 1695 OPJ_UINT32 alpha_channel = 0U; |
1696 | 1696 » |
1697 | 1697 |
1698 if(!jp2 || !parameters || !image) | 1698 if(!jp2 || !parameters || !image) |
1699 return OPJ_FALSE; | 1699 return OPJ_FALSE; |
1700 | 1700 |
1701 /* setup the J2K codec */ | 1701 /* setup the J2K codec */ |
1702 /* ------------------- */ | 1702 /* ------------------- */ |
1703 | 1703 |
1704 /* Check if number of components respects standard */ | 1704 /* Check if number of components respects standard */ |
1705 if (image->numcomps < 1 || image->numcomps > 16384) { | 1705 if (image->numcomps < 1 || image->numcomps > 16384) { |
1706 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 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1962 | 1962 |
1963 /* PARAMETER VALIDATION */ | 1963 /* PARAMETER VALIDATION */ |
1964 /* number of components */ | 1964 /* number of components */ |
1965 l_is_valid &= (jp2->numcl > 0); | 1965 l_is_valid &= (jp2->numcl > 0); |
1966 /* width */ | 1966 /* width */ |
1967 l_is_valid &= (jp2->h > 0); | 1967 l_is_valid &= (jp2->h > 0); |
1968 /* height */ | 1968 /* height */ |
1969 l_is_valid &= (jp2->w > 0); | 1969 l_is_valid &= (jp2->w > 0); |
1970 /* precision */ | 1970 /* precision */ |
1971 for (i = 0; i < jp2->numcomps; ++i) { | 1971 for (i = 0; i < jp2->numcomps; ++i) { |
1972 » » l_is_valid &= (jp2->comps[i].bpcc > 0); | 1972 » » l_is_valid &= ((jp2->comps[i].bpcc & 0x7FU) < 38U); /* 0 is vali
d, ignore sign for check */ |
1973 } | 1973 } |
1974 | 1974 |
1975 /* METH */ | 1975 /* METH */ |
1976 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3)); | 1976 l_is_valid &= ((jp2->meth > 0) && (jp2->meth < 3)); |
1977 | 1977 |
1978 /* stream validation */ | 1978 /* stream validation */ |
1979 /* back and forth is needed */ | 1979 /* back and forth is needed */ |
1980 l_is_valid &= opj_stream_has_seek(cio); | 1980 l_is_valid &= opj_stream_has_seek(cio); |
1981 | 1981 |
1982 return l_is_valid; | 1982 return l_is_valid; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2059 opj_free(l_current_data); | 2059 opj_free(l_current_data); |
2060 return OPJ_FALSE; | 2060 return OPJ_FALSE; |
2061 } | 2061 } |
2062 | 2062 |
2063 if (! l_current_handler->handler(jp2,l_current_data,l_cu
rrent_data_size,p_manager)) { | 2063 if (! l_current_handler->handler(jp2,l_current_data,l_cu
rrent_data_size,p_manager)) { |
2064 opj_free(l_current_data); | 2064 opj_free(l_current_data); |
2065 return OPJ_FALSE; | 2065 return OPJ_FALSE; |
2066 } | 2066 } |
2067 } | 2067 } |
2068 else { | 2068 else { |
| 2069 if (!(jp2->jp2_state & JP2_STATE_SIGNATURE)) { |
| 2070 opj_event_msg(p_manager, EVT_ERROR, "Malformed JP2 file format:
first box must be JPEG 2000 signature box\n"); |
| 2071 opj_free(l_current_data); |
| 2072 return OPJ_FALSE; |
| 2073 } |
| 2074 if (!(jp2->jp2_state & JP2_STATE_FILE_TYPE)) { |
| 2075 opj_event_msg(p_manager, EVT_ERROR, "Malformed JP2 file format:
second box must be file type box\n"); |
| 2076 opj_free(l_current_data); |
| 2077 return OPJ_FALSE; |
| 2078 } |
2069 jp2->jp2_state |= JP2_STATE_UNKNOWN; | 2079 jp2->jp2_state |= JP2_STATE_UNKNOWN; |
2070 if (opj_stream_skip(stream,l_current_data_size,p_manager
) != l_current_data_size) { | 2080 if (opj_stream_skip(stream,l_current_data_size,p_manager
) != l_current_data_size) { |
2071 opj_event_msg(p_manager, EVT_ERROR, "Problem wit
h skipping JPEG2000 box, stream error\n"); | 2081 opj_event_msg(p_manager, EVT_ERROR, "Problem wit
h skipping JPEG2000 box, stream error\n"); |
2072 opj_free(l_current_data); | 2082 opj_free(l_current_data); |
2073 return OPJ_FALSE; | 2083 return OPJ_FALSE; |
2074 } | 2084 } |
2075 } | 2085 } |
2076 } | 2086 } |
2077 | 2087 |
2078 opj_free(l_current_data); | 2088 opj_free(l_current_data); |
(...skipping 633 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2712 else | 2722 else |
2713 p_image->color_space = OPJ_CLRSPC_UNKNOWN; | 2723 p_image->color_space = OPJ_CLRSPC_UNKNOWN; |
2714 | 2724 |
2715 if(p_jp2->color.jp2_pclr) { | 2725 if(p_jp2->color.jp2_pclr) { |
2716 /* Part 1, I.5.3.4: Either both or none : */ | 2726 /* Part 1, I.5.3.4: Either both or none : */ |
2717 if( !p_jp2->color.jp2_pclr->cmap) | 2727 if( !p_jp2->color.jp2_pclr->cmap) |
2718 opj_jp2_free_pclr(&(p_jp2->color)); | 2728 opj_jp2_free_pclr(&(p_jp2->color)); |
2719 else | 2729 else |
2720 opj_jp2_apply_pclr(p_image, &(p_jp2->color)); | 2730 opj_jp2_apply_pclr(p_image, &(p_jp2->color)); |
2721 } | 2731 } |
2722 | 2732 » |
2723 /* Apply the color space if needed */ | 2733 /* Apply the color space if needed */ |
2724 if(p_jp2->color.jp2_cdef) { | 2734 if(p_jp2->color.jp2_cdef) { |
2725 opj_jp2_apply_cdef(p_image, &(p_jp2->color)); | 2735 opj_jp2_apply_cdef(p_image, &(p_jp2->color)); |
2726 } | 2736 } |
2727 | 2737 |
2728 if(p_jp2->color.icc_profile_buf) { | 2738 if(p_jp2->color.icc_profile_buf) { |
2729 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf; | 2739 p_image->icc_profile_buf = p_jp2->color.icc_profile_buf; |
2730 p_image->icc_profile_len = p_jp2->color.icc_profile_len; | 2740 p_image->icc_profile_len = p_jp2->color.icc_profile_len; |
2731 p_jp2->color.icc_profile_buf = NULL; | 2741 p_jp2->color.icc_profile_buf = NULL; |
2732 } | 2742 } |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2981 len = opj_stream_tell(cio)-lenp; | 2991 len = opj_stream_tell(cio)-lenp; |
2982 opj_stream_skip(cio, lenp, p_manager); | 2992 opj_stream_skip(cio, lenp, p_manager); |
2983 opj_write_bytes(l_data_header,len,4);/* L */ | 2993 opj_write_bytes(l_data_header,len,4);/* L */ |
2984 opj_stream_write_data(cio,l_data_header,4,p_manager); | 2994 opj_stream_write_data(cio,l_data_header,4,p_manager); |
2985 opj_stream_seek(cio, lenp+len,p_manager); | 2995 opj_stream_seek(cio, lenp+len,p_manager); |
2986 | 2996 |
2987 return len; | 2997 return len; |
2988 } | 2998 } |
2989 #endif | 2999 #endif |
2990 #endif /* USE_JPIP */ | 3000 #endif /* USE_JPIP */ |
OLD | NEW |