2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
4 This is a small image library.
6 This library is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * author: Mihalis Georgoulopoulos 2004
24 * modified: John Tsiombikas 2004
27 #include "3dengfx_config.h"
31 #ifdef IMGLIB_USE_JPEG
41 #include "color_bits.h"
49 int check_jpeg(FILE *fp){
50 unsigned char sig[10];
52 fseek(fp, 0, SEEK_SET);
53 fread(sig, 1, 10, fp);
55 if(sig[0]!=0xff || sig[1]!=0xd8 || sig[2]!=0xff || sig[3]!=0xe0) {
59 if(/*sig[6]!='J' ||*/sig[7]!='F' || sig[8]!='I' || sig[9]!='F') {
66 void *load_jpeg(FILE *fp, unsigned long *xsz, unsigned long *ysz) {
73 struct jpeg_decompress_struct cinfo;
74 struct jpeg_error_mgr jerr;
75 cinfo.err = jpeg_std_error(&jerr);
77 fseek(fp, 0, SEEK_SET);
79 jpeg_create_decompress(&cinfo);
80 jpeg_stdio_src(&cinfo, fp);
82 jpeg_read_header(&cinfo, TRUE);
84 /* force output to rgb */
85 cinfo.out_color_space = JCS_RGB;
88 buffer = malloc(cinfo.image_width * sizeof(RGBTriplet));
89 if (!buffer) return 0;
90 image = malloc(cinfo.image_width * (cinfo.image_height) * sizeof(uint32_t));
93 tmp = (JSAMPLE*) buffer;
96 *xsz = cinfo.image_width;
97 *ysz = cinfo.image_height;
99 /* Decompress, pack and store */
100 jpeg_start_decompress(&cinfo);
102 while (cinfo.output_scanline < cinfo.output_height)
104 jpeg_read_scanlines(&cinfo, &tmp, 1);
105 if (cinfo.output_scanline == 0) continue;
107 for (i=0;i<cinfo.image_width;i++)
109 int offs = i + (cinfo.output_scanline-1) * cinfo.output_width;
110 image[offs] = PACK_COLOR24(buffer[i].r, buffer[i].g, buffer[i].b);
114 jpeg_finish_decompress(&cinfo);
117 jpeg_destroy_decompress(&cinfo);
121 return (void*) image;
124 /* TODO: implement this */
125 int save_jpeg(FILE *fp, void *pixels, unsigned long xsz, unsigned long ysz) {
126 fprintf(stderr, "saving jpeg files is not implemented yet\n");
130 #endif /* IMGLIB_USE_JPEG */