From 245b982a17419d15dd03691e275996d6dd23429b Mon Sep 17 00:00:00 2001 From: jylam Date: Fri, 24 Oct 2008 11:49:31 +0000 Subject: [PATCH] * Added hability to save jpeg using modular codec git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@3081 92316355-f0b4-4df1-b90c-862c8a59935f --- pipi/codec.c | 6 +++- pipi/codec/modular/jpeg.c | 70 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/pipi/codec.c b/pipi/codec.c index ec6ace8..94d07c3 100644 --- a/pipi/codec.c +++ b/pipi/codec.c @@ -38,7 +38,7 @@ pipi_image_t *pipi_load(char const *name) if(!ret) ret = pipi_load_oric(name); -#if USE_LIBJPEG +#if USE_MODULAR if(!ret) ret = pipi_load_modular(name); #endif @@ -90,6 +90,10 @@ int pipi_save(pipi_image_t *img, const char *name) if(ret < 0) ret = pipi_save_oric(img, name); +#if USE_MODULAR + if(ret < 0) + ret = pipi_save_modular(img, name); +#endif #if USE_IMLIB2 if(ret < 0) ret = pipi_save_imlib2(img, name); diff --git a/pipi/codec/modular/jpeg.c b/pipi/codec/modular/jpeg.c index 720f89f..7080e8d 100644 --- a/pipi/codec/modular/jpeg.c +++ b/pipi/codec/modular/jpeg.c @@ -119,11 +119,79 @@ end: int pipi_save_jpeg(pipi_image_t *img, const char *name) { - return 0; + int quality = 75; /* FIXME */ + + struct jpeg_compress_struct cinfo; + struct jpeg_error_mgr jerr; + unsigned char *data = NULL; + unsigned char *line = NULL; + + pipi_pixels_t *pixels = pipi_getpixels(img, PIPI_PIXELS_RGBA_C); + + if (!pixels) + return 0; + + data = pixels->pixels; + + + line = malloc(img->w * 3 * sizeof(unsigned char)); + if (!line) + return 0; + + FILE *fp = fopen(name, "wb"); + if (!fp) { + free(line); + return 0; + } + + jerr.error_exit = error_msg; + jerr.emit_message = emit_msg; + jerr.output_message = output_msg; + + cinfo.err = jpeg_std_error(&(jerr)); + + jpeg_create_compress(&cinfo); + jpeg_stdio_dest(&cinfo, fp); + cinfo.image_width = img->w; + cinfo.image_height = img->h; + 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); + + int j, i, y = 0; + uint32_t *ptr = (uint32_t*)data; + JSAMPROW *jbuf; + + while (cinfo.next_scanline < cinfo.image_height) + { + for (j = 0, i = 0; i < img->w; i++) + { + line[j++] = ((*ptr) >> 16) & 0xff; + line[j++] = ((*ptr) >> 8) & 0xff; + line[j++] = ((*ptr)) & 0xff; + ptr++; + } + /* write scanline */ + jbuf = (JSAMPROW *) (&line); + jpeg_write_scanlines(&cinfo, jbuf, 1); + y++; + } + + jpeg_finish_compress(&cinfo); + jpeg_destroy_compress(&cinfo); + free(line); + fclose(fp); + + return 1; } static int pipi_free_jpeg(pipi_image_t *img) { + if(img->p[PIPI_PIXELS_RGBA_C].pixels) + free(img->p[PIPI_PIXELS_RGBA_C].pixels); return 0; }