/*
- * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
- * Copyright (c) 2002-2007, Professor Benoit Macq
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
+ * Copyright (c) 2002-2014, Professor Benoit Macq
* Copyright (c) 2001-2003, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
- * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
+ * Copyright (c) 2003-2007, Francois-Olivier Devaux
+ * Copyright (c) 2003-2014, Antonin Descampe
* Copyright (c) 2005, Herve Drolon, FreeImage Team
* Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
* Copyright (c) 2006-2007, Parvatha Elangovan
#include "opj_includes.h"
-#define CINEMA_24_CS 1302083 /*Codestream length for 24fps*/
-#define CINEMA_48_CS 651041 /*Codestream length for 48fps*/
-#define COMP_24_CS 1041666 /*Maximum size per color component for 2K & 4K @ 24fps*/
-#define COMP_48_CS 520833 /*Maximum size per color component for 2K @ 48fps*/
-
/** @defgroup J2K J2K - JPEG-2000 codestream reader/writer */
/*@{*/
static OPJ_BOOL opj_j2k_update_image_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data, opj_image_t* p_output_image);
+static void opj_get_tile_dimensions(opj_image_t * l_image,
+ opj_tcd_tilecomp_t * l_tilec,
+ opj_image_comp_t * l_img_comp,
+ OPJ_UINT32* l_size_comp,
+ OPJ_UINT32* l_width,
+ OPJ_UINT32* l_height,
+ OPJ_UINT32* l_offset_x,
+ OPJ_UINT32* l_offset_y,
+ OPJ_UINT32* l_image_width,
+ OPJ_UINT32* l_stride,
+ OPJ_UINT32* l_tile_offset);
+
static void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data);
static OPJ_BOOL opj_j2k_post_write_tile (opj_j2k_t * p_j2k,
- OPJ_BYTE * p_data,
- OPJ_UINT32 p_data_size,
opj_stream_private_t *p_stream,
opj_event_mgr_t * p_manager );
OPJ_UINT32 p_header_size,
opj_event_mgr_t * p_manager);
+#if 0
/**
* Writes the COC marker (Coding style component)
*
OPJ_UINT32 p_comp_no,
opj_stream_private_t *p_stream,
opj_event_mgr_t * p_manager );
+#endif
+
+#if 0
/**
* Writes the COC marker (Coding style component)
*
OPJ_BYTE * p_data,
OPJ_UINT32 * p_data_written,
opj_event_mgr_t * p_manager );
+#endif
/**
* Gets the maximum size taken by a coc.
OPJ_BYTE * p_header_data,
OPJ_UINT32 p_header_size,
opj_event_mgr_t * p_manager );
+#if 0
/**
* Writes the QCC marker (quantization component)
*
OPJ_UINT32 p_comp_no,
opj_stream_private_t *p_stream,
opj_event_mgr_t * p_manager );
+#endif
+#if 0
/**
* Writes the QCC marker (quantization component)
*
OPJ_BYTE * p_data,
OPJ_UINT32 * p_data_written,
opj_event_mgr_t * p_manager );
+#endif
/**
* Gets the maximum size taken by a qcc.
opj_stream_private_t *p_stream,
opj_event_mgr_t * p_manager );
+#if 0
/**
* Reads a EOC marker (End Of Codestream)
*
static OPJ_BOOL opj_j2k_read_eoc ( opj_j2k_t *p_j2k,
opj_stream_private_t *p_stream,
opj_event_mgr_t * p_manager );
+#endif
/**
* Writes the CBD-MCT-MCC-MCO markers (Multi components transform)
OPJ_UINT32 p_header_size,
opj_event_mgr_t * p_manager);
+#if 0
/**
* Writes COC marker for each component.
*
static OPJ_BOOL opj_j2k_write_all_coc( opj_j2k_t *p_j2k,
opj_stream_private_t *p_stream,
opj_event_mgr_t * p_manager );
+#endif
+#if 0
/**
* Writes QCC marker for each component.
*
static OPJ_BOOL opj_j2k_write_all_qcc( opj_j2k_t *p_j2k,
opj_stream_private_t *p_stream,
opj_event_mgr_t * p_manager );
+#endif
/**
* Writes regions of interests.
static void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager);
-static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_CINEMA_MODE cinema_mode, opj_event_mgr_t *p_manager);
+static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, opj_event_mgr_t *p_manager);
/*@}*/
opj_event_msg(p_manager , EVT_ERROR, "Not enough memory for checking the poc values.\n");
return OPJ_FALSE;
}
- memset(packet_array,0,step_l * p_num_layers* sizeof(OPJ_UINT32));
if (p_nb_pocs == 0) {
+ opj_free(packet_array);
return OPJ_TRUE;
}
OPJ_UINT32 l_nb_comp_remain;
OPJ_UINT32 l_remaining_size;
OPJ_UINT32 l_nb_tiles;
- OPJ_UINT32 l_tmp;
+ OPJ_UINT32 l_tmp, l_tx1, l_ty1;
opj_image_t *l_image = 00;
opj_cp_t *l_cp = 00;
opj_image_comp_t * l_img_comp = 00;
opj_read_bytes(p_header_data,&l_tmp ,2); /* Rsiz (capabilities) */
p_header_data+=2;
- l_cp->rsiz = (OPJ_RSIZ_CAPABILITIES) l_tmp;
+ l_cp->rsiz = (OPJ_UINT16) l_tmp;
opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->x1, 4); /* Xsiz */
p_header_data+=4;
opj_read_bytes(p_header_data, (OPJ_UINT32*) &l_image->y1, 4); /* Ysiz */
}
/* testcase 4035.pdf.SIGSEGV.d8b.3375 */
- if (l_image->x0 > l_image->x1 || l_image->y0 > l_image->y1) {
- opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: negative image size (%d x %d)\n", l_image->x1 - l_image->x0, l_image->y1 - l_image->y0);
+ /* testcase issue427-null-image-size.jp2 */
+ if ((l_image->x0 >= l_image->x1) || (l_image->y0 >= l_image->y1)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: negative or zero image size (%d x %d)\n", l_image->x1 - l_image->x0, l_image->y1 - l_image->y0);
return OPJ_FALSE;
}
/* testcase 2539.pdf.SIGFPE.706.1712 (also 3622.pdf.SIGFPE.706.2916 and 4008.pdf.SIGFPE.706.3345 and maybe more) */
/* testcase 1610.pdf.SIGSEGV.59c.681 */
if (((OPJ_UINT64)l_image->x1) * ((OPJ_UINT64)l_image->y1) != (l_image->x1 * l_image->y1)) {
- opj_event_msg(p_manager, EVT_ERROR, "Prevent buffer overflow (x1: %d, y1: %d)", l_image->x1, l_image->y1);
+ opj_event_msg(p_manager, EVT_ERROR, "Prevent buffer overflow (x1: %d, y1: %d)\n", l_image->x1, l_image->y1);
+ return OPJ_FALSE;
+ }
+
+ /* testcase issue427-illegal-tile-offset.jp2 */
+ l_tx1 = l_cp->tx0 + l_cp->tdx;
+ if (l_tx1 < l_cp->tx0) { /* manage overflow */
+ l_tx1 = 0xFFFFFFFFU;
+ }
+ l_ty1 = l_cp->ty0 + l_cp->tdy;
+ if (l_ty1 < l_cp->ty0) { /* manage overflow */
+ l_ty1 = 0xFFFFFFFFU;
+ }
+ if ((l_cp->tx0 > l_image->x0) || (l_cp->ty0 > l_image->y0) || (l_tx1 <= l_image->x0) || (l_ty1 <= l_image->y0) ) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error with SIZ marker: illegal tile offset\n");
return OPJ_FALSE;
}
return OPJ_FALSE;
}
- memset(l_image->comps,0,l_image->numcomps * sizeof(opj_image_comp_t));
l_img_comp = l_image->comps;
/* Read the component information */
l_img_comp->sgnd = tmp >> 7;
opj_read_bytes(p_header_data,&tmp,1); /* XRsiz_i */
++p_header_data;
- l_img_comp->dx = (OPJ_INT32)tmp; /* should be between 1 and 255 */
+ l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
opj_read_bytes(p_header_data,&tmp,1); /* YRsiz_i */
++p_header_data;
- l_img_comp->dy = (OPJ_INT32)tmp; /* should be between 1 and 255 */
+ l_img_comp->dy = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
if( l_img_comp->dx < 1 || l_img_comp->dx > 255 ||
l_img_comp->dy < 1 || l_img_comp->dy > 255 ) {
opj_event_msg(p_manager, EVT_ERROR,
}
/* Compute the number of tiles */
- l_cp->tw = opj_int_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
- l_cp->th = opj_int_ceildiv(l_image->y1 - l_cp->ty0, l_cp->tdy);
+ l_cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->x1 - l_cp->tx0), (OPJ_INT32)l_cp->tdx);
+ l_cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(l_image->y1 - l_cp->ty0), (OPJ_INT32)l_cp->tdy);
/* Check that the number of tiles is valid */
if (l_cp->tw == 0 || l_cp->th == 0 || l_cp->tw > 65535 / l_cp->th) {
if (p_j2k->m_specific_param.m_decoder.m_discard_tiles) {
p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_j2k->m_specific_param.m_decoder.m_start_tile_x - l_cp->tx0) / l_cp->tdx;
p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_j2k->m_specific_param.m_decoder.m_start_tile_y - l_cp->ty0) / l_cp->tdy;
- p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), l_cp->tdx);
- p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_int_ceildiv((p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), l_cp->tdy);
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_x - l_cp->tx0), (OPJ_INT32)l_cp->tdx);
+ p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0), (OPJ_INT32)l_cp->tdy);
}
else {
p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
return OPJ_FALSE;
}
- memset(l_cp->tcps,0,l_nb_tiles*sizeof(opj_tcp_t));
#ifdef USE_JPWL
if (l_cp->correct) {
opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
return OPJ_FALSE;
}
- memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->tccps ,0,l_image->numcomps*sizeof(opj_tccp_t));
p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records =
- (opj_mct_data_t*)opj_malloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
+ (opj_mct_data_t*)opj_calloc(OPJ_J2K_MCT_DEFAULT_NB_RECORDS ,sizeof(opj_mct_data_t));
if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records) {
opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
return OPJ_FALSE;
}
- memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mct_records,0,OPJ_J2K_MCT_DEFAULT_NB_RECORDS * sizeof(opj_mct_data_t));
p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mct_records = OPJ_J2K_MCT_DEFAULT_NB_RECORDS;
p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records =
(opj_simple_mcc_decorrelation_data_t*)
- opj_malloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
+ opj_calloc(OPJ_J2K_MCC_DEFAULT_NB_RECORDS, sizeof(opj_simple_mcc_decorrelation_data_t));
if (! p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records) {
opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
return OPJ_FALSE;
}
- memset(p_j2k->m_specific_param.m_decoder.m_default_tcp->m_mcc_records,0,OPJ_J2K_MCC_DEFAULT_NB_RECORDS * sizeof(opj_simple_mcc_decorrelation_data_t));
p_j2k->m_specific_param.m_decoder.m_default_tcp->m_nb_max_mcc_records = OPJ_J2K_MCC_DEFAULT_NB_RECORDS;
/* set up default dc level shift */
l_current_tile_param = l_cp->tcps;
for (i = 0; i < l_nb_tiles; ++i) {
- l_current_tile_param->tccps = (opj_tccp_t*) opj_malloc(l_image->numcomps * sizeof(opj_tccp_t));
+ l_current_tile_param->tccps = (opj_tccp_t*) opj_calloc(l_image->numcomps, sizeof(opj_tccp_t));
if (l_current_tile_param->tccps == 00) {
opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to take in charge SIZ marker\n");
return OPJ_FALSE;
}
- memset(l_current_tile_param->tccps,0,l_image->numcomps * sizeof(opj_tccp_t));
++l_current_tile_param;
}
opj_read_bytes(p_header_data,&l_tcp->csty,1); /* Scod */
++p_header_data;
+ /* Make sure we know how to decode this */
+ if ((l_tcp->csty & ~(OPJ_UINT32)(J2K_CP_CSTY_PRT | J2K_CP_CSTY_SOP | J2K_CP_CSTY_EPH)) != 0U) {
+ opj_event_msg(p_manager, EVT_ERROR, "Unknown Scod value in COD marker\n");
+ return OPJ_FALSE;
+ }
opj_read_bytes(p_header_data,&l_tmp,1); /* SGcod (A) */
++p_header_data;
l_tcp->prg = (OPJ_PROG_ORDER) l_tmp;
+ /* Make sure progression order is valid */
+ if (l_tcp->prg > OPJ_CPRL ) {
+ opj_event_msg(p_manager, EVT_ERROR, "Unknown progression order in COD marker\n");
+ l_tcp->prg = OPJ_PROG_UNKNOWN;
+ }
opj_read_bytes(p_header_data,&l_tcp->numlayers,2); /* SGcod (B) */
p_header_data+=2;
return OPJ_TRUE;
}
+#if 0
OPJ_BOOL opj_j2k_write_coc( opj_j2k_t *p_j2k,
OPJ_UINT32 p_comp_no,
opj_stream_private_t *p_stream,
return OPJ_TRUE;
}
+#endif
+#if 0
void opj_j2k_write_coc_in_memory( opj_j2k_t *p_j2k,
OPJ_UINT32 p_comp_no,
OPJ_BYTE * p_data,
opj_j2k_write_SPCod_SPCoc(p_j2k,p_j2k->m_current_tile_number,0,l_current_data,&l_remaining_size,p_manager);
* p_data_written = l_coc_size;
}
+#endif
OPJ_UINT32 opj_j2k_get_max_coc_size(opj_j2k_t *p_j2k)
{
return OPJ_TRUE;
}
+#if 0
OPJ_BOOL opj_j2k_write_qcc( opj_j2k_t *p_j2k,
OPJ_UINT32 p_comp_no,
opj_stream_private_t *p_stream,
return OPJ_TRUE;
}
+#endif
+#if 0
void opj_j2k_write_qcc_in_memory( opj_j2k_t *p_j2k,
OPJ_UINT32 p_comp_no,
OPJ_BYTE * p_data,
*p_data_written = l_qcc_size;
}
+#endif
OPJ_UINT32 opj_j2k_get_max_qcc_size (opj_j2k_t *p_j2k)
{
++l_current_data;
/* change the value of the max layer according to the actual number of layers in the file, components and resolutions*/
- l_current_poc->layno1 = opj_int_min(l_current_poc->layno1, l_tcp->numlayers);
- l_current_poc->resno1 = opj_int_min(l_current_poc->resno1, l_tccp->numresolutions);
- l_current_poc->compno1 = opj_int_min(l_current_poc->compno1, l_nb_comp);
+ l_current_poc->layno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->layno1, (OPJ_INT32)l_tcp->numlayers);
+ l_current_poc->resno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->resno1, (OPJ_INT32)l_tccp->numresolutions);
+ l_current_poc->compno1 = (OPJ_UINT32)opj_int_min((OPJ_INT32)l_current_poc->compno1, (OPJ_INT32)l_nb_comp);
++l_current_poc;
}
l_nb_comps = p_j2k->m_private_image->numcomps - 1;
l_nb_bytes += opj_j2k_get_max_toc_size(p_j2k);
- if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == 0) {
+ if (!(OPJ_IS_CINEMA(p_j2k->m_cp.rsiz))) {
l_coc_bytes = opj_j2k_get_max_coc_size(p_j2k);
l_nb_bytes += l_nb_comps * l_coc_bytes;
l_old_poc_nb = l_tcp->POC ? l_tcp->numpocs + 1 : 0;
l_current_poc_nb += l_old_poc_nb;
+ if(l_current_poc_nb >= 32)
+ {
+ opj_event_msg(p_manager, EVT_ERROR, "Too many POCs %d\n", l_current_poc_nb);
+ return OPJ_FALSE;
+ }
assert(l_current_poc_nb < 32);
/* now poc is in use.*/
opj_read_bytes(p_header_data,&(l_current_poc->compno0),l_comp_room); /* CSpoc_i */
p_header_data+=l_comp_room;
opj_read_bytes(p_header_data,&(l_current_poc->layno1),2); /* LYEpoc_i */
+ /* make sure layer end is in acceptable bounds */
+ l_current_poc->layno1 = opj_uint_min(l_current_poc->layno1, l_tcp->numlayers);
p_header_data+=2;
opj_read_bytes(p_header_data,&(l_current_poc->resno1),1); /* REpoc_i */
++p_header_data;
/* First PPM marker */
if (l_Z_ppm == 0) {
+ if (l_cp->ppm_data != NULL) {
+ opj_event_msg(p_manager, EVT_ERROR, "Zppm O already processed. Found twice.\n");
+ opj_free(l_cp->ppm_data);
+ l_cp->ppm_data = NULL;
+ l_cp->ppm_buffer = NULL;
+ l_cp->ppm = 0; /* do not use PPM */
+ return OPJ_FALSE;
+ }
/* We need now at least the Nppm^0 element */
if (p_header_size < 4) {
opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
p_header_data+=4;
p_header_size-=4;
+ /* sanity check: how much bytes is left for Ippm */
+ if( p_header_size < l_N_ppm )
+ {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes (%u) to hold Ippm series (%u), Index (%d)\n", p_header_size, l_N_ppm, l_Z_ppm );
+ opj_free(l_cp->ppm_data);
+ l_cp->ppm_data = NULL;
+ l_cp->ppm_buffer = NULL;
+ l_cp->ppm = 0; /* do not use PPM */
+ return OPJ_FALSE;
+ }
+
/* First PPM marker: Initialization */
l_cp->ppm_len = l_N_ppm;
l_cp->ppm_data_read = 0;
- l_cp->ppm_data = (OPJ_BYTE *) opj_malloc(l_cp->ppm_len);
+ l_cp->ppm_data = (OPJ_BYTE *) opj_calloc(1,l_cp->ppm_len);
l_cp->ppm_buffer = l_cp->ppm_data;
if (l_cp->ppm_data == 00) {
opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read ppm marker\n");
return OPJ_FALSE;
}
- memset(l_cp->ppm_data,0,l_cp->ppm_len);
l_cp->ppm_data_current = l_cp->ppm_data;
p_header_data+=4;
p_header_size-=4;
+ /* sanity check: how much bytes is left for Ippm */
+ if( p_header_size < l_N_ppm )
+ {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough bytes (%u) to hold Ippm series (%u), Index (%d)\n", p_header_size, l_N_ppm, l_Z_ppm );
+ opj_free(l_cp->ppm_data);
+ l_cp->ppm_data = NULL;
+ l_cp->ppm_buffer = NULL;
+ l_cp->ppm = 0; /* do not use PPM */
+ return OPJ_FALSE;
+ }
/* Increase the size of ppm_data to add the new Ippm series*/
assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");
new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
if (p_header_size)
{
+ if (p_header_size < 4) {
+ opj_free(l_cp->ppm_data);
+ l_cp->ppm_data = NULL;
+ l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */
+ l_cp->ppm_len = 0;
+ l_cp->ppm = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Error reading PPM marker\n");
+ return OPJ_FALSE;
+ }
opj_read_bytes(p_header_data,&l_N_ppm,4); /* N_ppm^i */
p_header_data+=4;
p_header_size-=4;
l_remaining_data = p_header_size;
/* Next Ippm series is a complete series ?*/
- if (l_remaining_data > l_N_ppm) {
+ if (l_remaining_data >= l_N_ppm) {
OPJ_BYTE *new_ppm_data;
/* Increase the size of ppm_data to add the new Ippm series*/
assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");
+ /* Overflow check */
+ if ((l_cp->ppm_len + l_N_ppm) < l_N_ppm) {
+ opj_free(l_cp->ppm_data);
+ l_cp->ppm_data = NULL;
+ l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */
+ l_cp->ppm_len = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (complete) Ippm series\n");
+ return OPJ_FALSE;
+ }
new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
if (! new_ppm_data) {
opj_free(l_cp->ppm_data);
if (l_remaining_data) {
OPJ_BYTE *new_ppm_data;
assert(l_cp->ppm_data == l_cp->ppm_buffer && "We need ppm_data and ppm_buffer to be the same when reallocating");
+
+ /* Overflow check */
+ if ((l_cp->ppm_len + l_N_ppm) < l_N_ppm) {
+ opj_free(l_cp->ppm_data);
+ l_cp->ppm_data = NULL;
+ l_cp->ppm_buffer = NULL; /* TODO: no need for a new local variable: ppm_buffer and ppm_data are enough */
+ l_cp->ppm_len = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to increase the size of ppm_data to add the new (complete) Ippm series\n");
+ return OPJ_FALSE;
+ }
new_ppm_data = (OPJ_BYTE *) opj_realloc(l_cp->ppm_data, l_cp->ppm_len + l_N_ppm);
if (! new_ppm_data) {
opj_free(l_cp->ppm_data);
l_tcp->ppt_data_size = 0;
l_tcp->ppt_len = p_header_size;
+ opj_free(l_tcp->ppt_buffer);
l_tcp->ppt_buffer = (OPJ_BYTE *) opj_calloc(l_tcp->ppt_len, sizeof(OPJ_BYTE) );
if (l_tcp->ppt_buffer == 00) {
opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
(opj_tp_index_t*)opj_calloc(l_num_parts, sizeof(opj_tp_index_t));
+ if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n");
+ return OPJ_FALSE;
+ }
}
else {
opj_tp_index_t *new_tp_index = (opj_tp_index_t *) opj_realloc(
if (! new_tp_index) {
opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
- opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n");
return OPJ_FALSE;
}
p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index;
p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index =
(opj_tp_index_t*)opj_calloc( p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps,
sizeof(opj_tp_index_t));
+ if (!p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index) {
+ p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n");
+ return OPJ_FALSE;
+ }
}
if ( l_current_part >= p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps ){
opj_free(p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index);
p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = NULL;
p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].current_nb_tps = 0;
- opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read PPT marker\n");
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to read SOT marker. Tile index allocation failed\n");
return OPJ_FALSE;
}
p_j2k->cstr_index->tile_index[p_j2k->m_current_tile_number].tp_index = new_tp_index;
/* Patch to support new PHR data */
if (p_j2k->m_specific_param.m_decoder.m_sot_length) {
+ /* If we are here, we'll try to read the data after allocation */
+ /* Check enough bytes left in stream before allocation */
+ if ((OPJ_OFF_T)p_j2k->m_specific_param.m_decoder.m_sot_length > opj_stream_get_number_byte_left(p_stream)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Tile part length size inconsistent with stream length\n");
+ return OPJ_FALSE;
+ }
if (! *l_current_data) {
/* LH: oddly enough, in this path, l_tile_len!=0.
* TODO: If this was consistant, we could simplify the code to only use realloc(), as realloc(0,...) default to malloc(0,...).
opj_write_bytes(l_current_data, 0,1); /* Srgn */
++l_current_data;
- opj_write_bytes(l_current_data, l_tccp->roishift,1); /* SPrgn */
+ opj_write_bytes(l_current_data, (OPJ_UINT32)l_tccp->roishift,1); /* SPrgn */
++l_current_data;
if (opj_stream_write_data(p_stream,p_j2k->m_specific_param.m_encoder.m_header_tile_data,l_rgn_size,p_manager) != l_rgn_size) {
OPJ_FLOAT32 l_offset = (OPJ_FLOAT32)(*l_tp_stride_func)(l_tcp) / (OPJ_FLOAT32)l_tcp->numlayers;
/* 4 borders of the tile rescale on the image if necessary */
- l_x0 = opj_int_max(l_cp->tx0 + j * l_cp->tdx, l_image->x0);
- l_y0 = opj_int_max(l_cp->ty0 + i * l_cp->tdy, l_image->y0);
- l_x1 = opj_int_min(l_cp->tx0 + (j + 1) * l_cp->tdx, l_image->x1);
- l_y1 = opj_int_min(l_cp->ty0 + (i + 1) * l_cp->tdy, l_image->y1);
+ l_x0 = opj_int_max((OPJ_INT32)(l_cp->tx0 + j * l_cp->tdx), (OPJ_INT32)l_image->x0);
+ l_y0 = opj_int_max((OPJ_INT32)(l_cp->ty0 + i * l_cp->tdy), (OPJ_INT32)l_image->y0);
+ l_x1 = opj_int_min((OPJ_INT32)(l_cp->tx0 + (j + 1) * l_cp->tdx), (OPJ_INT32)l_image->x1);
+ l_y1 = opj_int_min((OPJ_INT32)(l_cp->ty0 + (i + 1) * l_cp->tdy), (OPJ_INT32)l_image->y1);
l_rates = l_tcp->rates;
/* Modification of the RATE >> */
if (*l_rates) {
- *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
+ *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * (OPJ_UINT32)(l_y1 - l_y0)))
/
- ((*l_rates) * l_bits_empty)
+ ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)
)
-
l_offset;
for (k = 1; k < l_tcp->numlayers; ++k) {
if (*l_rates) {
- *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (l_x1 - l_x0) * (l_y1 - l_y0)))
+ *l_rates = (( (OPJ_FLOAT32) (l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) * (OPJ_UINT32)(l_y1 - l_y0)))
/
- ((*l_rates) * l_bits_empty)
+ ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)
)
-
l_offset;
return OPJ_FALSE;
}
- if (l_cp->m_specific_param.m_enc.m_cinema) {
+ if (OPJ_IS_CINEMA(l_cp->rsiz)) {
p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
(OPJ_BYTE *) opj_malloc(5*p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
if (! p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer) {
return OPJ_TRUE;
}
+#if 0
OPJ_BOOL opj_j2k_read_eoc ( opj_j2k_t *p_j2k,
opj_stream_private_t *p_stream,
opj_event_mgr_t * p_manager )
opj_tcd_destroy(l_tcd);
return OPJ_TRUE;
}
+#endif
OPJ_BOOL opj_j2k_get_end_header(opj_j2k_t *p_j2k,
struct opj_stream_private *p_stream,
return OPJ_TRUE;
}
+#if 0
OPJ_BOOL opj_j2k_write_all_coc(opj_j2k_t *p_j2k,
struct opj_stream_private *p_stream,
struct opj_event_mgr * p_manager )
return OPJ_TRUE;
}
+#endif
+#if 0
OPJ_BOOL opj_j2k_write_all_qcc(opj_j2k_t *p_j2k,
struct opj_stream_private *p_stream,
struct opj_event_mgr * p_manager )
return OPJ_TRUE;
}
+#endif
OPJ_BOOL opj_j2k_write_regions( opj_j2k_t *p_j2k,
l_cstr_index = p_j2k->cstr_index;
if (l_cstr_index) {
- l_cstr_index->codestream_size = opj_stream_tell(p_stream);
+ l_cstr_index->codestream_size = (OPJ_UINT64)opj_stream_tell(p_stream);
/* UniPG>> */
/* The following adjustment is done to adjust the codestream size */
/* if SOD is not at 0 in the buffer. Useful in case of JP2, where */
/* the first bunch of bytes is not in the codestream */
- l_cstr_index->codestream_size -= l_cstr_index->main_head_start;
+ l_cstr_index->codestream_size -= (OPJ_UINT64)l_cstr_index->main_head_start;
/* <<UniPG */
}
l_current_offset_data = l_offset_data;
for (i=0;i<p_image->numcomps;++i) {
- l_tccp->m_dc_level_shift = *(l_current_offset_data++);
+ l_tccp->m_dc_level_shift = (OPJ_INT32)*(l_current_offset_data++);
++l_tccp;
}
opj_j2k_t* opj_j2k_create_compress(void)
{
- opj_j2k_t *l_j2k = (opj_j2k_t*) opj_malloc(sizeof(opj_j2k_t));
+ opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1,sizeof(opj_j2k_t));
if (!l_j2k) {
return NULL;
}
- memset(l_j2k,0,sizeof(opj_j2k_t));
l_j2k->m_is_decoder = 0;
l_j2k->m_cp.m_is_decoder = 0;
POC[0].resno0 = 0;
POC[0].compno0 = 0;
POC[0].layno1 = 1;
- POC[0].resno1 = numres-1;
+ POC[0].resno1 = (OPJ_UINT32)(numres-1);
POC[0].compno1 = 3;
POC[0].prg1 = OPJ_CPRL;
POC[1].tile = 1;
- POC[1].resno0 = numres-1;
+ POC[1].resno0 = (OPJ_UINT32)(numres-1);
POC[1].compno0 = 0;
POC[1].layno1 = 1;
- POC[1].resno1 = numres;
+ POC[1].resno1 = (OPJ_UINT32)numres;
POC[1].compno1 = 3;
POC[1].prg1 = OPJ_CPRL;
return 2;
void opj_j2k_set_cinema_parameters(opj_cparameters_t *parameters, opj_image_t *image, opj_event_mgr_t *p_manager)
{
/* Configure cinema parameters */
- OPJ_FLOAT32 max_rate = 0;
- OPJ_FLOAT32 temp_rate = 0;
int i;
- /* profile (Rsiz) */
- switch (parameters->cp_cinema){
- case OPJ_CINEMA2K_24:
- case OPJ_CINEMA2K_48:
- parameters->cp_rsiz = OPJ_CINEMA2K;
- break;
- case OPJ_CINEMA4K_24:
- parameters->cp_rsiz = OPJ_CINEMA4K;
- break;
- case OPJ_OFF:
- assert(0);
- break;
- }
-
/* No tiling */
parameters->tile_size_on = OPJ_FALSE;
parameters->cp_tdx=1;
opj_event_msg(p_manager, EVT_WARNING,
"JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
"1 single quality layer"
- "-> Number of layers forced to 1 (rather than %d)\n",
- parameters->tcp_numlayers);
+ "-> Number of layers forced to 1 (rather than %d)\n"
+ "-> Rate of the last layer (%3.1f) will be used",
+ parameters->tcp_numlayers, parameters->tcp_rates[parameters->tcp_numlayers-1]);
+ parameters->tcp_rates[0] = parameters->tcp_rates[parameters->tcp_numlayers-1];
parameters->tcp_numlayers = 1;
}
/* Resolution levels */
- switch (parameters->cp_cinema){
- case OPJ_CINEMA2K_24:
- case OPJ_CINEMA2K_48:
+ switch (parameters->rsiz){
+ case OPJ_PROFILE_CINEMA_2K:
if(parameters->numresolution > 6){
opj_event_msg(p_manager, EVT_WARNING,
"JPEG 2000 Profile-3 (2k dc profile) requires:\n"
parameters->numresolution = 6;
}
break;
- case OPJ_CINEMA4K_24:
+ case OPJ_PROFILE_CINEMA_4K:
if(parameters->numresolution < 2){
opj_event_msg(p_manager, EVT_WARNING,
"JPEG 2000 Profile-4 (4k dc profile) requires:\n"
parameters->prog_order = OPJ_CPRL;
/* Progression order changes for 4K, disallowed for 2K */
- if (parameters->cp_cinema == OPJ_CINEMA4K_24) {
- parameters->numpocs = opj_j2k_initialise_4K_poc(parameters->POC,parameters->numresolution);
+ if (parameters->rsiz == OPJ_PROFILE_CINEMA_4K) {
+ parameters->numpocs = (OPJ_UINT32)opj_j2k_initialise_4K_poc(parameters->POC,parameters->numresolution);
} else {
parameters->numpocs = 0;
}
/* Limited bit-rate */
parameters->cp_disto_alloc = 1;
- switch (parameters->cp_cinema){
- case OPJ_CINEMA2K_24:
- case OPJ_CINEMA4K_24:
- max_rate = (OPJ_FLOAT32) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/
- (OPJ_FLOAT32)(CINEMA_24_CS * 8 * image->comps[0].dx * image->comps[0].dy);
- if (parameters->tcp_rates[0] == 0){
- parameters->tcp_rates[0] = max_rate;
- }else{
- temp_rate =(OPJ_FLOAT32)(image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/
- (OPJ_FLOAT32)(parameters->tcp_rates[0] * 8 * image->comps[0].dx * image->comps[0].dy);
- if (temp_rate > CINEMA_24_CS ){
- opj_event_msg(p_manager, EVT_WARNING,
- "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
- "Maximum 1302083 compressed bytes @ 24fps\n"
- "-> Specified rate (%3.1f) exceeds this limit. Rate will be forced to %3.1f.\n",
- parameters->tcp_rates[0], max_rate);
- parameters->tcp_rates[0]= max_rate;
- }else{
- opj_event_msg(p_manager, EVT_WARNING,
- "JPEG 2000 Profile-3 and 4 (2k/4k dc profile):\n"
- "INFO : Specified rate (%3.1f) is below the 2k/4k limit @ 24fps.\n",
- parameters->tcp_rates[0]);
- }
- }
- parameters->max_comp_size = COMP_24_CS;
- break;
- case OPJ_CINEMA2K_48:
- max_rate = ((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
- (CINEMA_48_CS * 8 * image->comps[0].dx * image->comps[0].dy);
- if (parameters->tcp_rates[0] == 0){
- parameters->tcp_rates[0] = max_rate;
- }else{
- temp_rate =((float) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec))/
- (parameters->tcp_rates[0] * 8 * image->comps[0].dx * image->comps[0].dy);
- if (temp_rate > CINEMA_48_CS ){
- opj_event_msg(p_manager, EVT_WARNING,
- "JPEG 2000 Profile-3 (2k dc profile) requires:\n"
- "Maximum 651041 compressed bytes @ 48fps\n"
- "-> Specified rate (%3.1f) exceeds this limit. Rate will be forced to %3.1f.\n",
- parameters->tcp_rates[0], max_rate);
- parameters->tcp_rates[0]= max_rate;
- }else{
- opj_event_msg(p_manager, EVT_WARNING,
- "JPEG 2000 Profile-3 (2k dc profile):\n"
- "INFO : Specified rate (%3.1f) is below the 2k limit @ 48 fps.\n",
- parameters->tcp_rates[0]);
- }
- }
- parameters->max_comp_size = COMP_48_CS;
- break;
- default:
- break;
+ if (parameters->max_cs_size <= 0) {
+ /* No rate has been introduced, 24 fps is assumed */
+ parameters->max_cs_size = OPJ_CINEMA_24_CS;
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
+ "Maximum 1302083 compressed bytes @ 24fps\n"
+ "As no rate has been given, this limit will be used.\n");
+ } else if (parameters->max_cs_size > OPJ_CINEMA_24_CS) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
+ "Maximum 1302083 compressed bytes @ 24fps\n"
+ "-> Specified rate exceeds this limit. Rate will be forced to 1302083 bytes.\n");
+ parameters->max_cs_size = OPJ_CINEMA_24_CS;
}
+
+ if (parameters->max_comp_size <= 0) {
+ /* No rate has been introduced, 24 fps is assumed */
+ parameters->max_comp_size = OPJ_CINEMA_24_COMP;
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
+ "Maximum 1041666 compressed bytes @ 24fps\n"
+ "As no rate has been given, this limit will be used.\n");
+ } else if (parameters->max_comp_size > OPJ_CINEMA_24_COMP) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Profile-3 and 4 (2k/4k dc profile) requires:\n"
+ "Maximum 1041666 compressed bytes @ 24fps\n"
+ "-> Specified rate exceeds this limit. Rate will be forced to 1041666 bytes.\n");
+ parameters->max_comp_size = OPJ_CINEMA_24_COMP;
+ }
+
+ parameters->tcp_rates[0] = (OPJ_FLOAT32) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/
+ (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * image->comps[0].dy);
+
}
-OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_CINEMA_MODE cinema_mode, opj_event_mgr_t *p_manager)
+OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz, opj_event_mgr_t *p_manager)
{
OPJ_UINT32 i;
}
/* Image size */
- switch (cinema_mode){
- case OPJ_CINEMA2K_24:
- case OPJ_CINEMA2K_48:
+ switch (rsiz){
+ case OPJ_PROFILE_CINEMA_2K:
if (((image->comps[0].w > 2048) | (image->comps[0].h > 1080))){
opj_event_msg(p_manager, EVT_WARNING,
"JPEG 2000 Profile-3 (2k dc profile) requires:\n"
return OPJ_FALSE;
}
break;
- case OPJ_CINEMA4K_24:
+ case OPJ_PROFILE_CINEMA_4K:
if (((image->comps[0].w > 4096) | (image->comps[0].h > 2160))){
opj_event_msg(p_manager, EVT_WARNING,
"JPEG 2000 Profile-4 (4k dc profile) requires:\n"
return OPJ_TRUE;
}
-void opj_j2k_setup_encoder( opj_j2k_t *p_j2k,
+OPJ_BOOL opj_j2k_setup_encoder( opj_j2k_t *p_j2k,
opj_cparameters_t *parameters,
opj_image_t *image,
opj_event_mgr_t * p_manager)
opj_cp_t *cp = 00;
if(!p_j2k || !parameters || ! image) {
- return;
+ return OPJ_FALSE;
+ }
+
+ if ((parameters->numresolution <= 0) || (parameters->numresolution > OPJ_J2K_MAXRLVLS)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Invalid number of resolutions : %d not in range [1,%d]\n", parameters->numresolution, OPJ_J2K_MAXRLVLS);
+ return OPJ_FALSE;
}
/* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
cp->tw = 1;
cp->th = 1;
+ /* FIXME ADE: to be removed once deprecated cp_cinema and cp_rsiz have been removed */
+ if (parameters->rsiz == OPJ_PROFILE_NONE) { /* consider deprecated fields only if RSIZ has not been set */
+ OPJ_BOOL deprecated_used = OPJ_FALSE;
+ switch (parameters->cp_cinema){
+ case OPJ_CINEMA2K_24:
+ parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
+ parameters->max_cs_size = OPJ_CINEMA_24_CS;
+ parameters->max_comp_size = OPJ_CINEMA_24_COMP;
+ deprecated_used = OPJ_TRUE;
+ break;
+ case OPJ_CINEMA2K_48:
+ parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
+ parameters->max_cs_size = OPJ_CINEMA_48_CS;
+ parameters->max_comp_size = OPJ_CINEMA_48_COMP;
+ deprecated_used = OPJ_TRUE;
+ break;
+ case OPJ_CINEMA4K_24:
+ parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
+ parameters->max_cs_size = OPJ_CINEMA_24_CS;
+ parameters->max_comp_size = OPJ_CINEMA_24_COMP;
+ deprecated_used = OPJ_TRUE;
+ break;
+ case OPJ_OFF:
+ default:
+ break;
+ }
+ switch (parameters->cp_rsiz){
+ case OPJ_CINEMA2K:
+ parameters->rsiz = OPJ_PROFILE_CINEMA_2K;
+ deprecated_used = OPJ_TRUE;
+ break;
+ case OPJ_CINEMA4K:
+ parameters->rsiz = OPJ_PROFILE_CINEMA_4K;
+ deprecated_used = OPJ_TRUE;
+ break;
+ case OPJ_MCT:
+ parameters->rsiz = OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT;
+ deprecated_used = OPJ_TRUE;
+ case OPJ_STD_RSIZ:
+ default:
+ break;
+ }
+ if (deprecated_used) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "Deprecated fields cp_cinema or cp_rsiz are used\n"
+ "Please consider using only the rsiz field\n"
+ "See openjpeg.h documentation for more details\n");
+ }
+ }
+
+ /* see if max_codestream_size does limit input rate */
+ if (parameters->max_cs_size <= 0) {
+ if (parameters->tcp_rates[parameters->tcp_numlayers-1] > 0) {
+ OPJ_FLOAT32 temp_size;
+ temp_size =(OPJ_FLOAT32)(image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/
+ (parameters->tcp_rates[parameters->tcp_numlayers-1] * 8 * (OPJ_FLOAT32)image->comps[0].dx * (OPJ_FLOAT32)image->comps[0].dy);
+ parameters->max_cs_size = (int) floor(temp_size);
+ } else {
+ parameters->max_cs_size = 0;
+ }
+ } else {
+ OPJ_FLOAT32 temp_rate;
+ OPJ_BOOL cap = OPJ_FALSE;
+ temp_rate = (OPJ_FLOAT32) (image->numcomps * image->comps[0].w * image->comps[0].h * image->comps[0].prec)/
+ (OPJ_FLOAT32)(((OPJ_UINT32)parameters->max_cs_size) * 8 * image->comps[0].dx * image->comps[0].dy);
+ for (i = 0; i < (OPJ_UINT32) parameters->tcp_numlayers; i++) {
+ if (parameters->tcp_rates[i] < temp_rate) {
+ parameters->tcp_rates[i] = temp_rate;
+ cap = OPJ_TRUE;
+ }
+ }
+ if (cap) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "The desired maximum codestream size has limited\n"
+ "at least one of the desired quality layers\n");
+ }
+ }
+
+ /* Manage profiles and applications and set RSIZ */
/* set cinema parameters if required */
- if (parameters->cp_cinema){
- opj_j2k_set_cinema_parameters(parameters,image,p_manager);
- if (!opj_j2k_is_cinema_compliant(image,parameters->cp_cinema,p_manager)) {
- parameters->cp_rsiz = OPJ_STD_RSIZ;
+ if (OPJ_IS_CINEMA(parameters->rsiz)){
+ if ((parameters->rsiz == OPJ_PROFILE_CINEMA_S2K)
+ || (parameters->rsiz == OPJ_PROFILE_CINEMA_S4K)){
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Scalable Digital Cinema profiles not yet supported\n");
+ parameters->rsiz = OPJ_PROFILE_NONE;
+ } else {
+ opj_j2k_set_cinema_parameters(parameters,image,p_manager);
+ if (!opj_j2k_is_cinema_compliant(image,parameters->rsiz,p_manager)) {
+ parameters->rsiz = OPJ_PROFILE_NONE;
+ }
+ }
+ } else if (OPJ_IS_STORAGE(parameters->rsiz)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Long Term Storage profile not yet supported\n");
+ parameters->rsiz = OPJ_PROFILE_NONE;
+ } else if (OPJ_IS_BROADCAST(parameters->rsiz)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Broadcast profiles not yet supported\n");
+ parameters->rsiz = OPJ_PROFILE_NONE;
+ } else if (OPJ_IS_IMF(parameters->rsiz)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 IMF profiles not yet supported\n");
+ parameters->rsiz = OPJ_PROFILE_NONE;
+ } else if (OPJ_IS_PART2(parameters->rsiz)) {
+ if (parameters->rsiz == ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_NONE))) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "JPEG 2000 Part-2 profile defined\n"
+ "but no Part-2 extension enabled.\n"
+ "Profile set to NONE.\n");
+ parameters->rsiz = OPJ_PROFILE_NONE;
+ } else if (parameters->rsiz != ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT))) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "Unsupported Part-2 extension enabled\n"
+ "Profile set to NONE.\n");
+ parameters->rsiz = OPJ_PROFILE_NONE;
}
}
/*
copy user encoding parameters
*/
- cp->m_specific_param.m_enc.m_cinema = parameters->cp_cinema;
- cp->m_specific_param.m_enc.m_max_comp_size = parameters->max_comp_size;
- cp->rsiz = parameters->cp_rsiz;
- cp->m_specific_param.m_enc.m_disto_alloc = parameters->cp_disto_alloc;
- cp->m_specific_param.m_enc.m_fixed_alloc = parameters->cp_fixed_alloc;
- cp->m_specific_param.m_enc.m_fixed_quality = parameters->cp_fixed_quality;
+ cp->m_specific_param.m_enc.m_max_comp_size = (OPJ_UINT32)parameters->max_comp_size;
+ cp->rsiz = parameters->rsiz;
+ cp->m_specific_param.m_enc.m_disto_alloc = (OPJ_UINT32)parameters->cp_disto_alloc & 1u;
+ cp->m_specific_param.m_enc.m_fixed_alloc = (OPJ_UINT32)parameters->cp_fixed_alloc & 1u;
+ cp->m_specific_param.m_enc.m_fixed_quality = (OPJ_UINT32)parameters->cp_fixed_quality & 1u;
/* mod fixed_quality */
if (parameters->cp_fixed_alloc && parameters->cp_matrice) {
- size_t array_size = parameters->tcp_numlayers * parameters->numresolution * 3 * sizeof(OPJ_INT32);
+ size_t array_size = (size_t)parameters->tcp_numlayers * (size_t)parameters->numresolution * 3 * sizeof(OPJ_INT32);
cp->m_specific_param.m_enc.m_matrice = (OPJ_INT32 *) opj_malloc(array_size);
+ if (!cp->m_specific_param.m_enc.m_matrice) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate copy of user encoding parameters matrix \n");
+ return OPJ_FALSE;
+ }
memcpy(cp->m_specific_param.m_enc.m_matrice, parameters->cp_matrice, array_size);
}
/* tiles */
- cp->tdx = parameters->cp_tdx;
- cp->tdy = parameters->cp_tdy;
+ cp->tdx = (OPJ_UINT32)parameters->cp_tdx;
+ cp->tdy = (OPJ_UINT32)parameters->cp_tdy;
/* tile offset */
- cp->tx0 = parameters->cp_tx0;
- cp->ty0 = parameters->cp_ty0;
+ cp->tx0 = (OPJ_UINT32)parameters->cp_tx0;
+ cp->ty0 = (OPJ_UINT32)parameters->cp_ty0;
/* comment string */
if(parameters->cp_comment) {
- cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1);
- if(cp->comment) {
- strcpy(cp->comment, parameters->cp_comment);
- }
- }
+ cp->comment = (char*)opj_malloc(strlen(parameters->cp_comment) + 1U);
+ if(!cp->comment) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate copy of comment string\n");
+ return OPJ_FALSE;
+ }
+ strcpy(cp->comment, parameters->cp_comment);
+ } else {
+ /* Create default comment for codestream */
+ const char comment[] = "Created by OpenJPEG version ";
+ const size_t clen = strlen(comment);
+ const char *version = opj_version();
+
+ /* UniPG>> */
+#ifdef USE_JPWL
+ cp->comment = (char*)opj_malloc(clen+strlen(version)+11);
+ if(!cp->comment) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate comment string\n");
+ return OPJ_FALSE;
+ }
+ sprintf(cp->comment,"%s%s with JPWL", comment, version);
+#else
+ cp->comment = (char*)opj_malloc(clen+strlen(version)+1);
+ if(!cp->comment) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate comment string\n");
+ return OPJ_FALSE;
+ }
+ sprintf(cp->comment,"%s%s", comment, version);
+#endif
+ /* <<UniPG */
+ }
/*
calculate other encoding parameters
*/
if (parameters->tile_size_on) {
- cp->tw = opj_int_ceildiv(image->x1 - cp->tx0, cp->tdx);
- cp->th = opj_int_ceildiv(image->y1 - cp->ty0, cp->tdy);
+ cp->tw = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->x1 - cp->tx0), (OPJ_INT32)cp->tdx);
+ cp->th = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)(image->y1 - cp->ty0), (OPJ_INT32)cp->tdy);
} else {
cp->tdx = image->x1 - cp->tx0;
cp->tdy = image->y1 - cp->ty0;
}
if (parameters->tp_on) {
- cp->m_specific_param.m_enc.m_tp_flag = parameters->tp_flag;
+ cp->m_specific_param.m_enc.m_tp_flag = (OPJ_BYTE)parameters->tp_flag;
cp->m_specific_param.m_enc.m_tp_on = 1;
}
/* initialize the mutiple tiles */
/* ---------------------------- */
cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
+ if (!cp->tcps) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate tile coding parameters\n");
+ return OPJ_FALSE;
+ }
if (parameters->numpocs) {
/* initialisation of POC */
- opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, parameters->numresolution, image->numcomps, parameters->tcp_numlayers, p_manager);
+ opj_j2k_check_poc_val(parameters->POC,parameters->numpocs, (OPJ_UINT32)parameters->numresolution, image->numcomps, (OPJ_UINT32)parameters->tcp_numlayers, p_manager);
/* TODO MSD use the return value*/
}
for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
opj_tcp_t *tcp = &cp->tcps[tileno];
- tcp->numlayers = parameters->tcp_numlayers;
+ tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers;
for (j = 0; j < tcp->numlayers; j++) {
- if(cp->m_specific_param.m_enc.m_cinema){
+ if(OPJ_IS_CINEMA(cp->rsiz)){
if (cp->m_specific_param.m_enc.m_fixed_quality) {
tcp->distoratio[j] = parameters->tcp_distoratio[j];
}
}
}
- tcp->csty = parameters->csty;
+ tcp->csty = (OPJ_UINT32)parameters->csty;
tcp->prg = parameters->prog_order;
- tcp->mct = parameters->tcp_mct;
+ tcp->mct = (OPJ_UINT32)parameters->tcp_mct;
numpocs_tile = 0;
tcp->POC = 0;
}
tcp->tccps = (opj_tccp_t*) opj_calloc(image->numcomps, sizeof(opj_tccp_t));
-
+ if (!tcp->tccps) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate tile component coding parameters\n");
+ return OPJ_FALSE;
+ }
if (parameters->mct_data) {
OPJ_UINT32 lMctSize = image->numcomps * image->numcomps * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
OPJ_FLOAT32 * lTmpBuf = (OPJ_FLOAT32*)opj_malloc(lMctSize);
OPJ_INT32 * l_dc_shift = (OPJ_INT32 *) ((OPJ_BYTE *) parameters->mct_data + lMctSize);
+ if (!lTmpBuf) {
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate temp buffer\n");
+ return OPJ_FALSE;
+ }
+
tcp->mct = 2;
tcp->m_mct_coding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
+ if (! tcp->m_mct_coding_matrix) {
+ opj_free(lTmpBuf);
+ lTmpBuf = NULL;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate encoder MCT coding matrix \n");
+ return OPJ_FALSE;
+ }
memcpy(tcp->m_mct_coding_matrix,parameters->mct_data,lMctSize);
memcpy(lTmpBuf,parameters->mct_data,lMctSize);
tcp->m_mct_decoding_matrix = (OPJ_FLOAT32*)opj_malloc(lMctSize);
- assert(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps));
+ if (! tcp->m_mct_decoding_matrix) {
+ opj_free(lTmpBuf);
+ lTmpBuf = NULL;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate encoder MCT decoding matrix \n");
+ return OPJ_FALSE;
+ }
+ if(opj_matrix_inversion_f(lTmpBuf,(tcp->m_mct_decoding_matrix),image->numcomps) == OPJ_FALSE) {
+ opj_free(lTmpBuf);
+ lTmpBuf = NULL;
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to inverse encoder MCT decoding matrix \n");
+ return OPJ_FALSE;
+ }
tcp->mct_norms = (OPJ_FLOAT64*)
opj_malloc(image->numcomps * sizeof(OPJ_FLOAT64));
-
+ if (! tcp->mct_norms) {
+ opj_free(lTmpBuf);
+ lTmpBuf = NULL;
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to allocate encoder MCT norms \n");
+ return OPJ_FALSE;
+ }
opj_calculate_norms(tcp->mct_norms,image->numcomps,tcp->m_mct_decoding_matrix);
opj_free(lTmpBuf);
tccp->m_dc_level_shift = l_dc_shift[i];
}
- opj_j2k_setup_mct_encoding(tcp,image);
+ if (opj_j2k_setup_mct_encoding(tcp,image) == OPJ_FALSE) {
+ /* free will be handled by opj_j2k_destroy */
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to setup j2k mct encoding\n");
+ return OPJ_FALSE;
+ }
}
else {
+ if(tcp->mct==1 && image->numcomps >= 3) { /* RGB->YCC MCT is enabled */
+ if ((image->comps[0].dx != image->comps[1].dx) ||
+ (image->comps[0].dx != image->comps[2].dx) ||
+ (image->comps[0].dy != image->comps[1].dy) ||
+ (image->comps[0].dy != image->comps[2].dy)) {
+ opj_event_msg(p_manager, EVT_WARNING, "Cannot perform MCT on components with different sizes. Disabling MCT.\n");
+ tcp->mct = 0;
+ }
+ }
for (i = 0; i < image->numcomps; i++) {
opj_tccp_t *tccp = &tcp->tccps[i];
opj_image_comp_t * l_comp = &(image->comps[i]);
opj_tccp_t *tccp = &tcp->tccps[i];
tccp->csty = parameters->csty & 0x01; /* 0 => one precinct || 1 => custom precinct */
- tccp->numresolutions = parameters->numresolution;
- tccp->cblkw = opj_int_floorlog2(parameters->cblockw_init);
- tccp->cblkh = opj_int_floorlog2(parameters->cblockh_init);
- tccp->cblksty = parameters->mode;
+ tccp->numresolutions = (OPJ_UINT32)parameters->numresolution;
+ tccp->cblkw = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockw_init);
+ tccp->cblkh = (OPJ_UINT32)opj_int_floorlog2(parameters->cblockh_init);
+ tccp->cblksty = (OPJ_UINT32)parameters->mode;
tccp->qmfbid = parameters->irreversible ? 0 : 1;
tccp->qntsty = parameters->irreversible ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
tccp->numgbits = 2;
if (parameters->csty & J2K_CCP_CSTY_PRT) {
OPJ_INT32 p = 0, it_res;
assert( tccp->numresolutions > 0 );
- for (it_res = tccp->numresolutions - 1; it_res >= 0; it_res--) {
+ for (it_res = (OPJ_INT32)tccp->numresolutions - 1; it_res >= 0; it_res--) {
if (p < parameters->res_spec) {
if (parameters->prcw_init[p] < 1) {
tccp->prcw[it_res] = 1;
} else {
- tccp->prcw[it_res] = opj_int_floorlog2(parameters->prcw_init[p]);
+ tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prcw_init[p]);
}
if (parameters->prch_init[p] < 1) {
tccp->prch[it_res] = 1;
}else {
- tccp->prch[it_res] = opj_int_floorlog2(parameters->prch_init[p]);
+ tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(parameters->prch_init[p]);
}
} else {
if (size_prcw < 1) {
tccp->prcw[it_res] = 1;
} else {
- tccp->prcw[it_res] = opj_int_floorlog2(size_prcw);
+ tccp->prcw[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prcw);
}
if (size_prch < 1) {
tccp->prch[it_res] = 1;
} else {
- tccp->prch[it_res] = opj_int_floorlog2(size_prch);
+ tccp->prch[it_res] = (OPJ_UINT32)opj_int_floorlog2(size_prch);
}
}
p++;
opj_free(parameters->mct_data);
parameters->mct_data = 00;
}
+ return OPJ_TRUE;
}
static OPJ_BOOL opj_j2k_add_mhmarker(opj_codestream_index_t *cstr_index, OPJ_UINT32 type, OPJ_OFF_T pos, OPJ_UINT32 len)
/* expand the list? */
if ((cstr_index->marknum + 1) > cstr_index->maxmarknum) {
opj_marker_info_t *new_marker;
- cstr_index->maxmarknum = 100 + (OPJ_INT32) ((OPJ_FLOAT32) cstr_index->maxmarknum * 1.0F);
+ cstr_index->maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) cstr_index->maxmarknum);
new_marker = (opj_marker_info_t *) opj_realloc(cstr_index->marker, cstr_index->maxmarknum *sizeof(opj_marker_info_t));
if (! new_marker) {
opj_free(cstr_index->marker);
/* expand the list? */
if ((cstr_index->tile_index[tileno].marknum + 1) > cstr_index->tile_index[tileno].maxmarknum) {
opj_marker_info_t *new_marker;
- cstr_index->tile_index[tileno].maxmarknum = 100 + (OPJ_INT32) ((OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum * 1.0F);
+ cstr_index->tile_index[tileno].maxmarknum = (OPJ_UINT32)(100 + (OPJ_FLOAT32) cstr_index->tile_index[tileno].maxmarknum);
new_marker = (opj_marker_info_t *) opj_realloc(
cstr_index->tile_index[tileno].marker,
cstr_index->tile_index[tileno].maxmarknum *sizeof(opj_marker_info_t));
/* make sure a validation list is present */
l_is_valid &= (p_j2k->m_validation_list != 00);
- if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
+ /* ISO 15444-1:2004 states between 1 & 33 (0 -> 32) */
+ /* 33 (32) would always fail the check below (if a cast to 64bits was done) */
+ /* FIXME Shall we change OPJ_J2K_MAXRLVLS to 32 ? */
+ if ((p_j2k->m_cp.tcps->tccps->numresolutions <= 0) || (p_j2k->m_cp.tcps->tccps->numresolutions > 32)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
+ return OPJ_FALSE;
+ }
+
+ if ((p_j2k->m_cp.tdx) < (OPJ_UINT32) (1 << (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) {
opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
return OPJ_FALSE;
}
- if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << p_j2k->m_cp.tcps->tccps->numresolutions)) {
+ if ((p_j2k->m_cp.tdy) < (OPJ_UINT32) (1 << (p_j2k->m_cp.tcps->tccps->numresolutions - 1U))) {
opj_event_msg(p_manager, EVT_ERROR, "Number of resolutions is too high in comparison to the size of tiles\n");
return OPJ_FALSE;
}
OPJ_UINT32 l_current_marker;
OPJ_UINT32 l_marker_size;
const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
+ OPJ_BOOL l_has_siz = 0;
+ OPJ_BOOL l_has_cod = 0;
+ OPJ_BOOL l_has_qcd = 0;
/* preconditions */
assert(p_stream != 00);
l_marker_handler = opj_j2k_get_marker_handler(l_current_marker);
}
+ if (l_marker_handler->id == J2K_MS_SIZ) {
+ /* Mark required SIZ marker as found */
+ l_has_siz = 1;
+ }
+ if (l_marker_handler->id == J2K_MS_COD) {
+ /* Mark required COD marker as found */
+ l_has_cod = 1;
+ }
+ if (l_marker_handler->id == J2K_MS_QCD) {
+ /* Mark required QCD marker as found */
+ l_has_qcd = 1;
+ }
+
/* Check if the marker is known and if it is the right place to find it */
if (! (p_j2k->m_specific_param.m_decoder.m_state & l_marker_handler->states) ) {
opj_event_msg(p_manager, EVT_ERROR, "Marker is not compliant with its position\n");
opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_current_marker,2);
}
+ if (l_has_siz == 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "required SIZ marker not found in main header\n");
+ return OPJ_FALSE;
+ }
+ if (l_has_cod == 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "required COD marker not found in main header\n");
+ return OPJ_FALSE;
+ }
+ if (l_has_qcd == 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "required QCD marker not found in main header\n");
+ return OPJ_FALSE;
+ }
+
opj_event_msg(p_manager, EVT_INFO, "Main header has been correctly decoded.\n");
/* Position of the last element if the main header */
/* Read 2 bytes from the buffer as the marker size */
opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,&l_marker_size,2);
+ /* Check marker size (does not include marker ID but includes marker size) */
+ if (l_marker_size < 2) {
+ opj_event_msg(p_manager, EVT_ERROR, "Inconsistent marker size\n");
+ return OPJ_FALSE;
+ }
+
/* cf. https://code.google.com/p/openjpeg/issues/detail?id=226 */
if (l_current_marker == 0x8080 && opj_stream_get_number_byte_left(p_stream) == 0) {
p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
/* Check if the marker size is compatible with the header data size */
if (l_marker_size > p_j2k->m_specific_param.m_decoder.m_header_data_size) {
- OPJ_BYTE *new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
+ OPJ_BYTE *new_header_data = NULL;
+ /* If we are here, this means we consider this marker as known & we will read it */
+ /* Check enough bytes left in stream before allocation */
+ if ((OPJ_OFF_T)l_marker_size > opj_stream_get_number_byte_left(p_stream)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Marker size inconsistent with stream length\n");
+ return OPJ_FALSE;
+ }
+ new_header_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_decoder.m_header_data, l_marker_size);
if (! new_header_data) {
opj_free(p_j2k->m_specific_param.m_decoder.m_header_data);
p_j2k->m_specific_param.m_decoder.m_header_data = NULL;
}
opj_event_msg(p_manager, EVT_INFO, "Header of tile %d / %d has been read.\n",
- p_j2k->m_current_tile_number, (p_j2k->m_cp.th * p_j2k->m_cp.tw) - 1);
+ p_j2k->m_current_tile_number+1, (p_j2k->m_cp.th * p_j2k->m_cp.tw));
*p_tile_index = p_j2k->m_current_tile_number;
*p_go_on = OPJ_TRUE;
p_j2k->cstr_index) ) {
opj_j2k_tcp_destroy(l_tcp);
p_j2k->m_specific_param.m_decoder.m_state |= 0x8000;/*FIXME J2K_DEC_STATE_ERR;*/
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n");
return OPJ_FALSE;
}
opj_j2k_tcp_data_destroy(l_tcp);
p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
- p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080));/* FIXME J2K_DEC_STATE_DATA);*/
+ p_j2k->m_specific_param.m_decoder.m_state &= (~ (0x0080u));/* FIXME J2K_DEC_STATE_DATA);*/
if(opj_stream_get_number_byte_left(p_stream) == 0
&& p_j2k->m_specific_param.m_decoder.m_state == J2K_STATE_NEOC){
p_j2k->m_specific_param.m_decoder.m_state = 0x0100;/*FIXME J2K_DEC_STATE_EOC;*/
}
else if (l_current_marker != J2K_MS_SOT)
- {
- opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
-
+ {
if(opj_stream_get_number_byte_left(p_stream) == 0) {
p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_NEOC;
+ opj_event_msg(p_manager, EVT_WARNING, "Stream does not end with EOC\n");
return OPJ_TRUE;
}
+ opj_event_msg(p_manager, EVT_ERROR, "Stream too short, expected SOT\n");
return OPJ_FALSE;
}
}
l_res->x0, l_res->x1, l_res->y0, l_res->y1);
}*/
- l_width_src = (l_res->x1 - l_res->x0);
- l_height_src = (l_res->y1 - l_res->y0);
+ l_width_src = (OPJ_UINT32)(l_res->x1 - l_res->x0);
+ l_height_src = (OPJ_UINT32)(l_res->y1 - l_res->y0);
/* Border of the current output component*/
- l_x0_dest = opj_int_ceildivpow2(l_img_comp_dest->x0, l_img_comp_dest->factor);
- l_y0_dest = opj_int_ceildivpow2(l_img_comp_dest->y0, l_img_comp_dest->factor);
+ l_x0_dest = (OPJ_UINT32)opj_int_ceildivpow2((OPJ_INT32)l_img_comp_dest->x0, (OPJ_INT32)l_img_comp_dest->factor);
+ l_y0_dest = (OPJ_UINT32)opj_int_ceildivpow2((OPJ_INT32)l_img_comp_dest->y0, (OPJ_INT32)l_img_comp_dest->factor);
l_x1_dest = l_x0_dest + l_img_comp_dest->w;
l_y1_dest = l_y0_dest + l_img_comp_dest->h;
assert( l_res->x0 >= 0);
assert( l_res->x1 >= 0);
if ( l_x0_dest < (OPJ_UINT32)l_res->x0 ) {
- l_start_x_dest = l_res->x0 - l_x0_dest;
+ l_start_x_dest = (OPJ_UINT32)l_res->x0 - l_x0_dest;
l_offset_x0_src = 0;
if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
l_offset_x1_src = 0;
}
else {
- l_width_dest = l_x1_dest - l_res->x0 ;
- l_offset_x1_src = l_width_src - l_width_dest;
+ l_width_dest = l_x1_dest - (OPJ_UINT32)l_res->x0 ;
+ l_offset_x1_src = (OPJ_INT32)(l_width_src - l_width_dest);
}
}
else {
l_start_x_dest = 0 ;
- l_offset_x0_src = l_x0_dest - l_res->x0;
+ l_offset_x0_src = (OPJ_INT32)l_x0_dest - l_res->x0;
if ( l_x1_dest >= (OPJ_UINT32)l_res->x1 ) {
- l_width_dest = l_width_src - l_offset_x0_src;
+ l_width_dest = l_width_src - (OPJ_UINT32)l_offset_x0_src;
l_offset_x1_src = 0;
}
else {
l_width_dest = l_img_comp_dest->w ;
- l_offset_x1_src = l_res->x1 - l_x1_dest;
+ l_offset_x1_src = l_res->x1 - (OPJ_INT32)l_x1_dest;
}
}
if ( l_y0_dest < (OPJ_UINT32)l_res->y0 ) {
- l_start_y_dest = l_res->y0 - l_y0_dest;
+ l_start_y_dest = (OPJ_UINT32)l_res->y0 - l_y0_dest;
l_offset_y0_src = 0;
if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
l_offset_y1_src = 0;
}
else {
- l_height_dest = l_y1_dest - l_res->y0 ;
- l_offset_y1_src = l_height_src - l_height_dest;
+ l_height_dest = l_y1_dest - (OPJ_UINT32)l_res->y0 ;
+ l_offset_y1_src = (OPJ_INT32)(l_height_src - l_height_dest);
}
}
else {
l_start_y_dest = 0 ;
- l_offset_y0_src = l_y0_dest - l_res->y0;
+ l_offset_y0_src = (OPJ_INT32)l_y0_dest - l_res->y0;
if ( l_y1_dest >= (OPJ_UINT32)l_res->y1 ) {
- l_height_dest = l_height_src - l_offset_y0_src;
+ l_height_dest = l_height_src - (OPJ_UINT32)l_offset_y0_src;
l_offset_y1_src = 0;
}
else {
l_height_dest = l_img_comp_dest->h ;
- l_offset_y1_src = l_res->y1 - l_y1_dest;
+ l_offset_y1_src = l_res->y1 - (OPJ_INT32)l_y1_dest;
}
}
/*-----*/
/* Compute the input buffer offset */
- l_start_offset_src = l_offset_x0_src + l_offset_y0_src * l_width_src;
+ l_start_offset_src = l_offset_x0_src + l_offset_y0_src * (OPJ_INT32)l_width_src;
l_line_offset_src = l_offset_x1_src + l_offset_x0_src;
- l_end_offset_src = l_offset_y1_src * l_width_src - l_offset_x0_src;
+ l_end_offset_src = l_offset_y1_src * (OPJ_INT32)l_width_src - l_offset_x0_src;
/* Compute the output buffer offset */
- l_start_offset_dest = l_start_x_dest + l_start_y_dest * l_img_comp_dest->w;
- l_line_offset_dest = l_img_comp_dest->w - l_width_dest;
+ l_start_offset_dest = (OPJ_INT32)(l_start_x_dest + l_start_y_dest * l_img_comp_dest->w);
+ l_line_offset_dest = (OPJ_INT32)(l_img_comp_dest->w - l_width_dest);
/* Move the output buffer to the first place where we will write*/
l_dest_ptr = l_img_comp_dest->data + l_start_offset_dest;
p_image->x0 = l_image->x0;
}
else {
- p_j2k->m_specific_param.m_decoder.m_start_tile_x = (p_start_x - l_cp->tx0) / l_cp->tdx;
- p_image->x0 = p_start_x;
+ p_j2k->m_specific_param.m_decoder.m_start_tile_x = ((OPJ_UINT32)p_start_x - l_cp->tx0) / l_cp->tdx;
+ p_image->x0 = (OPJ_UINT32)p_start_x;
}
/* Up */
p_image->y0 = l_image->y0;
}
else {
- p_j2k->m_specific_param.m_decoder.m_start_tile_y = (p_start_y - l_cp->ty0) / l_cp->tdy;
- p_image->y0 = p_start_y;
+ p_j2k->m_specific_param.m_decoder.m_start_tile_y = ((OPJ_UINT32)p_start_y - l_cp->ty0) / l_cp->tdy;
+ p_image->y0 = (OPJ_UINT32)p_start_y;
}
/* Right */
p_image->x1 = l_image->x1;
}
else {
- p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_int_ceildiv((p_end_x - l_cp->tx0), l_cp->tdx);
- p_image->x1 = p_end_x;
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = (OPJ_UINT32)opj_int_ceildiv(p_end_x - (OPJ_INT32)l_cp->tx0, (OPJ_INT32)l_cp->tdx);
+ p_image->x1 = (OPJ_UINT32)p_end_x;
}
/* Bottom */
p_image->y1 = l_image->y1;
}
else{
- p_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_int_ceildiv((p_end_y - l_cp->ty0), l_cp->tdy);
- p_image->y1 = p_end_y;
+ p_j2k->m_specific_param.m_decoder.m_end_tile_y = (OPJ_UINT32)opj_int_ceildiv(p_end_y - (OPJ_INT32)l_cp->ty0, (OPJ_INT32)l_cp->tdy);
+ p_image->y1 = (OPJ_UINT32)p_end_y;
}
/* ----- */
{
OPJ_INT32 l_h,l_w;
- l_img_comp->x0 = opj_int_ceildiv(p_image->x0, l_img_comp->dx);
- l_img_comp->y0 = opj_int_ceildiv(p_image->y0, l_img_comp->dy);
- l_comp_x1 = opj_int_ceildiv(p_image->x1, l_img_comp->dx);
- l_comp_y1 = opj_int_ceildiv(p_image->y1, l_img_comp->dy);
+ l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0, (OPJ_INT32)l_img_comp->dx);
+ l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0, (OPJ_INT32)l_img_comp->dy);
+ l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);
+ l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);
- l_w = opj_int_ceildivpow2(l_comp_x1, l_img_comp->factor)
- - opj_int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
+ l_w = opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor)
+ - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor);
if (l_w < 0){
opj_event_msg(p_manager, EVT_ERROR,
"Size x of the decoded component image is incorrect (comp[%d].w=%d).\n",
it_comp, l_w);
return OPJ_FALSE;
}
- l_img_comp->w = l_w;
+ l_img_comp->w = (OPJ_UINT32)l_w;
- l_h = opj_int_ceildivpow2(l_comp_y1, l_img_comp->factor)
- - opj_int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
+ l_h = opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor)
+ - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor);
if (l_h < 0){
opj_event_msg(p_manager, EVT_ERROR,
"Size y of the decoded component image is incorrect (comp[%d].h=%d).\n",
it_comp, l_h);
return OPJ_FALSE;
}
- l_img_comp->h = l_h;
+ l_img_comp->h = (OPJ_UINT32)l_h;
l_img_comp++;
}
opj_j2k_t* opj_j2k_create_decompress(void)
{
- opj_j2k_t *l_j2k = (opj_j2k_t*) opj_malloc(sizeof(opj_j2k_t));
+ opj_j2k_t *l_j2k = (opj_j2k_t*) opj_calloc(1,sizeof(opj_j2k_t));
if (!l_j2k) {
return 00;
}
- memset(l_j2k,0,sizeof(opj_j2k_t));
l_j2k->m_is_decoder = 1;
l_j2k->m_cp.m_is_decoder = 1;
- l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_malloc(sizeof(opj_tcp_t));
+ l_j2k->m_specific_param.m_decoder.m_default_tcp = (opj_tcp_t*) opj_calloc(1,sizeof(opj_tcp_t));
if (!l_j2k->m_specific_param.m_decoder.m_default_tcp) {
opj_j2k_destroy(l_j2k);
return 00;
}
- memset(l_j2k->m_specific_param.m_decoder.m_default_tcp,0,sizeof(opj_tcp_t));
- l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_malloc(OPJ_J2K_DEFAULT_HEADER_SIZE);
+ l_j2k->m_specific_param.m_decoder.m_header_data = (OPJ_BYTE *) opj_calloc(1,OPJ_J2K_DEFAULT_HEADER_SIZE);
if (! l_j2k->m_specific_param.m_decoder.m_header_data) {
opj_j2k_destroy(l_j2k);
return 00;
/* codestream index creation */
l_j2k->cstr_index = opj_j2k_create_cstr_index();
-
- /*(opj_codestream_index_t*) opj_malloc(sizeof(opj_codestream_index_t));
if (!l_j2k->cstr_index){
opj_j2k_destroy(l_j2k);
- return NULL;
+ return 00;
}
- l_j2k->cstr_index->marker = (opj_marker_info_t*) opj_malloc(100 * sizeof(opj_marker_info_t));
-*/
-
/* validation list creation */
l_j2k->m_validation_list = opj_procedure_list_create();
if (! l_j2k->m_validation_list) {
for (i = 0; i < l_tccp->numresolutions; ++i) {
opj_read_bytes(l_current_ptr,&l_tmp ,1); /* SPcoc (I_i) */
++l_current_ptr;
+ /* Precinct exponent 0 is only allowed for lowest resolution level (Table A.21) */
+ if ((i != 0) && (((l_tmp & 0xf) == 0) || ((l_tmp >> 4) == 0))) {
+ opj_event_msg(p_manager, EVT_ERROR, "Invalid precinct size\n");
+ return OPJ_FALSE;
+ }
l_tccp->prcw[i] = l_tmp & 0xf;
l_tccp->prch[i] = l_tmp >> 4;
}
++p_data;
for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
- l_expn = l_tccp->stepsizes[l_band_no].expn;
+ l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;
opj_write_bytes(p_data, l_expn << 3, 1); /* SPqcx_i */
++p_data;
}
++p_data;
for (l_band_no = 0; l_band_no < l_num_bands; ++l_band_no) {
- l_expn = l_tccp->stepsizes[l_band_no].expn;
- l_mant = l_tccp->stepsizes[l_band_no].mant;
+ l_expn = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].expn;
+ l_mant = (OPJ_UINT32)l_tccp->stepsizes[l_band_no].mant;
opj_write_bytes(p_data, (l_expn << 11) + l_mant, 2); /* SPqcx_i */
p_data += 2;
opj_read_bytes(l_current_ptr, &l_tmp ,1); /* SPqcx_i */
++l_current_ptr;
if (l_band_no < OPJ_J2K_MAXBANDS){
- l_tccp->stepsizes[l_band_no].expn = l_tmp>>3;
+ l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 3);
l_tccp->stepsizes[l_band_no].mant = 0;
}
}
opj_read_bytes(l_current_ptr, &l_tmp ,2); /* SPqcx_i */
l_current_ptr+=2;
if (l_band_no < OPJ_J2K_MAXBANDS){
- l_tccp->stepsizes[l_band_no].expn = l_tmp >> 11;
+ l_tccp->stepsizes[l_band_no].expn = (OPJ_INT32)(l_tmp >> 11);
l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
}
}
if (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) {
for (l_band_no = 1; l_band_no < OPJ_J2K_MAXBANDS; l_band_no++) {
l_tccp->stepsizes[l_band_no].expn =
- ((l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) > 0) ?
- (l_tccp->stepsizes[0].expn) - ((l_band_no - 1) / 3) : 0;
+ ((OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) > 0) ?
+ (OPJ_INT32)(l_tccp->stepsizes[0].expn) - (OPJ_INT32)((l_band_no - 1) / 3) : 0;
l_tccp->stepsizes[l_band_no].mant = l_tccp->stepsizes[0].mant;
}
}
}
}
+static void opj_j2k_dump_tile_info( opj_tcp_t * l_default_tile,OPJ_INT32 numcomps,FILE* out_stream)
+{
+ if (l_default_tile)
+ {
+ OPJ_INT32 compno;
+
+ fprintf(out_stream, "\t default tile {\n");
+ fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
+ fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
+ fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
+ fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
+
+ for (compno = 0; compno < numcomps; compno++) {
+ opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
+ OPJ_UINT32 resno;
+ OPJ_INT32 bandno, numbands;
+
+ /* coding style*/
+ fprintf(out_stream, "\t\t comp %d {\n", compno);
+ fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
+ fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
+ fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
+ fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
+ fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
+ fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
+
+ fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
+ for (resno = 0; resno < l_tccp->numresolutions; resno++) {
+ fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
+ }
+ fprintf(out_stream, "\n");
+
+ /* quantization style*/
+ fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
+ fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
+ fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
+ numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (OPJ_INT32)l_tccp->numresolutions * 3 - 2;
+ for (bandno = 0; bandno < numbands; bandno++) {
+ fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
+ l_tccp->stepsizes[bandno].expn);
+ }
+ fprintf(out_stream, "\n");
+
+ /* RGN value*/
+ fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
+
+ fprintf(out_stream, "\t\t }\n");
+ } /*end of component of default tile*/
+ fprintf(out_stream, "\t }\n"); /*end of default tile*/
+ }
+}
+
void j2k_dump (opj_j2k_t* p_j2k, OPJ_INT32 flag, FILE* out_stream)
{
/* Check if the flag is compatible with j2k file*/
if (flag & OPJ_J2K_MH_INFO){
opj_j2k_dump_MH_info(p_j2k, out_stream);
}
+ /* Dump all tile/codestream info */
+ if (flag & OPJ_J2K_TCH_INFO){
+ OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
+ OPJ_UINT32 i;
+ opj_tcp_t * l_tcp = p_j2k->m_cp.tcps;
+ for (i=0;i<l_nb_tiles;++i) {
+ opj_j2k_dump_tile_info( l_tcp,(OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream);
+ ++l_tcp;
+ }
+ }
/* Dump the codestream info of the current tile */
if (flag & OPJ_J2K_TH_INFO){
}
+
void opj_j2k_dump_MH_info(opj_j2k_t* p_j2k, FILE* out_stream)
{
- opj_tcp_t * l_default_tile=NULL;
fprintf(out_stream, "Codestream info from main header: {\n");
fprintf(out_stream, "\t tx0=%d, ty0=%d\n", p_j2k->m_cp.tx0, p_j2k->m_cp.ty0);
fprintf(out_stream, "\t tdx=%d, tdy=%d\n", p_j2k->m_cp.tdx, p_j2k->m_cp.tdy);
fprintf(out_stream, "\t tw=%d, th=%d\n", p_j2k->m_cp.tw, p_j2k->m_cp.th);
-
- l_default_tile = p_j2k->m_specific_param.m_decoder.m_default_tcp;
- if (l_default_tile)
- {
- OPJ_INT32 compno;
- OPJ_INT32 numcomps = p_j2k->m_private_image->numcomps;
-
- fprintf(out_stream, "\t default tile {\n");
- fprintf(out_stream, "\t\t csty=%#x\n", l_default_tile->csty);
- fprintf(out_stream, "\t\t prg=%#x\n", l_default_tile->prg);
- fprintf(out_stream, "\t\t numlayers=%d\n", l_default_tile->numlayers);
- fprintf(out_stream, "\t\t mct=%x\n", l_default_tile->mct);
-
- for (compno = 0; compno < numcomps; compno++) {
- opj_tccp_t *l_tccp = &(l_default_tile->tccps[compno]);
- OPJ_UINT32 resno;
- OPJ_INT32 bandno, numbands;
-
- /* coding style*/
- fprintf(out_stream, "\t\t comp %d {\n", compno);
- fprintf(out_stream, "\t\t\t csty=%#x\n", l_tccp->csty);
- fprintf(out_stream, "\t\t\t numresolutions=%d\n", l_tccp->numresolutions);
- fprintf(out_stream, "\t\t\t cblkw=2^%d\n", l_tccp->cblkw);
- fprintf(out_stream, "\t\t\t cblkh=2^%d\n", l_tccp->cblkh);
- fprintf(out_stream, "\t\t\t cblksty=%#x\n", l_tccp->cblksty);
- fprintf(out_stream, "\t\t\t qmfbid=%d\n", l_tccp->qmfbid);
-
- fprintf(out_stream, "\t\t\t preccintsize (w,h)=");
- for (resno = 0; resno < l_tccp->numresolutions; resno++) {
- fprintf(out_stream, "(%d,%d) ", l_tccp->prcw[resno], l_tccp->prch[resno]);
- }
- fprintf(out_stream, "\n");
-
- /* quantization style*/
- fprintf(out_stream, "\t\t\t qntsty=%d\n", l_tccp->qntsty);
- fprintf(out_stream, "\t\t\t numgbits=%d\n", l_tccp->numgbits);
- fprintf(out_stream, "\t\t\t stepsizes (m,e)=");
- numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
- for (bandno = 0; bandno < numbands; bandno++) {
- fprintf(out_stream, "(%d,%d) ", l_tccp->stepsizes[bandno].mant,
- l_tccp->stepsizes[bandno].expn);
- }
- fprintf(out_stream, "\n");
-
- /* RGN value*/
- fprintf(out_stream, "\t\t\t roishift=%d\n", l_tccp->roishift);
-
- fprintf(out_stream, "\t\t }\n");
- } /*end of component of default tile*/
- fprintf(out_stream, "\t }\n"); /*end of default tile*/
-
- }
-
+ opj_j2k_dump_tile_info(p_j2k->m_specific_param.m_decoder.m_default_tcp,(OPJ_INT32)p_j2k->m_private_image->numcomps, out_stream);
fprintf(out_stream, "}\n");
-
}
void j2k_dump_image_header(opj_image_t* img_header, OPJ_BOOL dev_dump_flag, FILE* out_stream)
l_tccp_info->qntsty = l_tccp->qntsty;
l_tccp_info->numgbits = l_tccp->numgbits;
- numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : l_tccp->numresolutions * 3 - 2;
+ numbands = (l_tccp->qntsty == J2K_CCP_QNTSTY_SIQNT) ? 1 : (OPJ_INT32)l_tccp->numresolutions * 3 - 2;
if (numbands < OPJ_J2K_MAXBANDS) {
for (bandno = 0; bandno < numbands; bandno++) {
- l_tccp_info->stepsizes_mant[bandno] = l_tccp->stepsizes[bandno].mant;
- l_tccp_info->stepsizes_expn[bandno] = l_tccp->stepsizes[bandno].expn;
+ l_tccp_info->stepsizes_mant[bandno] = (OPJ_UINT32)l_tccp->stepsizes[bandno].mant;
+ l_tccp_info->stepsizes_expn[bandno] = (OPJ_UINT32)l_tccp->stepsizes[bandno].expn;
}
}
if (! opj_j2k_decode_tile(p_j2k,l_current_tile_no,l_current_data,l_data_size,p_stream,p_manager)) {
opj_free(l_current_data);
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to decode tile %d/%d\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
return OPJ_FALSE;
}
opj_event_msg(p_manager, EVT_INFO, "Tile %d/%d has been decoded.\n", l_current_tile_no +1, p_j2k->m_cp.th * p_j2k->m_cp.tw);
}
}
/* Move into the codestream to the first SOT used to decode the desired tile */
- l_tile_no_to_dec = p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
+ l_tile_no_to_dec = (OPJ_UINT32)p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec;
if (p_j2k->cstr_index->tile_index)
if(p_j2k->cstr_index->tile_index->tp_index)
{
* so move to the last SOT read */
if ( !(opj_stream_read_seek(p_stream, p_j2k->m_specific_param.m_decoder.m_last_sot_read_pos+2, p_manager)) ){
opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
+ opj_free(l_current_data);
return OPJ_FALSE;
}
}
else{
if ( !(opj_stream_read_seek(p_stream, p_j2k->cstr_index->tile_index[l_tile_no_to_dec].tp_index[0].start_pos+2, p_manager)) ) {
opj_event_msg(p_manager, EVT_ERROR, "Problem with seek function\n");
+ opj_free(l_current_data);
return OPJ_FALSE;
}
}
for (compno = 0; compno < p_image->numcomps; compno++) {
p_image->comps[compno].resno_decoded = p_j2k->m_output_image->comps[compno].resno_decoded;
p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
+#if 0
+ char fn[256];
+ sprintf( fn, "/tmp/%d.raw", compno );
+ FILE *debug = fopen( fn, "wb" );
+ fwrite( p_image->comps[compno].data, sizeof(OPJ_INT32), p_image->comps[compno].w * p_image->comps[compno].h, debug );
+ fclose( debug );
+#endif
p_j2k->m_output_image->comps[compno].data = NULL;
}
l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
- l_img_comp->x0 = opj_int_ceildiv(p_image->x0, l_img_comp->dx);
- l_img_comp->y0 = opj_int_ceildiv(p_image->y0, l_img_comp->dy);
- l_comp_x1 = opj_int_ceildiv(p_image->x1, l_img_comp->dx);
- l_comp_y1 = opj_int_ceildiv(p_image->y1, l_img_comp->dy);
+ l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->x0, (OPJ_INT32)l_img_comp->dx);
+ l_img_comp->y0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_image->y0, (OPJ_INT32)l_img_comp->dy);
+ l_comp_x1 = opj_int_ceildiv((OPJ_INT32)p_image->x1, (OPJ_INT32)l_img_comp->dx);
+ l_comp_y1 = opj_int_ceildiv((OPJ_INT32)p_image->y1, (OPJ_INT32)l_img_comp->dy);
- l_img_comp->w = opj_int_ceildivpow2(l_comp_x1, l_img_comp->factor) - opj_int_ceildivpow2(l_img_comp->x0, l_img_comp->factor);
- l_img_comp->h = opj_int_ceildivpow2(l_comp_y1, l_img_comp->factor) - opj_int_ceildivpow2(l_img_comp->y0, l_img_comp->factor);
+ l_img_comp->w = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_x1, (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->x0, (OPJ_INT32)l_img_comp->factor));
+ l_img_comp->h = (OPJ_UINT32)(opj_int_ceildivpow2(l_comp_y1, (OPJ_INT32)l_img_comp->factor) - opj_int_ceildivpow2((OPJ_INT32)l_img_comp->y0, (OPJ_INT32)l_img_comp->factor));
l_img_comp++;
}
}
opj_copy_image_header(p_image, p_j2k->m_output_image);
- p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = tile_index;
+ p_j2k->m_specific_param.m_decoder.m_tile_ind_to_dec = (OPJ_INT32)tile_index;
/* customization of the decoding */
opj_j2k_setup_decoding_tile(p_j2k);
opj_stream_private_t *p_stream,
opj_event_mgr_t * p_manager )
{
- OPJ_UINT32 i;
+ OPJ_UINT32 i, j;
OPJ_UINT32 l_nb_tiles;
- OPJ_UINT32 l_max_tile_size, l_current_tile_size;
- OPJ_BYTE * l_current_data;
+ OPJ_UINT32 l_max_tile_size = 0, l_current_tile_size;
+ OPJ_BYTE * l_current_data = 00;
+ opj_tcd_t* p_tcd = 00;
/* preconditions */
assert(p_j2k != 00);
assert(p_stream != 00);
assert(p_manager != 00);
-
- l_current_data = (OPJ_BYTE*)opj_malloc(1000);
- if (! l_current_data) {
- opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");
- return OPJ_FALSE;
- }
- l_max_tile_size = 1000;
+
+ p_tcd = p_j2k->m_tcd;
l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
for (i=0;i<l_nb_tiles;++i) {
if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
- opj_free(l_current_data);
+ if (l_current_data) {
+ opj_free(l_current_data);
+ }
return OPJ_FALSE;
}
- l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd);
- if (l_current_tile_size > l_max_tile_size) {
- OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_current_tile_size);
- if (! l_new_current_data) {
- opj_free(l_current_data);
- opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");
- return OPJ_FALSE;
+ /* if we only have one tile, then simply set tile component data equal to image component data */
+ /* otherwise, allocate the data */
+ for (j=0;j<p_j2k->m_tcd->image->numcomps;++j) {
+ opj_tcd_tilecomp_t* l_tilec = p_tcd->tcd_image->tiles->comps + j;
+ if (l_nb_tiles == 1) {
+ opj_image_comp_t * l_img_comp = p_tcd->image->comps + j;
+ l_tilec->data = l_img_comp->data;
+ l_tilec->ownsData = OPJ_FALSE;
+ } else {
+ if(! opj_alloc_tile_component_data(l_tilec)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data." );
+ if (l_current_data) {
+ opj_free(l_current_data);
+ }
+ return OPJ_FALSE;
+ }
+ opj_alloc_tile_component_data(l_tilec);
}
- l_current_data = l_new_current_data;
- l_max_tile_size = l_current_tile_size;
}
+ l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd);
+ if (l_nb_tiles > 1) {
+ if (l_current_tile_size > l_max_tile_size) {
+ OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data, l_current_tile_size);
+ if (! l_new_current_data) {
+ if (l_current_data) {
+ opj_free(l_current_data);
+ }
+ opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to encode all tiles\n");
+ return OPJ_FALSE;
+ }
+ l_current_data = l_new_current_data;
+ l_max_tile_size = l_current_tile_size;
+ }
- opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
+ /* copy image data (32 bit) to l_current_data as contiguous, all-component, zero offset buffer */
+ /* 32 bit components @ 8 bit precision get converted to 8 bit */
+ /* 32 bit components @ 16 bit precision get converted to 16 bit */
+ opj_j2k_get_tile_data(p_j2k->m_tcd,l_current_data);
+
+ /* now copy this data into the tile component */
+ if (! opj_tcd_copy_tile_data(p_j2k->m_tcd,l_current_data,l_current_tile_size)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
+ return OPJ_FALSE;
+ }
+ }
- if (! opj_j2k_post_write_tile (p_j2k,l_current_data,l_current_tile_size,p_stream,p_manager)) {
+ if (! opj_j2k_post_write_tile (p_j2k,p_stream,p_manager)) {
return OPJ_FALSE;
}
}
- opj_free(l_current_data);
+ if (l_current_data) {
+ opj_free(l_current_data);
+ }
return OPJ_TRUE;
}
assert(p_manager != 00);
p_j2k->m_private_image = opj_image_create0();
+ if (! p_j2k->m_private_image) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to allocate image header." );
+ return OPJ_FALSE;
+ }
opj_copy_image_header(p_image, p_j2k->m_private_image);
/* TODO_MSD: Find a better way */
return OPJ_TRUE;
}
+void opj_get_tile_dimensions(opj_image_t * l_image,
+ opj_tcd_tilecomp_t * l_tilec,
+ opj_image_comp_t * l_img_comp,
+ OPJ_UINT32* l_size_comp,
+ OPJ_UINT32* l_width,
+ OPJ_UINT32* l_height,
+ OPJ_UINT32* l_offset_x,
+ OPJ_UINT32* l_offset_y,
+ OPJ_UINT32* l_image_width,
+ OPJ_UINT32* l_stride,
+ OPJ_UINT32* l_tile_offset) {
+ OPJ_UINT32 l_remaining;
+ *l_size_comp = l_img_comp->prec >> 3; /* (/8) */
+ l_remaining = l_img_comp->prec & 7; /* (%8) */
+ if (l_remaining) {
+ *l_size_comp += 1;
+ }
+
+ if (*l_size_comp == 3) {
+ *l_size_comp = 4;
+ }
+
+ *l_width = (OPJ_UINT32)(l_tilec->x1 - l_tilec->x0);
+ *l_height = (OPJ_UINT32)(l_tilec->y1 - l_tilec->y0);
+ *l_offset_x = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx);
+ *l_offset_y = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->y0, (OPJ_INT32)l_img_comp->dy);
+ *l_image_width = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)l_image->x1 - (OPJ_INT32)l_image->x0, (OPJ_INT32)l_img_comp->dx);
+ *l_stride = *l_image_width - *l_width;
+ *l_tile_offset = ((OPJ_UINT32)l_tilec->x0 - *l_offset_x) + ((OPJ_UINT32)l_tilec->y0 - *l_offset_y) * *l_image_width;
+}
+
void opj_j2k_get_tile_data (opj_tcd_t * p_tcd, OPJ_BYTE * p_data)
{
OPJ_UINT32 i,j,k = 0;
- OPJ_UINT32 l_width,l_height,l_stride, l_offset_x,l_offset_y, l_image_width;
- opj_image_comp_t * l_img_comp = 00;
- opj_tcd_tilecomp_t * l_tilec = 00;
- opj_image_t * l_image = 00;
- OPJ_UINT32 l_size_comp, l_remaining;
- OPJ_INT32 * l_src_ptr;
- l_tilec = p_tcd->tcd_image->tiles->comps;
- l_image = p_tcd->image;
- l_img_comp = l_image->comps;
for (i=0;i<p_tcd->image->numcomps;++i) {
- l_size_comp = l_img_comp->prec >> 3; /* (/8) */
- l_remaining = l_img_comp->prec & 7; /* (%8) */
- if (l_remaining) {
- ++l_size_comp;
- }
-
- if (l_size_comp == 3) {
- l_size_comp = 4;
- }
-
- l_width = (l_tilec->x1 - l_tilec->x0);
- l_height = (l_tilec->y1 - l_tilec->y0);
- l_offset_x = opj_int_ceildiv(l_image->x0, l_img_comp->dx);
- l_offset_y = opj_int_ceildiv(l_image->y0, l_img_comp->dy);
- l_image_width = opj_int_ceildiv(l_image->x1 - l_image->x0, l_img_comp->dx);
- l_stride = l_image_width - l_width;
- l_src_ptr = l_img_comp->data + (l_tilec->x0 - l_offset_x) + (l_tilec->y0 - l_offset_y) * l_image_width;
+ opj_image_t * l_image = p_tcd->image;
+ OPJ_INT32 * l_src_ptr;
+ opj_tcd_tilecomp_t * l_tilec = p_tcd->tcd_image->tiles->comps + i;
+ opj_image_comp_t * l_img_comp = l_image->comps + i;
+ OPJ_UINT32 l_size_comp,l_width,l_height,l_offset_x,l_offset_y, l_image_width,l_stride,l_tile_offset;
+
+ opj_get_tile_dimensions(l_image,
+ l_tilec,
+ l_img_comp,
+ &l_size_comp,
+ &l_width,
+ &l_height,
+ &l_offset_x,
+ &l_offset_y,
+ &l_image_width,
+ &l_stride,
+ &l_tile_offset);
+
+ l_src_ptr = l_img_comp->data + l_tile_offset;
switch (l_size_comp) {
case 1:
}
break;
}
-
- ++l_img_comp;
- ++l_tilec;
}
}
OPJ_BOOL opj_j2k_post_write_tile ( opj_j2k_t * p_j2k,
- OPJ_BYTE * p_data,
- OPJ_UINT32 p_data_size,
opj_stream_private_t *p_stream,
opj_event_mgr_t * p_manager )
{
- opj_tcd_t * l_tcd = 00;
OPJ_UINT32 l_nb_bytes_written;
OPJ_BYTE * l_current_data = 00;
OPJ_UINT32 l_tile_size = 0;
/* preconditions */
assert(p_j2k->m_specific_param.m_encoder.m_encoded_tile_data);
- l_tcd = p_j2k->m_tcd;
-
l_tile_size = p_j2k->m_specific_param.m_encoder.m_encoded_tile_size;
l_available_data = l_tile_size;
l_current_data = p_j2k->m_specific_param.m_encoder.m_encoded_tile_data;
- if (! opj_tcd_copy_tile_data(l_tcd,p_data,p_data_size)) {
- opj_event_msg(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
- return OPJ_FALSE;
- }
-
l_nb_bytes_written = 0;
if (! opj_j2k_write_first_tile_part(p_j2k,l_current_data,&l_nb_bytes_written,l_available_data,p_stream,p_manager)) {
return OPJ_FALSE;
/* DEVELOPER CORNER, insert your custom procedures */
opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_eoc );
- if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
+ if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz)) {
opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_updated_tlm);
}
opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_cod );
opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_qcd );
- if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema) {
+ if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz)) {
/* No need for COC or QCC, QCD and COD are used
opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_all_coc );
opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_all_qcc );
*/
opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_tlm );
- if (p_j2k->m_cp.m_specific_param.m_enc.m_cinema == OPJ_CINEMA4K_24) {
+ if (p_j2k->m_cp.rsiz == OPJ_PROFILE_CINEMA_4K) {
opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_poc );
}
}
}
/* DEVELOPER CORNER, insert your custom procedures */
- if (p_j2k->m_cp.rsiz & OPJ_MCT) {
+ if (p_j2k->m_cp.rsiz & OPJ_EXTENSION_MCT) {
opj_procedure_list_add_procedure(p_j2k->m_procedure_list,(opj_procedure)opj_j2k_write_mct_data_group );
}
/* End of Developer Corner */
p_data += l_current_nb_bytes_written;
p_total_data_size -= l_current_nb_bytes_written;
- if (l_cp->m_specific_param.m_enc.m_cinema == 0) {
+ if (!OPJ_IS_CINEMA(l_cp->rsiz)) {
#if 0
for (compno = 1; compno < p_j2k->m_private_image->numcomps; compno++) {
l_current_nb_bytes_written = 0;
/* Writing Psot in SOT marker */
opj_write_bytes(l_begin_data + 6,l_nb_bytes_written,4); /* PSOT */
- if (l_cp->m_specific_param.m_enc.m_cinema){
+ if (OPJ_IS_CINEMA(l_cp->rsiz)){
opj_j2k_update_tlm(p_j2k,l_nb_bytes_written);
}
/* Writing Psot in SOT marker */
opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
- if (l_cp->m_specific_param.m_enc.m_cinema) {
+ if (OPJ_IS_CINEMA(l_cp->rsiz)) {
opj_j2k_update_tlm(p_j2k,l_part_tile_size);
}
/* Writing Psot in SOT marker */
opj_write_bytes(l_begin_data + 6,l_part_tile_size,4); /* PSOT */
- if (l_cp->m_specific_param.m_enc.m_cinema) {
+ if (OPJ_IS_CINEMA(l_cp->rsiz)) {
opj_j2k_update_tlm(p_j2k,l_part_tile_size);
}
return OPJ_FALSE;
}
else {
- if (! opj_j2k_post_write_tile(p_j2k,p_data,p_data_size,p_stream,p_manager)) {
+ OPJ_UINT32 j;
+ /* Allocate data */
+ for (j=0;j<p_j2k->m_tcd->image->numcomps;++j) {
+ opj_tcd_tilecomp_t* l_tilec = p_j2k->m_tcd->tcd_image->tiles->comps + j;
+
+ if(! opj_alloc_tile_component_data(l_tilec)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Error allocating tile component data." );
+ return OPJ_FALSE;
+ }
+ }
+
+ /* now copy data into the the tile component */
+ if (! opj_tcd_copy_tile_data(p_j2k->m_tcd,p_data,p_data_size)) {
+ opj_event_msg(p_manager, EVT_ERROR, "Size mismatch between tile data and sent data." );
+ return OPJ_FALSE;
+ }
+ if (! opj_j2k_post_write_tile(p_j2k,p_stream,p_manager)) {
opj_event_msg(p_manager, EVT_ERROR, "Error while opj_j2k_post_write_tile with tile index = %d\n", p_tile_index);
return OPJ_FALSE;
}