/*@{*/
/**
- * Sets up the procedures to do on reading header. Developpers wanting to extend the library can add their own reading procedures.
+ * Sets up the procedures to do on reading header. Developers wanting to extend the library can add their own reading procedures.
*/
static OPJ_BOOL opj_j2k_setup_header_reading(opj_j2k_t *p_j2k,
opj_event_mgr_t * p_manager);
opj_event_mgr_t * p_manager);
/**
- * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
- * are valid. Developpers wanting to extend the library can add their own validation procedures.
+ * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
+ * are valid. Developers wanting to extend the library can add their own validation procedures.
*/
static OPJ_BOOL opj_j2k_setup_encoding_validation(opj_j2k_t *p_j2k,
opj_event_mgr_t * p_manager);
/**
- * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
- * are valid. Developpers wanting to extend the library can add their own validation procedures.
+ * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
+ * are valid. Developers wanting to extend the library can add their own validation procedures.
*/
static OPJ_BOOL opj_j2k_setup_decoding_validation(opj_j2k_t *p_j2k,
opj_event_mgr_t * p_manager);
/**
- * Sets up the validation ,i.e. adds the procedures to lauch to make sure the codec parameters
- * are valid. Developpers wanting to extend the library can add their own validation procedures.
+ * Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
+ * are valid. Developers wanting to extend the library can add their own validation procedures.
*/
static OPJ_BOOL opj_j2k_setup_end_compress(opj_j2k_t *p_j2k,
opj_event_mgr_t * p_manager);
opj_event_mgr_t * p_manager);
/**
- * Excutes the given procedures on the given codec.
+ * Executes the given procedures on the given codec.
*
* @param p_procedure_list the list of procedures to execute
* @param p_j2k the jpeg2000 codec to execute the procedures on.
/**
* Writes the SOD marker (Start of data)
*
+ * This also writes optional PLT markers (before SOD)
+ *
* @param p_j2k J2K codec.
* @param p_tile_coder FIXME DOC
* @param p_data FIXME DOC
static void opj_j2k_update_tlm(opj_j2k_t * p_j2k, OPJ_UINT32 p_tile_part_size)
{
- opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,
- p_j2k->m_current_tile_number, 1); /* PSOT */
- ++p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current;
+ if (p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte) {
+ opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,
+ p_j2k->m_current_tile_number, 1);
+ p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 1;
+ } else {
+ opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,
+ p_j2k->m_current_tile_number, 2);
+ p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current += 2;
+ }
opj_write_bytes(p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_current,
p_tile_part_size, 4); /* PSOT */
OPJ_UINT32 p_header_size,
opj_event_mgr_t * p_manager);
+/**
+ * Reads a CAP marker (extended capabilities definition). Empty implementation.
+ * Found in HTJ2K files
+ *
+ * @param p_header_data the data contained in the CAP box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the CAP marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_cap(opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager);
+
+/**
+ * Reads a CPF marker (corresponding profile). Empty implementation. Found in HTJ2K files
+ * @param p_header_data the data contained in the CPF box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the CPF marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_cpf(opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager);
+
/**
* Writes COC marker for each component.
static OPJ_BOOL opj_j2k_is_cinema_compliant(opj_image_t *image, OPJ_UINT16 rsiz,
opj_event_mgr_t *p_manager);
+static void opj_j2k_set_imf_parameters(opj_cparameters_t *parameters,
+ opj_image_t *image, opj_event_mgr_t *p_manager);
+
+static OPJ_BOOL opj_j2k_is_imf_compliant(opj_cparameters_t *parameters,
+ opj_image_t *image,
+ opj_event_mgr_t *p_manager);
+
/**
* Checks for invalid number of tile-parts in SOT marker (TPsot==TNsot). See issue 254.
*
{J2K_MS_COM, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_com},
{J2K_MS_MCT, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mct},
{J2K_MS_CBD, J2K_STATE_MH, opj_j2k_read_cbd},
+ {J2K_MS_CAP, J2K_STATE_MH, opj_j2k_read_cap},
+ {J2K_MS_CPF, J2K_STATE_MH, opj_j2k_read_cpf},
{J2K_MS_MCC, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mcc},
{J2K_MS_MCO, J2K_STATE_MH | J2K_STATE_TPH, opj_j2k_read_mco},
#ifdef USE_JPWL
-#ifdef TODO_MS /* remove these functions which are not commpatible with the v2 API */
+#ifdef TODO_MS /* remove these functions which are not compatible with the v2 API */
{J2K_MS_EPC, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epc},
{J2K_MS_EPB, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_epb},
{J2K_MS_ESD, J2K_STATE_MH | J2K_STATE_TPH, j2k_read_esd},
assert(p_nb_pocs > 0);
- packet_array = (OPJ_UINT32*) opj_calloc(step_l * p_num_layers,
+ packet_array = (OPJ_UINT32*) opj_calloc((size_t)step_l * p_num_layers,
sizeof(OPJ_UINT32));
if (packet_array == 00) {
opj_event_msg(p_manager, EVT_ERROR,
tpnum *= l_current_poc->layE;
break;
}
- /* whould we split here ? */
+ /* would we split here ? */
if (cp->m_specific_param.m_enc.m_tp_flag == prog[i]) {
cp->m_specific_param.m_enc.m_tp_pos = i;
break;
}
/* Compute the number of tiles */
- 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);
+ l_cp->tw = opj_uint_ceildiv(l_image->x1 - l_cp->tx0, l_cp->tdx);
+ l_cp->th = opj_uint_ceildiv(l_image->y1 - l_cp->ty0, 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) {
(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_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);
+ p_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_uint_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_uint_ceildiv(
+ p_j2k->m_specific_param.m_decoder.m_end_tile_y - l_cp->ty0,
+ l_cp->tdy);
} else {
p_j2k->m_specific_param.m_decoder.m_start_tile_x = 0;
p_j2k->m_specific_param.m_decoder.m_start_tile_y = 0;
opj_read_bytes(p_header_data, &l_tcp->mct, 1); /* SGcod (C) */
++p_header_data;
+ if (l_tcp->mct > 1) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Invalid multiple component transformation\n");
+ return OPJ_FALSE;
+ }
+
p_header_size -= 5;
for (i = 0; i < l_image->numcomps; ++i) {
l_tcp->tccps[i].csty = l_tcp->csty & J2K_CCP_CSTY_PRT;
l_nb_bytes += opj_j2k_get_max_poc_size(p_j2k);
+ if (p_j2k->m_specific_param.m_encoder.m_PLT) {
+ /* Reserve space for PLT markers */
+
+ OPJ_UINT32 i;
+ const opj_cp_t * l_cp = &(p_j2k->m_cp);
+ OPJ_UINT32 l_max_packet_count = 0;
+ for (i = 0; i < l_cp->th * l_cp->tw; ++i) {
+ l_max_packet_count = opj_uint_max(l_max_packet_count,
+ opj_get_encoding_packet_count(p_j2k->m_private_image, l_cp, i));
+ }
+ /* Minimum 6 bytes per PLT marker, and at a minimum (taking a pessimistic */
+ /* estimate of 4 bytes for a packet size), one can write */
+ /* (65536-6) / 4 = 16382 paquet sizes per PLT marker */
+ p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT =
+ 6 * opj_uint_ceildiv(l_max_packet_count, 16382);
+ /* Maximum 5 bytes per packet to encode a full UINT32 */
+ p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT +=
+ l_nb_bytes += 5 * l_max_packet_count;
+ p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT += 1;
+ l_nb_bytes += p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT;
+ }
+
/*** DEVELOPER CORNER, Add room for your headers ***/
return l_nb_bytes;
opj_read_bytes(l_data, &l_N_ppm, 4);
l_data += 4;
l_data_size -= 4;
- l_ppm_data_size +=
- l_N_ppm; /* can't overflow, max 256 markers of max 65536 bytes, that is when PPM markers are not corrupted which is checked elsewhere */
+ if (l_ppm_data_size > UINT_MAX - l_N_ppm) {
+ opj_event_msg(p_manager, EVT_ERROR, "Too large value for Nppm\n");
+ return OPJ_FALSE;
+ }
+ l_ppm_data_size += l_N_ppm;
if (l_data_size >= l_N_ppm) {
l_data_size -= l_N_ppm;
l_data += l_N_ppm;
{
OPJ_BYTE * l_current_data = 00;
OPJ_UINT32 l_tlm_size;
+ OPJ_UINT32 size_per_tile_part;
/* preconditions */
assert(p_j2k != 00);
assert(p_manager != 00);
assert(p_stream != 00);
- l_tlm_size = 6 + (5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
+ /* 10921 = (65535 - header_size) / size_per_tile_part where */
+ /* header_size = 4 and size_per_tile_part = 6 */
+ if (p_j2k->m_specific_param.m_encoder.m_total_tile_parts > 10921) {
+ /* We could do more but it would require writing several TLM markers */
+ opj_event_msg(p_manager, EVT_ERROR,
+ "A maximum of 10921 tile-parts are supported currently "
+ "when writing TLM marker\n");
+ return OPJ_FALSE;
+ }
+
+ if (p_j2k->m_specific_param.m_encoder.m_total_tile_parts <= 255) {
+ size_per_tile_part = 5;
+ p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte = OPJ_TRUE;
+ } else {
+ size_per_tile_part = 6;
+ p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte = OPJ_FALSE;
+ }
+
+ l_tlm_size = 2 + 4 + (size_per_tile_part *
+ p_j2k->m_specific_param.m_encoder.m_total_tile_parts);
if (l_tlm_size > p_j2k->m_specific_param.m_encoder.m_header_tile_data_size) {
OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(
p_j2k->m_specific_param.m_encoder.m_header_tile_data = new_header_tile_data;
p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = l_tlm_size;
}
+ memset(p_j2k->m_specific_param.m_encoder.m_header_tile_data, 0, l_tlm_size);
l_current_data = p_j2k->m_specific_param.m_encoder.m_header_tile_data;
1); /* Ztlm=0*/
++l_current_data;
- opj_write_bytes(l_current_data, 0x50,
- 1); /* Stlm ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
+ /* Stlm 0x50= ST=1(8bits-255 tiles max),SP=1(Ptlm=32bits) */
+ /* Stlm 0x60= ST=2(16bits-65535 tiles max),SP=1(Ptlm=32bits) */
+ opj_write_bytes(l_current_data,
+ size_per_tile_part == 5 ? 0x50 : 0x60,
+ 1);
++l_current_data;
- /* do nothing on the 5 * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
+ /* do nothing on the size_per_tile_part * l_j2k->m_specific_param.m_encoder.m_total_tile_parts remaining data */
if (opj_stream_write_data(p_stream,
p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_tlm_size,
p_manager) != l_tlm_size) {
return OPJ_TRUE;
}
+/**
+ * Write one or more PLT markers in the provided buffer
+ */
+static OPJ_BOOL opj_j2k_write_plt_in_memory(opj_j2k_t *p_j2k,
+ opj_tcd_marker_info_t* marker_info,
+ OPJ_BYTE * p_data,
+ OPJ_UINT32 * p_data_written,
+ opj_event_mgr_t * p_manager)
+{
+ OPJ_BYTE Zplt = 0;
+ OPJ_UINT16 Lplt;
+ OPJ_BYTE* p_data_start = p_data;
+ OPJ_BYTE* p_data_Lplt = p_data + 2;
+ OPJ_UINT32 i;
+
+ OPJ_UNUSED(p_j2k);
+
+ opj_write_bytes(p_data, J2K_MS_PLT, 2);
+ p_data += 2;
+
+ /* Reserve space for Lplt */
+ p_data += 2;
+
+ opj_write_bytes(p_data, Zplt, 1);
+ p_data += 1;
+
+ Lplt = 3;
+
+ for (i = 0; i < marker_info->packet_count; i++) {
+ OPJ_BYTE var_bytes[5];
+ OPJ_UINT8 var_bytes_size = 0;
+ OPJ_UINT32 packet_size = marker_info->p_packet_size[i];
+
+ /* Packet size written in variable-length way, starting with LSB */
+ var_bytes[var_bytes_size] = (OPJ_BYTE)(packet_size & 0x7f);
+ var_bytes_size ++;
+ packet_size >>= 7;
+ while (packet_size > 0) {
+ var_bytes[var_bytes_size] = (OPJ_BYTE)((packet_size & 0x7f) | 0x80);
+ var_bytes_size ++;
+ packet_size >>= 7;
+ }
+
+ /* Check if that can fit in the current PLT marker. If not, finish */
+ /* current one, and start a new one */
+ if (Lplt + var_bytes_size > 65535) {
+ if (Zplt == 255) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "More than 255 PLT markers would be needed for current tile-part !\n");
+ return OPJ_FALSE;
+ }
+
+ /* Patch Lplt */
+ opj_write_bytes(p_data_Lplt, Lplt, 2);
+
+ /* Start new segment */
+ opj_write_bytes(p_data, J2K_MS_PLT, 2);
+ p_data += 2;
+
+ /* Reserve space for Lplt */
+ p_data_Lplt = p_data;
+ p_data += 2;
+
+ Zplt ++;
+ opj_write_bytes(p_data, Zplt, 1);
+ p_data += 1;
+
+ Lplt = 3;
+ }
+
+ Lplt = (OPJ_UINT16)(Lplt + var_bytes_size);
+
+ /* Serialize variable-length packet size, starting with MSB */
+ for (; var_bytes_size > 0; --var_bytes_size) {
+ opj_write_bytes(p_data, var_bytes[var_bytes_size - 1], 1);
+ p_data += 1;
+ }
+ }
+
+ *p_data_written = (OPJ_UINT32)(p_data - p_data_start);
+
+ /* Patch Lplt */
+ opj_write_bytes(p_data_Lplt, Lplt, 2);
+
+ return OPJ_TRUE;
+}
+
static OPJ_BOOL opj_j2k_write_sod(opj_j2k_t *p_j2k,
opj_tcd_t * p_tile_coder,
OPJ_BYTE * p_data,
{
opj_codestream_info_t *l_cstr_info = 00;
OPJ_UINT32 l_remaining_data;
+ opj_tcd_marker_info_t* marker_info = NULL;
/* preconditions */
assert(p_j2k != 00);
opj_write_bytes(p_data, J2K_MS_SOD,
2); /* SOD */
- p_data += 2;
/* make room for the EOF marker */
l_remaining_data = total_data_size - 4;
*p_data_written = 0;
- if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number, p_data,
+ if (p_j2k->m_specific_param.m_encoder.m_PLT) {
+ marker_info = opj_tcd_marker_info_create(
+ p_j2k->m_specific_param.m_encoder.m_PLT);
+ if (marker_info == NULL) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Cannot encode tile: opj_tcd_marker_info_create() failed\n");
+ return OPJ_FALSE;
+ }
+ }
+
+ if (l_remaining_data <
+ p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Not enough bytes in output buffer to write SOD marker\n");
+ opj_tcd_marker_info_destroy(marker_info);
+ return OPJ_FALSE;
+ }
+ l_remaining_data -= p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT;
+
+ if (! opj_tcd_encode_tile(p_tile_coder, p_j2k->m_current_tile_number,
+ p_data + 2,
p_data_written, l_remaining_data, l_cstr_info,
+ marker_info,
p_manager)) {
opj_event_msg(p_manager, EVT_ERROR, "Cannot encode tile\n");
+ opj_tcd_marker_info_destroy(marker_info);
return OPJ_FALSE;
}
+ /* For SOD */
*p_data_written += 2;
+ if (p_j2k->m_specific_param.m_encoder.m_PLT) {
+ OPJ_UINT32 l_data_written_PLT = 0;
+ OPJ_BYTE* p_PLT_buffer = (OPJ_BYTE*)opj_malloc(
+ p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT);
+ if (!p_PLT_buffer) {
+ opj_event_msg(p_manager, EVT_ERROR, "Cannot allocate memory\n");
+ opj_tcd_marker_info_destroy(marker_info);
+ return OPJ_FALSE;
+ }
+ if (!opj_j2k_write_plt_in_memory(p_j2k,
+ marker_info,
+ p_PLT_buffer,
+ &l_data_written_PLT,
+ p_manager)) {
+ opj_tcd_marker_info_destroy(marker_info);
+ opj_free(p_PLT_buffer);
+ return OPJ_FALSE;
+ }
+
+ assert(l_data_written_PLT <=
+ p_j2k->m_specific_param.m_encoder.m_reserved_bytes_for_PLT);
+
+ /* Move PLT marker(s) before SOD */
+ memmove(p_data + l_data_written_PLT, p_data, *p_data_written);
+ memcpy(p_data, p_PLT_buffer, l_data_written_PLT);
+ opj_free(p_PLT_buffer);
+ *p_data_written += l_data_written_PLT;
+ }
+
+ opj_tcd_marker_info_destroy(marker_info);
+
return OPJ_TRUE;
}
/* 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 (p_j2k->m_cp.strict) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Tile part length size inconsistent with stream length\n");
+ return OPJ_FALSE;
+ } else {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "Tile part length size inconsistent with stream length\n");
+ }
}
if (p_j2k->m_specific_param.m_decoder.m_sot_length >
UINT_MAX - OPJ_COMMON_CBLK_DATA_EXTRA) {
OPJ_FLOAT32 * l_rates = 0;
OPJ_FLOAT32 l_sot_remove;
OPJ_UINT32 l_bits_empty, l_size_pixel;
- OPJ_UINT32 l_tile_size = 0;
+ OPJ_UINT64 l_tile_size = 0;
OPJ_UINT32 l_last_res;
OPJ_FLOAT32(* l_tp_stride_func)(opj_tcp_t *) = 00;
l_rates = l_tcp->rates;
/* Modification of the RATE >> */
- if (*l_rates > 0.0f) {
- *l_rates = (((OPJ_FLOAT32)(l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) *
- (OPJ_UINT32)(l_y1 - l_y0)))
- /
- ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)
- )
- -
- l_offset;
- }
-
- ++l_rates;
-
- for (k = 1; k < l_tcp->numlayers; ++k) {
+ for (k = 0; k < l_tcp->numlayers; ++k) {
if (*l_rates > 0.0f) {
- *l_rates = (((OPJ_FLOAT32)(l_size_pixel * (OPJ_UINT32)(l_x1 - l_x0) *
- (OPJ_UINT32)(l_y1 - l_y0)))
- /
- ((*l_rates) * (OPJ_FLOAT32)l_bits_empty)
- )
+ *l_rates = (OPJ_FLOAT32)(((OPJ_FLOAT64)l_size_pixel * (OPJ_UINT32)(
+ l_x1 - l_x0) *
+ (OPJ_UINT32)(l_y1 - l_y0))
+ / ((*l_rates) * (OPJ_FLOAT32)l_bits_empty))
-
l_offset;
}
l_tile_size = 0;
for (i = 0; i < l_image->numcomps; ++i) {
- l_tile_size += (opj_uint_ceildiv(l_cp->tdx, l_img_comp->dx)
- *
- opj_uint_ceildiv(l_cp->tdy, l_img_comp->dy)
- *
- l_img_comp->prec
- );
+ l_tile_size += (OPJ_UINT64)opj_uint_ceildiv(l_cp->tdx, l_img_comp->dx)
+ *
+ opj_uint_ceildiv(l_cp->tdy, l_img_comp->dy)
+ *
+ l_img_comp->prec;
++l_img_comp;
}
/* bin/test_tile_encoder 1 256 256 32 32 8 0 reversible_with_precinct.j2k 4 4 3 0 0 1 16 16 */
/* TODO revise this to take into account the overhead linked to the */
/* number of packets and number of code blocks in packets */
- l_tile_size = (OPJ_UINT32)(l_tile_size * 1.4 / 8);
+ l_tile_size = (OPJ_UINT64)((double)l_tile_size * 1.4 / 8);
/* Arbitrary amount to make the following work: */
/* bin/test_tile_encoder 1 256 256 17 16 8 0 reversible_no_precinct.j2k 4 4 3 0 0 1 */
l_tile_size += opj_j2k_get_specific_header_sizes(p_j2k);
- p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = l_tile_size;
+ if (l_tile_size > UINT_MAX) {
+ l_tile_size = UINT_MAX;
+ }
+
+ p_j2k->m_specific_param.m_encoder.m_encoded_tile_size = (OPJ_UINT32)l_tile_size;
p_j2k->m_specific_param.m_encoder.m_encoded_tile_data =
(OPJ_BYTE *) opj_malloc(p_j2k->m_specific_param.m_encoder.m_encoded_tile_size);
if (p_j2k->m_specific_param.m_encoder.m_encoded_tile_data == 00) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Not enough memory to allocate m_encoded_tile_data. %u MB required\n",
+ (OPJ_UINT32)(l_tile_size / 1024 / 1024));
return OPJ_FALSE;
}
- if (OPJ_IS_CINEMA(l_cp->rsiz)) {
+ if (p_j2k->m_specific_param.m_encoder.m_TLM) {
p_j2k->m_specific_param.m_encoder.m_tlm_sot_offsets_buffer =
- (OPJ_BYTE *) opj_malloc(5 *
+ (OPJ_BYTE *) opj_malloc(6 *
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_FALSE;
return OPJ_TRUE;
}
+/**
+ * Reads a CAP marker (extended capabilities definition). Empty implementation.
+ * Found in HTJ2K files.
+ *
+ * @param p_header_data the data contained in the CAP box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the CAP marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_cap(opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ (void)p_j2k;
+ (void)p_header_data;
+ (void)p_header_size;
+ (void)p_manager;
+
+ return OPJ_TRUE;
+}
+
+/**
+ * Reads a CPF marker (corresponding profile). Empty implementation. Found in HTJ2K files
+ * @param p_header_data the data contained in the CPF box.
+ * @param p_j2k the jpeg2000 codec.
+ * @param p_header_size the size of the data contained in the CPF marker.
+ * @param p_manager the user event manager.
+*/
+static OPJ_BOOL opj_j2k_read_cpf(opj_j2k_t *p_j2k,
+ OPJ_BYTE * p_header_data,
+ OPJ_UINT32 p_header_size,
+ opj_event_mgr_t * p_manager
+ )
+{
+ /* preconditions */
+ assert(p_header_data != 00);
+ assert(p_j2k != 00);
+ assert(p_manager != 00);
+
+ (void)p_j2k;
+ (void)p_header_data;
+ (void)p_header_size;
+ (void)p_manager;
+
+ return OPJ_TRUE;
+}
+
/* ----------------------------------------------------------------------- */
/* J2K / JPT decoder interface */
/* ----------------------------------------------------------------------- */
}
}
+void opj_j2k_decoder_set_strict_mode(opj_j2k_t *j2k, OPJ_BOOL strict)
+{
+ if (j2k) {
+ j2k->m_cp.strict = strict;
+ }
+}
+
OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads)
{
/* Currently we pass the thread-pool to the tcd, so we cannot re-set it */
return OPJ_FALSE;
}
-static int opj_j2k_get_default_thread_count()
+static int opj_j2k_get_default_thread_count(void)
{
const char* num_threads_str = getenv("OPJ_NUM_THREADS");
int num_cpus;
}
/* Precincts */
- parameters->csty |= 0x01;
+ parameters->csty |= J2K_CP_CSTY_PRT;
if (parameters->numresolution == 1) {
parameters->res_spec = 1;
parameters->prcw_init[0] = 128;
/* Bitdepth */
for (i = 0; i < image->numcomps; i++) {
- if ((image->comps[i].bpp != 12) | (image->comps[i].sgnd)) {
+ if ((image->comps[i].prec != 12) | (image->comps[i].sgnd)) {
char signed_str[] = "signed";
char unsigned_str[] = "unsigned";
char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str;
"Precision of each component shall be 12 bits unsigned"
"-> At least component %d of input image (%d bits, %s) is not compliant\n"
"-> Non-profile-3 codestream will be generated\n",
- i, image->comps[i].bpp, tmp_str);
+ i, image->comps[i].prec, tmp_str);
return OPJ_FALSE;
}
}
return OPJ_TRUE;
}
+static int opj_j2k_get_imf_max_NL(opj_cparameters_t *parameters,
+ opj_image_t *image)
+{
+ /* Decomposition levels */
+ const OPJ_UINT16 rsiz = parameters->rsiz;
+ const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz);
+ const OPJ_UINT32 XTsiz = parameters->tile_size_on ? (OPJ_UINT32)
+ parameters->cp_tdx : image->x1;
+ switch (profile) {
+ case OPJ_PROFILE_IMF_2K:
+ return 5;
+ case OPJ_PROFILE_IMF_4K:
+ return 6;
+ case OPJ_PROFILE_IMF_8K:
+ return 7;
+ case OPJ_PROFILE_IMF_2K_R: {
+ if (XTsiz >= 2048) {
+ return 5;
+ } else if (XTsiz >= 1024) {
+ return 4;
+ }
+ break;
+ }
+ case OPJ_PROFILE_IMF_4K_R: {
+ if (XTsiz >= 4096) {
+ return 6;
+ } else if (XTsiz >= 2048) {
+ return 5;
+ } else if (XTsiz >= 1024) {
+ return 4;
+ }
+ break;
+ }
+ case OPJ_PROFILE_IMF_8K_R: {
+ if (XTsiz >= 8192) {
+ return 7;
+ } else if (XTsiz >= 4096) {
+ return 6;
+ } else if (XTsiz >= 2048) {
+ return 5;
+ } else if (XTsiz >= 1024) {
+ return 4;
+ }
+ break;
+ }
+ default:
+ break;
+ }
+ return -1;
+}
+
+static void opj_j2k_set_imf_parameters(opj_cparameters_t *parameters,
+ opj_image_t *image, opj_event_mgr_t *p_manager)
+{
+ const OPJ_UINT16 rsiz = parameters->rsiz;
+ const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz);
+
+ OPJ_UNUSED(p_manager);
+
+ /* Override defaults set by opj_set_default_encoder_parameters */
+ if (parameters->cblockw_init == OPJ_COMP_PARAM_DEFAULT_CBLOCKW &&
+ parameters->cblockh_init == OPJ_COMP_PARAM_DEFAULT_CBLOCKH) {
+ parameters->cblockw_init = 32;
+ parameters->cblockh_init = 32;
+ }
+
+ /* One tile part for each component */
+ parameters->tp_flag = 'C';
+ parameters->tp_on = 1;
+
+ if (parameters->prog_order == OPJ_COMP_PARAM_DEFAULT_PROG_ORDER) {
+ parameters->prog_order = OPJ_CPRL;
+ }
+
+ if (profile == OPJ_PROFILE_IMF_2K ||
+ profile == OPJ_PROFILE_IMF_4K ||
+ profile == OPJ_PROFILE_IMF_8K) {
+ /* 9-7 transform */
+ parameters->irreversible = 1;
+ }
+
+ /* Adjust the number of resolutions if set to its defaults */
+ if (parameters->numresolution == OPJ_COMP_PARAM_DEFAULT_NUMRESOLUTION &&
+ image->x0 == 0 &&
+ image->y0 == 0) {
+ const int max_NL = opj_j2k_get_imf_max_NL(parameters, image);
+ if (max_NL >= 0 && parameters->numresolution > max_NL) {
+ parameters->numresolution = max_NL + 1;
+ }
+
+ /* Note: below is generic logic */
+ if (!parameters->tile_size_on) {
+ while (parameters->numresolution > 0) {
+ if (image->x1 < (1U << ((OPJ_UINT32)parameters->numresolution - 1U))) {
+ parameters->numresolution --;
+ continue;
+ }
+ if (image->y1 < (1U << ((OPJ_UINT32)parameters->numresolution - 1U))) {
+ parameters->numresolution --;
+ continue;
+ }
+ break;
+ }
+ }
+ }
+
+ /* Set defaults precincts */
+ if (parameters->csty == 0) {
+ parameters->csty |= J2K_CP_CSTY_PRT;
+ if (parameters->numresolution == 1) {
+ parameters->res_spec = 1;
+ parameters->prcw_init[0] = 128;
+ parameters->prch_init[0] = 128;
+ } else {
+ int i;
+ parameters->res_spec = parameters->numresolution - 1;
+ for (i = 0; i < parameters->res_spec; i++) {
+ parameters->prcw_init[i] = 256;
+ parameters->prch_init[i] = 256;
+ }
+ }
+ }
+}
+
+/* Table A.53 from JPEG2000 standard */
+static const OPJ_UINT16 tabMaxSubLevelFromMainLevel[] = {
+ 15, /* unspecified */
+ 1,
+ 1,
+ 1,
+ 2,
+ 3,
+ 4,
+ 5,
+ 6,
+ 7,
+ 8,
+ 9
+};
+
+static OPJ_BOOL opj_j2k_is_imf_compliant(opj_cparameters_t *parameters,
+ opj_image_t *image,
+ opj_event_mgr_t *p_manager)
+{
+ OPJ_UINT32 i;
+ const OPJ_UINT16 rsiz = parameters->rsiz;
+ const OPJ_UINT16 profile = OPJ_GET_IMF_PROFILE(rsiz);
+ const OPJ_UINT16 mainlevel = OPJ_GET_IMF_MAINLEVEL(rsiz);
+ const OPJ_UINT16 sublevel = OPJ_GET_IMF_SUBLEVEL(rsiz);
+ const int NL = parameters->numresolution - 1;
+ const OPJ_UINT32 XTsiz = parameters->tile_size_on ? (OPJ_UINT32)
+ parameters->cp_tdx : image->x1;
+ OPJ_BOOL ret = OPJ_TRUE;
+
+ /* Validate mainlevel */
+ if (mainlevel > OPJ_IMF_MAINLEVEL_MAX) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profile require mainlevel <= 11.\n"
+ "-> %d is thus not compliant\n"
+ "-> Non-IMF codestream will be generated\n",
+ mainlevel);
+ ret = OPJ_FALSE;
+ } else {
+ /* Validate sublevel */
+ assert(sizeof(tabMaxSubLevelFromMainLevel) ==
+ (OPJ_IMF_MAINLEVEL_MAX + 1) * sizeof(tabMaxSubLevelFromMainLevel[0]));
+ if (sublevel > tabMaxSubLevelFromMainLevel[mainlevel]) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profile require sublevel <= %d for mainlevel = %d.\n"
+ "-> %d is thus not compliant\n"
+ "-> Non-IMF codestream will be generated\n",
+ tabMaxSubLevelFromMainLevel[mainlevel],
+ mainlevel,
+ sublevel);
+ ret = OPJ_FALSE;
+ }
+ }
+
+ /* Number of components */
+ if (image->numcomps > 3) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profiles require at most 3 components.\n"
+ "-> Number of components of input image (%d) is not compliant\n"
+ "-> Non-IMF codestream will be generated\n",
+ image->numcomps);
+ ret = OPJ_FALSE;
+ }
+
+ if (image->x0 != 0 || image->y0 != 0) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profiles require image origin to be at 0,0.\n"
+ "-> %d,%d is not compliant\n"
+ "-> Non-IMF codestream will be generated\n",
+ image->x0, image->y0 != 0);
+ ret = OPJ_FALSE;
+ }
+
+ if (parameters->cp_tx0 != 0 || parameters->cp_ty0 != 0) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profiles require tile origin to be at 0,0.\n"
+ "-> %d,%d is not compliant\n"
+ "-> Non-IMF codestream will be generated\n",
+ parameters->cp_tx0, parameters->cp_ty0);
+ ret = OPJ_FALSE;
+ }
+
+ if (parameters->tile_size_on) {
+ if (profile == OPJ_PROFILE_IMF_2K ||
+ profile == OPJ_PROFILE_IMF_4K ||
+ profile == OPJ_PROFILE_IMF_8K) {
+ if ((OPJ_UINT32)parameters->cp_tdx < image->x1 ||
+ (OPJ_UINT32)parameters->cp_tdy < image->y1) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 2K/4K/8K single tile profiles require tile to be greater or equal to image size.\n"
+ "-> %d,%d is lesser than %d,%d\n"
+ "-> Non-IMF codestream will be generated\n",
+ parameters->cp_tdx,
+ parameters->cp_tdy,
+ image->x1,
+ image->y1);
+ ret = OPJ_FALSE;
+ }
+ } else {
+ if ((OPJ_UINT32)parameters->cp_tdx >= image->x1 &&
+ (OPJ_UINT32)parameters->cp_tdy >= image->y1) {
+ /* ok */
+ } else if (parameters->cp_tdx == 1024 &&
+ parameters->cp_tdy == 1024) {
+ /* ok */
+ } else if (parameters->cp_tdx == 2048 &&
+ parameters->cp_tdy == 2048 &&
+ (profile == OPJ_PROFILE_IMF_4K ||
+ profile == OPJ_PROFILE_IMF_8K)) {
+ /* ok */
+ } else if (parameters->cp_tdx == 4096 &&
+ parameters->cp_tdy == 4096 &&
+ profile == OPJ_PROFILE_IMF_8K) {
+ /* ok */
+ } else {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 2K_R/4K_R/8K_R single/multiple tile profiles "
+ "require tile to be greater or equal to image size,\n"
+ "or to be (1024,1024), or (2048,2048) for 4K_R/8K_R "
+ "or (4096,4096) for 8K_R.\n"
+ "-> %d,%d is non conformant\n"
+ "-> Non-IMF codestream will be generated\n",
+ parameters->cp_tdx,
+ parameters->cp_tdy);
+ ret = OPJ_FALSE;
+ }
+ }
+ }
+
+ /* Bitdepth */
+ for (i = 0; i < image->numcomps; i++) {
+ if (!(image->comps[i].prec >= 8 && image->comps[i].prec <= 16) ||
+ (image->comps[i].sgnd)) {
+ char signed_str[] = "signed";
+ char unsigned_str[] = "unsigned";
+ char *tmp_str = image->comps[i].sgnd ? signed_str : unsigned_str;
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profiles require precision of each component to b in [8-16] bits unsigned"
+ "-> At least component %d of input image (%d bits, %s) is not compliant\n"
+ "-> Non-IMF codestream will be generated\n",
+ i, image->comps[i].prec, tmp_str);
+ ret = OPJ_FALSE;
+ }
+ }
+
+ /* Sub-sampling */
+ for (i = 0; i < image->numcomps; i++) {
+ if (i == 0 && image->comps[i].dx != 1) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profiles require XRSiz1 == 1. Here it is set to %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ image->comps[i].dx);
+ ret = OPJ_FALSE;
+ }
+ if (i == 1 && image->comps[i].dx != 1 && image->comps[i].dx != 2) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profiles require XRSiz2 == 1 or 2. Here it is set to %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ image->comps[i].dx);
+ ret = OPJ_FALSE;
+ }
+ if (i > 1 && image->comps[i].dx != image->comps[i - 1].dx) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profiles require XRSiz%d to be the same as XRSiz2. "
+ "Here it is set to %d instead of %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ i + 1, image->comps[i].dx, image->comps[i - 1].dx);
+ ret = OPJ_FALSE;
+ }
+ if (image->comps[i].dy != 1) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profiles require YRsiz == 1. "
+ "Here it is set to %d for component %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ image->comps[i].dy, i);
+ ret = OPJ_FALSE;
+ }
+ }
+
+ /* Image size */
+ switch (profile) {
+ case OPJ_PROFILE_IMF_2K:
+ case OPJ_PROFILE_IMF_2K_R:
+ if (((image->comps[0].w > 2048) | (image->comps[0].h > 1556))) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 2K/2K_R profile require:\n"
+ "width <= 2048 and height <= 1556\n"
+ "-> Input image size %d x %d is not compliant\n"
+ "-> Non-IMF codestream will be generated\n",
+ image->comps[0].w, image->comps[0].h);
+ ret = OPJ_FALSE;
+ }
+ break;
+ case OPJ_PROFILE_IMF_4K:
+ case OPJ_PROFILE_IMF_4K_R:
+ if (((image->comps[0].w > 4096) | (image->comps[0].h > 3112))) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 4K/4K_R profile require:\n"
+ "width <= 4096 and height <= 3112\n"
+ "-> Input image size %d x %d is not compliant\n"
+ "-> Non-IMF codestream will be generated\n",
+ image->comps[0].w, image->comps[0].h);
+ ret = OPJ_FALSE;
+ }
+ break;
+ case OPJ_PROFILE_IMF_8K:
+ case OPJ_PROFILE_IMF_8K_R:
+ if (((image->comps[0].w > 8192) | (image->comps[0].h > 6224))) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 8K/8K_R profile require:\n"
+ "width <= 8192 and height <= 6224\n"
+ "-> Input image size %d x %d is not compliant\n"
+ "-> Non-IMF codestream will be generated\n",
+ image->comps[0].w, image->comps[0].h);
+ ret = OPJ_FALSE;
+ }
+ break;
+ default :
+ assert(0);
+ return OPJ_FALSE;
+ }
+
+ if (parameters->roi_compno != -1) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profile forbid RGN / region of interest marker.\n"
+ "-> Compression parameters specify a ROI\n"
+ "-> Non-IMF codestream will be generated\n");
+ ret = OPJ_FALSE;
+ }
+
+ if (parameters->cblockw_init != 32 || parameters->cblockh_init != 32) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profile require code block size to be 32x32.\n"
+ "-> Compression parameters set it to %dx%d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ parameters->cblockw_init,
+ parameters->cblockh_init);
+ ret = OPJ_FALSE;
+ }
+
+ if (parameters->prog_order != OPJ_CPRL) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profile require progression order to be CPRL.\n"
+ "-> Compression parameters set it to %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ parameters->prog_order);
+ ret = OPJ_FALSE;
+ }
+
+ if (parameters->numpocs != 0) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profile forbid POC markers.\n"
+ "-> Compression parameters set %d POC.\n"
+ "-> Non-IMF codestream will be generated\n",
+ parameters->numpocs);
+ ret = OPJ_FALSE;
+ }
+
+ /* Codeblock style: no mode switch enabled */
+ if (parameters->mode != 0) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profile forbid mode switch in code block style.\n"
+ "-> Compression parameters set code block style to %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ parameters->mode);
+ ret = OPJ_FALSE;
+ }
+
+ if (profile == OPJ_PROFILE_IMF_2K ||
+ profile == OPJ_PROFILE_IMF_4K ||
+ profile == OPJ_PROFILE_IMF_8K) {
+ /* Expect 9-7 transform */
+ if (parameters->irreversible != 1) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 2K/4K/8K profiles require 9-7 Irreversible Transform.\n"
+ "-> Compression parameters set it to reversible.\n"
+ "-> Non-IMF codestream will be generated\n");
+ ret = OPJ_FALSE;
+ }
+ } else {
+ /* Expect 5-3 transform */
+ if (parameters->irreversible != 0) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 2K/4K/8K profiles require 5-3 reversible Transform.\n"
+ "-> Compression parameters set it to irreversible.\n"
+ "-> Non-IMF codestream will be generated\n");
+ ret = OPJ_FALSE;
+ }
+ }
+
+ /* Number of layers */
+ if (parameters->tcp_numlayers != 1) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 2K/4K/8K profiles require 1 single quality layer.\n"
+ "-> Number of layers is %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ parameters->tcp_numlayers);
+ ret = OPJ_FALSE;
+ }
+
+ /* Decomposition levels */
+ switch (profile) {
+ case OPJ_PROFILE_IMF_2K:
+ if (!(NL >= 1 && NL <= 5)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 2K profile requires 1 <= NL <= 5:\n"
+ "-> Number of decomposition levels is %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ NL);
+ ret = OPJ_FALSE;
+ }
+ break;
+ case OPJ_PROFILE_IMF_4K:
+ if (!(NL >= 1 && NL <= 6)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 4K profile requires 1 <= NL <= 6:\n"
+ "-> Number of decomposition levels is %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ NL);
+ ret = OPJ_FALSE;
+ }
+ break;
+ case OPJ_PROFILE_IMF_8K:
+ if (!(NL >= 1 && NL <= 7)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 8K profile requires 1 <= NL <= 7:\n"
+ "-> Number of decomposition levels is %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ NL);
+ ret = OPJ_FALSE;
+ }
+ break;
+ case OPJ_PROFILE_IMF_2K_R: {
+ if (XTsiz >= 2048) {
+ if (!(NL >= 1 && NL <= 5)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 2K_R profile requires 1 <= NL <= 5 for XTsiz >= 2048:\n"
+ "-> Number of decomposition levels is %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ NL);
+ ret = OPJ_FALSE;
+ }
+ } else if (XTsiz >= 1024) {
+ if (!(NL >= 1 && NL <= 4)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 2K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n"
+ "-> Number of decomposition levels is %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ NL);
+ ret = OPJ_FALSE;
+ }
+ }
+ break;
+ }
+ case OPJ_PROFILE_IMF_4K_R: {
+ if (XTsiz >= 4096) {
+ if (!(NL >= 1 && NL <= 6)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 4K_R profile requires 1 <= NL <= 6 for XTsiz >= 4096:\n"
+ "-> Number of decomposition levels is %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ NL);
+ ret = OPJ_FALSE;
+ }
+ } else if (XTsiz >= 2048) {
+ if (!(NL >= 1 && NL <= 5)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 4K_R profile requires 1 <= NL <= 5 for XTsiz in [2048,4096[:\n"
+ "-> Number of decomposition levels is %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ NL);
+ ret = OPJ_FALSE;
+ }
+ } else if (XTsiz >= 1024) {
+ if (!(NL >= 1 && NL <= 4)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 4K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n"
+ "-> Number of decomposition levels is %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ NL);
+ ret = OPJ_FALSE;
+ }
+ }
+ break;
+ }
+ case OPJ_PROFILE_IMF_8K_R: {
+ if (XTsiz >= 8192) {
+ if (!(NL >= 1 && NL <= 7)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 4K_R profile requires 1 <= NL <= 7 for XTsiz >= 8192:\n"
+ "-> Number of decomposition levels is %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ NL);
+ ret = OPJ_FALSE;
+ }
+ } else if (XTsiz >= 4096) {
+ if (!(NL >= 1 && NL <= 6)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 4K_R profile requires 1 <= NL <= 6 for XTsiz in [4096,8192[:\n"
+ "-> Number of decomposition levels is %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ NL);
+ ret = OPJ_FALSE;
+ }
+ } else if (XTsiz >= 2048) {
+ if (!(NL >= 1 && NL <= 5)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 4K_R profile requires 1 <= NL <= 5 for XTsiz in [2048,4096[:\n"
+ "-> Number of decomposition levels is %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ NL);
+ ret = OPJ_FALSE;
+ }
+ } else if (XTsiz >= 1024) {
+ if (!(NL >= 1 && NL <= 4)) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF 4K_R profile requires 1 <= NL <= 4 for XTsiz in [1024,2048[:\n"
+ "-> Number of decomposition levels is %d.\n"
+ "-> Non-IMF codestream will be generated\n",
+ NL);
+ ret = OPJ_FALSE;
+ }
+ }
+ break;
+ }
+ default:
+ break;
+ }
+
+ if (parameters->numresolution == 1) {
+ if (parameters->res_spec != 1 ||
+ parameters->prcw_init[0] != 128 ||
+ parameters->prch_init[0] != 128) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profiles require PPx = PPy = 7 for NLLL band, else 8.\n"
+ "-> Supplied values are different from that.\n"
+ "-> Non-IMF codestream will be generated\n");
+ ret = OPJ_FALSE;
+ }
+ } else {
+ int i;
+ for (i = 0; i < parameters->res_spec; i++) {
+ if (parameters->prcw_init[i] != 256 ||
+ parameters->prch_init[i] != 256) {
+ opj_event_msg(p_manager, EVT_WARNING,
+ "IMF profiles require PPx = PPy = 7 for NLLL band, else 8.\n"
+ "-> Supplied values are different from that.\n"
+ "-> Non-IMF codestream will be generated\n");
+ ret = OPJ_FALSE;
+ }
+ }
+ }
+
+ return ret;
+}
+
+
OPJ_BOOL opj_j2k_setup_encoder(opj_j2k_t *p_j2k,
opj_cparameters_t *parameters,
opj_image_t *image,
return OPJ_FALSE;
}
+ if (parameters->cp_fixed_alloc) {
+ if (parameters->cp_matrice == NULL) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "cp_fixed_alloc set, but cp_matrice missing\n");
+ return OPJ_FALSE;
+ }
+
+ if (parameters->tcp_numlayers > J2K_TCD_MATRIX_MAX_LAYER_COUNT) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "tcp_numlayers when cp_fixed_alloc set should not exceed %d\n",
+ J2K_TCD_MATRIX_MAX_LAYER_COUNT);
+ return OPJ_FALSE;
+ }
+ if (parameters->numresolution > J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "numresolution when cp_fixed_alloc set should not exceed %d\n",
+ J2K_TCD_MATRIX_MAX_RESOLUTION_COUNT);
+ return OPJ_FALSE;
+ }
+ }
+
+ p_j2k->m_specific_param.m_encoder.m_nb_comps = image->numcomps;
+
/* keep a link to cp so that we can destroy it later in j2k_destroy_compress */
cp = &(p_j2k->m_cp);
image->comps[0].h * image->comps[0].prec) /
((double)parameters->tcp_rates[parameters->tcp_numlayers - 1] * 8 *
image->comps[0].dx * image->comps[0].dy));
- if (temp_size > INT_MAX) {
+ if (temp_size > (OPJ_FLOAT32)INT_MAX) {
parameters->max_cs_size = INT_MAX;
} else {
parameters->max_cs_size = (int) floor(temp_size);
} else {
OPJ_FLOAT32 temp_rate;
OPJ_BOOL cap = OPJ_FALSE;
+
+ if (OPJ_IS_IMF(parameters->rsiz) && parameters->max_cs_size > 0 &&
+ parameters->tcp_numlayers == 1 && parameters->tcp_rates[0] == 0) {
+ 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);
+ }
+
temp_rate = (OPJ_FLOAT32)(((double)image->numcomps * image->comps[0].w *
image->comps[0].h * image->comps[0].prec) /
(((double)parameters->max_cs_size) * 8 * image->comps[0].dx *
}
}
+ if (OPJ_IS_CINEMA(parameters->rsiz) || OPJ_IS_IMF(parameters->rsiz)) {
+ p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_TRUE;
+ }
+
/* Manage profiles and applications and set RSIZ */
/* set cinema parameters if required */
if (OPJ_IS_CINEMA(parameters->rsiz)) {
"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;
+ opj_j2k_set_imf_parameters(parameters, image, p_manager);
+ if (!opj_j2k_is_imf_compliant(parameters, image, p_manager)) {
+ 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,
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) {
+ if (parameters->cp_fixed_alloc) {
+ cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy = FIXED_LAYER;
+ } else if (parameters->cp_fixed_quality) {
+ cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy =
+ FIXED_DISTORTION_RATIO;
+ } else {
+ cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy =
+ RATE_DISTORTION_RATIO;
+ }
+
+ if (parameters->cp_fixed_alloc) {
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);
/* UniPG>> */
#ifdef USE_JPWL
- cp->comment = (char*)opj_malloc(clen + strlen(version) + 11);
+ const size_t cp_comment_buf_size = clen + strlen(version) + 11;
+ cp->comment = (char*)opj_malloc(cp_comment_buf_size);
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);
+ snprintf(cp->comment, cp_comment_buf_size, "%s%s with JPWL",
+ comment, version);
#else
- cp->comment = (char*)opj_malloc(clen + strlen(version) + 1);
+ const size_t cp_comment_buf_size = clen + strlen(version) + 1;
+ cp->comment = (char*)opj_malloc(cp_comment_buf_size);
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);
+ snprintf(cp->comment, cp_comment_buf_size, "%s%s", comment, version);
#endif
/* <<UniPG */
}
*/
if (parameters->tile_size_on) {
- 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);
+ if (cp->tdx == 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Invalid tile width\n");
+ return OPJ_FALSE;
+ }
+ if (cp->tdy == 0) {
+ opj_event_msg(p_manager, EVT_ERROR, "Invalid tile height\n");
+ return OPJ_FALSE;
+ }
+ cp->tw = opj_uint_ceildiv(image->x1 - cp->tx0, cp->tdx);
+ cp->th = opj_uint_ceildiv(image->y1 - cp->ty0, cp->tdy);
+ /* Check that the number of tiles is valid */
+ if (cp->tw > 65535 / cp->th) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Invalid number of tiles : %u x %u (maximum fixed by jpeg2000 norm is 65535 tiles)\n",
+ cp->tw, cp->th);
+ return OPJ_FALSE;
+ }
} else {
cp->tdx = image->x1 - cp->tx0;
cp->tdy = image->y1 - cp->ty0;
}
#endif /* USE_JPWL */
- /* initialize the mutiple tiles */
+ /* initialize the multiple tiles */
/* ---------------------------- */
cp->tcps = (opj_tcp_t*) opj_calloc(cp->tw * cp->th, sizeof(opj_tcp_t));
if (!cp->tcps) {
for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
opj_tcp_t *tcp = &cp->tcps[tileno];
+ const OPJ_BOOL fixed_distoratio =
+ cp->m_specific_param.m_enc.m_quality_layer_alloc_strategy ==
+ FIXED_DISTORTION_RATIO;
tcp->numlayers = (OPJ_UINT32)parameters->tcp_numlayers;
for (j = 0; j < tcp->numlayers; j++) {
- if (OPJ_IS_CINEMA(cp->rsiz)) {
- if (cp->m_specific_param.m_enc.m_fixed_quality) {
+ if (OPJ_IS_CINEMA(cp->rsiz) || OPJ_IS_IMF(cp->rsiz)) {
+ if (fixed_distoratio) {
tcp->distoratio[j] = parameters->tcp_distoratio[j];
}
tcp->rates[j] = parameters->tcp_rates[j];
} else {
- if (cp->m_specific_param.m_enc.m_fixed_quality) { /* add fixed_quality */
+ if (fixed_distoratio) {
tcp->distoratio[j] = parameters->tcp_distoratio[j];
} else {
tcp->rates[j] = parameters->tcp_rates[j];
}
}
- if (!cp->m_specific_param.m_enc.m_fixed_quality &&
+ if (!fixed_distoratio &&
tcp->rates[j] <= 1.0) {
tcp->rates[j] = 0.0; /* force lossless */
}
if (tileno + 1 == parameters->POC[i].tile) {
opj_poc_t *tcp_poc = &tcp->pocs[numpocs_tile];
+ if (parameters->POC[numpocs_tile].compno0 >= image->numcomps) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Invalid compno0 for POC %d\n", i);
+ return OPJ_FALSE;
+ }
+
tcp_poc->resno0 = parameters->POC[numpocs_tile].resno0;
tcp_poc->compno0 = parameters->POC[numpocs_tile].compno0;
tcp_poc->layno1 = parameters->POC[numpocs_tile].layno1;
tcp_poc->resno1 = parameters->POC[numpocs_tile].resno1;
- tcp_poc->compno1 = parameters->POC[numpocs_tile].compno1;
+ tcp_poc->compno1 = opj_uint_min(parameters->POC[numpocs_tile].compno1,
+ image->numcomps);
tcp_poc->prg1 = parameters->POC[numpocs_tile].prg1;
tcp_poc->tile = parameters->POC[numpocs_tile].tile;
/*Allocate and initialize some elements of codestrem index*/
if (!opj_j2k_allocate_tile_element_cstr_index(p_j2k)) {
+ opj_image_destroy(*p_image);
+ *p_image = NULL;
return OPJ_FALSE;
}
if (l_marker_handler->id == J2K_MS_UNK) {
if (! opj_j2k_read_unk(p_j2k, p_stream, &l_current_marker, p_manager)) {
opj_event_msg(p_manager, EVT_ERROR,
- "Unknow marker have been detected and generated error.\n");
+ "Unknown marker has been detected and generated error.\n");
return OPJ_FALSE;
}
OPJ_UINT32 l_marker_size;
const opj_dec_memory_marker_handler_t * l_marker_handler = 00;
opj_tcp_t * l_tcp = NULL;
+ const OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
/* preconditions */
assert(p_stream != 00);
return OPJ_FALSE;
}
if (l_correction_needed) {
- OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.tw * p_j2k->m_cp.th;
OPJ_UINT32 l_tile_no;
p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
"Non conformant codestream TPsot==TNsot.\n");
}
}
- if (! p_j2k->m_specific_param.m_decoder.m_can_decode) {
- /* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
- if (opj_stream_read_data(p_stream,
- p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
- opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
- return OPJ_FALSE;
- }
-
- /* Read 2 bytes from buffer as the new marker ID */
- opj_read_bytes(p_j2k->m_specific_param.m_decoder.m_header_data,
- &l_current_marker, 2);
- }
} else {
/* Indicate we will try to read a new tile-part header*/
p_j2k->m_specific_param.m_decoder.m_skip_data = 0;
p_j2k->m_specific_param.m_decoder.m_can_decode = 0;
p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_TPHSOT;
+ }
+ if (! p_j2k->m_specific_param.m_decoder.m_can_decode) {
/* Try to read 2 bytes (the next marker ID) from stream and copy them into the buffer */
if (opj_stream_read_data(p_stream,
p_j2k->m_specific_param.m_decoder.m_header_data, 2, p_manager) != 2) {
+
+ /* Deal with likely non conformant SPOT6 files, where the last */
+ /* row of tiles have TPsot == 0 and TNsot == 0, and missing EOC, */
+ /* but no other tile-parts were found. */
+ if (p_j2k->m_current_tile_number + 1 == l_nb_tiles) {
+ OPJ_UINT32 l_tile_no;
+ for (l_tile_no = 0U; l_tile_no < l_nb_tiles; ++l_tile_no) {
+ if (p_j2k->m_cp.tcps[l_tile_no].m_current_tile_part_number == 0 &&
+ p_j2k->m_cp.tcps[l_tile_no].m_nb_tile_parts == 0) {
+ break;
+ }
+ }
+ if (l_tile_no < l_nb_tiles) {
+ opj_event_msg(p_manager, EVT_INFO,
+ "Tile %u has TPsot == 0 and TNsot == 0, "
+ "but no other tile-parts were found. "
+ "EOC is also missing.\n",
+ l_tile_no);
+ p_j2k->m_current_tile_number = l_tile_no;
+ l_current_marker = J2K_MS_EOC;
+ p_j2k->m_specific_param.m_decoder.m_state = J2K_STATE_EOC;
+ break;
+ }
+ }
+
opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
return OPJ_FALSE;
}
}
}
- /* FIXME DOC ???*/
+ /* Deal with tiles that have a single tile-part with TPsot == 0 and TNsot == 0 */
if (! p_j2k->m_specific_param.m_decoder.m_can_decode) {
- OPJ_UINT32 l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
l_tcp = p_j2k->m_cp.tcps + p_j2k->m_current_tile_number;
while ((p_j2k->m_current_tile_number < l_nb_tiles) && (l_tcp->m_data == 00)) {
/* When using the opj_read_tile_header / opj_decode_tile_data API */
/* such as in test_tile_decoder, m_output_image is NULL, so fall back */
/* to the full image dimension. This is a bit surprising that */
- /* opj_set_decode_area() is only used to determinte intersecting tiles, */
+ /* opj_set_decode_area() is only used to determine intersecting tiles, */
/* but full tile decoding is done */
l_image_for_bounds = p_j2k->m_output_image ? p_j2k->m_output_image :
p_j2k->m_private_image;
if (p_j2k->m_specific_param.m_decoder.m_state != J2K_STATE_EOC) {
if (opj_stream_read_data(p_stream, l_data, 2, p_manager) != 2) {
- opj_event_msg(p_manager, EVT_ERROR, "Stream too short\n");
- return OPJ_FALSE;
+ opj_event_msg(p_manager, p_j2k->m_cp.strict ? EVT_ERROR : EVT_WARNING,
+ "Stream too short\n");
+ return p_j2k->m_cp.strict ? OPJ_FALSE : OPJ_TRUE;
}
-
opj_read_bytes(l_data, &l_current_marker, 2);
if (l_current_marker == J2K_MS_EOC) {
l_img_comp = p_image->comps;
for (it_comp = 0; it_comp < p_image->numcomps; ++it_comp) {
OPJ_INT32 l_h, l_w;
+ if (p_image->x0 > (OPJ_UINT32)INT_MAX ||
+ p_image->y0 > (OPJ_UINT32)INT_MAX ||
+ p_image->x1 > (OPJ_UINT32)INT_MAX ||
+ p_image->y1 > (OPJ_UINT32)INT_MAX) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Image coordinates above INT_MAX are not supported\n");
+ return OPJ_FALSE;
+ }
- 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_img_comp->x0 = opj_uint_ceildiv(p_image->x0, l_img_comp->dx);
+ l_img_comp->y0 = opj_uint_ceildiv(p_image->y0, 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);
p_j2k->m_specific_param.m_decoder.m_end_tile_x = l_cp->tw;
p_image->x1 = l_image->x1;
} else {
- 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_j2k->m_specific_param.m_decoder.m_end_tile_x = opj_uint_ceildiv((
+ OPJ_UINT32)p_end_x - l_cp->tx0, l_cp->tdx);
p_image->x1 = (OPJ_UINT32)p_end_x;
}
p_j2k->m_specific_param.m_decoder.m_end_tile_y = l_cp->th;
p_image->y1 = l_image->y1;
} else {
- 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_j2k->m_specific_param.m_decoder.m_end_tile_y = opj_uint_ceildiv((
+ OPJ_UINT32)p_end_y - l_cp->ty0, l_cp->tdy);
p_image->y1 = (OPJ_UINT32)p_end_y;
}
/* ----- */
/* per component is allowed */
l_j2k->m_cp.allow_different_bit_depth_sign = 1;
+ /* Default to using strict mode. */
+ l_j2k->m_cp.strict = OPJ_TRUE;
+
#ifdef OPJ_DISABLE_TPSOT_FIX
l_j2k->m_specific_param.m_decoder.m_nb_tile_parts_correction_checked = 1;
#endif
return OPJ_FALSE;
}
- opj_read_bytes(l_current_ptr, &l_tccp->numresolutions,
- 1); /* SPcox (D) */
- ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
+ /* SPcod (D) / SPcoc (A) */
+ opj_read_bytes(l_current_ptr, &l_tccp->numresolutions, 1);
+ ++l_tccp->numresolutions; /* tccp->numresolutions = read() + 1 */
if (l_tccp->numresolutions > OPJ_J2K_MAXRLVLS) {
opj_event_msg(p_manager, EVT_ERROR,
"Invalid value for numresolutions : %d, max value is set in openjpeg.h at %d\n",
return OPJ_FALSE;
}
- opj_read_bytes(l_current_ptr, &l_tccp->cblkw, 1); /* SPcoc (E) */
+ /* SPcod (E) / SPcoc (B) */
+ opj_read_bytes(l_current_ptr, &l_tccp->cblkw, 1);
++l_current_ptr;
l_tccp->cblkw += 2;
- opj_read_bytes(l_current_ptr, &l_tccp->cblkh, 1); /* SPcoc (F) */
+ /* SPcod (F) / SPcoc (C) */
+ opj_read_bytes(l_current_ptr, &l_tccp->cblkh, 1);
++l_current_ptr;
l_tccp->cblkh += 2;
return OPJ_FALSE;
}
-
- opj_read_bytes(l_current_ptr, &l_tccp->cblksty, 1); /* SPcoc (G) */
+ /* SPcod (G) / SPcoc (D) */
+ opj_read_bytes(l_current_ptr, &l_tccp->cblksty, 1);
++l_current_ptr;
- if (l_tccp->cblksty & 0xC0U) { /* 2 msb are reserved, assume we can't read */
+ if ((l_tccp->cblksty & J2K_CCP_CBLKSTY_HTMIXED) != 0) {
+ /* We do not support HT mixed mode yet. For conformance, it should be supported.*/
opj_event_msg(p_manager, EVT_ERROR,
- "Error reading SPCod SPCoc element, Invalid code-block style found\n");
+ "Error reading SPCod SPCoc element. Unsupported Mixed HT code-block style found\n");
return OPJ_FALSE;
}
- opj_read_bytes(l_current_ptr, &l_tccp->qmfbid, 1); /* SPcoc (H) */
+ /* SPcod (H) / SPcoc (E) */
+ opj_read_bytes(l_current_ptr, &l_tccp->qmfbid, 1);
++l_current_ptr;
+ if (l_tccp->qmfbid > 1) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Error reading SPCod SPCoc element, Invalid transformation found\n");
+ return OPJ_FALSE;
+ }
+
*p_header_size = *p_header_size - 5;
/* use custom precinct size ? */
return OPJ_FALSE;
}
+ /* SPcod (I_i) / SPcoc (F_i) */
for (i = 0; i < l_tccp->numresolutions; ++i) {
- opj_read_bytes(l_current_ptr, &l_tmp, 1); /* SPcoc (I_i) */
+ opj_read_bytes(l_current_ptr, &l_tmp, 1);
++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))) {
l_tccp->stepsizes[l_band_no].mant = 0;
}
}
+
+ if (*p_header_size < l_num_band) {
+ return OPJ_FALSE;
+ }
*p_header_size = *p_header_size - l_num_band;
} else {
for (l_band_no = 0; l_band_no < l_num_band; l_band_no++) {
l_tccp->stepsizes[l_band_no].mant = l_tmp & 0x7ff;
}
}
+
+ if (*p_header_size < 2 * l_num_band) {
+ return OPJ_FALSE;
+ }
*p_header_size = *p_header_size - 2 * l_num_band;
}
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);
+ fprintf(out_stream, "\t tx0=%" PRIu32 ", ty0=%" PRIu32 "\n", p_j2k->m_cp.tx0,
+ p_j2k->m_cp.ty0);
+ fprintf(out_stream, "\t tdx=%" PRIu32 ", tdy=%" PRIu32 "\n", p_j2k->m_cp.tdx,
+ p_j2k->m_cp.tdy);
+ fprintf(out_stream, "\t tw=%" PRIu32 ", th=%" PRIu32 "\n", p_j2k->m_cp.tw,
+ p_j2k->m_cp.th);
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");
return OPJ_TRUE;
}
+static OPJ_BOOL opj_j2k_are_all_used_components_decoded(opj_j2k_t *p_j2k,
+ opj_event_mgr_t * p_manager)
+{
+ OPJ_UINT32 compno;
+ OPJ_BOOL decoded_all_used_components = OPJ_TRUE;
+
+ if (p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode) {
+ for (compno = 0;
+ compno < p_j2k->m_specific_param.m_decoder.m_numcomps_to_decode; compno++) {
+ OPJ_UINT32 dec_compno =
+ p_j2k->m_specific_param.m_decoder.m_comps_indices_to_decode[compno];
+ if (p_j2k->m_output_image->comps[dec_compno].data == NULL) {
+ opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n",
+ dec_compno);
+ decoded_all_used_components = OPJ_FALSE;
+ }
+ }
+ } else {
+ for (compno = 0; compno < p_j2k->m_output_image->numcomps; compno++) {
+ if (p_j2k->m_output_image->comps[compno].data == NULL) {
+ opj_event_msg(p_manager, EVT_WARNING, "Failed to decode component %d\n",
+ compno);
+ decoded_all_used_components = OPJ_FALSE;
+ }
+ }
+ }
+
+ if (decoded_all_used_components == OPJ_FALSE) {
+ opj_event_msg(p_manager, EVT_ERROR, "Failed to decode all used components\n");
+ return OPJ_FALSE;
+ }
+
+ return OPJ_TRUE;
+}
+
+
static OPJ_BOOL opj_j2k_decode_tiles(opj_j2k_t *p_j2k,
opj_stream_private_t *p_stream,
opj_event_mgr_t * p_manager)
}
}
+ if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) {
+ return OPJ_FALSE;
+ }
+
return OPJ_TRUE;
}
/**
- * Sets up the procedures to do on decoding data. Developpers wanting to extend the library can add their own reading procedures.
+ * Sets up the procedures to do on decoding data. Developers wanting to extend the library can add their own reading procedures.
*/
static OPJ_BOOL opj_j2k_setup_decoding(opj_j2k_t *p_j2k,
opj_event_mgr_t * p_manager)
}
+ if (! opj_j2k_are_all_used_components_decoded(p_j2k, p_manager)) {
+ return OPJ_FALSE;
+ }
+
return OPJ_TRUE;
}
/**
- * Sets up the procedures to do on decoding one tile. Developpers wanting to extend the library can add their own reading procedures.
+ * Sets up the procedures to do on decoding one tile. Developers wanting to extend the library can add their own reading procedures.
*/
static OPJ_BOOL opj_j2k_setup_decoding_tile(opj_j2k_t *p_j2k,
opj_event_mgr_t * p_manager)
p_image->comps[compno].data = p_j2k->m_output_image->comps[compno].data;
#if 0
char fn[256];
- sprintf(fn, "/tmp/%d.raw", compno);
+ snprintf(fn, sizeof 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);
l_img_comp->factor = p_j2k->m_private_image->comps[compno].factor;
- 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_img_comp->x0 = opj_uint_ceildiv(p_image->x0, l_img_comp->dx);
+ l_img_comp->y0 = opj_uint_ceildiv(p_image->y0, 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);
opj_image_destroy(p_j2k->m_output_image);
}
- /* Create the ouput image from the information previously computed*/
+ /* Create the output image from the information previously computed*/
p_j2k->m_output_image = opj_image_create0();
if (!(p_j2k->m_output_image)) {
return OPJ_FALSE;
return OPJ_FALSE;
}
+/* ----------------------------------------------------------------------- */
+
+OPJ_BOOL opj_j2k_encoder_set_extra_options(
+ opj_j2k_t *p_j2k,
+ const char* const* p_options,
+ opj_event_mgr_t * p_manager)
+{
+ const char* const* p_option_iter;
+
+ if (p_options == NULL) {
+ return OPJ_TRUE;
+ }
+
+ for (p_option_iter = p_options; *p_option_iter != NULL; ++p_option_iter) {
+ if (strncmp(*p_option_iter, "PLT=", 4) == 0) {
+ if (strcmp(*p_option_iter, "PLT=YES") == 0) {
+ p_j2k->m_specific_param.m_encoder.m_PLT = OPJ_TRUE;
+ } else if (strcmp(*p_option_iter, "PLT=NO") == 0) {
+ p_j2k->m_specific_param.m_encoder.m_PLT = OPJ_FALSE;
+ } else {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Invalid value for option: %s.\n", *p_option_iter);
+ return OPJ_FALSE;
+ }
+ } else if (strncmp(*p_option_iter, "TLM=", 4) == 0) {
+ if (strcmp(*p_option_iter, "TLM=YES") == 0) {
+ p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_TRUE;
+ } else if (strcmp(*p_option_iter, "TLM=NO") == 0) {
+ p_j2k->m_specific_param.m_encoder.m_TLM = OPJ_FALSE;
+ } else {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Invalid value for option: %s.\n", *p_option_iter);
+ return OPJ_FALSE;
+ }
+ } else if (strncmp(*p_option_iter, "GUARD_BITS=", strlen("GUARD_BITS=")) == 0) {
+ OPJ_UINT32 tileno;
+ opj_cp_t *cp = cp = &(p_j2k->m_cp);
+
+ int numgbits = atoi(*p_option_iter + strlen("GUARD_BITS="));
+ if (numgbits < 0 || numgbits > 7) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Invalid value for option: %s. Should be in [0,7]\n", *p_option_iter);
+ return OPJ_FALSE;
+ }
+
+ for (tileno = 0; tileno < cp->tw * cp->th; tileno++) {
+ OPJ_UINT32 i;
+ opj_tcp_t *tcp = &cp->tcps[tileno];
+ for (i = 0; i < p_j2k->m_specific_param.m_encoder.m_nb_comps; i++) {
+ opj_tccp_t *tccp = &tcp->tccps[i];
+ tccp->numgbits = (OPJ_UINT32)numgbits;
+ }
+ }
+ } else {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Invalid option: %s.\n", *p_option_iter);
+ return OPJ_FALSE;
+ }
+ }
+
+ return OPJ_TRUE;
+}
+
+/* ----------------------------------------------------------------------- */
+
OPJ_BOOL opj_j2k_encode(opj_j2k_t * p_j2k,
opj_stream_private_t *p_stream,
opj_event_mgr_t * p_manager)
}
}
}
- l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd);
+ l_current_tile_size = opj_tcd_get_encoder_input_buffer_size(p_j2k->m_tcd);
if (!l_reuse_data) {
if (l_current_tile_size > l_max_tile_size) {
OPJ_BYTE *l_new_current_data = (OPJ_BYTE *) opj_realloc(l_current_data,
*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_offset_x = opj_uint_ceildiv(l_image->x0, l_img_comp->dx);
+ *l_offset_y = opj_uint_ceildiv(l_image->y0, l_img_comp->dy);
+ *l_image_width = opj_uint_ceildiv(l_image->x1 - l_image->x0, 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;
return OPJ_FALSE;
}
- if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz)) {
+ if (p_j2k->m_specific_param.m_encoder.m_TLM) {
if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
(opj_procedure)opj_j2k_write_updated_tlm, p_manager)) {
return OPJ_FALSE;
return OPJ_FALSE;
}
- if (OPJ_IS_CINEMA(p_j2k->m_cp.rsiz)) {
+ if (p_j2k->m_specific_param.m_encoder.m_TLM) {
if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
(opj_procedure)opj_j2k_write_tlm, p_manager)) {
return OPJ_FALSE;
}
/* DEVELOPER CORNER, insert your custom procedures */
- if (p_j2k->m_cp.rsiz & OPJ_EXTENSION_MCT) {
+ if ((p_j2k->m_cp.rsiz & (OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT)) ==
+ (OPJ_PROFILE_PART2 | OPJ_EXTENSION_MCT)) {
if (! opj_procedure_list_add_procedure(p_j2k->m_procedure_list,
(opj_procedure)opj_j2k_write_mct_data_group, p_manager)) {
return OPJ_FALSE;
opj_write_bytes(l_begin_data + 6, l_nb_bytes_written,
4); /* PSOT */
- if (OPJ_IS_CINEMA(l_cp->rsiz)) {
+ if (p_j2k->m_specific_param.m_encoder.m_TLM) {
opj_j2k_update_tlm(p_j2k, l_nb_bytes_written);
}
opj_write_bytes(l_begin_data + 6, l_part_tile_size,
4); /* PSOT */
- if (OPJ_IS_CINEMA(l_cp->rsiz)) {
+ if (p_j2k->m_specific_param.m_encoder.m_TLM) {
opj_j2k_update_tlm(p_j2k, l_part_tile_size);
}
opj_write_bytes(l_begin_data + 6, l_part_tile_size,
4); /* PSOT */
- if (OPJ_IS_CINEMA(l_cp->rsiz)) {
+ if (p_j2k->m_specific_param.m_encoder.m_TLM) {
opj_j2k_update_tlm(p_j2k, l_part_tile_size);
}
{
OPJ_UINT32 l_tlm_size;
OPJ_OFF_T l_tlm_position, l_current_position;
+ OPJ_UINT32 size_per_tile_part;
/* preconditions */
assert(p_j2k != 00);
assert(p_manager != 00);
assert(p_stream != 00);
- l_tlm_size = 5 * p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
+ size_per_tile_part = p_j2k->m_specific_param.m_encoder.m_Ttlmi_is_byte ? 5 : 6;
+ l_tlm_size = size_per_tile_part *
+ p_j2k->m_specific_param.m_encoder.m_total_tile_parts;
l_tlm_position = 6 + p_j2k->m_specific_param.m_encoder.m_tlm_start;
l_current_position = opj_stream_tell(p_stream);