polygon intersector: added horizontal line reconstruction
[swftools.git] / lib / jpeg.c
1 #include <stdio.h> 
2 #include <stdlib.h>
3 #include <memory.h>
4 #include "jpeg.h"
5 #include "../config.h"
6
7 #ifdef HAVE_JPEGLIB
8 #define HAVE_BOOLEAN
9 #include <jpeglib.h>
10
11 #define OUTBUFFER_SIZE 0x8000
12
13 static FILE*fi;
14 static JOCTET * buffer;
15 static unsigned char*dest;
16 static int len;
17 static int destlen;
18 static unsigned char*data;
19 static int pos;
20 static int size;
21
22 static void file_init_destination(j_compress_ptr cinfo) 
23
24   struct jpeg_destination_mgr*dmgr = 
25       (struct jpeg_destination_mgr*)(cinfo->dest);
26   buffer = (JOCTET*)malloc(OUTBUFFER_SIZE);
27   if(!buffer) {
28       perror("malloc");
29       printf("Out of memory!\n");
30       exit(1);
31   }
32   dmgr->next_output_byte = buffer;
33   dmgr->free_in_buffer = OUTBUFFER_SIZE;
34 }
35
36 static boolean file_empty_output_buffer(j_compress_ptr cinfo)
37
38   struct jpeg_destination_mgr*dmgr = 
39       (struct jpeg_destination_mgr*)(cinfo->dest);
40   if(fi)
41     fwrite(buffer, OUTBUFFER_SIZE, 1, fi);
42   dmgr->next_output_byte = buffer;
43   dmgr->free_in_buffer = OUTBUFFER_SIZE;
44   return 1;
45 }
46
47 static void file_term_destination(j_compress_ptr cinfo) 
48 { struct jpeg_destination_mgr*dmgr = 
49       (struct jpeg_destination_mgr*)(cinfo->dest);
50   if(fi)
51     fwrite(buffer, OUTBUFFER_SIZE-dmgr->free_in_buffer, 1, fi);
52   free(buffer);
53   buffer = 0;
54   dmgr->free_in_buffer = 0;
55 }
56
57 static void mem_init_destination(j_compress_ptr cinfo) 
58
59   struct jpeg_destination_mgr*dmgr = 
60       (struct jpeg_destination_mgr*)(cinfo->dest);
61   dmgr->next_output_byte = dest;
62   dmgr->free_in_buffer = destlen;
63 }
64
65 static boolean mem_empty_output_buffer(j_compress_ptr cinfo)
66
67     printf("jpeg mem overflow!\n");
68     exit(1);
69 }
70
71 static void mem_term_destination(j_compress_ptr cinfo) 
72
73   struct jpeg_destination_mgr*dmgr = 
74       (struct jpeg_destination_mgr*)(cinfo->dest);
75   len = destlen - dmgr->free_in_buffer;
76   dmgr->free_in_buffer = 0;
77 }
78
79 int jpeg_save(unsigned char*data, int width, int height, int quality, const char*filename)
80 {
81   struct jpeg_destination_mgr mgr;
82   struct jpeg_compress_struct cinfo;
83   struct jpeg_error_mgr jerr;
84   int t;
85
86   if(filename)
87     fi = fopen(filename, "wb");
88   else
89     fi = 0;
90
91   memset(&cinfo, 0, sizeof(cinfo));
92   memset(&jerr, 0, sizeof(jerr));
93   memset(&mgr, 0, sizeof(mgr));
94   cinfo.err = jpeg_std_error(&jerr);
95   jpeg_create_compress(&cinfo);
96
97   mgr.init_destination = file_init_destination;
98   mgr.empty_output_buffer = file_empty_output_buffer;
99   mgr.term_destination = file_term_destination;
100   cinfo.dest = &mgr;
101
102   // init compression
103   
104   cinfo.image_width  = width;
105   cinfo.image_height = height;
106   cinfo.input_components = 3;
107   cinfo.in_color_space = JCS_RGB;
108   jpeg_set_defaults(&cinfo);
109   jpeg_set_quality(&cinfo,quality,TRUE);
110
111   //jpeg_write_tables(&cinfo);
112   //jpeg_suppress_tables(&cinfo, TRUE);
113   jpeg_start_compress(&cinfo, FALSE);
114   
115   for(t=0;t<height;t++) {
116     unsigned char*data2 = &data[width*3*t];
117     jpeg_write_scanlines(&cinfo, &data2, 1);
118   }
119   jpeg_finish_compress(&cinfo);
120
121   if(fi)
122     fclose(fi);
123   jpeg_destroy_compress(&cinfo);
124   return 1;
125 }
126
127 int jpeg_save_to_file(unsigned char*data, int width, int height, int quality, FILE*_fi)
128 {
129   struct jpeg_destination_mgr mgr;
130   struct jpeg_compress_struct cinfo;
131   struct jpeg_error_mgr jerr;
132   int t;
133
134   fi = _fi;
135
136   memset(&cinfo, 0, sizeof(cinfo));
137   memset(&jerr, 0, sizeof(jerr));
138   memset(&mgr, 0, sizeof(mgr));
139   cinfo.err = jpeg_std_error(&jerr);
140   jpeg_create_compress(&cinfo);
141
142   mgr.init_destination = file_init_destination;
143   mgr.empty_output_buffer = file_empty_output_buffer;
144   mgr.term_destination = file_term_destination;
145   cinfo.dest = &mgr;
146
147   // init compression
148   
149   cinfo.image_width  = width;
150   cinfo.image_height = height;
151   cinfo.input_components = 3;
152   cinfo.in_color_space = JCS_RGB;
153   jpeg_set_defaults(&cinfo);
154   cinfo.dct_method = JDCT_IFAST;
155   jpeg_set_quality(&cinfo,quality,TRUE);
156
157   //jpeg_write_tables(&cinfo);
158   //jpeg_suppress_tables(&cinfo, TRUE);
159   jpeg_start_compress(&cinfo, FALSE);
160   
161   for(t=0;t<height;t++) {
162     unsigned char*data2 = &data[width*3*t];
163     jpeg_write_scanlines(&cinfo, &data2, 1);
164   }
165   jpeg_finish_compress(&cinfo);
166   jpeg_destroy_compress(&cinfo);
167   return 1;
168 }
169
170 int jpeg_save_to_mem(unsigned char*data, int width, int height, int quality, unsigned char*_dest, int _destlen)
171 {
172   struct jpeg_destination_mgr mgr;
173   struct jpeg_compress_struct cinfo;
174   struct jpeg_error_mgr jerr;
175   int t;
176
177   memset(&cinfo, 0, sizeof(cinfo));
178   memset(&jerr, 0, sizeof(jerr));
179   memset(&mgr, 0, sizeof(mgr));
180   cinfo.err = jpeg_std_error(&jerr);
181   jpeg_create_compress(&cinfo);
182
183   dest = _dest;
184   len = 0;
185   destlen = _destlen;
186
187   mgr.init_destination = mem_init_destination;
188   mgr.empty_output_buffer = mem_empty_output_buffer;
189   mgr.term_destination = mem_term_destination;
190   cinfo.dest = &mgr;
191
192   // init compression
193   
194   cinfo.image_width  = width;
195   cinfo.image_height = height;
196   cinfo.input_components = 3;
197   cinfo.in_color_space = JCS_RGB;
198   jpeg_set_defaults(&cinfo);
199   cinfo.dct_method = JDCT_IFAST;
200   jpeg_set_quality(&cinfo,quality,TRUE);
201
202   jpeg_start_compress(&cinfo, FALSE);
203   for(t=0;t<height;t++) {
204     unsigned char*data2 = &data[width*3*t];
205     jpeg_write_scanlines(&cinfo, &data2, 1);
206   }
207   jpeg_finish_compress(&cinfo);
208   jpeg_destroy_compress(&cinfo);
209   return len;
210 }
211
212 void mem_init_source (j_decompress_ptr cinfo)
213 {
214     struct jpeg_source_mgr* mgr = cinfo->src;
215     mgr->next_input_byte = data;
216     mgr->bytes_in_buffer = size;
217     //printf("init %d\n", size - mgr->bytes_in_buffer);
218 }
219
220 boolean mem_fill_input_buffer (j_decompress_ptr cinfo)
221 {
222     struct jpeg_source_mgr* mgr = cinfo->src;
223     printf("fill %d\n", size - mgr->bytes_in_buffer);
224     return 0;
225 }
226
227 void mem_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
228 {
229     struct jpeg_source_mgr* mgr = cinfo->src;
230     printf("skip %d +%d\n", size - mgr->bytes_in_buffer, num_bytes);
231     if(num_bytes<=0)
232         return;
233     mgr->next_input_byte += num_bytes;
234     mgr->bytes_in_buffer -= num_bytes;
235 }
236
237 boolean mem_resync_to_restart (j_decompress_ptr cinfo, int desired)
238 {
239     struct jpeg_source_mgr* mgr = cinfo->src;
240     printf("resync %d\n", size - mgr->bytes_in_buffer);
241     mgr->next_input_byte = data;
242     mgr->bytes_in_buffer = size;
243     return 1;
244 }
245
246 void mem_term_source (j_decompress_ptr cinfo)
247 {
248     struct jpeg_source_mgr* mgr = cinfo->src;
249     //printf("term %d\n", size - mgr->bytes_in_buffer);
250 }
251
252 int jpeg_load_from_mem(unsigned char*_data, int _size, unsigned char*dest, int width, int height)
253 {
254     struct jpeg_decompress_struct cinfo;
255     struct jpeg_error_mgr jerr;
256     struct jpeg_source_mgr mgr;
257     int y,x;
258
259     data = _data;
260     size = _size;
261
262     jpeg_create_decompress(&cinfo); 
263
264     mgr.next_input_byte = data;
265     mgr.bytes_in_buffer = size;
266     mgr.init_source        =mem_init_source ;
267     mgr.fill_input_buffer  =mem_fill_input_buffer ;
268     mgr.skip_input_data    =mem_skip_input_data ;
269     mgr.resync_to_restart  =mem_resync_to_restart ;
270     mgr.term_source        =mem_term_source ;
271
272     cinfo.err = jpeg_std_error(&jerr);
273     cinfo.src = &mgr;
274
275     jpeg_read_header(&cinfo, TRUE);
276     jpeg_start_decompress(&cinfo);
277
278     for(y=0;y<height;y++) {
279         unsigned char*j = &dest[width*y*3];
280         jpeg_read_scanlines(&cinfo,&j,1);
281     }
282
283     jpeg_finish_decompress(&cinfo);
284     jpeg_destroy_decompress(&cinfo);
285     return 1;
286 }
287
288 typedef struct _RGBA {
289     unsigned char a,r,g,b;
290 } RGBA;
291
292 typedef unsigned char U8;
293
294 int jpeg_load(const char*filename, unsigned char**dest, int*_width, int*_height)
295 {
296     struct jpeg_decompress_struct cinfo;
297     struct jpeg_error_mgr jerr;
298     struct jpeg_source_mgr mgr;
299
300     FILE*fi = fopen(filename, "rb");
301     if(!fi) {
302         fprintf(stderr, "Couldn't open file %s\n", filename);
303         return 0;
304     }
305
306     cinfo.err = jpeg_std_error(&jerr);
307     jpeg_create_decompress(&cinfo); 
308     jpeg_stdio_src(&cinfo, fi);
309     jpeg_read_header(&cinfo, TRUE);
310     jpeg_start_decompress(&cinfo);
311     
312     U8*scanline = (U8 *)malloc(4 * cinfo.output_width);
313
314     int width = *_width = cinfo.output_width;
315     int height = *_height = cinfo.output_height;
316     *dest = (unsigned char*)malloc(width*height*4);
317
318     int y;
319     for (y=0;y<height;y++) {
320         int x;
321         U8 *js = scanline;
322         RGBA*line = &((RGBA*)(*dest))[y*width];
323
324         jpeg_read_scanlines(&cinfo, &js, 1);
325         if (cinfo.out_color_space == JCS_GRAYSCALE) {
326             for (x = 0; x < width; x++) {
327                 line[x].a = 255;
328                 line[x].r = line[x].g = line[x].b = js[x];
329             }
330         } else if (cinfo.out_color_space == JCS_RGB) {
331             for (x = width - 1; x >= 0; x--) {
332                 line[x].a = 255;
333                 line[x].r = js[x*3+0];
334                 line[x].g = js[x*3+1];
335                 line[x].b = js[x*3+2];
336             }
337         } else if (cinfo.out_color_space == JCS_YCCK) {
338             fprintf(stderr, "Error: Can't convert YCCK to RGB.\n");
339             return 0;
340         } else if (cinfo.out_color_space == JCS_YCbCr) {
341             for (x = 0; x < width; x++) {
342                 int y = js[x * 3 + 0];
343                 int u = js[x * 3 + 1];
344                 int v = js[x * 3 + 1];
345                 line[x].a = 255;
346                 line[x].r = y + ((360 * (v - 128)) >> 8);
347                 line[x].g = y - ((88 * (u - 128) + 183 * (v - 128)) >> 8);
348                 line[x].b = y + ((455 * (u - 128)) >> 8);
349             }
350         } else if (cinfo.out_color_space == JCS_CMYK) {
351             for (x = 0; x < width; x++) {
352                 int white = 255 - js[x * 4 + 3];
353                 line[x].a = 255;
354                 line[x].r = white - ((js[x * 4] * white) >> 8);
355                 line[x].g = white - ((js[x * 4 + 1] * white) >> 8);
356                 line[x].b = white - ((js[x * 4 + 2] * white) >> 8);
357             }
358         }
359     }
360
361     free(scanline);
362
363     jpeg_finish_decompress(&cinfo);
364     jpeg_destroy_decompress(&cinfo);
365     return 1;
366 }
367
368 #else
369
370 int jpeg_save(unsigned char*data, int width, int height, int quality, const char*filename)
371 {
372     fprintf(stderr, "jpeg_save: No JPEG support compiled in\n");
373     return 0;
374 }
375 int jpeg_save_to_file(unsigned char*data, int width, int height, int quality, FILE*fi)
376 {
377     fprintf(stderr, "jpeg_save_to_file: No JPEG support compiled in\n");
378     return 0;
379 }
380 int jpeg_save_to_mem(unsigned char*data, int width, int height, int quality, unsigned char*dest, int destsize)
381 {
382     fprintf(stderr, "jpeg_save_tomem: No JPEG support compiled in\n");
383     return 0;
384 }
385 int jpeg_load_from_mem(unsigned char*_data, int size, unsigned char*dest, int width, int height)
386 {
387     fprintf(stderr, "jpeg_load_from_mem: No JPEG support compiled in\n");
388     return 0;
389 }
390 int jpeg_load(const char*filename, unsigned char**dest, int*_width, int*_height)
391 {
392     fprintf(stderr, "jpeg_load: No JPEG support compiled in\n");
393     return 0;
394 }
395
396 #endif