33 #undef PACKAGE_BUGREPORT
36 #undef PACKAGE_TARNAME
37 #undef PACKAGE_VERSION
40 #include <jasper/jasper.h>
42 #define ERR_NO_ERROR 0
46 #define ERR_OPEN_WRITE 4
48 #define ERR_NOT_IMPLEMENTED 6
53 static int jasper_init(
void)
55 static int did_init = 0;
57 if (jas_init() == 0) {
63 static void jasper_copy_matrix(
unsigned char * buffer,
72 unsigned char * dst = buffer + component;
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);
80 *dst = (
unsigned char) tmp;
89 switch (jaspererror) {
91 strncpy(buffer,
"JASPER loader: Error initializing Jasper", buflen);
94 strncpy(buffer,
"JASPER loader: Error opening file", buflen);
97 strncpy(buffer,
"JASPER loader: Error reading file", buflen);
100 strncpy(buffer,
"JASPER loader: Out of memory error", buflen);
103 strncpy(buffer,
"JASPER saver: Error opening file", buflen);
106 strncpy(buffer,
"JASPER loader: Error writing file", buflen);
109 strncpy(buffer,
"JASPER loader: Feature not implemented", buflen);
117 const unsigned char * header,
120 static unsigned char jaspercmp[] = {0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50};
123 if (headerlen < 6)
return 0;
124 if (memcmp((
const void*)header, (
const void*)jaspercmp, 6) == 0)
return 1;
132 int * numComponents_ret)
139 int compfound[4] = {0,0,0,0};
141 jas_image_t * image = NULL;
142 jas_stream_t * stream = NULL;
143 jas_matrix_t * data = NULL;
144 unsigned char * buffer = NULL;
147 if (!jasper_init()) {
153 stream = jas_stream_fopen(filename,
"rb");
158 image = jas_image_decode(stream, -1, 0);
159 jas_stream_close(stream);
167 width = jas_image_width(image);
168 height = jas_image_height(image);
169 numcomps = jas_image_numcmpts(image);
174 for (compno = 0; compno < numcomps; compno++) {
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);
182 if (w != width || h != height) {
187 case JAS_IMAGE_CT_RGB_R:
190 case JAS_IMAGE_CT_RGB_G:
193 case JAS_IMAGE_CT_RGB_B:
196 case JAS_IMAGE_CT_OPACITY:
214 if (compfound[0] && compfound[1] && compfound[2]) {
216 if (compfound[3]) realnumcomp = 4;
227 if (realnumcomp == 0) {
232 buffer = malloc(width * height * realnumcomp);
233 if (buffer == NULL) {
237 data = jas_matrix_create(height, width);
243 for (compno = 0; compno < numcomps; compno++) {
246 type = jas_image_cmpttype(image, compno);
247 d = jas_image_cmptprec(image, compno);
249 if (jas_image_readcmpt(image, compno, 0, 0, width, height, data)) {
254 case JAS_IMAGE_CT_RGB_R:
257 case JAS_IMAGE_CT_RGB_G:
260 case JAS_IMAGE_CT_RGB_B:
263 case JAS_IMAGE_CT_OPACITY:
264 realcomp = realnumcomp - 1;
270 jasper_copy_matrix(buffer, data, width, height, d, realcomp, realnumcomp);
274 if (stream) jas_stream_close(stream);
275 if (image) jas_image_destroy(image);
276 if (data) jas_matrix_destroy(data);
279 if (buffer) free(buffer);
283 *height_ret = height;
284 *numComponents_ret = realnumcomp;
290 const unsigned char * bytes,
304 } simage_jasper_opendata;
320 simage_jasper_opendata * od = (simage_jasper_opendata*) opendata;
327 simage_jasper_opendata * od;
330 od = (simage_jasper_opendata*) opendata;
int simage_jasper_read_line(void *opendata, int y, unsigned char *buf)
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)
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)