OutputRGBTIFFImage,
a quick Save-to-TIFF writer for C++

Sorry, this product is no longer being sold.

last updated March 18, 2008

Overview

Powerful TIFF image interface for C++. A C++ object that does all the work of writing a bitmap to a TIFF file. You copy the pixels into the object; this code does the rest.

Purpose

Caution

:^) Don't try to build one of these at home! Testing it to perfection would cost much more than $5 worth of your time.

System Requirements

Code Example

Suppose we wish to save a square region of an onscreen image to disk as a TIFF file.
We have to do only two things: 1. Create an OutputRGBTIFFImage. 2. Copy all the pixels into it:

{
const int   width  = xmax - xmin, 
            height = ymax - ymin; 
    
OutputRGBTIFFImage  imagefile(filename.c_str(), width, height, "packbits"); 

FOR_EACH_TIFF_ROW(imagefile, y) 
    FOR_EACH_TIFF_COL(imagefile, x) 
        {
        RGBColor    color; 
        ::GetCPixel(x + xmin, y + ymin, &color); // or XGetPixel() 
        imagefile.SetPixel(x, y, color); 
        }
// Here the imagefile goes out of scope, and its destructor 
// writes the TIFF file.  And we're done. 
}
	

Source Code

/* ======================================================================== */
/* OutputRGBTIFFImage.cc                                      jB  Sa 102106 */
/* ======================================================================== */

#include "OutputRGBTIFFImage.h" // to check the public prototypes

        // Standard headers 

#include <ctype.h>          // for isdigit() 
#include <stdlib.h>         // for calloc(), free() 
#include <string.h>         /* for str*() functions */

/* ======================================================================== */
/*                           D E F I N I T I O N S                          */
/* ======================================================================== */

#define streq(a,b)  (strcmp(a,b) == 0)
#define strneq(a,b,n)   (strncmp(a,b,n) == 0)


/* ======================================================================== */
/* OutputRGBTIFFImage()       10-Feb-2005, 21-Oct-2006      TIFF v3.5.5, jB */ 
/* ======================================================================== */

OutputRGBTIFFImage:: 
OutputRGBTIFFImage(
    const char *fname, 
    uint32      wid, 
    uint32      ht, 
    const char *compress_opts ) 
  : filename(fname), 
    width(wid), 
    height(ht), 
    image(NULL), 
    compression(-1) 
{
    // Open the TIFF image 
    if((image = TIFFOpen(fname, "w")) == NULL) 
        throw "OutputRGBTIFFImage: Could not open output file"; 
    
    TIFF *out = image;

    uint16 photometric = PHOTOMETRIC_RGB;
    uint16 config = PLANARCONFIG_CONTIG;
    uint32 rowsperstrip = (uint32) -1;
    
    if (!processCompressOptions(compress_opts))
        throw "OutputRGBTIFFImage: invalid compression option";
    ••• 

In the missing block of code, we call TIFFSetField() to set various TIFF image attributes.

Need the full source code? To receive it (along with the .h file, not shown here), click the payment button below.
Immediately after your payment, the full source code will appear on your screen.
The code is fully tested and you'll have it running in minutes.

Sorry, this product is no longer being sold.

You could write the code yourself, but figuring out what the missing pieces of the code are, and testing them, would cost much more than $5 worth of your time.

Click to pay $5 and instantly receive the code.

This business accepts PayPal for completely paperless, totally electronic service

We accept major credit cards via PayPal

This price increases often and without notice. He who snoozes loses.

    // Allocate space for the image: 
    
    // pixels is a vector of rows -- will be used as if it were a 2-D array: 
    
    pixels = (u_char **) calloc(height, sizeof(u_char *));
    
    slsize = TIFFScanlineSize(image); 
    
    FOR_EACH_TIFF_ROW(*this, y) 
        {
        // To avoid memory fragmentation, do all the allocation before reading 
        // any image data, 
        // just in case of memory leaks in the reader. 
        
        u_char *rp = (u_char *) _TIFFmalloc(slsize); 
        if (rp == NULL) 
            throw "OutputRGBTIFFImage: Not enough memory to read entire image"; 
        pixels[y] = rp; 
        }
    
}


/* ======================================================================== */
/* ~OutputRGBTIFFImage()                10-Feb-2005                      jB */ 
/* ======================================================================== */

OutputRGBTIFFImage:: 
~OutputRGBTIFFImage() 
{
    if (image) 
        {
        write_all_scanlines();
        TIFFClose(image); 
        }
    
    image = NULL; 
    
    if (pixels) 
        {
        FOR_EACH_TIFF_ROW(*this, y) 
	

The missing deallocation code is provided to paying customers only.
Correct deallocation prevents memory corruption.

        
        free(pixels); 
        pixels = NULL; 
        }
    
    _TIFFfree(scanlinebuf); 
}


/* ======================================================================== */
/* write_all_scanlines()                10-Feb-2005                      jB */
/* ======================================================================== */

void
OutputRGBTIFFImage:: 
write_all_scanlines()
{
    FOR_EACH_TIFF_ROW(*this, row) 
        WriteScanline(row); 
}


/* ======================================================================== */
/* ======================================================================== */

	

Pricing

OutputRGBTIFFImage is provided in one edition:


Availability

As of October 24, 2006, OutputRGBTIFFImage source code (Gold Edition) is available.




Concepts:

TIFFTAG_IMAGEWIDTH, TIFFTAG_IMAGELENGTH, TIFFTAG_ORIENTATION, TIFFTAG_SAMPLESPERPIXEL, TIFFTAG_BITSPERSAMPLE, TIFFTAG_PLANARCONFIG, TIFFTAG_JPEGCOLORMODE, TIFFTAG_JPEGQUALITY, COMPRESSION_LZW, COMPRESSION_DEFLATE, TIFFTAG_PHOTOMETRIC, TIFFTAG_COMPRESSION, TIFFTAG_ROWSPERSTRIP, tif file, tif files, tiff files, bitmap, Pixmap, XImage


Contact

Copyright © 2006-2007  J. E. Brown   all rights reserved.
write me here
              
Los Alamos, NM  USA