istereo2
diff libs/imago2/file_jpeg.c @ 2:81d35769f546
added the tunnel effect source
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 19 Sep 2015 05:51:51 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/imago2/file_jpeg.c Sat Sep 19 05:51:51 2015 +0300 1.3 @@ -0,0 +1,291 @@ 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 readjpeg(struct img_pixmap *img, struct img_io *io); 1.59 +static int writejpeg(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 int 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 int 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, readjpeg, writejpeg}; 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 + 6, "JFIF", 4) != 0) { 1.91 + io->seek(pos, SEEK_SET, io->uptr); 1.92 + return -1; 1.93 + } 1.94 + io->seek(pos, SEEK_SET, io->uptr); 1.95 + return 0; 1.96 +} 1.97 + 1.98 +static int readjpeg(struct img_pixmap *img, struct img_io *io) 1.99 +{ 1.100 + int i, nlines = 0; 1.101 + struct jpeg_decompress_struct cinfo; 1.102 + struct jpeg_error_mgr jerr; 1.103 + struct src_mgr src; 1.104 + unsigned char **scanlines; 1.105 + 1.106 + cinfo.err = jpeg_std_error(&jerr); /* XXX change... */ 1.107 + jpeg_create_decompress(&cinfo); 1.108 + 1.109 + src.pub.init_source = init_source; 1.110 + src.pub.fill_input_buffer = fill_input_buffer; 1.111 + src.pub.skip_input_data = skip_input_data; 1.112 + src.pub.resync_to_restart = jpeg_resync_to_restart; 1.113 + src.pub.term_source = term_source; 1.114 + src.pub.next_input_byte = 0; 1.115 + src.pub.bytes_in_buffer = 0; 1.116 + src.io = io; 1.117 + cinfo.src = (struct jpeg_source_mgr*)&src; 1.118 + 1.119 + jpeg_read_header(&cinfo, 1); 1.120 + cinfo.out_color_space = JCS_RGB; 1.121 + 1.122 + if(img_set_pixels(img, cinfo.image_width, cinfo.image_height, IMG_FMT_RGB24, 0) == -1) { 1.123 + jpeg_destroy_decompress(&cinfo); 1.124 + return -1; 1.125 + } 1.126 + 1.127 + if(!(scanlines = malloc(img->height * sizeof *scanlines))) { 1.128 + jpeg_destroy_decompress(&cinfo); 1.129 + return -1; 1.130 + } 1.131 + scanlines[0] = img->pixels; 1.132 + for(i=1; i<img->height; i++) { 1.133 + scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz; 1.134 + } 1.135 + 1.136 + jpeg_start_decompress(&cinfo); 1.137 + while(nlines < img->height) { 1.138 + int res = jpeg_read_scanlines(&cinfo, scanlines + nlines, img->height - nlines); 1.139 + nlines += res; 1.140 + } 1.141 + jpeg_finish_decompress(&cinfo); 1.142 + jpeg_destroy_decompress(&cinfo); 1.143 + 1.144 + free(scanlines); 1.145 + return 0; 1.146 +} 1.147 + 1.148 +static int writejpeg(struct img_pixmap *img, struct img_io *io) 1.149 +{ 1.150 + int i, nlines = 0; 1.151 + struct jpeg_compress_struct cinfo; 1.152 + struct jpeg_error_mgr jerr; 1.153 + struct dst_mgr dest; 1.154 + struct img_pixmap tmpimg; 1.155 + unsigned char **scanlines; 1.156 + 1.157 + img_init(&tmpimg); 1.158 + 1.159 + if(img->fmt != IMG_FMT_RGB24) { 1.160 + if(img_copy(&tmpimg, img) == -1) { 1.161 + return -1; 1.162 + } 1.163 + if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) { 1.164 + img_destroy(&tmpimg); 1.165 + return -1; 1.166 + } 1.167 + img = &tmpimg; 1.168 + } 1.169 + 1.170 + if(!(scanlines = malloc(img->height * sizeof *scanlines))) { 1.171 + img_destroy(&tmpimg); 1.172 + return -1; 1.173 + } 1.174 + scanlines[0] = img->pixels; 1.175 + for(i=1; i<img->height; i++) { 1.176 + scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz; 1.177 + } 1.178 + 1.179 + cinfo.err = jpeg_std_error(&jerr); /* XXX */ 1.180 + jpeg_create_compress(&cinfo); 1.181 + 1.182 + dest.pub.init_destination = init_destination; 1.183 + dest.pub.empty_output_buffer = empty_output_buffer; 1.184 + dest.pub.term_destination = term_destination; 1.185 + dest.io = io; 1.186 + cinfo.dest = (struct jpeg_destination_mgr*)&dest; 1.187 + 1.188 + cinfo.image_width = img->width; 1.189 + cinfo.image_height = img->height; 1.190 + cinfo.input_components = 3; 1.191 + cinfo.in_color_space = JCS_RGB; 1.192 + 1.193 + jpeg_set_defaults(&cinfo); 1.194 + 1.195 + jpeg_start_compress(&cinfo, 1); 1.196 + while(nlines < img->height) { 1.197 + int res = jpeg_write_scanlines(&cinfo, scanlines + nlines, img->height - nlines); 1.198 + nlines += res; 1.199 + } 1.200 + jpeg_finish_compress(&cinfo); 1.201 + jpeg_destroy_compress(&cinfo); 1.202 + 1.203 + free(scanlines); 1.204 + img_destroy(&tmpimg); 1.205 + return 0; 1.206 +} 1.207 + 1.208 +/* -- read source functions -- 1.209 + * the following functions are adapted from jdatasrc.c in jpeglib 1.210 + */ 1.211 +static void init_source(j_decompress_ptr jd) 1.212 +{ 1.213 + struct src_mgr *src = (struct src_mgr*)jd->src; 1.214 + src->start_of_file = 1; 1.215 +} 1.216 + 1.217 +static int fill_input_buffer(j_decompress_ptr jd) 1.218 +{ 1.219 + struct src_mgr *src = (struct src_mgr*)jd->src; 1.220 + size_t nbytes; 1.221 + 1.222 + nbytes = src->io->read(src->buffer, INPUT_BUF_SIZE, src->io->uptr); 1.223 + 1.224 + if(nbytes <= 0) { 1.225 + if(src->start_of_file) { 1.226 + return 0; 1.227 + } 1.228 + /* insert a fake EOI marker */ 1.229 + src->buffer[0] = 0xff; 1.230 + src->buffer[1] = JPEG_EOI; 1.231 + nbytes = 2; 1.232 + } 1.233 + 1.234 + src->pub.next_input_byte = src->buffer; 1.235 + src->pub.bytes_in_buffer = nbytes; 1.236 + src->start_of_file = 0; 1.237 + return 1; 1.238 +} 1.239 + 1.240 +static void skip_input_data(j_decompress_ptr jd, long num_bytes) 1.241 +{ 1.242 + struct src_mgr *src = (struct src_mgr*)jd->src; 1.243 + 1.244 + if(num_bytes > 0) { 1.245 + while(num_bytes > (long)src->pub.bytes_in_buffer) { 1.246 + num_bytes -= (long)src->pub.bytes_in_buffer; 1.247 + fill_input_buffer(jd); 1.248 + } 1.249 + src->pub.next_input_byte += (size_t)num_bytes; 1.250 + src->pub.bytes_in_buffer -= (size_t)num_bytes; 1.251 + } 1.252 +} 1.253 + 1.254 +static void term_source(j_decompress_ptr jd) 1.255 +{ 1.256 + /* nothing to see here, move along */ 1.257 +} 1.258 + 1.259 + 1.260 +/* -- write destination functions -- 1.261 + * the following functions are adapted from jdatadst.c in jpeglib 1.262 + */ 1.263 +static void init_destination(j_compress_ptr jc) 1.264 +{ 1.265 + struct dst_mgr *dest = (struct dst_mgr*)jc->dest; 1.266 + 1.267 + dest->pub.next_output_byte = dest->buffer; 1.268 + dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; 1.269 +} 1.270 + 1.271 +static int empty_output_buffer(j_compress_ptr jc) 1.272 +{ 1.273 + struct dst_mgr *dest = (struct dst_mgr*)jc->dest; 1.274 + 1.275 + if(dest->io->write(dest->buffer, OUTPUT_BUF_SIZE, dest->io->uptr) != OUTPUT_BUF_SIZE) { 1.276 + return 0; 1.277 + } 1.278 + 1.279 + dest->pub.next_output_byte = dest->buffer; 1.280 + dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; 1.281 + return 1; 1.282 +} 1.283 + 1.284 +static void term_destination(j_compress_ptr jc) 1.285 +{ 1.286 + struct dst_mgr *dest = (struct dst_mgr*)jc->dest; 1.287 + size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer; 1.288 + 1.289 + /* write any remaining data in the buffer */ 1.290 + if(datacount > 0) { 1.291 + dest->io->write(dest->buffer, datacount, dest->io->uptr); 1.292 + } 1.293 + /* XXX flush? ... */ 1.294 +}