| AWARE [SYSTEMS] | Imaging expertise for the Delphi developer | |||||||
![]() |
TIFF and LibTiff Mailing List Archive | |||||||
LibTiff Mailing List
TIFF and LibTiff Mailing List Archive Contact
The TIFF Mailing List Homepage |
Thread2008.11.26 10:26 "Re: Help write 16bites grey level image", by Reinhard Mayr Aka CzerwinskiCristian,
I can offer you a method that worked for me. I used a .NET wrapper, but the
calls are exactly the same as in C.
hth & cheers,
Reinhard.
------
private void tbCreateTIFF_Click(object sender, System.EventArgs e)
{
if (sfdCreateTIFF.ShowDialog() != DialogResult.OK) return;
DateTime startTime = DateTime.Now;
// image size
int width = 1024*Convert.ToInt16(tbSizeFactorX.Text);
int height = 1024*Convert.ToInt16(tbSizeFactorY.Text);
short rowsPerStrip = (short)height;
// create file
IntPtr newTiff = LibTIFF.Open(sfdCreateTIFF.FileName,"w");
IntPtr pBuffer;
if (chkSaveStrips.Checked) rowsPerStrip =
Convert.ToInt16(tbSaveRowsPerStrip.Text);
// set required tag. WATCH THE TYPES!!!
LibTIFF.SetField(newTiff,(int)TIFFTags.TIFFTAG_IMAGEWIDTH,(short)width);
LibTIFF.SetField(newTiff,(int)TIFFTags.TIFFTAG_IMAGELENGTH,(short)height);
LibTIFF.SetField(newTiff,(int)TIFFTags.TIFFTAG_BITSPERSAMPLE,(int)16);
LibTIFF.SetField(newTiff,(int)TIFFTags.TIFFTAG_SAMPLESPERPIXEL,(short)1);
LibTIFF.SetField(newTiff,(int)TIFFTags.TIFFTAG_ROWSPERSTRIP,(short)rowsPerStrip);
LibTIFF.SetField(newTiff,(int)TIFFTags.TIFFTAG_PHOTOMETRIC,(short)TIFFTags.PHOTOMETRIC_MINISBLACK);
LibTIFF.SetField(newTiff,(int)TIFFTags.TIFFTAG_PLANARCONFIG,(short)TIFFTags.PLANARCONFIG_CONTIG);
if (chkSaveStrips.Checked) {
// write strips
pBuffer = LibTIFF._malloc(width*2*rowsPerStrip);
// assume raw data to be provided as short[]
short[] shortbits = new short[width*rowsPerStrip];
// strips
int strips = height/rowsPerStrip;
for (int strip = 0; strip < strips; strip++)
{
for (int y = 0; y < rowsPerStrip; y++)
{
// pixels in line: set to specific gray value
for (int x=0; x < width; x++)
{
// short = -32,768 to 32,767 = signed 16-bit integer
// ushort is not supported by Marshal.Copy
shortbits[y*width+x] = (short) (10 * y);
}
}
// copy data to unsafe context
Marshal.Copy(shortbits,0,pBuffer,width*rowsPerStrip);
// ... and write to file
int ret =
LibTIFF.WriteRawStrip(newTiff,(uint)strip,pBuffer,width*2*rowsPerStrip);
if (ret == -1)
Console.WriteLine("writing strip " + strip + " returned " + ret);
}
}
else
{
// write scanline by scanline
pBuffer = LibTIFF._malloc(width*2); // 16 bpp
// assume raw data to be provided as short[]
short[] shortbits = new short[width];
// lines
for (int y = 0; y < height; y++)
{
// pixels in line: set to specific gray value
for (int x=0; x < width; x++)
{
// short = -32,768 to 32,767 = signed 16-bit integer
// ushort is not supported by Marshal.Copy
shortbits[x] = (short) (100 * y);
}
// copy data to unsafe context
Marshal.Copy(shortbits,0,pBuffer,width);
// ... and write to file
if (!LibTIFF.WriteScanline(newTiff,pBuffer,(uint)y))
{
Console.WriteLine("writing line " + y + " failed.");
}
}
}
LibTIFF._free(pBuffer);
LibTIFF.Close(newTiff);
MessageBox.Show("Time: " + (DateTime.Now - startTime));
}
-----
-------- Original-Nachricht --------
> Datum: Wed, 26 Nov 2008 01:37:27 -0800 (PST)
> Von: Cristian Perca <cristianperca@yahoo.com>
> An: tiff@lists.maptools.org
> Betreff: [Tiff] Help write 16bites grey level image
>
>
> Hi,
> I have a problem while trying to write a 16 bites grey level TIFF image.
> Here is a part of the code. The compilation and the execution are OK but
> the image I get has the expected size but all pixels are black.
> If someone has any idea I would really appreciated.
> Thank you
>
>
>
> char *flatBuffer=NULL;
> TIFF *flatTiff=NULL;
>
>
> flatBuffer=malloc(bufferSize);
>
> /*test: fill the image with average of input images in imageArray*/
> //imageArray is an dynamical array of struct, each object contains
> an image
> //All images are correctly read and object buffers filled
> for (count = 0; count < bufferSize; count +=2) {
> uint32 sumPixel=0;
> for(i=0; i<noFiles; i++) {
> //noFiles is the number of input images == number of
> created objects
> sumPixel += (*(uint16 *)((imageArray+i) -> buffer + count));
> }
> *(uint16 *)(flatBuffer+count) = (uint16)sumPixel/noFiles;
> }
>
> if((flatTiff = TIFFOpen("flat.tiff", "w")) == NULL){
> fprintf(stderr, "Could not open flat.tif for writing\n");
> exit(42);
> }
>
> TIFFSetField(flatTiff, TIFFTAG_IMAGEWIDTH, width);
> TIFFSetField(flatTiff, TIFFTAG_IMAGELENGTH, height);
> TIFFSetField(flatTiff, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
> TIFFSetField(flatTiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
> TIFFSetField(flatTiff, TIFFTAG_BITSPERSAMPLE, bps); //bps=16
> TIFFSetField(flatTiff, TIFFTAG_SAMPLESPERPIXEL, spp); //spp=1
> TIFFSetField(flatTiff, TIFFTAG_ROWSPERSTRIP, height); // put all
> rows(==height no of rows) in one strip
> TIFFSetField(flatTiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
> TIFFSetField(flatTiff, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
>
> if( i=TIFFWriteEncodedStrip(flatTiff, 0, flatBuffer, bufferSize) ==
> -1) {
> fprintf(stderr, "Error while writing image to file!");
> exit(42);
> }
> //the image has only black pixels, but if I print flatBuffer on the screen
> I get the expected values:
> //printf("%d %d\n", *(uint16 *)(flatBuffer+0), *(uint16 *)(flatBuffer+2));
> TIFFClose(flatTiff);
> }
>
>
>
>
> _______________________________________________
> Tiff mailing list: Tiff@lists.maptools.org
> http://lists.maptools.org/mailman/listinfo/tiff
> http://www.remotesensing.org/libtiff/
--
Sensationsangebot nur bis 30.11: GMX FreeDSL - Telefonanschluss + DSL
für nur 16,37 Euro/mtl.!* http://dsl.gmx.de/?ac=OM.AD.PD003K11308T4569a
|
|||||||