Return-Path: <tiff-bounces@remotesensing.org>
Received: from inmx016.isp.belgacom.be (inmx016.isp.belgacom.be [195.238.2.219])
        by mailst034.isp.belgacom.be (8.12.11/8.12.11/Skynet-MAILSTORE-2.14) with ESMTP id i998AMMA021616
        for <joris.at.lebbeke@skynet.be>; Sat, 9 Oct 2004 10:10:22 +0200
        (envelope-from <tiff-bounces@remotesensing.org>)
Received: from xserve.flids.com (adsl-065-083-169-202.sip.mco.bellsouth.net [65.83.169.202])
        by inmx016.isp.belgacom.be (8.12.11/8.12.11/Skynet-IN-2.32) with ESMTP id i998A7Wx003563
        for <joris.at.lebbeke@skynet.be>; Sat, 9 Oct 2004 10:10:12 +0200
        (envelope-from <tiff-bounces@remotesensing.org>)
Received: from adsl-065-083-169-202.sip.mco.bellsouth.net (localhost [127.0.0.1])
	by xserve.flids.com (Postfix) with ESMTP
	id 2CD173945C6; Sat,  9 Oct 2004 04:09:31 -0400 (EDT)
X-Original-To: tiff@remotesensing.org
Delivered-To: tiff@remotesensing.org
Received: from ahk.pu.ru (ahk.pu.ru [193.124.85.198])
	by xserve.flids.com (Postfix) with ESMTP id 1A4AD3945B6
	for <tiff@remotesensing.org>; Sat,  9 Oct 2004 04:09:29 -0400 (EDT)
Received: from wg.pu.ru (wg.pu.ru [193.124.85.219])
	by ahk.pu.ru (8.12.11/8.12.9) with ESMTP id i9989QXm024680
	for <tiff@remotesensing.org>; Sat, 9 Oct 2004 12:09:27 +0400 (MSD)
	(envelope-from dron@ak4719.spb.edu)
Received: (from uucp@localhost)
	by wg.pu.ru (8.9.1a/8.9.1) with UUCP id IAA11381
	for tiff@remotesensing.org; Sat, 9 Oct 2004 08:09:26 GMT
Received: from ak4719.spb.edu (localhost [127.0.0.1])
	by ak4719.spb.edu (8.12.11/8.12.11/Debian-5) with ESMTP id
	i9980gkc012021
	for <tiff@remotesensing.org>; Sat, 9 Oct 2004 12:00:42 +0400
Received: (from dron@localhost)
	by ak4719.spb.edu (8.12.11/8.12.11/Debian-5) id i9980g92012018
	for tiff@remotesensing.org; Sat, 9 Oct 2004 12:00:42 +0400
From: Andrey Kiselev <dron@ak4719.spb.edu>
Date: Sat, 9 Oct 2004 12:00:42 +0400
To: tiff@remotesensing.org
Subject: Re: [Tiff] Converting DIB to Tif using libtiff
Message-ID: <20041009080042.GB9868@ak4719.spb.edu>
Mail-Followup-To: tiff@remotesensing.org
References: <20041006150525.14023.qmail@web90105.mail.scd.yahoo.com>
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="pWyiEgJYm5f9v55/"
Content-Disposition: inline
In-Reply-To: <20041006150525.14023.qmail@web90105.mail.scd.yahoo.com>
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.6+20040722i
X-Virus-Scanned: clamd / ClamAV version 0.75-1, clamav-milter version 0.75c
	on ak4719
X-Virus-Status: Clean
X-BeenThere: tiff@remotesensing.org
X-Mailman-Version: 2.1.2
Precedence: list
List-Id: TIFF and libtiff discussion  <tiff.remotesensing.org>
List-Unsubscribe: <http://xserve.flids.com/mailman/listinfo/tiff>,
	<mailto:tiff-request@remotesensing.org?subject=unsubscribe>
List-Archive: <http://xserve.flids.com/pipermail/tiff>
List-Post: <mailto:tiff@remotesensing.org>
List-Help: <mailto:tiff-request@remotesensing.org?subject=help>
List-Subscribe: <http://xserve.flids.com/mailman/listinfo/tiff>,
	<mailto:tiff-request@remotesensing.org?subject=subscribe>
Sender: tiff-bounces@remotesensing.org
Errors-To: tiff-bounces@remotesensing.org
X-UIDL: c048ad3d6d2ba46214b125ea0caeee31


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

On Wed, Oct 06, 2004 at 08:05:25AM -0700, Lib Tiff wrote:
> I was wondering if anyone knows of a general routine that will convert
> generic Windows DIB (given handle, etc) to a corresponding tiff file?

You can found the sample code attached. It was written for specific
purpose to save the monocrome and grayscale images only, but it should
give you a basic idea how to do the conversion.

						Best regards,
						Andrey

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

--pWyiEgJYm5f9v55/
Content-Type: text/x-csrc; charset=us-ascii
Content-Disposition: attachment; filename="bmp2tiff.c"

/******************************************************************************
 * Project:  ???
 * Purpose:  Routine to save Windows Device Dependent Bitmaps (DDB)
 *           as TIFF files.
 * Author:   Andrey Kiselev, dron@at1895.spb.edu
 *
 ******************************************************************************
 * Copyright (c) 2002, Andrey Kiselev <dron@at1895.spb.edu>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ******************************************************************************
 */

