#include <stdlib.h>
#include <string.h>
#include <ctype.h>
+#include <limits.h>
#include "openjpeg.h"
#include "convert.h"
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;
}
}
return NULL;
}
+ /* This limitation could be removed by making sure to use size_t below */
+ if (header_info.height != 0 &&
+ header_info.width > INT_MAX / header_info.height) {
+ fprintf(stderr, "pnmtoimage:Image %dx%d too big!\n",
+ header_info.width, header_info.height);
+ fclose(fp);
+ return NULL;
+ }
+
format = header_info.format;
switch (format) {
return image;
}/* pnmtoimage() */
+static int are_comps_similar(opj_image_t * image)
+{
+ unsigned int i;
+ for (i = 1; i < image->numcomps; i++) {
+ if (image->comps[0].dx != image->comps[i].dx ||
+ image->comps[0].dy != image->comps[i].dy ||
+ (i <= 2 &&
+ (image->comps[0].prec != image->comps[i].prec ||
+ image->comps[0].sgnd != image->comps[i].sgnd))) {
+ return OPJ_FALSE;
+ }
+ }
+ return OPJ_TRUE;
+}
+
+
int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
{
int *red, *green, *blue, *alpha;
ncomp = 1;
}
- if ((force_split == 0) &&
- (ncomp == 2 /* GRAYA */
- || (ncomp > 2 /* RGB, RGBA */
- && image->comps[0].dx == image->comps[1].dx
- && image->comps[1].dx == image->comps[2].dx
- && image->comps[0].dy == image->comps[1].dy
- && image->comps[1].dy == image->comps[2].dy
- && image->comps[0].prec == image->comps[1].prec
- && image->comps[1].prec == image->comps[2].prec
- ))) {
+ if ((force_split == 0) && ncomp >= 2 &&
+ are_comps_similar(image)) {
fdest = fopen(outfile, "wb");
if (!fdest) {
{
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);