eqemu

annotate libs/libimago/src/file_jpeg.c @ 10:819c7ebb1bec

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