Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: NITF Read/Write Library
4 : * Purpose: Module implement BILEVEL (C1) compressed image reading.
5 : * Author: Frank Warmerdam, warmerdam@pobox.com
6 : *
7 : **********************************************************************
8 : * Copyright (c) 2007, Frank Warmerdam
9 : * Copyright (c) 2009, Even Rouault <even dot rouault at spatialys.com>
10 : *
11 : * SPDX-License-Identifier: MIT
12 : ****************************************************************************/
13 :
14 : #include "cpl_port.h"
15 : #include "nitflib.h"
16 :
17 : #include <cstring>
18 :
19 : #include "cpl_conv.h"
20 : #include "cpl_string.h"
21 : #include "cpl_vsi.h"
22 : #include "gdal.h"
23 : // #include "tiff.h"
24 : CPL_C_START
25 : #include "tiffio.h"
26 : CPL_C_END
27 : #include "tifvsi.h"
28 :
29 : /************************************************************************/
30 : /* NITFUncompressBILEVEL() */
31 : /************************************************************************/
32 :
33 1 : int NITFUncompressBILEVEL(NITFImage *psImage, GByte *pabyInputData,
34 : int nInputBytes, GByte *pabyOutputImage)
35 :
36 : {
37 : /* -------------------------------------------------------------------- */
38 : /* Write memory TIFF with the bilevel data. */
39 : /* -------------------------------------------------------------------- */
40 1 : const int nOutputBytes =
41 1 : (psImage->nBlockWidth * psImage->nBlockHeight + 7) / 8;
42 :
43 : const CPLString osFilename(
44 2 : VSIMemGenerateHiddenFilename("nitf_bilevel.tif"));
45 1 : VSILFILE *fpL = VSIFOpenL(osFilename, "w+");
46 1 : if (fpL == nullptr)
47 0 : return FALSE;
48 1 : TIFF *hTIFF = VSI_TIFFOpen(osFilename, "w+", fpL);
49 1 : if (hTIFF == nullptr)
50 : {
51 0 : CPL_IGNORE_RET_VAL(VSIFCloseL(fpL));
52 0 : return FALSE;
53 : }
54 :
55 1 : TIFFSetField(hTIFF, TIFFTAG_IMAGEWIDTH, psImage->nBlockWidth);
56 1 : TIFFSetField(hTIFF, TIFFTAG_IMAGELENGTH, psImage->nBlockHeight);
57 1 : TIFFSetField(hTIFF, TIFFTAG_BITSPERSAMPLE, 1);
58 1 : TIFFSetField(hTIFF, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
59 1 : TIFFSetField(hTIFF, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
60 1 : TIFFSetField(hTIFF, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
61 :
62 1 : TIFFSetField(hTIFF, TIFFTAG_ROWSPERSTRIP, psImage->nBlockHeight);
63 1 : TIFFSetField(hTIFF, TIFFTAG_SAMPLESPERPIXEL, 1);
64 1 : TIFFSetField(hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
65 1 : TIFFSetField(hTIFF, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX3);
66 :
67 1 : if (psImage->szCOMRAT[0] == '2')
68 1 : TIFFSetField(hTIFF, TIFFTAG_GROUP3OPTIONS, GROUP3OPT_2DENCODING);
69 :
70 1 : TIFFWriteRawStrip(hTIFF, 0, pabyInputData, nInputBytes);
71 1 : TIFFWriteDirectory(hTIFF);
72 :
73 1 : TIFFClose(hTIFF);
74 :
75 : /* -------------------------------------------------------------------- */
76 : /* Now open and read it back. */
77 : /* -------------------------------------------------------------------- */
78 1 : bool bResult = true;
79 :
80 1 : hTIFF = VSI_TIFFOpen(osFilename, "r", fpL);
81 1 : if (hTIFF == nullptr)
82 : {
83 0 : CPL_IGNORE_RET_VAL(VSIFCloseL(fpL));
84 0 : return FALSE;
85 : }
86 :
87 1 : if (TIFFReadEncodedStrip(hTIFF, 0, pabyOutputImage, nOutputBytes) == -1)
88 : {
89 0 : memset(pabyOutputImage, 0, nOutputBytes);
90 0 : bResult = false;
91 : }
92 :
93 1 : TIFFClose(hTIFF);
94 1 : CPL_IGNORE_RET_VAL(VSIFCloseL(fpL));
95 :
96 1 : VSIUnlink(osFilename);
97 :
98 1 : return bResult;
99 : }
|