Skip to content

Commit

Permalink
NITF: fix writing of COMRAT field in JPEG/JPEG2000 mode, when there a…
Browse files Browse the repository at this point in the history
…re FILE TREs
  • Loading branch information
rouault committed Jun 9, 2021
1 parent 9f314e7 commit 0decd82
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 44 deletions.
60 changes: 16 additions & 44 deletions gdal/frmts/nitf/nitfdataset.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -204,7 +205,7 @@ int NITFDataset::CloseDependentDatasets()

CPL_IGNORE_RET_VAL(
NITFPatchImageLength( GetDescription(), nImageStart, nPixelCount,
"C8", nullptr ));
"C8", m_nICOffset, nullptr ));
}

bJP2Writing = FALSE;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -5221,6 +5225,7 @@ static bool NITFPatchImageLength( const char *pszFilename,
GUIntBig nImageOffset,
GIntBig nPixelCount,
const char *pszIC,
int nICOffset,
CSLConstList papszCreationOptions )

{
Expand Down Expand Up @@ -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];
Expand Down
1 change: 1 addition & 0 deletions gdal/frmts/nitf/nitfdataset.h
Expand Up @@ -72,6 +72,7 @@ class NITFDataset final: public GDALPamDataset

GDALDataset *poJ2KDataset;
int bJP2Writing;
int m_nICOffset = 0;

GDALDataset *poJPEGDataset;

Expand Down
12 changes: 12 additions & 0 deletions gdal/frmts/nitf/nitffile.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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' )
Expand Down
5 changes: 5 additions & 0 deletions gdal/frmts/nitf/nitflib.h
Expand Up @@ -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,
Expand Down

0 comments on commit 0decd82

Please sign in to comment.