File: simage_jasper.c

package info (click to toggle)
simage 1.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,908 kB
  • sloc: ansic: 14,231; sh: 12,587; cpp: 930; perl: 854; makefile: 617; lisp: 25
file content (316 lines) | stat: -rw-r--r-- 7,391 bytes parent folder | download | duplicates (2)
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
/*
 * a jasper (Jpeg 2000) importer
 * 
 */

#include <config.h>
#ifdef HAVE_JASPER

#include <simage_jasper.h>

/* needed since Japser includes its own config file */
#undef PACKAGE
#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#undef VERSION

#include <jasper/jasper.h>

#define ERR_NO_ERROR        0
#define ERR_OPEN            1
#define ERR_READ            2
#define ERR_MEM             3
#define ERR_OPEN_WRITE      4
#define ERR_WRITE           5
#define ERR_NOT_IMPLEMENTED 6
#define ERR_INIT            7

static int jaspererror = ERR_NO_ERROR;

static int jasper_init(void) 
{
  static int did_init = 0;
  if (!did_init) {
    if (jas_init() == 0) {
      did_init = 1;
    }
  }
  return did_init;
}
static void jasper_copy_matrix(unsigned char * buffer, 
                               jas_matrix_t * data,
                               int w,
                               int h,
                               int bits,
                               int component,
                               int numcomp) 
{
  int x, y;
  unsigned char * dst = buffer + component;

  for (y = 0; y < h; y++) {
    for (x = 0; x < w; x++) {
      int tmp = jas_matrix_get(data, (h-y)-1, x);
      tmp <<= 8;
      tmp >>= bits;

      *dst = (unsigned char) tmp;
      dst += numcomp;
    }
  }
}

int
simage_jasper_error(char * buffer, int buflen)
{
  switch (jaspererror) {
  case ERR_INIT:
    strncpy(buffer, "JASPER loader: Error initializing Jasper", buflen);    
    break;
  case ERR_OPEN:
    strncpy(buffer, "JASPER loader: Error opening file", buflen);
    break;
  case ERR_READ:
    strncpy(buffer, "JASPER loader: Error reading file", buflen);
    break;
  case ERR_MEM:
    strncpy(buffer, "JASPER loader: Out of memory error", buflen);
    break;
  case ERR_OPEN_WRITE:
    strncpy(buffer, "JASPER saver: Error opening file", buflen);
    break;
  case ERR_WRITE:
    strncpy(buffer, "JASPER loader: Error writing file", buflen);
    break;
  case ERR_NOT_IMPLEMENTED:
    strncpy(buffer, "JASPER loader: Feature not implemented", buflen);
    break;
  }
  return jaspererror;
}

int 
simage_jasper_identify(const char *ptr,
                       const unsigned char * header,
                       int headerlen)
{
  static unsigned char jaspercmp[] = {0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50};

  
  if (headerlen < 6) return 0;
  if (memcmp((const void*)header, (const void*)jaspercmp, 6) == 0) return 1;
  return 0;
}

