#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include "openjpeg.h"
+#ifdef WIN32
#include "../libs/libtiff/tiffio.h"
+#include "../libs/libpng/png.h"
+#else
+#include <tiffio.h>
+#include <png.h>
+#endif /* WIN32 */
+#include "openjpeg.h"
#include "convert.h"
/*
} tga_header;
#pragma pack(pop) // Return to normal structure packing alignment.
-int tga_readheader(FILE *fp, int *bits_per_pixel, int *width, int *height, int *flip_image)
+int tga_readheader(FILE *fp, uint32 *bits_per_pixel, uint32 *width, uint32 *height, int *flip_image)
{
int palette_size;
tga_header tga ;
}
}
- width = int_ceildiv(image->x1-image->x0, image->comps[0].dx);
- height = int_ceildiv(image->y1-image->y0, image->comps[0].dy);
+ width = image->comps[0].w;
+ height = image->comps[0].h;
// Mono with alpha, or RGB with alpha.
write_alpha = (image->numcomps==2) || (image->numcomps==4);
// Write TGA header
bpp = write_alpha ? 32 : 24;
- if (!tga_writeheader(fdest, bpp, width, height, true))
+ if (!tga_writeheader(fdest, bpp, width , height, true))
return 1;
alpha_channel = image->numcomps-1;
}
int imagetobmp(opj_image_t * image, const char *outfile) {
- int w, wr, h, hr;
+ int w, h;
int i, pad;
FILE *fdest = NULL;
int adjustR, adjustG, adjustB;
return 1;
}
- w = image->comps[0].w;
- wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
-
+ w = image->comps[0].w;
h = image->comps[0].h;
- hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
fprintf(fdest, "BM");
/* FILE HEADER */
/* ------------- */
fprintf(fdest, "%c%c%c%c",
- (unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) + 54) & 0xff,
- (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 8) & 0xff,
- (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 16) & 0xff,
- (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54) >> 24) & 0xff);
+ (unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
+ (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) & 0xff,
+ (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) & 0xff,
+ (unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
/* INFO HEADER */
/* ------------- */
fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
- fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
- (unsigned char) ((wr) >> 8) & 0xff,
- (unsigned char) ((wr) >> 16) & 0xff,
- (unsigned char) ((wr) >> 24) & 0xff);
- fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
- (unsigned char) ((hr) >> 8) & 0xff,
- (unsigned char) ((hr) >> 16) & 0xff,
- (unsigned char) ((hr) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
+ (unsigned char) ((w) >> 8) & 0xff,
+ (unsigned char) ((w) >> 16) & 0xff,
+ (unsigned char) ((w) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
+ (unsigned char) ((h) >> 8) & 0xff,
+ (unsigned char) ((h) >> 16) & 0xff,
+ (unsigned char) ((h) >> 24) & 0xff);
fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
- fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * hr * wr + 3 * hr * (wr % 2)) & 0xff,
- (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 8) & 0xff,
- (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 16) & 0xff,
- (unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
+ (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
+ (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
+ (unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
else
adjustB = 0;
- for (i = 0; i < wr * hr; i++) {
+ for (i = 0; i < w * h; i++) {
unsigned char rc, gc, bc;
int r, g, b;
- r = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
+ r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
- g = image->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
+ g = image->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
gc = (unsigned char) ((g >> adjustG)+((g >> (adjustG-1))%2));
- b = image->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
+ b = image->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
bc = (unsigned char) ((b >> adjustB)+((b >> (adjustB-1))%2));
fprintf(fdest, "%c%c%c", bc, gc, rc);
- if ((i + 1) % wr == 0) {
- for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
+ if ((i + 1) % w == 0) {
+ for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */
fprintf(fdest, "%c", 0);
}
}
<<-- <<-- <<-- <<-- */
fdest = fopen(outfile, "wb");
- w = image->comps[0].w;
- wr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
-
+ w = image->comps[0].w;
h = image->comps[0].h;
- hr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
fprintf(fdest, "BM");
/* FILE HEADER */
/* ------------- */
- fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + 54 + 1024 + hr * (wr % 2)) & 0xff,
- (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 8) & 0xff,
- (unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2)) >> 16) & 0xff,
- (unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2)) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
+ (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) & 0xff,
+ (unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) & 0xff,
+ (unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
((54 + 1024) >> 16) & 0xff,
/* INFO HEADER */
/* ------------- */
fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff, ((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
- fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
- (unsigned char) ((wr) >> 8) & 0xff,
- (unsigned char) ((wr) >> 16) & 0xff,
- (unsigned char) ((wr) >> 24) & 0xff);
- fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
- (unsigned char) ((hr) >> 8) & 0xff,
- (unsigned char) ((hr) >> 16) & 0xff,
- (unsigned char) ((hr) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (unsigned char) ((w) & 0xff),
+ (unsigned char) ((w) >> 8) & 0xff,
+ (unsigned char) ((w) >> 16) & 0xff,
+ (unsigned char) ((w) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (unsigned char) ((h) & 0xff),
+ (unsigned char) ((h) >> 8) & 0xff,
+ (unsigned char) ((h) >> 16) & 0xff,
+ (unsigned char) ((h) >> 24) & 0xff);
fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff, ((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
- fprintf(fdest, "%c%c%c%c", (unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
- (unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) & 0xff,
- (unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) & 0xff,
- (unsigned char) ((hr * wr + hr * (wr % 2)) >> 24) & 0xff);
+ fprintf(fdest, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
+ (unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
+ (unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
+ (unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff, ((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff, ((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
if (image->comps[0].prec > 8) {
adjustR = image->comps[0].prec - 8;
printf("BMP CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
- }
+ }else
+ adjustR = 0;
for (i = 0; i < 256; i++) {
fprintf(fdest, "%c%c%c%c", i, i, i, 0);
}
- for (i = 0; i < wr * hr; i++) {
+ for (i = 0; i < w * h; i++) {
unsigned char rc;
int r;
- r = image->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
+ r = image->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
fprintf(fdest, "%c", rc);
- if ((i + 1) % wr == 0) {
- for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--) /* ADD */
+ if ((i + 1) % w == 0) {
+ for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */
fprintf(fdest, "%c", 0);
}
}
}
int imagetopgx(opj_image_t * image, const char *outfile) {
- int w, wr, h, hr;
+ int w, h;
int i, j, compno;
FILE *fdest = NULL;
}
w = image->comps[compno].w;
- wr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
-
h = image->comps[compno].h;
- hr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
- fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, wr, hr);
+ fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec, w, h);
if (comp->prec <= 8) {
nbytes = 1;
} else if (comp->prec <= 16) {
} else {
nbytes = 4;
}
- for (i = 0; i < wr * hr; i++) {
- int v = image->comps[compno].data[i / wr * w + i % wr];
+ for (i = 0; i < w * h; i++) {
+ int v = image->comps[compno].data[i];
for (j = nbytes - 1; j >= 0; j--) {
char byte = (char) (v >> (j * 8));
fwrite(&byte, 1, 1, fdest);
}
int imagetopnm(opj_image_t * image, const char *outfile) {
- int w, wr, wrr, h, hr, hrr, max;
+ int w, wr, h, hr, max;
int i, compno;
int adjustR, adjustG, adjustB, adjustX;
FILE *fdest = NULL;
w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
wr = image->comps[0].w;
- wrr = int_ceildivpow2(image->comps[0].w, image->comps[0].factor);
h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
hr = image->comps[0].h;
- hrr = int_ceildivpow2(image->comps[0].h, image->comps[0].factor);
max = image->comps[0].prec > 8 ? 255 : (1 << image->comps[0].prec) - 1;
image->comps[0].x0 = int_ceildivpow2(image->comps[0].x0 - int_ceildiv(image->x0, image->comps[0].dx), image->comps[0].factor);
image->comps[0].y0 = int_ceildivpow2(image->comps[0].y0 - int_ceildiv(image->y0, image->comps[0].dy), image->comps[0].factor);
- fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max);
+ fprintf(fdest, "P6\n%d %d\n%d\n", wr, hr, max);
if (image->comps[0].prec > 8) {
adjustR = image->comps[0].prec - 8;
adjustB = 0;
- for (i = 0; i < wrr * hrr; i++) {
+ for (i = 0; i < wr * hr; i++) {
int r, g, b;
unsigned char rc,gc,bc;
- r = image->comps[0].data[i / wrr * wr + i % wrr];
+ r = image->comps[0].data[i];
r += (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0);
rc = (unsigned char) ((r >> adjustR)+((r >> (adjustR-1))%2));
- g = image->comps[1].data[i / wrr * wr + i % wrr];
+ g = image->comps[1].data[i];
g += (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0);
gc = (unsigned char) ((g >> adjustG)+((g >> (adjustG-1))%2));
- b = image->comps[2].data[i / wrr * wr + i % wrr];
+ b = image->comps[2].data[i];
b += (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0);
bc = (unsigned char) ((b >> adjustB)+((b >> (adjustB-1))%2));
w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
wr = image->comps[compno].w;
- wrr = int_ceildivpow2(image->comps[compno].w, image->comps[compno].factor);
h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
hr = image->comps[compno].h;
- hrr = int_ceildivpow2(image->comps[compno].h, image->comps[compno].factor);
max = image->comps[compno].prec > 8 ? 255 : (1 << image->comps[compno].prec) - 1;
image->comps[compno].x0 = int_ceildivpow2(image->comps[compno].x0 - int_ceildiv(image->x0, image->comps[compno].dx), image->comps[compno].factor);
image->comps[compno].y0 = int_ceildivpow2(image->comps[compno].y0 - int_ceildiv(image->y0, image->comps[compno].dy), image->comps[compno].factor);
- fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max);
+ fprintf(fdest, "P5\n%d %d\n%d\n", wr, hr, max);
if (image->comps[compno].prec > 8) {
adjustX = image->comps[0].prec - 8;
else
adjustX = 0;
- for (i = 0; i < wrr * hrr; i++) {
+ for (i = 0; i < wr * hr; i++) {
int l;
unsigned char lc;
- l = image->comps[compno].data[i / wrr * wr + i % wrr];
+ l = image->comps[compno].data[i];
l += (image->comps[compno].sgnd ? 1 << (image->comps[compno].prec - 1) : 0);
lc = (unsigned char) ((l >> adjustX)+((l >> (adjustX-1))%2));
fprintf(fdest, "%c", lc);
}tiff_infoheader_t;
int imagetotif(opj_image_t * image, const char *outfile) {
- int width, height, imgsize ;
+ int width, height, imgsize;
int bps,index,adjust = 0;
int last_i=0;
TIFF *tif;
width = image->comps[0].w;
height = image->comps[0].h;
- imgsize = image->comps[0].w * image->comps[0].h ;
+ imgsize = width * height ;
bps = image->comps[0].prec;
/* Set tags */
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
/* Get a buffer for the data */
- buf = _TIFFmalloc(TIFFStripSize(tif));
- index=0;
- strip_size=0;
strip_size=TIFFStripSize(tif);
+ buf = _TIFFmalloc(strip_size);
+ index=0;
adjust = image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0;
for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
unsigned char *dat8;
int i, ssize;
ssize = TIFFStripSize(tif);
- dat8 = buf;
+ dat8 = (unsigned char*)buf;
if (image->comps[0].prec == 8){
for (i=0; i<ssize-2; i+=3) { // 8 bits per pixel
int r = 0,g = 0,b = 0;
width = image->comps[0].w;
height = image->comps[0].h;
- imgsize = image->comps[0].w * image->comps[0].h ;
+ imgsize = width * height;
bps = image->comps[0].prec;
/* Set tags */
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
/* Get a buffer for the data */
- buf = _TIFFmalloc(TIFFStripSize(tif));
- index = 0;
- strip_size = 0;
strip_size = TIFFStripSize(tif);
+ buf = _TIFFmalloc(strip_size);
+ index = 0;
for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
unsigned char *dat8;
int i;
- dat8 = buf;
+ dat8 = (unsigned char*)buf;
if (image->comps[0].prec == 8){
for (i=0; i<TIFFStripSize(tif); i+=1) { // 8 bits per pixel
if(index < imgsize){
break;
}
}else{
- fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
+ fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
fprintf(stderr,"Aborting\n");
return 1;
}
_TIFFfree(buf);
TIFFClose(tif);
}else{
- fprintf(stderr,"False color format. Only RGB & Grayscale has been implemented\n");
+ fprintf(stderr,"TIFF file creation. Bad color format. Only RGB & Grayscale has been implemented\n");
fprintf(stderr,"Aborting\n");
return 1;
}
unsigned char *dat8;
int i, ssize;
ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
- dat8 = buf;
+ dat8 = (unsigned char*)buf;
if (Info.tiBps==12){
for (i=0; i<ssize; i+=9) { /*12 bits per pixel*/
}
}
else{
- fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
+ fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
fprintf(stderr,"Aborting\n");
return NULL;
}
unsigned char *dat8;
int i, ssize;
ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
- dat8 = buf;
+ dat8 = (unsigned char*)buf;
if (Info.tiBps==12){
for (i=0; i<ssize; i+=3) { /* 12 bits per pixel*/
}
}
else{
- fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
+ fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",Info.tiBps);
fprintf(stderr,"Aborting\n");
return NULL;
}
_TIFFfree(buf);
TIFFClose(tif);
}else{
- fprintf(stderr,"False color format. Only RGB & Grayscale has been implemented\n");
+ fprintf(stderr,"TIFF file creation. Bad color format. Only RGB & Grayscale has been implemented\n");
fprintf(stderr,"Aborting\n");
return NULL;
}
}
}
}
- else
+ else if(raw_cp->rawBitDepth <= 16)
{
- unsigned short value = 0;
+ unsigned short value;
for(compno = 0; compno < numcomps; compno++) {
for (i = 0; i < w * h; i++) {
- if (!fread(&value, 2, 1, f)) {
+ unsigned char temp;
+ if (!fread(&temp, 1, 1, f)) {
fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
return NULL;
}
+ value = temp << 8;
+ if (!fread(&temp, 1, 1, f)) {
+ fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
+ return NULL;
+ }
+ value += temp;
image->comps[compno].data[i] = raw_cp->rawSigned?(short)value:value;
}
}
}
+ else {
+ fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
+ return NULL;
+ }
if (fread(&ch, 1, 1, f)) {
fprintf(stderr,"Warning. End of raw file not reached... processing anyway\n");
int imagetoraw(opj_image_t * image, const char *outfile)
{
FILE *rawFile = NULL;
- int compno, pixelsToWrite, offset, cont;
+ int compno;
+ int w, h;
+ int line, row;
+ int *ptr;
if((image->numcomps * image->x1 * image->y1) == 0)
{
fprintf(stdout,"Component %d characteristics: %dx%dx%d %s\n", compno, image->comps[compno].w,
image->comps[compno].h, image->comps[compno].prec, image->comps[compno].sgnd==1 ? "signed": "unsigned");
- pixelsToWrite = image->comps[compno].w * image->comps[compno].h;
- offset = 0;
+ w = image->comps[compno].w;
+ h = image->comps[compno].h;
if(image->comps[compno].prec <= 8)
{
{
signed char curr;
int mask = (1 << image->comps[compno].prec) - 1;
- for(cont = 0; cont < pixelsToWrite; cont++)
- {
- curr = (signed char) (image->comps[compno].data[cont] & mask);
- fwrite(&curr, sizeof(signed char), 1, rawFile);
+ ptr = image->comps[compno].data;
+ for (line = 0; line < h; line++) {
+ for(row = 0; row < w; row++) {
+ curr = (signed char) (*ptr & mask);
+ fwrite(&curr, sizeof(signed char), 1, rawFile);
+ ptr++;
+ }
}
}
else if(image->comps[compno].sgnd == 0)
{
unsigned char curr;
int mask = (1 << image->comps[compno].prec) - 1;
- for(cont = 0; cont < pixelsToWrite; cont++)
- {
- curr = (unsigned char) (image->comps[compno].data[cont] & mask);
- fwrite(&curr, sizeof(unsigned char), 1, rawFile);
+ ptr = image->comps[compno].data;
+ for (line = 0; line < h; line++) {
+ for(row = 0; row < w; row++) {
+ curr = (unsigned char) (*ptr & mask);
+ fwrite(&curr, sizeof(unsigned char), 1, rawFile);
+ ptr++;
+ }
}
}
}
{
signed short int curr;
int mask = (1 << image->comps[compno].prec) - 1;
- for(cont = 0; cont < pixelsToWrite; cont++)
- {
- curr = (signed short int) (image->comps[compno].data[cont] & mask);
- fwrite(&curr, sizeof(signed short int), 1, rawFile);
+ ptr = image->comps[compno].data;
+ for (line = 0; line < h; line++) {
+ for(row = 0; row < w; row++) {
+ unsigned char temp;
+ curr = (signed short int) (*ptr & mask);
+ temp = (unsigned char) (curr >> 8);
+ fwrite(&temp, 1, 1, rawFile);
+ temp = (unsigned char) curr;
+ fwrite(&temp, 1, 1, rawFile);
+ ptr++;
+ }
}
}
else if(image->comps[compno].sgnd == 0)
{
unsigned short int curr;
int mask = (1 << image->comps[compno].prec) - 1;
- for(cont = 0; cont < pixelsToWrite; cont++)
- {
- curr = (unsigned short int) (image->comps[compno].data[cont] & mask);
- fwrite(&curr, sizeof(unsigned short int), 1, rawFile);
+ ptr = image->comps[compno].data;
+ for (line = 0; line < h; line++) {
+ for(row = 0; row < w; row++) {
+ unsigned char temp;
+ curr = (unsigned short int) (*ptr & mask);
+ temp = (unsigned char) (curr >> 8);
+ fwrite(&temp, 1, 1, rawFile);
+ temp = (unsigned char) curr;
+ fwrite(&temp, 1, 1, rawFile);
+ ptr++;
+ }
}
}
}
else if (image->comps[compno].prec <= 32)
{
-
-
+ fprintf(stderr,"More than 16 bits per component no handled yet\n");
+ return 1;
}
else
{
- fprintf(stderr,"\nError: invalid precision\n");
+ fprintf(stderr,"Error: invalid precision: %d\n", image->comps[compno].prec);
return 1;
}
}
fclose(rawFile);
return 0;
}
+
+opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params)
+#ifdef WIN32
+{
+ printf("Error. PNG format is not yet handled under windows\n");
+ return NULL;
+}
+#else
+{
+ png_bytep row;
+ png_structp png;
+ png_infop info;
+ double gamma, display_exponent;
+ int bit_depth, interlace_type, compression_type, filter_type;
+ int unit, pass, nr_passes;
+ png_uint_32 resx, resy;
+ unsigned int i, max, src_w;
+ png_uint_32 width, height;
+ int color_type, has_alpha;
+ unsigned char *png_buf, *s;
+ FILE *reader;
+/* j2k: */
+ opj_image_t *image;
+ opj_image_cmptparm_t cmptparm[4];
+ int sub_dx, sub_dy;
+ unsigned int nr_comp;
+ int *r, *g, *b, *a;
+
+ if ((reader = fopen(read_idf, "rb")) == NULL) {
+ fprintf(stderr, "pngtoimage: can not open %s\n", read_idf);
+ return NULL;
+ }
+ nr_passes = 0;
+ png_buf = NULL;
+
+/* libpng-VERSION/example.c:
+ * PC : screen_gamma = 2.2;
+ * Mac: screen_gamma = 1.7 or 1.0;
+*/
+ display_exponent = 2.2;
+
+ if ((png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
+ NULL, NULL, NULL)) == NULL)
+ goto fin;
+ if ((info = png_create_info_struct(png)) == NULL)
+ goto fin;
+
+ if (setjmp(png_jmpbuf(png)))
+ goto fin;
+
+ png_init_io(png, reader);
+ png_read_info(png, info);
+
+ png_get_IHDR(png, info, &width, &height,
+ &bit_depth, &color_type, &interlace_type,
+ &compression_type, &filter_type);
+
+ if (color_type == PNG_COLOR_TYPE_PALETTE)
+ png_set_expand(png);
+ else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
+ png_set_expand(png);
+
+ if (png_get_valid(png, info, PNG_INFO_tRNS))
+ png_set_expand(png);
+
+ if (bit_depth == 16)
+ png_set_strip_16(png);
+
+/* GRAY => RGB; GRAY_ALPHA => RGBA
+*/
+ if (color_type == PNG_COLOR_TYPE_GRAY
+ || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
+ png_set_gray_to_rgb(png);
+ color_type =
+ (color_type == PNG_COLOR_TYPE_GRAY ? PNG_COLOR_TYPE_RGB :
+ PNG_COLOR_TYPE_RGB_ALPHA);
+ }
+ if (!png_get_gAMA(png, info, &gamma))
+ gamma = 0.45455;
+
+ png_set_gamma(png, display_exponent, gamma);
+
+ nr_passes = png_set_interlace_handling(png);
+
+ png_read_update_info(png, info);
+
+ png_get_pHYs(png, info, &resx, &resy, &unit);
+
+ color_type = png_get_color_type(png, info);
+
+ has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
+
+ if (has_alpha)
+ nr_comp = 4;
+ else
+ nr_comp = 3;
+
+ src_w = width * nr_comp;
+ png_buf = (unsigned char *) malloc(src_w * height);
+
+ if (nr_passes == 0)
+ nr_passes = 1;
+
+ for (pass = 0; pass < nr_passes; pass++) {
+ s = png_buf;
+
+ for (i = 0; i < height; i++) {
+/* libpng.3:
+ * If you want the "sparkle" effect, just call png_read_rows() as
+ * normal, with the third parameter NULL.
+*/
+ png_read_rows(png, &s, NULL, 1);
+
+ s += src_w;
+ }
+ }
+ memset(&cmptparm, 0, 4 * sizeof(opj_image_cmptparm_t));
+
+ sub_dx = params->subsampling_dx;
+ sub_dy = params->subsampling_dy;
+
+ for (i = 0; i < nr_comp; ++i) {
+ cmptparm[i].prec = 8;
+ cmptparm[i].bpp = 8;
+ cmptparm[i].sgnd = 0;
+ cmptparm[i].dx = sub_dx;
+ cmptparm[i].dy = sub_dy;
+ cmptparm[i].w = width;
+ cmptparm[i].h = height;
+ }
+
+ image = opj_image_create(nr_comp, &cmptparm[0], CLRSPC_SRGB);
+
+ if (image == NULL)
+ goto fin;
+
+ image->x0 = params->image_offset_x0;
+ image->y0 = params->image_offset_y0;
+ image->x1 =
+ params->image_offset_x0 + (width - 1) * sub_dx + 1 + image->x0;
+ image->y1 =
+ params->image_offset_y0 + (height - 1) * sub_dy + 1 + image->y0;
+
+ r = image->comps[0].data;
+ g = image->comps[1].data;
+ b = image->comps[2].data;
+ a = image->comps[3].data;
+ s = png_buf;
+
+ max = width * height;
+
+ for (i = 0; i < max; ++i) {
+ *r++ = *s++;
+ *g++ = *s++;
+ *b++ = *s++;
+
+ if (has_alpha)
+ *a++ = *s++;
+ }
+
+ fin:
+ if (png)
+ png_destroy_read_struct(&png, &info, NULL);
+ if (png_buf)
+ free(png_buf);
+
+ fclose(reader);
+
+ return image;
+
+} /* pngtoimage() */
+#endif
+
+int imagetopng(opj_image_t * image, const char *write_idf)
+#ifdef WIN32
+{
+ printf("Error. PNG format is not yet handled under windows\n");
+ return -1;
+}
+#else
+{
+ FILE *writer;
+ png_structp png_ptr;
+ png_infop info_ptr;
+ int *rs, *gs, *bs, *as;
+ unsigned char *row_buf, *d;
+ int fails, mono, graya, rgb, rgba;
+ int width, height, nr_colors, color_type;
+ int bit_depth, adjust, x, y;
+ png_color_8 sig_bit;
+
+ writer = fopen(write_idf, "wb");
+
+ if (writer == NULL)
+ return 1;
+
+ info_ptr = NULL;
+ fails = 1;
+
+/* Create and initialize the png_struct with the desired error handler
+ * functions. If you want to use the default stderr and longjump method,
+ * you can supply NULL for the last three parameters. We also check that
+ * the library version is compatible with the one used at compile time,
+ * in case we are using dynamically linked libraries. REQUIRED.
+*/
+ png_ptr =
+ png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+/*png_voidp user_error_ptr, user_error_fn, user_warning_fn); */
+
+ if (png_ptr == NULL)
+ goto fin;
+
+/* Allocate/initialize the image information data. REQUIRED
+*/
+ info_ptr = png_create_info_struct(png_ptr);
+
+ if (info_ptr == NULL)
+ goto fin;
+
+/* Set error handling. REQUIRED if you are not supplying your own
+ * error handling functions in the png_create_write_struct() call.
+*/
+ if (setjmp(png_jmpbuf(png_ptr)))
+ goto fin;
+
+/* I/O initialization functions is REQUIRED
+*/
+ png_init_io(png_ptr, writer);
+
+/* Set the image information here. Width and height are up to 2^31,
+ * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
+ * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
+ * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
+ * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
+ * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
+ * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE.
+ * REQUIRED
+*/
+ png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);
+
+ mono = graya = rgb = rgba = adjust = 0;
+
+ nr_colors = image->numcomps;
+ width = image->comps[0].w;
+ height = image->comps[0].h;
+ rs = image->comps[0].data;
+
+ if (nr_colors == 2) {
+ graya = 1;
+ color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
+ sig_bit.gray = image->comps[0].prec;
+ bit_depth = image->comps[0].prec;
+ as = image->comps[1].data;
+ } else if (nr_colors == 3) {
+ rgb = 1;
+ color_type = PNG_COLOR_TYPE_RGB;
+ sig_bit.red = image->comps[0].prec;
+ sig_bit.green = image->comps[1].prec;
+ sig_bit.blue = image->comps[2].prec;
+ bit_depth = image->comps[0].prec;
+ gs = image->comps[1].data;
+ bs = image->comps[2].data;
+ if (image->comps[0].sgnd)
+ adjust = 1 << (image->comps[0].prec - 1);
+ } else if (nr_colors == 4) {
+ rgb = rgba = 1;
+ color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+ sig_bit.red = image->comps[0].prec;
+ sig_bit.green = image->comps[1].prec;
+ sig_bit.blue = image->comps[2].prec;
+ sig_bit.alpha = image->comps[3].prec;
+ bit_depth = image->comps[0].prec;
+ gs = image->comps[1].data;
+ bs = image->comps[2].data;
+ as = image->comps[3].data;
+ if (image->comps[0].sgnd)
+ adjust = 1 << (image->comps[0].prec - 1);
+ } else {
+ mono = 1;
+ color_type = PNG_COLOR_TYPE_GRAY;
+ sig_bit.gray = image->comps[0].prec;
+ bit_depth = image->comps[0].prec;
+ }
+ png_set_sBIT(png_ptr, info_ptr, &sig_bit);
+
+ png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
+ color_type,
+ PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
+
+#ifdef HIDDEN_CODE
+/* Optional gamma chunk is strongly suggested if you have any guess
+ * as to the correct gamma of the image.
+*/
+
+ if (gamma > 0.0) {
+ png_set_gAMA(png_ptr, info_ptr, gamma);
+ }
+#endif /* HIDDEN_CODE */
+#ifdef HIDDEN_CODE
+ if (have_bg) {
+ png_color_16 background;
+
+ background.red = bg_red;
+ background.green = bg_green;
+ background.blue = bg_blue;
+
+ png_set_bKGD(png_ptr, info_ptr, &background);
+ }
+#endif /* HIDDEN_CODE */
+ png_write_info(png_ptr, info_ptr);
+
+ png_set_packing(png_ptr);
+
+ row_buf = (unsigned char *) malloc(width * nr_colors);
+
+ for (y = 0; y < height; ++y) {
+ d = row_buf;
+
+ for (x = 0; x < width; ++x) {
+ if (mono) {
+ *d++ = (unsigned char) *rs++;
+ }
+ if (graya) {
+ *d++ = (unsigned char) *rs++;
+ *d++ = (unsigned char) *as++;
+ } else if (rgb) {
+ *d++ = (unsigned char) (*rs++ + adjust);
+ *d++ = (unsigned char) (*gs++ + adjust);
+ *d++ = (unsigned char) (*bs++ + adjust);
+
+ if (rgba)
+ *d++ = (unsigned char) (*as++ + adjust);
+ }
+ } /* for(x) */
+
+ png_write_row(png_ptr, row_buf);
+
+ } /* for(y) */
+
+ png_write_end(png_ptr, info_ptr);
+
+ fails = 0;
+
+ fin:
+
+ if (png_ptr) {
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+ }
+ fclose(writer);
+
+ return fails;
+}
+#endif