Hello to all gdal community members,

I had a very basic question, but I couldn't find a proper answer anywhere in 
the forums or mailing lists, even on the official tutorials like:
https://gdal.org/tutorials/raster_api_tut.html

So my simple question is how to handle properly a "generic" data type (i.e. 
known only at runtime) when reading a raster dataset from an image.

So far I could use properly GetRasterDataType / RasterIO to read my data, but I 
always have to specify in my code the actual data type (ex uint16_t).

The best workaround that I could find is, first read the dataset as "raw" 
binary data (i.e. type=uint8_t) then "interpret" it to the proper actual type 
(ex uint16_t).
But this is still a heavy solution and moreover during the data interpretation 
I could not avoid a copy of the complete dataset !
Even my attemps to use templates did not help much because they only apply to 
functions (not variables or members) and don't really solve the problem.

So to sum up I would like to:
1) open an image (e.g. a tiff image with 1 single band) WITHOUT knowing its 
data type beforehand
2) store the raster dataset in a variable of any kind, WITHOUT making copy or 
type cast (I mean involving math operations ) !
3) easily find and print some values (ex: image[0;0])

I will appreciate any answer on this subject !
Thank you all.

Jérôme Moré.
PS: I included my current code in a separate file.



Envoyé à partir d’Outlook<http://aka.ms/weboutlook>
// g++ gdal_mwe.cpp -o gdal_mwe -I /usr/include/gdal -l gdal

#include <iostream>
#include "gdal_priv.h"
#include <time.h>

using namespace std;

// ----------------------------------------------------------------------

class gdalImage {
  private:
		string 					path;
		int 						width;
		int 						height;
		int 						bandCount;
		GDALDataType		dataType;
		int							dataTypeSizeBytes;
		GDALDataset*		gdalDataset;
		vector<uint8_t>	pixelBytes;
  public:
		void	read (string path);
		template <typename pixelT>
		void	displayTemplate ();
		void	display ();
};

// ----------------------------------------------------------------------

void	gdalImage::read (string p)
{
	path							= p;	
	gdalDataset 			= (GDALDataset*) GDALOpen (path.c_str (), GA_ReadOnly);
	width							= gdalDataset->GetRasterXSize ();
	height						= gdalDataset->GetRasterYSize ();
	bandCount					= gdalDataset->GetRasterCount ();
	GDALRasterBand	&poBand	= *gdalDataset->GetRasterBand (1);
	dataType					= poBand.GetRasterDataType ();
	dataTypeSizeBytes	= GDALGetDataTypeSizeBytes(dataType);
	pixelBytes.resize	(width*height*bandCount*dataTypeSizeBytes);
	CPLErr err				= poBand.RasterIO (GF_Read, 0, 0, width, height,
			&pixelBytes[0], width, height, dataType, 0, 0); //ok juste
//	CPLErr err				= poBand.RasterIO (GF_Read, 0, 0, width, height,
//			&pixelBytes[0], width, height, GDT_Byte, 0, 0);
}

// ----------------------------------------------------------------------

template <typename pixelT>
void	gdalImage::displayTemplate ()
{
	cout << "image: " << path << endl;
	cout << "  dimensions: ";
	cout << width << " x " << height << " x " << bandCount << " bands";
	cout << ", type: " << dataType;
	cout << " (size " << dataTypeSizeBytes << " Bytes)" << endl;
	cout << "  nb of bytes: " << pixelBytes.size() << endl;

//	vector<uint16_t> pixels (&pixelBytes[0], &pixelBytes[0] + width*height*bandCount);

	vector<pixelT> pixels (width*height*bandCount);
	memcpy ( &pixels[0], &pixelBytes[0], pixelBytes.size() );

	for (int k=0; k<width*height && k<10*10; k++)
	{
//		cout << 0+pixelBytes[k] << " ";
		cout << 0+pixels[k] << " ";
		if ((k+1)%width==0)
			cout << endl;
	}
	cout << endl;

//	for (int w=0; w<width && w<10; w++)
//	{
//		for (int h=0; h<height && h<10; h++)
//			cout << 0+pixelBytes[w + width*h] << "   ";
//		cout << endl;
//	}
}

// ----------------------------------------------------------------------

void	gdalImage::display ()
{
	switch (dataType)
	{
		case GDT_Byte:		displayTemplate<uint8_t>();		break;
		case GDT_UInt16:	displayTemplate<uint16_t>();	break;
		case GDT_Int16:		displayTemplate<int16_t>();		break;
		case GDT_UInt32:	displayTemplate<uint32_t>();	break;
		case GDT_Int32:		displayTemplate<int32_t>();	 	break;
		case GDT_Float32:	displayTemplate<float>();	 		break;
		case GDT_Float64:	displayTemplate<double>();	 	break;
		default: break;
	}
}

// ----------------------------------------------------------------------

int main()
{
	GDALAllRegister ();

//	string 					path		= "/media/jerome/Data/Images/datatypes/4x4_Byte.tif";
//	string 					path		= "/media/jerome/Data/Images/datatypes/4x4_UInt16_sc.tif";
//	string 					path		= "/media/jerome/Data/Images/datatypes/4x4_Int16.tif";
//	string 					path		= "/media/jerome/Data/Images/datatypes/4x4_UInt32.tif";
//	string 					path		= "/media/jerome/Data/Images/datatypes/4x4_Int32.tif";
//	string 					path		= "/media/jerome/Data/Images/arles.tif";
	string 					path		= "/media/jerome/Data/Images/IMG_L2A_TOA_20181025_a20181005T104652_pSPOT6_mMS.TIF";

//	gdalImage <uint16_t> gim;
	gdalImage gim;
	gim.read (path);
//	gim.displayTemplate<uint16_t>();
	gim.display();
}

// ----------------------------------------------------------------------

_______________________________________________
gdal-dev mailing list
[email protected]
https://lists.osgeo.org/mailman/listinfo/gdal-dev

Reply via email to