unsigned char *
simage_jasper_load(const char * filename,
                   int * width_ret,
                   int * height_ret,
                   int * numComponents_ret)
{
  int width;
  int height;
  int depth;
  int numcomps;
  int realnumcomp;
  int compno;
  int compfound[4] = {0,0,0,0};

        jas_image_t * image = NULL;
  jas_stream_t * stream = NULL;
        jas_matrix_t * data = NULL;
  unsigned char * buffer = NULL;

  jaspererror = ERR_NO_ERROR;
  if (!jasper_init()) {
    jaspererror = ERR_INIT;
    return NULL;
  }

  do {
    stream = jas_stream_fopen(filename, "rb");
    if (!stream) {
      jaspererror = ERR_OPEN;
      break; /* break out of do/while loop */
    }
    image = jas_image_decode(stream, -1, 0);
    jas_stream_close(stream);
    stream = NULL;

    if (!image) {
      jaspererror = ERR_READ;
      break; /* break out of do/while loop */
    }

    width = jas_image_width(image);
    height = jas_image_height(image);
    numcomps = jas_image_numcmpts(image);
    
    /*  
     * verify components 
     */
    for (compno = 0; compno < numcomps; compno++) {
      int w, h, d;
      int type;
      w = jas_image_cmptwidth(image, compno);
      h = jas_image_cmptheight(image, compno);
      type = jas_image_cmpttype(image, compno);
      d = jas_image_cmptprec(image, compno);
    
      if (w != width || h != height) {
        jaspererror = ERR_READ;
        break; /* break out of for loop  */
      }
      switch (type) {
      case JAS_IMAGE_CT_RGB_R:
        compfound[0] = 1;
        break;
      case JAS_IMAGE_CT_RGB_G:
        compfound[1] = 1;
        break;
      case JAS_IMAGE_CT_RGB_B:
        compfound[2] = 1;
        break;
      case JAS_IMAGE_CT_OPACITY:
        compfound[3] = 1;
        break;
      default:
        /* just ignore */
        break;
      }
    }
    if (jaspererror != ERR_NO_ERROR) {
      break; /* break out of do/while loop */
    }
    
    /*
     * try to figure out the actual number of components 
     */

    realnumcomp = 0;
    if (numcomps >= 3) { /* assume RGB image */
      if (compfound[0] && compfound[1] && compfound[2]) {
        realnumcomp = 3;
        if (compfound[3]) realnumcomp = 4;
      }
    }
    else { /* assume grayscale */
      if (compfound[0]) {
        realnumcomp = 1;
        if (compfound[3]) {
          realnumcomp = 2;
        }
      }
    }
    if (realnumcomp == 0) {
      jaspererror = ERR_READ;
      break; /* break out of do/while loop */
    }
    
    buffer = malloc(width * height * realnumcomp);
    if (buffer == NULL) {
      jaspererror = ERR_MEM;
      break; /* break out of do/while loop */
    }
    data = jas_matrix_create(height, width);
    if (!data) {
      jaspererror = ERR_MEM;
      break; /* break out of do/while loop */
    }
    
    for (compno = 0; compno < numcomps; compno++) {
      int d, type;
      int realcomp;
      type = jas_image_cmpttype(image, compno);
      d = jas_image_cmptprec(image, compno);
      
      if (jas_image_readcmpt(image, compno, 0, 0, width, height, data)) {
        jaspererror = ERR_READ;
        break; /* break out of for loop */
      }
      switch (type) {
      case JAS_IMAGE_CT_RGB_R:
        realcomp = 0;
        break;
      case JAS_IMAGE_CT_RGB_G:
        realcomp = 1;
        break;
      case JAS_IMAGE_CT_RGB_B:
        realcomp = 2;
        break;
      case JAS_IMAGE_CT_OPACITY:
        realcomp = realnumcomp - 1;
        break;
      default:
        /* just ignore */
        break;
      }  
      jasper_copy_matrix(buffer, data, width, height, d, realcomp, realnumcomp);
    }
  } while (0);

  if (stream) jas_stream_close(stream);
  if (image) jas_image_destroy(image);
  if (data) jas_matrix_destroy(data);
  
  if (jaspererror != ERR_NO_ERROR) {
    if (buffer) free(buffer);
    return NULL;
  }
  *width_ret = width;
  *height_ret = height;
  *numComponents_ret = realnumcomp;
  return buffer;
}

int 
simage_jasper_save(const char *filename,
                   const unsigned char * bytes,
                   int width,
                   int height,
                   int numcomponents)
{
  jaspererror = ERR_NOT_IMPLEMENTED;
  return 0;
}

typedef struct {
  int width;
  int height;
  int numcomp;
  int depth;  
} simage_jasper_opendata;

void * 
simage_jasper_open(const char * filename,
                   int * width,
                   int * height,
                   int * numcomponents)
{
  jaspererror = ERR_NOT_IMPLEMENTED;
  return NULL;
}


void 
simage_jasper_close(void * opendata)
{
  simage_jasper_opendata * od = (simage_jasper_opendata*) opendata;
  jaspererror = ERR_NOT_IMPLEMENTED;
}

int 
simage_jasper_read_line(void * opendata, int y, unsigned char * buf)
{
  simage_jasper_opendata * od;
  jaspererror = ERR_NOT_IMPLEMENTED;
   
  od = (simage_jasper_opendata*) opendata;
  return 0;
}

#endif /* HAVE_JASPER */