summaryrefslogtreecommitdiff
path: root/im/src/im_format.cpp
blob: ad04a3b0743a6eb7d830bc22ed1baea1c03a9c39 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/** \file
 * \brief File Format Access
 *
 * See Copyright Notice in im_lib.h
 * $Id: im_format.cpp,v 1.3 2009/11/23 17:13:05 scuri Exp $
 */


#include <stdlib.h>
#include <ctype.h>
#include <memory.h>
#include <string.h>
#include <assert.h>

#include "im.h"
#include "im_format.h"
#include "im_util.h"


static imFormat* iFormatList[50];
static int iFormatCount = 0;
static int iFormatRegistredAll = 0;

void imFormatRemoveAll(void)
{
  for (int i = 0; i < iFormatCount; i++)
  {
    imFormat* iformat = iFormatList[i];
    delete iformat;
    iFormatList[i] = NULL;
  }
  iFormatCount = 0;
  iFormatRegistredAll = 0;
}

void imFormatRegister(imFormat* iformat)
{
  iFormatList[iFormatCount] = iformat;
  iFormatCount++;
}

static imFormat* iFormatFind(const char* format)
{
  assert(format);

  if (!iFormatRegistredAll) 
  {
    imFormatRegisterInternal();
    iFormatRegistredAll = 1;
  }

  for (int i = 0; i < iFormatCount; i++)
  {
    imFormat* iformat = iFormatList[i];
    if (imStrEqual(format, iformat->format))
      return iformat;
  }
  return NULL;
}

void imFormatList(char** format_list, int *format_count)
{
  assert(format_list);
  assert(format_count);

  if (!iFormatRegistredAll) 
  {
    imFormatRegisterInternal();
    iFormatRegistredAll = 1;
  }

  static char format_list_buffer[50][50];

  *format_count = iFormatCount;
  for (int i = 0; i < iFormatCount; i++)
  {
    imFormat* iformat = iFormatList[i];
    strcpy(format_list_buffer[i], iformat->format);
    format_list[i] = format_list_buffer[i];
  }
}

int imFormatInfo(const char* format, char* desc, char* ext, int *can_sequence)
{
  imFormat* iformat = iFormatFind(format);
  if (!iformat) return IM_ERR_FORMAT;

  if (desc) strcpy(desc, iformat->desc);
  if (ext) strcpy(ext, iformat->ext);
  if (can_sequence) *can_sequence = iformat->can_sequence;

  return IM_ERR_NONE;
}

int imFormatCompressions(const char* format, char** comp, int *comp_count, int color_mode, int data_type)
{
  imFormat* iformat = iFormatFind(format);
  if (!iformat) return IM_ERR_FORMAT;

  int count = 0;

  static char comp_buffer[50][50];

  for (int i = 0; i < iformat->comp_count; i++)
  {
    if (color_mode == -1 || data_type == -1 || 
        iformat->CanWrite(iformat->comp[i], color_mode, data_type) == IM_ERR_NONE)
    {
      strcpy(comp_buffer[count], iformat->comp[i]);
      comp[count] = comp_buffer[count];
      count++;
    }
  }

  *comp_count = count;

  return IM_ERR_NONE;
}

int imFormatCanWriteImage(const char* format, const char* compression, int color_mode, int data_type)
{
  assert(format);

  imFormat* iformat = iFormatFind(format);
  if (!iformat) return IM_ERR_FORMAT;

  int error = iformat->CanWrite(compression, color_mode, data_type);
  return error;
}

static char* utlFileGetExt(const char *file_name)
{
  int len = strlen(file_name);

  // Starts at the last character
  int offset = len - 1;
  while (offset != 0)
  {
    // if found a path separator, no extension found
    if (file_name[offset] == '\\' || file_name[offset] == '/')
      return NULL;

    if (file_name[offset] == '.')
    {
      offset++;
      break;
    }

    offset--;
  }

  // if at the first character, no extension found
  if (offset == 0) 
    return NULL;

  int ext_size = len - offset + 1;
  char* file_ext = (char*)malloc(ext_size);

  for (int i = 0; i < ext_size-1; i++)
    file_ext[i] = (char)tolower(file_name[i+offset]);
  file_ext[ext_size-1] = 0;

  return file_ext;
}

