nuclear@1: /* nuclear@1: libimago - a multi-format image file input/output library. nuclear@1: Copyright (C) 2010 John Tsiombikas nuclear@1: nuclear@1: This program is free software: you can redistribute it and/or modify nuclear@1: it under the terms of the GNU Lesser General Public License as published nuclear@1: by the Free Software Foundation, either version 3 of the License, or nuclear@1: (at your option) any later version. nuclear@1: nuclear@1: This program is distributed in the hope that it will be useful, nuclear@1: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@1: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@1: GNU Lesser General Public License for more details. nuclear@1: nuclear@1: You should have received a copy of the GNU Lesser General Public License nuclear@1: along with this program. If not, see . nuclear@1: */ nuclear@1: nuclear@1: /* -- JPEG module -- */ nuclear@1: nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: nuclear@1: #ifdef WIN32 nuclear@1: #include nuclear@1: #define HAVE_BOOLEAN nuclear@1: #endif nuclear@1: nuclear@1: #include nuclear@1: #include "imago2.h" nuclear@1: #include "ftype_module.h" nuclear@1: nuclear@1: #define INPUT_BUF_SIZE 512 nuclear@1: #define OUTPUT_BUF_SIZE 512 nuclear@1: nuclear@1: /* data source manager: adapted from jdatasrc.c */ nuclear@1: struct src_mgr { nuclear@1: struct jpeg_source_mgr pub; nuclear@1: nuclear@1: struct img_io *io; nuclear@1: unsigned char buffer[INPUT_BUF_SIZE]; nuclear@1: int start_of_file; nuclear@1: }; nuclear@1: nuclear@1: /* data destination manager: adapted from jdatadst.c */ nuclear@1: struct dst_mgr { nuclear@1: struct jpeg_destination_mgr pub; nuclear@1: nuclear@1: struct img_io *io; nuclear@1: unsigned char buffer[OUTPUT_BUF_SIZE]; nuclear@1: }; nuclear@1: nuclear@1: static int check(struct img_io *io); nuclear@1: static int readbuf(struct img_pixmap *img, struct img_io *io); nuclear@1: static int writebuf(struct img_pixmap *img, struct img_io *io); nuclear@1: nuclear@1: /* read source functions */ nuclear@1: static void init_source(j_decompress_ptr jd); nuclear@1: static boolean fill_input_buffer(j_decompress_ptr jd); nuclear@1: static void skip_input_data(j_decompress_ptr jd, long num_bytes); nuclear@1: static void term_source(j_decompress_ptr jd); nuclear@1: nuclear@1: /* write destination functions */ nuclear@1: static void init_destination(j_compress_ptr jc); nuclear@1: static boolean empty_output_buffer(j_compress_ptr jc); nuclear@1: static void term_destination(j_compress_ptr jc); nuclear@1: nuclear@1: int img_register_jpeg(void) nuclear@1: { nuclear@1: static struct ftype_module mod = {".jpg", check, readbuf, writebuf}; nuclear@1: return img_register_module(&mod); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: static int check(struct img_io *io) nuclear@1: { nuclear@1: unsigned char sig[10]; nuclear@1: nuclear@1: long pos = io->seek(0, SEEK_CUR, io->uptr); nuclear@1: nuclear@1: if(io->read(sig, 10, io->uptr) < 10) { nuclear@1: io->seek(pos, SEEK_SET, io->uptr); nuclear@1: return -1; nuclear@1: } nuclear@1: nuclear@1: if(memcmp(sig, "\xff\xd8\xff\xe0", 4) != 0 && memcmp(sig, "\xff\xd8\xff\xe1", 4) != 0 nuclear@1: && memcmp(sig + 6, "JFIF", 4) != 0) { nuclear@1: io->seek(pos, SEEK_SET, io->uptr); nuclear@1: return -1; nuclear@1: } nuclear@1: io->seek(pos, SEEK_SET, io->uptr); nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: static int readbuf(struct img_pixmap *img, struct img_io *io) nuclear@1: { nuclear@1: int i, nlines = 0; nuclear@1: struct jpeg_decompress_struct cinfo; nuclear@1: struct jpeg_error_mgr jerr; nuclear@1: struct src_mgr src; nuclear@1: unsigned char **scanlines; nuclear@1: nuclear@1: io->seek(0, SEEK_CUR, io->uptr); nuclear@1: nuclear@1: cinfo.err = jpeg_std_error(&jerr); /* XXX change... */ nuclear@1: jpeg_create_decompress(&cinfo); nuclear@1: nuclear@1: src.pub.init_source = init_source; nuclear@1: src.pub.fill_input_buffer = fill_input_buffer; nuclear@1: src.pub.skip_input_data = skip_input_data; nuclear@1: src.pub.resync_to_restart = jpeg_resync_to_restart; nuclear@1: src.pub.term_source = term_source; nuclear@1: src.pub.next_input_byte = 0; nuclear@1: src.pub.bytes_in_buffer = 0; nuclear@1: src.io = io; nuclear@1: cinfo.src = (struct jpeg_source_mgr*)&src; nuclear@1: nuclear@1: jpeg_read_header(&cinfo, 1); nuclear@1: cinfo.out_color_space = JCS_RGB; nuclear@1: nuclear@1: if(img_set_pixels(img, cinfo.image_width, cinfo.image_height, IMG_FMT_RGB24, 0) == -1) { nuclear@1: jpeg_destroy_decompress(&cinfo); nuclear@1: return -1; nuclear@1: } nuclear@1: nuclear@1: if(!(scanlines = malloc(img->height * sizeof *scanlines))) { nuclear@1: jpeg_destroy_decompress(&cinfo); nuclear@1: return -1; nuclear@1: } nuclear@1: scanlines[0] = img->pixels; nuclear@1: for(i=1; iheight; i++) { nuclear@1: scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz; nuclear@1: } nuclear@1: nuclear@1: jpeg_start_decompress(&cinfo); nuclear@1: while(nlines < img->height) { nuclear@1: int res = jpeg_read_scanlines(&cinfo, scanlines + nlines, img->height - nlines); nuclear@1: nlines += res; nuclear@1: } nuclear@1: jpeg_finish_decompress(&cinfo); nuclear@1: jpeg_destroy_decompress(&cinfo); nuclear@1: nuclear@1: free(scanlines); nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: static int writebuf(struct img_pixmap *img, struct img_io *io) nuclear@1: { nuclear@1: int i, nlines = 0; nuclear@1: struct jpeg_compress_struct cinfo; nuclear@1: struct jpeg_error_mgr jerr; nuclear@1: struct dst_mgr dest; nuclear@1: struct img_pixmap tmpimg; nuclear@1: unsigned char **scanlines; nuclear@1: nuclear@1: img_init(&tmpimg); nuclear@1: nuclear@1: if(img->fmt != IMG_FMT_RGB24) { nuclear@1: if(img_copy(&tmpimg, img) == -1) { nuclear@1: return -1; nuclear@1: } nuclear@1: if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) { nuclear@1: img_destroy(&tmpimg); nuclear@1: return -1; nuclear@1: } nuclear@1: img = &tmpimg; nuclear@1: } nuclear@1: nuclear@1: if(!(scanlines = malloc(img->height * sizeof *scanlines))) { nuclear@1: img_destroy(&tmpimg); nuclear@1: return -1; nuclear@1: } nuclear@1: scanlines[0] = img->pixels; nuclear@1: for(i=1; iheight; i++) { nuclear@1: scanlines[i] = scanlines[i - 1] + img->width * img->pixelsz; nuclear@1: } nuclear@1: nuclear@1: cinfo.err = jpeg_std_error(&jerr); /* XXX */ nuclear@1: jpeg_create_compress(&cinfo); nuclear@1: nuclear@1: dest.pub.init_destination = init_destination; nuclear@1: dest.pub.empty_output_buffer = empty_output_buffer; nuclear@1: dest.pub.term_destination = term_destination; nuclear@1: dest.io = io; nuclear@1: cinfo.dest = (struct jpeg_destination_mgr*)&dest; nuclear@1: nuclear@1: cinfo.image_width = img->width; nuclear@1: cinfo.image_height = img->height; nuclear@1: cinfo.input_components = 3; nuclear@1: cinfo.in_color_space = JCS_RGB; nuclear@1: nuclear@1: jpeg_set_defaults(&cinfo); nuclear@1: nuclear@1: jpeg_start_compress(&cinfo, 1); nuclear@1: while(nlines < img->height) { nuclear@1: int res = jpeg_write_scanlines(&cinfo, scanlines + nlines, img->height - nlines); nuclear@1: nlines += res; nuclear@1: } nuclear@1: jpeg_finish_compress(&cinfo); nuclear@1: jpeg_destroy_compress(&cinfo); nuclear@1: nuclear@1: free(scanlines); nuclear@1: img_destroy(&tmpimg); nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: /* -- read source functions -- nuclear@1: * the following functions are adapted from jdatasrc.c in jpeglib nuclear@1: */ nuclear@1: static void init_source(j_decompress_ptr jd) nuclear@1: { nuclear@1: struct src_mgr *src = (struct src_mgr*)jd->src; nuclear@1: src->start_of_file = 1; nuclear@1: } nuclear@1: nuclear@1: static boolean fill_input_buffer(j_decompress_ptr jd) nuclear@1: { nuclear@1: struct src_mgr *src = (struct src_mgr*)jd->src; nuclear@1: size_t nbytes; nuclear@1: nuclear@1: nbytes = src->io->read(src->buffer, INPUT_BUF_SIZE, src->io->uptr); nuclear@1: nuclear@1: if(nbytes <= 0) { nuclear@1: if(src->start_of_file) { nuclear@1: return 0; nuclear@1: } nuclear@1: /* insert a fake EOI marker */ nuclear@1: src->buffer[0] = 0xff; nuclear@1: src->buffer[1] = JPEG_EOI; nuclear@1: nbytes = 2; nuclear@1: } nuclear@1: nuclear@1: src->pub.next_input_byte = src->buffer; nuclear@1: src->pub.bytes_in_buffer = nbytes; nuclear@1: src->start_of_file = 0; nuclear@1: return 1; nuclear@1: } nuclear@1: nuclear@1: static void skip_input_data(j_decompress_ptr jd, long num_bytes) nuclear@1: { nuclear@1: struct src_mgr *src = (struct src_mgr*)jd->src; nuclear@1: nuclear@1: if(num_bytes > 0) { nuclear@1: while(num_bytes > (long)src->pub.bytes_in_buffer) { nuclear@1: num_bytes -= (long)src->pub.bytes_in_buffer; nuclear@1: fill_input_buffer(jd); nuclear@1: } nuclear@1: src->pub.next_input_byte += (size_t)num_bytes; nuclear@1: src->pub.bytes_in_buffer -= (size_t)num_bytes; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: static void term_source(j_decompress_ptr jd) nuclear@1: { nuclear@1: /* nothing to see here, move along */ nuclear@1: } nuclear@1: nuclear@1: nuclear@1: /* -- write destination functions -- nuclear@1: * the following functions are adapted from jdatadst.c in jpeglib nuclear@1: */ nuclear@1: static void init_destination(j_compress_ptr jc) nuclear@1: { nuclear@1: struct dst_mgr *dest = (struct dst_mgr*)jc->dest; nuclear@1: nuclear@1: dest->pub.next_output_byte = dest->buffer; nuclear@1: dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; nuclear@1: } nuclear@1: nuclear@1: static boolean empty_output_buffer(j_compress_ptr jc) nuclear@1: { nuclear@1: struct dst_mgr *dest = (struct dst_mgr*)jc->dest; nuclear@1: nuclear@1: if(dest->io->write(dest->buffer, OUTPUT_BUF_SIZE, dest->io->uptr) != OUTPUT_BUF_SIZE) { nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: dest->pub.next_output_byte = dest->buffer; nuclear@1: dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; nuclear@1: return 1; nuclear@1: } nuclear@1: nuclear@1: static void term_destination(j_compress_ptr jc) nuclear@1: { nuclear@1: struct dst_mgr *dest = (struct dst_mgr*)jc->dest; nuclear@1: size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer; nuclear@1: nuclear@1: /* write any remaining data in the buffer */ nuclear@1: if(datacount > 0) { nuclear@1: dest->io->write(dest->buffer, datacount, dest->io->uptr); nuclear@1: } nuclear@1: /* XXX flush? ... */ nuclear@1: }