2009.12.07 16:06 "[Tiff] Problems with multiple strip G4 to single strip G4 tiff.", by Phillip Wiles

2010.01.20 01:30 "Re: [Tiff] 2010.01.11 09:47 "Re: me again...", by Gerben Vos", by Kavallieratou Ergina

Thanks to your advices, I manage to build the program below. However, it works only with one kind of tiff and not any e.g from paint or matlab. What is wrong? My files are always black and white.

thank you again
Gin

#include <stddef.h>
#include <tiffio.h>
using namespace std;
typedef unsigned char BYTE;

struct gen

{

BYTE** data;

unsigned short int width;

unsigned short int height;

unsigned short int bits_per_pixel;

};

struct eidiki
{
 int **data;
 int width;
 int height;
};

int main(int argc, char *argv[])
{
 int i, j, y;
 gen *imagen=(gen*)malloc(sizeof(gen));
 gen *im=(gen*)malloc(sizeof(gen));
 eidiki *imagen1=(eidiki*)malloc(sizeof(eidiki));
 char *strin;
 strin=(char*)malloc(100*sizeof(char));
 char *strout0;
 strout0=(char*)malloc(100*sizeof(char));
 strcpy(strin, argv[1]);
 strcpy(strout0,"C:\\bethlehem\\samples\\rmvRulerLines.tif\0");
//****************************************************************************************
 // Reading Tif Image
 TIFF *in = TIFFOpen(strin, "r");
 cout<<"Ok";
 if (in) {
  int w=0, h=0;
  uint16 photo, bpp, spp, fillorder, rows_per_strip, compression, planar, orientation, res_unit;
  uint32 image_offset = 0;
  float xres, yres;

//info reading
TIFFGetField(in,TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(in,TIFFTAG_IMAGELENGTH, &h);
TIFFGetField(in, TIFFTAG_PHOTOMETRIC, &photo);
TIFFGetField(in, TIFFTAG_BITSPERSAMPLE, &bpp);
TIFFGetField(in, TIFFTAG_SAMPLESPERPIXEL, &spp);
TIFFGetField(in, TIFFTAG_FILLORDER, &fillorder);

TIFFGetField(in, TIFFTAG_COMPRESSION, &compression);

TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &rows_per_strip);

TIFFGetField(in, TIFFTAG_STRIPOFFSETS, &image_offset);

TIFFGetField(in, TIFFTAG_PLANARCONFIG, &planar);

TIFFGetField(in, TIFFTAG_ORIENTATION, &orientation);

TIFFGetField(in, TIFFTAG_RESOLUTIONUNIT, &res_unit);

TIFFGetField(in, TIFFTAG_XRESOLUTION, &xres);
TIFFGetField(in, TIFFTAG_YRESOLUTION, &yres);
size_t npixels=w*h;

  uint32 * raster=(uint32 *) _TIFFmalloc(npixels *sizeof(uint32));

//image reading
if (raster != NULL) {
 if (TIFFReadRGBAImage(in, w, h, raster, 0)) {
  imagen1->height=h;
  imagen1->width=w;
  imagen1->data=(int**)malloc(sizeof(int*)*h);
  for(i=0;i<h;i++)
   imagen1->data[i]=(int*)malloc(sizeof(int)*w);
  for(i=0;i<h;i++)
   for(j=0;j<w;j++)
    imagen1->data[h-1-i][j]=1-raster[i*w+j]; //1- in order to be appropriate for my functions

   txt_to_bmp(imagen,imagen1);
  im->height=imagen->height;
  im->width=imagen->width;
  im->data=(BYTE**)malloc(sizeof(BYTE*)*im->height);
  for(i=0;i<im->height;i++){
   im->data[i]=(BYTE*)malloc(sizeof(BYTE)*im->width);
   for(j=0;j<im->width;j++){
    if (imagen->data[i][j]==2)
     im->data[i][j]=0;
    else
     im->data[i][j]=1;
   }
  }

 //my functions...

  for(i=0;i<im->height;i++)
   for(j=0;j<im->width;j++){
    if (im->data[i][j]==0)
     imagen->data[i][j]=2;
    else
     imagen->data[i][j]=1;
   }

  //writing tiff
  TIFF *out=TIFFOpen(strout0,"w");
  int width=w;int height=h;int sampleperpixel=1; //RGBA=4 bw/gray=1

    TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
    TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
    TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photo);
    TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bpp);
    TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
    //TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);
    TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
    TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rows_per_strip);
    TIFFSetField(out, TIFFTAG_PLANARCONFIG, planar);
    TIFFSetField(out, TIFFTAG_ORIENTATION, orientation);
    TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, res_unit);
    TIFFSetField(out, TIFFTAG_XRESOLUTION, xres);
    TIFFSetField(out, TIFFTAG_YRESOLUTION, yres);

  for(i=0;i<imagen->height;i++)
   for(j=0;j<imagen->width;j++){
    imagen->data[i][j]=(BYTE)1-imagen->data[i][j]; //1- gia swsto swsimo
    }
  BYTE *bits;
  bits=new BYTE [imagen->width];
  for ( y = 0; y < imagen->height; y++)

{
bits= imagen->data[y];

     if (TIFFWriteScanline(out,bits, y, 0)==-1) return 0;
    }
    TIFFClose(out);
   }
   _TIFFfree(raster);
  }
  TIFFClose(in);
 }
return 1;
}