summaryrefslogtreecommitdiff
path: root/src/im_format_raw.cpp
blob: 6a6a7fd921557b52d3f2baf79501b3e018e0705d (plain)
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
/** \file
 * \brief RAW File Format
 *
 * See Copyright Notice in im_lib.h
 * $Id: im_format_raw.cpp,v 1.4 2009/09/10 17:33:35 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[1] = 
{
  "NONE"
};

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, 
              1, 
              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;
  }

  imCounterTotal(this->counter, count, "Reading RAW...");

  int row = 0, plane = 0;
  for (int i = 0; i < count; i++)
  {
    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;
  }

  imCounterTotal(this->counter, count, "Writing RAW...");

  int row = 0, plane = 0;
  for (int i = 0; i < count; i++)
  {
    imFileLineBufferWrite(this, data, row, plane);

    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"))
    return IM_ERR_COMPRESS;

  return IM_ERR_NONE;
}