for (i = 0; i < image->numcomps - 1; i++) {
if ((image->comps[0].dx != image->comps[i + 1].dx)
|| (image->comps[0].dy != image->comps[i + 1].dy)
- || (image->comps[0].prec != image->comps[i + 1].prec)) {
+ || (image->comps[0].prec != image->comps[i + 1].prec)
+ || (image->comps[0].sgnd != image->comps[i + 1].sgnd)) {
fclose(fdest);
fprintf(stderr,
- "Unable to create a tga file with such J2K image charateristics.");
+ "Unable to create a tga file with such J2K image charateristics.\n");
return 1;
}
}
{
FILE *rawFile = NULL;
size_t res;
- unsigned int compno;
+ unsigned int compno, numcomps;
int w, h, fails;
int line, row, curr, mask;
int *ptr;
return 1;
}
+ numcomps = image->numcomps;
+
+ if (numcomps > 4) {
+ numcomps = 4;
+ }
+
+ for (compno = 1; compno < numcomps; ++compno) {
+ if (image->comps[0].dx != image->comps[compno].dx) {
+ break;
+ }
+ if (image->comps[0].dy != image->comps[compno].dy) {
+ break;
+ }
+ if (image->comps[0].prec != image->comps[compno].prec) {
+ break;
+ }
+ if (image->comps[0].sgnd != image->comps[compno].sgnd) {
+ break;
+ }
+ }
+ if (compno != numcomps) {
+ fprintf(stderr,
+ "imagetoraw_common: All components shall have the same subsampling, same bit depth, same sign.\n");
+ fprintf(stderr, "\tAborting\n");
+ return 1;
+ }
+
rawFile = fopen(outfile, "wb");
if (!rawFile) {
fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
}
}
} else if (image->comps[compno].prec <= 32) {
- fprintf(stderr, "More than 16 bits per component no handled yet\n");
+ fprintf(stderr, "More than 16 bits per component not handled yet\n");
goto fin;
} else {
fprintf(stderr, "Error: invalid precision: %d\n", image->comps[compno].prec);
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
+#include <limits.h>
#ifndef OPJ_HAVE_LIBTIFF
# error OPJ_HAVE_LIBTIFF_NOT_DEFINED
#include <tiffio.h>
#include "openjpeg.h"
#include "convert.h"
+#include "opj_inttypes.h"
/* -->> -->> -->> -->>
int imagetotif(opj_image_t * image, const char *outfile)
{
- int width, height;
- int bps, adjust, sgnd;
- int tiPhoto;
TIFF *tif;
tdata_t buf;
- tsize_t strip_size;
+ uint32 width, height, bps, tiPhoto;
+ int adjust, sgnd;
+ int64_t strip_size, rowStride, TIFF_MAX;
OPJ_UINT32 i, numcomps;
- OPJ_SIZE_T rowStride;
OPJ_INT32* buffer32s = NULL;
OPJ_INT32 const* planes[4];
convert_32s_PXCX cvtPxToCx = NULL;
convert_32sXXx_C1R cvt32sToTif = NULL;
- bps = (int)image->comps[0].prec;
+ bps = (uint32)image->comps[0].prec;
planes[0] = image->comps[0].data;
numcomps = image->numcomps;
break;
}
sgnd = (int)image->comps[0].sgnd;
- adjust = sgnd ? 1 << (image->comps[0].prec - 1) : 0;
- width = (int)image->comps[0].w;
- height = (int)image->comps[0].h;
+ adjust = sgnd ? (int)(1 << (image->comps[0].prec - 1)) : 0;
+ width = (uint32)image->comps[0].w;
+ height = (uint32)image->comps[0].h;
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
- TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, numcomps);
+ TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (uint32)numcomps);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, tiPhoto);
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
+ if (sizeof(tsize_t) == 4) {
+ TIFF_MAX = INT_MAX;
+ } else {
+ TIFF_MAX = UINT_MAX;
+ }
+ strip_size = (int64_t)TIFFStripSize(tif);
- strip_size = TIFFStripSize(tif);
- rowStride = ((OPJ_SIZE_T)width * numcomps * (OPJ_SIZE_T)bps + 7U) / 8U;
- if (rowStride != (OPJ_SIZE_T)strip_size) {
+ if ((int64_t)width > (int64_t)(TIFF_MAX / numcomps) ||
+ (int64_t)(width * numcomps) > (int64_t)(TIFF_MAX / bps) ||
+ (int64_t)(width * numcomps) > (int64_t)(TIFF_MAX / (int64_t)sizeof(
+ OPJ_INT32))) {
+ fprintf(stderr, "Buffer overflow\n");
+ TIFFClose(tif);
+ return 1;
+ }
+ rowStride = (int64_t)((width * numcomps * bps + 7U) / 8U);
+ if (rowStride != strip_size) {
fprintf(stderr, "Invalid TIFF strip size\n");
TIFFClose(tif);
return 1;
}
- buf = _TIFFmalloc(strip_size);
+ buf = malloc((OPJ_SIZE_T)strip_size);
if (buf == NULL) {
TIFFClose(tif);
return 1;
}
- buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)width * numcomps * sizeof(
- OPJ_INT32));
+ buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)(width * numcomps * sizeof(
+ OPJ_INT32)));
if (buffer32s == NULL) {
_TIFFfree(buf);
TIFFClose(tif);
for (i = 0; i < image->comps[0].h; ++i) {
cvtPxToCx(planes, buffer32s, (OPJ_SIZE_T)width, adjust);
cvt32sToTif(buffer32s, (OPJ_BYTE *)buf, (OPJ_SIZE_T)width * numcomps);
- (void)TIFFWriteEncodedStrip(tif, i, (void*)buf, strip_size);
+ (void)TIFFWriteEncodedStrip(tif, i, (void*)buf, (tsize_t)strip_size);
planes[0] += width;
planes[1] += width;
planes[2] += width;
TIFF *tif;
tdata_t buf;
tstrip_t strip;
- tsize_t strip_size;
+ int64_t strip_size, rowStride, TIFF_MAX;
int j, currentPlane, numcomps = 0, w, h;
OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN;
opj_image_cmptparm_t cmptparm[4]; /* RGBA */
opj_image_t *image = NULL;
int has_alpha = 0;
- unsigned short tiBps, tiPhoto, tiSf, tiSpp, tiPC;
- unsigned int tiWidth, tiHeight;
+ uint32 tiBps, tiPhoto, tiSf, tiSpp, tiPC, tiWidth, tiHeight;
OPJ_BOOL is_cinema = OPJ_IS_CINEMA(parameters->rsiz);
convert_XXx32s_C1R cvtTifTo32s = NULL;
convert_32s_CXPX cvtCxToPx = NULL;
OPJ_INT32* buffer32s = NULL;
OPJ_INT32* planes[4];
- OPJ_SIZE_T rowStride;
tif = TIFFOpen(filename, "r");
w = (int)tiWidth;
h = (int)tiHeight;
- if (tiBps > 16U) {
- fprintf(stderr, "tiftoimage: Bits=%d, Only 1 to 16 bits implemented\n", tiBps);
- fprintf(stderr, "\tAborting\n");
+ if (tiSpp == 0 || tiSpp > 4) { /* should be 1 ... 4 */
+ fprintf(stderr, "tiftoimage: Bad value for samples per pixel == %hu.\n"
+ "\tAborting.\n", tiSpp);
+ TIFFClose(tif);
+ return NULL;
+ }
+ if (tiBps > 16U || tiBps == 0) {
+ fprintf(stderr, "tiftoimage: Bad values for Bits == %d.\n"
+ "\tMax. 16 Bits are allowed here.\n\tAborting.\n", tiBps);
TIFFClose(tif);
return NULL;
}
if (tiPhoto != PHOTOMETRIC_MINISBLACK && tiPhoto != PHOTOMETRIC_RGB) {
fprintf(stderr,
- "tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n",
+ "tiftoimage: Bad color format %d.\n\tOnly RGB(A) and GRAY(A) has been implemented\n\tAborting.\n",
(int) tiPhoto);
- fprintf(stderr, "\tAborting\n");
TIFFClose(tif);
return NULL;
}
+ if (tiWidth == 0 || tiHeight == 0) {
+ fprintf(stderr, "tiftoimage: Bad values for width(%u) "
+ "and/or height(%u)\n\tAborting.\n", tiWidth, tiHeight);
+ TIFFClose(tif);
+ return NULL;
+ }
+ w = (int)tiWidth;
+ h = (int)tiHeight;
switch (tiBps) {
case 1:
image->y0 = (OPJ_UINT32)parameters->image_offset_y0;
image->x1 = !image->x0 ? (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1 :
image->x0 + (OPJ_UINT32)(w - 1) * (OPJ_UINT32)subsampling_dx + 1;
+ if (image->x1 <= image->x0) {
+ fprintf(stderr, "tiftoimage: Bad value for image->x1(%d) vs. "
+ "image->x0(%d)\n\tAborting.\n", image->x1, image->x0);
+ TIFFClose(tif);
+ opj_image_destroy(image);
+ return NULL;
+ }
image->y1 = !image->y0 ? (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1 :
image->y0 + (OPJ_UINT32)(h - 1) * (OPJ_UINT32)subsampling_dy + 1;
+ if (image->y1 <= image->y0) {
+ fprintf(stderr, "tiftoimage: Bad value for image->y1(%d) vs. "
+ "image->y0(%d)\n\tAborting.\n", image->y1, image->y0);
+ TIFFClose(tif);
+ opj_image_destroy(image);
+ return NULL;
+ }
for (j = 0; j < numcomps; j++) {
planes[j] = image->comps[j].data;
}
image->comps[numcomps - 1].alpha = (OPJ_UINT16)(1 - (numcomps & 1));
- strip_size = TIFFStripSize(tif);
+ strip_size = (int64_t)TIFFStripSize(tif);
- buf = _TIFFmalloc(strip_size);
+ buf = malloc((OPJ_SIZE_T)strip_size);
if (buf == NULL) {
TIFFClose(tif);
opj_image_destroy(image);
return NULL;
}
- rowStride = ((OPJ_SIZE_T)w * tiSpp * tiBps + 7U) / 8U;
- buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)w * tiSpp * sizeof(OPJ_INT32));
+ if (sizeof(tsize_t) == 4) {
+ TIFF_MAX = INT_MAX;
+ } else {
+ TIFF_MAX = UINT_MAX;
+ }
+ if ((int64_t)tiWidth > (int64_t)(TIFF_MAX / tiSpp) ||
+ (int64_t)(tiWidth * tiSpp) > (int64_t)(TIFF_MAX / tiBps) ||
+ (int64_t)(tiWidth * tiSpp) > (int64_t)(TIFF_MAX / (int64_t)sizeof(OPJ_INT32))) {
+ fprintf(stderr, "Buffer overflow\n");
+ _TIFFfree(buf);
+ TIFFClose(tif);
+ opj_image_destroy(image);
+ return NULL;
+ }
+
+ rowStride = (int64_t)((tiWidth * tiSpp * tiBps + 7U) / 8U);
+ buffer32s = (OPJ_INT32 *)malloc((OPJ_SIZE_T)(tiWidth * tiSpp * sizeof(
+ OPJ_INT32)));
if (buffer32s == NULL) {
_TIFFfree(buf);
TIFFClose(tif);
/* Read the Image components */
for (; (h > 0) && (strip < TIFFNumberOfStrips(tif)); strip++) {
const OPJ_UINT8 *dat8;
- OPJ_SIZE_T ssize;
-
- ssize = (OPJ_SIZE_T)TIFFReadEncodedStrip(tif, strip, buf, strip_size);
+ int64_t ssize;
+
+ ssize = (int64_t)TIFFReadEncodedStrip(tif, strip, buf, (tsize_t)strip_size);
+
+ if (ssize < 1 || ssize > strip_size) {
+ fprintf(stderr, "tiftoimage: Bad value for ssize(%" PRId64 ") "
+ "vs. strip_size(%" PRId64 ").\n\tAborting.\n", ssize, strip_size);
+ _TIFFfree(buf);
+ _TIFFfree(buffer32s);
+ TIFFClose(tif);
+ opj_image_destroy(image);
+ return NULL;
+ }
dat8 = (const OPJ_UINT8*)buf;
while (ssize >= rowStride) {
OPJ_UINT32 l_remaining_size;
OPJ_UINT32 l_nb_tiles;
OPJ_UINT32 l_tmp, l_tx1, l_ty1;
+ OPJ_UINT32 l_prec0, l_sgnd0;
opj_image_t *l_image = 00;
opj_cp_t *l_cp = 00;
opj_image_comp_t * l_img_comp = 00;
"Error with SIZ marker: illegal tile offset\n");
return OPJ_FALSE;
}
+ if (!p_j2k->dump_state) {
+ OPJ_UINT32 siz_w, siz_h;
+ siz_w = l_image->x1 - l_image->x0;
+ siz_h = l_image->y1 - l_image->y0;
+
+ if (p_j2k->ihdr_w > 0 && p_j2k->ihdr_h > 0
+ && (p_j2k->ihdr_w != siz_w || p_j2k->ihdr_h != siz_h)) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Error with SIZ marker: IHDR w(%u) h(%u) vs. SIZ w(%u) h(%u)\n", p_j2k->ihdr_w,
+ p_j2k->ihdr_h, siz_w, siz_h);
+ return OPJ_FALSE;
+ }
+ }
#ifdef USE_JPWL
if (l_cp->correct) {
/* if JPWL is on, we check whether TX errors have damaged
l_img_comp = l_image->comps;
+ l_prec0 = 0;
+ l_sgnd0 = 0;
/* Read the component information */
for (i = 0; i < l_image->numcomps; ++i) {
OPJ_UINT32 tmp;
++p_header_data;
l_img_comp->prec = (tmp & 0x7f) + 1;
l_img_comp->sgnd = tmp >> 7;
+
+ if (p_j2k->dump_state == 0) {
+ if (i == 0) {
+ l_prec0 = l_img_comp->prec;
+ l_sgnd0 = l_img_comp->sgnd;
+ } else if (l_cp->bpc_is_255 == 0
+ && (l_img_comp->prec != l_prec0 || l_img_comp->sgnd != l_sgnd0)) {
+ opj_event_msg(p_manager, EVT_ERROR,
+ "Invalid precision and/or sgnd values for comp[%d]:\n"
+ " [0] prec(%d) sgnd(%d) [%d] prec(%d) sgnd(%d)\n", i, l_prec0, l_sgnd0,
+ i, l_img_comp->prec, l_img_comp->sgnd);
+ return OPJ_FALSE;
+ }
+ }
opj_read_bytes(p_header_data, &tmp, 1); /* XRsiz_i */
++p_header_data;
l_img_comp->dx = (OPJ_UINT32)tmp; /* should be between 1 and 255 */
i, l_img_comp->prec);
return OPJ_FALSE;
}
-
#ifdef USE_JPWL
if (l_cp->correct) {
/* if JPWL is on, we check whether TX errors have damaged
++l_img_comp;
}
+ if (l_cp->tdx == 0 || l_cp->tdy == 0) {
+ return OPJ_FALSE;
+ }
+
/* 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);
j2k->m_cp.m_specific_param.m_dec.m_layer = parameters->cp_layer;
j2k->m_cp.m_specific_param.m_dec.m_reduce = parameters->cp_reduce;
+ j2k->dump_state = (parameters->flags & OPJ_DPARAMETERS_DUMP_FLAG);
#ifdef USE_JPWL
j2k->m_cp.correct = parameters->jpwl_correct;
j2k->m_cp.exp_comps = parameters->jpwl_exp_comps;
opj_event_msg(p_manager, EVT_ERROR, "Failed to decode.\n");
return OPJ_FALSE;
}
+ p_j2k->m_tcd->enumcs = p_j2k->enumcs;
/* p_data can be set to NULL when the call will take care of using */
/* itself the TCD data. This is typically the case for whole single */