BitMiracle.LibTiff.Classic.Tiff.ReadRGBATile C# (CSharp) Method

ReadRGBATile() public method

Reads a whole tile of a tile-based image, decodes it and converts it to RGBA format.

ReadRGBATile reads a single tile of a tile-based image into memory, storing the result in the user supplied RGBA raster. The raster is assumed to be an array of width times length 32-bit entries, where width is the width of the tile (TiffTag.TILEWIDTH) and length is the height of a tile (TiffTag.TILELENGTH).

The col and row values are the offsets from the top left corner of the image to the top left corner of the tile to be read. They must be an exact multiple of the tile width and length.

Note that the raster is assumed to be organized such that the pixel at location (x, y) is raster[y * width + x]; with the raster origin in the lower-left hand corner of the tile. That is bottom to top organization. Edge tiles which partly fall off the image will be filled out with appropriate zeroed areas.

Raster pixels are 8-bit packed red, green, blue, alpha samples. The Tiff.GetR, Tiff.GetG, Tiff.GetB, and Tiff.GetA should be used to access individual samples. Images without Associated Alpha matting information have a constant Alpha of 1.0 (255).

See TiffRgbaImage for more details on how various image types are converted to RGBA values.

Samples must be either 1, 2, 4, 8, or 16 bits. Colorimetric samples/pixel must be either 1, 3, or 4 (i.e. SamplesPerPixel minus ExtraSamples).

Palette image colormaps that appear to be incorrectly written as 8-bit values are automatically scaled to 16-bits.

ReadRGBATile's main advantage over the similar O:BitMiracle.LibTiff.Classic.Tiff.ReadRGBAImage function is that for large images a single buffer capable of holding the whole image doesn't need to be allocated, only enough for one tile. The ReadRGBAStrip function does a similar operation for stripped images.

ReadRGBATile is just a wrapper around the more general TiffRgbaImage facilities.

All error messages are directed to the current error handler.

public ReadRGBATile ( int col, int row, int raster ) : bool
col int The column.
row int The row.
raster int The RGBA raster.
return bool
        public bool ReadRGBATile(int col, int row, int[] raster)
        {
            // Verify that our request is legal - on a tile file, and on a tile boundary.
            if (!IsTiled())
            {
                ErrorExt(this, m_clientdata, FileName(), "Can't use ReadRGBATile() with stripped file.");
                return false;
            }

            FieldValue[] result = GetFieldDefaulted(TiffTag.TILEWIDTH);
            int tile_xsize = result[0].ToInt();
            result = GetFieldDefaulted(TiffTag.TILELENGTH);
            int tile_ysize = result[0].ToInt();

            if ((col % tile_xsize) != 0 || (row % tile_ysize) != 0)
            {
                ErrorExt(this, m_clientdata, FileName(), "Row/col passed to ReadRGBATile() must be topleft corner of a tile.");
                return false;
            }

            // Setup the RGBA reader.
            string emsg;
            TiffRgbaImage img = TiffRgbaImage.Create(this, false, out emsg);
            if (!RGBAImageOK(out emsg) || img == null)
            {
                ErrorExt(this, m_clientdata, FileName(), "{0}", emsg);
                return false;
            }

            // The TiffRgbaImage.Get() function doesn't allow us to get off the edge of the
            // image, even to fill an otherwise valid tile. So we figure out how much we can read,
            // and fix up the tile buffer to a full tile configuration afterwards.
            int read_ysize;
            if (row + tile_ysize > img.Height)
                read_ysize = img.Height - row;
            else
                read_ysize = tile_ysize;

            int read_xsize;
            if (col + tile_xsize > img.Width)
                read_xsize = img.Width - col;
            else
                read_xsize = tile_xsize;

            // Read the chunk of imagery.
            img.row_offset = row;
            img.col_offset = col;

            bool ok = img.GetRaster(raster, 0, read_xsize, read_ysize);

            // If our read was incomplete we will need to fix up the tile by shifting the data
            // around as if a full tile of data is being returned. This is all the more
            // complicated because the image is organized in bottom to top format.
            if (read_xsize == tile_xsize && read_ysize == tile_ysize)
                return ok;

            for (int i_row = 0; i_row < read_ysize; i_row++)
            {
                Buffer.BlockCopy(raster, (read_ysize - i_row - 1) * read_xsize * sizeof(int),
                    raster, (tile_ysize - i_row - 1) * tile_xsize * sizeof(int), read_xsize * sizeof(int));

                Array.Clear(raster, (tile_ysize - i_row - 1) * tile_xsize + read_xsize, tile_xsize - read_xsize);
            }

            for (int i_row = read_ysize; i_row < tile_ysize; i_row++)
                Array.Clear(raster, (tile_ysize - i_row - 1) * tile_xsize, tile_xsize);

            return ok;
        }

Usage Example

コード例 #1
0
        //Displays the specified tiff in the picturebox
        private bool displayTiff(string tiffname)
        {
            int[] raster = new int[tileSize];
            try
            {
                using (image = Tiff.Open(tiffname, "r"))
                {
                    //loop through each tile column
                    for (int g = 0; g < bmp.Width / tileWidth + 1; g++)
                    {
                        //loop through each tile in that column
                        for (int h = 0; h < bmp.Height / tileHeight + 1; h++)
                        {
                            image.ReadRGBATile(tileWidth * g, tileHeight * h, raster);
                            //loop through each pixel column
                            for (int i = 0; i < tileWidth; i++)
                            {
                                //loop through each pixel in that column
                                for (int j = 0; j < tileHeight; j++)
                                {

                                    if ((g * tileWidth + i < bmp.Width) && (h * tileHeight + j < bmp.Height))
                                    {

                                        bmp.SetPixel(g * tileWidth + i, h * tileHeight + j, getSample(i, j, raster, tileWidth, tileHeight));

                                    }
                                }
                            }
                        }
                    }
                    pictureBox1.Image = bmp;
                    return true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return false;
            }
        }
All Usage Examples Of BitMiracle.LibTiff.Classic.Tiff::ReadRGBATile
Tiff