+PNM IMAGE FORMAT
+
+<<-- <<-- <<-- <<-- */
+
+opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) {
+ 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[3]; /* maximum of 3 components */
+ opj_image_t * image = NULL;
+ char value;
+
+ f = fopen(filename, "rb");
+ if (!f) {
+ fprintf(stderr, "Failed to open %s for reading !!\n", filename);
+ return 0;
+ }
+
+ if (fgetc(f) != 'P')
+ return 0;
+ value = fgetc(f);
+
+ switch(value) {
+ case '2': /* greyscale image type */
+ case '5':
+ numcomps = 1;
+ color_space = CLRSPC_GRAY;
+ break;
+
+ case '3': /* RGB image type */
+ case '6':
+ numcomps = 3;
+ color_space = CLRSPC_SRGB;
+ break;
+
+ default:
+ fclose(f);
+ return NULL;
+ }
+
+ fgetc(f);
+
+ /* skip comments */
+ while(fgetc(f) == '#') while(fgetc(f) != '\n');
+
+ fseek(f, -1, SEEK_CUR);
+ fscanf(f, "%d %d\n255", &w, &h);
+ fgetc(f); /* <cr><lf> */
+
+ /* initialize image components */
+ memset(&cmptparm[0], 0, 3 * sizeof(opj_image_cmptparm_t));
+ for(i = 0; i < numcomps; i++) {
+ cmptparm[i].prec = 8;
+ cmptparm[i].bpp = 8;
+ cmptparm[i].sgnd = 0;
+ 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;
+
+ /* set image data */
+
+ if ((value == '2') || (value == '3')) { /* ASCII */
+ for (i = 0; i < w * h; i++) {
+ for(compno = 0; compno < numcomps; compno++) {
+ unsigned int index = 0;
+ fscanf(f, "%u", &index);
+ /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
+ image->comps[compno].data[i] = index;
+ }
+ }
+ } else if ((value == '5') || (value == '6')) { /* BINARY */
+ for (i = 0; i < w * h; i++) {
+ for(compno = 0; compno < numcomps; compno++) {
+ unsigned char index = 0;
+ fread(&index, 1, 1, f);
+ /* compno : 0 = GREY, (0, 1, 2) = (R, G, B) */
+ image->comps[compno].data[i] = index;
+ }
+ }
+ }
+
+ fclose(f);
+
+ return image;
+}
+
+int imagetopnm(opj_image_t * image, const char *outfile) {
+ int w, wr, h, hr, max;
+ int i, compno;
+ int adjustR, adjustG, adjustB, adjustX;
+ FILE *fdest = NULL;
+ char S2;
+ const char *tmp = outfile;
+
+ while (*tmp) {
+ tmp++;
+ }
+ tmp--;
+ tmp--;
+ S2 = *tmp;
+
+ 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
+ && S2 !='g' && S2 !='G') {
+
+ fdest = fopen(outfile, "wb");
+ if (!fdest) {
+ fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
+ return 1;
+ }
+
+ w = int_ceildiv(image->x1 - image->x0, image->comps[0].dx);
+ wr = image->comps[0].w;
+
+ h = int_ceildiv(image->y1 - image->y0, image->comps[0].dy);
+ hr = image->comps[0].h;
+
+ 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", wr, hr, max);
+
+ if (image->comps[0].prec > 8) {
+ adjustR = image->comps[0].prec - 8;
+ printf("PNM CONVERSION: Truncating component 0 from %d bits to 8 bits\n", image->comps[0].prec);
+ }
+ else
+ adjustR = 0;
+ if (image->comps[1].prec > 8) {
+ adjustG = image->comps[1].prec - 8;
+ printf("PNM CONVERSION: Truncating component 1 from %d bits to 8 bits\n", image->comps[1].prec);
+ }
+ else
+ adjustG = 0;
+ if (image->comps[2].prec > 8) {
+ adjustB = image->comps[2].prec - 8;
+ printf("PNM CONVERSION: Truncating component 2 from %d bits to 8 bits\n", image->comps[2].prec);
+ }
+ else
+ adjustB = 0;
+
+
+ for (i = 0; i < wr * hr; i++) {
+ int r, g, b;
+ unsigned char rc,gc,bc;
+ 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];
+ 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];
+ 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", rc, gc, bc);
+ }
+ fclose(fdest);
+
+ } else {
+ int ncomp=(S2=='g' || S2=='G')?1:image->numcomps;
+ if (image->numcomps > ncomp) {
+ fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
+ fprintf(stderr," is written to the file\n");
+ }
+ for (compno = 0; compno < ncomp; compno++) {
+ char name[256];
+ if (ncomp > 1) {
+ sprintf(name, "%d.%s", compno, outfile);
+ } else {
+ sprintf(name, "%s", outfile);
+ }
+
+ fdest = fopen(name, "wb");
+ if (!fdest) {
+ fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
+ return 1;
+ }
+
+ w = int_ceildiv(image->x1 - image->x0, image->comps[compno].dx);
+ wr = image->comps[compno].w;
+
+ h = int_ceildiv(image->y1 - image->y0, image->comps[compno].dy);
+ hr = image->comps[compno].h;
+
+ 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", wr, hr, max);
+
+ if (image->comps[compno].prec > 8) {
+ adjustX = image->comps[0].prec - 8;
+ printf("PNM CONVERSION: Truncating component %d from %d bits to 8 bits\n",compno, image->comps[compno].prec);
+ }
+ else
+ adjustX = 0;
+
+ for (i = 0; i < wr * hr; i++) {
+ int l;
+ unsigned char lc;
+ 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);
+ }
+ fclose(fdest);
+ }
+ }
+
+ return 0;
+}
+
+/* -->> -->> -->> -->>
+
+ 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 = 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 = 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 = 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 = 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