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
        //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;
            }
        }
示例#2
0
        static bool cvt_by_tile(Tiff inImage, Tiff outImage, int width, int height)
        {
            int tile_width = 0;
            int tile_height = 0;

            FieldValue[] result = inImage.GetField(TiffTag.TILEWIDTH);
            if (result != null)
            {
                tile_width = result[0].ToInt();

                result = inImage.GetField(TiffTag.TILELENGTH);
                if (result != null)
                    tile_height = result[0].ToInt();
            }

            if (result == null)
            {
                Tiff.Error(inImage.FileName(), "Source image not tiled");
                return false;
            }

            outImage.SetField(TiffTag.TILEWIDTH, tile_width);
            outImage.SetField(TiffTag.TILELENGTH, tile_height);

            // Allocate tile buffer
            int raster_size = multiply(tile_width, tile_height);
            int rasterByteSize = multiply(raster_size, sizeof(int));
            if (raster_size == 0 || rasterByteSize == 0)
            {
                Tiff.Error(inImage.FileName(),
                    "Can't allocate buffer for raster of size {0}x{1}", tile_width, tile_height);
                return false;
            }

            int[] raster = new int[raster_size];
            byte[] rasterBytes = new byte[rasterByteSize];

            // Allocate a scanline buffer for swapping during the vertical mirroring pass.
            // (Request can't overflow given prior checks.)
            int[] wrk_line = new int[tile_width];

            // Loop over the tiles.
            for (int row = 0; row < height; row += tile_height)
            {
                for (int col = 0; col < width; col += tile_width)
                {
                    // Read the tile into an RGBA array
                    if (!inImage.ReadRGBATile(col, row, raster))
                        return false;

                    // For some reason the ReadRGBATile() function chooses the lower left corner
                    // as the origin. Vertically mirror scanlines.
                    for (int i_row = 0; i_row < tile_height / 2; i_row++)
                    {
                        int topIndex = tile_width * i_row * sizeof(int);
                        int bottomIndex = tile_width * (tile_height - i_row - 1) * sizeof(int);

                        Buffer.BlockCopy(raster, topIndex, wrk_line, 0, tile_width * sizeof(int));
                        Buffer.BlockCopy(raster, bottomIndex, raster, topIndex, tile_width * sizeof(int));
                        Buffer.BlockCopy(wrk_line, 0, raster, bottomIndex, tile_width * sizeof(int));
                    }

                    // Write out the result in a tile.
                    int tile = outImage.ComputeTile(col, row, 0, 0);
                    Buffer.BlockCopy(raster, 0, rasterBytes, 0, rasterByteSize);
                    if (outImage.WriteEncodedTile(tile, rasterBytes, rasterByteSize) == -1)
                        return false;
                }
            }

            return true;
        }