SGIDataset::~SGIDataset() { FlushCache(); // Do we need to write out rle table? if( image.rleTableDirty ) { CPLDebug( "SGI", "Flushing RLE offset table." ); ConvertLong( image.rowStart, image.ysize * image.zsize ); ConvertLong( (GUInt32 *) image.rowSize, image.ysize * image.zsize ); VSIFSeekL( fpImage, 512, SEEK_SET ); VSIFWriteL( image.rowStart, 4, image.ysize * image.zsize, fpImage ); VSIFWriteL( image.rowSize, 4, image.ysize * image.zsize, fpImage ); image.rleTableDirty = FALSE; } if(fpImage != NULL) VSIFCloseL(fpImage); CPLFree(image.tmp); CPLFree(image.rowSize); CPLFree(image.rowStart); }
SGIDataset::~SGIDataset() { FlushCache(); // Do we need to write out rle table? if( image.rleTableDirty ) { CPLDebug( "SGI", "Flushing RLE offset table." ); ConvertLong( image.rowStart, image.ysize * image.zsize ); ConvertLong( reinterpret_cast<GUInt32 *>( image.rowSize ), image.ysize * image.zsize ); VSIFSeekL( fpImage, 512, SEEK_SET ); size_t nSize = static_cast<size_t>(image.ysize) * static_cast<size_t>(image.zsize); VSIFWriteL( image.rowStart, 4, nSize, fpImage ); VSIFWriteL( image.rowSize, 4, nSize, fpImage ); image.rleTableDirty = FALSE; } if(fpImage != nullptr) VSIFCloseL(fpImage); CPLFree(image.tmp); CPLFree(image.rowSize); CPLFree(image.rowStart); }
static bool ImageOpen(ImageRec &image, std::istream &is) { image.is = &is; is.read(reinterpret_cast<char*>(&image), 12); if(is.gcount() != 12) return false; bool swapFlag = !osgIsBigEndian(); if(swapFlag == true) ConvertShort(&image.imagic, 6); if((image.type & 0xFF00) == 0x0100) { is.ignore(512 - 12); if (is.gcount() != 512 - 12) return false; int n = image.ysize * image.zsize; int len = n * sizeof(unsigned); image.rowStart.resize(n); is.read(reinterpret_cast<char*>(&(image.rowStart.front())), len); if(is.gcount() != len) return false; image.rowSize.resize(n); is.read(reinterpret_cast<char*>(&(image.rowSize.front())), len); if(is.gcount() != len) return false; if(swapFlag == true) { ConvertLong(&(image.rowStart.front()), n); ConvertLong(&(image.rowSize .front()), n); } unsigned int maxSize = 0; for(int i = 0; i < n; ++i) { if(image.rowSize[i] > maxSize) maxSize = image.rowSize[i]; } image.tmp.resize(maxSize); } else { image.tmp.resize(image.xsize); } return true; }
static rawImageRec *RawImageOpen(char *fileName) { union { int testWord; char testByte[4]; } endianTest; rawImageRec *raw; GLenum swapFlag; int x; endianTest.testWord = 1; if (endianTest.testByte[0] == 1) { swapFlag = GL_TRUE; } else { swapFlag = GL_FALSE; } raw = (rawImageRec *)malloc(sizeof(rawImageRec)); if (raw == NULL) { return NULL; } if ((raw->file = fopen(fileName, "rb")) == NULL) { return NULL; } fread(raw, 1, 12, raw->file); if (swapFlag) { ConvertShort(&raw->imagic, 6); } raw->tmp = (unsigned char *)malloc(raw->sizeX*256); raw->tmpR = (unsigned char *)malloc(raw->sizeX*256); raw->tmpG = (unsigned char *)malloc(raw->sizeX*256); raw->tmpB = (unsigned char *)malloc(raw->sizeX*256); if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL || raw->tmpB == NULL) { return NULL; } if ((raw->type & 0xFF00) == 0x0100) { x = raw->sizeY * raw->sizeZ * sizeof(GLuint); raw->rowStart = (GLuint *)malloc(x); raw->rowSize = (GLint *)malloc(x); if (raw->rowStart == NULL || raw->rowSize == NULL) { return NULL; } raw->rleEnd = 512 + (2 * x); fseek(raw->file, 512, SEEK_SET); fread(raw->rowStart, 1, x, raw->file); fread(raw->rowSize, 1, x, raw->file); if (swapFlag) { ConvertLong(raw->rowStart, x/sizeof(GLuint)); ConvertLong((GLuint *)raw->rowSize, x/sizeof(GLint)); } } return raw; }
static ImageRec *ImageOpen(const char *fileName) { union { int testWord; char testByte[4]; } endianTest; ImageRec *image; int swapFlag; int x; endianTest.testWord = 1; if (endianTest.testByte[0] == 1) { swapFlag = 1; } else { swapFlag = 0; } image = (ImageRec *)malloc(sizeof(ImageRec)); if (image == NULL) { fprintf(stderr, "Out of memory!\n"); exit(1); } if ((image->file = fopen(fileName, "rb")) == NULL) { perror(fileName); exit(1); } fread(image, 1, 12, image->file); if (swapFlag) { ConvertShort(&image->imagic, 6); } image->tmp = (unsigned char *)malloc(image->xsize*256); image->tmpR = (unsigned char *)malloc(image->xsize*256); image->tmpG = (unsigned char *)malloc(image->xsize*256); image->tmpB = (unsigned char *)malloc(image->xsize*256); if (image->tmp == NULL || image->tmpR == NULL || image->tmpG == NULL || image->tmpB == NULL) { fprintf(stderr, "Out of memory!\n"); exit(1); } if ((image->type & 0xFF00) == 0x0100) { x = image->ysize * image->zsize * sizeof(unsigned); image->rowStart = (unsigned *)malloc(x); image->rowSize = (int *)malloc(x); if (image->rowStart == NULL || image->rowSize == NULL) { fprintf(stderr, "Out of memory!\n"); exit(1); } image->rleEnd = 512 + (2 * x); fseek(image->file, 512, SEEK_SET); fread(image->rowStart, 1, x, image->file); fread(image->rowSize, 1, x, image->file); if (swapFlag) { ConvertLong(image->rowStart, x/sizeof(unsigned)); ConvertLong((unsigned *)image->rowSize, x/sizeof(int)); } } return image; }
static rawImageRec *RawImageOpen(const char *fileName) { union { int testWord; char testByte[4]; } endianTest; rawImageRec *raw; GLenum swapFlag; int x; size_t result; endianTest.testWord = 1; if (endianTest.testByte[0] == 1) { swapFlag = GL_TRUE; } else { swapFlag = GL_FALSE; } raw = (rawImageRec *)calloc(1, sizeof(rawImageRec)); if (raw == NULL) { fprintf(stderr, "Out of memory!\n"); return NULL; } raw->file = fopen(fileName, "rb"); if (raw->file == NULL) { const char *baseName = strrchr(fileName, '/'); if(baseName) raw->file = fopen(baseName + 1, "rb"); if(raw->file == NULL) { perror(fileName); free(raw); return NULL; } } result = fread(raw, 1, 12, raw->file); assert(result == 12); if (swapFlag) { ConvertShort(&raw->imagic, 1); ConvertShort(&raw->type, 1); ConvertShort(&raw->dim, 1); ConvertShort(&raw->sizeX, 1); ConvertShort(&raw->sizeY, 1); ConvertShort(&raw->sizeZ, 1); } raw->tmp = (unsigned char *)malloc(raw->sizeX*256); raw->tmpR = (unsigned char *)malloc(raw->sizeX*256); raw->tmpG = (unsigned char *)malloc(raw->sizeX*256); raw->tmpB = (unsigned char *)malloc(raw->sizeX*256); if (raw->sizeZ==4) { raw->tmpA = (unsigned char *)malloc(raw->sizeX*256); } if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL || raw->tmpB == NULL) { fprintf(stderr, "Out of memory!\n"); free(raw->tmp); free(raw->tmpR); free(raw->tmpG); free(raw->tmpB); free(raw->tmpA); free(raw); return NULL; } if ((raw->type & 0xFF00) == 0x0100) { x = raw->sizeY * raw->sizeZ * sizeof(GLuint); raw->rowStart = (GLuint *)malloc(x); raw->rowSize = (GLint *)malloc(x); if (raw->rowStart == NULL || raw->rowSize == NULL) { fprintf(stderr, "Out of memory!\n"); free(raw->tmp); free(raw->tmpR); free(raw->tmpG); free(raw->tmpB); free(raw->tmpA); free(raw->rowStart); free(raw->rowSize); free(raw); return NULL; } raw->rleEnd = 512 + (2 * x); fseek(raw->file, 512, SEEK_SET); result = fread(raw->rowStart, 1, x, raw->file); assert(result == x); result = fread(raw->rowSize, 1, x, raw->file); assert(result == x); if (swapFlag) { ConvertLong(raw->rowStart, (long) (x/sizeof(GLuint))); ConvertLong((GLuint *)raw->rowSize, (long) (x/sizeof(GLint))); } } return raw; }
GDALDataset* SGIDataset::Open(GDALOpenInfo* poOpenInfo) { /* -------------------------------------------------------------------- */ /* First we check to see if the file has the expected header */ /* bytes. */ /* -------------------------------------------------------------------- */ if(poOpenInfo->nHeaderBytes < 12) return NULL; ImageRec tmpImage; memcpy(&tmpImage, poOpenInfo->pabyHeader, 12); tmpImage.Swap(); if(tmpImage.imagic != 474) return NULL; if (tmpImage.type != 0 && tmpImage.type != 1) return NULL; if (tmpImage.bpc != 1 && tmpImage.bpc != 2) return NULL; if (tmpImage.dim != 1 && tmpImage.dim != 2 && tmpImage.dim != 3) return NULL; if(tmpImage.bpc != 1) { CPLError(CE_Failure, CPLE_NotSupported, "The SGI driver only supports 1 byte channel values.\n"); return NULL; } /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ SGIDataset* poDS; poDS = new SGIDataset(); poDS->eAccess = poOpenInfo->eAccess; /* -------------------------------------------------------------------- */ /* Open the file using the large file api. */ /* -------------------------------------------------------------------- */ if( poDS->eAccess == GA_ReadOnly ) poDS->fpImage = VSIFOpenL(poOpenInfo->pszFilename, "rb"); else poDS->fpImage = VSIFOpenL(poOpenInfo->pszFilename, "rb+"); if(poDS->fpImage == NULL) { CPLError(CE_Failure, CPLE_OpenFailed, "VSIFOpenL(%s) failed unexpectedly in sgidataset.cpp\n%s", poOpenInfo->pszFilename, VSIStrerror( errno ) ); delete poDS; return NULL; } /* -------------------------------------------------------------------- */ /* Read pre-image data after ensuring the file is rewound. */ /* -------------------------------------------------------------------- */ VSIFSeekL(poDS->fpImage, 0, SEEK_SET); if(VSIFReadL((void*)(&(poDS->image)), 1, 12, poDS->fpImage) != 12) { CPLError(CE_Failure, CPLE_OpenFailed, "file read error while reading header in sgidataset.cpp"); delete poDS; return NULL; } poDS->image.Swap(); poDS->image.file = poDS->fpImage; poDS->image.fileName = poOpenInfo->pszFilename; /* -------------------------------------------------------------------- */ /* Capture some information from the file that is of interest. */ /* -------------------------------------------------------------------- */ poDS->nRasterXSize = poDS->image.xsize; poDS->nRasterYSize = poDS->image.ysize; if (poDS->nRasterXSize <= 0 || poDS->nRasterYSize <= 0) { CPLError(CE_Failure, CPLE_OpenFailed, "Invalid image dimensions : %d x %d", poDS->nRasterXSize, poDS->nRasterYSize); delete poDS; return NULL; } poDS->nBands = MAX(1,poDS->image.zsize); if (poDS->nBands > 256) { CPLError(CE_Failure, CPLE_OpenFailed, "Too many bands : %d", poDS->nBands); delete poDS; return NULL; } int numItems = (int(poDS->image.bpc) == 1) ? 256 : 65536; poDS->image.tmp = (unsigned char*)VSICalloc(poDS->image.xsize,numItems); if (poDS->image.tmp == NULL) { CPLError(CE_Failure, CPLE_OutOfMemory, "Out of memory"); delete poDS; return NULL; } /* -------------------------------------------------------------------- */ /* Read RLE Pointer tables. */ /* -------------------------------------------------------------------- */ if(int(poDS->image.type) == 1) // RLE compressed { int x = poDS->image.ysize * poDS->nBands * sizeof(GUInt32); poDS->image.rowStart = (GUInt32*)VSIMalloc2(poDS->image.ysize, poDS->nBands * sizeof(GUInt32)); poDS->image.rowSize = (GInt32*)VSIMalloc2(poDS->image.ysize, poDS->nBands * sizeof(GUInt32)); if (poDS->image.rowStart == NULL || poDS->image.rowSize == NULL) { CPLError(CE_Failure, CPLE_OutOfMemory, "Out of memory"); delete poDS; return NULL; } memset(poDS->image.rowStart, 0, x); memset(poDS->image.rowSize, 0, x); poDS->image.rleEnd = 512 + (2 * x); VSIFSeekL(poDS->fpImage, 512, SEEK_SET); if((int) VSIFReadL(poDS->image.rowStart, 1, x, poDS->image.file) != x) { delete poDS; CPLError(CE_Failure, CPLE_OpenFailed, "file read error while reading start positions in sgidataset.cpp"); return NULL; } if((int) VSIFReadL(poDS->image.rowSize, 1, x, poDS->image.file) != x) { delete poDS; CPLError(CE_Failure, CPLE_OpenFailed, "file read error while reading row lengths in sgidataset.cpp"); return NULL; } ConvertLong(poDS->image.rowStart, x/(int)sizeof(GUInt32)); ConvertLong((GUInt32*)poDS->image.rowSize, x/(int)sizeof(GInt32)); } else // uncompressed. { poDS->image.rowStart = NULL; poDS->image.rowSize = NULL; } /* -------------------------------------------------------------------- */ /* Create band information objects. */ /* -------------------------------------------------------------------- */ for(int iBand = 0; iBand < poDS->nBands; iBand++) poDS->SetBand(iBand+1, new SGIRasterBand(poDS, iBand+1)); /* -------------------------------------------------------------------- */ /* Check for world file. */ /* -------------------------------------------------------------------- */ poDS->bGeoTransformValid = GDALReadWorldFile(poOpenInfo->pszFilename, ".wld", poDS->adfGeoTransform); /* -------------------------------------------------------------------- */ /* Initialize any PAM information. */ /* -------------------------------------------------------------------- */ poDS->SetDescription(poOpenInfo->pszFilename); poDS->TryLoadXML(); /* -------------------------------------------------------------------- */ /* Check for overviews. */ /* -------------------------------------------------------------------- */ poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename ); return poDS; }
static rawImageRec *RawImageOpen(char *fileName) { union { int testWord; char testByte[4]; } endianTest; rawImageRec *raw; int swapFlag; int x; endianTest.testWord = 1; if (endianTest.testByte[0] == 1) { swapFlag = 1; } else { swapFlag = 0; } /* 23Oct06 Phil McDonald */ raw = (rawImageRec *) TMP_CALLOC (1, sizeof (rawImageRec)); /* end PM */ if (raw == NULL) { fprintf(stderr, "Out of memory!\n"); return NULL; } if ((raw->file = fopen(fileName, "rb")) == NULL) { perror(fileName); return NULL; } fread(raw, 1, 12, raw->file); if (swapFlag) { ConvertShort(&raw->imagic, 6); } /* 23Oct06 Phil McDonald */ raw->tmp = (unsigned char *) TMP_MALLOC (raw->sizeX*256); raw->tmpR = (unsigned char *) TMP_MALLOC (raw->sizeX*256); raw->tmpG = (unsigned char *) TMP_MALLOC (raw->sizeX*256); raw->tmpB = (unsigned char *) TMP_MALLOC (raw->sizeX*256); if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL || raw->tmpB == NULL) { fprintf(stderr, "Out of memory!\n"); RawImageClose (raw); /* end PM */ return NULL; } if ((raw->type & 0xFF00) == 0x0100) { x = raw->sizeY * raw->sizeZ * sizeof(unsigned int); /* 23Oct06 Phil McDonald */ raw->rowStart = (unsigned int *) TMP_MALLOC (x); raw->rowSize = (int *) TMP_MALLOC (x); if (raw->rowStart == NULL || raw->rowSize == NULL) { fprintf(stderr, "Out of memory!\n"); RawImageClose (raw); /* end PM */ return NULL; } raw->rleEnd = 512 + (2 * x); fseek(raw->file, 512, SEEK_SET); fread(raw->rowStart, 1, x, raw->file); fread(raw->rowSize, 1, x, raw->file); if (swapFlag) { ConvertLong(raw->rowStart, x/sizeof(unsigned int)); ConvertLong((unsigned int *)raw->rowSize, x/sizeof(int)); } } return raw; }
GDALDataset* SGIDataset::Open(GDALOpenInfo* poOpenInfo) { /* -------------------------------------------------------------------- */ /* First we check to see if the file has the expected header */ /* bytes. */ /* -------------------------------------------------------------------- */ if(poOpenInfo->nHeaderBytes < 12 || poOpenInfo->fpL == nullptr ) return nullptr; ImageRec tmpImage; memcpy(&tmpImage.imagic, poOpenInfo->pabyHeader + 0, 2); memcpy(&tmpImage.type, poOpenInfo->pabyHeader + 2, 1); memcpy(&tmpImage.bpc, poOpenInfo->pabyHeader + 3, 1); memcpy(&tmpImage.dim, poOpenInfo->pabyHeader + 4, 2); memcpy(&tmpImage.xsize, poOpenInfo->pabyHeader + 6, 2); memcpy(&tmpImage.ysize, poOpenInfo->pabyHeader + 8, 2); memcpy(&tmpImage.zsize, poOpenInfo->pabyHeader + 10, 2); tmpImage.Swap(); if(tmpImage.imagic != 474) return nullptr; if (tmpImage.type != 0 && tmpImage.type != 1) return nullptr; if (tmpImage.bpc != 1 && tmpImage.bpc != 2) return nullptr; if (tmpImage.dim != 1 && tmpImage.dim != 2 && tmpImage.dim != 3) return nullptr; if(tmpImage.bpc != 1) { CPLError(CE_Failure, CPLE_NotSupported, "The SGI driver only supports 1 byte channel values.\n"); return nullptr; } /* -------------------------------------------------------------------- */ /* Create a corresponding GDALDataset. */ /* -------------------------------------------------------------------- */ SGIDataset* poDS = new SGIDataset(); poDS->eAccess = poOpenInfo->eAccess; poDS->fpImage = poOpenInfo->fpL; poOpenInfo->fpL = nullptr; /* -------------------------------------------------------------------- */ /* Read pre-image data after ensuring the file is rewound. */ /* -------------------------------------------------------------------- */ VSIFSeekL(poDS->fpImage, 0, SEEK_SET); if(VSIFReadL(reinterpret_cast<void*>( &(poDS->image) ), 1, 12, poDS->fpImage) != 12) { CPLError(CE_Failure, CPLE_OpenFailed, "file read error while reading header in sgidataset.cpp"); delete poDS; return nullptr; } poDS->image.Swap(); poDS->image.file = poDS->fpImage; poDS->image.fileName = poOpenInfo->pszFilename; /* -------------------------------------------------------------------- */ /* Capture some information from the file that is of interest. */ /* -------------------------------------------------------------------- */ poDS->nRasterXSize = poDS->image.xsize; poDS->nRasterYSize = poDS->image.ysize; if (poDS->nRasterXSize <= 0 || poDS->nRasterYSize <= 0) { CPLError(CE_Failure, CPLE_OpenFailed, "Invalid image dimensions : %d x %d", poDS->nRasterXSize, poDS->nRasterYSize); delete poDS; return nullptr; } poDS->nBands = std::max(static_cast<GUInt16>(1), poDS->image.zsize); if (poDS->nBands > 256) { CPLError(CE_Failure, CPLE_OpenFailed, "Too many bands : %d", poDS->nBands); delete poDS; return nullptr; } const int numItems = (static_cast<int>( poDS->image.bpc ) == 1) ? 256 : 65536; if( poDS->image.xsize > INT_MAX / numItems ) { delete poDS; return nullptr; } poDS->image.tmpSize = poDS->image.xsize * numItems; poDS->image.tmp = (unsigned char*)VSI_CALLOC_VERBOSE(poDS->image.xsize,numItems); if (poDS->image.tmp == nullptr) { delete poDS; return nullptr; } /* -------------------------------------------------------------------- */ /* Read RLE Pointer tables. */ /* -------------------------------------------------------------------- */ if( static_cast<int>( poDS->image.type ) == 1 ) // RLE compressed { const size_t x = static_cast<size_t>(poDS->image.ysize) * poDS->nBands * sizeof(GUInt32); poDS->image.rowStart = reinterpret_cast<GUInt32*>( VSI_MALLOC2_VERBOSE(poDS->image.ysize, poDS->nBands * sizeof(GUInt32) ) ); poDS->image.rowSize = reinterpret_cast<GInt32 *>( VSI_MALLOC2_VERBOSE(poDS->image.ysize, poDS->nBands * sizeof(GUInt32) ) ); if (poDS->image.rowStart == nullptr || poDS->image.rowSize == nullptr) { delete poDS; return nullptr; } memset(poDS->image.rowStart, 0, x); memset(poDS->image.rowSize, 0, x); poDS->image.rleEnd = static_cast<GUInt32>(512 + (2 * x)); VSIFSeekL(poDS->fpImage, 512, SEEK_SET); if( VSIFReadL(poDS->image.rowStart, 1, x, poDS->image.file ) != x ) { delete poDS; CPLError(CE_Failure, CPLE_OpenFailed, "file read error while reading start positions in sgidataset.cpp"); return nullptr; } if( VSIFReadL(poDS->image.rowSize, 1, x, poDS->image.file) != x) { delete poDS; CPLError(CE_Failure, CPLE_OpenFailed, "file read error while reading row lengths in sgidataset.cpp"); return nullptr; } ConvertLong(poDS->image.rowStart, static_cast<int>(x / static_cast<int>( sizeof(GUInt32))) ); ConvertLong(reinterpret_cast<GUInt32 *>( poDS->image.rowSize ), static_cast<int>(x / static_cast<int>( sizeof(GInt32) )) ); } else // uncompressed. { poDS->image.rowStart = nullptr; poDS->image.rowSize = nullptr; } /* -------------------------------------------------------------------- */ /* Create band information objects. */ /* -------------------------------------------------------------------- */ for(int iBand = 0; iBand < poDS->nBands; iBand++) poDS->SetBand(iBand+1, new SGIRasterBand(poDS, iBand+1)); /* -------------------------------------------------------------------- */ /* Check for world file. */ /* -------------------------------------------------------------------- */ poDS->bGeoTransformValid = GDALReadWorldFile(poOpenInfo->pszFilename, ".wld", poDS->adfGeoTransform); /* -------------------------------------------------------------------- */ /* Initialize any PAM information. */ /* -------------------------------------------------------------------- */ poDS->SetDescription(poOpenInfo->pszFilename); poDS->TryLoadXML(); /* -------------------------------------------------------------------- */ /* Check for overviews. */ /* -------------------------------------------------------------------- */ poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename ); return poDS; }