I've used the following code to crop some parts of a Tiff image using LibTiff.net library, but the image colors changed to false or pseudo colors.I also tried to change the photometric tag to other possible parameters like YCBCR or Pallete but the results were similar. The original image and cropped one are attached below the code. Any idea would be appreciated to help me solving this problem.
using (input)
{
int scanlineSize = input.ScanlineSize();
byte[][] buffer = new byte[height][];
int yy = height/hRatio;
int xx = width/wRatio;
int yEnd = yo + yy;
// read
int k = 0;
for (int i = yo; i < yEnd ; i++)
{
buffer[k] = new byte[scanlineSize];
input.ReadScanline(buffer[k], i);
k++;
}
// write
byte[][] bigHolder = new byte[height][];
byte[][] holder = new byte[yy][];
using (Tiff output = Tiff.Open("output.tif", "w"))
{
output.SetField(TiffTag.SAMPLESPERPIXEL, samplesPerPixel);
output.SetField(TiffTag.IMAGEWIDTH, xx);
output.SetField(TiffTag.IMAGELENGTH, yy);
output.SetField(TiffTag.BITSPERSAMPLE, bitsPerSample);
output.SetField(TiffTag.ROWSPERSTRIP, output.DefaultStripSize(0));
output.SetField(TiffTag.PHOTOMETRIC, photo);
output.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
int j = 0;
int w = scanlineSize / wRatio;
for (int i = 0; i < yy; i++)
{
bigHolder[i] = buffer[i].Skip(xo).ToArray();
holder[i] = bigHolder[i].Take(w).ToArray();
output.WriteScanline(holder[i], j);
j++;
}
}
}