seed random from ruby interface
[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_gray(unsigned char*data, int width, int height, int quality, const char*filename)
128 {
129   struct jpeg_destination_mgr mgr;
130   struct jpeg_compress_struct cinfo;
131   struct jpeg_error_mgr jerr;
132
133   if(filename) fi = fopen(filename, "wb");
134   else         fi = 0;
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   cinfo.image_width  = width;
147   cinfo.image_height = height;
148   cinfo.input_components = 1;
149   cinfo.in_color_space = JCS_GRAYSCALE;
150   jpeg_set_defaults(&cinfo);
151   jpeg_set_quality(&cinfo,quality,TRUE);
152   jpeg_start_compress(&cinfo, FALSE);
153   int t;
154   for(t=0;t<height;t++) {
155     unsigned char*data2 = &data[width*t];
156     jpeg_write_scanlines(&cinfo, &data2, 1);
157   }
158   jpeg_finish_compress(&cinfo);
159
160   if(fi) fclose(fi);
161   jpeg_destroy_compress(&cinfo);
162   return 1;
163 }
164
165
166 int jpeg_save_to_file(unsigned char*data, int width, int height, int quality, FILE*_fi)
167 {
168   struct jpeg_destination_mgr mgr;
169   struct jpeg_compress_struct cinfo;
170   struct jpeg_error_mgr jerr;
171   int t;
172
173   fi = _fi;
174
175   memset(&cinfo, 0, sizeof(cinfo));
176   memset(&jerr, 0, sizeof(jerr));
177   memset(&mgr, 0, sizeof(mgr));
178   cinfo.err = jpeg_std_error(&jerr);
179   jpeg_create_compress(&cinfo);
180
181   mgr.init_destination = file_init_destination;
182   mgr.empty_output_buffer = file_empty_output_buffer;
183   mgr.term_destination = file_term_destination;
184   cinfo.dest = &mgr;
185
186   // init compression
187   
188   cinfo.image_width  = width;
189   cinfo.image_height = height;
190   cinfo.input_components = 3;
191   cinfo.in_color_space = JCS_RGB;
192   jpeg_set_defaults(&cinfo);
193   cinfo.dct_method = JDCT_IFAST;
194   jpeg_set_quality(&cinfo,quality,TRUE);
195
196   //jpeg_write_tables(&cinfo);
197   //jpeg_suppress_tables(&cinfo, TRUE);
198   jpeg_start_compress(&cinfo, FALSE);
199   
200   for(t=0;t<height;t++) {
201     unsigned char*data2 = &data[width*3*t];
202     jpeg_write_scanlines(&cinfo, &data2, 1);
203   }
204   jpeg_finish_compress(&cinfo);
205   jpeg_destroy_compress(&cinfo);
206   return 1;
207 }
208
209 int jpeg_save_to_mem(unsigned char*data, int width, int height, int quality, unsigned char*_dest, int _destlen)
210 {
211   struct jpeg_destination_mgr mgr;
212   struct jpeg_compress_struct cinfo;
213   struct jpeg_error_mgr jerr;
214   int t;
215
216   memset(&cinfo, 0, sizeof(cinfo));
217   memset(&jerr, 0, sizeof(jerr));
218   memset(&mgr, 0, sizeof(mgr));
219   cinfo.err = jpeg_std_error(&jerr);
220   jpeg_create_compress(&cinfo);
221
222   dest = _dest;
223   len = 0;
224   destlen = _destlen;
225
226   mgr.init_destination = mem_init_destination;
227   mgr.empty_output_buffer = mem_empty_output_buffer;
228   mgr.term_destination = mem_term_destination;
229   cinfo.dest = &mgr;
230
231   // init compression
232   
233   cinfo.image_width  = width;
234   cinfo.image_height = height;
235   cinfo.input_components = 3;
236   cinfo.in_color_space = JCS_RGB;
237   jpeg_set_defaults(&cinfo);
238   cinfo.dct_method = JDCT_IFAST;
239   jpeg_set_quality(&cinfo,quality,TRUE);
240
241   jpeg_start_compress(&cinfo, FALSE);
242   for(t=0;t<height;t++) {
243     unsigned char*data2 = &data[width*3*t];
244     jpeg_write_scanlines(&cinfo, &data2, 1);
245   }
246   jpeg_finish_compress(&cinfo);
247   jpeg_destroy_compress(&cinfo);
248   return len;
249 }
250
251 void mem_init_source (j_decompress_ptr cinfo)
252 {
253     struct jpeg_source_mgr* mgr = cinfo->src;
254     mgr->next_input_byte = data;
255     mgr->bytes_in_buffer = size;
256     //printf("init %d\n", size - mgr->bytes_in_buffer);
257 }
258
259 boolean mem_fill_input_buffer (j_decompress_ptr cinfo)
260 {
261     struct jpeg_source_mgr* mgr = cinfo->src;
262     printf("fill %d\n", size - mgr->bytes_in_buffer);
263     return 0;
264 }
265
266 void mem_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
267 {
268     struct jpeg_source_mgr* mgr = cinfo->src;
269     printf("skip %d +%ld\n", size - mgr->bytes_in_buffer, num_bytes);
270     if(num_bytes<=0)
271         return;
272     mgr->next_input_byte += num_bytes;
273     mgr->bytes_in_buffer -= num_bytes;
274 }
275
276 boolean mem_resync_to_restart (j_decompress_ptr cinfo, int desired)
277 {
278     struct jpeg_source_mgr* mgr = cinfo->src;
279     printf("resync %d\n", size - mgr->bytes_in_buffer);
280     mgr->next_input_byte = data;
281     mgr->bytes_in_buffer = size;
282     return 1;
283 }
284
285 void mem_term_source (j_decompress_ptr cinfo)
286 {
287     struct jpeg_source_mgr* mgr = cinfo->src;
288     //printf("term %d\n", size - mgr->bytes_in_buffer);
289 }
290
291 int jpeg_load_from_mem(unsigned char*_data, int _size, unsigned char*dest, int width, int height)
292 {
293     struct jpeg_decompress_struct cinfo;
294     struct jpeg_error_mgr jerr;
295     struct jpeg_source_mgr mgr;
296     int y,x;
297
298     data = _data;
299     size = _size;
300
301     jpeg_create_decompress(&cinfo); 
302
303     mgr.next_input_byte = data;
304     mgr.bytes_in_buffer = size;
305     mgr.init_source        =mem_init_source ;
306     mgr.fill_input_buffer  =mem_fill_input_buffer ;
307     mgr.skip_input_data    =mem_skip_input_data ;
308     mgr.resync_to_restart  =mem_resync_to_restart ;
309     mgr.term_source        =mem_term_source ;
310
311     cinfo.err = jpeg_std_error(&jerr);
312     cinfo.src = &mgr;
313
314     jpeg_read_header(&cinfo, TRUE);
315     jpeg_start_decompress(&cinfo);
316
317     for(y=0;y<height;y++) {
318         unsigned char*j = &dest[width*y*3];
319         jpeg_read_scanlines(&cinfo,&j,1);
320     }
321
322     jpeg_finish_decompress(&cinfo);
323     jpeg_destroy_decompress(&cinfo);
324     return 1;
325 }
326
327 typedef struct _RGBA {
328     unsigned char a,r,g,b;
329 } RGBA;
330
331 typedef unsigned char U8;
332
333 int jpeg_load(const char*filename, unsigned char**dest, int*_width, int*_height)
334 {
335     struct jpeg_decompress_struct cinfo;
336     struct jpeg_error_mgr jerr;
337     struct jpeg_source_mgr mgr;
338
339     FILE*fi = fopen(filename, "rb");
340     if(!fi) {
341         fprintf(stderr, "Couldn't open file %s\n", filename);
342         return 0;
343     }
344
345     cinfo.err = jpeg_std_error(&jerr);
346     jpeg_create_decompress(&cinfo); 
347     jpeg_stdio_src(&cinfo, fi);
348     jpeg_read_header(&cinfo, TRUE);
349     jpeg_start_decompress(&cinfo);
350     
351     U8*scanline = (U8 *)malloc(4 * cinfo.output_width);
352
353     int width = *_width = cinfo.output_width;
354     int height = *_height = cinfo.output_height;
355     *dest = (unsigned char*)malloc(width*height*4);
356
357     int y;
358     for (y=0;y<height;y++) {
359         int x;
360         U8 *js = scanline;
361         RGBA*line = &((RGBA*)(*dest))[y*width];
362
363         jpeg_read_scanlines(&cinfo, &js, 1);
364         if (cinfo.out_color_space == JCS_GRAYSCALE) {
365             for (x = 0; x < width; x++) {
366                 line[x].a = 255;
367                 line[x].r = line[x].g = line[x].b = js[x];
368             }
369         } else if (cinfo.out_color_space == JCS_RGB) {
370             for (x = width - 1; x >= 0; x--) {
371                 line[x].a = 255;
372                 line[x].r = js[x*3+0];
373                 line[x].g = js[x*3+1];
374                 line[x].b = js[x*3+2];
375             }
376         } else if (cinfo.out_color_space == JCS_YCCK) {
377             fprintf(stderr, "Error: Can't convert YCCK to RGB.\n");
378             return 0;
379         } else if (cinfo.out_color_space == JCS_YCbCr) {
380             for (x = 0; x < width; x++) {
381                 int y = js[x * 3 + 0];
382                 int u = js[x * 3 + 1];
383                 int v = js[x * 3 + 1];
384                 line[x].a = 255;
385                 line[x].r = y + ((360 * (v - 128)) >> 8);
386                 line[x].g = y - ((88 * (u - 128) + 183 * (v - 128)) >> 8);
387                 line[x].b = y + ((455 * (u - 128)) >> 8);
388             }
389         } else if (cinfo.out_color_space == JCS_CMYK) {
390             for (x = 0; x < width; x++) {
391                 int white = 255 - js[x * 4 + 3];
392                 line[x].a = 255;
393                 line[x].r = white - ((js[x * 4] * white) >> 8);
394                 line[x].g = white - ((js[x * 4 + 1] * white) >> 8);
395                 line[x].b = white - ((js[x * 4 + 2] * white) >> 8);
396             }
397         }
398     }
399
400     free(scanline);
401
402     jpeg_finish_decompress(&cinfo);
403     jpeg_destroy_decompress(&cinfo);
404     fclose(fi);
405     return 1;
406 }
407
408 #else
409
410 int jpeg_save(unsigned char*data, int width, int height, int quality, const char*filename)
411 {
412     fprintf(stderr, "jpeg_save: No JPEG support compiled in\n");
413     return 0;
414 }
415 int jpeg_save_to_file(unsigned char*data, int width, int height, int quality, FILE*fi)
416 {
417     fprintf(stderr, "jpeg_save_to_file: No JPEG support compiled in\n");
418     return 0;
419 }
420 int jpeg_save_to_mem(unsigned char*data, int width, int height, int quality, unsigned char*dest, int destsize)
421 {
422     fprintf(stderr, "jpeg_save_tomem: No JPEG support compiled in\n");
423     return 0;
424 }
425 int jpeg_load_from_mem(unsigned char*_data, int size, unsigned char*dest, int width, int height)
426 {
427     fprintf(stderr, "jpeg_load_from_mem: No JPEG support compiled in\n");
428     return 0;
429 }
430 int jpeg_load(const char*filename, unsigned char**dest, int*_width, int*_height)
431 {
432     fprintf(stderr, "jpeg_load: No JPEG support compiled in\n");
433     return 0;
434 }
435
436 #endif