Browse Source

* Added setjmp/longjmp support to jpeg loader.

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@3165 92316355-f0b4-4df1-b90c-862c8a59935f
master
jylam 16 years ago
parent
commit
4eb5808fa1
1 changed files with 33 additions and 13 deletions
  1. +33
    -13
      pipi/codec/modular/jpeg.c

+ 33
- 13
pipi/codec/modular/jpeg.c View File

@@ -18,10 +18,20 @@
*/

#include "../modular.h"
#include <setjmp.h>

static int pipi_free_jpeg(pipi_image_t *img);




struct my_error_mgr {
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};

typedef struct my_error_mgr * my_error_ptr;

static void format_msg(j_common_ptr cinfo, char *buf)
{
}
@@ -30,7 +40,9 @@ static void emit_msg(j_common_ptr cinfo, int level)
}
static void error_msg(j_common_ptr cinfo)
{
my_error_ptr myerr = (my_error_ptr) cinfo->err;
cinfo->client_data = (void*)0x1;
longjmp(myerr->setjmp_buffer, 1);
}
static void output_msg(j_common_ptr cinfo)
{
@@ -39,7 +51,7 @@ static void output_msg(j_common_ptr cinfo)
pipi_image_t *pipi_load_jpeg(const char *name)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
struct my_error_mgr jerr;
unsigned char *image = NULL, *scanline = NULL;
pipi_image_t *img = NULL;
unsigned int i, j, k = 0;
@@ -47,12 +59,15 @@ pipi_image_t *pipi_load_jpeg(const char *name)
FILE *fp = fopen(name, "rb");
if(!fp) goto end;

/* Set callbacks */
cinfo.err = jpeg_std_error(&jerr);
jerr.error_exit = error_msg;
jerr.emit_message = emit_msg;
jerr.output_message = output_msg;
jerr.format_message = format_msg;

if (setjmp(jerr.setjmp_buffer)) {
goto end;
}
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = error_msg;
jerr.pub.emit_message = emit_msg;
jerr.pub.output_message = output_msg;
jerr.pub.format_message = format_msg;

/* Initialize libjpeg */
jpeg_create_decompress(&cinfo);
@@ -124,7 +139,7 @@ int pipi_save_jpeg(pipi_image_t *img, const char *name)
int quality = 75; /* FIXME */

struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
struct my_error_mgr jerr;
unsigned char *data = NULL;
unsigned char *line = NULL;

@@ -144,12 +159,16 @@ int pipi_save_jpeg(pipi_image_t *img, const char *name)
free(line);
return 0;
}
if (setjmp(jerr.setjmp_buffer)) {
goto end;
}
jerr.pub.error_exit = error_msg;
jerr.pub.emit_message = emit_msg;
jerr.pub.output_message = output_msg;

jerr.error_exit = error_msg;
jerr.emit_message = emit_msg;
jerr.output_message = output_msg;

cinfo.err = jpeg_std_error(&(jerr));
cinfo.err = jpeg_std_error(&(jerr.pub));

jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, fp);
@@ -181,6 +200,7 @@ int pipi_save_jpeg(pipi_image_t *img, const char *name)
}

jpeg_finish_compress(&cinfo);
end:
jpeg_destroy_compress(&cinfo);
free(line);
fclose(fp);


Loading…
Cancel
Save