dbf-halloween2015

annotate libs/imago/file_jpeg.c @ 2:5ae5fd3626fa

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