I'm trying to simply use an offset to select tiles from one tiff image and write them to another image. I am using libtiff.net. Instead of each pixel having an ARGB value, there is one pixel that is alpha, then the pixel to the right of that is a red pixel, the one to the right of that is green and of course the one to the right of that is blue. I need to store those 4 pixels in one and something is happening along the way. Can you see anything wrong with this logic?
using (Tiff output = Tiff.Open(@"C:\base.tif", "w"))
{
if (output == null)
{
System.Console.Error.WriteLine("Could not open outgoing image");
return;
}
// We need to know the width and the height before we can malloc
int[] raster = new int[tileWidth * tileHeight];
// Write the tiff tags to the file
output.SetField(TiffTag.IMAGEWIDTH, 1024);
output.SetField(TiffTag.IMAGELENGTH, 1024);
output.SetField(TiffTag.TILEWIDTH, 256);
output.SetField(TiffTag.TILELENGTH, 256);
output.SetField(TiffTag.COMPRESSION, Compression.DEFLATE);
output.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
output.SetField(TiffTag.PHOTOMETRIC, Photometric.RGB);
output.SetField(TiffTag.BITSPERSAMPLE, 8);
output.SetField(TiffTag.SAMPLESPERPIXEL, numOfBands);
byte[] inputBuffer = new byte[tileWidth * tileHeight * numOfBands];
// loop through every tile column
for (int x = 0; x < 1024 / tileWidth; x++)
{
// loop through every tile row in the current column
for (int y = 0; y < 1024 / tileHeight; y++)
{
image.ReadRGBATile((x + xOffset) * tileWidth, (y + yOffset) * tileHeight, raster);
Tiff.IntsToByteArray(raster, 0, raster.Length, inputBuffer, 0);
output.WriteEncodedTile(getTileIndex(x, y, 1024 / tileWidth), inputBuffer, 0, raster.Length);
}
}
}