AWARE [SYSTEMS] Imaging expertise for the Delphi developer
AWare Systems, Imaging expertise for the Delphi developer, Home TIFF and LibTiff Mailing List Archive

LibTiff Mailing List

TIFF and LibTiff Mailing List Archive
March 2004

Previous Thread
Next Thread

Previous by Thread
Next by Thread

Previous by Date
Next by Date

Contact

The TIFF Mailing List Homepage
This list is run by Frank Warmerdam
Archive maintained by AWare Systems



Valid HTML 4.01!



Thread

2004.03.19 01:57 "TIFF to BMP or similar", by Robert Baer
2004.03.19 04:05 "Re: TIFF to BMP or similar", by Bob Friesenhahn
2004.03.19 10:19 "Re: TIFF to BMP or similar", by Andrey Kiselev

2004.03.19 10:19 "Re: TIFF to BMP or similar", by Andrey Kiselev

On Thu, Mar 18, 2004 at 05:57:00PM -0800, Robert Baer wrote:
>   I am a dummy concerning what might be required to create and compile a
> program that would convert a TIFF file to a BMP file (or equivalent).
>   I am even worse concerning the inclusion of this library into a
> FORTRAN program so that i could read and convert into an uncompressed
> form similar to the data body found in a BMP file (and having available
> the width and length of the image).
>   Is there anything available that would do this, or *simple*
> instructions as how to do this (I have Watcom C/C+)?

BMP format is pretty simple and can be easily coded. Attached simple
program should convert TIFF into BMP. You should be aware of endiannes
issues (BMP header is always in Intel order) and memory constraints.

Of course, so simple program is not suitable for the complex TIFFs such
as multiband, floating point, multipage ones etc. But in usual cases it
should work good.

> Is there anything available that would do this, or *simple*
> instructions as how to do this (I have Watcom C/C+)?

libtiff has makefiles to build it with the MSVC; Watcom compiler has a
MSVC compatibility utilities (nmake and cl), so it should not be a big
problem to build the library using Watcom compiler. Just use nmake, as
with the MSVC.

						Regards,
						Andrey

-- 
Andrey V. Kiselev
Home phone:  +7 812 5274898  ICQ# 26871517


#include <stdio.h>
#include <stdlib.h>
#include "tiffio.h"

int main (int argc, char **argv)
{
    char *infile, *outfile;
    TIFF *tif;
    FILE *bmp;
    uint16 short_val;
    uint32 i, j, image_width, image_height, scansize, long_val;
    u_long *inbuf;
    char *outbuf;

    if(argc < 3)
    {
        fprintf(stderr, "\nUSAGE: tiff2bmp infile.tif outfile.bmp\n");
        return 1;
    }
    else
    {
        infile = argv[1];
        outfile = argv[2];
    }

    tif = TIFFOpen(infile, "r");
    if (!tif)
    {
	fprintf (stderr, "Can't open %s for reading\n", infile);
	return 2;
    }

    bmp = fopen(outfile, "wb");
    if (!bmp)
    {
	fprintf (stderr, "Can't open %s for writing\n", outfile);
        TIFFClose(tif);
	return 3;
    }

    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &image_width);
    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &image_height);
    scansize = ((image_width * 24 + 31) & ~31) / 8;

    inbuf = (u_long *)malloc(image_width * image_height * sizeof(uint32));
    TIFFReadRGBAImage(tif, image_width, image_height, inbuf, 0);

    fwrite("BM", 1, 2, bmp);        /* Signature */
    long_val = 14 + 40 + scansize * image_height;
    fwrite(&long_val, 4, 1, bmp);   /* Size in bytes of the bitmap file */
    short_val = 0;
    fwrite(&short_val, 2, 1, bmp);  /* Reserved, set as 0 */
    fwrite(&short_val, 2, 1, bmp);  /* Reserved, set as 0 */
    long_val = 14 + 40;             /* Offset of the image from file start */
    fwrite(&long_val, 4, 1, bmp);

    long_val = 40;                  /* Size of BMPInfoHeader structure */
    fwrite(&long_val, 4, 1, bmp);
    fwrite(&image_width, 4, 1, bmp);
    fwrite(&image_height, 4, 1, bmp);
    short_val = 1;                  /* Number of image planes */
    fwrite(&short_val, 2, 1, bmp);
    short_val = 24;                 /* Number of bits per pixel */
    fwrite(&short_val, 2, 1, bmp);
    long_val = 0;                   /* Compression method */
    fwrite(&long_val, 4, 1, bmp);
    long_val = 0;                   /* Size of uncomressed image in bytes */
    fwrite(&long_val, 4, 1, bmp);
    long_val = 0;                   /* X resolution, pixels per meter */
    fwrite(&long_val, 4, 1, bmp);
    long_val = 0;                   /* Y resolution, pixels per meter */
    fwrite(&long_val, 4, 1, bmp);
    long_val = 0;                   /* Size of colour table */
    fwrite(&long_val, 4, 1, bmp);
    long_val = 0;                   /* Number of important colours */
    fwrite(&long_val, 4, 1, bmp);

    outbuf = (char *)malloc(scansize);
    memset(outbuf, 0, scansize);
    for (i = 0; i < image_height; i++)
    {
        u_long *buf = inbuf + i * image_width;
        /* BMP scanlines should be aligned at 4-byte boundary */
        for (j = 0; j < image_width; j++)
        {
            outbuf[j * 3] = TIFFGetB(buf[j]);
            outbuf[j * 3 + 1] = TIFFGetG(buf[j]);
            outbuf[j * 3 + 2] = TIFFGetR(buf[j]);
        }
        fwrite(outbuf, 1, scansize, bmp);
    }

    free(inbuf);
    free(outbuf);
    TIFFClose(tif);
    fclose(bmp);

    return 0;
}