+/* -->> -->> -->> -->>
+
+ 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 = image->comps[0].w * image->comps[0].h ;
+ 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 */
+ buf = _TIFFmalloc(TIFFStripSize(tif));
+ index=0;
+ strip_size=0;
+ strip_size=TIFFStripSize(tif);
+ 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;
+ 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;
+ 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 */
+ buf = _TIFFmalloc(TIFFStripSize(tif));
+ index = 0;
+ strip_size = 0;
+ strip_size = TIFFStripSize(tif);
+ for (strip = 0; strip < TIFFNumberOfStrips(tif); strip++) {
+ unsigned char *dat8;
+ int i;
+ dat8 = buf;
+ if (image->comps[0].prec == 8){
+ for (i=0; i<TIFFStripSize(tif); i+=1) { // 8 bits per pixel
+ int r = 0;
+ r = image->comps[0].data[index];
+ if (image->comps[0].sgnd){
+ r += adjust;
+ }
+ dat8[i+0] = r;
+ index++;
+ }
+ }else if (image->comps[0].prec == 12){
+ for (i = 0; i<TIFFStripSize(tif); i+=3) { // 12 bits per pixel
+ 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 if (image->comps[0].prec == 16){
+ for (i=0; i<TIFFStripSize(tif); i+=2) { // 16 bits per pixel
+ 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{
+ 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{
+ fprintf(stderr,"False 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;
+
+ 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 = 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,"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;
+ /* 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 = 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,"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,"Aborting\n");
+ return NULL;
+ }
+ return image;
+}
+
+/* -->> -->> -->> -->>
+
+ RAW IMAGE FORMAT