/src/gdal/frmts/nitf/nitfbilevel.cpp
Line | Count | Source |
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 | | int NITFUncompressBILEVEL(NITFImage *psImage, GByte *pabyInputData, |
34 | | int nInputBytes, GByte *pabyOutputImage) |
35 | | |
36 | 15 | { |
37 | | /* -------------------------------------------------------------------- */ |
38 | | /* Write memory TIFF with the bilevel data. */ |
39 | | /* -------------------------------------------------------------------- */ |
40 | 15 | const int nOutputBytes = |
41 | 15 | (psImage->nBlockWidth * psImage->nBlockHeight + 7) / 8; |
42 | | |
43 | 15 | const CPLString osFilename( |
44 | 15 | VSIMemGenerateHiddenFilename("nitf_bilevel.tif")); |
45 | 15 | VSILFILE *fpL = VSIFOpenL(osFilename, "w+"); |
46 | 15 | if (fpL == nullptr) |
47 | 0 | return FALSE; |
48 | 15 | TIFF *hTIFF = VSI_TIFFOpen(osFilename, "w+", fpL); |
49 | 15 | if (hTIFF == nullptr) |
50 | 0 | { |
51 | 0 | CPL_IGNORE_RET_VAL(VSIFCloseL(fpL)); |
52 | 0 | return FALSE; |
53 | 0 | } |
54 | | |
55 | 15 | TIFFSetField(hTIFF, TIFFTAG_IMAGEWIDTH, psImage->nBlockWidth); |
56 | 15 | TIFFSetField(hTIFF, TIFFTAG_IMAGELENGTH, psImage->nBlockHeight); |
57 | 15 | TIFFSetField(hTIFF, TIFFTAG_BITSPERSAMPLE, 1); |
58 | 15 | TIFFSetField(hTIFF, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT); |
59 | 15 | TIFFSetField(hTIFF, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
60 | 15 | TIFFSetField(hTIFF, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB); |
61 | | |
62 | 15 | TIFFSetField(hTIFF, TIFFTAG_ROWSPERSTRIP, psImage->nBlockHeight); |
63 | 15 | TIFFSetField(hTIFF, TIFFTAG_SAMPLESPERPIXEL, 1); |
64 | 15 | TIFFSetField(hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); |
65 | 15 | TIFFSetField(hTIFF, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX3); |
66 | | |
67 | 15 | if (psImage->szCOMRAT[0] == '2') |
68 | 12 | TIFFSetField(hTIFF, TIFFTAG_GROUP3OPTIONS, GROUP3OPT_2DENCODING); |
69 | | |
70 | 15 | TIFFWriteRawStrip(hTIFF, 0, pabyInputData, nInputBytes); |
71 | 15 | TIFFWriteDirectory(hTIFF); |
72 | | |
73 | 15 | TIFFClose(hTIFF); |
74 | | |
75 | | /* -------------------------------------------------------------------- */ |
76 | | /* Now open and read it back. */ |
77 | | /* -------------------------------------------------------------------- */ |
78 | 15 | bool bResult = true; |
79 | | |
80 | 15 | hTIFF = VSI_TIFFOpen(osFilename, "r", fpL); |
81 | 15 | if (hTIFF == nullptr) |
82 | 0 | { |
83 | 0 | CPL_IGNORE_RET_VAL(VSIFCloseL(fpL)); |
84 | 0 | return FALSE; |
85 | 0 | } |
86 | | |
87 | 15 | if (TIFFReadEncodedStrip(hTIFF, 0, pabyOutputImage, nOutputBytes) == -1) |
88 | 4 | { |
89 | 4 | memset(pabyOutputImage, 0, nOutputBytes); |
90 | 4 | bResult = false; |
91 | 4 | } |
92 | | |
93 | 15 | TIFFClose(hTIFF); |
94 | 15 | CPL_IGNORE_RET_VAL(VSIFCloseL(fpL)); |
95 | | |
96 | 15 | VSIUnlink(osFilename); |
97 | | |
98 | 15 | return bResult; |
99 | 15 | } |