// Read from a RAM Tiff. This is rather generic CPLErr DecompressTIF(buf_mgr &dst, buf_mgr &src, const ILImage &img) { CPLString fname = uniq_memfname("mrf_tif_read"); VSILFILE *fp = VSIFileFromMemBuffer(fname, (GByte *)(src.buffer), src.size, false); // Comes back opened, but we can't use it if (fp) VSIFCloseL(fp); else { CPLError(CE_Failure,CPLE_AppDefined, CPLString().Printf("MRF: TIFF, can't open %s as a temp file", fname.c_str())); return CE_Failure; } GDALDataset *poTiff = reinterpret_cast<GDALDataset*>(GDALOpen(fname, GA_ReadOnly)); if (!fp) { CPLError(CE_Failure,CPLE_AppDefined, CPLString().Printf("MRF: TIFF, can't open page as a Tiff")); return CE_Failure; } CPLErr ret; // Bypass the GDAL caching if (img.pagesize.c == 1) { ret = poTiff->GetRasterBand(1)->ReadBlock(0,0,dst.buffer); } else { ret = poTiff->RasterIO(GF_Read,0,0,img.pagesize.x,img.pagesize.y, dst.buffer, img.pagesize.x, img.pagesize.y, img.dt, img.pagesize.c, NULL, 0,0,0); } GDALClose(poTiff); if (CE_None != ret) return ret; VSIUnlink(fname); return CE_None; }
// // Uses GDAL to create a temporary TIF file, using the band create options // copies the content to the destination buffer then erases the temp TIF // static CPLErr CompressTIF(buf_mgr &dst, buf_mgr &src, const ILImage &img, char **papszOptions) { CPLErr ret; GDALDriver *poTiffDriver = GetGDALDriverManager()->GetDriverByName("GTiff"); VSIStatBufL statb; CPLString fname = uniq_memfname("mrf_tif_write"); GDALDataset *poTiff = poTiffDriver->Create(fname, img.pagesize.x, img.pagesize.y, img.pagesize.c, img.dt, papszOptions ); // Read directly to avoid double caching in GDAL // Unfortunately not possible for multiple bands if (img.pagesize.c == 1) { ret = poTiff->GetRasterBand(1)->WriteBlock(0,0,src.buffer); } else { ret = poTiff->RasterIO(GF_Write, 0,0,img.pagesize.x,img.pagesize.y, src.buffer, img.pagesize.x, img.pagesize.y, img.dt, img.pagesize.c, NULL, 0,0,0 #if GDAL_VERSION_MAJOR >= 2 ,NULL #endif ); } if (CE_None != ret) return ret; GDALClose(poTiff); // Check that we can read the file if (VSIStatL(fname, &statb)) { CPLError(CE_Failure,CPLE_AppDefined, "MRF: TIFF, can't stat %s", fname.c_str()); return CE_Failure; } if (size_t(statb.st_size) > dst.size) { CPLError(CE_Failure,CPLE_AppDefined, "MRF: TIFF, Tiff generated is too large"); return CE_Failure; } VSILFILE *pf = VSIFOpenL(fname,"rb"); if (pf == NULL) { CPLError(CE_Failure,CPLE_AppDefined, "MRF: TIFF, can't open %s", fname.c_str()); return CE_Failure; } VSIFReadL(dst.buffer, static_cast<size_t>(statb.st_size), 1, pf); dst.size = static_cast<size_t>(statb.st_size); VSIFCloseL(pf); VSIUnlink(fname); return CE_None; }
// Read from a RAM Tiff. This is rather generic static CPLErr DecompressTIF(buf_mgr &dst, buf_mgr &src, const ILImage &img) { CPLString fname = uniq_memfname("mrf_tif_read"); VSILFILE *fp = VSIFileFromMemBuffer(fname, (GByte *)(src.buffer), src.size, false); // Comes back opened, but we can't use it if (fp) VSIFCloseL(fp); else { CPLError(CE_Failure,CPLE_AppDefined, "MRF: TIFF, can't open %s as a temp file", fname.c_str()); return CE_Failure; } #if GDAL_VERSION_MAJOR >= 2 const char* const apszAllowedDrivers[] = { "GTiff", NULL }; GDALDataset *poTiff = reinterpret_cast<GDALDataset*>(GDALOpenEx(fname, GDAL_OF_RASTER, apszAllowedDrivers, NULL, NULL)); #else GDALDataset *poTiff = reinterpret_cast<GDALDataset*>(GDALOpen(fname, GA_ReadOnly)); #endif if (poTiff == NULL) { CPLError(CE_Failure,CPLE_AppDefined, "MRF: TIFF, can't open page as a Tiff"); VSIUnlink(fname); return CE_Failure; } CPLErr ret; // Bypass the GDAL caching if (img.pagesize.c == 1) { ret = poTiff->GetRasterBand(1)->ReadBlock(0,0,dst.buffer); } else { ret = poTiff->RasterIO(GF_Read,0,0,img.pagesize.x,img.pagesize.y, dst.buffer, img.pagesize.x, img.pagesize.y, img.dt, img.pagesize.c, NULL, 0,0,0 #if GDAL_VERSION_MAJOR >= 2 ,NULL #endif ); } GDALClose(poTiff); VSIUnlink(fname); return ret; }