diff --git a/gdal/frmts/nitf/nitfdataset.cpp b/gdal/frmts/nitf/nitfdataset.cpp index 5505dbde5a77..3f304a8c2059 100644 --- a/gdal/frmts/nitf/nitfdataset.cpp +++ b/gdal/frmts/nitf/nitfdataset.cpp @@ -67,6 +67,7 @@ static bool NITFPatchImageLength( const char *pszFilename, GUIntBig nImageOffset, GIntBig nPixelCount, const char *pszIC, + int nICOffset, CSLConstList papszCreationOptions ); static bool NITFWriteCGMSegments( const char *pszFilename, char **papszList ); static bool NITFWriteTextSegments( const char *pszFilename, char **papszList ); @@ -204,7 +205,7 @@ int NITFDataset::CloseDependentDatasets() CPL_IGNORE_RET_VAL( NITFPatchImageLength( GetDescription(), nImageStart, nPixelCount, - "C8", nullptr )); + "C8", m_nICOffset, nullptr )); } bJP2Writing = FALSE; @@ -4151,9 +4152,10 @@ NITFDataset::NITFDatasetCreate( const char *pszFilename, int nXSize, int nYSize, /* Create the file. */ /* -------------------------------------------------------------------- */ - if( !NITFCreate( pszFilename, nXSize, nYSize, nBands, - GDALGetDataTypeSize( eType ), pszPVType, - papszFullOptions ) ) + int nICOffset = 0; + if( !NITFCreateEx( pszFilename, nXSize, nYSize, nBands, + GDALGetDataTypeSize( eType ), pszPVType, + papszFullOptions, &nICOffset ) ) { CSLDestroy(papszTextMD); CSLDestroy(papszCgmMD); @@ -4205,6 +4207,7 @@ NITFDataset::NITFDatasetCreate( const char *pszFilename, int nXSize, int nYSize, NITFDataset::OpenInternal(&oOpenInfo, poWritableJ2KDataset, TRUE) ); if (poDS) { + poDS->m_nICOffset = nICOffset; poDS->papszTextMDToWrite = papszTextMD; poDS->papszCgmMDToWrite = papszCgmMD; } @@ -4877,9 +4880,10 @@ NITFDataset::NITFCreateCopy( } } - if (!NITFCreate( pszFilename, nXSize, nYSize, poSrcDS->GetRasterCount(), + int nICOffset = 0; + if (!NITFCreateEx( pszFilename, nXSize, nYSize, poSrcDS->GetRasterCount(), GDALGetDataTypeSize( eType ), pszPVType, - papszFullOptions )) + papszFullOptions, &nICOffset )) { CSLDestroy( papszFullOptions ); CSLDestroy(papszCgmMD); @@ -4967,7 +4971,7 @@ NITFDataset::NITFCreateCopy( poSrcDS->GetRasterCount(); bool bOK = NITFPatchImageLength( pszFilename, nImageOffset, nPixelCount, - "C8", papszFullOptions ); + "C8", nICOffset, papszFullOptions ); bOK &= NITFWriteCGMSegments( pszFilename, papszCgmMD ); bOK &= NITFWriteTextSegments( pszFilename, papszTextMD ); if( !bOK ) @@ -5028,7 +5032,7 @@ NITFDataset::NITFCreateCopy( NITFClose( psFile ); bool bOK = NITFPatchImageLength( pszFilename, nImageOffset, - nPixelCount, pszIC, papszFullOptions ); + nPixelCount, pszIC, nICOffset, papszFullOptions ); bOK &= NITFWriteCGMSegments( pszFilename, papszCgmMD ); bOK &= NITFWriteTextSegments( pszFilename, papszTextMD ); @@ -5221,6 +5225,7 @@ static bool NITFPatchImageLength( const char *pszFilename, GUIntBig nImageOffset, GIntBig nPixelCount, const char *pszIC, + int nICOffset, CSLConstList papszCreationOptions ) { @@ -5273,44 +5278,11 @@ static bool NITFPatchImageLength( const char *pszFilename, } /* -------------------------------------------------------------------- */ -/* Update COMRAT, the compression rate variable. We have to */ -/* take into account the presence of graphic and text segments, */ -/* the optional presence of IGEOLO and ICOM to find its position. */ +/* Update COMRAT, the compression rate variable. */ /* -------------------------------------------------------------------- */ - // get number of graphic and text segment so we can calculate offset for - // image IC. - const int nNumIOffset = 360; - bool bOK = VSIFSeekL( fpVSIL, nNumIOffset, SEEK_SET ) == 0; - char achNUM[4]; // buffer for segment size. 3 digits plus null character - achNUM[3] = '\0'; - bOK &= VSIFReadL( achNUM, 3, 1, fpVSIL ) == 1; - const int nIM = atoi(achNUM); // number of image segment - const int nNumSOffset = nNumIOffset + 3 + nIM * 16; - bOK &= VSIFSeekL( fpVSIL, nNumSOffset, SEEK_SET ) == 0; - bOK &= VSIFReadL( achNUM, 3, 1, fpVSIL ) == 1; - const int nGS = atoi(achNUM); // number of graphic segment - - const int nNumTOffset = nNumSOffset + 3 + 10 * nGS + 3; - bOK &= VSIFSeekL( fpVSIL, nNumTOffset, SEEK_SET ) == 0; - bOK &= VSIFReadL( achNUM, 3, 1, fpVSIL ) == 1; - const int nTS = atoi(achNUM); // number of text segment - - const int nAdditionalOffset = nGS * 10 + nTS * 9; - - /* Read ICORDS */ - bOK &= VSIFSeekL( fpVSIL, 775 + nAdditionalOffset , SEEK_SET ) == 0; - char chICORDS; - bOK &= VSIFReadL( &chICORDS, 1, 1, fpVSIL ) == 1; - if (chICORDS != ' ') - bOK &= VSIFSeekL( fpVSIL, 60, SEEK_CUR) == 0; /* skip IGEOLO */ - - /* Read NICOM */ - char achNICOM[2]; - bOK &= VSIFReadL( achNICOM, 1, 1, fpVSIL ) == 1; - achNICOM[1] = 0; - const int nNICOM = atoi(achNICOM); - bOK &= VSIFSeekL( fpVSIL, nNICOM * 80, SEEK_CUR) == 0; /* skip comments */ + /* Set to IC position */ + bool bOK = VSIFSeekL( fpVSIL, nICOffset, SEEK_SET) == 0; /* Read IC */ char szICBuf[2]; diff --git a/gdal/frmts/nitf/nitfdataset.h b/gdal/frmts/nitf/nitfdataset.h index 4753854ebc57..0dd3830b52c1 100644 --- a/gdal/frmts/nitf/nitfdataset.h +++ b/gdal/frmts/nitf/nitfdataset.h @@ -72,6 +72,7 @@ class NITFDataset final: public GDALPamDataset GDALDataset *poJ2KDataset; int bJP2Writing; + int m_nICOffset = 0; GDALDataset *poJPEGDataset; diff --git a/gdal/frmts/nitf/nitffile.c b/gdal/frmts/nitf/nitffile.c index eea9da38cd5c..e6e4265169ed 100644 --- a/gdal/frmts/nitf/nitffile.c +++ b/gdal/frmts/nitf/nitffile.c @@ -538,6 +538,16 @@ int NITFCreate( const char *pszFilename, int nBitsPerSample, const char *pszPVType, char **papszOptions ) +{ + return NITFCreateEx(pszFilename, nPixels, nLines, nBands, nBitsPerSample, + pszPVType, papszOptions, NULL); +} + +int NITFCreateEx( const char *pszFilename, + int nPixels, int nLines, int nBands, + int nBitsPerSample, const char *pszPVType, + char **papszOptions, int* pnICOffset ) + { VSILFILE *fp; GUIntBig nCur = 0; @@ -1010,6 +1020,8 @@ int NITFCreate( const char *pszFilename, } } + if( pnICOffset && iIM == 0 ) + *pnICOffset = (int)(nCur+nOffset+1); OVR( 2,nCur+nOffset+1, IC , "NC" ); if( pszIC[0] != 'N' ) diff --git a/gdal/frmts/nitf/nitflib.h b/gdal/frmts/nitf/nitflib.h index a09d14040c9a..eecab5a0c037 100644 --- a/gdal/frmts/nitf/nitflib.h +++ b/gdal/frmts/nitf/nitflib.h @@ -94,6 +94,11 @@ int CPL_DLL NITFCreate( const char *pszFilename, int nBitsPerSample, const char *pszPVType, char **papszOptions ); +int NITFCreateEx( const char *pszFilename, + int nPixels, int nLines, int nBands, + int nBitsPerSample, const char *pszPVType, + char **papszOptions, int* pnICOffset ); + const char CPL_DLL *NITFFindTRE( const char *pszTREData, int nTREBytes, const char *pszTag, int *pnFoundTRESize ); const char CPL_DLL *NITFFindTREByIndex( const char *pszTREData, int nTREBytes,