0

I'm trying to convert RGB888 image data to TIFF image. But code generates improper image. I'm reading the RGB data from text file.Here is the output image

Attached the improper image the black region shouldn't be there it seems that code is making low RGB values to zero. I'm creating tiff image without alpha channel.Please help me comprehend the issue.

TIFF *out= TIFFOpen("new.tif", "w");

int sampleperpixel = 3;
uint32 width=320;
uint32 height=240;
unsigned char image[width*height*sampleperpixel];
int pixval;
int count=0;


FILE *ptr=fopen("data.txt","r");
if (ptr!=NULL)
    {
      while(count<width*height)
        {fscanf(ptr,"%d",&pixval);            
        *(image+count)=(unsigned char)pixval;
        count++;}
    }
printf("%d\n",count);

TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);  // set the width of the image
TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);    // set the height of the image
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, sampleperpixel);   // set number of channels per pixel
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);    // set the size of the channels
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);    // set the origin of the image.
//   Some other essential fields to set that you do not have to understand for now.
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);

tsize_t linebytes = sampleperpixel * width;
unsigned char *buf = NULL; 
//if (TIFFScanlineSize(out)<linebytes)
//  buf =(unsigned char *)_TIFFmalloc(linebytes);
//else
    buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(out));




TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, width*sampleperpixel));

uint32 row ;
//Now writing image to the file one strip at a time
for (row = 0; row < height; row++)
{
    //memcpy(buf, &image[(height-row-1)*linebytes], linebytes);    // check the index here, and figure out why not using h*linebytes
    memcpy(buf, &image[(row)*linebytes], linebytes);        
if (TIFFWriteScanline(out, buf, row, 0) < 0)
    break;
}


TIFFClose(out);

if (buf)
 _TIFFfree(buf);
vid09
  • 149
  • 1
  • 3
  • 8
  • You should try to progress one step at a time. First, what happens if you try first to fill all scanlines with harcoded values, instead of reading the data from your text file ? (for example generating an completely red tiff) – Jem May 04 '13 at 19:14
  • Your TIFFTAG_ROWSPERSTRIP is wrong. It needs be set to either HEIGHT or a fractional division of HEIGHT. The rest looks ok. Post the actual file and I can tell you more precisely where it's going wrong. – BitBank May 05 '13 at 14:38
  • @BitBank Could be you please be more specific about the files as this is the complete code. Apart from this there is only data file. Thanks – vid09 May 05 '13 at 16:04
  • Post the TIFF file so I can inspect it, not a PNG image of the TIFF file. – BitBank May 05 '13 at 16:19
  • https://docs.google.com/file/d/0B4CW5GKV8jUueHlrenFlaVZsSk0/edit?usp=sharing ---- this the link , i have uploaded on goolge drive – vid09 May 05 '13 at 16:47
  • Thanks for posting the image. The improper ROWSPERSTRIP value is ignored by most viewers (including mine). The real problem is that you only generated 1/3 of the necessary data. Somewhere upstream you're not copying or generating the full data size. Maybe you're doing a memcpy(dest, src, width*height) instead of width*height*3. – BitBank May 06 '13 at 01:47

1 Answers1

0

I found your example at this tutorial. Since you are not the original author, you should always post a link to help others and avoid plagiarism.

The example works great except the part you added. To read file into byte array, use this instead (after you read what fread does):

fread(image, 1, sizeof image, ptr);

in your original code, you omitted sampleperpixel

while(count<width*height*sampleperpixel) ...

so you read only one third of the image.

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169