I'm using libtiff to read Image data into an array. I have the following code
std::vector <uint32>> image;
uint32 width;
uint32 height;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
uint32 npixels = width * height;
uint32* raster;
raster = (uint32*)_TIFFmalloc(npixels * sizeof(uint32));
if (TIFFReadRGBAImageOriented(tif, width, height, raster, ORIENTATION_TOPLEFT, 0) == 1)
{
std::cout << "success" << std:endl;
}
This code works. However, what I actually want is to reduce my width and height so that only a cropped part of the image is read into the raster. Thus my actual code for npixels is:
uint32 npixels = (width -100) * (height -100);
When I try to run this, I get an:
Exception Error at 0x00007FFEC7A2FC4E (tiff.dll): Access violation when trying to write at position 0x00000251B12C7000
In the libtiff documentation it says:
The raster is assumed to be an array of width times height 32-bit entries, where width must be less than or equal to the width of the image (height may be any non-zero size). If the raster dimensions are smaller than the image, the image data is cropped to the raster bounds.
based on that I thought reducing npixels does the trick... How do I cut the right and lower part of the image I want to write into my raster?
You just changed the number of elements in allocated buffer, but still try to read the image of original size, thus you get access violation since the buffer is overflown. To get the cropping you should pass correct width and height to TIFFReadRGBAImageOriented
as well:
uint32 nwidth = width - 100;
uint32 nheight = height - 100;
uint32 npixels = nwidth * nheight;
raster = (uint32*)_TIFFmalloc(npixels * sizeof(uint32));
if (TIFFReadRGBAImageOriented(tif, nwidth, nheight, raster, ORIENTATION_TOPLEFT, 0) == 1)
{
std::cout << "success" << std:endl;
}