1 : /******************************************************************************
2 : * $Id: nitfbilevel.cpp 16416 2009-02-25 21:13:08Z rouault $
3 : *
4 : * Project: NITF Read/Write Library
5 : * Purpose: Module implement BILEVEL (C1) compressed image reading.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : **********************************************************************
9 : * Copyright (c) 2007, Frank Warmerdam
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "gdal.h"
31 : #include "nitflib.h"
32 : #include "cpl_conv.h"
33 : #include "cpl_string.h"
34 : #include "cpl_multiproc.h"
35 :
36 : CPL_C_START
37 : #include "tiffio.h"
38 : CPL_C_END
39 :
40 : TIFF* VSI_TIFFOpen(const char* name, const char* mode);
41 :
42 : CPL_CVSID("$Id: nitfbilevel.cpp 16416 2009-02-25 21:13:08Z rouault $");
43 :
44 : /************************************************************************/
45 : /* NITFUncompressBILEVEL() */
46 : /************************************************************************/
47 :
48 1 : int NITFUncompressBILEVEL( NITFImage *psImage,
49 : GByte *pabyInputData, int nInputBytes,
50 : GByte *pabyOutputImage )
51 :
52 : {
53 1 : int nOutputBytes= (psImage->nBlockWidth * psImage->nBlockHeight + 7)/8;
54 :
55 : /* -------------------------------------------------------------------- */
56 : /* Write memory TIFF with the bilevel data. */
57 : /* -------------------------------------------------------------------- */
58 1 : CPLString osFilename;
59 :
60 1 : osFilename.Printf( "/vsimem/nitf-wrk-%ld.tif", (long) CPLGetPID() );
61 :
62 1 : TIFF *hTIFF = VSI_TIFFOpen( osFilename, "w+" );
63 1 : if (hTIFF == NULL)
64 : {
65 0 : return FALSE;
66 : }
67 :
68 1 : TIFFSetField( hTIFF, TIFFTAG_IMAGEWIDTH, psImage->nBlockWidth );
69 1 : TIFFSetField( hTIFF, TIFFTAG_IMAGELENGTH, psImage->nBlockHeight );
70 1 : TIFFSetField( hTIFF, TIFFTAG_BITSPERSAMPLE, 1 );
71 1 : TIFFSetField( hTIFF, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT );
72 1 : TIFFSetField( hTIFF, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );
73 1 : TIFFSetField( hTIFF, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB );
74 :
75 1 : TIFFSetField( hTIFF, TIFFTAG_ROWSPERSTRIP, psImage->nBlockHeight );
76 1 : TIFFSetField( hTIFF, TIFFTAG_SAMPLESPERPIXEL, 1 );
77 1 : TIFFSetField( hTIFF, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK );
78 1 : TIFFSetField( hTIFF, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX3 );
79 :
80 1 : if( psImage->szCOMRAT[0] == '2' )
81 1 : TIFFSetField( hTIFF, TIFFTAG_GROUP3OPTIONS, GROUP3OPT_2DENCODING );
82 :
83 1 : TIFFWriteRawStrip( hTIFF, 0, pabyInputData, nInputBytes );
84 1 : TIFFWriteDirectory( hTIFF );
85 :
86 1 : TIFFClose( hTIFF );
87 :
88 : /* -------------------------------------------------------------------- */
89 : /* Now open and read it back. */
90 : /* -------------------------------------------------------------------- */
91 1 : int bResult = TRUE;
92 :
93 1 : hTIFF = VSI_TIFFOpen( osFilename, "r" );
94 1 : if (hTIFF == NULL)
95 : {
96 0 : return FALSE;
97 : }
98 :
99 :
100 1 : if( TIFFReadEncodedStrip( hTIFF, 0, pabyOutputImage, nOutputBytes ) == -1 )
101 : {
102 0 : memset( pabyOutputImage, 0, nOutputBytes );
103 0 : bResult = FALSE;
104 : }
105 :
106 1 : TIFFClose( hTIFF );
107 :
108 1 : VSIUnlink( osFilename );
109 :
110 1 : return bResult;
111 : }
|