0

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++;
                }
            }
        }

original image croped image

Nasser Tahani
  • 725
  • 12
  • 33
  • What is the TiffTag.PHOTOMETRIC and TiffTag.COMPRESSION tag values for iriginak and cropped images? – Bobrovsky Apr 23 '17 at 16:07
  • I got the TiffTag.PHOTOMETRIC from the input image using getField and replace it in the output image. Also, my input image does not contain any compression mode. I found out that there is a relation to change colors; in which Red is converted to Green, Green to Blue and Blue to Red as you notice the input and output image. – Nasser Tahani Apr 23 '17 at 18:03

1 Answers1

0

I found the problem! As the input tiff includes 3 samples per pixel, I skip the number of image pixels instead of samples which happened in line " bigHolder[i] = buffer[i].Skip(xo).ToArray();". So I've changed the xo to SamplePerPixel * xo and the output tiff remains in true colors.

Nasser Tahani
  • 725
  • 12
  • 33