void Swap() { #ifdef CPL_LSB CPL_SWAP16PTR(&imagic); CPL_SWAP16PTR(&dim); CPL_SWAP16PTR(&xsize); CPL_SWAP16PTR(&ysize); CPL_SWAP16PTR(&zsize); CPL_SWAP32PTR(&min); CPL_SWAP32PTR(&max); #endif }
int RIKDataset::Identify( GDALOpenInfo * poOpenInfo ) { if( poOpenInfo->fpL == NULL || poOpenInfo->nHeaderBytes < 50 ) return FALSE; if( EQUALN((const char *) poOpenInfo->pabyHeader, "RIK3", 4) ) { return TRUE; } else { GUInt16 actLength; memcpy(&actLength, poOpenInfo->pabyHeader, 2); #ifdef CPL_MSB CPL_SWAP16PTR( &actLength ); #endif if( actLength + 2 > 1024 ) { return FALSE; } if( actLength == 0 ) return -1; if( strlen( (const char*)poOpenInfo->pabyHeader + 2 ) != actLength ) { return FALSE; } return TRUE; } }
void SAGARasterBand::SwapBuffer(void* pImage) { #ifdef CPL_LSB int bSwap = ( m_ByteOrder == 1); #else int bSwap = ( m_ByteOrder == 0); #endif if (bSwap) { if ( m_nBits == 16 ) { short* pImage16 = (short*) pImage; for( int iPixel=0; iPixel<nBlockXSize; iPixel++ ) { CPL_SWAP16PTR( pImage16 + iPixel ); } } else if ( m_nBits == 32 ) { int* pImage32 = (int*) pImage; for( int iPixel=0; iPixel<nBlockXSize; iPixel++ ) { CPL_SWAP32PTR( pImage32 + iPixel ); } } else if ( m_nBits == 64 ) { double* pImage64 = (double*) pImage; for( int iPixel=0; iPixel<nBlockXSize; iPixel++ ) { CPL_SWAP64PTR( pImage64 + iPixel ); } } } }
static GUInt16 GetRikString( VSILFILE *fp, char *str, GUInt16 strLength ) { GUInt16 actLength; VSIFReadL( &actLength, 1, sizeof(actLength), fp ); #ifdef CPL_MSB CPL_SWAP16PTR( &actLength ); #endif if( actLength + 2 > strLength ) { return actLength; } VSIFReadL( str, 1, actLength, fp ); str[actLength] = '\0'; return actLength; }
int RIKDataset::Identify( GDALOpenInfo * poOpenInfo ) { if( poOpenInfo->fpL == NULL || poOpenInfo->nHeaderBytes < 50 ) return FALSE; if( EQUALN((const char *) poOpenInfo->pabyHeader, "RIK3", 4) ) { return TRUE; } else { GUInt16 actLength; memcpy(&actLength, poOpenInfo->pabyHeader, 2); #ifdef CPL_MSB CPL_SWAP16PTR( &actLength ); #endif if( actLength + 2 > 1024 ) { return FALSE; } if( actLength == 0 ) return -1; for( int i=0;i<actLength;i++ ) { if( poOpenInfo->pabyHeader[2+i] == 0 ) return FALSE; } if( EQUAL( CPLGetExtension(poOpenInfo->pszFilename), "rik") ) return TRUE; // We really need Open to be able to conclude return -1; } }
GDALDataset *GFFDataset::Open( GDALOpenInfo *poOpenInfo ) { /* Check that the dataset is indeed a GSAT File Format (GFF) file */ if (!GFFDataset::Identify(poOpenInfo)) return NULL; /* -------------------------------------------------------------------- */ /* Confirm the requested access is supported. */ /* -------------------------------------------------------------------- */ if( poOpenInfo->eAccess == GA_Update ) { CPLError( CE_Failure, CPLE_NotSupported, "The GFF driver does not support update access to existing" " datasets.\n" ); return NULL; } GFFDataset *poDS = new GFFDataset(); poDS->fp = VSIFOpenL( poOpenInfo->pszFilename, "r" ); if( poDS->fp == NULL ) { delete poDS; return NULL; } /* Check the endianness of the file */ VSIFSeekL(poDS->fp,54,SEEK_SET); VSIFReadL(&(poDS->nEndianness),2,1,poDS->fp); const bool bSwap = #if defined(CPL_LSB) false #else true #endif ; VSIFSeekL(poDS->fp,8,SEEK_SET); VSIFReadL(&poDS->nVersionMinor,2,1,poDS->fp); if (bSwap) CPL_SWAP16PTR(&poDS->nVersionMinor); VSIFReadL(&poDS->nVersionMajor,2,1,poDS->fp); if (bSwap) CPL_SWAP16PTR(&poDS->nVersionMajor); VSIFReadL(&poDS->nLength,4,1,poDS->fp); if (bSwap) CPL_SWAP32PTR(&poDS->nLength); unsigned short nCreatorLength = 0; VSIFReadL(&nCreatorLength,2,1,poDS->fp); if (bSwap) CPL_SWAP16PTR(&nCreatorLength); /* Hack for now... I should properly load the date metadata, for * example */ VSIFSeekL(poDS->fp,56,SEEK_SET); /* By looking at the Matlab code, one should write something like the following test */ /* but the results don't seem to be the ones really expected */ /*if ((poDS->nVersionMajor == 1 && poDS->nVersionMinor > 7) || (poDS->nVersionMajor > 1)) { float fBPP; VSIFRead(&fBPP,4,1,poDS->fp); poDS->nBPP = fBPP; } else*/ { VSIFReadL(&poDS->nBPP,4,1,poDS->fp); if (bSwap) CPL_SWAP32PTR(&poDS->nBPP); } VSIFReadL(&poDS->nFrameCnt,4,1,poDS->fp); if (bSwap) CPL_SWAP32PTR(&poDS->nFrameCnt); VSIFReadL(&poDS->nImageType,4,1,poDS->fp); if (bSwap) CPL_SWAP32PTR(&poDS->nImageType); VSIFReadL(&poDS->nRowMajor,4,1,poDS->fp); if (bSwap) CPL_SWAP32PTR(&poDS->nRowMajor); VSIFReadL(&poDS->nRgCnt,4,1,poDS->fp); if (bSwap) CPL_SWAP32PTR(&poDS->nRgCnt); VSIFReadL(&poDS->nAzCnt,4,1,poDS->fp); if (bSwap) CPL_SWAP32PTR(&poDS->nAzCnt); /* We now have enough information to determine the number format */ switch (poDS->nImageType) { case 0: poDS->eDataType = GDT_Byte; break; case 1: if (poDS->nBPP == 4) poDS->eDataType = GDT_CInt16; else poDS->eDataType = GDT_CInt32; break; case 2: poDS->eDataType = GDT_CFloat32; break; default: CPLError(CE_Failure, CPLE_AppDefined, "Unknown image type found!"); delete poDS; return NULL; } /* Set raster width/height * Note that the images that are complex are listed as having twice the * number of X-direction values than there are actual pixels. This is * because whoever came up with the format was crazy (actually, my * hunch is that they designed it very much for Matlab) * */ if (poDS->nRowMajor) { poDS->nRasterXSize = poDS->nRgCnt/(poDS->nImageType == 0 ? 1 : 2); poDS->nRasterYSize = poDS->nAzCnt; } else { poDS->nRasterXSize = poDS->nAzCnt/(poDS->nImageType == 0 ? 1 : 2); poDS->nRasterYSize = poDS->nRgCnt; } if (poDS->nRasterXSize <= 0 || poDS->nRasterYSize <= 0) { CPLError(CE_Failure, CPLE_AppDefined, "Invalid raster dimensions : %d x %d", poDS->nRasterXSize, poDS->nRasterYSize); delete poDS; return NULL; } poDS->SetBand(1, new GFFRasterBand(poDS, 1, poDS->eDataType)); /* -------------------------------------------------------------------- */ /* Initialize any PAM information. */ /* -------------------------------------------------------------------- */ poDS->SetDescription( poOpenInfo->pszFilename ); poDS->TryLoadXML(); /* -------------------------------------------------------------------- */ /* Support overviews. */ /* -------------------------------------------------------------------- */ poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename ); return poDS; }
GDALDataset *LANDataset::Open( GDALOpenInfo * poOpenInfo ) { /* -------------------------------------------------------------------- */ /* We assume the user is pointing to the header (.pcb) file. */ /* Does this appear to be a pcb file? */ /* -------------------------------------------------------------------- */ if( poOpenInfo->nHeaderBytes < ERD_HEADER_SIZE ) return NULL; if( !STARTS_WITH_CI( reinterpret_cast<char *>(poOpenInfo->pabyHeader), "HEADER" ) && !STARTS_WITH_CI( reinterpret_cast<char *>(poOpenInfo->pabyHeader), "HEAD74" ) ) return NULL; /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ LANDataset *poDS = new LANDataset(); poDS->eAccess = poOpenInfo->eAccess; /* -------------------------------------------------------------------- */ /* Adopt the openinfo file pointer for use with this file. */ /* -------------------------------------------------------------------- */ if( poOpenInfo->eAccess == GA_ReadOnly ) poDS->fpImage = VSIFOpenL( poOpenInfo->pszFilename, "rb" ); else poDS->fpImage = VSIFOpenL( poOpenInfo->pszFilename, "rb+" ); if( poDS->fpImage == NULL ) { delete poDS; return NULL; } /* -------------------------------------------------------------------- */ /* Do we need to byte swap the headers to local machine order? */ /* -------------------------------------------------------------------- */ int bBigEndian = poOpenInfo->pabyHeader[8] == 0; memcpy( poDS->pachHeader, poOpenInfo->pabyHeader, ERD_HEADER_SIZE ); #ifdef CPL_LSB const int bNeedSwap = bBigEndian; #else const int bNeedSwap = !bBigEndian; #endif if( bNeedSwap ) { CPL_SWAP16PTR( poDS->pachHeader + 6 ); CPL_SWAP16PTR( poDS->pachHeader + 8 ); CPL_SWAP32PTR( poDS->pachHeader + 16 ); CPL_SWAP32PTR( poDS->pachHeader + 20 ); CPL_SWAP32PTR( poDS->pachHeader + 24 ); CPL_SWAP32PTR( poDS->pachHeader + 28 ); CPL_SWAP16PTR( poDS->pachHeader + 88 ); CPL_SWAP16PTR( poDS->pachHeader + 90 ); CPL_SWAP16PTR( poDS->pachHeader + 106 ); CPL_SWAP32PTR( poDS->pachHeader + 108 ); CPL_SWAP32PTR( poDS->pachHeader + 112 ); CPL_SWAP32PTR( poDS->pachHeader + 116 ); CPL_SWAP32PTR( poDS->pachHeader + 120 ); CPL_SWAP32PTR( poDS->pachHeader + 124 ); } /* -------------------------------------------------------------------- */ /* Capture some information from the file that is of interest. */ /* -------------------------------------------------------------------- */ if( STARTS_WITH_CI(poDS->pachHeader,"HEADER") ) { float fTmp = 0.0; memcpy(&fTmp, poDS->pachHeader + 16, 4); CPL_LSBPTR32(&fTmp); poDS->nRasterXSize = (int) fTmp; memcpy(&fTmp, poDS->pachHeader + 20, 4); CPL_LSBPTR32(&fTmp); poDS->nRasterYSize = (int) fTmp; } else { GInt32 nTmp = 0; memcpy(&nTmp, poDS->pachHeader + 16, 4); CPL_LSBPTR32(&nTmp); poDS->nRasterXSize = nTmp; memcpy(&nTmp, poDS->pachHeader + 20, 4); CPL_LSBPTR32(&nTmp); poDS->nRasterYSize = nTmp; } GInt16 nTmp16 = 0; memcpy(&nTmp16, poDS->pachHeader + 6, 2); CPL_LSBPTR16(&nTmp16); int nPixelOffset = 0; GDALDataType eDataType = GDT_Unknown; if( nTmp16 == 0 ) { eDataType = GDT_Byte; nPixelOffset = 1; } else if( nTmp16 == 1 ) // 4 bit { eDataType = GDT_Byte; nPixelOffset = -1; } else if( nTmp16 == 2 ) { nPixelOffset = 2; eDataType = GDT_Int16; } else { CPLError( CE_Failure, CPLE_AppDefined, "Unsupported pixel type (%d).", nTmp16 ); delete poDS; return NULL; } memcpy(&nTmp16, poDS->pachHeader + 8, 2); CPL_LSBPTR16(&nTmp16); const int nBandCount = nTmp16; if( !GDALCheckDatasetDimensions(poDS->nRasterXSize, poDS->nRasterYSize) || !GDALCheckBandCount(nBandCount, FALSE) ) { delete poDS; return NULL; } if( nPixelOffset != -1 && poDS->nRasterXSize > INT_MAX / (nPixelOffset * nBandCount) ) { CPLError( CE_Failure, CPLE_AppDefined, "Int overflow occurred." ); delete poDS; return NULL; } /* -------------------------------------------------------------------- */ /* Create band information object. */ /* -------------------------------------------------------------------- */ CPLErrorReset(); for( int iBand = 1; iBand <= nBandCount; iBand++ ) { if( nPixelOffset == -1 ) /* 4 bit case */ poDS->SetBand( iBand, new LAN4BitRasterBand( poDS, iBand ) ); else poDS->SetBand( iBand, new RawRasterBand( poDS, iBand, poDS->fpImage, ERD_HEADER_SIZE + (iBand-1) * nPixelOffset * poDS->nRasterXSize, nPixelOffset, poDS->nRasterXSize*nPixelOffset*nBandCount, eDataType, !bNeedSwap, TRUE )); if( CPLGetLastErrorType() != CE_None ) { delete poDS; return NULL; } } /* -------------------------------------------------------------------- */ /* Initialize any PAM information. */ /* -------------------------------------------------------------------- */ poDS->SetDescription( poOpenInfo->pszFilename ); poDS->CheckForStatistics(); poDS->TryLoadXML(); /* -------------------------------------------------------------------- */ /* Check for overviews. */ /* -------------------------------------------------------------------- */ poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename ); /* -------------------------------------------------------------------- */ /* Try to interpret georeferencing. */ /* -------------------------------------------------------------------- */ float fTmp = 0.0; memcpy(&fTmp, poDS->pachHeader + 112, 4); CPL_LSBPTR32(&fTmp); poDS->adfGeoTransform[0] = fTmp; memcpy(&fTmp, poDS->pachHeader + 120, 4); CPL_LSBPTR32(&fTmp); poDS->adfGeoTransform[1] = fTmp; poDS->adfGeoTransform[2] = 0.0; memcpy(&fTmp, poDS->pachHeader + 116, 4); CPL_LSBPTR32(&fTmp); poDS->adfGeoTransform[3] = fTmp; poDS->adfGeoTransform[4] = 0.0; memcpy(&fTmp, poDS->pachHeader + 124, 4); CPL_LSBPTR32(&fTmp); poDS->adfGeoTransform[5] = - fTmp; // adjust for center of pixel vs. top left corner of pixel. poDS->adfGeoTransform[0] -= poDS->adfGeoTransform[1] * 0.5; poDS->adfGeoTransform[3] -= poDS->adfGeoTransform[5] * 0.5; /* -------------------------------------------------------------------- */ /* If we didn't get any georeferencing, try for a worldfile. */ /* -------------------------------------------------------------------- */ if( poDS->adfGeoTransform[1] == 0.0 || poDS->adfGeoTransform[5] == 0.0 ) { if( !GDALReadWorldFile( poOpenInfo->pszFilename, NULL, poDS->adfGeoTransform ) ) GDALReadWorldFile( poOpenInfo->pszFilename, ".wld", poDS->adfGeoTransform ); } /* -------------------------------------------------------------------- */ /* Try to come up with something for the coordinate system. */ /* -------------------------------------------------------------------- */ memcpy(&nTmp16, poDS->pachHeader + 88, 2); CPL_LSBPTR16(&nTmp16); int nCoordSys = nTmp16; if( nCoordSys == 0 ) { poDS->pszProjection = CPLStrdup(SRS_WKT_WGS84); } else if( nCoordSys == 1 ) { poDS->pszProjection = CPLStrdup( "LOCAL_CS[\"UTM - Zone Unknown\",UNIT[\"Meter\",1]]" ); } else if( nCoordSys == 2 ) { poDS->pszProjection = CPLStrdup( "LOCAL_CS[\"State Plane - Zone Unknown\"," "UNIT[\"US survey foot\",0.3048006096012192]]" ); } else { poDS->pszProjection = CPLStrdup( "LOCAL_CS[\"Unknown\",UNIT[\"Meter\",1]]" ); } /* -------------------------------------------------------------------- */ /* Check for a trailer file with a colormap in it. */ /* -------------------------------------------------------------------- */ char *pszPath = CPLStrdup( CPLGetPath(poOpenInfo->pszFilename) ); char *pszBasename = CPLStrdup( CPLGetBasename(poOpenInfo->pszFilename) ); const char *pszTRLFilename = CPLFormCIFilename( pszPath, pszBasename, "trl" ); VSILFILE *fpTRL = VSIFOpenL( pszTRLFilename, "rb" ); if( fpTRL != NULL ) { char szTRLData[896] = { '\0' }; CPL_IGNORE_RET_VAL(VSIFReadL( szTRLData, 1, 896, fpTRL )); CPL_IGNORE_RET_VAL(VSIFCloseL( fpTRL )); GDALColorTable *poCT = new GDALColorTable(); for( int iColor = 0; iColor < 256; iColor++ ) { GDALColorEntry sEntry = { 0, 0, 0, 0}; sEntry.c2 = reinterpret_cast<GByte *>(szTRLData)[iColor+128]; sEntry.c1 = reinterpret_cast<GByte *>(szTRLData)[iColor+128+256]; sEntry.c3 = reinterpret_cast<GByte *>(szTRLData)[iColor+128+512]; sEntry.c4 = 255; poCT->SetColorEntry( iColor, &sEntry ); // Only 16 colors in 4bit files. if( nPixelOffset == -1 && iColor == 15 ) break; } poDS->GetRasterBand(1)->SetColorTable( poCT ); poDS->GetRasterBand(1)->SetColorInterpretation( GCI_PaletteIndex ); delete poCT; } CPLFree( pszPath ); CPLFree( pszBasename ); return poDS; }