3dphotoshoot

diff libs/imago/file_jpeg.c @ 14:06dc8b9b4f89

added libimago, libjpeg and libpng
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Jun 2015 17:25:49 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/imago/file_jpeg.c	Sun Jun 07 17:25:49 2015 +0300
     1.3 @@ -0,0 +1,294 @@
     1.4 +/*
     1.5 +libimago - a multi-format image file input/output library.
     1.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     1.7 +
     1.8 +This program is free software: you can redistribute it and/or modify
     1.9 +it under the terms of the GNU Lesser General Public License as published
    1.10 +by the Free Software Foundation, either version 3 of the License, or
    1.11 +(at your option) any later version.
    1.12 +
    1.13 +This program is distributed in the hope that it will be useful,
    1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +GNU Lesser General Public License for more details.
    1.17 +
    1.18 +You should have received a copy of the GNU Lesser General Public License
    1.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 +*/
    1.21 +
    1.22 +/* -- JPEG module -- */
    1.23 +
    1.24 +#include <stdio.h>
    1.25 +#include <stdlib.h>
    1.26 +#include <string.h>
    1.27 +
    1.28 +#ifdef WIN32
    1.29 +#include <windows.h>
    1.30 +#define HAVE_BOOLEAN
    1.31 +#endif
    1.32 +
    1.33 +#include <jpeglib.h>
    1.34 +#include "imago2.h"
    1.35 +#include "ftype_module.h"
    1.36 +
    1.37 +#define INPUT_BUF_SIZE	512
    1.38 +#define OUTPUT_BUF_SIZE	512
    1.39 +
    1.40 +/* data source manager: adapted from jdatasrc.c */
    1.41 +struct src_mgr {
    1.42 +	struct jpeg_source_mgr pub;
    1.43 +
    1.44 +	struct img_io *io;
    1.45 +	unsigned char buffer[INPUT_BUF_SIZE];
    1.46 +	int start_of_file;
    1.47 +};
    1.48 +
    1.49 +/* data destination manager: adapted from jdatadst.c */
    1.50 +struct dst_mgr {
    1.51 +	struct jpeg_destination_mgr pub;
    1.52 +
    1.53 +	struct img_io *io;
    1.54 +	unsigned char buffer[OUTPUT_BUF_SIZE];
    1.55 +};
    1.56 +
    1.57 +static int check(struct img_io *io);
    1.58 +static int read(struct img_pixmap *img, struct img_io *io);
    1.59 +static int write(struct img_pixmap *img, struct img_io *io);
    1.60 +
    1.61 +/* read source functions */
    1.62 +static void init_source(j_decompress_ptr jd);
    1.63 +static boolean fill_input_buffer(j_decompress_ptr jd);
    1.64 +static void skip_input_data(j_decompress_ptr jd, long num_bytes);
    1.65 +static void term_source(j_decompress_ptr jd);
    1.66 +
    1.67 +/* write destination functions */
    1.68 +static void init_destination(j_compress_ptr jc);
    1.69 +static boolean empty_output_buffer(j_compress_ptr jc);
    1.70 +static void term_destination(j_compress_ptr jc);
    1.71 +
    1.72 +int img_register_jpeg(void)
    1.73 +{
    1.74 +	static struct ftype_module mod = {".jpg", check, read, write};
    1.75 +	return img_register_module(&mod);
    1.76 +}
    1.77 +
    1.78 +
    1.79 +static int check(struct img_io *io)
    1.80 +{
    1.81 +	unsigned char sig[10];
    1.82 +
    1.83 +	long pos = io->seek(0, SEEK_CUR, io->uptr);
    1.84 +
    1.85 +	if(io->read(sig, 10, io->uptr) < 10) {
    1.86 +		io->seek(pos, SEEK_SET, io->uptr);
    1.87 +		return -1;
    1.88 +	}
    1.89 +
    1.90 +	if(memcmp(sig, "\xff\xd8\xff\xe0", 4) != 0 && memcmp(sig, "\xff\xd8\xff\xe1", 4) != 0
    1.91 +			&& memcmp(sig + 6, "JFIF", 4) != 0) {
    1.92 +		io->seek(pos, SEEK_SET, io->uptr);
    1.93 +		return -1;
    1.94 +	}
    1.95 +	io->seek(pos, SEEK_SET, io->uptr);
    1.96 +	return 0;
    1.97 +}
    1.98 +
    1.99 +static int read(struct img_pixmap *img, struct img_io *io)
   1.100 +{
   1.101 +	int i, nlines = 0;
   1.102 +	struct jpeg_decompress_struct cinfo;
   1.103 +	struct jpeg_error_mgr jerr;
   1.104 +	struct src_mgr src;
   1.105 +	unsigned char **scanlines;
   1.106 +
   1.107 +	io->seek(0, SEEK_CUR, io->uptr);
   1.108 +
   1.109 +	cinfo.err = jpeg_std_error(&jerr);	/* XXX change... */
   1.110 +	jpeg_create_decompress(&cinfo);
   1.111 +
   1.112 +	src.pub.init_source = init_source;
   1.113 +	src.pub.fill_input_buffer = fill_input_buffer;
   1.114 +	src.pub.skip_input_data = skip_input_data;
   1.115 +	src.pub.resync_to_restart = jpeg_resync_to_restart;
   1.116 +	src.pub.term_source = term_source;
   1.117 +	src.pub.next_input_byte = 0;
   1.118 +	src.pub.bytes_in_buffer = 0;
   1.119 +	src.io = io;
   1.120 +	cinfo.src = (struct jpeg_source_mgr*)&src;
   1.121 +
   1.122 +	jpeg_read_header(&cinfo, 1);
   1.123 +	cinfo.out_color_space = JCS_RGB;
   1.124 +
   1.125 +	if(img_set_pixels(img, cinfo.image_width, cinfo.image_height, IMG_FMT_RGB24, 0) == -1) {
   1.126 +		jpeg_destroy_decompress(&cinfo);
   1.127 +		return -1;
   1.128 +	}
   1.129 +
   1.130 +	if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
   1.131 +		jpeg_destroy_decompress(&cinfo);
   1.132 +		return -1;
   1.133 +	}
   1.134 +	scanlines[0] = img->pixels;
   1.135 +	for(i=1; i<img->height; i++) {
   1.136 +		scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
   1.137 +	}
   1.138 +
   1.139 +	jpeg_start_decompress(&cinfo);
   1.140 +	while(nlines < img->height) {
   1.141 +		int res = jpeg_read_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
   1.142 +		nlines += res;
   1.143 +	}
   1.144 +	jpeg_finish_decompress(&cinfo);
   1.145 +	jpeg_destroy_decompress(&cinfo);
   1.146 +
   1.147 +	free(scanlines);
   1.148 +	return 0;
   1.149 +}
   1.150 +
   1.151 +static int write(struct img_pixmap *img, struct img_io *io)
   1.152 +{
   1.153 +	int i, nlines = 0;
   1.154 +	struct jpeg_compress_struct cinfo;
   1.155 +	struct jpeg_error_mgr jerr;
   1.156 +	struct dst_mgr dest;
   1.157 +	struct img_pixmap tmpimg;
   1.158 +	unsigned char **scanlines;
   1.159 +
   1.160 +	img_init(&tmpimg);
   1.161 +
   1.162 +	if(img->fmt != IMG_FMT_RGB24) {
   1.163 +		if(img_copy(&tmpimg, img) == -1) {
   1.164 +			return -1;
   1.165 +		}
   1.166 +		if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
   1.167 +			img_destroy(&tmpimg);
   1.168 +			return -1;
   1.169 +		}
   1.170 +		img = &tmpimg;
   1.171 +	}
   1.172 +
   1.173 +	if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
   1.174 +		img_destroy(&tmpimg);
   1.175 +		return -1;
   1.176 +	}
   1.177 +	scanlines[0] = img->pixels;
   1.178 +	for(i=1; i<img->height; i++) {
   1.179 +		scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
   1.180 +	}
   1.181 +
   1.182 +	cinfo.err = jpeg_std_error(&jerr);	/* XXX */
   1.183 +	jpeg_create_compress(&cinfo);
   1.184 +
   1.185 +	dest.pub.init_destination = init_destination;
   1.186 +	dest.pub.empty_output_buffer = empty_output_buffer;
   1.187 +	dest.pub.term_destination = term_destination;
   1.188 +	dest.io = io;
   1.189 +	cinfo.dest = (struct jpeg_destination_mgr*)&dest;
   1.190 +
   1.191 +	cinfo.image_width = img->width;
   1.192 +	cinfo.image_height = img->height;
   1.193 +	cinfo.input_components = 3;
   1.194 +	cinfo.in_color_space = JCS_RGB;
   1.195 +
   1.196 +	jpeg_set_defaults(&cinfo);
   1.197 +
   1.198 +	jpeg_start_compress(&cinfo, 1);
   1.199 +	while(nlines < img->height) {
   1.200 +		int res = jpeg_write_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
   1.201 +		nlines += res;
   1.202 +	}
   1.203 +	jpeg_finish_compress(&cinfo);
   1.204 +	jpeg_destroy_compress(&cinfo);
   1.205 +
   1.206 +	free(scanlines);
   1.207 +	img_destroy(&tmpimg);
   1.208 +	return 0;
   1.209 +}
   1.210 +
   1.211 +/* -- read source functions --
   1.212 + * the following functions are adapted from jdatasrc.c in jpeglib
   1.213 + */
   1.214 +static void init_source(j_decompress_ptr jd)
   1.215 +{
   1.216 +	struct src_mgr *src = (struct src_mgr*)jd->src;
   1.217 +	src->start_of_file = 1;
   1.218 +}
   1.219 +
   1.220 +static boolean fill_input_buffer(j_decompress_ptr jd)
   1.221 +{
   1.222 +	struct src_mgr *src = (struct src_mgr*)jd->src;
   1.223 +	size_t nbytes;
   1.224 +
   1.225 +	nbytes = src->io->read(src->buffer, INPUT_BUF_SIZE, src->io->uptr);
   1.226 +
   1.227 +	if(nbytes <= 0) {
   1.228 +		if(src->start_of_file) {
   1.229 +			return 0;
   1.230 +		}
   1.231 +		/* insert a fake EOI marker */
   1.232 +		src->buffer[0] = 0xff;
   1.233 +		src->buffer[1] = JPEG_EOI;
   1.234 +		nbytes = 2;
   1.235 +	}
   1.236 +
   1.237 +	src->pub.next_input_byte = src->buffer;
   1.238 +	src->pub.bytes_in_buffer = nbytes;
   1.239 +	src->start_of_file = 0;
   1.240 +	return 1;
   1.241 +}
   1.242 +
   1.243 +static void skip_input_data(j_decompress_ptr jd, long num_bytes)
   1.244 +{
   1.245 +	struct src_mgr *src = (struct src_mgr*)jd->src;
   1.246 +
   1.247 +	if(num_bytes > 0) {
   1.248 +		while(num_bytes > (long)src->pub.bytes_in_buffer) {
   1.249 +			num_bytes -= (long)src->pub.bytes_in_buffer;
   1.250 +			fill_input_buffer(jd);
   1.251 +		}
   1.252 +		src->pub.next_input_byte += (size_t)num_bytes;
   1.253 +		src->pub.bytes_in_buffer -= (size_t)num_bytes;
   1.254 +	}
   1.255 +}
   1.256 +
   1.257 +static void term_source(j_decompress_ptr jd)
   1.258 +{
   1.259 +	/* nothing to see here, move along */
   1.260 +}
   1.261 +
   1.262 +
   1.263 +/* -- write destination functions --
   1.264 + * the following functions are adapted from jdatadst.c in jpeglib
   1.265 + */
   1.266 +static void init_destination(j_compress_ptr jc)
   1.267 +{
   1.268 +	struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
   1.269 +
   1.270 +	dest->pub.next_output_byte = dest->buffer;
   1.271 +	dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
   1.272 +}
   1.273 +
   1.274 +static boolean empty_output_buffer(j_compress_ptr jc)
   1.275 +{
   1.276 +	struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
   1.277 +
   1.278 +	if(dest->io->write(dest->buffer, OUTPUT_BUF_SIZE, dest->io->uptr) != OUTPUT_BUF_SIZE) {
   1.279 +		return 0;
   1.280 +	}
   1.281 +
   1.282 +	dest->pub.next_output_byte = dest->buffer;
   1.283 +	dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
   1.284 +	return 1;
   1.285 +}
   1.286 +
   1.287 +static void term_destination(j_compress_ptr jc)
   1.288 +{
   1.289 +	struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
   1.290 +	size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
   1.291 +
   1.292 +	/* write any remaining data in the buffer */
   1.293 +	if(datacount > 0) {
   1.294 +		dest->io->write(dest->buffer, datacount, dest->io->uptr);
   1.295 +	}
   1.296 +	/* XXX flush? ... */
   1.297 +}