This forum contains outdated content and is available for reading only. Please contact technical support if you have any questions.

How to read pixel values of 48bpp Tiff file

Hi…

I was able to read 8-bit JPG (24bpp RGB) pixels using C# and the pixel values are in the range of 0-255 which is normal as 2^8=256.
Following is the code I used to access 8 bit image pixel values in C#:


       for (int x = 0; x < image.width; x++)  
         {       
            for (int y = 0; y < image.height; y++)    
            {           
                     int redValue     = image.GetPixel(x, y).R;      
                     int greenValue = image.GetPixel(x, y).G;  
                     int blueValue   = image.GetPixel(x, y).B;  
                    //Do something with redValue, greenValue, blueValue here
             } 
        }


Please let me know if I can read 16-bit TIFF (48bpp RGB) pixel values using GM and what would be the range of those pixel values.

Thanks.
Could you attach an example of 48bpp TIFF image?
Hi,

Please try the following code.

using (var bitmap = new Bitmap("48bppRgb.tif"))
{
    var pixel = (Rgb16Color)bitmap.GetPixel(x, y);
    Console.WriteLine(pixel.R);
    Console.WriteLine(pixel.G);
    Console.WriteLine(pixel.B);
}


The range will be [0..65535].
That's good. But the code takes very long time to execute. Is it possible to make code execution faster?
Thank you for the link. The example on the page states that the code is for 8 bit images where each byte is incremented to get next color.
But, I'm not sure how to implement the same example for 48bpp images where two bytes are incremented to get next color.
Okay, I finally figured it out. Here is the code:

 using (var bitmap = new Aurigma.GraphicsMill.Bitmap(@"48bpp.tif"))
            {
               unsafe
                {
                      //A pointer to the beginning of the pixel data region
                      byte* pointer = (byte*)(bitmap.Scan0.ToPointer());

                      //Number of bytes in a row
                      ushort stride =(ushort) bitmap.Stride;

                      //Number of rows
                      int height = bitmap.Height;

                      //bytesperpixel
                      int bytesperpixel = bitmap.PixelFormat.Size/8;

                        for (int i = 0; i < height; i++)
                        {
                            ushort* position = (ushort*)(pointer + stride * i);

                            for (int j = 0; j < stride; j = j + bytesperpixel)
                            {
                                double red = position[j + 2];
                                double green = position[j + 1];
                                double blue = position[j];

                                //do something with red green blue

                            }
                        }

                }
            }