1

I am writing an application to save multi-page tiff saving with big tiff library and I have two problems with the big tiff library:

  1. So far I am able to save some images in a stack, but with the image viewer I can only see 3 or 4 pages even though the file size for the image stack is much larger than 4 images; I expect to see more images. I followed the example here.

    //open
    TIFFOpen(destPtr, "w8");
    //set
    TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
    TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
    TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bitsPerSample);
    TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
    TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_NONE); //either one works
    TIFFSetField(out, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
    TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, 8);
    TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
    TIFFSetField(out, TIFFTAG_MAXSAMPLEVALUE, maxSampleValue);
    
    //loop
    for (int i =0; i<=10; i++)
    {
        // stack
        TIFFSetField(out, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
        // set the page number
        TIFFSetField(out, TIFFTAG_PAGENUMBER, i, 10);
    
        int width = 5056;
        int height = 2960;
    
        tdata_t l_buffer;
        unsigned long l_buffer_size = width * 2;
        l_buffer = _TIFFmalloc(l_buffer_size);
        unsigned short *da = (unsigned short*)data;
    
        for (int row = 0; row < height; row++)
        {
            memcpy(l_buffer, &da[row*width], l_buffer_size);
    
    
            int ret = TIFFWriteScanline(out, l_buffer, row, 0);
            if (ret == -1)
            {
                TIFFClose(out);
            }
        }
        _TIFFfree(l_buffer);
    }
    
    //close
    (void)TIFFClose(out);
    
  2. If I use TIFFWriteRawStrip() then there is offset in the image and I can't view all the images even though the stack seems to contain all the images. With the regular tiff library this code worked just fine.

    //open
    TIFFOpen(destPtr, "w8");
    //set
    TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);
    TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
    TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bitsPerSample);
    TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
    TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_NONE); //either one works
    TIFFSetField(out, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
    
    TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
    TIFFSetField(out, TIFFTAG_MAXSAMPLEVALUE, maxSampleValue);
    
    //loop
    for (int i =0; i<=10; i++)
    {
        // stack
        TIFFSetField(out, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
        // set the page number
        TIFFSetField(out, TIFFTAG_PAGENUMBER, i, 10);
    
        TIFFWriteRawStrip((TIFF*)out, (tstrip_t)0, (tdata_t)data, (tsize_t)length);
    }
    
    //close
    (void)TIFFClose(out);
    

enter image description here

I figured out. The following code will do the job.

tif = TIFFOpen(destPtr, "w");

int npages = 10;


for (int i = 0; i < npages; i++)
{
    TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
    TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
    TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
    TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
    TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
    TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 8);

    TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
    TIFFSetField(tif, TIFFTAG_PAGENUMBER, i, npages);

    // not sure if this is needed...
    TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    TIFFSetField(tif, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
    TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);


    //TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_CENTIMETER);
    //TIFFSetField(tif, TIFFTAG_XRESOLUTION, pixels_per_cm);
    //TIFFSetField(tif, TIFFTAG_YRESOLUTION, pixels_per_cm);


    //tdata_t       l_buffer;
    //unsigned long l_buffer_size = width * 2; // LINE buffer for 16bit
    //l_buffer = _TIFFmalloc(l_buffer_size);

    char*dat = (char*)data;

    for (int row = 0; row < height; row++) {
        //memcpy(l_buffer, &data[row*width], l_buffer_size);

        int ret = TIFFWriteScanline(tif, dat + row * width * 2, row, 0);
        if (ret == -1) {
            TIFFClose(tif);
            return -1;
        }
    }
    TIFFWriteDirectory(tif);
}
//_TIFFfree(l_buffer);
TIFFClose(tif);

takanoha
  • 183
  • 4
  • 17

0 Answers0