3 JPEG to SWF converter tool
\r
5 Part of the swftools package.
\r
7 Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
\r
9 This file is distributed under the GPL, see file COPYING for details
\r
16 #include <jpeglib.h>
\r
17 #include "../lib/rfxswf.h"
\r
18 #include "../lib/args.h" // not really a header ;-)
\r
20 #define MAX_INPUT_FILES 1024
\r
21 #define VERBOSE(x) (global.verbose>=x)
\r
26 int max_image_width;
\r
27 int max_image_height;
\r
32 char * files[MAX_INPUT_FILES];
\r
36 TAG * MovieStart(SWF * swf,int framerate,int dx,int dy)
\r
40 memset(swf,0x00,sizeof(SWF));
\r
42 swf->FileVersion = 4;
\r
43 swf->FrameRate = (25600/framerate);
\r
44 swf->MovieSize.xmax = dx*20;
\r
45 swf->MovieSize.ymax = dy*20;
\r
47 t = swf->FirstTag = InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
\r
49 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
\r
55 int MovieFinish(SWF * swf,TAG * t,char * sname)
\r
56 { int handle, so = fileno(stdout);
\r
57 t = InsertTag(t,ST_END);
\r
59 if ((!isatty(so))&&(!sname)) handle = so;
\r
61 { if (!sname) sname = "out.swf";
\r
62 handle = open(sname,O_RDWR|O_CREAT|O_TRUNC,0666);
\r
64 if FAILED(WriteSWF(handle,swf)) if (VERBOSE(1)) fprintf(stderr,"Unable to write output file: %s\n",sname);
\r
65 if (handle!=so) close(handle);
\r
71 TAG * MovieAddFrame(SWF * swf,TAG * t,char * sname,int quality,int id)
\r
77 struct jpeg_decompress_struct cinfo;
\r
78 struct jpeg_error_mgr jerr;
\r
83 if ((f=fopen(sname,"rb"))==NULL)
\r
84 { if (VERBOSE(1)) fprintf(stderr,"Read access failed: %s\n",sname);
\r
88 cinfo.err = jpeg_std_error(&jerr);
\r
89 jpeg_create_decompress(&cinfo);
\r
90 jpeg_stdio_src(&cinfo,f);
\r
91 jpeg_read_header(&cinfo, TRUE);
\r
92 jpeg_start_decompress(&cinfo);
\r
94 t = InsertTag(t,ST_DEFINEBITSJPEG2);
\r
98 out = SetJPEGBitsStart(t,cinfo.output_width,cinfo.output_height,quality);
\r
99 scanline = (U8*)malloc(4*cinfo.output_width);
\r
103 U8 * js = scanline;
\r
104 for (y=0;y<cinfo.output_height;y++)
\r
105 { jpeg_read_scanlines(&cinfo,&js,1);
\r
106 SetJPEGBitsLines(out,(U8**)&js,1);
\r
111 SetJPEGBitsFinish(out);
\r
113 t = InsertTag(t,ST_DEFINESHAPE);
\r
116 GetMatrix(NULL,&m);
\r
119 fs = ShapeAddBitmapFillStyle(s,&m,id,0);
\r
121 SetU16(t,id+1); // id
\r
124 r.xmin = r.ymin = 0;
\r
125 r.xmax = cinfo.output_width*20;
\r
126 r.ymax = cinfo.output_height*20;
\r
129 SetShapeHeader(t,s);
\r
131 ShapeSetAll(t,s,0,0,0,fs,0);
\r
132 ShapeSetLine(t,s,r.xmax,0);
\r
133 ShapeSetLine(t,s,0,r.ymax);
\r
134 ShapeSetLine(t,s,-r.xmax,0);
\r
135 ShapeSetLine(t,s,0,-r.ymax);
\r
139 t = InsertTag(t,ST_REMOVEOBJECT2);
\r
140 SetU16(t,1); // depth
\r
142 t = InsertTag(t,ST_PLACEOBJECT2);
\r
144 GetMatrix(NULL,&m);
\r
145 m.tx = (swf->MovieSize.xmax-(int)cinfo.output_width*20)/2;
\r
146 m.ty = (swf->MovieSize.ymax-(int)cinfo.output_height*20)/2;
\r
147 ObjectPlace(t,id+1,1,&m,NULL,NULL);
\r
149 t = InsertTag(t,ST_SHOWFRAME);
\r
151 jpeg_finish_decompress(&cinfo);
\r
157 int CheckInputFile(char * fname,char ** realname)
\r
158 { struct jpeg_decompress_struct cinfo;
\r
159 struct jpeg_error_mgr jerr;
\r
161 char * s = malloc(strlen(fname)+5);
\r
167 // Check whether file exists (with typical extensions)
\r
169 if ((f=fopen(s,"rb"))==NULL)
\r
170 { sprintf(s,"%s.jpg",fname);
\r
171 if ((f=fopen(s,"rb"))==NULL)
\r
172 { sprintf(s,"%s.jpeg",fname);
\r
173 if ((f=fopen(s,"rb"))==NULL)
\r
174 { sprintf(s,"%s.JPG",fname);
\r
175 if ((f=fopen(s,"rb"))==NULL)
\r
176 { sprintf(s,"%s.JPEG",fname);
\r
177 if ((f=fopen(s,"rb"))==NULL)
\r
184 cinfo.err = jpeg_std_error(&jerr);
\r
185 jpeg_create_decompress(&cinfo);
\r
186 jpeg_stdio_src(&cinfo,f);
\r
187 jpeg_read_header(&cinfo, TRUE);
\r
189 // Get image dimensions
\r
191 if (global.max_image_width<cinfo.image_width) global.max_image_width = cinfo.image_width;
\r
192 if (global.max_image_height<cinfo.image_height) global.max_image_height = cinfo.image_height;
\r
194 jpeg_destroy_decompress(&cinfo);
\r
200 int args_callback_option(char*arg,char*val)
\r
202 if (arg[1]) res = -1;
\r
203 else switch (arg[0])
\r
205 if (val) global.quality = atoi(val);
\r
206 if ((global.quality<1)||(global.quality>100))
\r
207 { if (VERBOSE(1)) fprintf(stderr,"Error: You must specify a valid quality between 1 and 100.\n");
\r
214 if (val) global.framerate = atoi(val);
\r
215 if ((global.framerate<1)||(global.framerate>5000))
\r
216 { if (VERBOSE(1)) fprintf(stderr,"Error: You must specify a valid framerate between 1 and 10000.\n");
\r
223 if (val) global.outfile = val; res = 1; break;
\r
226 if (val) global.verbose = atoi(val); res = 1; break;
\r
229 if (val) global.force_width = atoi(val); res = 1; break;
\r
232 if (val) global.force_height = atoi(val); res = 1; break;
\r
235 printf("jpeg2swf - part of %s %s\n", PACKAGE, VERSION);exit(0);
\r
243 { if (VERBOSE(1)) fprintf(stderr,"Unknown option: -v%s\n",arg);
\r
249 struct options_t options[] =
\r
259 int args_callback_longoption(char*name,char*val) {
\r
260 return args_long2shortoption(options, name, val);
\r
263 int args_callback_command(char*arg,char*next) // actually used as filename
\r
265 if (CheckInputFile(arg,&s)<0)
\r
266 { if (VERBOSE(1)) fprintf(stderr, "Unable to open input file: %s\n",arg);
\r
270 { global.files[global.nfiles] = s;
\r
272 if (global.nfiles>=MAX_INPUT_FILES)
\r
273 { if (VERBOSE(1)) fprintf(stderr, "Error: Too many input files.\n");
\r
280 void args_callback_usage(char*name)
\r
281 { fprintf(stderr,"Usage: %s imagefiles[.jpg]|[.jpeg] [...] [-options [value]]\n",name);
\r
282 fprintf(stderr,"-q quality (quality) Set JPEG compression quality (1-100)\n");
\r
283 fprintf(stderr,"-r framerate (rate) Set movie framerate (100/sec)\n");
\r
284 fprintf(stderr,"-o outputfile (output) Set name for SWF output file\n");
\r
285 fprintf(stderr,"-v level (verbose) Set verbose level (0=quiet, 1=default, 2=debug)\n");
\r
286 fprintf(stderr,"-X pixel (width) Force movie width to scale (default: autodetect)\n");
\r
287 fprintf(stderr,"-Y pixel (height) Force movie height to scale (default: autodetect)\n");
\r
288 fprintf(stderr,"-V (version) Print version information and exit\n");
\r
292 int main(int argc, char ** argv)
\r
296 memset(&global,0x00,sizeof(global));
\r
298 global.quality = 60;
\r
299 global.framerate = 100;
\r
300 global.verbose = 1;
\r
302 processargs(argc, argv);
\r
304 if (VERBOSE(2)) fprintf(stderr,"Processing %i file(s)...\n",global.nfiles);
\r
306 t = MovieStart(&swf,global.framerate,
\r
307 global.force_width?global.force_width:global.max_image_width,
\r
308 global.force_height?global.force_height:global.max_image_height);
\r
311 for (i=0;i<global.nfiles;i++)
\r
312 { if (VERBOSE(3)) fprintf(stderr,"[%03i] %s\n",i,global.files[i]);
\r
313 t = MovieAddFrame(&swf,t,global.files[i],global.quality,(i*2)+1);
\r
314 free(global.files[i]);
\r
318 MovieFinish(&swf,t,global.outfile);
\r
324 // Old main routine
\r
327 int ConvertJPEG2SWF(char * sname,char * dname,int quality)
\r
337 struct jpeg_decompress_struct cinfo;
\r
338 struct jpeg_error_mgr jerr;
\r
345 cinfo.err = jpeg_std_error(&jerr);
\r
346 jpeg_create_decompress(&cinfo);
\r
348 if ((f=fopen(sname,"rb"))==NULL)
\r
349 { fprintf(stderr,"Read access failed: %s\n",sname);
\r
353 jpeg_stdio_src(&cinfo,f);
\r
354 jpeg_read_header(&cinfo, TRUE);
\r
355 jpeg_start_decompress(&cinfo);
\r
357 memset(&swf,0x00,sizeof(SWF));
\r
359 swf.FileVersion = 4;
\r
360 swf.FrameRate = 0x1000;
\r
361 swf.MovieSize.xmax = cinfo.output_width*20;
\r
362 swf.MovieSize.ymax = cinfo.output_height*20;
\r
364 printf("dx = %i, dy = %i\n",cinfo.output_width,cinfo.output_height);
\r
366 t = swf.FirstTag = InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
\r
368 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
\r
371 t = InsertTag(t,ST_DEFINEBITSJPEG2);
\r
375 out = SetJPEGBitsStart(t,cinfo.output_width,cinfo.output_height,quality);
\r
376 scanline = (U8*)malloc(4*cinfo.output_width);
\r
380 U8 * js = scanline;
\r
381 for (y=0;y<cinfo.output_height;y++)
\r
382 { jpeg_read_scanlines(&cinfo,&js,1);
\r
383 SetJPEGBitsLines(out,(U8**)&js,1);
\r
388 SetJPEGBitsFinish(out);
\r
390 printf("JPEG Tag-Length: %06x\n",GetDataSize(t));
\r
392 t = InsertTag(t,ST_DEFINESHAPE);
\r
395 GetMatrix(NULL,&m);
\r
399 fs = ShapeAddBitmapFillStyle(s,&m,1,0);
\r
400 // fs = ShapeAddSolidFillStyle(s,&rgb);
\r
403 SetRect(t,&swf.MovieSize);
\r
404 SetShapeHeader(t,s);
\r
406 ShapeSetAll(t,s,0,0,0,fs,0);
\r
407 ShapeSetLine(t,s,swf.MovieSize.xmax,0);
\r
408 ShapeSetLine(t,s,0,swf.MovieSize.ymax);
\r
409 ShapeSetLine(t,s,-swf.MovieSize.xmax,0);
\r
410 ShapeSetLine(t,s,0,-swf.MovieSize.ymax);
\r
414 t = InsertTag(t,ST_PLACEOBJECT2);
\r
416 ObjectPlace(t,2,1,NULL,NULL,NULL);
\r
418 t = InsertTag(t,ST_SHOWFRAME);
\r
420 t = InsertTag(t,ST_END);
\r
422 jpeg_finish_decompress(&cinfo);
\r
425 handle = open(dname,O_RDWR|O_CREAT|O_TRUNC,0666);
\r
426 if FAILED(WriteSWF(handle,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
\r