VRTSource *VRTDriver::ParseSource( CPLXMLNode *psSrc, const char *pszVRTPath ) { if( psSrc == NULL || psSrc->eType != CXT_Element ) { CPLError( CE_Failure, CPLE_AppDefined, "Corrupt or empty VRT source XML document." ); return NULL; } const char *pszParserFunc = CSLFetchNameValue( papszSourceParsers, psSrc->pszValue ); if( pszParserFunc == NULL ) return NULL; VRTSourceParser pfnParser = reinterpret_cast<VRTSourceParser>( CPLScanPointer( pszParserFunc, static_cast<int>(strlen(pszParserFunc)) ) ); if( pfnParser == NULL ) return NULL; return pfnParser( psSrc, pszVRTPath ); }
CPLErr MEMDataset::AddBand( GDALDataType eType, char **papszOptions ) { int nBandId = GetRasterCount() + 1; GByte *pData; int nPixelSize = (GDALGetDataTypeSize(eType) / 8); /* -------------------------------------------------------------------- */ /* Do we need to allocate the memory ourselves? This is the */ /* simple case. */ /* -------------------------------------------------------------------- */ if( CSLFetchNameValue( papszOptions, "DATAPOINTER" ) == NULL ) { pData = (GByte *) VSICalloc(nPixelSize * GetRasterXSize(), GetRasterYSize() ); if( pData == NULL ) { CPLError( CE_Failure, CPLE_OutOfMemory, "Unable to create band arrays ... out of memory." ); return CE_Failure; } SetBand( nBandId, new MEMRasterBand( this, nBandId, pData, eType, nPixelSize, nPixelSize * GetRasterXSize(), TRUE ) ); return CE_None; } /* -------------------------------------------------------------------- */ /* Get layout of memory and other flags. */ /* -------------------------------------------------------------------- */ const char *pszOption; int nPixelOffset, nLineOffset; const char *pszDataPointer; pszDataPointer = CSLFetchNameValue(papszOptions,"DATAPOINTER"); pData = (GByte *) CPLScanPointer(pszDataPointer, strlen(pszDataPointer)); pszOption = CSLFetchNameValue(papszOptions,"PIXELOFFSET"); if( pszOption == NULL ) nPixelOffset = nPixelSize; else nPixelOffset = atoi(pszOption); pszOption = CSLFetchNameValue(papszOptions,"LINEOFFSET"); if( pszOption == NULL ) nLineOffset = GetRasterXSize() * nPixelOffset; else nLineOffset = atoi(pszOption); SetBand( nBandId, new MEMRasterBand( this, nBandId, pData, eType, nPixelOffset, nLineOffset, FALSE ) ); return CE_None; }
GDALDataset *MEMDataset::Open( GDALOpenInfo * poOpenInfo ) { char **papszOptions; /* -------------------------------------------------------------------- */ /* Do we have the special filename signature for MEM format */ /* description strings? */ /* -------------------------------------------------------------------- */ if( !EQUALN(poOpenInfo->pszFilename,"MEM:::",6) || poOpenInfo->fp != NULL ) return NULL; papszOptions = CSLTokenizeStringComplex(poOpenInfo->pszFilename+6, ",", TRUE, FALSE ); /* -------------------------------------------------------------------- */ /* Verify we have all required fields */ /* -------------------------------------------------------------------- */ if( CSLFetchNameValue( papszOptions, "PIXELS" ) == NULL || CSLFetchNameValue( papszOptions, "LINES" ) == NULL || CSLFetchNameValue( papszOptions, "DATAPOINTER" ) == NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "Missing required field (one of PIXELS, LINES or DATAPOINTER)\n" "Unable to access in-memory array." ); CSLDestroy( papszOptions ); return NULL; } /* -------------------------------------------------------------------- */ /* Create the new MEMDataset object. */ /* -------------------------------------------------------------------- */ MEMDataset *poDS; poDS = new MEMDataset(); poDS->nRasterXSize = atoi(CSLFetchNameValue(papszOptions,"PIXELS")); poDS->nRasterYSize = atoi(CSLFetchNameValue(papszOptions,"LINES")); poDS->eAccess = GA_Update; /* -------------------------------------------------------------------- */ /* Extract other information. */ /* -------------------------------------------------------------------- */ const char *pszOption; GDALDataType eType; int nBands, nPixelOffset, nLineOffset; size_t nBandOffset; const char *pszDataPointer; GByte *pabyData; pszOption = CSLFetchNameValue(papszOptions,"BANDS"); if( pszOption == NULL ) nBands = 1; else { nBands = atoi(pszOption); } if (!GDALCheckDatasetDimensions(poDS->nRasterXSize, poDS->nRasterYSize) || !GDALCheckBandCount(nBands, TRUE)) { CSLDestroy( papszOptions ); delete poDS; return NULL; } pszOption = CSLFetchNameValue(papszOptions,"DATATYPE"); if( pszOption == NULL ) eType = GDT_Byte; else { if( atoi(pszOption) > 0 && atoi(pszOption) < GDT_TypeCount ) eType = (GDALDataType) atoi(pszOption); else { int iType; eType = GDT_Unknown; for( iType = 0; iType < GDT_TypeCount; iType++ ) { if( EQUAL(GDALGetDataTypeName((GDALDataType) iType), pszOption) ) { eType = (GDALDataType) iType; break; } } if( eType == GDT_Unknown ) { CPLError( CE_Failure, CPLE_AppDefined, "DATATYPE=%s not recognised.", pszOption ); CSLDestroy( papszOptions ); delete poDS; return NULL; } } } pszOption = CSLFetchNameValue(papszOptions,"PIXELOFFSET"); if( pszOption == NULL ) nPixelOffset = GDALGetDataTypeSize(eType) / 8; else nPixelOffset = atoi(pszOption); pszOption = CSLFetchNameValue(papszOptions,"LINEOFFSET"); if( pszOption == NULL ) nLineOffset = poDS->nRasterXSize * nPixelOffset; else nLineOffset = atoi(pszOption); pszOption = CSLFetchNameValue(papszOptions,"BANDOFFSET"); if( pszOption == NULL ) nBandOffset = nLineOffset * (size_t) poDS->nRasterYSize; else nBandOffset = atoi(pszOption); pszDataPointer = CSLFetchNameValue(papszOptions,"DATAPOINTER"); pabyData = (GByte *) CPLScanPointer( pszDataPointer, strlen(pszDataPointer) ); /* -------------------------------------------------------------------- */ /* Create band information objects. */ /* -------------------------------------------------------------------- */ for( int iBand = 0; iBand < nBands; iBand++ ) { poDS->SetBand( iBand+1, new MEMRasterBand( poDS, iBand+1, pabyData + iBand * nBandOffset, eType, nPixelOffset, nLineOffset, FALSE ) ); } /* -------------------------------------------------------------------- */ /* Try to return a regular handle on the file. */ /* -------------------------------------------------------------------- */ CSLDestroy( papszOptions ); return poDS; }
CPLErr MEMDataset::AddBand( GDALDataType eType, char **papszOptions ) { const int nBandId = GetRasterCount() + 1; const GSpacing nPixelSize = GDALGetDataTypeSizeBytes(eType); /* -------------------------------------------------------------------- */ /* Do we need to allocate the memory ourselves? This is the */ /* simple case. */ /* -------------------------------------------------------------------- */ if( CSLFetchNameValue( papszOptions, "DATAPOINTER" ) == NULL ) { const GSpacing nTmp = nPixelSize * GetRasterXSize(); GByte *pData = NULL; #if SIZEOF_VOIDP == 4 if( nTmp > INT_MAX ) pData = NULL; else #endif pData = reinterpret_cast<GByte *>( VSI_CALLOC_VERBOSE((size_t)nTmp, GetRasterYSize() ) ); if( pData == NULL ) { return CE_Failure; } SetBand( nBandId, new MEMRasterBand( this, nBandId, pData, eType, nPixelSize, nPixelSize * GetRasterXSize(), TRUE ) ); return CE_None; } /* -------------------------------------------------------------------- */ /* Get layout of memory and other flags. */ /* -------------------------------------------------------------------- */ const char *pszDataPointer = CSLFetchNameValue(papszOptions, "DATAPOINTER"); GByte *pData = reinterpret_cast<GByte *>( CPLScanPointer( pszDataPointer, static_cast<int>(strlen(pszDataPointer)) ) ); const char *pszOption = CSLFetchNameValue(papszOptions, "PIXELOFFSET"); GSpacing nPixelOffset; if( pszOption == NULL ) nPixelOffset = nPixelSize; else nPixelOffset = CPLAtoGIntBig(pszOption); pszOption = CSLFetchNameValue(papszOptions, "LINEOFFSET"); GSpacing nLineOffset; if( pszOption == NULL ) nLineOffset = GetRasterXSize() * static_cast<size_t>( nPixelOffset ); else nLineOffset = CPLAtoGIntBig(pszOption); SetBand( nBandId, new MEMRasterBand( this, nBandId, pData, eType, nPixelOffset, nLineOffset, FALSE ) ); return CE_None; }