istereo

annotate libs/imago2/file_jpeg.c @ 40:9e8fb79fd959

converted to xcode 4 created xcodebuild makefile
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 19 Feb 2012 17:25:41 +0200
parents
children
rev   line source
nuclear@26 1 /*
nuclear@26 2 libimago - a multi-format image file input/output library.
nuclear@26 3 Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
nuclear@26 4
nuclear@26 5 This program is free software: you can redistribute it and/or modify
nuclear@26 6 it under the terms of the GNU Lesser General Public License as published
nuclear@26 7 by the Free Software Foundation, either version 3 of the License, or
nuclear@26 8 (at your option) any later version.
nuclear@26 9
nuclear@26 10 This program is distributed in the hope that it will be useful,
nuclear@26 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@26 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@26 13 GNU Lesser General Public License for more details.
nuclear@26 14
nuclear@26 15 You should have received a copy of the GNU Lesser General Public License
nuclear@26 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@26 17 */
nuclear@26 18
nuclear@26 19 /* -- JPEG module -- */
nuclear@26 20
nuclear@26 21 #include <stdio.h>
nuclear@26 22 #include <stdlib.h>
nuclear@26 23 #include <string.h>
nuclear@26 24
nuclear@26 25 #ifdef WIN32
nuclear@26 26 #include <windows.h>
nuclear@26 27 #define HAVE_BOOLEAN
nuclear@26 28 #endif
nuclear@26 29
nuclear@26 30 #include <jpeglib.h>
nuclear@26 31 #include "imago2.h"
nuclear@26 32 #include "ftype_module.h"
nuclear@26 33
nuclear@26 34 #define INPUT_BUF_SIZE 512
nuclear@26 35 #define OUTPUT_BUF_SIZE 512
nuclear@26 36
nuclear@26 37 /* data source manager: adapted from jdatasrc.c */
nuclear@26 38 struct src_mgr {
nuclear@26 39 struct jpeg_source_mgr pub;
nuclear@26 40
nuclear@26 41 struct img_io *io;
nuclear@26 42 unsigned char buffer[INPUT_BUF_SIZE];
nuclear@26 43 int start_of_file;
nuclear@26 44 };
nuclear@26 45
nuclear@26 46 /* data destination manager: adapted from jdatadst.c */
nuclear@26 47 struct dst_mgr {
nuclear@26 48 struct jpeg_destination_mgr pub;
nuclear@26 49
nuclear@26 50 struct img_io *io;
nuclear@26 51 unsigned char buffer[OUTPUT_BUF_SIZE];
nuclear@26 52 };
nuclear@26 53
nuclear@26 54 static int check(struct img_io *io);
nuclear@26 55 static int read(struct img_pixmap *img, struct img_io *io);
nuclear@26 56 static int write(struct img_pixmap *img, struct img_io *io);
nuclear@26 57
nuclear@26 58 /* read source functions */
nuclear@26 59 static void init_source(j_decompress_ptr jd);
nuclear@26 60 static int fill_input_buffer(j_decompress_ptr jd);
nuclear@26 61 static void skip_input_data(j_decompress_ptr jd, long num_bytes);
nuclear@26 62 static void term_source(j_decompress_ptr jd);
nuclear@26 63
nuclear@26 64 /* write destination functions */
nuclear@26 65 static void init_destination(j_compress_ptr jc);
nuclear@26 66 static int empty_output_buffer(j_compress_ptr jc);
nuclear@26 67 static void term_destination(j_compress_ptr jc);
nuclear@26 68
nuclear@26 69 int img_register_jpeg(void)
nuclear@26 70 {
nuclear@26 71 static struct ftype_module mod = {".jpg", check, read, write};
nuclear@26 72 return img_register_module(&mod);
nuclear@26 73 }
nuclear@26 74
nuclear@26 75
nuclear@26 76 static int check(struct img_io *io)
nuclear@26 77 {
nuclear@26 78 unsigned char sig[10];
nuclear@26 79
nuclear@26 80 long pos = io->seek(0, SEEK_CUR, io->uptr);
nuclear@26 81
nuclear@26 82 if(io->read(sig, 10, io->uptr) < 10) {
nuclear@26 83 io->seek(pos, SEEK_SET, io->uptr);
nuclear@26 84 return -1;
nuclear@26 85 }
nuclear@26 86
nuclear@26 87 if(memcmp(sig, "\xff\xd8\xff\xe0", 4) != 0 || memcmp(sig + 6, "JFIF", 4) != 0) {
nuclear@26 88 io->seek(pos, SEEK_SET, io->uptr);
nuclear@26 89 return -1;
nuclear@26 90 }
nuclear@26 91 io->seek(pos, SEEK_SET, io->uptr);
nuclear@26 92 return 0;
nuclear@26 93 }
nuclear@26 94
nuclear@26 95 static int read(struct img_pixmap *img, struct img_io *io)
nuclear@26 96 {
nuclear@26 97 int i, nlines = 0;
nuclear@26 98 struct jpeg_decompress_struct cinfo;
nuclear@26 99 struct jpeg_error_mgr jerr;
nuclear@26 100 struct src_mgr src;
nuclear@26 101 unsigned char **scanlines;
nuclear@26 102
nuclear@26 103 cinfo.err = jpeg_std_error(&jerr); /* XXX change... */
nuclear@26 104 jpeg_create_decompress(&cinfo);
nuclear@26 105
nuclear@26 106 src.pub.init_source = init_source;
nuclear@26 107 src.pub.fill_input_buffer = fill_input_buffer;
nuclear@26 108 src.pub.skip_input_data = skip_input_data;
nuclear@26 109 src.pub.resync_to_restart = jpeg_resync_to_restart;
nuclear@26 110 src.pub.term_source = term_source;
nuclear@26 111 src.pub.next_input_byte = 0;
nuclear@26 112 src.pub.bytes_in_buffer = 0;
nuclear@26 113 src.io = io;
nuclear@26 114 cinfo.src = (struct jpeg_source_mgr*)&src;
nuclear@26 115
nuclear@26 116 jpeg_read_header(&cinfo, 1);
nuclear@26 117 cinfo.out_color_space = JCS_RGB;
nuclear@26 118
nuclear@26 119 if(img_set_pixels(img, cinfo.image_width, cinfo.image_height, IMG_FMT_RGB24, 0) == -1) {
nuclear@26 120 jpeg_destroy_decompress(&cinfo);
nuclear@26 121 return -1;
nuclear@26 122 }
nuclear@26 123
nuclear@26 124 if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
nuclear@26 125 jpeg_destroy_decompress(&cinfo);
nuclear@26 126 return -1;
nuclear@26 127 }
nuclear@26 128 scanlines[0] = img->pixels;
nuclear@26 129 for(i=1; i<img->height; i++) {
nuclear@26 130 scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
nuclear@26 131 }
nuclear@26 132
nuclear@26 133 jpeg_start_decompress(&cinfo);
nuclear@26 134 while(nlines < img->height) {
nuclear@26 135 int res = jpeg_read_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
nuclear@26 136 nlines += res;
nuclear@26 137 }
nuclear@26 138 jpeg_finish_decompress(&cinfo);
nuclear@26 139 jpeg_destroy_decompress(&cinfo);
nuclear@26 140
nuclear@26 141 free(scanlines);
nuclear@26 142 return 0;
nuclear@26 143 }
nuclear@26 144
nuclear@26 145 static int write(struct img_pixmap *img, struct img_io *io)
nuclear@26 146 {
nuclear@26 147 int i, nlines = 0;
nuclear@26 148 struct jpeg_compress_struct cinfo;
nuclear@26 149 struct jpeg_error_mgr jerr;
nuclear@26 150 struct dst_mgr dest;
nuclear@26 151 struct img_pixmap tmpimg;
nuclear@26 152 unsigned char **scanlines;
nuclear@26 153
nuclear@26 154 img_init(&tmpimg);
nuclear@26 155
nuclear@26 156 if(img->fmt != IMG_FMT_RGB24) {
nuclear@26 157 if(img_copy(&tmpimg, img) == -1) {
nuclear@26 158 return -1;
nuclear@26 159 }
nuclear@26 160 if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
nuclear@26 161 img_destroy(&tmpimg);
nuclear@26 162 return -1;
nuclear@26 163 }
nuclear@26 164 img = &tmpimg;
nuclear@26 165 }
nuclear@26 166
nuclear@26 167 if(!(scanlines = malloc(img->height * sizeof *scanlines))) {
nuclear@26 168 img_destroy(&tmpimg);
nuclear@26 169 return -1;
nuclear@26 170 }
nuclear@26 171 scanlines[0] = img->pixels;
nuclear@26 172 for(i=1; i<img->height; i++) {
nuclear@26 173 scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz;
nuclear@26 174 }
nuclear@26 175
nuclear@26 176 cinfo.err = jpeg_std_error(&jerr); /* XXX */
nuclear@26 177 jpeg_create_compress(&cinfo);
nuclear@26 178
nuclear@26 179 dest.pub.init_destination = init_destination;
nuclear@26 180 dest.pub.empty_output_buffer = empty_output_buffer;
nuclear@26 181 dest.pub.term_destination = term_destination;
nuclear@26 182 dest.io = io;
nuclear@26 183 cinfo.dest = (struct jpeg_destination_mgr*)&dest;
nuclear@26 184
nuclear@26 185 cinfo.image_width = img->width;
nuclear@26 186 cinfo.image_height = img->height;
nuclear@26 187 cinfo.input_components = 3;
nuclear@26 188 cinfo.in_color_space = JCS_RGB;
nuclear@26 189
nuclear@26 190 jpeg_set_defaults(&cinfo);
nuclear@26 191
nuclear@26 192 jpeg_start_compress(&cinfo, 1);
nuclear@26 193 while(nlines < img->height) {
nuclear@26 194 int res = jpeg_write_scanlines(&cinfo, scanlines + nlines, img->height - nlines);
nuclear@26 195 nlines += res;
nuclear@26 196 }
nuclear@26 197 jpeg_finish_compress(&cinfo);
nuclear@26 198 jpeg_destroy_compress(&cinfo);
nuclear@26 199
nuclear@26 200 free(scanlines);
nuclear@26 201 img_destroy(&tmpimg);
nuclear@26 202 return 0;
nuclear@26 203 }
nuclear@26 204
nuclear@26 205 /* -- read source functions --
nuclear@26 206 * the following functions are adapted from jdatasrc.c in jpeglib
nuclear@26 207 */
nuclear@26 208 static void init_source(j_decompress_ptr jd)
nuclear@26 209 {
nuclear@26 210 struct src_mgr *src = (struct src_mgr*)jd->src;
nuclear@26 211 src->start_of_file = 1;
nuclear@26 212 }
nuclear@26 213
nuclear@26 214 static int fill_input_buffer(j_decompress_ptr jd)
nuclear@26 215 {
nuclear@26 216 struct src_mgr *src = (struct src_mgr*)jd->src;
nuclear@26 217 size_t nbytes;
nuclear@26 218
nuclear@26 219 nbytes = src->io->read(src->buffer, INPUT_BUF_SIZE, src->io->uptr);
nuclear@26 220
nuclear@26 221 if(nbytes <= 0) {
nuclear@26 222 if(src->start_of_file) {
nuclear@26 223 return 0;
nuclear@26 224 }
nuclear@26 225 /* insert a fake EOI marker */
nuclear@26 226 src->buffer[0] = 0xff;
nuclear@26 227 src->buffer[1] = JPEG_EOI;
nuclear@26 228 nbytes = 2;
nuclear@26 229 }
nuclear@26 230
nuclear@26 231 src->pub.next_input_byte = src->buffer;
nuclear@26 232 src->pub.bytes_in_buffer = nbytes;
nuclear@26 233 src->start_of_file = 0;
nuclear@26 234 return 1;
nuclear@26 235 }
nuclear@26 236
nuclear@26 237 static void skip_input_data(j_decompress_ptr jd, long num_bytes)
nuclear@26 238 {
nuclear@26 239 struct src_mgr *src = (struct src_mgr*)jd->src;
nuclear@26 240
nuclear@26 241 if(num_bytes > 0) {
nuclear@26 242 while(num_bytes > (long)src->pub.bytes_in_buffer) {
nuclear@26 243 num_bytes -= (long)src->pub.bytes_in_buffer;
nuclear@26 244 fill_input_buffer(jd);
nuclear@26 245 }
nuclear@26 246 src->pub.next_input_byte += (size_t)num_bytes;
nuclear@26 247 src->pub.bytes_in_buffer -= (size_t)num_bytes;
nuclear@26 248 }
nuclear@26 249 }
nuclear@26 250
nuclear@26 251 static void term_source(j_decompress_ptr jd)
nuclear@26 252 {
nuclear@26 253 /* nothing to see here, move along */
nuclear@26 254 }
nuclear@26 255
nuclear@26 256
nuclear@26 257 /* -- write destination functions --
nuclear@26 258 * the following functions are adapted from jdatadst.c in jpeglib
nuclear@26 259 */
nuclear@26 260 static void init_destination(j_compress_ptr jc)
nuclear@26 261 {
nuclear@26 262 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
nuclear@26 263
nuclear@26 264 dest->pub.next_output_byte = dest->buffer;
nuclear@26 265 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
nuclear@26 266 }
nuclear@26 267
nuclear@26 268 static int empty_output_buffer(j_compress_ptr jc)
nuclear@26 269 {
nuclear@26 270 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
nuclear@26 271
nuclear@26 272 if(dest->io->write(dest->buffer, OUTPUT_BUF_SIZE, dest->io->uptr) != OUTPUT_BUF_SIZE) {
nuclear@26 273 return 0;
nuclear@26 274 }
nuclear@26 275
nuclear@26 276 dest->pub.next_output_byte = dest->buffer;
nuclear@26 277 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
nuclear@26 278 return 1;
nuclear@26 279 }
nuclear@26 280
nuclear@26 281 static void term_destination(j_compress_ptr jc)
nuclear@26 282 {
nuclear@26 283 struct dst_mgr *dest = (struct dst_mgr*)jc->dest;
nuclear@26 284 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
nuclear@26 285
nuclear@26 286 /* write any remaining data in the buffer */
nuclear@26 287 if(datacount > 0) {
nuclear@26 288 dest->io->write(dest->buffer, datacount, dest->io->uptr);
nuclear@26 289 }
nuclear@26 290 /* XXX flush? ... */
nuclear@26 291 }