Move readImageData from GdalRawTileDataReader to RawTileDataReader so that it can be used by other RawTileDataReaders

This commit is contained in:
Kalle Bladin
2017-07-13 20:23:57 +02:00
parent b09f89f918
commit 09b3675491
8 changed files with 153 additions and 141 deletions
@@ -62,6 +62,10 @@ std::shared_ptr<RawTileDataReader> AsyncTileDataProvider::getRawTileDataReader()
}
bool AsyncTileDataProvider::enqueueTileIO(const TileIndex& tileIndex) {
if (tileIndex.level < 2) {
int hej = 0;
}
if (_resetMode == ResetMode::ShouldNotReset && satisfiesEnqueueCriteria(tileIndex)) {
if (_pboContainer) {
char* dataPtr = static_cast<char*>(_pboContainer->mapBuffer(
@@ -106,6 +106,10 @@ int GdalRawTileDataReader::rasterYSize() const {
return _gdalDatasetMetaDataCached.rasterYSize;
}
int GdalRawTileDataReader::dataSourceNumRasters() const {
return _gdalDatasetMetaDataCached.rasterCount;
}
float GdalRawTileDataReader::depthOffset() const {
return _gdalDatasetMetaDataCached.offset;
}
@@ -182,134 +186,6 @@ void GdalRawTileDataReader::initialize() {
_cached._maxLevel = std::max(_cached._maxLevel, 2);
}
void GdalRawTileDataReader::readImageData(
IODescription& io, RawTile::ReadError& worstError, char* imageDataDest) const {
// Only read the minimum number of rasters
int nRastersToRead = std::min(_gdalDatasetMetaDataCached.rasterCount,
static_cast<int>(_initData.nRasters()));
switch (_initData.ghoulTextureFormat()) {
case ghoul::opengl::Texture::Format::Red:
if (nRastersToRead == 1) { // One channel
// The final destination pointer is offsetted by one datum byte size
// for every raster (or data channel, i.e. R in RGB)
char* dataDestination = imageDataDest + _initData.bytesPerDatum();
RawTile::ReadError err = repeatedRasterRead(1, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
break;
case ghoul::opengl::Texture::Format::RG:
case ghoul::opengl::Texture::Format::RGB:
case ghoul::opengl::Texture::Format::RGBA: {
if (nRastersToRead == 1) { // Grayscale
for (int i = 0; i < 3; i++) {
// The final destination pointer is offsetted by one datum byte size
// for every raster (or data channel, i.e. R in RGB)
char* dataDestination = imageDataDest + (i * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(1, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
}
else if (nRastersToRead == 2) { // Grayscale + alpha
for (int i = 0; i < 3; i++) {
// The final destination pointer is offsetted by one datum byte size
// for every raster (or data channel, i.e. R in RGB)
char* dataDestination = imageDataDest + (i * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(1, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
// Last read is the alpha channel
char* dataDestination = imageDataDest + (3 * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(2, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
else { // Three or more rasters
for (int i = 0; i < nRastersToRead; i++) {
// The final destination pointer is offsetted by one datum byte size
// for every raster (or data channel, i.e. R in RGB)
char* dataDestination = imageDataDest + (i * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(i + 1, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
}
break;
}
case ghoul::opengl::Texture::Format::BGR:
case ghoul::opengl::Texture::Format::BGRA: {
if (nRastersToRead == 1) { // Grayscale
for (int i = 0; i < 3; i++) {
// The final destination pointer is offsetted by one datum byte size
// for every raster (or data channel, i.e. R in RGB)
char* dataDestination = imageDataDest + (i * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(1, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
}
else if (nRastersToRead == 2) { // Grayscale + alpha
for (int i = 0; i < 3; i++) {
// The final destination pointer is offsetted by one datum byte size
// for every raster (or data channel, i.e. R in RGB)
char* dataDestination = imageDataDest + (i * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(1, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
// Last read is the alpha channel
char* dataDestination = imageDataDest + (3 * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(2, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
else { // Three or more rasters
for (int i = 0; i < 3 && i < nRastersToRead; i++) {
// The final destination pointer is offsetted by one datum byte size
// for every raster (or data channel, i.e. R in RGB)
char* dataDestination = imageDataDest + (i * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(3 - i, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
}
if (nRastersToRead > 3) { // Alpha channel exists
// Last read is the alpha channel
char* dataDestination = imageDataDest + (3 * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(4, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
break;
}
default: {
ghoul_assert(false, "Texture format not supported for tiles");
break;
}
}
}
RawTile::ReadError GdalRawTileDataReader::rasterRead(
int rasterBand, const IODescription& io, char* dataDestination) const
{
@@ -400,4 +276,4 @@ int GdalRawTileDataReader::calculateTileLevelDifference(int minimumPixelSize) co
} // namespace globebrowsing
} // namespace openspace
#endif // GLOBEBROWSING_USE_GDAL
#endif // GLOBEBROWSING_USE_GDAL
@@ -79,6 +79,7 @@ public:
virtual float noDataValueAsFloat() const override;
virtual int rasterXSize() const override;
virtual int rasterYSize() const override;
virtual int dataSourceNumRasters() const;
virtual float depthOffset() const override;
virtual float depthScale() const override;
@@ -96,8 +97,6 @@ protected:
private:
// Private virtual function overloading
virtual void initialize() override;
virtual void readImageData(IODescription& io, RawTile::ReadError& worstError,
char* dataDestination) const override;
virtual RawTile::ReadError rasterRead(
int rasterBand, const IODescription& io, char* dst) const override;
@@ -116,6 +116,134 @@ std::shared_ptr<RawTile> RawTileDataReader::readTileData(TileIndex tileIndex,
return rawTile;
}
void RawTileDataReader::readImageData(
IODescription& io, RawTile::ReadError& worstError, char* imageDataDest) const {
// Only read the minimum number of rasters
int nRastersToRead = std::min(dataSourceNumRasters(),
static_cast<int>(_initData.nRasters()));
switch (_initData.ghoulTextureFormat()) {
case ghoul::opengl::Texture::Format::Red:
if (nRastersToRead == 1) { // One channel
// The final destination pointer is offsetted by one datum byte size
// for every raster (or data channel, i.e. R in RGB)
char* dataDestination = imageDataDest + _initData.bytesPerDatum();
RawTile::ReadError err = repeatedRasterRead(1, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
break;
case ghoul::opengl::Texture::Format::RG:
case ghoul::opengl::Texture::Format::RGB:
case ghoul::opengl::Texture::Format::RGBA: {
if (nRastersToRead == 1) { // Grayscale
for (int i = 0; i < 3; i++) {
// The final destination pointer is offsetted by one datum byte size
// for every raster (or data channel, i.e. R in RGB)
char* dataDestination = imageDataDest + (i * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(1, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
}
else if (nRastersToRead == 2) { // Grayscale + alpha
for (int i = 0; i < 3; i++) {
// The final destination pointer is offsetted by one datum byte size
// for every raster (or data channel, i.e. R in RGB)
char* dataDestination = imageDataDest + (i * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(1, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
// Last read is the alpha channel
char* dataDestination = imageDataDest + (3 * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(2, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
else { // Three or more rasters
for (int i = 0; i < nRastersToRead; i++) {
// The final destination pointer is offsetted by one datum byte size
// for every raster (or data channel, i.e. R in RGB)
char* dataDestination = imageDataDest + (i * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(i + 1, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
}
break;
}
case ghoul::opengl::Texture::Format::BGR:
case ghoul::opengl::Texture::Format::BGRA: {
if (nRastersToRead == 1) { // Grayscale
for (int i = 0; i < 3; i++) {
// The final destination pointer is offsetted by one datum byte size
// for every raster (or data channel, i.e. R in RGB)
char* dataDestination = imageDataDest + (i * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(1, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
}
else if (nRastersToRead == 2) { // Grayscale + alpha
for (int i = 0; i < 3; i++) {
// The final destination pointer is offsetted by one datum byte size
// for every raster (or data channel, i.e. R in RGB)
char* dataDestination = imageDataDest + (i * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(1, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
// Last read is the alpha channel
char* dataDestination = imageDataDest + (3 * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(2, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
else { // Three or more rasters
for (int i = 0; i < 3 && i < nRastersToRead; i++) {
// The final destination pointer is offsetted by one datum byte size
// for every raster (or data channel, i.e. R in RGB)
char* dataDestination = imageDataDest + (i * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(3 - i, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
}
if (nRastersToRead > 3) { // Alpha channel exists
// Last read is the alpha channel
char* dataDestination = imageDataDest + (3 * _initData.bytesPerDatum());
RawTile::ReadError err = repeatedRasterRead(4, io, dataDestination);
// CE_None = 0, CE_Debug = 1, CE_Warning = 2, CE_Failure = 3, CE_Fatal = 4
worstError = std::max(worstError, err);
}
break;
}
default: {
ghoul_assert(false, "Texture format not supported for tiles");
break;
}
}
}
TileDepthTransform RawTileDataReader::getDepthTransform() const {
return _depthTransform;
}
@@ -76,6 +76,7 @@ public:
virtual float noDataValueAsFloat() const = 0;
virtual int rasterXSize() const = 0;
virtual int rasterYSize() const = 0;
virtual int dataSourceNumRasters() const = 0;
virtual float depthOffset() const;
virtual float depthScale() const;
PixelRegion fullPixelRegion() const;
@@ -96,11 +97,6 @@ protected:
*/
virtual void initialize() = 0;
/**
* Call this in the constructor of the class extending <code>RawTileDataReader</code>
*/
//void ensureInitialized();
/**
* The function returns a transform to map
* the pixel coordinates to cover the whole geodetic lat long space.
@@ -113,8 +109,8 @@ protected:
* \param <code>worstError</code> should be set to the error code returned when
* reading the data.
*/
virtual void readImageData(
IODescription& io, RawTile::ReadError& worstError, char* dataDestination) const = 0;
void readImageData(
IODescription& io, RawTile::ReadError& worstError, char* dataDestination) const;
virtual RawTile::ReadError rasterRead(
int rasterBand, const IODescription& io, char* dst) const = 0;
@@ -73,6 +73,10 @@ int SimpleRawTileDataReader::rasterYSize() const {
return _dataTexture->dimensions().y;
}
int SimpleRawTileDataReader::dataSourceNumRasters() const {
return _dataTexture->numberOfChannels();
}
float SimpleRawTileDataReader::depthOffset() const {
return 0.0f;
}
@@ -115,6 +119,7 @@ void SimpleRawTileDataReader::initialize() {
_depthTransform = {depthScale(), depthOffset()};
}
/*
void SimpleRawTileDataReader::readImageData(
IODescription& io, RawTile::ReadError& worstError, char* dataDestination) const {
@@ -127,6 +132,7 @@ void SimpleRawTileDataReader::readImageData(
// None = 0, Debug = 1, Warning = 2, Failure = 3, Fatal = 4
worstError = std::max(worstError, err);
}
*/
RawTile::ReadError SimpleRawTileDataReader::rasterRead(
int rasterBand, const IODescription& io, char* dataDestination) const
@@ -141,6 +147,8 @@ RawTile::ReadError SimpleRawTileDataReader::rasterRead(
"IODescription does not match data texture.");
char* pixelWriteRow = dataDestination;
/*
try {
// For each row
for (int y = 0; y < io.read.region.numPixels.y; y++) {
@@ -157,6 +165,7 @@ RawTile::ReadError SimpleRawTileDataReader::rasterRead(
return RawTile::ReadError::Failure;
}
return RawTile::ReadError::None;
*/
}
@@ -58,6 +58,7 @@ public:
virtual float noDataValueAsFloat() const override;
virtual int rasterXSize() const override;
virtual int rasterYSize() const override;
virtual int dataSourceNumRasters() const;
virtual float depthOffset() const override;
virtual float depthScale() const override;
@@ -68,8 +69,6 @@ protected:
private:
// Private virtual function overloading
virtual void initialize() override;
virtual void readImageData(
IODescription& io, RawTile::ReadError& worstError, char* dataDestination) const override;
virtual RawTile::ReadError rasterRead(
int rasterBand, const IODescription& io, char* dst) const override;
@@ -214,7 +214,8 @@ void DefaultTileProvider::initAsyncTileDataReader(TileTextureInitData initData)
_asyncTextureDataProvider = std::make_shared<AsyncTileDataProvider>(_name, tileDataset);
if (_preCacheLevel > -1) {
// Tiles are only available for levels 2 and higher.
if (_preCacheLevel >= 2) {
LDEBUG("Precaching '" << _filePath << "' with level '" << _preCacheLevel << "'");
for (int level = 0; level <= _preCacheLevel; ++level) {
for (int x = 0; x <= level * 2; ++x) {