#include <stdio.h>
#include <string.h>
#include <tiffio.h>

/* Hack to test function without Windows development environment */

#ifndef __WIN32__

#define	BYTE	unsigned char
typedef struct tagBITMAP
{
    int	    bmType;
    int	    bmWidth;
    int	    bmHeight;
    int	    bmWidthBytes;
    BYTE    bmPlanes;
    BYTE    bmBitsPixel;
    void    *bmBits;
} BITMAP;

#else

#include <windows.h>

#endif

/******************************************************************************
 *                               SaveAsTIFF()                                 *
 ******************************************************************************	
 * SYNOPSIS:
 * 
 * psBmp	pointer to input BITMAP structure
 * pszFilename	pointer to string which containes path to output TIFF file
 * 
 * Only 1-bit (monochrome) or 8-bit (grayscale) images supported.
 * pszFilename file will be overwritten, caller responsible to check
 * if it is not desirable.
 * 
 * RETURN VALUE:
 * The number of actually written scanlines returned or -1 if function fails.
 ****************************************************************************** */

int SaveAsTIFF(BITMAP *psBmp, char *pszFilename)
{
    uint32  iWidth, iHeight;
    float   fXRes, fYRes;
    uint16  iSPP, iBPP, iResUnit, iRowsPerStrip;
    int	    iRow;
    TIFF    *fpOut;
    BYTE    *pbBuf;
    
    if (psBmp->bmWidth <= 0 || psBmp->bmHeight <= 0   ||
	(psBmp->bmBitsPixel != 1 && psBmp->bmBitsPixel != 8) ||
	psBmp->bmPlanes < 1 || psBmp->bmBits == NULL)
    {
	fprintf (stderr, "Input image has wrong dimesions.\n");
	return -1;
    }
    
    fpOut = TIFFOpen(pszFilename, "w");
    if (!fpOut)
    {
	fprintf (stderr, "Can't open %s for writing.\n", pszFilename);
	return -1;
    }

    iWidth = psBmp->bmWidth;
    iHeight = psBmp->bmHeight;
    iBPP = psBmp->bmBitsPixel;
    if (psBmp->bmPlanes == 1)
	iSPP = 1;
    else
	iSPP = 8;

    TIFFSetField(fpOut, TIFFTAG_IMAGEWIDTH, iWidth);
    TIFFSetField(fpOut, TIFFTAG_BITSPERSAMPLE, iBPP);
    TIFFSetField(fpOut, TIFFTAG_SAMPLESPERPIXEL, iSPP);
    TIFFSetField(fpOut, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
    TIFFSetField(fpOut, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    TIFFSetField(fpOut, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
    
    /* It is good to set resolutions too */
    fXRes = fYRes = 100;	    /* dots per resolution unit (see below) */
    iResUnit = RESUNIT_INCH;	    /* or RESUNIT_CENTIMETER */
    TIFFSetField(fpOut, TIFFTAG_XRESOLUTION, fXRes);
    TIFFSetField(fpOut, TIFFTAG_YRESOLUTION, fYRes);
    TIFFSetField(fpOut, TIFFTAG_RESOLUTIONUNIT, iResUnit);

    /* Uncomment the next line if you need compression.
     * Possible options are:
     * 
     * COMPRESSION_CCITTRLE        CCITT modified Huffman RLE 
     * COMPRESSION_CCITTFAX3       CCITT Group 3 fax encoding
     * COMPRESSION_CCITTFAX4       CCITT Group 4 fax encoding
     * COMPRESSION_PACKBITS        Macintosh RLE
     *
     * You may try all of these with your data to choose
     * which one produces best results.
     */
    /* TIFFSetField(fpOut, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); */
    
    iRowsPerStrip = TIFFDefaultStripSize(fpOut, 0);
    TIFFSetField(fpOut,TIFFTAG_ROWSPERSTRIP, iRowsPerStrip);

    pbBuf = (BYTE *)psBmp->bmBits;
    for (iRow = 0; iRow < iHeight; iRow++)
    {
	if (TIFFWriteScanline(fpOut, pbBuf, iRow, 0) < 0)
	{
	    fprintf(stderr, "Can't write scanline %d to file %s.\n",
		    iRow, pszFilename);
	    break;
	}
	pbBuf += psBmp->bmWidthBytes;
    }
    
    TIFFClose(fpOut);
    
    return iRow; /* Number of successfuly written scanlines */
}

/* Below is the testsuite for the SavaAsTIFF() function */

BYTE bTest[] = 
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

BITMAP sTest[]=
{
    {
	0, 64, 18, 8, 1, 1, &bTest
    },
    {
	0, 8, 18, 8, 1, 8, &bTest
    },
    {
	0, 0, 0, 0, 0, 0, NULL
    }
};

int main()
{
    int	    i;
    char    szName[1024];

    i = 0;
    while (sTest[i].bmBits)
    {
	sprintf(szName, "out%d.tif", i);
	if (SaveAsTIFF(&sTest[i], szName) < 0)
	    fprintf(stderr, "Test %d failed!\n", i);
	else
	    fprintf(stderr, "Test %d succeeded!\n", i);
	i++;
    }
    
    return 0;
}


--pWyiEgJYm5f9v55/
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Tiff mailing list
Tiff@remotesensing.org
http://xserve.flids.com/mailman/listinfo/tiff

--pWyiEgJYm5f9v55/--
