OpenJPEG experiment, PCH circumcision

This commit is contained in:
siana
2011-03-20 04:44:58 +01:00
parent e70a91311f
commit baac16b5d2
13 changed files with 85 additions and 63 deletions

View File

@@ -151,13 +151,8 @@ BOOL LLImageJ2COJ::decodeImpl(LLImageJ2C &base, LLImageRaw &raw_image, F32 decod
/* open a byte stream */
cio = opj_cio_open((opj_common_ptr)dinfo, base.getData(), base.getDataSize());
/* decode the stream and fill the image structure, also fill in an additional
structure to get the decoding result. This structure is a bit unusual in that
it is not received through opj, but still has some dynamically allocated fields
that need to be cleared up at the end by calling a destroy function. */
opj_codestream_info_t cinfo;
memset(&cinfo, 0, sizeof(opj_codestream_info_t));
image = opj_decode_with_info(dinfo, cio, &cinfo);
/* decode the stream and fill the image structure */
image = opj_decode(dinfo, cio);
/* close the byte stream */
opj_cio_close(cio);
@@ -171,70 +166,45 @@ BOOL LLImageJ2COJ::decodeImpl(LLImageJ2C &base, LLImageRaw &raw_image, F32 decod
// The image decode failed if the return was NULL or the component
// count was zero. The latter is just a sanity check before we
// dereference the array.
if(!image)
if(!image || !image->numcomps)
{
LL_DEBUGS("Openjpeg") << "ERROR -> decodeImpl: failed to decode image - no image" << LL_ENDL;
return TRUE; // done
}
S32 img_components = image->numcomps;
if( !img_components ) // < 1 ||img_components > 4 )
{
LL_DEBUGS("Openjpeg") << "ERROR -> decodeImpl: failed to decode image wrong number of components: " << img_components << LL_ENDL;
llwarns << "ERROR -> decodeImpl: failed to decode image!" << llendl;
if (image)
{
opj_destroy_cstr_info(&cinfo);
opj_image_destroy(image);
}
base.decodeFailed();
return TRUE; // done
}
// sometimes we get bad data out of the cache - check to see if the decode succeeded
int decompdifference = 0;
if (cinfo.numdecompos) // sanity
for (S32 i = 0; i < image->numcomps; i++)
{
for (int comp = 0; comp < image->numcomps; comp++)
{ /* get maximum decomposition level difference, first field is from the COD header and the second
is what is actually met in the codestream, NB: if everything was ok, this calculation will
return what was set in the cp_reduce value! */
decompdifference = llmax(decompdifference, cinfo.numdecompos[comp] - image->comps[comp].resno_decoded);
}
if (decompdifference < 0) // sanity
if (image->comps[i].factor != base.getRawDiscardLevel())
{
decompdifference = 0;
// if we didn't get the discard level we're expecting, fail
opj_image_destroy(image);
base.decodeFailed();
return TRUE;
}
}
/* if OpenJPEG failed to decode all requested decomposition levels
the difference will be greater than this level */
if (decompdifference > base.getRawDiscardLevel())
if(image->numcomps <= first_channel)
{
llwarns << "not enough data for requested discard level, setting mDecoding to FALSE, difference: " << (decompdifference - base.getRawDiscardLevel()) << llendl;
opj_destroy_cstr_info(&cinfo);
opj_image_destroy(image);
base.mDecoding = FALSE;
return TRUE;
}
if(img_components <= first_channel)
{
// sanity
LL_DEBUGS("Openjpeg") << "trying to decode more channels than are present in image: numcomps: " << img_components << " first_channel: " << first_channel << LL_ENDL;
llwarns << "trying to decode more channels than are present in image: numcomps: " << image->numcomps << " first_channel: " << first_channel << llendl;
if (image)
{
opj_destroy_cstr_info(&cinfo);
opj_image_destroy(image);
}
base.decodeFailed();
return TRUE;
}
// Copy image data into our raw image format (instead of the separate channel format
S32 img_components = image->numcomps;
S32 channels = img_components - first_channel;
if( channels > max_channel_count )
channels = max_channel_count;
@@ -274,19 +244,15 @@ BOOL LLImageJ2COJ::decodeImpl(LLImageJ2C &base, LLImageRaw &raw_image, F32 decod
else // Some rare OpenJPEG versions have this bug.
{
llwarns << "ERROR -> decodeImpl: failed to decode image! (NULL comp data - OpenJPEG bug)" << llendl;
opj_destroy_cstr_info(&cinfo);
opj_image_destroy(image);
base.decodeFailed();
return TRUE; // done
}
}
/* free opj data structures */
if (image)
{
opj_destroy_cstr_info(&cinfo);
opj_image_destroy(image);
}
/* free image data structure */
opj_image_destroy(image);
return TRUE; // done
}
@@ -349,7 +315,7 @@ BOOL LLImageJ2COJ::encodeImpl(LLImageJ2C &base, const LLImageRaw &raw_image, con
OPJ_COLOR_SPACE color_space = CLRSPC_SRGB;
opj_image_cmptparm_t cmptparm[MAX_COMPS];
opj_image_t * image = NULL;
S32 numcomps = llmin((S32)raw_image.getComponents(),(S32)MAX_COMPS);
S32 numcomps = llmin((S32)raw_image.getComponents(), MAX_COMPS);
S32 width = raw_image.getWidth();
S32 height = raw_image.getHeight();