OPJ_UINT32 l_nb_tiles;
OPJ_UINT32 l_max_tile_size = 0, l_current_tile_size;
OPJ_BYTE * l_current_data = 00;
+ OPJ_BOOL l_reuse_data = OPJ_FALSE;
opj_tcd_t* p_tcd = 00;
/* preconditions */
p_tcd = p_j2k->m_tcd;
l_nb_tiles = p_j2k->m_cp.th * p_j2k->m_cp.tw;
+ if (l_nb_tiles == 1) {
+ l_reuse_data = OPJ_TRUE;
+#ifdef __SSE__
+ for (j=0;j<p_j2k->m_tcd->image->numcomps;++j) {
+ opj_image_comp_t * l_img_comp = p_tcd->image->comps + j;
+ if (((size_t)l_img_comp->data & 0xFU) != 0U) { /* tile data shall be aligned on 16 bytes */
+ l_reuse_data = OPJ_FALSE;
+ }
+ }
+#endif
+ }
for (i=0;i<l_nb_tiles;++i) {
if (! opj_j2k_pre_write_tile(p_j2k,i,p_stream,p_manager)) {
if (l_current_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) {
+ if (l_reuse_data) {
opj_image_comp_t * l_img_comp = p_tcd->image->comps + j;
l_tilec->data = l_img_comp->data;
l_tilec->ownsData = OPJ_FALSE;
}
}
l_current_tile_size = opj_tcd_get_encoded_tile_size(p_j2k->m_tcd);
- if (l_nb_tiles > 1) {
+ 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_current_tile_size);
if (! l_new_current_data) {
* allocated array (eg. _msize on Windows, malloc_size on MacOS,
* malloc_usable_size on systems with glibc) */
a_ptr = opj_aligned_alloc_n(alignment, size);
- /* memory may overlap, do not use memcpy */
- memmove(a_ptr, r_ptr, size);
+ memcpy(a_ptr, r_ptr, size);
free( r_ptr );
return a_ptr;
/* _MSC_VER */
l_tilec->ownsData = OPJ_TRUE;
}
else if (l_tilec->data_size_needed > l_tilec->data_size) {
- OPJ_INT32 * new_data = (OPJ_INT32 *) opj_aligned_realloc(l_tilec->data, l_tilec->data_size_needed);
- /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to handle tile datan"); */
- /* fprintf(stderr, "Not enough memory to handle tile data"); */
- if (! new_data) {
- opj_aligned_free(l_tilec->data);
- l_tilec->data = NULL;
+ /* We don't need to keep old data */
+ opj_aligned_free(l_tilec->data);
+ l_tilec->data = (OPJ_INT32 *) opj_aligned_malloc(l_tilec->data_size_needed);
+ if (! l_tilec->data ) {
l_tilec->data_size = 0;
l_tilec->data_size_needed = 0;
l_tilec->ownsData = OPJ_FALSE;
return OPJ_FALSE;
}
- l_tilec->data = new_data;
/*fprintf(stderr, "tReallocate data of tilec (int): from %d to %d x OPJ_UINT32n", l_tilec->data_size, l_data_size);*/
l_tilec->data_size = l_tilec->data_size_needed;
l_tilec->ownsData = OPJ_TRUE;
}
if (l_tile_comp->ownsData && l_tile_comp->data) {
- opj_free(l_tile_comp->data);
+ opj_aligned_free(l_tile_comp->data);
l_tile_comp->data = 00;
l_tile_comp->ownsData = 0;
l_tile_comp->data_size = 0;