From d9c4fee5be3f73586714d4d74a00d873af8ea834 Mon Sep 17 00:00:00 2001 From: jylam Date: Fri, 24 Oct 2008 12:03:15 +0000 Subject: [PATCH] * Added file extension detection to modular codec git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/libpipi/trunk@3083 92316355-f0b4-4df1-b90c-862c8a59935f --- pipi/codec/modular.c | 24 +++++++++++++++++++++++- pipi/codec/modular/jpeg.c | 8 ++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/pipi/codec/modular.c b/pipi/codec/modular.c index eeb3bb2..1a7d55d 100644 --- a/pipi/codec/modular.c +++ b/pipi/codec/modular.c @@ -1,3 +1,22 @@ +/* + * libpipi Pathetic image processing interface library + * Copyright (c) 2004-2008 Sam Hocevar + * 2008 Jean-Yves Lamoureux + * All Rights Reserved + * + * $Id$ + * + * This library is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +/* + * modular.c: hand-written codecs multiplexer + */ + #include "modular.h" @@ -8,5 +27,8 @@ pipi_image_t *pipi_load_modular(const char *name) int pipi_save_modular(pipi_image_t *img, const char *name) { - return pipi_save_jpeg(img, name); + if(!strncasecmp(&name[strlen(name) - 3], "jpg", 3) || + !strncasecmp(&name[strlen(name) - 4], "jpeg", 4) ) + return pipi_save_jpeg(img, name); + return -1; } diff --git a/pipi/codec/modular/jpeg.c b/pipi/codec/modular/jpeg.c index 7080e8d..793eee3 100644 --- a/pipi/codec/modular/jpeg.c +++ b/pipi/codec/modular/jpeg.c @@ -47,13 +47,14 @@ 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; + /* Initialize libjpeg */ jpeg_create_decompress(&cinfo); cinfo.client_data = 0x0; jpeg_stdio_src(&cinfo, fp); @@ -69,12 +70,13 @@ pipi_image_t *pipi_load_jpeg(const char *name) goto end; } + /* One scanline */ image = malloc(cinfo.output_width * cinfo.output_height * 4); if(!image) goto end; scanline = malloc(cinfo.output_width * 3); - /* Read scanlines, converting them to RGBA */ + for(i=0; i < cinfo.output_height; i++) { jpeg_read_scanlines(&cinfo, &scanline, 1); @@ -133,7 +135,6 @@ int pipi_save_jpeg(pipi_image_t *img, const char *name) data = pixels->pixels; - line = malloc(img->w * 3 * sizeof(unsigned char)); if (!line) return 0; @@ -174,7 +175,6 @@ int pipi_save_jpeg(pipi_image_t *img, const char *name) line[j++] = ((*ptr)) & 0xff; ptr++; } - /* write scanline */ jbuf = (JSAMPROW *) (&line); jpeg_write_scanlines(&cinfo, jbuf, 1); y++;