3dphotoshoot

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