+/* -->> -->> -->> -->>
+
+ TIFF IMAGE FORMAT
+
+ <<-- <<-- <<-- <<-- */
+
+typedef struct tiff_infoheader{
+ DWORD tiWidth; // Width of Image in pixel
+ DWORD tiHeight; // Height of Image in pixel
+ DWORD tiPhoto; // Photometric
+ WORD tiBps; // Bits per sample
+ WORD tiSf; // Sample Format
+ WORD tiSpp; // Sample per pixel 1-bilevel,gray scale , 2- RGB
+ WORD tiPC; // Planar config (1-Interleaved, 2-Planarcomp)
+}tiff_infoheader_t;
+
+int imagetotif(opj_image_t * image, const char *outfile) {
+ int width, height, imgsize;
+ int bps,index,adjust = 0;
+ int last_i=0;
+ TIFF *tif;
+ tdata_t buf;
+ tstrip_t strip;
+ tsize_t strip_size;
+
+ if (image->numcomps == 3 && 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) {
+
+ /* -->> -->> -->>
+ RGB color
+ <<-- <<-- <<-- */
+
+ tif = TIFFOpen(outfile, "wb");
+ if (!tif) {
+ fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
+ return 1;
+ }
+
+ width = image->comps[0].w;
+ height = image->comps[0].h;
+ imgsize = width * height ;
+ bps = image->comps[0].prec;
+ /* Set tags */
+ TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
+ TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
+ TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
+ TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
+ TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
+ TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
+ TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
+ TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
+
+ /* Get a buffer for the data */
+ 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 = (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;
+ if(index < imgsize){
+ r = image->comps[0].data[index];
+ g = image->comps[1].data[index];
+ b = image->comps[2].data[index];
+ if (image->comps[0].sgnd){
+ r += adjust;
+ g += adjust;
+ b += adjust;
+ }
+ dat8[i+0] = r ; // R
+ dat8[i+1] = g ; // G
+ dat8[i+2] = b ; // B
+ index++;
+ last_i = i+3;
+ }else
+ break;
+ }
+ if(last_i < ssize){
+ for (i=last_i; i<ssize; i+=3) { // 8 bits per pixel
+ int r = 0,g = 0,b = 0;
+ if(index < imgsize){
+ r = image->comps[0].data[index];
+ g = image->comps[1].data[index];
+ b = image->comps[2].data[index];
+ if (image->comps[0].sgnd){
+ r += adjust;
+ g += adjust;
+ b += adjust;
+ }
+ dat8[i+0] = r ; // R
+ if(i+1 <ssize) dat8[i+1] = g ; else break;// G
+ if(i+2 <ssize) dat8[i+2] = b ; else break;// B
+ index++;
+ }else
+ break;
+ }
+ }
+ }else if (image->comps[0].prec == 12){
+ for (i=0; i<ssize-8; i+=9) { // 12 bits per pixel
+ int r = 0,g = 0,b = 0;
+ int r1 = 0,g1 = 0,b1 = 0;
+ if((index < imgsize)&(index+1 < imgsize)){
+ r = image->comps[0].data[index];
+ g = image->comps[1].data[index];
+ b = image->comps[2].data[index];
+ r1 = image->comps[0].data[index+1];
+ g1 = image->comps[1].data[index+1];
+ b1 = image->comps[2].data[index+1];
+ if (image->comps[0].sgnd){
+ r += adjust;
+ g += adjust;
+ b += adjust;
+ r1 += adjust;
+ g1 += adjust;
+ b1 += adjust;
+ }
+ dat8[i+0] = (r >> 4);
+ dat8[i+1] = ((r & 0x0f) << 4 )|((g >> 8)& 0x0f);
+ dat8[i+2] = g ;
+ dat8[i+3] = (b >> 4);
+ dat8[i+4] = ((b & 0x0f) << 4 )|((r1 >> 8)& 0x0f);
+ dat8[i+5] = r1;
+ dat8[i+6] = (g1 >> 4);
+ dat8[i+7] = ((g1 & 0x0f)<< 4 )|((b1 >> 8)& 0x0f);
+ dat8[i+8] = b1;
+ index+=2;
+ last_i = i+9;
+ }else
+ break;
+ }
+ if(last_i < ssize){
+ for (i= last_i; i<ssize; i+=9) { // 12 bits per pixel
+ int r = 0,g = 0,b = 0;
+ int r1 = 0,g1 = 0,b1 = 0;
+ if((index < imgsize)&(index+1 < imgsize)){
+ r = image->comps[0].data[index];
+ g = image->comps[1].data[index];
+ b = image->comps[2].data[index];
+ r1 = image->comps[0].data[index+1];
+ g1 = image->comps[1].data[index+1];
+ b1 = image->comps[2].data[index+1];
+ if (image->comps[0].sgnd){
+ r += adjust;
+ g += adjust;
+ b += adjust;
+ r1 += adjust;
+ g1 += adjust;
+ b1 += adjust;
+ }
+ dat8[i+0] = (r >> 4);
+ if(i+1 <ssize) dat8[i+1] = ((r & 0x0f) << 4 )|((g >> 8)& 0x0f); else break;
+ if(i+2 <ssize) dat8[i+2] = g ; else break;
+ if(i+3 <ssize) dat8[i+3] = (b >> 4); else break;
+ if(i+4 <ssize) dat8[i+4] = ((b & 0x0f) << 4 )|((r1 >> 8)& 0x0f);else break;
+ if(i+5 <ssize) dat8[i+5] = r1; else break;
+ if(i+6 <ssize) dat8[i+6] = (g1 >> 4); else break;
+ if(i+7 <ssize) dat8[i+7] = ((g1 & 0x0f)<< 4 )|((b1 >> 8)& 0x0f);else break;
+ if(i+8 <ssize) dat8[i+8] = b1; else break;
+ index+=2;
+ }else
+ break;
+ }
+ }
+ }else if (image->comps[0].prec == 16){
+ for (i=0 ; i<ssize-5 ; i+=6) { // 16 bits per pixel
+ int r = 0,g = 0,b = 0;
+ if(index < imgsize){
+ r = image->comps[0].data[index];
+ g = image->comps[1].data[index];
+ b = image->comps[2].data[index];
+ if (image->comps[0].sgnd){
+ r += adjust;
+ g += adjust;
+ b += adjust;
+ }
+ dat8[i+0] = r;//LSB
+ dat8[i+1] = (r >> 8);//MSB
+ dat8[i+2] = g;
+ dat8[i+3] = (g >> 8);
+ dat8[i+4] = b;
+ dat8[i+5] = (b >> 8);
+ index++;
+ last_i = i+6;
+ }else
+ break;
+ }
+ if(last_i < ssize){
+ for (i=0 ; i<ssize ; i+=6) { // 16 bits per pixel
+ int r = 0,g = 0,b = 0;
+ if(index < imgsize){
+ r = image->comps[0].data[index];
+ g = image->comps[1].data[index];
+ b = image->comps[2].data[index];
+ if (image->comps[0].sgnd){
+ r += adjust;
+ g += adjust;
+ b += adjust;
+ }
+ dat8[i+0] = r;//LSB
+ if(i+1 <ssize) dat8[i+1] = (r >> 8);else break;//MSB
+ if(i+2 <ssize) dat8[i+2] = g; else break;
+ if(i+3 <ssize) dat8[i+3] = (g >> 8);else break;
+ if(i+4 <ssize) dat8[i+4] = b; else break;
+ if(i+5 <ssize) dat8[i+5] = (b >> 8);else break;
+ index++;
+ }else
+ break;
+ }
+ }
+ }else{
+ fprintf(stderr,"Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
+ fprintf(stderr,"Aborting\n");
+ return 1;
+ }
+ TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
+ }
+ _TIFFfree(buf);
+ TIFFClose(tif);
+ }else if (image->numcomps == 1){
+ /* -->> -->> -->>
+ Black and White
+ <<-- <<-- <<-- */
+
+ tif = TIFFOpen(outfile, "wb");
+ if (!tif) {
+ fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
+ return 1;
+ }
+
+ width = image->comps[0].w;
+ height = image->comps[0].h;
+ imgsize = width * height;
+ bps = image->comps[0].prec;
+
+ /* Set tags */
+ TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
+ TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
+ TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
+ TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
+ TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
+ TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
+ TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
+ TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
+
+ /* Get a buffer for the data */
+ strip_size = TIFFStripSize(tif);
+ buf = _TIFFmalloc(strip_size);
+ index = 0;
+ for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
+ unsigned char *dat8;
+ int i;
+ 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){
+ int r = 0;
+ r = image->comps[0].data[index];
+ if (image->comps[0].sgnd){
+ r += adjust;
+ }
+ dat8[i+0] = r;
+ index++;
+ }else
+ break;
+ }
+ }else if (image->comps[0].prec == 12){
+ for (i = 0; i<TIFFStripSize(tif); i+=3) { // 12 bits per pixel
+ if(index < imgsize){
+ int r = 0, r1 = 0;
+ r = image->comps[0].data[index];
+ r1 = image->comps[0].data[index+1];
+ if (image->comps[0].sgnd){
+ r += adjust;
+ r1 += adjust;
+ }
+ dat8[i+0] = (r >> 4);
+ dat8[i+1] = ((r & 0x0f) << 4 )|((r1 >> 8)& 0x0f);
+ dat8[i+2] = r1 ;
+ index+=2;
+ }else
+ break;
+ }
+ }else if (image->comps[0].prec == 16){
+ for (i=0; i<TIFFStripSize(tif); i+=2) { // 16 bits per pixel
+ if(index < imgsize){
+ int r = 0;
+ r = image->comps[0].data[index];
+ if (image->comps[0].sgnd){
+ r += adjust;
+ }
+ dat8[i+0] = r;
+ dat8[i+1] = r >> 8;
+ index++;
+ }else
+ break;
+ }
+ }else{
+ fprintf(stderr,"TIFF file creation. Bits=%d, Only 8,12,16 bits implemented\n",image->comps[0].prec);
+ fprintf(stderr,"Aborting\n");
+ return 1;
+ }
+ TIFFWriteEncodedStrip(tif, strip, buf, strip_size);
+ }
+ _TIFFfree(buf);
+ TIFFClose(tif);
+ }else{
+ fprintf(stderr,"TIFF file creation. Bad color format. Only RGB & Grayscale has been implemented\n");
+ fprintf(stderr,"Aborting\n");
+ return 1;
+ }
+ return 0;
+}
+
+opj_image_t* tiftoimage(const char *filename, opj_cparameters_t *parameters)
+{
+ int subsampling_dx = parameters->subsampling_dx;
+ int subsampling_dy = parameters->subsampling_dy;
+ TIFF *tif;
+ tiff_infoheader_t Info;
+ tdata_t buf;
+ tstrip_t strip;
+ tsize_t strip_size;
+ int j, numcomps, w, h,index;
+ OPJ_COLOR_SPACE color_space;
+ opj_image_cmptparm_t cmptparm[3];
+ opj_image_t * image = NULL;
+ int imgsize = 0;
+
+ tif = TIFFOpen(filename, "r");
+
+ if (!tif) {
+ fprintf(stderr, "Failed to open %s for reading\n", filename);
+ return 0;
+ }
+
+ TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Info.tiWidth);
+ TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Info.tiHeight);
+ TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &Info.tiBps);
+ TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &Info.tiSf);
+ TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &Info.tiSpp);
+ Info.tiPhoto = 0;
+ TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &Info.tiPhoto);
+ TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &Info.tiPC);
+ w= Info.tiWidth;
+ h= Info.tiHeight;
+
+ if (Info.tiPhoto == 2) {
+ /* -->> -->> -->>
+ RGB color
+ <<-- <<-- <<-- */
+
+ numcomps = 3;
+ color_space = CLRSPC_SRGB;
+ /* initialize image components*/
+ memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
+ for(j = 0; j < numcomps; j++) {
+ if (parameters->cp_cinema) {
+ cmptparm[j].prec = 12;
+ cmptparm[j].bpp = 12;
+ }else{
+ cmptparm[j].prec = Info.tiBps;
+ cmptparm[j].bpp = Info.tiBps;
+ }
+ cmptparm[j].sgnd = 0;
+ cmptparm[j].dx = subsampling_dx;
+ cmptparm[j].dy = subsampling_dy;
+ cmptparm[j].w = w;
+ cmptparm[j].h = h;
+ }
+ /* create the image*/
+ image = opj_image_create(numcomps, &cmptparm[0], color_space);
+ if(!image) {
+ TIFFClose(tif);
+ return NULL;
+ }
+
+ /* set image offset and reference grid */
+ image->x0 = parameters->image_offset_x0;
+ image->y0 = parameters->image_offset_y0;
+ image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
+ image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
+
+ buf = _TIFFmalloc(TIFFStripSize(tif));
+ strip_size=0;
+ strip_size=TIFFStripSize(tif);
+ index = 0;
+ imgsize = image->comps[0].w * image->comps[0].h ;
+ /* Read the Image components*/
+ for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
+ unsigned char *dat8;
+ int i, ssize;
+ ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
+ dat8 = (unsigned char*)buf;
+
+ if (Info.tiBps==12){
+ for (i=0; i<ssize; i+=9) { /*12 bits per pixel*/
+ if((index < imgsize)&(index+1 < imgsize)){
+ image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4);
+ image->comps[1].data[index] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
+ image->comps[2].data[index] = ( dat8[i+3]<<4) |(dat8[i+4]>>4);
+ image->comps[0].data[index+1] = ((dat8[i+4]& 0x0f)<< 8) | dat8[i+5];
+ image->comps[1].data[index+1] = ( dat8[i+6] <<4) |(dat8[i+7]>>4);
+ image->comps[2].data[index+1] = ((dat8[i+7]& 0x0f)<< 8) | dat8[i+8];
+ index+=2;
+ }else
+ break;
+ }
+ }
+ else if( Info.tiBps==16){
+ for (i=0; i<ssize; i+=6) { /* 16 bits per pixel */
+ if(index < imgsize){
+ image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0]; // R
+ image->comps[1].data[index] = ( dat8[i+3] << 8 ) | dat8[i+2]; // G
+ image->comps[2].data[index] = ( dat8[i+5] << 8 ) | dat8[i+4]; // B
+ if(parameters->cp_cinema){/* Rounding to 12 bits*/
+ image->comps[0].data[index] = (image->comps[0].data[index] + 0x08) >> 4 ;
+ image->comps[1].data[index] = (image->comps[1].data[index] + 0x08) >> 4 ;
+ image->comps[2].data[index] = (image->comps[2].data[index] + 0x08) >> 4 ;
+ }
+ index++;
+ }else
+ break;
+ }
+ }
+ else if ( Info.tiBps==8){
+ for (i=0; i<ssize; i+=3) { /* 8 bits per pixel */
+ if(index < imgsize){
+ image->comps[0].data[index] = dat8[i+0];// R
+ image->comps[1].data[index] = dat8[i+1];// G
+ image->comps[2].data[index] = dat8[i+2];// B
+ if(parameters->cp_cinema){/* Rounding to 12 bits*/
+ image->comps[0].data[index] = image->comps[0].data[index] << 4 ;
+ image->comps[1].data[index] = image->comps[1].data[index] << 4 ;
+ image->comps[2].data[index] = image->comps[2].data[index] << 4 ;
+ }
+ index++;
+ }else
+ break;
+ }
+ }
+ else{
+ 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 if(Info.tiPhoto == 1) {
+ /* -->> -->> -->>
+ Black and White
+ <<-- <<-- <<-- */
+
+ numcomps = 1;
+ color_space = CLRSPC_GRAY;
+ /* initialize image components*/
+ memset(&cmptparm[0], 0, sizeof(opj_image_cmptparm_t));
+ cmptparm[0].prec = Info.tiBps;
+ cmptparm[0].bpp = Info.tiBps;
+ cmptparm[0].sgnd = 0;
+ cmptparm[0].dx = subsampling_dx;
+ cmptparm[0].dy = subsampling_dy;
+ cmptparm[0].w = w;
+ cmptparm[0].h = h;
+
+ /* create the image*/
+ image = opj_image_create(numcomps, &cmptparm[0], color_space);
+ if(!image) {
+ TIFFClose(tif);
+ return NULL;
+ }
+ /* set image offset and reference grid */
+ image->x0 = parameters->image_offset_x0;
+ image->y0 = parameters->image_offset_y0;
+ image->x1 = !image->x0 ? (w - 1) * subsampling_dx + 1 : image->x0 + (w - 1) * subsampling_dx + 1;
+ image->y1 = !image->y0 ? (h - 1) * subsampling_dy + 1 : image->y0 + (h - 1) * subsampling_dy + 1;
+
+ buf = _TIFFmalloc(TIFFStripSize(tif));
+ strip_size = 0;
+ strip_size = TIFFStripSize(tif);
+ index = 0;
+ imgsize = image->comps[0].w * image->comps[0].h ;
+ /* Read the Image components*/
+ for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
+ unsigned char *dat8;
+ int i, ssize;
+ ssize = TIFFReadEncodedStrip(tif, strip, buf, strip_size);
+ dat8 = (unsigned char*)buf;
+
+ if (Info.tiBps==12){
+ for (i=0; i<ssize; i+=3) { /* 12 bits per pixel*/
+ if(index < imgsize){
+ image->comps[0].data[index] = ( dat8[i+0]<<4 ) |(dat8[i+1]>>4) ;
+ image->comps[0].data[index+1] = ((dat8[i+1]& 0x0f)<< 8) | dat8[i+2];
+ index+=2;
+ }else
+ break;
+ }
+ }
+ else if( Info.tiBps==16){
+ for (i=0; i<ssize; i+=2) { /* 16 bits per pixel */
+ if(index < imgsize){
+ image->comps[0].data[index] = ( dat8[i+1] << 8 ) | dat8[i+0];
+ index++;
+ }else
+ break;
+ }
+ }
+ else if ( Info.tiBps==8){
+ for (i=0; i<ssize; i+=1) { /* 8 bits per pixel */
+ if(index < imgsize){
+ image->comps[0].data[index] = dat8[i+0];
+ index++;
+ }else
+ break;
+ }
+ }
+ else{
+ 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,"TIFF file creation. Bad color format. Only RGB & Grayscale has been implemented\n");
+ fprintf(stderr,"Aborting\n");
+ return NULL;
+ }
+ return image;
+}
+
+/* -->> -->> -->> -->>
+
+ RAW IMAGE FORMAT
+
+ <<-- <<-- <<-- <<-- */
+
+opj_image_t* rawtoimage(const char *filename, opj_cparameters_t *parameters, raw_cparameters_t *raw_cp) {
+ int subsampling_dx = parameters->subsampling_dx;
+ int subsampling_dy = parameters->subsampling_dy;
+
+ FILE *f = NULL;
+ int i, compno, numcomps, w, h;
+ OPJ_COLOR_SPACE color_space;
+ opj_image_cmptparm_t *cmptparm;
+ opj_image_t * image = NULL;
+ unsigned short ch;
+
+ if((! (raw_cp->rawWidth & raw_cp->rawHeight & raw_cp->rawComp & raw_cp->rawBitDepth)) == 0)
+ {
+ fprintf(stderr,"\nError: invalid raw image parameters\n");
+ fprintf(stderr,"Please use the Format option -F:\n");
+ fprintf(stderr,"-F rawWidth,rawHeight,rawComp,rawBitDepth,s/u (Signed/Unsigned)\n");
+ fprintf(stderr,"Example: -i lena.raw -o lena.j2k -F 512,512,3,8,u\n");
+ fprintf(stderr,"Aborting\n");
+ return NULL;
+ }
+
+ f = fopen(filename, "rb");
+ if (!f) {
+ fprintf(stderr, "Failed to open %s for reading !!\n", filename);
+ fprintf(stderr,"Aborting\n");
+ return NULL;
+ }
+ numcomps = raw_cp->rawComp;
+ color_space = CLRSPC_SRGB;
+ w = raw_cp->rawWidth;
+ h = raw_cp->rawHeight;
+ cmptparm = (opj_image_cmptparm_t*) malloc(numcomps * sizeof(opj_image_cmptparm_t));
+
+ /* initialize image components */
+ memset(&cmptparm[0], 0, numcomps * sizeof(opj_image_cmptparm_t));
+ for(i = 0; i < numcomps; i++) {
+ cmptparm[i].prec = raw_cp->rawBitDepth;
+ cmptparm[i].bpp = raw_cp->rawBitDepth;
+ cmptparm[i].sgnd = raw_cp->rawSigned;
+ cmptparm[i].dx = subsampling_dx;
+ cmptparm[i].dy = subsampling_dy;
+ cmptparm[i].w = w;
+ cmptparm[i].h = h;
+ }
+ /* create the image */
+ image = opj_image_create(numcomps, &cmptparm[0], color_space);
+ if(!image) {
+ fclose(f);
+ return NULL;
+ }
+ /* set image offset and reference grid */
+ image->x0 = parameters->image_offset_x0;
+ image->y0 = parameters->image_offset_y0;
+ image->x1 = parameters->image_offset_x0 + (w - 1) * subsampling_dx + 1;
+ image->y1 = parameters->image_offset_y0 + (h - 1) * subsampling_dy + 1;
+
+ if(raw_cp->rawBitDepth <= 8)
+ {
+ unsigned char value = 0;
+ for(compno = 0; compno < numcomps; compno++) {
+ for (i = 0; i < w * h; i++) {
+ if (!fread(&value, 1, 1, f)) {
+ fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
+ return NULL;
+ }
+ image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
+ }
+ }
+ }
+ else if(raw_cp->rawBitDepth <= 16)
+ {
+ unsigned short value;
+ for(compno = 0; compno < numcomps; compno++) {
+ for (i = 0; i < w * h; i++) {
+ 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");
+ }
+ fclose(f);
+
+ return image;
+}
+
+int imagetoraw(opj_image_t * image, const char *outfile)
+{
+ FILE *rawFile = NULL;
+ unsigned int compno;
+ int w, h;
+ int line, row;
+ int *ptr;
+
+ if((image->numcomps * image->x1 * image->y1) == 0)
+ {
+ fprintf(stderr,"\nError: invalid raw image parameters\n");
+ return 1;
+ }
+
+ rawFile = fopen(outfile, "wb");
+ if (!rawFile) {
+ fprintf(stderr, "Failed to open %s for writing !!\n", outfile);
+ return 1;
+ }
+
+ fprintf(stdout,"Raw image characteristics: %d components\n", image->numcomps);
+
+ for(compno = 0; compno < image->numcomps; compno++)
+ {
+ 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");
+
+ w = image->comps[compno].w;
+ h = image->comps[compno].h;
+
+ if(image->comps[compno].prec <= 8)
+ {
+ if(image->comps[compno].sgnd == 1)
+ {
+ signed char curr;
+ int mask = (1 << image->comps[compno].prec) - 1;
+ 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;
+ 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++;
+ }
+ }
+ }
+ }
+ else if(image->comps[compno].prec <= 16)
+ {
+ if(image->comps[compno].sgnd == 1)
+ {
+ signed short int curr;
+ int mask = (1 << image->comps[compno].prec) - 1;
+ 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 = curr >> 8;
+ fwrite(&temp, 1, 1, rawFile);
+ temp = 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;
+ 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 = curr >> 8;
+ fwrite(&temp, 1, 1, rawFile);
+ temp = 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,"Error: invalid precision: %d\n", image->comps[compno].prec);
+ return 1;
+ }
+ }
+ fclose(rawFile);
+ return 0;
+}