1 : /******************************************************************************
2 : * $Id: vsiiostream.h 10957 2007-03-13 18:59:03Z warmerdam $
3 : *
4 : * Project: GDAL
5 : * Purpose: ECW Driver: virtualized io stream declaration.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2004, Frank Warmerdam <warmerdam@pobox.com>
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 : #ifndef VSIIOSTREAM_H_INCLUDED
31 : #define VSIIOSTREAM_H_INCLUDED
32 :
33 : #include "cpl_vsi.h"
34 : #include "gdal_priv.h"
35 : #include "gdal_frmts.h"
36 :
37 : #ifdef FRMT_ecw
38 :
39 : /* -------------------------------------------------------------------- */
40 : /* These definitions aren't really specific to the VSIIOStream, */
41 : /* but are shared amoung the ECW driver modules. */
42 : /* -------------------------------------------------------------------- */
43 : #include <NCSECWClient.h>
44 : #include <NCSECWCompressClient.h>
45 : #include <NCSErrors.h>
46 : #include <NCSFile.h>
47 : #include <NCSJP2File.h>
48 : #include <NCSJP2FileView.h>
49 :
50 : /* As of July 2002 only uncompress support is available on Unix */
51 : #define HAVE_COMPRESS
52 :
53 : #ifdef HAVE_COMPRESS
54 : GDALDataset *
55 : ECWCreateCopyECW( const char * pszFilename, GDALDataset *poSrcDS,
56 : int bStrict, char ** papszOptions,
57 : GDALProgressFunc pfnProgress, void * pProgressData );
58 : GDALDataset *
59 : ECWCreateCopyJPEG2000( const char * pszFilename, GDALDataset *poSrcDS,
60 : int bStrict, char ** papszOptions,
61 : GDALProgressFunc pfnProgress, void * pProgressData );
62 :
63 : GDALDataset *
64 : ECWCreateECW( const char * pszFilename, int nXSize, int nYSize, int nBands,
65 : GDALDataType eType, char **papszOptions );
66 : GDALDataset *
67 : ECWCreateJPEG2000(const char *pszFilename, int nXSize, int nYSize, int nBands,
68 : GDALDataType eType, char **papszOptions );
69 : #endif
70 :
71 : /************************************************************************/
72 : /* ==================================================================== */
73 : /* VSIIOStream */
74 : /* ==================================================================== */
75 : /************************************************************************/
76 :
77 : class VSIIOStream : public CNCSJPCIOStream
78 :
79 : {
80 : public:
81 :
82 : INT64 startOfJPData;
83 : INT64 lengthOfJPData;
84 : FILE *fpVSIL;
85 : int bWritable;
86 : int nFileViewCount;
87 : char *pszFilename;
88 :
89 48 : VSIIOStream() {
90 48 : nFileViewCount = 0;
91 48 : startOfJPData = 0;
92 48 : lengthOfJPData = -1;
93 48 : fpVSIL = NULL;
94 48 : }
95 63 : virtual ~VSIIOStream() {
96 48 : Close();
97 48 : if( fpVSIL != NULL )
98 : {
99 18 : VSIFCloseL( fpVSIL );
100 18 : fpVSIL = NULL;
101 : }
102 63 : }
103 :
104 18 : virtual CNCSError Access( FILE *fpVSILIn, BOOLEAN bWrite,
105 : const char *pszFilename,
106 : INT64 start, INT64 size = -1) {
107 :
108 18 : fpVSIL = fpVSILIn;
109 18 : startOfJPData = start;
110 18 : lengthOfJPData = size;
111 18 : bWritable = bWrite;
112 18 : VSIFSeekL(fpVSIL, startOfJPData, SEEK_SET);
113 :
114 18 : return(CNCSJPCIOStream::Open((char *)pszFilename, (bool) bWrite));
115 : }
116 :
117 147 : virtual bool NCS_FASTCALL Seek() {
118 147 : return(true);
119 : }
120 :
121 955 : virtual bool NCS_FASTCALL Seek(INT64 offset, Origin origin = CURRENT) {
122 955 : switch(origin) {
123 : case START:
124 903 : return(0 == VSIFSeekL(fpVSIL, offset+startOfJPData, SEEK_SET));
125 :
126 : case CURRENT:
127 0 : return(0 == VSIFSeekL(fpVSIL, offset, SEEK_CUR));
128 :
129 : case END:
130 52 : return(0 == VSIFSeekL(fpVSIL, offset, SEEK_END));
131 : }
132 :
133 0 : return(false);
134 : }
135 :
136 3883 : virtual INT64 NCS_FASTCALL Tell() {
137 3883 : return VSIFTellL( fpVSIL ) - startOfJPData;
138 : }
139 :
140 52 : virtual INT64 NCS_FASTCALL Size() {
141 52 : if( lengthOfJPData != -1 )
142 0 : return lengthOfJPData;
143 : else
144 : {
145 52 : INT64 curPos = Tell(), size;
146 :
147 52 : Seek( 0, END );
148 52 : size = Tell();
149 52 : Seek( curPos, START );
150 :
151 52 : return size;
152 : }
153 : }
154 :
155 7298 : virtual bool NCS_FASTCALL Read(void* buffer, UINT32 count) {
156 7298 : if( count == 0 )
157 561 : return true;
158 :
159 : // return(1 == VSIFReadL( buffer, count, 1, fpVSIL ) );
160 :
161 : // The following is a hack
162 6737 : if( VSIFReadL( buffer, count, 1, fpVSIL ) != 1 )
163 : {
164 : CPLDebug( "VSIIOSTREAM",
165 : "Read(%d) failed @ %d, ignoring failure.",
166 5 : count, (int) (VSIFTellL( fpVSIL ) - startOfJPData) );
167 : }
168 :
169 6737 : return true;
170 : }
171 :
172 1584 : virtual bool NCS_FASTCALL Write(void* buffer, UINT32 count) {
173 1584 : if( count == 0 )
174 0 : return true;
175 1584 : return(1 == VSIFWriteL(buffer, count, 1, fpVSIL));
176 : }
177 : };
178 :
179 : #endif /* def FRMT_ecw */
180 :
181 : #endif /* ndef VSIIOSTREAM_H_INCLUDED */
182 :
|