-//-------------------------------------------------------------------
-
-struct r_jpeg_error_mgr
-{
- struct jpeg_error_mgr pub;
- jmp_buf envbuffer;
-};
-
-void jpeg_cb_error_exit(j_common_ptr cinfo)
-{
- struct r_jpeg_error_mgr *mptr;
- mptr = (struct r_jpeg_error_mgr *) cinfo->err;
- (*cinfo->err->output_message) (cinfo);
- longjmp(mptr->envbuffer, 1);
-}
-
-static int jpeg_save(unsigned char *image_buffer, const char * filename, int quality, int image_height, int image_width)
-{
- struct jpeg_compress_struct cinfo;
- struct jpeg_error_mgr jerr;
- FILE * outfile; /* target file */
- JSAMPROW row_pointer[1];/* pointer to JSAMPLE row[s] */
- int row_stride; /* physical row width in image buffer */
-
- cinfo.err = jpeg_std_error(&jerr);
- jpeg_create_compress(&cinfo);
-
- if ((outfile = fopen(filename, "wb")) == NULL)
- {
- eDebug("[JPEG] can't open %s", filename);
- return -1;
- }
- jpeg_stdio_dest(&cinfo, outfile);
-
- cinfo.image_width = image_width;
- cinfo.image_height = image_height;
- cinfo.input_components = 3;
- cinfo.in_color_space = JCS_RGB;
- jpeg_set_defaults(&cinfo);
- jpeg_set_quality(&cinfo, quality, TRUE );
- jpeg_start_compress(&cinfo, TRUE);
- row_stride = image_width * 3;
- while (cinfo.next_scanline < cinfo.image_height)
- {
- row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
- (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
- }
- jpeg_finish_compress(&cinfo);
- fclose(outfile);
- jpeg_destroy_compress(&cinfo);
- return 0;
-}
-
-
-static int jpeg_load(const char *filename, int *x, int *y)
-{
- struct jpeg_decompress_struct cinfo;
- struct jpeg_decompress_struct *ciptr = &cinfo;
- struct r_jpeg_error_mgr emgr;
- FILE *fh;
-
- if (!(fh = fopen(filename, "rb")))
- return 0;
-
- ciptr->err = jpeg_std_error(&emgr.pub);
- emgr.pub.error_exit = jpeg_cb_error_exit;
- if (setjmp(emgr.envbuffer) == 1)
- {
- jpeg_destroy_decompress(ciptr);
- fclose(fh);
- return 0;
- }
-
- jpeg_create_decompress(ciptr);
- jpeg_stdio_src(ciptr, fh);
- jpeg_read_header(ciptr, TRUE);
- ciptr->out_color_space = JCS_RGB;
- ciptr->scale_denom = 1;
-
- jpeg_start_decompress(ciptr);
-
- *x=ciptr->output_width;
- *y=ciptr->output_height;
-
- if(ciptr->output_components == 3)
- {
- JSAMPLE *lb = (JSAMPLE *)(*ciptr->mem->alloc_small)((j_common_ptr) ciptr, JPOOL_PERMANENT, ciptr->output_width * ciptr->output_components);
- pic_buffer = new unsigned char[ciptr->output_height * ciptr->output_width * ciptr->output_components];
- unsigned char *bp = pic_buffer;
-
- while (ciptr->output_scanline < ciptr->output_height)
- {
- jpeg_read_scanlines(ciptr, &lb, 1);
- memcpy(bp, lb, ciptr->output_width * ciptr->output_components);
- bp += ciptr->output_width * ciptr->output_components;
- }
- }
- jpeg_finish_decompress(ciptr);
- jpeg_destroy_decompress(ciptr);
- fclose(fh);
- return 1;
-}
-