Coin Logo Coin3D is Free Software,
published under the BSD 3-clause license.
https://bitbucket.org/Coin3D/
http://www.kongsberg.com/kogt/
simage_jasper.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) Kongsberg Oil & Gas Technologies
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /*
18  * a jasper (Jpeg 2000) importer
19  *
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif /* HAVE_CONFIG_H */
25 
26 #ifdef HAVE_JASPER
27 
28 #include <simage_jasper.h>
29 
30 /* needed since Japser includes its own config file */
31 #undef PACKAGE
32 #undef PACKAGE_BUGREPORT
33 #undef PACKAGE_NAME
34 #undef PACKAGE_STRING
35 #undef PACKAGE_TARNAME
36 #undef PACKAGE_VERSION
37 #undef VERSION
38 
39 #include <jasper/jasper.h>
40 
41 #define ERR_NO_ERROR 0
42 #define ERR_OPEN 1
43 #define ERR_READ 2
44 #define ERR_MEM 3
45 #define ERR_OPEN_WRITE 4
46 #define ERR_WRITE 5
47 #define ERR_NOT_IMPLEMENTED 6
48 #define ERR_INIT 7
49 
50 static int jaspererror = ERR_NO_ERROR;
51 
52 static int jasper_init(void)
53 {
54  static int did_init = 0;
55  if (!did_init) {
56  if (jas_init() == 0) {
57  did_init = 1;
58  }
59  }
60  return did_init;
61 }
62 static void jasper_copy_matrix(unsigned char * buffer,
63  jas_matrix_t * data,
64  int w,
65  int h,
66  int bits,
67  int component,
68  int numcomp)
69 {
70  int x, y;
71  unsigned char * dst = buffer + component;
72 
73  for (y = 0; y < h; y++) {
74  for (x = 0; x < w; x++) {
75  int tmp = jas_matrix_get(data, (h-y)-1, x);
76  tmp <<= 8;
77  tmp >>= bits;
78 
79  *dst = (unsigned char) tmp;
80  dst += numcomp;
81  }
82  }
83 }
84 
85 int
86 simage_jasper_error(char * buffer, int buflen)
87 {
88  switch (jaspererror) {
89  case ERR_INIT:
90  strncpy(buffer, "JASPER loader: Error initializing Jasper", buflen);
91  break;
92  case ERR_OPEN:
93  strncpy(buffer, "JASPER loader: Error opening file", buflen);
94  break;
95  case ERR_READ:
96  strncpy(buffer, "JASPER loader: Error reading file", buflen);
97  break;
98  case ERR_MEM:
99  strncpy(buffer, "JASPER loader: Out of memory error", buflen);
100  break;
101  case ERR_OPEN_WRITE:
102  strncpy(buffer, "JASPER saver: Error opening file", buflen);
103  break;
104  case ERR_WRITE:
105  strncpy(buffer, "JASPER loader: Error writing file", buflen);
106  break;
107  case ERR_NOT_IMPLEMENTED:
108  strncpy(buffer, "JASPER loader: Feature not implemented", buflen);
109  break;
110  }
111  return jaspererror;
112 }
113 
114 int
115 simage_jasper_identify(const char *ptr,
116  const unsigned char * header,
117  int headerlen)
118 {
119  static unsigned char jaspercmp[] = {0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50};
120 
121 
122  if (headerlen < 6) return 0;
123  if (memcmp((const void*)header, (const void*)jaspercmp, 6) == 0) return 1;
124  return 0;
125 }
126 
127 unsigned char *
128 simage_jasper_load(const char * filename,
129  int * width_ret,
130  int * height_ret,
131  int * numComponents_ret)
132 {
133  int width;
134  int height;
135  int numcomps;
136  int realnumcomp;
137  int compno;
138  int compfound[4] = {0,0,0,0};
139 
140  jas_image_t * image = NULL;
141  jas_stream_t * stream = NULL;
142  jas_matrix_t * data = NULL;
143  unsigned char * buffer = NULL;
144 
145  jaspererror = ERR_NO_ERROR;
146  if (!jasper_init()) {
147  jaspererror = ERR_INIT;
148  return NULL;
149  }
150 
151  do {
152  stream = jas_stream_fopen(filename, "rb");
153  if (!stream) {
154  jaspererror = ERR_OPEN;
155  break; /* break out of do/while loop */
156  }
157  image = jas_image_decode(stream, -1, 0);
158  jas_stream_close(stream);
159  stream = NULL;
160 
161  if (!image) {
162  jaspererror = ERR_READ;
163  break; /* break out of do/while loop */
164  }
165 
166  width = jas_image_width(image);
167  height = jas_image_height(image);
168  numcomps = jas_image_numcmpts(image);
169 
170  /*
171  * verify components
172  */
173  for (compno = 0; compno < numcomps; compno++) {
174  int w, h, d;
175  int type;
176  w = jas_image_cmptwidth(image, compno);
177  h = jas_image_cmptheight(image, compno);
178  type = jas_image_cmpttype(image, compno);
179  d = jas_image_cmptprec(image, compno);
180 
181  if (w != width || h != height) {
182  jaspererror = ERR_READ;
183  break; /* break out of for loop */
184  }
185  switch (type) {
186  case JAS_IMAGE_CT_RGB_R:
187  compfound[0] = 1;
188  break;
189  case JAS_IMAGE_CT_RGB_G:
190  compfound[1] = 1;
191  break;
192  case JAS_IMAGE_CT_RGB_B:
193  compfound[2] = 1;
194  break;
195  case JAS_IMAGE_CT_OPACITY:
196  compfound[3] = 1;
197  break;
198  default:
199  /* just ignore */
200  break;
201  }
202  }
203  if (jaspererror != ERR_NO_ERROR) {
204  break; /* break out of do/while loop */
205  }
206 
207  /*
208  * try to figure out the actual number of components
209  */
210 
211  realnumcomp = 0;
212  if (numcomps >= 3) { /* assume RGB image */
213  if (compfound[0] && compfound[1] && compfound[2]) {
214  realnumcomp = 3;
215  if (compfound[3]) realnumcomp = 4;
216  }
217  }
218  else { /* assume grayscale */
219  if (compfound[0]) {
220  realnumcomp = 1;
221  if (compfound[3]) {
222  realnumcomp = 2;
223  }
224  }
225  }
226  if (realnumcomp == 0) {
227  jaspererror = ERR_READ;
228  break; /* break out of do/while loop */
229  }
230 
231  buffer = malloc(width * height * realnumcomp);
232  if (buffer == NULL) {
233  jaspererror = ERR_MEM;
234  break; /* break out of do/while loop */
235  }
236  data = jas_matrix_create(height, width);
237  if (!data) {
238  jaspererror = ERR_MEM;
239  break; /* break out of do/while loop */
240  }
241 
242  for (compno = 0; compno < numcomps; compno++) {
243  int d, type;
244  int realcomp;
245  type = jas_image_cmpttype(image, compno);
246  d = jas_image_cmptprec(image, compno);
247 
248  if (jas_image_readcmpt(image, compno, 0, 0, width, height, data)) {
249  jaspererror = ERR_READ;
250  break; /* break out of for loop */
251  }
252  switch (type) {
253  case JAS_IMAGE_CT_RGB_R:
254  realcomp = 0;
255  break;
256  case JAS_IMAGE_CT_RGB_G:
257  realcomp = 1;
258  break;
259  case JAS_IMAGE_CT_RGB_B:
260  realcomp = 2;
261  break;
262  case JAS_IMAGE_CT_OPACITY:
263  realcomp = realnumcomp - 1;
264  break;
265  default:
266  /* just ignore */
267  break;
268  }
269  jasper_copy_matrix(buffer, data, width, height, d, realcomp, realnumcomp);
270  }
271  } while (0);
272 
273  if (stream) jas_stream_close(stream);
274  if (image) jas_image_destroy(image);
275  if (data) jas_matrix_destroy(data);
276 
277  if (jaspererror != ERR_NO_ERROR) {
278  if (buffer) free(buffer);
279  return NULL;
280  }
281  *width_ret = width;
282  *height_ret = height;
283  *numComponents_ret = realnumcomp;
284  return buffer;
285 }
286 
287 int
288 simage_jasper_save(const char *filename,
289  const unsigned char * bytes,
290  int width,
291  int height,
292  int numcomponents)
293 {
294  jaspererror = ERR_NOT_IMPLEMENTED;
295  return 0;
296 }
297 
298 typedef struct {
299  int width;
300  int height;
301  int numcomp;
302  int depth;
303 } simage_jasper_opendata;
304 
305 void *
306 simage_jasper_open(const char * filename,
307  int * width,
308  int * height,
309  int * numcomponents)
310 {
311  jaspererror = ERR_NOT_IMPLEMENTED;
312  return NULL;
313 }
314 
315 
316 void
317 simage_jasper_close(void * opendata)
318 {
319  simage_jasper_opendata * od = (simage_jasper_opendata*) opendata;
320  jaspererror = ERR_NOT_IMPLEMENTED;
321 }
322 
323 int
324 simage_jasper_read_line(void * opendata, int y, unsigned char * buf)
325 {
326  simage_jasper_opendata * od;
327  jaspererror = ERR_NOT_IMPLEMENTED;
328 
329  od = (simage_jasper_opendata*) opendata;
330  return 0;
331 }
332 
333 #endif /* HAVE_JASPER */
char * filename
Definition: stream.c:38
#define ERR_WRITE
int simage_jasper_read_line(void *opendata, int y, unsigned char *buf)
#define ERR_OPEN
#define ERR_MEM
int simage_jasper_save(const char *filename, const unsigned char *bytes, int width, int height, int numcomponents)
int simage_jasper_error(char *buffer, int bufferlen)
void * simage_jasper_open(const char *filename, int *width, int *height, int *numcomponents)
#define ERR_NO_ERROR
int simage_jasper_identify(const char *filename, const unsigned char *header, int headerlen)
unsigned char * simage_jasper_load(const char *filename, int *width, int *height, int *numComponents)
void simage_jasper_close(void *opendata)