istereo2

annotate libs/imago2/file_jpeg.c @ 35:643f4ab609a4

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