Here's the content of my Tiff IO library (uses libtiff). It probably
won't do exactly what you want, but I'm providing it as a reference.
It would be great if someone took this and ran with it to make a full
featured Tiff IO library...
-Judd
#
# Tiff.pd
#
# PDL interface to the libtiff c library
#
# Judd Taylor, USF IMaRS
# 21 April 2003
#
use strict;
use PDL;
use vars qw( $VERSION );
$VERSION = "1.0";
# needed header files:
pp_addhdr(<<'EOH');
#include "tiffio.h"
#include <stdio.h>
EOH
# Function to write a Tiff image from a piddle variable:
pp_def( 'write_tiff',
Pars => 'img(x,y); byte [o] stat()',
GenericTypes => ['B'],
OtherPars => 'char* filename',
Code => <<'EOCODE' );
char *module = "Judd::PDL::IO::Tiff::write_tiff()";
/* error message buffer */
char emesg[1024];
/* Open the tif file for writing */
TIFF* tiff_file = TIFFOpen( $COMP(filename), "w");
if (tiff_file == NULL)
{
/* Is this how you use TIFFError? the docs suck... */
TIFFError(module, emesg);
}
/* Set the required tiff tags for a greyscale image */
TIFFSetField(tiff_file, TIFFTAG_COPYRIGHT, "USF Institute for Marine
Remote Sensing");
TIFFSetField(tiff_file, TIFFTAG_DOCUMENTNAME, $COMP(filename));
TIFFSetField(tiff_file, TIFFTAG_IMAGEWIDTH, $SIZE(x));
TIFFSetField(tiff_file, TIFFTAG_IMAGELENGTH, $SIZE(y));
TIFFSetField(tiff_file, TIFFTAG_IMAGEDEPTH, 1);
TIFFSetField(tiff_file, TIFFTAG_BITSPERSAMPLE, 8 );
TIFFSetField(tiff_file, TIFFTAG_COMPRESSION, 1);
TIFFSetField(tiff_file, TIFFTAG_RESOLUTIONUNIT, 1);
TIFFSetField(tiff_file, TIFFTAG_XRESOLUTION, $SIZE(x) );
TIFFSetField(tiff_file, TIFFTAG_YRESOLUTION, $SIZE(y) );
TIFFSetField(tiff_file, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tiff_file, TIFFTAG_PHOTOMETRIC, 1);
TIFFSetField(tiff_file, TIFFTAG_PLANARCONFIG, 1);
/* Figure out tilewidth and tilelength */
int tilewidth = $SIZE(x) + (16 - ($SIZE(x) % 16));
int tilelength = $SIZE(y) + (16 - ($SIZE(y) % 16));
fprintf(stderr, "%s Image width : %d\n", module, $SIZE(x));
fprintf(stderr, "%s Image length: %d\n", module, $SIZE(y));
fprintf(stderr, "%s Tile width : %d\n", module, tilewidth);
fprintf(stderr, "%s Tile length : %d\n", module, tilelength);
TIFFSetField(tiff_file, TIFFTAG_TILEDEPTH, 1);
TIFFSetField(tiff_file, TIFFTAG_TILEWIDTH, tilewidth);
TIFFSetField(tiff_file, TIFFTAG_TILELENGTH, tilelength);
/* Write the scanlines into the file */
uint32 status = 0;
/* If the tile is larger than the image, then we have to make a new tile
buffer */
if ( $SIZE(x) == tilewidth && $SIZE(y) == tilelength)
{
fprintf(stderr, "%s Writing pdl data directly to tile...\n",
module);
status = TIFFWriteTile(tiff_file, $P(img), $SIZE(x) - 1, $SIZE(y) -
1, 0, 0);
if ( status == -1 )
{
TIFFError(module, emesg);
$stat() = status;
return;
}
}
else
{
fprintf(stderr, "%s Writing pdl data from temporary tile
buffer...\n", module);
PDL_Byte * tiff_buffer = _TIFFmalloc( tilewidth * tilelength );
/* Copy the original data into the larger buffer one row at a time
*/
int row, col;
for( row = 0; row < $SIZE(y); row++ )
{
for( col = 0; col < $SIZE(x); col++ )
{
((PDL_Byte *)tiff_buffer)[(row * tilewidth) + col] = $img
(x=>col,y=>row);
}
}
/*
fprintf(stderr, "%s: Double checking contents of tiff_buffer...\n",
module);
for( row = 0; row < tilelength; row++ )
{
for( col = 0; col < tilewidth; col++ )
{
int ind = (row * tilewidth) + col;
fprintf(stderr, "%3d ", ((PDL_Byte *)tiff_buffer)[ind]);
}
fprintf(stderr, "\n");
}
*/
status = TIFFWriteTile(tiff_file, tiff_buffer, $SIZE(x) - 1, $SIZE
(y) - 1, 0, 0);
if ( status == -1 )
{
TIFFError(module, emesg);
$stat() = status;
return;
}
_TIFFfree(tiff_buffer);
}
/* Close the file, we're finished */
TIFFClose(tiff_file);
$stat() = 1;
EOCODE
On Thu, 2007-12-13 at 08:14 +1300, Christian Soeller wrote:
> Hi Janos,
>
>
> The currently included Tiff IO code (and all other Pic code) is 8 bit
> only. A better interface would use libtiff for I/O but that is non-
> trivial due to the variety of TIFFs one encounters in the field.
>
>
> I once made a very simplistic 16bit TiFF reader for PDL. Somebody
> sufficiently knowledgeable could write a 16bit writer I suppose.
>
>
> Christian
>
>
>
> On 13/12/2007, at 6:34 AM, János Gonzales wrote:
>
>
> > Hello,
> > This is probably really easy, but I'm a newbie so I'm not sure how
> > to
> > do it. I'm trying to output a 16-bit Tiff, but all I get is an 8
> > bit
> > tiff.
> > Here's my pseudo code:
> >
> >
> >
> >
> > use PDL;
> > use PDL::NiceSlice;
> > use PDL::IO::Pic;
> >
> >
> > $blank_slate = zeroes(1800,900);
> >
> >
> > .......in here i add values to the blank_slate piddle from 0 to
> > about 3000
> >
> >
> > $blank_slate->wpic("test.tif");
> >
> >
> >
> >
> > So, if I look at test.tiff it's 1800x900 pixels, but the max value
> > is
> > 255 and not 3000. If it was a 16-bit tiff it could accomodate this
> > value. I'm running this on Windows XP, and I have the netpbm
> > binaries
> > installed. (I noticed my wpic command calls pnmtotiff )
> >
> >
> > Any ideas?
> >
> >
> > Thanks much,
> > Janos
> >
> >
> > _______________________________________________
> > Perldl mailing list
> > [email protected]
> > http://mailman.jach.hawaii.edu/mailman/listinfo/perldl
>
>
> --
> Christian Soeller PhD Dept. of Physiology +64 9 3737599 x82770
> University of Auckland Auckland, New Zealand fax +64 9 3737499
>
>
>
>
>
> _______________________________________________
> Perldl mailing list
> [email protected]
> http://mailman.jach.hawaii.edu/mailman/listinfo/perldl
--
____________________________
Judd Taylor
Software Engineer
Orbital Systems, Ltd.
3807 Carbon Rd.
Irving, TX 75038-3415
[EMAIL PROTECTED]
(972) 915-3669 x127
_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl