Skip to content

Commit

Permalink
Fixed issue with cam2map requesting buffers completely outside of the…
Browse files Browse the repository at this point in the history
… tiff
  • Loading branch information
acpaquette committed Jan 7, 2025
1 parent c0885ba commit d46d1f5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 49 deletions.
101 changes: 52 additions & 49 deletions isis/src/base/objs/ImageIoHandler/GdalIoHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,48 +93,51 @@ namespace Isis {
sampleSize = m_samples - sampleStart;
outOfBounds = true;
}
if (outOfBounds) {
Brick boundedBrick(sampleSize, lineSize, bufferToFill.BandDimension(), GdalPixelToIsis(m_pixelType));
boundedBrick.SetBasePosition(sampleStart + 1, lineStart + 1, bufferToFill.Band());
CPLErr err = poBand->RasterIO(GF_Read, sampleStart, lineStart,
sampleSize, lineSize,
boundedBrick.RawBuffer(),
sampleSize, lineSize,
m_pixelType,
0, 0);

if (err >= CE_Failure) {
QString msg = "Failure when trying to read Tiff";
throw IException(IException::Unknown, msg, _FILEINFO_);
}

// Handle pixel type conversion
char *buffersRawBuf = (char *)boundedBrick.RawBuffer();
double *buffersDoubleBuf = boundedBrick.DoubleBuffer();
for (int bufferIdx = 0; bufferIdx < boundedBrick.size(); bufferIdx++) {
readPixelType(buffersDoubleBuf, buffersRawBuf, bufferIdx);
}
bufferToFill = NULL8;
bufferToFill.CopyOverlapFrom(boundedBrick);
}
else {
// silence warnings
CPLErr err = poBand->RasterIO(GF_Read, sampleStart, lineStart,
sampleSize, lineSize,
bufferToFill.RawBuffer(),
sampleSize, lineSize,
m_pixelType,
0, 0);
if (err >= CE_Failure) {
QString msg = "Failure when trying to read Tiff";
throw IException(IException::Unknown, msg, _FILEINFO_);
}

// Handle pixel type conversion
char *buffersRawBuf = (char *)bufferToFill.RawBuffer();
double *buffersDoubleBuf = bufferToFill.DoubleBuffer();
for (int bufferIdx = 0; bufferIdx < bufferToFill.size(); bufferIdx++) {
readPixelType(buffersDoubleBuf, buffersRawBuf, bufferIdx);

bufferToFill = NULL8;
if (sampleSize > 0 && lineSize > 0) {
if (outOfBounds) {
Brick boundedBrick(sampleSize, lineSize, bufferToFill.BandDimension(), GdalPixelToIsis(m_pixelType));
boundedBrick.SetBasePosition(sampleStart + 1, lineStart + 1, bufferToFill.Band());
CPLErr err = poBand->RasterIO(GF_Read, sampleStart, lineStart,
sampleSize, lineSize,
boundedBrick.RawBuffer(),
sampleSize, lineSize,
m_pixelType,
0, 0);

if (err >= CE_Failure) {
QString msg = "Failure when trying to read Tiff";
throw IException(IException::Unknown, msg, _FILEINFO_);
}

// Handle pixel type conversion
char *buffersRawBuf = (char *)boundedBrick.RawBuffer();
double *buffersDoubleBuf = boundedBrick.DoubleBuffer();
for (int bufferIdx = 0; bufferIdx < boundedBrick.size(); bufferIdx++) {
readPixelType(buffersDoubleBuf, buffersRawBuf, bufferIdx);
}
bufferToFill.CopyOverlapFrom(boundedBrick);
}
else {
// silence warnings
CPLErr err = poBand->RasterIO(GF_Read, sampleStart, lineStart,
sampleSize, lineSize,
bufferToFill.RawBuffer(),
sampleSize, lineSize,
m_pixelType,
0, 0);
if (err >= CE_Failure) {
QString msg = "Failure when trying to read Tiff";
throw IException(IException::Unknown, msg, _FILEINFO_);
}

// Handle pixel type conversion
char *buffersRawBuf = (char *)bufferToFill.RawBuffer();
double *buffersDoubleBuf = bufferToFill.DoubleBuffer();
for (int bufferIdx = 0; bufferIdx < bufferToFill.size(); bufferIdx++) {
readPixelType(buffersDoubleBuf, buffersRawBuf, bufferIdx);
}
}
}
}
Expand Down Expand Up @@ -179,13 +182,13 @@ namespace Isis {
throw IException(IException::Unknown, msg, _FILEINFO_);
}

// poBand = m_geodataSet->GetRasterBand(band)->GetMaskBand();
// err = poBand->RasterIO(GF_Write, sampleStart, lineStart,
// sampleSize, lineSize,
// m_maskBuff,
// sampleSize, lineSize,
// GDT_Byte,
// 0, 0);
poBand = m_geodataSet->GetRasterBand(band)->GetMaskBand();
gdalerr = poBand->RasterIO(GF_Write, sampleStart, lineStart,
sampleSize, lineSize,
m_maskBuff,
sampleSize, lineSize,
GDT_Byte,
0, 0);

if (gdalerr >= CE_Failure) {
QString msg = "Failure when trying to write msk file";
Expand Down
37 changes: 37 additions & 0 deletions isis/tests/GdalIoHandlerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,4 +632,41 @@ TEST_F(ReadWriteTiff, GdalIoTestsReadOutside) {
for (short i = 0; i < 7 * 7; i++) {
EXPECT_EQ(brickDoubleBuff[i], resultVec[i]);
}

// Test reading completely outside of an image
delete localBrick;
localBrick = NULL;
localBrick = new Brick(2, 2, 1, isisPixelType);

// Read non-NULL8 data
localBrick->SetBasePosition(2, 2, 1);
gdalHandler.read(*localBrick);

// Read over to ensure brick is updated back to NULL8s
localBrick->SetBasePosition(8, 8, 1);
gdalHandler.read(*localBrick);
brickDoubleBuff = localBrick->DoubleBuffer();
resultVec = {NULL8, NULL8,
NULL8, NULL8};
for (short i = 0; i < 2 * 2; i++) {
EXPECT_EQ(brickDoubleBuff[i], resultVec[i]);
}

delete localBrick;
localBrick = NULL;
localBrick = new Brick(2, 2, 1, isisPixelType);

// Read non-NULL8 data
localBrick->SetBasePosition(2, 2, 1);
gdalHandler.read(*localBrick);

// Read over to ensure brick is updated back to NULL8s
localBrick->SetBasePosition(-4, -4, 1);
gdalHandler.read(*localBrick);
brickDoubleBuff = localBrick->DoubleBuffer();
resultVec = {NULL8, NULL8,
NULL8, NULL8};
for (short i = 0; i < 2 * 2; i++) {
EXPECT_EQ(brickDoubleBuff[i], resultVec[i]);
}
}

0 comments on commit d46d1f5

Please sign in to comment.