Browse Source

Minor reorg in the JPEG codec.

git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@3344 92316355-f0b4-4df1-b90c-862c8a59935f
master
sam 16 years ago
parent
commit
bc4b740e20
1 changed files with 53 additions and 49 deletions
  1. +53
    -49
      pipi/codec/jpeg.c

+ 53
- 49
pipi/codec/jpeg.c View File

@@ -32,7 +32,8 @@

static int pipi_free_jpeg(pipi_image_t *img);

struct my_error_mgr {
struct my_error_mgr
{
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
@@ -42,15 +43,18 @@ typedef struct my_error_mgr * my_error_ptr;
static void format_msg(j_common_ptr cinfo, char *buf)
{
}

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)
{
}
@@ -65,12 +69,12 @@ pipi_image_t *pipi_load_jpeg(const char *name)
FILE *fp;

fp = fopen(name, "rb");
if(!fp) goto end;
if(!fp)
goto end;

if (setjmp(jerr.setjmp_buffer)) {
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;
@@ -81,40 +85,38 @@ pipi_image_t *pipi_load_jpeg(const char *name)
jpeg_create_decompress(&cinfo);
cinfo.client_data = 0x0;
jpeg_stdio_src(&cinfo, fp);
if(cinfo.client_data == (void *)0x1) {
if(cinfo.client_data == (void *)0x1)
goto end;
}
jpeg_read_header(&cinfo, TRUE);
if(cinfo.client_data == (void *)0x1) {
if(cinfo.client_data == (void *)0x1)
goto end;
}
jpeg_start_decompress(&cinfo);
if(cinfo.client_data == (void *)0x1) {
if(cinfo.client_data == (void *)0x1)
goto end;
}

/* One scanline */
image = malloc(cinfo.output_width * cinfo.output_height * 4);
if(!image) goto end;
if(!image)
goto end;

scanline = malloc(cinfo.output_width * 3);


for(i=0; i < cinfo.output_height; i++)
for(i = 0; i < cinfo.output_height; i++)
{
jpeg_read_scanlines(&cinfo, &scanline, 1);
if(cinfo.client_data == (void *)0x1) {
if(cinfo.client_data == (void *)0x1)
{
free(img);
img = NULL;
goto end;
}
for(j=0 ; j<cinfo.output_width*3; j+=3)
for(j = 0; j < cinfo.output_width * 3; j += 3)
{
image[k+2] = scanline[j];
image[k+1] = scanline[j+1];
image[k] = scanline[j+2];
image[k+3] = 255;
k+=4;
image[k + 2] = scanline[j];
image[k + 1] = scanline[j + 1];
image[k] = scanline[j + 2];
image[k + 3] = 255;
k += 4;
}
}

@@ -123,7 +125,7 @@ pipi_image_t *pipi_load_jpeg(const char *name)
img->p[PIPI_PIXELS_RGBA_U8].pixels = image;
img->p[PIPI_PIXELS_RGBA_U8].w = cinfo.output_width;
img->p[PIPI_PIXELS_RGBA_U8].h = cinfo.output_height;
img->p[PIPI_PIXELS_RGBA_U8].pitch = cinfo.output_width*4;
img->p[PIPI_PIXELS_RGBA_U8].pitch = cinfo.output_width * 4;
img->p[PIPI_PIXELS_RGBA_U8].bpp = 24;
img->p[PIPI_PIXELS_RGBA_U8].bytes = 4 * img->w * img->h;
img->last_modified = PIPI_PIXELS_RGBA_U8;
@@ -136,22 +138,29 @@ pipi_image_t *pipi_load_jpeg(const char *name)
img->u8 = 1;

end:
if(fp) fclose(fp);
if(scanline) free(scanline);
if(fp)
fclose(fp);
if(scanline)
free(scanline);
jpeg_destroy_decompress(&cinfo);
return img;
}

int pipi_save_jpeg(pipi_image_t *img, const char *name)
{
int quality = 75; /* FIXME */

struct jpeg_compress_struct cinfo;
struct my_error_mgr jerr;
unsigned char *data = NULL;
unsigned char *line = NULL;
unsigned char *data;
unsigned char *line;
pipi_pixels_t *pixels;
JSAMPROW *jbuf;
uint32_t *ptr;
FILE *fp;
int i, j, y = 0;
size_t len;

int quality = 75; /* FIXME */

len = strlen(name);
if(len < 4 || name[len - 4] != '.'
|| toupper(name[len - 3]) != 'J'
@@ -166,27 +175,26 @@ int pipi_save_jpeg(pipi_image_t *img, const char *name)
return -1;
}

pipi_pixels_t *pixels = pipi_get_pixels(img, PIPI_PIXELS_RGBA_U8);

if (!pixels)
return 0;
pixels = pipi_get_pixels(img, PIPI_PIXELS_RGBA_U8);
if(!pixels)
return -1;

data = pixels->pixels;

line = malloc(img->w * 3 * sizeof(unsigned char));
if (!line)
return 0;
if(!line)
return -1;

FILE *fp = fopen(name, "wb");
if (!fp) {
fp = fopen(name, "wb");
if(!fp)
{
free(line);
return 0;
return -1;
}
if (setjmp(jerr.setjmp_buffer)) {
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;
@@ -204,11 +212,9 @@ int pipi_save_jpeg(pipi_image_t *img, const char *name)
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);

int j, i, y = 0;
uint32_t *ptr = (uint32_t*)data;
JSAMPROW *jbuf;
ptr = (uint32_t*)data;

while (cinfo.next_scanline < cinfo.image_height)
while(cinfo.next_scanline < cinfo.image_height)
{
for (j = 0, i = 0; i < img->w; i++)
{
@@ -223,6 +229,7 @@ int pipi_save_jpeg(pipi_image_t *img, const char *name)
}

jpeg_finish_compress(&cinfo);

end:
jpeg_destroy_compress(&cinfo);
free(line);
@@ -230,7 +237,7 @@ end:

pipi_release_pixels(img, pixels);

return 1;
return 0;
}

static int pipi_free_jpeg(pipi_image_t *img)
@@ -240,6 +247,3 @@ static int pipi_free_jpeg(pipi_image_t *img)
return 0;
}





Loading…
Cancel
Save