3 Bitmap functions (needs libjpeg)
5 Extension module for the rfxswf library.
6 Part of the swftools package.
8 Copyright (c) 2000, 2001 Rainer Böhme <rfxswf@reflex-studio.de>
10 This file is distributed under the GPL, see file COPYING for details
14 #ifdef _JPEGLIB_INCLUDED_
15 #define OUTBUFFER_SIZE 0x8000
17 typedef struct _JPEGDESTMGR
18 { struct jpeg_destination_mgr mgr;
21 struct jpeg_compress_struct cinfo;
22 struct jpeg_error_mgr jerr;
23 } JPEGDESTMGR, * LPJPEGDESTMGR;
25 // Destination manager callbacks
27 void swf_init_destination(j_compress_ptr cinfo)
28 { LPJPEGDESTMGR dmgr = (LPJPEGDESTMGR)cinfo->dest;
29 dmgr->buffer = (JOCTET*)malloc(OUTBUFFER_SIZE);
30 dmgr->mgr.next_output_byte = dmgr->buffer;
31 dmgr->mgr.free_in_buffer = OUTBUFFER_SIZE;
34 boolean swf_empty_output_buffer(j_compress_ptr cinfo)
35 { LPJPEGDESTMGR dmgr = (LPJPEGDESTMGR)cinfo->dest;
36 SetBlock(dmgr->t,(U8*)dmgr->buffer,OUTBUFFER_SIZE);
37 dmgr->mgr.next_output_byte = dmgr->buffer;
38 dmgr->mgr.free_in_buffer = OUTBUFFER_SIZE;
42 void swf_term_destination(j_compress_ptr cinfo)
43 { LPJPEGDESTMGR dmgr = (LPJPEGDESTMGR)cinfo->dest;
44 SetBlock(dmgr->t,(U8*)dmgr->buffer,OUTBUFFER_SIZE-dmgr->mgr.free_in_buffer);
46 dmgr->mgr.free_in_buffer = 0;
49 LPJPEGBITS SetJPEGBitsStart(LPTAG t,int width,int height,int quality)
53 // redirect compression lib output to local SWF Tag structure
55 jpeg = (LPJPEGDESTMGR)malloc(sizeof(JPEGDESTMGR));
56 if (!jpeg) return NULL;
58 memset(jpeg,0x00,sizeof(JPEGDESTMGR));
59 jpeg->cinfo.err = jpeg_std_error(&jpeg->jerr);
61 jpeg_create_compress(&jpeg->cinfo);
63 jpeg->mgr.init_destination = swf_init_destination;
64 jpeg->mgr.empty_output_buffer = swf_empty_output_buffer;
65 jpeg->mgr.term_destination = swf_term_destination;
69 jpeg->cinfo.dest = (struct jpeg_destination_mgr *)jpeg;
73 jpeg->cinfo.image_width = width;
74 jpeg->cinfo.image_height = height;
75 jpeg->cinfo.input_components = 3;
76 jpeg->cinfo.in_color_space = JCS_RGB;
78 jpeg_set_defaults(&jpeg->cinfo);
79 jpeg_set_quality(&jpeg->cinfo,quality,TRUE);
81 // write tables to SWF
83 jpeg_write_tables(&jpeg->cinfo);
85 // compess image to SWF
87 jpeg_suppress_tables(&jpeg->cinfo, TRUE);
88 jpeg_start_compress(&jpeg->cinfo, FALSE);
90 return (LPJPEGBITS)jpeg;
93 int SetJPEGBitsLines(LPJPEGBITS jpegbits,U8 ** data,int n)
94 { LPJPEGDESTMGR jpeg = (LPJPEGDESTMGR)jpegbits;
96 jpeg_write_scanlines(&jpeg->cinfo,data,n);
100 int SetJPEGBitsLine(LPJPEGBITS jpegbits,U8 * data)
101 { return SetJPEGBitsLines(jpegbits,&data,1);
104 int SetJPEGBitsFinish(LPJPEGBITS jpegbits)
105 { LPJPEGDESTMGR jpeg = (LPJPEGDESTMGR)jpegbits;
106 if (!jpeg) return -1;
107 jpeg_finish_compress(&jpeg->cinfo);
112 int SetJPEGBits(LPTAG t,char * fname,int quality)
113 { struct jpeg_decompress_struct cinfo;
114 struct jpeg_error_mgr jerr;
119 cinfo.err = jpeg_std_error(&jerr);
120 jpeg_create_decompress(&cinfo);
122 if ((f=fopen(fname,"rb"))==NULL) return -1;
125 jpeg_stdio_src(&cinfo,f);
126 jpeg_read_header(&cinfo, TRUE);
127 jpeg_start_decompress(&cinfo);
129 out = SetJPEGBitsStart(t,cinfo.output_width,cinfo.output_height,quality);
130 scanline = (U8*)malloc(4*cinfo.output_width);
135 for (y=0;y<cinfo.output_height;y++)
136 { jpeg_read_scanlines(&cinfo,&js,1);
137 SetJPEGBitsLines(out,(U8**)&js,1);
141 SetJPEGBitsFinish(out);
142 jpeg_finish_decompress(&cinfo);
148 #undef OUTBUFFER_SIZE
151 // insert zlib/PNG functions here