1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
/** \file
* \brief ECW - ECW JPEG 2000
*
* See Copyright Notice in im_lib.h
*/
#include "im_format.h"
#include "im_util.h"
#include "im_format_ecw.h"
#include "im_counter.h"
#include <NCSECWClient.h>
// #include <NCSEcwCompressClient.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
static const char* iECWCompTable[2] =
{
"ECW",
"JPEG-2000",
};
class imFormatECW: public imFormat
{
NCSFileView *pNCSFileView;
// NCSEcwCompressClient *pClient;
public:
imFormatECW()
:imFormat("ECW",
"ECW JPEG-2000 File Format",
"*.ecw;*.jp2;*.j2k;*.jpc;*.j2c;",
iECWCompTable,
2,
0)
{}
int Open(const char* file_name);
int New(const char* file_name);
void Close();
void* Handle(int index);
int ReadImageInfo(int index);
int ReadImageData(void* data);
int WriteImageInfo(){return 0;} // do nothing for now;
int WriteImageData(void* data){(void)data; return 0;} // do nothing for now;
int CanWrite(const char* compression, int color_mode, int data_type) const;
};
void imFormatRegisterECW(void)
{
imFormatRegister(new imFormatECW());
}
int imFormatECW::Open(const char* file_name)
{
NCSError eError = NCScbmOpenFileView((char*)file_name, &this->pNCSFileView, NULL);
if (eError != NCS_SUCCESS)
{
if (eError == NCS_FILE_OPEN_ERROR ||
eError == NCS_FILE_NOT_FOUND ||
eError == NCS_FILE_INVALID)
return IM_ERR_OPEN;
else if (eError == NCS_FILE_OPEN_FAILED)
return IM_ERR_FORMAT;
else
return IM_ERR_ACCESS;
}
NCSFileType fileType = NCScbmGetFileType(this->pNCSFileView);
if (fileType == NCS_FILE_ECW)
strcpy(this->compression, "ECW");
else if (fileType == NCS_FILE_JP2)
strcpy(this->compression, "JPEG-2000");
else
return IM_ERR_COMPRESS;
this->image_count = 1;
return IM_ERR_NONE;
}
int imFormatECW::New(const char* file_name)
{
strcpy(this->compression, "JPEG-2000");
this->image_count = 1;
(void)file_name;
return IM_ERR_FORMAT;
}
void imFormatECW::Close()
{
if (this->is_new)
;// NCSEcwCompressClose(this->pClient);
else
NCScbmCloseFileView(this->pNCSFileView);
}
void* imFormatECW::Handle(int index)
{
(void)index;
if (this->is_new)
return NULL; // return (void*)this->pClient;
else
return (void*)this->pNCSFileView;
}
int imFormatECW::ReadImageInfo(int index)
{
NCSFileViewFileInfoEx *pNCSFileInfo;
imAttribTable* attrib_table = AttribTable();
(void)index;
if (NCScbmGetViewFileInfoEx(this->pNCSFileView, &pNCSFileInfo) != NCS_SUCCESS)
return IM_ERR_ACCESS;
this->width = pNCSFileInfo->nSizeX;
this->height = pNCSFileInfo->nSizeY;
switch(pNCSFileInfo->eColorSpace)
{
case NCSCS_GREYSCALE:
this->file_color_mode = IM_GRAY;
break;
case NCSCS_YUV:
case NCSCS_sRGB:
this->file_color_mode = IM_RGB;
break;
case NCSCS_YCbCr:
this->file_color_mode = IM_YCBCR;
break;
case NCSCS_MULTIBAND:
/* multiband data, we read only one band */
this->file_color_mode = IM_GRAY;
attrib_table->Set("MultiBandCount", IM_USHORT, 1, (void*)&pNCSFileInfo->nBands);
break;
default:
return IM_ERR_DATA;
}
switch(pNCSFileInfo->eCellType)
{
case NCSCT_INT8:
case NCSCT_UINT8:
this->file_data_type = IM_BYTE;
break;
case NCSCT_INT16:
case NCSCT_UINT16:
this->file_data_type = IM_USHORT;
break;
case NCSCT_UINT64:
case NCSCT_INT64:
case NCSCT_UINT32:
case NCSCT_INT32:
// Should be: this->file_data_type = IM_INT;
// but 32bits ints are not supported by the NCScbmReadViewLineBILEx function
this->file_data_type = IM_USHORT;
break;
case NCSCT_IEEE4:
case NCSCT_IEEE8:
this->file_data_type = IM_FLOAT;
break;
default:
return IM_ERR_DATA;
}
int prec = pNCSFileInfo->pBands->nBits;
if (prec < 8)
this->convert_bpp = -prec; // just expand to 0-255
if (prec == 1 && this->file_color_mode == IM_GRAY)
this->file_color_mode = IM_BINARY;
if (pNCSFileInfo->nBands > imColorModeDepth(this->file_color_mode))
this->file_color_mode |= IM_ALPHA;
if (this->file_color_mode != IM_GRAY)
this->file_color_mode |= IM_PACKED;
this->file_color_mode |= IM_TOPDOWN;
float float_value = (float)pNCSFileInfo->fOriginX;
attrib_table->Set("OriginX", IM_FLOAT, 1, (void*)&float_value);
float_value = (float)pNCSFileInfo->fOriginY;
attrib_table->Set("OriginY", IM_FLOAT, 1, (void*)&float_value);
float_value = (float)pNCSFileInfo->fCWRotationDegrees;
attrib_table->Set("Rotation", IM_FLOAT, 1, (void*)&float_value);
float_value = (float)pNCSFileInfo->fCellIncrementX;
attrib_table->Set("CellIncrementX", IM_FLOAT, 1, (void*)&float_value);
float_value = (float)pNCSFileInfo->fCellIncrementY;
attrib_table->Set("CellIncrementY", IM_FLOAT, 1, (void*)&float_value);
attrib_table->Set("Datum", IM_BYTE, strlen(pNCSFileInfo->szDatum)+1, pNCSFileInfo->szDatum);
attrib_table->Set("Projection", IM_BYTE, strlen(pNCSFileInfo->szProjection)+1, pNCSFileInfo->szProjection);
switch (pNCSFileInfo->eCellSizeUnits)
{
case ECW_CELL_UNITS_INVALID:
attrib_table->Set("CellUnits", IM_BYTE, 8, "INVALID");
break;
case ECW_CELL_UNITS_METERS:
attrib_table->Set("CellUnits", IM_BYTE, 7, "METERS");
break;
case ECW_CELL_UNITS_DEGREES:
attrib_table->Set("CellUnits", IM_BYTE, 7, "DEGREES");
break;
case ECW_CELL_UNITS_FEET:
attrib_table->Set("CellUnits", IM_BYTE, 5, "FEET");
break;
case ECW_CELL_UNITS_UNKNOWN:
attrib_table->Set("CellUnits", IM_BYTE, 8, "UNKNOWN");
break;
}
float_value = (float)pNCSFileInfo->nCompressionRate;
attrib_table->Set("CompressionRatio", IM_FLOAT, 1, (void*)&float_value);
return IM_ERR_NONE;
}
static void iCopyDataBuffer(UINT8 **ppOutputLine, imbyte* line_buffer, int nBands, int view_width, int type_size)
{
if (nBands > 1)
{
for(int i = 0; i < view_width; i++)
{
for(int j = 0; j < nBands; j++)
{
for(int k = 0; k < type_size; k++)
{
*line_buffer++ = (ppOutputLine[j])[i*type_size + k];
}
}
}
}
else
memcpy(line_buffer, ppOutputLine[0], nBands*type_size*view_width);
}
int imFormatECW::ReadImageData(void* data)
{
imAttribTable* attrib_table = AttribTable();
int i, *attrib_data, view_width, view_height,
nBands = imColorModeDepth(this->file_color_mode);
// this size is free, can be anything, but we restricted to less than the image size
attrib_data = (int*)attrib_table->Get("ViewWidth");
view_width = attrib_data? *attrib_data: this->width;
if (view_width > this->width) view_width = this->width;
attrib_data = (int*)attrib_table->Get("ViewHeight");
view_height = attrib_data? *attrib_data: this->height;
if (view_height > this->height) view_height = this->height;
imCounterTotal(this->counter, view_height, "Reading ECW...");
{
int xmin, xmax, ymin, ymax, band_start;
// full image if not defined.
// this size must be inside the image
attrib_data = (int*)attrib_table->Get("ViewXmin");
xmin = attrib_data? *attrib_data: 0;
if (xmin < 0) xmin = 0;
attrib_data = (int*)attrib_table->Get("ViewYmin");
ymin = attrib_data? *attrib_data: 0;
if (ymin < 0) ymin = 0;
attrib_data = (int*)attrib_table->Get("ViewXmax");
xmax = attrib_data? *attrib_data: this->width-1;
if (xmax > this->width-1) xmax = this->width-1;
attrib_data = (int*)attrib_table->Get("ViewYmax");
ymax = attrib_data? *attrib_data: this->height-1;
if (ymax > this->height-1) ymax = this->height-1;
band_start = 0;
UINT16* start_plane = (UINT16*)attrib_table->Get("MultiBandSelect");
if (start_plane)
band_start = *start_plane;
UINT32 *pBandList = (UINT32*)malloc(sizeof(UINT32)*nBands);
for(i = 0; i < nBands; i++)
pBandList[i] = i+band_start;
NCSError eError = NCScbmSetFileView(this->pNCSFileView, nBands, pBandList,
xmin, ymin, xmax, ymax,
view_width, view_height);
free(pBandList);
if( eError != NCS_SUCCESS)
return IM_ERR_DATA;
}
// this is necessary to fool line buffer management
this->width = view_width;
this->height = view_height;
this->line_buffer_size = imImageLineSize(this->width, this->file_color_mode, this->file_data_type);
NCSEcwCellType eType = NCSCT_UINT8;
int type_size = 1;
if (this->file_data_type == IM_USHORT)
{
eType = NCSCT_UINT16;
type_size = 2;
}
else if (this->file_data_type == IM_FLOAT)
{
eType = NCSCT_IEEE4;
type_size = 4;
}
UINT8 **ppOutputLine = (UINT8**)malloc(sizeof(UINT8*)*nBands);
UINT8 *ppOutputBuffer = (UINT8*)malloc(type_size*view_width*nBands);
for(i = 0; i < nBands; i++)
ppOutputLine[i] = ppOutputBuffer + i*type_size*view_width;
for (int row = 0; row < view_height; row++)
{
NCSEcwReadStatus eError = NCScbmReadViewLineBILEx(this->pNCSFileView, eType, (void**)ppOutputLine);
if( eError != NCS_SUCCESS)
{
free(ppOutputLine);
free(ppOutputBuffer);
return IM_ERR_DATA;
}
iCopyDataBuffer(ppOutputLine, (imbyte*)this->line_buffer, nBands, view_width, type_size);
imFileLineBufferRead(this, data, row, 0);
if (!imCounterInc(this->counter))
{
free(ppOutputLine);
free(ppOutputBuffer);
return IM_ERR_COUNTER;
}
}
free(ppOutputLine);
free(ppOutputBuffer);
return IM_ERR_NONE;
}
int imFormatECW::CanWrite(const char* compression, int color_mode, int data_type) const
{
(void)compression;
(void)color_mode;
(void)data_type;
return IM_ERR_DATA;
//int color_space = imColorModeSpace(color_mode);
//if (color_space != IM_GRAY && color_space != IM_RGB)// && color_space != IM_LUV)
// return IM_ERR_DATA;
//
//if (data_type != IM_BYTE && data_type != IM_USHORT && data_type != IM_FLOAT)
// return IM_ERR_DATA;
//if (!compression || compression[0] == 0)
// return IM_ERR_NONE;
//if (!imStrEqual(compression, "JPEG-2000"))
// return IM_ERR_COMPRESS;
//return IM_ERR_NONE;
}
|