32 #undef PACKAGE_BUGREPORT
35 #undef PACKAGE_TARNAME
36 #undef PACKAGE_VERSION
39 #include <jasper/jasper.h>
41 #define ERR_NO_ERROR 0
45 #define ERR_OPEN_WRITE 4
47 #define ERR_NOT_IMPLEMENTED 6
52 static int jasper_init(
void)
54 static int did_init = 0;
56 if (jas_init() == 0) {
62 static void jasper_copy_matrix(
unsigned char * buffer,
71 unsigned char * dst = buffer + component;
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);
79 *dst = (
unsigned char) tmp;
88 switch (jaspererror) {
90 strncpy(buffer,
"JASPER loader: Error initializing Jasper", buflen);
93 strncpy(buffer,
"JASPER loader: Error opening file", buflen);
96 strncpy(buffer,
"JASPER loader: Error reading file", buflen);
99 strncpy(buffer,
"JASPER loader: Out of memory error", buflen);
102 strncpy(buffer,
"JASPER saver: Error opening file", buflen);
105 strncpy(buffer,
"JASPER loader: Error writing file", buflen);
108 strncpy(buffer,
"JASPER loader: Feature not implemented", buflen);
116 const unsigned char * header,
119 static unsigned char jaspercmp[] = {0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50};
122 if (headerlen < 6)
return 0;
123 if (memcmp((
const void*)header, (
const void*)jaspercmp, 6) == 0)
return 1;
131 int * numComponents_ret)
138 int compfound[4] = {0,0,0,0};
140 jas_image_t * image = NULL;
141 jas_stream_t * stream = NULL;
142 jas_matrix_t * data = NULL;
143 unsigned char * buffer = NULL;
146 if (!jasper_init()) {
152 stream = jas_stream_fopen(filename,
"rb");
157 image = jas_image_decode(stream, -1, 0);
158 jas_stream_close(stream);
166 width = jas_image_width(image);
167 height = jas_image_height(image);
168 numcomps = jas_image_numcmpts(image);
173 for (compno = 0; compno < numcomps; compno++) {
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);
181 if (w != width || h != height) {
186 case JAS_IMAGE_CT_RGB_R:
189 case JAS_IMAGE_CT_RGB_G:
192 case JAS_IMAGE_CT_RGB_B:
195 case JAS_IMAGE_CT_OPACITY:
213 if (compfound[0] && compfound[1] && compfound[2]) {
215 if (compfound[3]) realnumcomp = 4;
226 if (realnumcomp == 0) {
231 buffer = malloc(width * height * realnumcomp);
232 if (buffer == NULL) {
236 data = jas_matrix_create(height, width);
242 for (compno = 0; compno < numcomps; compno++) {
245 type = jas_image_cmpttype(image, compno);
246 d = jas_image_cmptprec(image, compno);
248 if (jas_image_readcmpt(image, compno, 0, 0, width, height, data)) {
253 case JAS_IMAGE_CT_RGB_R:
256 case JAS_IMAGE_CT_RGB_G:
259 case JAS_IMAGE_CT_RGB_B:
262 case JAS_IMAGE_CT_OPACITY:
263 realcomp = realnumcomp - 1;
269 jasper_copy_matrix(buffer, data, width, height, d, realcomp, realnumcomp);
273 if (stream) jas_stream_close(stream);
274 if (image) jas_image_destroy(image);
275 if (data) jas_matrix_destroy(data);
278 if (buffer) free(buffer);
282 *height_ret = height;
283 *numComponents_ret = realnumcomp;
289 const unsigned char * bytes,
303 } simage_jasper_opendata;
319 simage_jasper_opendata * od = (simage_jasper_opendata*) opendata;
326 simage_jasper_opendata * od;
329 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)