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
|
/** \file
* \brief RAW File Format
*
* See Copyright Notice in im_lib.h
* $Id: im_format_raw.cpp,v 1.5 2009/10/01 14:15:47 scuri Exp $
*/
#include "im_format.h"
#include "im_util.h"
#include "im_format_raw.h"
#include "im_counter.h"
#include "im_binfile.h"
#include <stdlib.h>
#include <string.h>
static const char* iRAWCompTable[2] =
{
"NONE",
"ASCII"
};
class imFileFormatRAW: public imFileFormatBase
{
imBinFile* handle;
int padding;
int iRawUpdateParam(int index);
public:
imFileFormatRAW(const imFormat* _iformat): imFileFormatBase(_iformat) {}
~imFileFormatRAW() {}
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();
int WriteImageData(void* data);
};
class imFormatRAW: public imFormat
{
public:
imFormatRAW()
:imFormat("RAW",
"RAW File Format",
"*.*;",
iRAWCompTable,
2,
1)
{}
~imFormatRAW() {}
imFileFormatBase* Create(void) const { return new imFileFormatRAW(this); }
int CanWrite(const char* compression, int color_mode, int data_type) const;
};
static imFormat* raw_format = NULL;
void imFormatFinishRAW(void)
{
if (raw_format)
{
delete raw_format;
raw_format = NULL;
}
}
imFormat* imFormatInitRAW(void)
{
if (!raw_format)
raw_format = new imFormatRAW();
return raw_format;
}
int imFileFormatRAW::Open(const char* file_name)
{
this->handle = imBinFileOpen(file_name);
if (this->handle == NULL)
return IM_ERR_OPEN;
strcpy(this->compression, "NONE");
this->image_count = 1; /* at least one image */
this->padding = 0;
return IM_ERR_NONE;
}
int imFileFormatRAW::New(const char* file_name)
{
this->handle = imBinFileNew(file_name);
if (this->handle == NULL)
return IM_ERR_OPEN;
this->padding = 0;
return IM_ERR_NONE;
}
void imFileFormatRAW::Close()
{
imBinFileClose(this->handle);
}
void* imFileFormatRAW::Handle(int index)
{
if (index == 0)
return (void*)this->handle;
else
return NULL;
}
static int iCalcPad(int padding, int line_size)
{
if (padding == 1)
return 0;
{
int rest = line_size % padding;
if (rest == 0)
return 0;
return padding - rest;
}
}
int imFileFormatRAW::iRawUpdateParam(int index)
{
(void)index;
imAttribTable* attrib_table = AttribTable();
// update image count
int* icount = (int*)attrib_table->Get("ImageCount");
if (icount)
this->image_count = *icount;
else
this->image_count = 1;
// update file byte order
int* byte_order = (int*)attrib_table->Get("ByteOrder");
if (byte_order)
imBinFileByteOrder(this->handle, *byte_order);
// position at start offset, the default is at 0
int* start_offset = (int*)attrib_table->Get("StartOffset");
if (!start_offset)
imBinFileSeekOffset(this->handle, 0);
else
imBinFileSeekOffset(this->handle, *start_offset);
if (imBinFileError(this->handle))
return IM_ERR_ACCESS;
int* stype = (int*)attrib_table->Get("SwitchType");
if (stype)
this->switch_type = *stype;
// The following attributes MUST exist
this->width = *(int*)attrib_table->Get("Width");
this->height = *(int*)attrib_table->Get("Height");
this->file_color_mode = *(int*)attrib_table->Get("ColorMode");
this->file_data_type = *(int*)attrib_table->Get("DataType");
int* pad = (int*)attrib_table->Get("Padding");
if (pad)
{
int line_size = imImageLineSize(this->width, this->file_color_mode, this->file_data_type);
if (this->switch_type && (this->file_data_type == IM_FLOAT || this->file_data_type == IM_CFLOAT))
line_size *= 2;
this->padding = iCalcPad(*pad, line_size);
}
return IM_ERR_NONE;
}
int imFileFormatRAW::ReadImageInfo(int index)
{
return iRawUpdateParam(index);
}
int imFileFormatRAW::WriteImageInfo()
{
this->file_color_mode = this->user_color_mode;
this->file_data_type = this->user_data_type;
return iRawUpdateParam(this->image_count);
}
static int iFileDataTypeSize(int file_data_type, int switch_type)
{
int type_size = imDataTypeSize(file_data_type);
if ((file_data_type == IM_FLOAT || file_data_type == IM_CFLOAT) && switch_type)
type_size *= 2;
return type_size;
}
int imFileFormatRAW::ReadImageData(void* data)
{
int count = imFileLineBufferCount(this);
int line_count = imImageLineCount(this->width, this->file_color_mode);
int type_size = iFileDataTypeSize(this->file_data_type, this->switch_type);
// treat complex as 2 real
if (this->file_data_type == IM_CFLOAT)
{
type_size /= 2;
line_count *= 2;
}
int ascii;
if (imStrEqual(this->compression, "ASCII"))
ascii = 1;
else
ascii = 0;
imCounterTotal(this->counter, count, "Reading RAW...");
int row = 0, plane = 0;
for (int i = 0; i < count; i++)
{
if (ascii)
{
for (int col = 0; col < line_count; col++)
{
if (this->file_data_type == IM_FLOAT)
{
float value;
if (!imBinFileReadFloat(handle, &value))
return IM_ERR_ACCESS;
((float*)this->line_buffer)[col] = value;
}
else
{
int value;
if (!imBinFileReadInteger(handle, &value))
return IM_ERR_ACCESS;
if (this->file_data_type == IM_INT)
((int*)this->line_buffer)[col] = value;
else if (this->file_data_type == IM_USHORT)
((imushort*)this->line_buffer)[col] = (imushort)value;
else
((imbyte*)this->line_buffer)[col] = (unsigned char)value;
}
}
}
else
{
imBinFileRead(this->handle, (imbyte*)this->line_buffer, line_count, type_size);
if (imBinFileError(this->handle))
return IM_ERR_ACCESS;
}
imFileLineBufferRead(this, data, row, plane);
if (!imCounterInc(this->counter))
return IM_ERR_COUNTER;
imFileLineBufferInc(this, &row, &plane);
if (this->padding)
imBinFileSeekOffset(this->handle, this->padding);
}
return IM_ERR_NONE;
}
int imFileFormatRAW::WriteImageData(void* data)
{
int count = imFileLineBufferCount(this);
int line_count = imImageLineCount(this->width, this->file_color_mode);
int type_size = iFileDataTypeSize(this->file_data_type, this->switch_type);
// treat complex as 2 real
if (this->file_data_type == IM_CFLOAT)
{
type_size /= 2;
line_count *= 2;
}
int ascii;
if (imStrEqual(this->compression, "ASCII"))
ascii = 1;
else
ascii = 0;
imCounterTotal(this->counter, count, "Writing RAW...");
int row = 0, plane = 0;
for (int i = 0; i < count; i++)
{
imFileLineBufferWrite(this, data, row, plane);
if (ascii)
{
for (int col = 0; col < line_count; col++)
{
if (this->file_data_type == IM_FLOAT)
{
float value = ((float*)this->line_buffer)[col];
if (!imBinFilePrintf(handle, "%f ", (double)value))
return IM_ERR_ACCESS;
}
else
{
int value;
if (this->file_data_type == IM_INT)
value = ((int*)this->line_buffer)[col];
else if (this->file_data_type == IM_USHORT)
value = ((imushort*)this->line_buffer)[col];
else
value = ((imbyte*)this->line_buffer)[col];
if (!imBinFilePrintf(handle, "%d ", value))
return IM_ERR_ACCESS;
}
}
imBinFileWrite(handle, (void*)"\n", 1, 1);
}
else
{
imBinFileWrite(this->handle, (imbyte*)this->line_buffer, line_count, type_size);
}
if (imBinFileError(this->handle))
return IM_ERR_ACCESS;
if (!imCounterInc(this->counter))
return IM_ERR_COUNTER;
imFileLineBufferInc(this, &row, &plane);
if (this->padding)
imBinFileSeekOffset(this->handle, this->padding);
}
this->image_count++;
return IM_ERR_NONE;
}
int imFormatRAW::CanWrite(const char* compression, int color_mode, int data_type) const
{
(void)data_type;
if (imColorSpace(color_mode) == IM_MAP)
return IM_ERR_DATA;
if (!compression || compression[0] == 0)
return IM_ERR_NONE;
if (!imStrEqual(compression, "NONE") && !imStrEqual(compression, "ASCII"))
return IM_ERR_COMPRESS;
return IM_ERR_NONE;
}
|