Return-Path: <tiff-admin@remotesensing.org>
Received: from inav010.isp.belgacom.be (inav010.isp.belgacom.be [195.238.3.53])
        by six.skynet.be (8.12.9/8.12.9/Skynet-MAILSTORE-2.14) with ESMTP id i2JAgI4N025241
        for <joris.at.lebbeke@skynet.be>; Fri, 19 Mar 2004 11:42:18 +0100
        (envelope-from <tiff-admin@remotesensing.org>)
Received: from inmx009.isp.belgacom.be (inmx009.isp.belgacom.be [195.238.3.146])
        by inav010.isp.belgacom.be (8.12.9/8.12.9/Skynet-IN-AV-2.02) with ESMTP id i2JAflKT024120
        for <joris.at.lebbeke@skynet.be>; Fri, 19 Mar 2004 11:41:49 +0100
        (envelope-from <tiff-admin@remotesensing.org>)
Received: from yoda.imagelinks.com ([208.24.120.42])
        by inmx009.isp.belgacom.be (8.12.9/8.12.9/Skynet-IN-2.32) with ESMTP id i2JAfhhh007081
        for <joris.at.lebbeke@skynet.be>; Fri, 19 Mar 2004 11:41:43 +0100
        (envelope-from <tiff-admin@remotesensing.org>)
Received: from yoda.imagelinks.com (localhost.localdomain [127.0.0.1])
	by yoda.imagelinks.com (8.11.6/8.11.2) with ESMTP id i2JAYRl15478;
	Fri, 19 Mar 2004 05:34:28 -0500
Received: from ahk.pu.ru (ahk.pu.ru [193.124.85.198])
	by yoda.imagelinks.com (8.11.6/8.11.2) with ESMTP id i2JAUvl15432
	for <tiff@remotesensing.org>; Fri, 19 Mar 2004 05:30:57 -0500
Received: from wg.pu.ru (wg.pu.ru [193.124.85.219])
	by ahk.pu.ru (8.12.9p2/8.12.9) with ESMTP id i2JAUtqv038292
	for <tiff@remotesensing.org>; Fri, 19 Mar 2004 13:30:55 +0300 (MSK)
	(envelope-from dron@ak4719.spb.edu)
Received: (from uucp@localhost)
	by wg.pu.ru (8.9.1a/8.9.1) with UUCP id KAA15571
	for tiff@remotesensing.org; Fri, 19 Mar 2004 10:30:55 GMT
Received: from ak4719.spb.edu (localhost [127.0.0.1])
	by ak4719.spb.edu (8.12.1/8.12.1/Debian -5) with ESMTP id i2JAJfWP028170
	for <tiff@remotesensing.org>; Fri, 19 Mar 2004 13:19:41 +0300
Received: (from dron@localhost)
	by ak4719.spb.edu (8.12.1/8.12.1/Debian -5) id i2JAJfVt028167
	for tiff@remotesensing.org; Fri, 19 Mar 2004 13:19:41 +0300
From: Andrey Kiselev <dron@ak4719.spb.edu>
To: tiff@remotesensing.org
Subject: Re: [Tiff] TIFF to BMP or similar
Message-ID: <20040319101941.GA26861@ak4719.spb.edu>
Mail-Followup-To: tiff@remotesensing.org
References: <405A536C.D69F901B@earthlink.net>
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="HcAYCG3uE/tztfnV"
Content-Disposition: inline
In-Reply-To: <405A536C.D69F901B@earthlink.net>
X-GnuPG-Key: http://www.srces.spb.org/dron/export.home.key
X-Face: "t+se9s~+y~[XUH]L|pmeO3Pt/Q@DNv**S,d@_1-C=}/,c%<3Qp]eu!ok<"2lYJm$!9qVcg
 F?7o*L;|$`6|Q|>Rt-cU%2_y@6tQ|`!Li}TsvZvEKx;>fuY=fc(=sX^`7Wmw8%SsR`AvM$a;0U/eN\
 P?B\FTyqD,0np$U2!*'z_4D,VPlFf?bN0&zYf"zF0s!7Tl7?$we)xD0@lL>[h|8bhys0i.g|zLnu@C
 Ti@d]i=[!:j1CB\5/TF[m]`lfHfaqSN[0qn@T3zeosV/[LAss{@p_?],x*j8P_9t[}uHD^X-;j~\M!
 gyTly;fj`}&Y~i{<#bN.#QmkE}#T`dxP.I1\.)_MEN{@#W
User-Agent: Mutt/1.5.3i
Sender: tiff-admin@remotesensing.org
Errors-To: tiff-admin@remotesensing.org
X-BeenThere: tiff@remotesensing.org
X-Mailman-Version: 2.0.11
Precedence: bulk
List-Help: <mailto:tiff-request@remotesensing.org?subject=help>
List-Post: <mailto:tiff@remotesensing.org>
List-Subscribe: <http://remotesensing.org/mailman/listinfo/tiff>,
	<mailto:tiff-request@remotesensing.org?subject=subscribe>
List-Id: TIFF and libtiff discussion <tiff.remotesensing.org>
List-Unsubscribe: <http://remotesensing.org/mailman/listinfo/tiff>,
	<mailto:tiff-request@remotesensing.org?subject=unsubscribe>
List-Archive: <http://remotesensing.org/pipermail/tiff/>
Date: Fri, 19 Mar 2004 13:19:41 +0300
X-RAVMilter-Version: 8.4.3(snapshot 20030212) (inav010.isp.belgacom.be)
X-UIDL: c7b9b72192d391635f88e0595d8eb864


--HcAYCG3uE/tztfnV
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

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

--HcAYCG3uE/tztfnV
Content-Type: text/x-csrc; charset=us-ascii
Content-Disposition: attachment; filename="tiff2bmp.c"

#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;
}


--HcAYCG3uE/tztfnV--
_______________________________________________
Tiff mailing list
Tiff@remotesensing.org
http://remotesensing.org/mailman/listinfo/tiff