imFileFormatBase* imFileFormatBaseOpen(const char* file_name, int *error)
{
  int i;

  assert(file_name);
  assert(error);

  if (!iFormatRegistredAll) 
  {
    imFormatRegisterInternal();
    iFormatRegistredAll = 1;
  }

  int* ext_mark = new int [iFormatCount];
  memset(ext_mark, 0, sizeof(int)*iFormatCount);

  // Search for the extension first, this usually is going to speed the search
  char* extension = utlFileGetExt(file_name);
  if (extension)
  {
    for(i = 0; i < iFormatCount; i++)
    {
      imFormat* iformat = iFormatList[i];

      if (strstr(iformat->ext, extension) != NULL)
      {
        ext_mark[i] = 1; // Mark this format to avoid testing it again in the next phase

        imFileFormatBase* ifileformat = iformat->Create();
        *error = ifileformat->Open(file_name);                                               
        if (*error != IM_ERR_NONE && *error != IM_ERR_FORMAT)  // Error situation that must abort
        {                                                      // Only IM_ERR_FORMAT is considered here
          free(extension);
          delete [] ext_mark;
          delete ifileformat;
          return NULL;
        }
        else if (*error == IM_ERR_NONE) // Sucessfully oppened the file
        {
          free(extension);
          delete [] ext_mark;
          return ifileformat;
        }
        else
        {
          /* Other errors, release the format and test another one */
          delete ifileformat;
        }
      }
    }

    free(extension);
  }

  // If the search did not work, try all the formats
  // except those already tested.

  for(i = 0; i < iFormatCount; i++)
  {
    if (!ext_mark[i])
    {
      imFormat* iformat = iFormatList[i];
      imFileFormatBase* ifileformat = iformat->Create();
      *error = ifileformat->Open(file_name);
      if (*error != IM_ERR_NONE && *error != IM_ERR_FORMAT)  // Error situation that must abort
      {                                                      // Only IM_ERR_FORMAT is a valid error here
        delete [] ext_mark;
        delete ifileformat;
        return NULL;
      }
      else if (*error == IM_ERR_NONE) // Sucessfully oppened the file
      {
        delete [] ext_mark;
        return ifileformat;
      }
      else
      {
        /* Other errors, release the format and test another one */
        delete ifileformat;
      }
    }
  }

  *error = IM_ERR_FORMAT;
  delete [] ext_mark;
  return NULL;
}

imFileFormatBase* imFileFormatBaseOpenAs(const char* file_name, const char* format, int *error)
{
  assert(file_name);
  assert(format);
  assert(error);

  if (!iFormatRegistredAll) 
  {
    imFormatRegisterInternal();
    iFormatRegistredAll = 1;
  }

  imFormat* iformat = iFormatFind(format);
  if (!format)
  {
    *error = IM_ERR_FORMAT;
    return NULL;
  }

  imFileFormatBase* ifileformat = iformat->Create();
  *error = ifileformat->Open(file_name);
  if (*error != IM_ERR_NONE && *error != IM_ERR_FORMAT)  // Error situation that must abort
  {
    delete ifileformat;
    return NULL;
  }
  else if (*error == IM_ERR_NONE) // Sucessfully oppened the file
    return ifileformat;
  else
  {
    *error = IM_ERR_FORMAT;
    delete ifileformat;
    return NULL;
  }
}

imFileFormatBase* imFileFormatBaseNew(const char* file_name, const char* format, int *error)
{
  assert(file_name);
  assert(format);
  assert(error);

  imFormat* iformat = iFormatFind(format);
  if (!iformat)
  {
    *error = IM_ERR_FORMAT;
    return NULL;
  }

  imFileFormatBase* ifileformat = iformat->Create();
  *error = ifileformat->New(file_name);
  if (*error)
  {
    delete ifileformat;
    return NULL;
  }

  return ifileformat;
}