2005-12-02 14:34:15 +01:00
|
|
|
/*
|
2011-01-29 15:15:26 +01:00
|
|
|
* Copyright (c) 2005, Herve Drolon, FreeImage Team
|
2005-12-02 14:34:15 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opj_includes.h"
|
|
|
|
|
2007-08-21 14:02:23 +02:00
|
|
|
opj_image_t* opj_image_create0(void) {
|
2008-01-22 11:35:26 +01:00
|
|
|
opj_image_t *image = (opj_image_t*)opj_calloc(1, sizeof(opj_image_t));
|
2005-12-08 10:38:47 +01:00
|
|
|
return image;
|
2005-12-02 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
2012-03-26 11:15:26 +02:00
|
|
|
opj_image_t* OPJ_CALLCONV opj_image_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) {
|
2012-09-27 16:48:47 +02:00
|
|
|
OPJ_UINT32 compno;
|
2005-12-08 10:38:47 +01:00
|
|
|
opj_image_t *image = NULL;
|
2005-12-02 14:34:15 +01:00
|
|
|
|
2007-10-18 14:26:11 +02:00
|
|
|
image = (opj_image_t*) opj_calloc(1, sizeof(opj_image_t));
|
2005-12-08 10:38:47 +01:00
|
|
|
if(image) {
|
|
|
|
image->color_space = clrspc;
|
|
|
|
image->numcomps = numcmpts;
|
|
|
|
/* allocate memory for the per-component information */
|
|
|
|
image->comps = (opj_image_comp_t*)opj_malloc(image->numcomps * sizeof(opj_image_comp_t));
|
|
|
|
if(!image->comps) {
|
2007-09-07 17:01:55 +02:00
|
|
|
fprintf(stderr,"Unable to allocate memory for image.\n");
|
2005-12-08 10:38:47 +01:00
|
|
|
opj_image_destroy(image);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* create the individual image components */
|
|
|
|
for(compno = 0; compno < numcmpts; compno++) {
|
|
|
|
opj_image_comp_t *comp = &image->comps[compno];
|
|
|
|
comp->dx = cmptparms[compno].dx;
|
|
|
|
comp->dy = cmptparms[compno].dy;
|
|
|
|
comp->w = cmptparms[compno].w;
|
|
|
|
comp->h = cmptparms[compno].h;
|
|
|
|
comp->x0 = cmptparms[compno].x0;
|
|
|
|
comp->y0 = cmptparms[compno].y0;
|
|
|
|
comp->prec = cmptparms[compno].prec;
|
|
|
|
comp->bpp = cmptparms[compno].bpp;
|
|
|
|
comp->sgnd = cmptparms[compno].sgnd;
|
2012-11-16 09:29:43 +01:00
|
|
|
comp->data = (OPJ_INT32*) opj_calloc(comp->w * comp->h, sizeof(OPJ_INT32));
|
2005-12-08 10:38:47 +01:00
|
|
|
if(!comp->data) {
|
2007-09-07 17:01:55 +02:00
|
|
|
fprintf(stderr,"Unable to allocate memory for image.\n");
|
2005-12-08 10:38:47 +01:00
|
|
|
opj_image_destroy(image);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-12-02 14:34:15 +01:00
|
|
|
|
2005-12-08 10:38:47 +01:00
|
|
|
return image;
|
2005-12-02 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
2006-01-31 22:26:11 +01:00
|
|
|
void OPJ_CALLCONV opj_image_destroy(opj_image_t *image) {
|
2005-12-08 10:38:47 +01:00
|
|
|
if(image) {
|
|
|
|
if(image->comps) {
|
2011-10-05 18:27:16 +02:00
|
|
|
OPJ_UINT32 compno;
|
2011-10-12 16:42:21 +02:00
|
|
|
|
2005-12-08 10:38:47 +01:00
|
|
|
/* image components */
|
2011-10-05 18:27:16 +02:00
|
|
|
for(compno = 0; compno < image->numcomps; compno++) {
|
|
|
|
opj_image_comp_t *image_comp = &(image->comps[compno]);
|
2005-12-08 10:38:47 +01:00
|
|
|
if(image_comp->data) {
|
|
|
|
opj_free(image_comp->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
opj_free(image->comps);
|
|
|
|
}
|
2005-12-02 14:34:15 +01:00
|
|
|
|
2011-10-05 18:27:16 +02:00
|
|
|
if(image->icc_profile_buf) {
|
|
|
|
opj_free(image->icc_profile_buf);
|
2011-09-19 15:55:06 +02:00
|
|
|
}
|
2011-10-12 16:42:21 +02:00
|
|
|
|
|
|
|
opj_free(image);
|
2011-09-19 15:30:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-19 15:04:04 +02:00
|
|
|
/**
|
2011-09-19 15:30:21 +02:00
|
|
|
* Updates the components characteristics of the image from the coding parameters.
|
2011-09-19 15:04:04 +02:00
|
|
|
*
|
2011-09-19 15:30:21 +02:00
|
|
|
* @param p_image_header the image header to update.
|
|
|
|
* @param p_cp the coding parameters from which to update the image.
|
2011-09-19 15:04:04 +02:00
|
|
|
*/
|
2012-10-25 17:09:31 +02:00
|
|
|
void opj_image_comp_header_update(opj_image_t * p_image_header, const struct opj_cp * p_cp)
|
2011-09-19 15:04:04 +02:00
|
|
|
{
|
|
|
|
OPJ_UINT32 i, l_width, l_height;
|
2011-09-19 15:30:21 +02:00
|
|
|
OPJ_INT32 l_x0, l_y0, l_x1, l_y1;
|
|
|
|
OPJ_INT32 l_comp_x0, l_comp_y0, l_comp_x1, l_comp_y1;
|
2011-10-05 18:27:16 +02:00
|
|
|
opj_image_comp_t* l_img_comp = NULL;
|
2011-09-19 15:04:04 +02:00
|
|
|
|
2012-10-05 11:10:15 +02:00
|
|
|
l_x0 = opj_int_max(p_cp->tx0 , p_image_header->x0);
|
|
|
|
l_y0 = opj_int_max(p_cp->ty0 , p_image_header->y0);
|
|
|
|
l_x1 = opj_int_min(p_cp->tx0 + p_cp->tw * p_cp->tdx, p_image_header->x1);
|
|
|
|
l_y1 = opj_int_min(p_cp->ty0 + p_cp->th * p_cp->tdy, p_image_header->y1);
|
2011-09-19 15:04:04 +02:00
|
|
|
|
2011-09-19 15:30:21 +02:00
|
|
|
l_img_comp = p_image_header->comps;
|
|
|
|
for (i = 0; i < p_image_header->numcomps; ++i) {
|
2012-10-05 11:10:15 +02:00
|
|
|
l_comp_x0 = opj_int_ceildiv(l_x0, l_img_comp->dx);
|
|
|
|
l_comp_y0 = opj_int_ceildiv(l_y0, l_img_comp->dy);
|
|
|
|
l_comp_x1 = opj_int_ceildiv(l_x1, l_img_comp->dx);
|
|
|
|
l_comp_y1 = opj_int_ceildiv(l_y1, l_img_comp->dy);
|
|
|
|
l_width = opj_int_ceildivpow2(l_comp_x1 - l_comp_x0, l_img_comp->factor);
|
|
|
|
l_height = opj_int_ceildivpow2(l_comp_y1 - l_comp_y0, l_img_comp->factor);
|
2011-09-19 15:04:04 +02:00
|
|
|
l_img_comp->w = l_width;
|
|
|
|
l_img_comp->h = l_height;
|
2011-10-05 18:27:16 +02:00
|
|
|
l_img_comp->x0 = l_comp_x0/*l_x0*/;
|
|
|
|
l_img_comp->y0 = l_comp_y0/*l_y0*/;
|
2011-09-19 15:04:04 +02:00
|
|
|
++l_img_comp;
|
|
|
|
}
|
|
|
|
}
|
2011-10-21 18:38:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy only header of image and its component header (no data are copied)
|
|
|
|
* if dest image have data, they will be freed
|
|
|
|
*
|
|
|
|
* @param p_image_src the src image
|
|
|
|
* @param p_image_dest the dest image
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void opj_copy_image_header(const opj_image_t* p_image_src, opj_image_t* p_image_dest)
|
|
|
|
{
|
|
|
|
OPJ_UINT32 compno;
|
|
|
|
|
|
|
|
/* preconditions */
|
|
|
|
assert(p_image_src != 00);
|
|
|
|
assert(p_image_dest != 00);
|
|
|
|
|
|
|
|
p_image_dest->x0 = p_image_src->x0;
|
|
|
|
p_image_dest->y0 = p_image_src->y0;
|
|
|
|
p_image_dest->x1 = p_image_src->x1;
|
|
|
|
p_image_dest->y1 = p_image_src->y1;
|
|
|
|
|
|
|
|
if (p_image_dest->comps){
|
|
|
|
for(compno = 0; compno < p_image_dest->numcomps; compno++) {
|
|
|
|
opj_image_comp_t *image_comp = &(p_image_dest->comps[compno]);
|
|
|
|
if(image_comp->data) {
|
|
|
|
opj_free(image_comp->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
opj_free(p_image_dest->comps);
|
|
|
|
p_image_dest->comps = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_image_dest->numcomps = p_image_src->numcomps;
|
|
|
|
|
|
|
|
p_image_dest->comps = (opj_image_comp_t*) opj_malloc(p_image_dest->numcomps * sizeof(opj_image_comp_t));
|
|
|
|
if (!p_image_dest->comps){
|
|
|
|
p_image_dest->comps = NULL;
|
|
|
|
p_image_dest->numcomps = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (compno=0; compno < p_image_dest->numcomps; compno++){
|
|
|
|
memcpy( &(p_image_dest->comps[compno]),
|
|
|
|
&(p_image_src->comps[compno]),
|
|
|
|
sizeof(opj_image_comp_t));
|
|
|
|
p_image_dest->comps[compno].data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_image_dest->color_space = p_image_src->color_space;
|
|
|
|
p_image_dest->icc_profile_len = p_image_src->icc_profile_len;
|
|
|
|
|
|
|
|
if (p_image_dest->icc_profile_len) {
|
|
|
|
p_image_dest->icc_profile_buf = (OPJ_BYTE*)opj_malloc(p_image_dest->icc_profile_len);
|
|
|
|
if (!p_image_dest->icc_profile_buf){
|
|
|
|
p_image_dest->icc_profile_buf = NULL;
|
|
|
|
p_image_dest->icc_profile_len = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy( p_image_dest->icc_profile_buf,
|
|
|
|
p_image_src->icc_profile_buf,
|
|
|
|
p_image_src->icc_profile_len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
p_image_dest->icc_profile_buf = NULL;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2012-03-23 18:47:53 +01:00
|
|
|
|
|
|
|
opj_image_t* OPJ_CALLCONV opj_image_tile_create(OPJ_UINT32 numcmpts, opj_image_cmptparm_t *cmptparms, OPJ_COLOR_SPACE clrspc) {
|
|
|
|
OPJ_UINT32 compno;
|
|
|
|
opj_image_t *image = 00;
|
|
|
|
|
|
|
|
image = (opj_image_t*) opj_malloc(sizeof(opj_image_t));
|
2012-05-02 15:37:54 +02:00
|
|
|
if (image)
|
2012-03-23 18:47:53 +01:00
|
|
|
{
|
|
|
|
memset(image,0,sizeof(opj_image_t));
|
2012-05-02 15:37:54 +02:00
|
|
|
|
2012-03-23 18:47:53 +01:00
|
|
|
image->color_space = clrspc;
|
|
|
|
image->numcomps = numcmpts;
|
2012-05-02 15:37:54 +02:00
|
|
|
|
2012-03-23 18:47:53 +01:00
|
|
|
/* allocate memory for the per-component information */
|
|
|
|
image->comps = (opj_image_comp_t*)opj_malloc(image->numcomps * sizeof(opj_image_comp_t));
|
2012-05-02 15:37:54 +02:00
|
|
|
if (!image->comps) {
|
2012-03-23 18:47:53 +01:00
|
|
|
opj_image_destroy(image);
|
|
|
|
return 00;
|
|
|
|
}
|
|
|
|
memset(image->comps,0,image->numcomps * sizeof(opj_image_comp_t));
|
2012-05-02 15:37:54 +02:00
|
|
|
|
2012-03-23 18:47:53 +01:00
|
|
|
/* create the individual image components */
|
|
|
|
for(compno = 0; compno < numcmpts; compno++) {
|
|
|
|
opj_image_comp_t *comp = &image->comps[compno];
|
|
|
|
comp->dx = cmptparms[compno].dx;
|
|
|
|
comp->dy = cmptparms[compno].dy;
|
|
|
|
comp->w = cmptparms[compno].w;
|
|
|
|
comp->h = cmptparms[compno].h;
|
|
|
|
comp->x0 = cmptparms[compno].x0;
|
|
|
|
comp->y0 = cmptparms[compno].y0;
|
|
|
|
comp->prec = cmptparms[compno].prec;
|
|
|
|
comp->sgnd = cmptparms[compno].sgnd;
|
|
|
|
comp->data = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-05-02 15:37:54 +02:00
|
|
|
|
2012-03-23 18:47:53 +01:00
|
|
|
return image;
|
|
|
|
}
|