int main() { char *fname = "newgeo.tif"; TIFF *tif=(TIFF*)0; /* TIFF-level descriptor */ GTIF *gtif=(GTIF*)0; /* GeoKey-level descriptor */ tif=XTIFFOpen(fname,"w"); if (!tif) goto failure; gtif = GTIFNew(tif); if (!gtif) { printf("failed in GTIFNew\n"); goto failure; } SetUpTIFFDirectory(tif); SetUpGeoKeys(gtif); WriteImage(tif); GTIFWriteKeys(gtif); GTIFFree(gtif); XTIFFClose(tif); return 0; failure: printf("failure in makegeo\n"); if (tif) TIFFClose(tif); if (gtif) GTIFFree(gtif); return -1; }
void set_geotif_header(char *tiff_fname, char *utm_zone, float xoff, float yoff, float scale) { // open tiff file TIFF *tif = XTIFFOpen(tiff_fname, "r+"); if (!tif) fail("failed in XTIFFOpen\n"); GTIF *gtif = GTIFNew(tif); if (!gtif) fail("failed in GTIFNew\n"); // write TIFF tags double pixsize[3] = {scale, scale, 0.0}; TIFFSetField(tif, GTIFF_PIXELSCALE, 3, pixsize); double tiepoint[6] = {0.0, 0.0, 0.0, xoff, yoff, 0.0}; TIFFSetField(tif, GTIFF_TIEPOINTS, 6, tiepoint); // write GEOTIFF keys int utm_ind = get_utm_zone_index_for_geotiff(utm_zone); GTIFKeySet(gtif, ProjectedCSTypeGeoKey, TYPE_SHORT, 1, utm_ind); GTIFWriteKeys(gtif); // free and close GTIFFree(gtif); XTIFFClose(tif); }
// Returns true if georeferenced ...all geocoded and georeferenced GeoTIFFs qualify int isGeotiff(const char *file) { TIFF *tiff = NULL; GTIF *gtif = NULL; int num_tie_points = 0; int num_pixel_scales = 0; double *tie_point = NULL; double *pixel_scale = NULL; tiff = XTIFFOpen (file, "r"); if (tiff != NULL) { gtif = GTIFNew (tiff); } if (gtif) { (gtif->gt_methods.get)(gtif->gt_tif, GTIFF_TIEPOINTS, &num_tie_points, &tie_point); (gtif->gt_methods.get)(gtif->gt_tif, GTIFF_PIXELSCALE, &num_pixel_scales, &pixel_scale); } if (gtif && num_tie_points == 6 && num_pixel_scales == 3 && pixel_scale[0] > 0.0 && pixel_scale[1] > 0.0) { GTIFFree(gtif); XTIFFClose(tiff); FREE(tie_point); FREE(pixel_scale); return 1; } if (gtif) GTIFFree(gtif); if (tiff) XTIFFClose(tiff); FREE(tie_point); FREE(pixel_scale); return 0; }
bool FetchTIFFCorners(const char * inFileName, double corners[8], int& post_pos) { bool retVal = false; TIFF * tiffFile = XTIFFOpen(inFileName, "r"); if (tiffFile) { retVal = FetchTIFFCornersWithTIFF(tiffFile, corners, post_pos); XTIFFClose(tiffFile); } return retVal; }
void main() { TIFF *tif=(TIFF*)0; /* TIFF-level descriptor */ tif=XTIFFOpen("newtif.tif","w"); if (!tif) goto failure; SetUpTIFFDirectory(tif); WriteImage(tif); XTIFFClose(tif); exit (0); failure: printf("failure in maketif\n"); if (tif) XTIFFClose(tif); exit (-1); }
int isgeotiff(char *inFile) { FILE *fp; TIFF *tiff=NULL; GTIF *gtif=NULL; int isGeotiff = 0; char magic[2]; fp = fopen(inFile, "r"); if (!fp) { char n[1024]; sprintf(n, "%s.tif", inFile); fp = fopen(n, "r"); } if (!fp) { char *basename = get_basename(inFile); char n[1024]; sprintf(n, "%s.tif", basename); fp = fopen(n, "r"); FREE(basename); } if (!fp) { char *basename = get_basename(inFile); char n[1024]; sprintf(n, "%s.tiff", basename); fp = fopen(n, "r"); FREE(basename); } if (fp) { magic[0] = fgetc(fp); magic[1] = fgetc(fp); if ((magic[0] == 'I' && magic[1] == 'I') || (magic[0] == 'M' && magic[1] == 'M') ) { // Looks like a TIFF file, so let's see if it's a GeoTIFF file tiff = XTIFFOpen(inFile, "r"); if (!tiff) isGeotiff = 0; // Not a TIFF (or GeoTIFF) file ...rare if the magic bytes were correct. if (tiff) gtif = GTIFNew(tiff); if (tiff && !gtif) isGeotiff = 0; // Usually GTIFNew() succeeds even if not a GeoTIFF ... if (tiff && gtif) { double *tie_point=NULL; double *pixel_scale=NULL; int tp_count=0, ps_count=0; (gtif->gt_methods.get)(gtif->gt_tif, GTIFF_TIEPOINTS, &tp_count, &tie_point); (gtif->gt_methods.get)(gtif->gt_tif, GTIFF_PIXELSCALE, &ps_count, &pixel_scale); if (tie_point && pixel_scale && tp_count == 6 && ps_count == 3) { // Assume that if tie points and pixel scales exist that the file is a // GeoTIFF ...it's still possible that read_generic_geotiff_metadata() will // fail due to the file only being a georeferenced rather than a geocoded // GeoTIFF, but that's a different problem... isGeotiff = 1; } FREE(tie_point); FREE(pixel_scale); } if (gtif) GTIFFree(gtif); if (tiff) XTIFFClose(tiff); } fclose(fp); } return isGeotiff; }
CPLErr GTIFFBuildOverviews( const char * pszFilename, int nBands, GDALRasterBand **papoBandList, int nOverviews, int * panOverviewList, const char * pszResampling, GDALProgressFunc pfnProgress, void * pProgressData ) { TIFF *hOTIFF; int nBitsPerPixel=0, nCompression=COMPRESSION_NONE, nPhotometric=0; int nSampleFormat=0, nPlanarConfig, iOverview, iBand; int nXSize=0, nYSize=0; if( nBands == 0 || nOverviews == 0 ) return CE_None; GTiffOneTimeInit(); /* -------------------------------------------------------------------- */ /* Verify that the list of bands is suitable for emitting in */ /* TIFF file. */ /* -------------------------------------------------------------------- */ for( iBand = 0; iBand < nBands; iBand++ ) { int nBandBits, nBandFormat; GDALRasterBand *hBand = papoBandList[iBand]; switch( hBand->GetRasterDataType() ) { case GDT_Byte: nBandBits = 8; nBandFormat = SAMPLEFORMAT_UINT; break; case GDT_UInt16: nBandBits = 16; nBandFormat = SAMPLEFORMAT_UINT; break; case GDT_Int16: nBandBits = 16; nBandFormat = SAMPLEFORMAT_INT; break; case GDT_UInt32: nBandBits = 32; nBandFormat = SAMPLEFORMAT_UINT; break; case GDT_Int32: nBandBits = 32; nBandFormat = SAMPLEFORMAT_INT; break; case GDT_Float32: nBandBits = 32; nBandFormat = SAMPLEFORMAT_IEEEFP; break; case GDT_Float64: nBandBits = 64; nBandFormat = SAMPLEFORMAT_IEEEFP; break; case GDT_CInt16: nBandBits = 32; nBandFormat = SAMPLEFORMAT_COMPLEXINT; break; case GDT_CFloat32: nBandBits = 64; nBandFormat = SAMPLEFORMAT_COMPLEXIEEEFP; break; case GDT_CFloat64: nBandBits = 128; nBandFormat = SAMPLEFORMAT_COMPLEXIEEEFP; break; default: CPLAssert( FALSE ); return CE_Failure; } if( iBand == 0 ) { nBitsPerPixel = nBandBits; nSampleFormat = nBandFormat; nXSize = hBand->GetXSize(); nYSize = hBand->GetYSize(); } else if( nBitsPerPixel != nBandBits || nSampleFormat != nBandFormat ) { CPLError( CE_Failure, CPLE_NotSupported, "GTIFFBuildOverviews() doesn't support a mixture of band" " data types." ); return CE_Failure; } else if( hBand->GetColorTable() != NULL ) { CPLError( CE_Failure, CPLE_NotSupported, "GTIFFBuildOverviews() doesn't support building" " overviews of multiple colormapped bands." ); return CE_Failure; } else if( hBand->GetXSize() != nXSize || hBand->GetYSize() != nYSize ) { CPLError( CE_Failure, CPLE_NotSupported, "GTIFFBuildOverviews() doesn't support building" " overviews of different sized bands." ); return CE_Failure; } } /* -------------------------------------------------------------------- */ /* Use specified compression method. */ /* -------------------------------------------------------------------- */ const char *pszCompress = CPLGetConfigOption( "COMPRESS_OVERVIEW", NULL ); if( pszCompress != NULL && pszCompress[0] != '\0' ) { if( EQUAL( pszCompress, "JPEG" ) ) nCompression = COMPRESSION_JPEG; else if( EQUAL( pszCompress, "LZW" ) ) nCompression = COMPRESSION_LZW; else if( EQUAL( pszCompress, "PACKBITS" )) nCompression = COMPRESSION_PACKBITS; else if( EQUAL( pszCompress, "DEFLATE" ) || EQUAL( pszCompress, "ZIP" )) nCompression = COMPRESSION_ADOBE_DEFLATE; else CPLError( CE_Warning, CPLE_IllegalArg, "COMPRESS_OVERVIEW=%s value not recognised, ignoring.", pszCompress ); } /* -------------------------------------------------------------------- */ /* Figure out the planar configuration to use. */ /* -------------------------------------------------------------------- */ if( nBands == 1 ) nPlanarConfig = PLANARCONFIG_CONTIG; else nPlanarConfig = PLANARCONFIG_SEPARATE; const char* pszInterleave = CPLGetConfigOption( "INTERLEAVE_OVERVIEW", NULL ); if (pszInterleave != NULL && pszInterleave[0] != '\0') { if( EQUAL( pszInterleave, "PIXEL" ) ) nPlanarConfig = PLANARCONFIG_CONTIG; else if( EQUAL( pszInterleave, "BAND" ) ) nPlanarConfig = PLANARCONFIG_SEPARATE; else { CPLError( CE_Failure, CPLE_AppDefined, "INTERLEAVE_OVERVIEW=%s unsupported, value must be PIXEL or BAND. ignoring", pszInterleave ); } } /* -------------------------------------------------------------------- */ /* Figure out the photometric interpretation to use. */ /* -------------------------------------------------------------------- */ if( nBands == 3 ) nPhotometric = PHOTOMETRIC_RGB; else if( papoBandList[0]->GetColorTable() != NULL && !EQUALN(pszResampling,"AVERAGE_BIT2",12) ) { nPhotometric = PHOTOMETRIC_PALETTE; /* should set the colormap up at this point too! */ } else nPhotometric = PHOTOMETRIC_MINISBLACK; const char* pszPhotometric = CPLGetConfigOption( "PHOTOMETRIC_OVERVIEW", NULL ); if (pszPhotometric != NULL && pszPhotometric[0] != '\0') { if( EQUAL( pszPhotometric, "MINISBLACK" ) ) nPhotometric = PHOTOMETRIC_MINISBLACK; else if( EQUAL( pszPhotometric, "MINISWHITE" ) ) nPhotometric = PHOTOMETRIC_MINISWHITE; else if( EQUAL( pszPhotometric, "RGB" )) { nPhotometric = PHOTOMETRIC_RGB; } else if( EQUAL( pszPhotometric, "CMYK" )) { nPhotometric = PHOTOMETRIC_SEPARATED; } else if( EQUAL( pszPhotometric, "YCBCR" )) { nPhotometric = PHOTOMETRIC_YCBCR; } else if( EQUAL( pszPhotometric, "CIELAB" )) { nPhotometric = PHOTOMETRIC_CIELAB; } else if( EQUAL( pszPhotometric, "ICCLAB" )) { nPhotometric = PHOTOMETRIC_ICCLAB; } else if( EQUAL( pszPhotometric, "ITULAB" )) { nPhotometric = PHOTOMETRIC_ITULAB; } else { CPLError( CE_Warning, CPLE_IllegalArg, "PHOTOMETRIC_OVERVIEW=%s value not recognised, ignoring.\n", pszPhotometric ); } } /* -------------------------------------------------------------------- */ /* Create the file, if it does not already exist. */ /* -------------------------------------------------------------------- */ VSIStatBuf sStatBuf; if( VSIStat( pszFilename, &sStatBuf ) != 0 ) { hOTIFF = XTIFFOpen( pszFilename, "w+" ); if( hOTIFF == NULL ) { if( CPLGetLastErrorNo() == 0 ) CPLError( CE_Failure, CPLE_OpenFailed, "Attempt to create new tiff file `%s'\n" "failed in XTIFFOpen().\n", pszFilename ); return CE_Failure; } } /* -------------------------------------------------------------------- */ /* Otherwise just open it for update access. */ /* -------------------------------------------------------------------- */ else { hOTIFF = XTIFFOpen( pszFilename, "r+" ); if( hOTIFF == NULL ) { if( CPLGetLastErrorNo() == 0 ) CPLError( CE_Failure, CPLE_OpenFailed, "Attempt to create new tiff file `%s'\n" "failed in XTIFFOpen().\n", pszFilename ); return CE_Failure; } } /* -------------------------------------------------------------------- */ /* Do we have a palette? If so, create a TIFF compatible version. */ /* -------------------------------------------------------------------- */ unsigned short *panRed=NULL, *panGreen=NULL, *panBlue=NULL; if( nPhotometric == PHOTOMETRIC_PALETTE ) { GDALColorTable *poCT = papoBandList[0]->GetColorTable(); int nColorCount; if( nBitsPerPixel <= 8 ) nColorCount = 256; else nColorCount = 65536; panRed = (unsigned short *) CPLCalloc(nColorCount,sizeof(unsigned short)); panGreen = (unsigned short *) CPLCalloc(nColorCount,sizeof(unsigned short)); panBlue = (unsigned short *) CPLCalloc(nColorCount,sizeof(unsigned short)); for( int iColor = 0; iColor < nColorCount; iColor++ ) { GDALColorEntry sRGB; if( poCT->GetColorEntryAsRGB( iColor, &sRGB ) ) { panRed[iColor] = (unsigned short) (257 * sRGB.c1); panGreen[iColor] = (unsigned short) (257 * sRGB.c2); panBlue[iColor] = (unsigned short) (257 * sRGB.c3); } } } /* -------------------------------------------------------------------- */ /* Do we need some metadata for the overviews? */ /* -------------------------------------------------------------------- */ CPLString osMetadata; GDALDataset *poBaseDS = papoBandList[0]->GetDataset(); GTIFFBuildOverviewMetadata( pszResampling, poBaseDS, osMetadata ); /* -------------------------------------------------------------------- */ /* Loop, creating overviews. */ /* -------------------------------------------------------------------- */ for( iOverview = 0; iOverview < nOverviews; iOverview++ ) { int nOXSize, nOYSize; uint32 nDirOffset; nOXSize = (nXSize + panOverviewList[iOverview] - 1) / panOverviewList[iOverview]; nOYSize = (nYSize + panOverviewList[iOverview] - 1) / panOverviewList[iOverview]; nDirOffset = GTIFFWriteDirectory(hOTIFF, FILETYPE_REDUCEDIMAGE, nOXSize, nOYSize, nBitsPerPixel, nPlanarConfig, nBands, 128, 128, TRUE, nCompression, nPhotometric, nSampleFormat, panRed, panGreen, panBlue, 0, NULL, /* FIXME ? how can we fetch extrasamples from here */ osMetadata ); } if (panRed) { CPLFree(panRed); CPLFree(panGreen); CPLFree(panBlue); panRed = panGreen = panBlue = NULL; } XTIFFClose( hOTIFF ); /* -------------------------------------------------------------------- */ /* Open the overview dataset so that we can get at the overview */ /* bands. */ /* -------------------------------------------------------------------- */ GDALDataset *hODS; hODS = (GDALDataset *) GDALOpen( pszFilename, GA_Update ); if( hODS == NULL ) return CE_Failure; /* -------------------------------------------------------------------- */ /* Loop writing overview data. */ /* -------------------------------------------------------------------- */ if (nCompression != COMPRESSION_NONE && nPlanarConfig == PLANARCONFIG_CONTIG && GDALDataTypeIsComplex(papoBandList[0]->GetRasterDataType()) == FALSE && papoBandList[0]->GetColorTable() == NULL && (EQUALN(pszResampling, "NEAR", 4) || EQUAL(pszResampling, "AVERAGE") || EQUAL(pszResampling, "GAUSS"))) { /* In the case of pixel interleaved compressed overviews, we want to generate */ /* the overviews for all the bands block by block, and not band after band, */ /* in order to write the block once and not loose space in the TIFF file */ GDALRasterBand ***papapoOverviewBands; papapoOverviewBands = (GDALRasterBand ***) CPLCalloc(sizeof(void*),nBands); for( iBand = 0; iBand < nBands; iBand++ ) { GDALRasterBand *hDstBand = hODS->GetRasterBand( iBand+1 ); papapoOverviewBands[iBand] = (GDALRasterBand **) CPLCalloc(sizeof(void*),nOverviews); papapoOverviewBands[iBand][0] = hDstBand; for( int i = 0; i < nOverviews-1; i++ ) { papapoOverviewBands[iBand][i+1] = hDstBand->GetOverview(i); } } GDALRegenerateOverviewsMultiBand(nBands, papoBandList, nOverviews, papapoOverviewBands, pszResampling, pfnProgress, pProgressData ); for( iBand = 0; iBand < nBands; iBand++ ) { CPLFree(papapoOverviewBands[iBand]); } CPLFree(papapoOverviewBands); } else { GDALRasterBand **papoOverviews; papoOverviews = (GDALRasterBand **) CPLCalloc(sizeof(void*),128); for( iBand = 0; iBand < nBands; iBand++ ) { GDALRasterBand *hSrcBand = papoBandList[iBand]; GDALRasterBand *hDstBand; int nDstOverviews; CPLErr eErr; hDstBand = hODS->GetRasterBand( iBand+1 ); papoOverviews[0] = hDstBand; nDstOverviews = hDstBand->GetOverviewCount() + 1; CPLAssert( nDstOverviews < 128 ); nDstOverviews = MIN(128,nDstOverviews); for( int i = 0; i < nDstOverviews-1; i++ ) { papoOverviews[i+1] = hDstBand->GetOverview(i); } void *pScaledProgressData; pScaledProgressData = GDALCreateScaledProgress( iBand / (double) nBands, (iBand+1) / (double) nBands, pfnProgress, pProgressData ); eErr = GDALRegenerateOverviews( (GDALRasterBandH) hSrcBand, nDstOverviews, (GDALRasterBandH *) papoOverviews, pszResampling, GDALScaledProgress, pScaledProgressData); GDALDestroyScaledProgress( pScaledProgressData ); if( eErr != CE_None ) { delete hODS; return eErr; } } CPLFree( papoOverviews ); } /* -------------------------------------------------------------------- */ /* Cleanup */ /* -------------------------------------------------------------------- */ hODS->FlushCache(); delete hODS; pfnProgress( 1.0, NULL, pProgressData ); return CE_None; }
int main(int argc, char *argv[]) { char *fname = NULL; TIFF *tif=(TIFF*)0; /* TIFF-level descriptor */ GTIF *gtif=(GTIF*)0; /* GeoKey-level descriptor */ int i, norm_print_flag = 1, proj4_print_flag = 0; int tfw_flag = 0, inv_flag = 0, dec_flag = 0; /* * Handle command line options. */ for( i = 1; i < argc; i++ ) { if( strcmp(argv[i],"-no_norm") == 0 ) norm_print_flag = 0; else if( strcmp(argv[i],"-t") == 0 ) { CSVDirName = argv[++i]; SetCSVFilenameHook( CSVFileOverride ); } else if( strcmp(argv[i],"-tfw") == 0 ) tfw_flag = 1; else if( strcmp(argv[i],"-proj4") == 0 ) proj4_print_flag = 1; else if( strcmp(argv[i],"-i") == 0 ) inv_flag = 1; else if( strcmp(argv[i],"-d") == 0 ) dec_flag = 1; else if( fname == NULL && argv[i][0] != '-' ) fname = argv[i]; else { Usage(); } } if( fname == NULL ) Usage(); /* * Open the file, read the GeoTIFF information, and print to stdout. */ tif=XTIFFOpen(fname,"r"); if (!tif) goto failure; gtif = GTIFNew(tif); if (!gtif) { fprintf(stderr,"failed in GTIFNew\n"); goto failure; } if( tfw_flag ) { WriteTFWFile( gtif, fname ); goto Success; } /* dump the GeoTIFF metadata to std out */ GTIFPrint(gtif,0,0); /* * Capture, and report normalized information if requested. */ if( norm_print_flag ) { GTIFDefn defn; if( GTIFGetDefn( gtif, &defn ) ) { int xsize, ysize; printf( "\n" ); GTIFPrintDefn( &defn, stdout ); if( proj4_print_flag ) { printf( "\n" ); printf( "PROJ.4 Definition: %s\n", GTIFGetProj4Defn(&defn)); } TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &xsize ); TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &ysize ); GTIFPrintCorners( gtif, &defn, stdout, xsize, ysize, inv_flag, dec_flag ); } } Success: GTIFFree(gtif); XTIFFClose(tif); return 0; failure: fprintf(stderr,"failure in listgeo\n"); if (tif) XTIFFClose(tif); if (gtif) GTIFFree(gtif); return 1; }
CPLErr GTIFFBuildOverviews( const char * pszFilename, int nBands, GDALRasterBand **papoBandList, int nOverviews, int * panOverviewList, const char * pszResampling, GDALProgressFunc pfnProgress, void * pProgressData ) { TIFF *hOTIFF; int nBitsPerPixel=0, nCompression=COMPRESSION_NONE, nPhotometric=0; int nSampleFormat=0, nPlanarConfig, iOverview, iBand; int nXSize=0, nYSize=0; if( nBands == 0 || nOverviews == 0 ) return CE_None; if (!GTiffOneTimeInit()) return CE_Failure; /* -------------------------------------------------------------------- */ /* Verify that the list of bands is suitable for emitting in */ /* TIFF file. */ /* -------------------------------------------------------------------- */ for( iBand = 0; iBand < nBands; iBand++ ) { int nBandBits, nBandFormat; GDALRasterBand *hBand = papoBandList[iBand]; switch( hBand->GetRasterDataType() ) { case GDT_Byte: nBandBits = 8; nBandFormat = SAMPLEFORMAT_UINT; break; case GDT_UInt16: nBandBits = 16; nBandFormat = SAMPLEFORMAT_UINT; break; case GDT_Int16: nBandBits = 16; nBandFormat = SAMPLEFORMAT_INT; break; case GDT_UInt32: nBandBits = 32; nBandFormat = SAMPLEFORMAT_UINT; break; case GDT_Int32: nBandBits = 32; nBandFormat = SAMPLEFORMAT_INT; break; case GDT_Float32: nBandBits = 32; nBandFormat = SAMPLEFORMAT_IEEEFP; break; case GDT_Float64: nBandBits = 64; nBandFormat = SAMPLEFORMAT_IEEEFP; break; case GDT_CInt16: nBandBits = 32; nBandFormat = SAMPLEFORMAT_COMPLEXINT; break; case GDT_CInt32: nBandBits = 64; nBandFormat = SAMPLEFORMAT_COMPLEXINT; break; case GDT_CFloat32: nBandBits = 64; nBandFormat = SAMPLEFORMAT_COMPLEXIEEEFP; break; case GDT_CFloat64: nBandBits = 128; nBandFormat = SAMPLEFORMAT_COMPLEXIEEEFP; break; default: CPLAssert( FALSE ); return CE_Failure; } if( hBand->GetMetadataItem( "NBITS", "IMAGE_STRUCTURE" ) ) { nBandBits = atoi(hBand->GetMetadataItem("NBITS","IMAGE_STRUCTURE")); if( nBandBits == 1 && EQUALN(pszResampling,"AVERAGE_BIT2",12) ) nBandBits = 8; } if( iBand == 0 ) { nBitsPerPixel = nBandBits; nSampleFormat = nBandFormat; nXSize = hBand->GetXSize(); nYSize = hBand->GetYSize(); } else if( nBitsPerPixel != nBandBits || nSampleFormat != nBandFormat ) { CPLError( CE_Failure, CPLE_NotSupported, "GTIFFBuildOverviews() doesn't support a mixture of band" " data types." ); return CE_Failure; } else if( hBand->GetColorTable() != NULL ) { CPLError( CE_Failure, CPLE_NotSupported, "GTIFFBuildOverviews() doesn't support building" " overviews of multiple colormapped bands." ); return CE_Failure; } else if( hBand->GetXSize() != nXSize || hBand->GetYSize() != nYSize ) { CPLError( CE_Failure, CPLE_NotSupported, "GTIFFBuildOverviews() doesn't support building" " overviews of different sized bands." ); return CE_Failure; } } /* -------------------------------------------------------------------- */ /* Use specified compression method. */ /* -------------------------------------------------------------------- */ const char *pszCompress = CPLGetConfigOption( "COMPRESS_OVERVIEW", NULL ); if( pszCompress != NULL && pszCompress[0] != '\0' ) { nCompression = GTIFFGetCompressionMethod(pszCompress, "COMPRESS_OVERVIEW"); if (nCompression < 0) return CE_Failure; } if( nCompression == COMPRESSION_JPEG && nBitsPerPixel == 16 ) nBitsPerPixel = 12; /* -------------------------------------------------------------------- */ /* Figure out the planar configuration to use. */ /* -------------------------------------------------------------------- */ if( nBands == 1 ) nPlanarConfig = PLANARCONFIG_CONTIG; else nPlanarConfig = PLANARCONFIG_SEPARATE; const char* pszInterleave = CPLGetConfigOption( "INTERLEAVE_OVERVIEW", NULL ); if (pszInterleave != NULL && pszInterleave[0] != '\0') { if( EQUAL( pszInterleave, "PIXEL" ) ) nPlanarConfig = PLANARCONFIG_CONTIG; else if( EQUAL( pszInterleave, "BAND" ) ) nPlanarConfig = PLANARCONFIG_SEPARATE; else { CPLError( CE_Failure, CPLE_AppDefined, "INTERLEAVE_OVERVIEW=%s unsupported, value must be PIXEL or BAND. ignoring", pszInterleave ); } } /* -------------------------------------------------------------------- */ /* Figure out the photometric interpretation to use. */ /* -------------------------------------------------------------------- */ if( nBands == 3 ) nPhotometric = PHOTOMETRIC_RGB; else if( papoBandList[0]->GetColorTable() != NULL && !EQUALN(pszResampling,"AVERAGE_BIT2",12) ) { nPhotometric = PHOTOMETRIC_PALETTE; /* should set the colormap up at this point too! */ } else nPhotometric = PHOTOMETRIC_MINISBLACK; const char* pszPhotometric = CPLGetConfigOption( "PHOTOMETRIC_OVERVIEW", NULL ); if (pszPhotometric != NULL && pszPhotometric[0] != '\0') { if( EQUAL( pszPhotometric, "MINISBLACK" ) ) nPhotometric = PHOTOMETRIC_MINISBLACK; else if( EQUAL( pszPhotometric, "MINISWHITE" ) ) nPhotometric = PHOTOMETRIC_MINISWHITE; else if( EQUAL( pszPhotometric, "RGB" )) { nPhotometric = PHOTOMETRIC_RGB; } else if( EQUAL( pszPhotometric, "CMYK" )) { nPhotometric = PHOTOMETRIC_SEPARATED; } else if( EQUAL( pszPhotometric, "YCBCR" )) { nPhotometric = PHOTOMETRIC_YCBCR; /* Because of subsampling, setting YCBCR without JPEG compression leads */ /* to a crash currently. Would need to make GTiffRasterBand::IWriteBlock() */ /* aware of subsampling so that it doesn't overrun buffer size returned */ /* by libtiff */ if ( nCompression != COMPRESSION_JPEG ) { CPLError(CE_Failure, CPLE_NotSupported, "Currently, PHOTOMETRIC_OVERVIEW=YCBCR requires COMPRESS_OVERVIEW=JPEG"); return CE_Failure; } if (pszInterleave != NULL && pszInterleave[0] != '\0' && nPlanarConfig == PLANARCONFIG_SEPARATE) { CPLError(CE_Failure, CPLE_NotSupported, "PHOTOMETRIC_OVERVIEW=YCBCR requires INTERLEAVE_OVERVIEW=PIXEL"); return CE_Failure; } else { nPlanarConfig = PLANARCONFIG_CONTIG; } /* YCBCR strictly requires 3 bands. Not less, not more */ /* Issue an explicit error message as libtiff one is a bit cryptic : */ /* JPEGLib:Bogus input colorspace */ if ( nBands != 3 ) { CPLError(CE_Failure, CPLE_NotSupported, "PHOTOMETRIC_OVERVIEW=YCBCR requires a source raster with only 3 bands (RGB)"); return CE_Failure; } } else if( EQUAL( pszPhotometric, "CIELAB" )) { nPhotometric = PHOTOMETRIC_CIELAB; } else if( EQUAL( pszPhotometric, "ICCLAB" )) { nPhotometric = PHOTOMETRIC_ICCLAB; } else if( EQUAL( pszPhotometric, "ITULAB" )) { nPhotometric = PHOTOMETRIC_ITULAB; } else { CPLError( CE_Warning, CPLE_IllegalArg, "PHOTOMETRIC_OVERVIEW=%s value not recognised, ignoring.\n", pszPhotometric ); } } /* -------------------------------------------------------------------- */ /* Figure out the predictor value to use. */ /* -------------------------------------------------------------------- */ int nPredictor = PREDICTOR_NONE; if ( nCompression == COMPRESSION_LZW || nCompression == COMPRESSION_ADOBE_DEFLATE ) { const char* pszPredictor = CPLGetConfigOption( "PREDICTOR_OVERVIEW", NULL ); if( pszPredictor != NULL ) { nPredictor = atoi( pszPredictor ); } } /* -------------------------------------------------------------------- */ /* Create the file, if it does not already exist. */ /* -------------------------------------------------------------------- */ VSIStatBufL sStatBuf; if( VSIStatExL( pszFilename, &sStatBuf, VSI_STAT_EXISTS_FLAG ) != 0 ) { /* -------------------------------------------------------------------- */ /* Compute the uncompressed size. */ /* -------------------------------------------------------------------- */ double dfUncompressedOverviewSize = 0; int nDataTypeSize = GDALGetDataTypeSize(papoBandList[0]->GetRasterDataType())/8; for( iOverview = 0; iOverview < nOverviews; iOverview++ ) { int nOXSize, nOYSize; nOXSize = (nXSize + panOverviewList[iOverview] - 1) / panOverviewList[iOverview]; nOYSize = (nYSize + panOverviewList[iOverview] - 1) / panOverviewList[iOverview]; dfUncompressedOverviewSize += nOXSize * ((double)nOYSize) * nBands * nDataTypeSize; } if( nCompression == COMPRESSION_NONE && dfUncompressedOverviewSize > 4200000000.0 ) { #ifndef BIGTIFF_SUPPORT CPLError( CE_Failure, CPLE_NotSupported, "The overview file would be larger than 4GB\n" "but this is the largest size a TIFF can be, and BigTIFF is unavailable.\n" "Creation failed." ); return CE_Failure; #endif } /* -------------------------------------------------------------------- */ /* Should the file be created as a bigtiff file? */ /* -------------------------------------------------------------------- */ const char *pszBIGTIFF = CPLGetConfigOption( "BIGTIFF_OVERVIEW", NULL ); if( pszBIGTIFF == NULL ) pszBIGTIFF = "IF_NEEDED"; int bCreateBigTIFF = FALSE; if( EQUAL(pszBIGTIFF,"IF_NEEDED") ) { if( nCompression == COMPRESSION_NONE && dfUncompressedOverviewSize > 4200000000.0 ) bCreateBigTIFF = TRUE; } else if( EQUAL(pszBIGTIFF,"IF_SAFER") ) { /* Look at the size of the base image and suppose that */ /* the added overview levels won't be more than 1/2 of */ /* the size of the base image. The theory says 1/3 of the */ /* base image size if the overview levels are 2, 4, 8, 16... */ /* Thus take 1/2 as the security margin for 1/3 */ double dfUncompressedImageSize = nXSize * ((double)nYSize) * nBands * nDataTypeSize; if( dfUncompressedImageSize * .5 > 4200000000.0 ) bCreateBigTIFF = TRUE; } else { bCreateBigTIFF = CSLTestBoolean( pszBIGTIFF ); if (!bCreateBigTIFF && nCompression == COMPRESSION_NONE && dfUncompressedOverviewSize > 4200000000.0 ) { CPLError( CE_Failure, CPLE_NotSupported, "The overview file will be larger than 4GB, so BigTIFF is necessary.\n" "Creation failed."); return CE_Failure; } } #ifndef BIGTIFF_SUPPORT if( bCreateBigTIFF ) { CPLError( CE_Warning, CPLE_NotSupported, "BigTIFF requested, but GDAL built without BigTIFF\n" "enabled libtiff, request ignored." ); bCreateBigTIFF = FALSE; } #endif if( bCreateBigTIFF ) CPLDebug( "GTiff", "File being created as a BigTIFF." ); hOTIFF = XTIFFOpen( pszFilename, (bCreateBigTIFF) ? "w+8" : "w+" ); if( hOTIFF == NULL ) { if( CPLGetLastErrorNo() == 0 ) CPLError( CE_Failure, CPLE_OpenFailed, "Attempt to create new tiff file `%s'\n" "failed in XTIFFOpen().\n", pszFilename ); return CE_Failure; } } /* -------------------------------------------------------------------- */ /* Otherwise just open it for update access. */ /* -------------------------------------------------------------------- */ else { hOTIFF = XTIFFOpen( pszFilename, "r+" ); if( hOTIFF == NULL ) { if( CPLGetLastErrorNo() == 0 ) CPLError( CE_Failure, CPLE_OpenFailed, "Attempt to create new tiff file `%s'\n" "failed in XTIFFOpen().\n", pszFilename ); return CE_Failure; } } /* -------------------------------------------------------------------- */ /* Do we have a palette? If so, create a TIFF compatible version. */ /* -------------------------------------------------------------------- */ unsigned short *panRed=NULL, *panGreen=NULL, *panBlue=NULL; if( nPhotometric == PHOTOMETRIC_PALETTE ) { GDALColorTable *poCT = papoBandList[0]->GetColorTable(); int nColorCount; if( nBitsPerPixel <= 8 ) nColorCount = 256; else nColorCount = 65536; panRed = (unsigned short *) CPLCalloc(nColorCount,sizeof(unsigned short)); panGreen = (unsigned short *) CPLCalloc(nColorCount,sizeof(unsigned short)); panBlue = (unsigned short *) CPLCalloc(nColorCount,sizeof(unsigned short)); for( int iColor = 0; iColor < nColorCount; iColor++ ) { GDALColorEntry sRGB; if( poCT->GetColorEntryAsRGB( iColor, &sRGB ) ) { panRed[iColor] = (unsigned short) (257 * sRGB.c1); panGreen[iColor] = (unsigned short) (257 * sRGB.c2); panBlue[iColor] = (unsigned short) (257 * sRGB.c3); } } } /* -------------------------------------------------------------------- */ /* Do we need some metadata for the overviews? */ /* -------------------------------------------------------------------- */ CPLString osMetadata; GDALDataset *poBaseDS = papoBandList[0]->GetDataset(); GTIFFBuildOverviewMetadata( pszResampling, poBaseDS, osMetadata ); /* -------------------------------------------------------------------- */ /* Loop, creating overviews. */ /* -------------------------------------------------------------------- */ int nOvrBlockXSize, nOvrBlockYSize; GTIFFGetOverviewBlockSize(&nOvrBlockXSize, &nOvrBlockYSize); for( iOverview = 0; iOverview < nOverviews; iOverview++ ) { int nOXSize, nOYSize; nOXSize = (nXSize + panOverviewList[iOverview] - 1) / panOverviewList[iOverview]; nOYSize = (nYSize + panOverviewList[iOverview] - 1) / panOverviewList[iOverview]; GTIFFWriteDirectory(hOTIFF, FILETYPE_REDUCEDIMAGE, nOXSize, nOYSize, nBitsPerPixel, nPlanarConfig, nBands, nOvrBlockXSize, nOvrBlockYSize, TRUE, nCompression, nPhotometric, nSampleFormat, nPredictor, panRed, panGreen, panBlue, 0, NULL, /* FIXME? how can we fetch extrasamples */ osMetadata ); } if (panRed) { CPLFree(panRed); CPLFree(panGreen); CPLFree(panBlue); panRed = panGreen = panBlue = NULL; } XTIFFClose( hOTIFF ); /* -------------------------------------------------------------------- */ /* Open the overview dataset so that we can get at the overview */ /* bands. */ /* -------------------------------------------------------------------- */ GDALDataset *hODS; CPLErr eErr = CE_None; hODS = (GDALDataset *) GDALOpen( pszFilename, GA_Update ); if( hODS == NULL ) return CE_Failure; /* -------------------------------------------------------------------- */ /* Do we need to set the jpeg quality? */ /* -------------------------------------------------------------------- */ TIFF *hTIFF = (TIFF*) hODS->GetInternalHandle(NULL); if( nCompression == COMPRESSION_JPEG && CPLGetConfigOption( "JPEG_QUALITY_OVERVIEW", NULL ) != NULL ) { int nJpegQuality = atoi(CPLGetConfigOption("JPEG_QUALITY_OVERVIEW","75")); TIFFSetField( hTIFF, TIFFTAG_JPEGQUALITY, nJpegQuality ); GTIFFSetJpegQuality((GDALDatasetH)hODS, nJpegQuality); } /* -------------------------------------------------------------------- */ /* Loop writing overview data. */ /* -------------------------------------------------------------------- */ if (nCompression != COMPRESSION_NONE && nPlanarConfig == PLANARCONFIG_CONTIG && GDALDataTypeIsComplex(papoBandList[0]->GetRasterDataType()) == FALSE && papoBandList[0]->GetColorTable() == NULL && (EQUALN(pszResampling, "NEAR", 4) || EQUAL(pszResampling, "AVERAGE") || EQUAL(pszResampling, "GAUSS"))) { /* In the case of pixel interleaved compressed overviews, we want to generate */ /* the overviews for all the bands block by block, and not band after band, */ /* in order to write the block once and not loose space in the TIFF file */ GDALRasterBand ***papapoOverviewBands; papapoOverviewBands = (GDALRasterBand ***) CPLCalloc(sizeof(void*),nBands); for( iBand = 0; iBand < nBands && eErr == CE_None; iBand++ ) { GDALRasterBand *hDstBand = hODS->GetRasterBand( iBand+1 ); papapoOverviewBands[iBand] = (GDALRasterBand **) CPLCalloc(sizeof(void*),nOverviews); papapoOverviewBands[iBand][0] = hDstBand; for( int i = 0; i < nOverviews-1 && eErr == CE_None; i++ ) { papapoOverviewBands[iBand][i+1] = hDstBand->GetOverview(i); if (papapoOverviewBands[iBand][i+1] == NULL) eErr = CE_Failure; } } if (eErr == CE_None) eErr = GDALRegenerateOverviewsMultiBand(nBands, papoBandList, nOverviews, papapoOverviewBands, pszResampling, pfnProgress, pProgressData ); for( iBand = 0; iBand < nBands; iBand++ ) { CPLFree(papapoOverviewBands[iBand]); } CPLFree(papapoOverviewBands); } else { GDALRasterBand **papoOverviews; papoOverviews = (GDALRasterBand **) CPLCalloc(sizeof(void*),128); for( iBand = 0; iBand < nBands && eErr == CE_None; iBand++ ) { GDALRasterBand *hSrcBand = papoBandList[iBand]; GDALRasterBand *hDstBand; int nDstOverviews; hDstBand = hODS->GetRasterBand( iBand+1 ); papoOverviews[0] = hDstBand; nDstOverviews = hDstBand->GetOverviewCount() + 1; CPLAssert( nDstOverviews < 128 ); nDstOverviews = MIN(128,nDstOverviews); for( int i = 0; i < nDstOverviews-1 && eErr == CE_None; i++ ) { papoOverviews[i+1] = hDstBand->GetOverview(i); if (papoOverviews[i+1] == NULL) eErr = CE_Failure; } void *pScaledProgressData; pScaledProgressData = GDALCreateScaledProgress( iBand / (double) nBands, (iBand+1) / (double) nBands, pfnProgress, pProgressData ); if (eErr == CE_None) eErr = GDALRegenerateOverviews( (GDALRasterBandH) hSrcBand, nDstOverviews, (GDALRasterBandH *) papoOverviews, pszResampling, GDALScaledProgress, pScaledProgressData); GDALDestroyScaledProgress( pScaledProgressData ); } CPLFree( papoOverviews ); } /* -------------------------------------------------------------------- */ /* Cleanup */ /* -------------------------------------------------------------------- */ if (eErr == CE_None) hODS->FlushCache(); delete hODS; pfnProgress( 1.0, NULL, pProgressData ); return eErr; }
int main(int argc, char *argv[]) { char *fname = NULL; char *outfile = NULL; TIFF *tif=(TIFF*)0; /* TIFF-level descriptor */ GTIF *gtif=(GTIF*)0; /* GeoKey-level descriptor */ int i, j, norm_print_flag = 0, proj4_print_flag = 0; int tfw_flag = 0, inv_flag = 0, dec_flag = 1; int st_test_flag = 0; size_t npixels; int16* elevations; int x, y, skip; GTIFDefn defn; FILE* out; /* * Handle command line options. */ for( i = 1; i < argc; i++ ) { if( fname == NULL && argv[i][0] != '-' ) fname = argv[i]; else { Usage(); } } if( fname == NULL) Usage(); skip = 5; /* * Open the file, read the GeoTIFF information, and print to stdout. */ int flength = strlen(fname); outfile = (char *) malloc(flength); strncpy(outfile,fname,flength-4); strcat(outfile, ".asc\0"); out = fopen(outfile, "w"); printf("Writing to %s\n\n",outfile); free(outfile); tif=XTIFFOpen(fname,"r"); if (!tif) goto failure; gtif = GTIFNew(tif); if (!gtif) { fprintf(stderr,"failed in GTIFNew\n"); goto failure; } /* dump the GeoTIFF metadata to std out */ GTIFPrint(gtif,0,0); if( GTIFGetDefn( gtif, &defn ) ) { uint32 xsize, ysize; printf( "\n" ); GTIFPrintDefn( &defn, stdout ); if( proj4_print_flag ) { printf( "\n" ); printf( "PROJ.4 Definition: %s\n", GTIFGetProj4Defn(&defn)); } int count, orient; double* data; TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &xsize ); TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &ysize ); TIFFGetField( tif, TIFFTAG_GEOPIXELSCALE, &count, &data); TIFFGetField( tif, TIFFTAG_ORIENTATION, &orient ); printf("Orientation:%d\n",orient); GTIFPrintCorners( gtif, &defn, stdout, xsize, ysize, inv_flag, dec_flag ); const char* project = "ASTERGDEM"; const char* yymmdd = "090629"; double originx, originy; GTIFImageToPCS( gtif, &originx, &originy); int lat = (int)originy*1000; int lon = (int)originx*1000; int xmin = 0; int ymin = 0; int nx = xsize/skip + 1; int ny = ysize/skip + 1; int dx = 30 * skip; int dy = 30 * skip; int16 elev = 0; fprintf(out, "%12s%12s%7d%7d%7d%7d%7d%7d%7d%7d\n", project, yymmdd, lat, lon, xmin, ymin, nx, ny, dx,dy); npixels = xsize * ysize; int16* buf; tsample_t sample; int nbytes; nbytes = TIFFScanlineSize(tif); buf = (int16*) _TIFFmalloc(nbytes); elevations = (int16*) _TIFFmalloc(npixels * sizeof (int16)); if (elevations != NULL) { for( y = 0; y < ysize; y++ ) { uint32 row = ysize - 1 - y; TIFFReadScanline(tif, buf, row, sample); for( x = 0; x < xsize; x++ ) { elevations[y*xsize + x] = buf[x]; } } _TIFFfree(buf); } for( y = 0; y < ysize; y+=skip ) { for( x = 0; x < xsize; x+=skip ) { fprintf(out, "%6d", elevations[y*xsize + x]); } fprintf(out, "\n"); } _TIFFfree(elevations); } Success: GTIFFree(gtif); if( st_test_flag ) ST_Destroy( (ST_TIFF *) tif ); else XTIFFClose(tif); GTIFDeaccessCSV(); return 0; failure: fprintf(stderr,"failure in listgeo\n"); if (tif) XTIFFClose(tif); if (gtif) GTIFFree(gtif); GTIFDeaccessCSV(); return 1; }
bool TiffDetails::savePict(QString strFilename, QImage img, const SessionItem *pItem) { if (strFilename.isEmpty() || img.isNull()) { return false; } std::string filename = strFilename.toStdString(); unsigned int outputWidth = img.width(); unsigned int outputHeight = img.height(); if (mpOptionsWidget.get() == NULL) { computeExportResolution(outputWidth, outputHeight); } else { mpOptionsWidget->getResolution(outputWidth, outputHeight); } img = img.scaled(outputWidth, outputHeight); #if defined(WIN_API) // replace "/" with "\\" to accommodate the sgi specific code // in the library unsigned int i; for (i = 0; i < filename.length(); i++) { if (filename[i] == '/') { filename[i] = '\\'; } } #endif //open the file to write TIFF* pOut = XTIFFOpen(filename.c_str(), "wb"); if (pOut == NULL) { return false; } unsigned int rowsPerStrip = OptionsTiffExporter::getSettingRowsPerStrip(); bool packBits = OptionsTiffExporter::getSettingPackBitsCompression(); bool saveBackgroundAsTransparent = isBackgroundTransparent(); if (mpOptionsWidget.get() != NULL) { rowsPerStrip = mpOptionsWidget->getRowsPerStrip(); packBits = mpOptionsWidget->getPackBitsCompression(); } int samplesPerPixel = 3; if (saveBackgroundAsTransparent) { samplesPerPixel = 4; } // Initialize private variables for tiff TIFFSetField(pOut, TIFFTAG_IMAGEWIDTH, static_cast<uint32>(img.width())); TIFFSetField(pOut, TIFFTAG_IMAGELENGTH, static_cast<uint32>(img.height())); TIFFSetField(pOut, TIFFTAG_BITSPERSAMPLE, 8); TIFFSetField(pOut, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); TIFFSetField(pOut, TIFFTAG_SAMPLESPERPIXEL, samplesPerPixel); TIFFSetField(pOut, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(pOut, TIFFTAG_ROWSPERSTRIP, rowsPerStrip); TIFFSetField(pOut, TIFFTAG_COMPRESSION, (packBits ? COMPRESSION_PACKBITS : COMPRESSION_NONE)); //allocate the memory needed tdata_t pBuf = _TIFFmalloc(TIFFScanlineSize(pOut)); //determine if we should mask out the background pixels //loop through the image and convert to tiff and write to the file int x; int y; for (y = 0; y < img.height(); y++) { uint8* pp = static_cast<uint8*>(pBuf); QRgb* pData = reinterpret_cast<QRgb*>(img.scanLine(y)); for (x = 0; x < img.width(); x++) { pp[0] = qRed(pData[x]); pp[1] = qGreen(pData[x]); pp[2] = qBlue(pData[x]); if (saveBackgroundAsTransparent) { pp[3] = qAlpha(pData[x]); } pp += samplesPerPixel; } //write out 1 row and check for error if (TIFFWriteScanline(pOut, pBuf, y, 0) < 0) { //error message return false; } } // Add in the geo info if it is available addGeoKeys(pOut, img.width(), img.height(), pItem); XTIFFClose(pOut); return true; }
static int InstallGeoTIFF(const char *geofile, const char *tiffile) { TIFF *tif = (TIFF*)0; /* TIFF-level descriptor */ GTIF *gtif=(GTIF*)0; /* GeoKey-level descriptor */ FILE *fp; uint16 *panVI = NULL; uint16 nKeyCount; tif = XTIFFOpen(tiffile, "r+"); if (!tif) { perror(tiffile); fprintf(stderr, "Cannot open TIFF file %s (does not exist or not a valid TIFF file)\n", tiffile); return(-1); } /* If we have existing geokeys, try to wipe them by writing a dummy geokey directory. (#2546) */ if( TIFFGetField( tif, TIFFTAG_GEOKEYDIRECTORY, &nKeyCount, &panVI ) ) { uint16 anGKVersionInfo[4] = { 1, 1, 0, 0 }; double adfDummyDoubleParams[1] = { 0.0 }; TIFFSetField( tif, TIFFTAG_GEOKEYDIRECTORY, 4, anGKVersionInfo ); TIFFSetField( tif, TIFFTAG_GEODOUBLEPARAMS, 1, adfDummyDoubleParams ); TIFFSetField( tif, TIFFTAG_GEOASCIIPARAMS, "" ); } gtif = GTIFNew(tif); if (!gtif) { fprintf(stderr, "Internal error (GTIFNew)\n"); return(-2); } /* Read GeoTIFF projection information from geofile */ fp = fopen(geofile, "r"); if( fp == NULL ) { perror( geofile ); fprintf(stderr, "Cannot open projection definition file %s\n", geofile); return(-3); } if (!GTIFImport(gtif, 0, fp)) { fprintf(stderr,"Projection definition file is not valid (%s)\n", geofile); return(-4); } fclose(fp); /* Install GeoTIFF keys into the TIFF file */ GTIFWriteKeys(gtif); /* Clean up */ GTIFFree(gtif); TIFFRewriteDirectory(tif); XTIFFClose(tif); return(0); }
bool DEM::readDem(char* fname) { demFilename = fname; TIFF *tif=(TIFF*)0; /* TIFF-level descriptor */ GTIF *gtif=(GTIF*)0; /* GeoKey-level descriptor */ int proj4_print_flag = 0; int inv_flag = 0, dec_flag = 1; int st_test_flag = 0; GTIFDefn defn; /* * Open the file, read the GeoTIFF information, and print some info to stdout. */ tif=XTIFFOpen(fname,"r"); if (!tif) goto failure; gtif = GTIFNew(tif); if (!gtif) { fprintf(stderr,"failed in GTIFNew\n"); goto failure; } /* dump the GeoTIFF metadata to std out */ GTIFPrint(gtif,0,0); if( GTIFGetDefn( gtif, &defn ) ) { printf( "\n" ); GTIFPrintDefn( &defn, stdout ); if( proj4_print_flag ) { printf( "\n" ); printf( "PROJ.4 Definition: %s\n", GTIFGetProj4Defn(&defn)); } int count, orient; double* data; TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &xsize ); TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &ysize ); TIFFGetField( tif, TIFFTAG_GEOPIXELSCALE, &count, &data); TIFFGetField( tif, TIFFTAG_ORIENTATION, &orient ); printf("Orientation:%d\n",orient); GTIFPrintCorners( gtif, &defn, stdout, xsize, ysize, inv_flag, dec_flag ); double originx = 0.0; double originy = ysize; GTIFImageToPCS( gtif, &originx, &originy); refLat = originy; refLon = originx; npixels = xsize * ysize; int16* buf; tsample_t sample; int nbytes; nbytes = TIFFScanlineSize(tif); buf = (int16*) _TIFFmalloc(nbytes); elevations = (int16*) _TIFFmalloc(npixels * sizeof (int16)); if (elevations != NULL) { for(unsigned int y = 0; y < ysize; y++ ) { uint32 row = ysize - 1 - y; TIFFReadScanline(tif, buf, row, sample); for(unsigned int x = 0; x < xsize; x++ ) { elevations[y*xsize + x] = buf[x]; } } _TIFFfree(buf); } _TIFFfree(elevations); } GTIFFree(gtif); if( st_test_flag ) ST_Destroy( (ST_TIFF *) tif ); else XTIFFClose(tif); GTIFDeaccessCSV(); return true; failure: fprintf(stderr,"failure in listgeo\n"); if (tif) XTIFFClose(tif); if (gtif) GTIFFree(gtif); GTIFDeaccessCSV(); return false; }
void CGeoImageView::EngineSetup(void) { Ogre::Root *Root = ((CGeoImageApp*)AfxGetApp())->m_Engine->GetRoot(); Ogre::SceneManager *SceneManager = NULL; SceneManager = Root->createSceneManager(Ogre::ST_GENERIC, "MFCOgre"); // // Create a render window // This window should be the current ChildView window using the externalWindowHandle // value pair option. // Ogre::NameValuePairList parms; parms["externalWindowHandle"] = Ogre::StringConverter::toString((long)m_hWnd); parms["vsync"] = "true"; CRect rect; GetClientRect(&rect); Ogre::RenderTarget *RenderWindow = Root->getRenderTarget("Ogre in MFC"); if (RenderWindow == NULL) { try { m_RenderWindow = Root->createRenderWindow("Ogre in MFC", rect.Width(), rect.Height(), false, &parms); } catch(...) { MessageBox("Cannot initialize\nCheck that graphic-card driver is up-to-date", "Initialize Render System", MB_OK | MB_ICONSTOP); exit(EXIT_SUCCESS); } } // Load resources Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); // Create the camera m_Camera = SceneManager->createCamera("Camera"); m_Camera->setNearClipDistance(0.5); m_Camera->setFarClipDistance(5000); m_Camera->setCastShadows(false); m_Camera->setUseRenderingDistance(true); // m_Camera->setPosition(Ogre::Vector3(320.0, 240.0, 500.0)); Ogre::SceneNode *CameraNode = NULL; CameraNode = SceneManager->getRootSceneNode()->createChildSceneNode("CameraNode"); Ogre::Viewport* Viewport = NULL; if (0 == m_RenderWindow->getNumViewports()) { Viewport = m_RenderWindow->addViewport(m_Camera); Viewport->setBackgroundColour(Ogre::ColourValue(0.8f, 1.0f, 0.8f)); } // Alter the camera aspect ratio to match the viewport m_Camera->setAspectRatio(Ogre::Real(rect.Width()) / Ogre::Real(rect.Height())); TIFF *Tif = (TIFF*)0; /* TIFF-level descriptor */ GTIF *GTif = (GTIF*)0; /* GeoKey-level descriptor */ CString SourcePath = "C:\\Users\\Ilya\\Documents\\Visual Studio 2010\\Projects\\Recipes\\media\\materials\\textures\\o41078a1.tif"; int ImageWidth; int ImageHeight; double LowerLeftX; double LowerLeftY; double UpperRightX; double UpperRightY; int Xpos; int Ypos; Tif = XTIFFOpen((LPCSTR)SourcePath, "r"); GTif = GTIFNew(Tif); GTIFDefn Definition; GTIFGetDefn(GTif, &Definition); GTIFPrint(GTif, 0, 0); TIFFGetField( Tif, TIFFTAG_IMAGEWIDTH, &ImageWidth); TIFFGetField( Tif, TIFFTAG_IMAGELENGTH, &ImageHeight); int res = 0; double LowerRightX = ImageWidth; double LowerRightY = ImageHeight; res = GTIFImageToPCS(GTif, &LowerRightX, &LowerRightY); //Lower Left LowerLeftX = 0.0; LowerLeftY = ImageHeight; res = GTIFImageToPCS(GTif, &LowerLeftX, &LowerLeftY); //Upper Right UpperRightX = ImageWidth; UpperRightY = 0.0; res = GTIFImageToPCS(GTif, &UpperRightX, &UpperRightY); double UpperLeftX = 0.0; double UpperLeftY = 0.0; res = GTIFImageToPCS(GTif, &UpperLeftX, &UpperLeftY); Ogre::ManualObject *Terrain = SceneManager->createManualObject("Terrain"); Terrain->setDynamic(false); Terrain->begin("Terrain", Ogre::RenderOperation::OT_TRIANGLE_FAN); #define MIN(x,y) (((x) < (y)) ? (x) : (y)) #define MAX(x,y) (((x) > (y)) ? (x) : (y)) #define SW 0 #define NW 1 #define NE 2 #define SE 3 FILE *File = NULL; char Dummy[160]; char inText[24]; char *Dest; int base[2048]; /* array of base elevations */ char mapLabel[145]; int DEMlevel, elevationPattern, groundSystem, groundZone; double projectParams[15]; int planeUnitOfMeasure, elevUnitOfMeasure, polygonSizes; double groundCoords[4][2], elevBounds[2], localRotation; int accuracyCode; double spatialResolution[3]; int profileDimension[2]; int firstRow, lastRow; int wcount = 0; double verticalScale = 1.0; /* to stretch or shrink elevations */ double deltaY; char *junk; double eastMost; double westMost; double southMost; double northMost; int eastMostSample; int westMostSample; int southMostSample; int northMostSample; int rowCount, columnCount, r, c; int rowStr = 1; int rowEnd; int colStr = 1; int colEnd; int colInt = 1; int rowInt = 1; int outType = 0; int k,l ; double comp = 0.0; int mod ; int tempInt, lastProfile = 0; float noValue = 0.0f; int profileID[2], profileSize[2]; double planCoords[2], localElevation, elevExtremea[2]; File = fopen("C:\\Users\\Ilya\\Documents\\Visual Studio 2010\\Projects\\Recipes\\media\\geotiff\\karthaus_pa.dem", "r"); fscanf(File, "%144c%6d%6d%6d%6d", mapLabel, /* 1 */ &DEMlevel, /* 2 */ &elevationPattern, /* 3 */ &groundSystem, /* 4 */ &groundZone ); /* 5 */ for (k=0; k<15 ; k++) { fscanf(File,"%24c", inText); /* 6 */ Dest = strchr(inText,'D'); *Dest = 'E'; projectParams[k] = strtod(inText,&junk); } fscanf(File,"%6d%6d%6d", &planeUnitOfMeasure, /* 7 */ &elevUnitOfMeasure, /* 8 */ &polygonSizes); /* 9 */ for (k=0; k < 4 ; k++) for (l=0; l < 2 ; l++) { fscanf(File,"%24c", inText); /* 6 */ Dest = strchr(inText,'D'); *Dest = 'E'; groundCoords[k][l] = strtod(inText,&junk); } fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; elevBounds[0] = strtod(inText,&junk); /* 10 */ fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; elevBounds[1] = strtod(inText,&junk); /* 11 */ fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; localRotation = strtod(inText,&junk); /* 12 */ fscanf(File,"%1d%12le%12le%12le%6d%6d", &accuracyCode, /* 13 */ &spatialResolution[0], /* 14 */ &spatialResolution[1], /* 14 */ &spatialResolution[2], /* 14 */ &profileDimension[0], /* 15 */ &profileDimension[1]); /* 15 */ fscanf(File, "%160c", Dummy); if (spatialResolution[0] == 3.0) { comp = spatialResolution[0] - 1.0 ; deltaY = spatialResolution[1] / 3600. ; } else { comp = 0.; deltaY = spatialResolution[1] ; } eastMost = MAX(groundCoords[NE][0], groundCoords[SE][0]); westMost = MIN(groundCoords[NW][0], groundCoords[SW][0]); northMost = MAX(groundCoords[NE][1], groundCoords[NW][1]); southMost = MIN(groundCoords[SW][1], groundCoords[SE][1]); eastMostSample = ((int) (eastMost / spatialResolution[0])) * (int) spatialResolution[0]; westMostSample = ((int) ((westMost + comp) / spatialResolution[0])) * (int) spatialResolution[0] ; northMostSample = ((int) (northMost / spatialResolution[1])) * (int) spatialResolution[1] ; southMostSample = ((int) ((southMost + comp) / spatialResolution[1])) * (int) spatialResolution[1] ; columnCount = (eastMostSample - westMostSample) / (int) spatialResolution[0] + 1; rowCount = (northMostSample - southMostSample) / (int) spatialResolution[1] + 1; if (columnCount != profileDimension[1]) columnCount =MIN( profileDimension[1], columnCount); for (c = 1; c <= columnCount; c++) { fscanf(File, "%d%d%d%d", &profileID[0], /* 1 */ &profileID[1], /* 1 */ &profileSize[0], /* 2 */ &profileSize[1]); /* 2 */ fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; planCoords[0] = strtod(inText,&junk); /* 3 */ fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; planCoords[1] = strtod(inText,&junk); /* 3 */ fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; localElevation = strtod(inText,&junk); /* 4 */ fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; elevExtremea[0] = strtod(inText,&junk); /* 5 */ fscanf(File,"%24c", inText); Dest = strchr(inText,'D'); *Dest = 'E'; elevExtremea[1] = strtod(inText,&junk); /* 5 */ lastProfile = profileID[1]; firstRow = abs(((int) (planCoords[1] - southMostSample)) / (int) spatialResolution[1]); lastRow = firstRow + profileSize[0] - 1; for ( r = 0 ; r < firstRow ; r++ ) base[r] = 0; /* read in all the data for this column */ for (r = firstRow; r <= lastRow; r++ ) { fscanf(File, "%6d", &tempInt); base[r] = tempInt; } double tempFloat; for (r = firstRow; r <= lastRow; r += rowInt) { tempFloat = (float) base[r] * verticalScale; Terrain->position(planCoords[0], tempFloat, planCoords[1]); Ogre::Real u = 0.0; Ogre::Real v = 0.0; if (planCoords[0] > LowerLeftX && planCoords[0] < UpperRightX) { u = (planCoords[0] - LowerLeftX) / (UpperRightX - LowerLeftX); } if (planCoords[1] > LowerLeftY && planCoords[1] < UpperRightY) { v = (planCoords[1] - LowerLeftY) / (UpperRightY - LowerLeftY); } Terrain->textureCoord(u, v); planCoords[1] += deltaY; } } Terrain->end(); fclose(File); Ogre::SceneNode* node = SceneManager->getRootSceneNode()->createChildSceneNode(); node->setPosition(0, 0, 0); node->attachObject(Terrain); Ogre::AxisAlignedBox Box = Terrain->getBoundingBox(); Ogre::Vector3 Center = Box.getCenter(); Ogre::Vector3 Target = Center; Ogre::Vector3 Position = Center; Position[1] += 5000.0; m_Camera->setPosition(Position); m_Camera->lookAt(Target); }
void import_radarsat2(const char *inBaseName, radiometry_t radiometry, const char *outBaseName, int ampOnly) { FILE *fp; radarsat2_meta *radarsat2; meta_parameters *meta; char **inDataNames=NULL, inDataName[1024], *inMetaName=NULL; char *outDataName=NULL, str[512]; float *amp = NULL, *phase = NULL, *tmp = NULL, re, im; int band, sample; // Check radiometry if (radiometry != r_AMP) { asfPrintWarning("Radiometry other than AMPLITUDE is currently not " "supported.\n"); radiometry = r_AMP; } if (!fileExists(inBaseName)) inMetaName = appendExt(inBaseName, ".xml"); else { inMetaName = (char *) MALLOC(sizeof(char)*1024); strcpy(inMetaName, inBaseName); } outDataName = appendExt(outBaseName, ".img"); radarsat2 = read_radarsat2_meta(inMetaName); asfPrintStatus(" DataType: %s, ProductType: %s\n", radarsat2->dataType, radarsat2->productType); if (strcmp_case(radarsat2->dataType, "COMPLEX") != 0) asfPrintError("Currently only complex data supported!\n"); meta = radarsat2meta(radarsat2); meta_write(meta, outDataName); // Let's check the GeoTIFF data. // Unfortunately, there is no identifier in the GeoTIFF that would identify // the data as Radarsat-2 data. // // The only thing that we can actually do is to look whether the data in the // GeoTIFF file fit the general bill. We can the image dimensions. The data // needs to have 2 bands (I and Q) and 16 bit. The citation geokey needs to // be the really non-descriptive "Uncorrected Satellite Data". TIFF *tiff = NULL; GTIF *gtif = NULL; data_type_t data_type; short sample_format, bits_per_sample, planar_config; short num_bands; int is_scanline_format, is_palette_color_tiff, wrong=FALSE; char *error_message = (char *) MALLOC(sizeof(char)*2048); inDataNames = extract_band_names(meta->general->basename, meta->general->band_count); fp = FOPEN(outDataName, "wb"); int band_count = radarsat2->band_count; if (ampOnly) { strcpy(meta->general->bands, "AMP"); meta->general->band_count = 1; band_count = 1; } for (band=0; band<band_count; band++) { // path from the xml (metadata) file char *path = get_dirname(inBaseName); if (strlen(path)>0) { strcpy(inDataName, path); if (inDataName[strlen(inDataName)-1] != '/') strcat(inDataName, "/"); } else strcpy(inDataName, ""); free(path); strcat(inDataName, inDataNames[band]); tiff = XTIFFOpen(inDataName, "r"); if (!tiff) asfPrintError("Could not open data file (%s)\n", inDataName); gtif = GTIFNew(tiff); if (!gtif) asfPrintError("Could not read GeoTIFF keys from data file (%s)\n", inDataName); // Check image dimensions uint32 tif_sample_count; uint32 tif_line_count; TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &tif_line_count); TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &tif_sample_count); if ((meta->general->sample_count != tif_sample_count) || (meta->general->line_count != tif_line_count)) asfPrintError(error_message, "Problem with image dimensions. Was looking for %d lines " "and %d samples.\nFound %ld lines and %ld samples instead!" "\n", meta->general->line_count, meta->general->sample_count, tif_line_count, tif_sample_count); // Check general TIFF tags get_tiff_data_config(tiff, &sample_format, &bits_per_sample, &planar_config, &data_type, &num_bands, &is_scanline_format, &is_palette_color_tiff, REPORT_LEVEL_WARNING); // The specs say the data is supposed to be unsigned but it is not. // Let is pass as long as we are talking about integer data here strcpy(error_message, ""); if (sample_format != SAMPLEFORMAT_UINT && sample_format != SAMPLEFORMAT_INT) { strcat(error_message, "Problem with sampling format. Was looking for integer, "); if (sample_format == SAMPLEFORMAT_COMPLEXIEEEFP) strcat(error_message, "found complex floating point instead!\n"); else if (sample_format == SAMPLEFORMAT_COMPLEXINT) strcat(error_message, "found complex integer instead!\n"); else if (sample_format == SAMPLEFORMAT_IEEEFP) strcat(error_message, "found floating point instead!\n"); else if (sample_format == SAMPLEFORMAT_VOID) strcat(error_message, "found void instead!\n"); wrong = TRUE; } if (bits_per_sample != 16) { sprintf(str, "Problem with bits per sample. Was looking for 16, found %d " "instead!\n", bits_per_sample); strcat(error_message, str); wrong = TRUE; } if (data_type != INTEGER16) { strcat(error_message, "Problem with data type. Was looking INTEGER16, "); if (data_type == ASF_BYTE) strcat(error_message, "found BYTE instead!\n"); else if (data_type == INTEGER32) strcat(error_message, "found INTEGER32 instead!\n"); else if (data_type == REAL32) strcat(error_message, "found REAL32 instead!\n"); else if (data_type == REAL64) strcat(error_message, "found REAL64 instead!\n"); else if (data_type == COMPLEX_BYTE) strcat(error_message, "found COMPLEX_BYTE instead!\n"); else if (data_type == COMPLEX_INTEGER16) strcat(error_message, "found COMPLEX_INTEGER16 instead!\n"); else if (data_type == COMPLEX_INTEGER32) strcat(error_message, "found COMPLEX_INTEGER32 instead!\n"); else if (data_type == COMPLEX_REAL32) strcat(error_message, "found COMPLEX_REAL32 instead!\n"); else if (data_type == COMPLEX_REAL64) strcat(error_message, "found COMPLEX_REAL64 instead!\n"); wrong = TRUE; } if (num_bands != 2) { sprintf(str, "Problem with number of bands. Was looking for 2, " "found %d instead!\n", num_bands); strcat(error_message, str); wrong = TRUE; } if (wrong) asfPrintError(error_message); // Check GTCitationGeoKey char *citation = NULL; int citation_length, typeSize; tagtype_t citation_type; citation_length = GTIFKeyInfo(gtif, GTCitationGeoKey, &typeSize, &citation_type); if (citation_length > 0) { citation = (char *) MALLOC(citation_length * typeSize); GTIFKeyGet(gtif, GTCitationGeoKey, citation, 0, citation_length); if (citation && strcmp_case(citation, "UNCORRECTED SATELLITE DATA") != 0) { asfPrintError("Problem with GTCitationGeoKey. Was looking for " "'Uncorrected Satellite Data',\nfound '%s' instead!\n", citation); } } else asfPrintError("Problem with GTCitationGeoKey. Was looking for " "'Uncorrected Satellite Data',\ndid not find any key!\n"); tiff_type_t tiffInfo; get_tiff_type(tiff, &tiffInfo); if (tiffInfo.format != SCANLINE_TIFF && tiffInfo.format != STRIP_TIFF && tiffInfo.format != TILED_TIFF) asfPrintError("Can't read the GeoTIFF file (%s). Unrecognized TIFF " "type!\n", inDataNames[band]); // If we made it here, we are reasonably sure that we have the file that // we are looking for. asfPrintStatus("\n Importing %s ...\n", inDataNames[band]); uint32 scanlineSize = TIFFScanlineSize(tiff); tdata_t *tiff_real_buf = _TIFFmalloc(scanlineSize); tdata_t *tiff_imag_buf = _TIFFmalloc(scanlineSize); if (!tiff_real_buf || !tiff_imag_buf) asfPrintError("Can't allocate buffer for reading TIFF lines!\n"); amp = (float *) MALLOC(sizeof(float)*meta->general->sample_count); phase = (float *) MALLOC(sizeof(float)*meta->general->sample_count); // Check whether we need to flip the image in any fashion int flip_vertical = FALSE; if (strcmp_case(radarsat2->lineTimeOrdering, "DECREASING") == 0) { asfPrintStatus(" Data will be flipped vertically while ingesting!\n"); flip_vertical = TRUE; } int flip_horizontal = FALSE; if (strcmp_case(radarsat2->pixelTimeOrdering, "DECREASING") == 0) { asfPrintStatus(" Data will be flipped horizontally while ingesting!\n"); flip_horizontal = TRUE; } if (flip_horizontal) tmp = (float *) MALLOC(sizeof(float)*meta->general->sample_count); // FIXME: still need to implement flipping vertically // Read file line by line uint32 row; int sample_count = meta->general->sample_count; int line_count = meta->general->line_count; for (row=0; row<(uint32)meta->general->line_count; row++) { asfLineMeter(row, meta->general->line_count); if (flip_vertical) { switch (tiffInfo.format) { case SCANLINE_TIFF: TIFFReadScanline(tiff, tiff_real_buf, line_count-row-1, 0); TIFFReadScanline(tiff, tiff_imag_buf, line_count-row-1, 1); break; case STRIP_TIFF: ReadScanline_from_TIFF_Strip(tiff, tiff_real_buf, line_count-row-1, 0); ReadScanline_from_TIFF_Strip(tiff, tiff_imag_buf, line_count-row-1, 1); break; case TILED_TIFF: ReadScanline_from_TIFF_TileRow(tiff, tiff_real_buf, line_count-row-1, 0); ReadScanline_from_TIFF_TileRow(tiff, tiff_imag_buf, line_count-row-1, 1); break; default: asfPrintError("Can't read this TIFF format!\n"); break; } } else { switch (tiffInfo.format) { case SCANLINE_TIFF: TIFFReadScanline(tiff, tiff_real_buf, row, 0); TIFFReadScanline(tiff, tiff_imag_buf, row, 1); break; case STRIP_TIFF: ReadScanline_from_TIFF_Strip(tiff, tiff_real_buf, row, 0); ReadScanline_from_TIFF_Strip(tiff, tiff_imag_buf, row, 1); break; case TILED_TIFF: ReadScanline_from_TIFF_TileRow(tiff, tiff_real_buf, row, 0); ReadScanline_from_TIFF_TileRow(tiff, tiff_imag_buf, row, 1); break; default: asfPrintError("Can't read this TIFF format!\n"); break; } } for (sample=0; sample<sample_count; sample++) { switch (sample_format) { case SAMPLEFORMAT_UINT: re = (float)(((uint16*)tiff_real_buf)[sample]); im = (float)(((uint16*)tiff_imag_buf)[sample]); break; case SAMPLEFORMAT_INT: re = (float)(((int16*)tiff_real_buf)[sample]); im = (float)(((int16*)tiff_imag_buf)[sample]); break; } amp[sample] = sqrt(re*re + im*im); phase[sample] = atan2(im, re); } if (flip_horizontal) { for (sample=0; sample<sample_count; sample++) tmp[sample] = amp[sample]; for (sample=0; sample<sample_count; sample++) amp[sample] = tmp[sample_count-sample-1]; } put_band_float_line(fp, meta, band*2, (int)row, amp); if (!ampOnly) put_band_float_line(fp, meta, band*2+1, (int)row, phase); } FREE(amp); FREE(phase); if (tmp) FREE(tmp); _TIFFfree(tiff_real_buf); _TIFFfree(tiff_imag_buf); GTIFFree(gtif); XTIFFClose(tiff); } // update the name field with directory name char *path = get_dirname(inBaseName); if (strlen(path)<=0) path = g_get_current_dir(); char *p = path, *q = path; while (q) { if ((q = strchr(p, DIR_SEPARATOR)) != NULL) p = q+1; } sprintf(meta->general->basename, "%s", p); FREE(path); meta_write(meta, outDataName); meta_free(meta); FREE(radarsat2); FCLOSE(fp); }
int read_tiff(const char *filename, int *nlines, int *nsamps, unsigned char **data) { TIFF *tiff = XTIFFOpen(filename, "r"); if (!tiff) { return FALSE; } uint32 width; uint32 height; TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height); TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width); *nlines = (int)height; *nsamps = (int)width; unsigned char *dest = CALLOC(width*height*3, sizeof(unsigned char)); data_type_t data_type; tiff_data_config_t data_config; int num_bands, is_scanline_format, is_palette_color_tiff; uint32 row; // Determine what type of TIFF this is (scanline/strip/tiled) if (get_tiff_data_config(tiff, &data_config.sample_format, &data_config.bits_per_sample, &data_config.planar_config, &data_type, &data_config.samples_per_pixel, &is_scanline_format, &is_palette_color_tiff, REPORT_LEVEL_NONE)) { return FALSE; } num_bands = data_config.samples_per_pixel; tiff_type_t tiffInfo; get_tiff_type(tiff, &tiffInfo); if (tiffInfo.imageCount > 1) { ; // Only first image in multi-image files will be utilized - WARN the user here? } if (tiffInfo.imageCount < 1) { // TIFF contains zero images ...fail return FALSE; } if (tiffInfo.format != SCANLINE_TIFF && tiffInfo.format != STRIP_TIFF && tiffInfo.format != TILED_TIFF) { // Unrecognized TIFF type return FALSE; } if (tiffInfo.volume_tiff) { // 3-dimensional (a 'volume tiff') found ...this is unsupported return FALSE; } if (num_bands > 1 && data_config.planar_config != PLANARCONFIG_CONTIG && data_config.planar_config != PLANARCONFIG_SEPARATE) { // Invalid planar configuration setting found in TIFF file... return FALSE; } uint32 scanlineSize = TIFFScanlineSize(tiff); if (scanlineSize <= 0) { // Invalid scanline size found in TIFF file... return FALSE; } if (data_config.bits_per_sample != 8) return FALSE; int is_rgb = data_config.samples_per_pixel >= 3; // Populate the buffer with actual data if (is_rgb) { // TIFF read buffer (red band) tdata_t *rtif_buf = _TIFFmalloc(scanlineSize); // TIFF read buffer (green band) tdata_t *gtif_buf = _TIFFmalloc(scanlineSize); // TIFF read buffer (blue band) tdata_t *btif_buf = _TIFFmalloc(scanlineSize); if (!rtif_buf || !gtif_buf || !btif_buf) return FALSE; for (row=0; row < height; row++) { // Read a scanline and populate r, g, and b tiff buffers // NOTE: Empty bands will have the no_data value populated in the //tiff buffer read_tiff_rgb_scanline(tiff, tiffInfo.format, &data_config, row, scanlineSize, width, 0, 1, 2, rtif_buf, gtif_buf, btif_buf); // Interleave the rgb values into an rgb buffer interleave_byte_rgbScanlines_to_byte_buff(dest, rtif_buf, gtif_buf, btif_buf, 0, 1, 2, row, width, &data_config); } _TIFFfree(rtif_buf); _TIFFfree(gtif_buf); _TIFFfree(btif_buf); } else { // is greyscale // TIFF read buffer (interleaved bands) tdata_t *tif_buf = _TIFFmalloc(scanlineSize); if (!tif_buf) return FALSE; for (row=0; row < height; row++) { // Read a scanline into a tiff buffer (using first non-blank band as // the greyscale image) // NOTE: Since displaying a greyscale band specifically selects a band, // empty or not, the selected band is read as-is. read_tiff_greyscale_scanline(tiff, tiffInfo.format, &data_config, row, scanlineSize, width, 0, tif_buf); copy_byte_scanline_to_byte_buff(dest, tif_buf, row, width, &data_config); } _TIFFfree(tif_buf); } *data = dest; return TRUE; }