Coin Logo Coin3D is Free Software,
published under the BSD 3-clause license.
https://coin3d.github.io
https://www.kongsberg.com/en/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 #include <stdlib.h>
30 
31 /* needed since Japser includes its own config file */
32 #undef PACKAGE
33 #undef PACKAGE_BUGREPORT
34 #undef PACKAGE_NAME
35 #undef PACKAGE_STRING
36 #undef PACKAGE_TARNAME
37 #undef PACKAGE_VERSION
38 #undef VERSION
39 
40 #include <jasper/jasper.h>
41 
42 #define ERR_NO_ERROR 0
43 #define ERR_OPEN 1
44 #define ERR_READ 2
45 #define ERR_MEM 3
46 #define ERR_OPEN_WRITE 4
47 #define ERR_WRITE 5
48 #define ERR_NOT_IMPLEMENTED 6
49 #define ERR_INIT 7
50 
51 static int jaspererror = ERR_NO_ERROR;
52 
53 static int jasper_init(void)
54 {
55  static int did_init = 0;
56  if (!did_init) {
57  if (jas_init() == 0) {
58  did_init = 1;
59  }
60  }
61  return did_init;
62 }
63 static void jasper_copy_matrix(unsigned char * buffer,
64  jas_matrix_t * data,
65  int w,
66  int h,
67  int bits,
68  int component,
69  int numcomp)
70 {
71  int x, y;
72  unsigned char * dst = buffer + component;
73 
74  for (y = 0; y < h; y++) {
75  for (x = 0; x < w; x++) {
76  int tmp = jas_matrix_get(data, (h-y)-1, x);
77  tmp <<= 8;
78  tmp >>= bits;
79 
80  *dst = (unsigned char) tmp;
81  dst += numcomp;
82  }
83  }
84 }
85 
86 int
87 simage_jasper_error(char * buffer, int buflen)
88 {
89  switch (jaspererror) {
90  case ERR_INIT:
91  strncpy(buffer, "JASPER loader: Error initializing Jasper", buflen);
92  break;
93  case ERR_OPEN:
94  strncpy(buffer, "JASPER loader: Error opening file", buflen);
95  break;
96  case ERR_READ:
97  strncpy(buffer, "JASPER loader: Error reading file", buflen);
98  break;
99  case ERR_MEM:
100  strncpy(buffer, "JASPER loader: Out of memory error", buflen);
101  break;
102  case ERR_OPEN_WRITE:
103  strncpy(buffer, "JASPER saver: Error opening file", buflen);
104  break;
105  case ERR_WRITE:
106  strncpy(buffer, "JASPER loader: Error writing file", buflen);
107  break;
108  case ERR_NOT_IMPLEMENTED:
109  strncpy(buffer, "JASPER loader: Feature not implemented", buflen);
110  break;
111  }
112  return jaspererror;
113 }
114 
115 int
116 simage_jasper_identify(const char *ptr,
117  const unsigned char * header,
118  int headerlen)
119 {
120  static unsigned char jaspercmp[] = {0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50};
121 
122 
123  if (headerlen < 6) return 0;
124  if (memcmp((const void*)header, (const void*)jaspercmp, 6) == 0) return 1;
125  return 0;
126 }
127 
128 unsigned char *
129 simage_jasper_load(const char * filename,
130  int * width_ret,
131  int * height_ret,
132  int * numComponents_ret)
133 {
134  int width;
135  int height;
136  int numcomps;
137  int realnumcomp;
138  int compno;
139  int compfound[4] = {0,0,0,0};
140 
141  jas_image_t * image = NULL;
142  jas_stream_t * stream = NULL;
143  jas_matrix_t * data = NULL;
144  unsigned char * buffer = NULL;
145 
146  jaspererror = ERR_NO_ERROR;
147  if (!jasper_init()) {
148  jaspererror = ERR_INIT;
149  return NULL;
150  }
151 
152  do {
153  stream = jas_stream_fopen(filename, "rb");
154  if (!stream) {
155  jaspererror = ERR_OPEN;
156  break; /* break out of do/while loop */
157  }
158  image = jas_image_decode(stream, -1, 0);
159  jas_stream_close(stream);
160  stream = NULL;
161 
162  if (!image) {
163  jaspererror = ERR_READ;
164  break; /* break out of do/while loop */
165  }
166 
167  width = jas_image_width(image);
168  height = jas_image_height(image);
169  numcomps = jas_image_numcmpts(image);
170 
171  /*
172  * verify components
173  */
174  for (compno = 0; compno < numcomps; compno++) {
175  int w, h, d;
176  int type;
177  w = jas_image_cmptwidth(image, compno);
178  h = jas_image_cmptheight(image, compno);
179  type = jas_image_cmpttype(image, compno);
180  d = jas_image_cmptprec(image, compno);
181 
182  if (w != width || h != height) {
183  jaspererror = ERR_READ;
184  break; /* break out of for loop */
185  }
186  switch (type) {
187  case JAS_IMAGE_CT_RGB_R:
188  compfound[0] = 1;
189  break;
190  case JAS_IMAGE_CT_RGB_G:
191  compfound[1] = 1;
192  break;
193  case JAS_IMAGE_CT_RGB_B:
194  compfound[2] = 1;
195  break;
196  case JAS_IMAGE_CT_OPACITY:
197  compfound[3] = 1;
198  break;
199  default:
200  /* just ignore */
201  break;
202  }
203  }
204  if (jaspererror != ERR_NO_ERROR) {
205  break; /* break out of do/while loop */
206  }
207 
208  /*
209  * try to figure out the actual number of components
210  */
211 
212  realnumcomp = 0;
213  if (numcomps >= 3) { /* assume RGB image */
214  if (compfound[0] && compfound[1] && compfound[2]) {
215  realnumcomp = 3;
216  if (compfound[3]) realnumcomp = 4;
217  }
218  }
219  else { /* assume grayscale */
220  if (compfound[0]) {
221  realnumcomp = 1;
222  if (compfound[3]) {
223  realnumcomp = 2;
224  }
225  }
226  }
227  if (realnumcomp == 0) {
228  jaspererror = ERR_READ;
229  break; /* break out of do/while loop */
230  }
231 
232  buffer = malloc(width * height * realnumcomp);
233  if (buffer == NULL) {
234  jaspererror = ERR_MEM;
235  break; /* break out of do/while loop */
236  }
237  data = jas_matrix_create(height, width);
238  if (!data) {
239  jaspererror = ERR_MEM;
240  break; /* break out of do/while loop */
241  }
242 
243  for (compno = 0; compno < numcomps; compno++) {
244  int d, type;
245  int realcomp;
246  type = jas_image_cmpttype(image, compno);
247  d = jas_image_cmptprec(image, compno);
248 
249  if (jas_image_readcmpt(image, compno, 0, 0, width, height, data)) {
250  jaspererror = ERR_READ;
251  break; /* break out of for loop */
252  }
253  switch (type) {
254  case JAS_IMAGE_CT_RGB_R:
255  realcomp = 0;
256  break;
257  case JAS_IMAGE_CT_RGB_G:
258  realcomp = 1;
259  break;
260  case JAS_IMAGE_CT_RGB_B:
261  realcomp = 2;
262  break;
263  case JAS_IMAGE_CT_OPACITY:
264  realcomp = realnumcomp - 1;
265  break;
266  default:
267  /* just ignore */
268  break;
269  }
270  jasper_copy_matrix(buffer, data, width, height, d, realcomp, realnumcomp);
271  }
272  } while (0);
273 
274  if (stream) jas_stream_close(stream);
275  if (image) jas_image_destroy(image);
276  if (data) jas_matrix_destroy(data);
277 
278  if (jaspererror != ERR_NO_ERROR) {
279  if (buffer) free(buffer);
280  return NULL;
281  }
282  *width_ret = width;
283  *height_ret = height;
284  *numComponents_ret = realnumcomp;
285  return buffer;
286 }
287 
288 int
289 simage_jasper_save(const char *filename,
290  const unsigned char * bytes,
291  int width,
292  int height,
293  int numcomponents)
294 {
295  jaspererror = ERR_NOT_IMPLEMENTED;
296  return 0;
297 }
298 
299 typedef struct {
300  int width;
301  int height;
302  int numcomp;
303  int depth;
304 } simage_jasper_opendata;
305 
306 void *
307 simage_jasper_open(const char * filename,
308  int * width,
309  int * height,
310  int * numcomponents)
311 {
312  jaspererror = ERR_NOT_IMPLEMENTED;
313  return NULL;
314 }
315 
316 
317 void
318 simage_jasper_close(void * opendata)
319 {
320  simage_jasper_opendata * od = (simage_jasper_opendata*) opendata;
321  jaspererror = ERR_NOT_IMPLEMENTED;
322 }
323 
324 int
325 simage_jasper_read_line(void * opendata, int y, unsigned char * buf)
326 {
327  simage_jasper_opendata * od;
328  jaspererror = ERR_NOT_IMPLEMENTED;
329 
330  od = (simage_jasper_opendata*) opendata;
331  return 0;
332 }
333 
334 #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)