Reputation: 113
I am using LibTiff.NET to create greyscale TIFF images. The images are the raw pixel values from a camera. The camera creates 14 bit images ie. pixel intensities from 0 to 16383. Im using the code displayed below to create the TIFF image. The result looks correct except it is very dark. I'm guessing this is because the TIFF is assigning 16 bits per pixel which gives a maximum of 65535. This would mean the maximum pixel value from my camera is only one quarter of the maximum and the brightness is scaled accordingly. Is there an easy fix for this. I could multiply my pixel values by 4 but I don't want to amend the raw pixel values. The reason we prefer TIFF format is it can be used to store the original pixel data.
public static void createTiffShorts(int width, int height, ushort[] imageData)
{
string fileName = "myImage.tif";
using (Tiff output = Tiff.Open(fileName, "w"))
{
output.SetField(TiffTag.IMAGEWIDTH, width);
output.SetField(TiffTag.IMAGELENGTH, height);
output.SetField(TiffTag.SAMPLESPERPIXEL, 1);
output.SetField(TiffTag.BITSPERSAMPLE, 16);
output.SetField(TiffTag.ORIENTATION, BitMiracle.LibTiff.Classic.Orientation.TOPLEFT);
output.SetField(TiffTag.ROWSPERSTRIP, height);
output.SetField(TiffTag.XRESOLUTION, 88.0);
output.SetField(TiffTag.YRESOLUTION, 88.0);
output.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.CENTIMETER);
output.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
output.SetField(TiffTag.PHOTOMETRIC, Photometric.MINISBLACK);
output.SetField(TiffTag.COMPRESSION, Compression.NONE);
output.SetField(TiffTag.FILLORDER, FillOrder.MSB2LSB);
for (int i = 0; i < height; i++)
{
ushort[] samples = new ushort[width];
for (int j = 0; j < width; j++)
samples[j] = (ushort)(imageData[(height - 1 - i) * width + j]);
byte[] buffer = new byte[samples.Length * sizeof(ushort)];
Buffer.BlockCopy(samples, 0, buffer, 0, buffer.Length);
output.WriteScanline(buffer, i);
}
output.WriteDirectory();
}
System.Diagnostics.Process.Start(fileName);
}
Upvotes: 0
Views: 63