diff --git a/src/Lib/HGT/hgt.cxx b/src/Lib/HGT/hgt.cxx index 76f77c66..f768b031 100644 --- a/src/Lib/HGT/hgt.cxx +++ b/src/Lib/HGT/hgt.cxx @@ -58,16 +58,12 @@ TGHgt::TGHgt( int _res ) hgt_resolution = _res; data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; - output_data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; + read_buffer = new short int[MAX_HGT_SIZE]; } -TGHgt::TGHgt( int _res, const SGPath &file ) +TGHgt::TGHgt( int _res, const SGPath &file ) : TGHgt( _res ) { - hgt_resolution = _res; - data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; - output_data = new short int[MAX_HGT_SIZE][MAX_HGT_SIZE]; - TGHgt::open( file ); } @@ -164,16 +160,21 @@ TGHgt::load( ) { return false; } - short int *var; + for ( int row = size - 1; row >= 0; --row ) { - for ( int col = 0; col < size; ++col ) { - var = &data[col][row]; - if ( gzread ( fd, var, sizeof(short) ) != sizeof(short) ) { - return false; - } - if ( sgIsLittleEndian() ) { - sgEndianSwap( (unsigned short int*)var); - } + if ( gzfread( (voidp)read_buffer, 2, size, fd ) != (unsigned)size ) { + return false; + } + + // convert to column-major + for ( int col = 0; col < size; ++col ) + data[col][row] = *(read_buffer + col); + } + + if (sgIsLittleEndian()) { + auto pData = (unsigned short *)data; + for (int i = 0; i < rows * cols; ++i) { + sgEndianSwap(pData++); } } @@ -181,9 +182,8 @@ TGHgt::load( ) { } - TGHgt::~TGHgt() { // printf("class TGSrtmBase DEstructor called.\n"); delete [] data; - delete [] output_data; + delete [] read_buffer; } diff --git a/src/Lib/HGT/hgt.hxx b/src/Lib/HGT/hgt.hxx index faa9f49c..afd7026f 100644 --- a/src/Lib/HGT/hgt.hxx +++ b/src/Lib/HGT/hgt.hxx @@ -53,7 +53,7 @@ private: // pointers to the actual grid data allocated here short int (*data)[MAX_HGT_SIZE]; - short int (*output_data)[MAX_HGT_SIZE]; + short int *read_buffer; public: