added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / gfx / image_jpg.c
1 /*
2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
3
4 This is a small image library.
5
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.
10
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.
15
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
19 */
20
21 /* jpeg support
22  * 
23  * author: Mihalis Georgoulopoulos 2004
24  * modified: John Tsiombikas 2004
25  */
26
27 #include "3dengfx_config.h"
28 #include "image.h"
29 #include <stdio.h>
30
31 #ifdef IMGLIB_USE_JPEG
32
33 #include <stdlib.h>
34
35 #ifdef WIN32
36 #include <windows.h>
37 #define HAVE_BOOLEAN
38 #endif
39
40 #include <jpeglib.h>
41 #include "color_bits.h"
42
43 typedef struct
44 {
45         unsigned char r,g,b;
46 } RGBTriplet;
47
48 /*jpeg signature*/
49 int check_jpeg(FILE *fp){
50         unsigned char sig[10];
51
52         fseek(fp, 0, SEEK_SET);
53         fread(sig, 1, 10, fp);
54         
55     if(sig[0]!=0xff || sig[1]!=0xd8 || sig[2]!=0xff || sig[3]!=0xe0) {
56                 return 0;
57         }
58
59         if(/*sig[6]!='J' ||*/sig[7]!='F' || sig[8]!='I' || sig[9]!='F') {
60                 return 0;
61         }
62         
63     return 1;
64 }
65
66 void *load_jpeg(FILE *fp, unsigned long *xsz, unsigned long *ysz) {
67         int i;
68         RGBTriplet *buffer;
69         uint32_t *image;
70         
71         JSAMPLE *tmp;
72     
73         struct jpeg_decompress_struct cinfo;
74         struct jpeg_error_mgr jerr;
75         cinfo.err = jpeg_std_error(&jerr);
76         
77         fseek(fp, 0, SEEK_SET);
78
79         jpeg_create_decompress(&cinfo);
80         jpeg_stdio_src(&cinfo, fp);
81         
82         jpeg_read_header(&cinfo, TRUE);
83         
84         /* force output to rgb */
85         cinfo.out_color_space = JCS_RGB;
86         
87     /* allocate space */
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));
91     if (!image) return 0;
92     
93     tmp = (JSAMPLE*) buffer;
94
95         /* return w and h */
96         *xsz = cinfo.image_width;
97         *ysz = cinfo.image_height;
98     
99         /* Decompress, pack and store */
100     jpeg_start_decompress(&cinfo);
101     {
102         while (cinfo.output_scanline < cinfo.output_height) 
103         {
104             jpeg_read_scanlines(&cinfo, &tmp, 1);
105                         if (cinfo.output_scanline == 0) continue;
106             
107             for (i=0;i<cinfo.image_width;i++)
108             {
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);
111             }
112         }
113     }
114     jpeg_finish_decompress(&cinfo);
115         
116         /*Done - cleanup*/
117         jpeg_destroy_decompress(&cinfo);
118         free(buffer);
119         fclose(fp);
120
121         return (void*) image;
122 }
123
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");
127         return -1;
128 }
129
130 #endif  /* IMGLIB_USE_JPEG */