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
19 #include "args.h" // not really a header ;-)
\r
21 #define MAX_INPUT_FILES 1024
\r
22 #define VERBOSE(x) (global.verbose>=x)
\r
27 int max_image_width;
\r
28 int max_image_height;
\r
33 char * files[MAX_INPUT_FILES];
\r
37 TAG * MovieStart(SWF * swf,int framerate,int dx,int dy)
\r
41 memset(swf,0x00,sizeof(SWF));
\r
43 swf->FileVersion = 4;
\r
44 swf->FrameRate = (25600/framerate);
\r
45 swf->MovieSize.xmax = dx*20;
\r
46 swf->MovieSize.ymax = dy*20;
\r
48 t = swf->FirstTag = InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
\r
50 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
\r
56 int MovieFinish(SWF * swf,TAG * t,char * sname)
\r
57 { int handle, so = fileno(stdout);
\r
58 t = InsertTag(t,ST_END);
\r
60 if ((!isatty(so))&&(!sname)) handle = so;
\r
62 { if (!sname) sname = "out.swf";
\r
63 handle = open(sname,O_RDWR|O_CREAT|O_TRUNC,0666);
\r
65 if FAILED(WriteSWF(handle,swf)) if (VERBOSE(1)) fprintf(stderr,"Unable to write output file: %s\n",sname);
\r
66 if (handle!=so) close(handle);
\r
72 TAG * MovieAddFrame(SWF * swf,TAG * t,char * sname,int quality,int id)
\r
78 struct jpeg_decompress_struct cinfo;
\r
79 struct jpeg_error_mgr jerr;
\r
84 if ((f=fopen(sname,"rb"))==NULL)
\r
85 { if (VERBOSE(1)) fprintf(stderr,"Read access failed: %s\n",sname);
\r
89 cinfo.err = jpeg_std_error(&jerr);
\r
90 jpeg_create_decompress(&cinfo);
\r
91 jpeg_stdio_src(&cinfo,f);
\r
92 jpeg_read_header(&cinfo, TRUE);
\r
93 jpeg_start_decompress(&cinfo);
\r
95 t = InsertTag(t,ST_DEFINEBITSJPEG2);
\r
99 out = SetJPEGBitsStart(t,cinfo.output_width,cinfo.output_height,quality);
\r
100 scanline = (U8*)malloc(4*cinfo.output_width);
\r
104 U8 * js = scanline;
\r
105 for (y=0;y<cinfo.output_height;y++)
\r
106 { jpeg_read_scanlines(&cinfo,&js,1);
\r
107 SetJPEGBitsLines(out,(U8**)&js,1);
\r
112 SetJPEGBitsFinish(out);
\r
114 t = InsertTag(t,ST_DEFINESHAPE);
\r
117 GetMatrix(NULL,&m);
\r
120 fs = ShapeAddBitmapFillStyle(s,&m,id,0);
\r
122 SetU16(t,id+1); // id
\r
125 r.xmin = r.ymin = 0;
\r
126 r.xmax = cinfo.output_width*20;
\r
127 r.ymax = cinfo.output_height*20;
\r
130 SetShapeHeader(t,s);
\r
132 ShapeSetAll(t,s,0,0,0,fs,0);
\r
133 ShapeSetLine(t,s,r.xmax,0);
\r
134 ShapeSetLine(t,s,0,r.ymax);
\r
135 ShapeSetLine(t,s,-r.xmax,0);
\r
136 ShapeSetLine(t,s,0,-r.ymax);
\r
140 t = InsertTag(t,ST_REMOVEOBJECT2);
\r
141 SetU16(t,1); // depth
\r
143 t = InsertTag(t,ST_PLACEOBJECT2);
\r
145 GetMatrix(NULL,&m);
\r
146 m.tx = (swf->MovieSize.xmax-(int)cinfo.output_width*20)/2;
\r
147 m.ty = (swf->MovieSize.ymax-(int)cinfo.output_height*20)/2;
\r
148 ObjectPlace(t,id+1,1,&m,NULL,NULL);
\r
150 t = InsertTag(t,ST_SHOWFRAME);
\r
152 jpeg_finish_decompress(&cinfo);
\r
158 int CheckInputFile(char * fname,char ** realname)
\r
159 { struct jpeg_decompress_struct cinfo;
\r
160 struct jpeg_error_mgr jerr;
\r
162 char * s = malloc(strlen(fname)+5);
\r
168 // Check whether file exists (with typical extensions)
\r
170 if ((f=fopen(s,"rb"))==NULL)
\r
171 { sprintf(s,"%s.jpg",fname);
\r
172 if ((f=fopen(s,"rb"))==NULL)
\r
173 { sprintf(s,"%s.jpeg",fname);
\r
174 if ((f=fopen(s,"rb"))==NULL)
\r
175 { sprintf(s,"%s.JPG",fname);
\r
176 if ((f=fopen(s,"rb"))==NULL)
\r
177 { sprintf(s,"%s.JPEG",fname);
\r
178 if ((f=fopen(s,"rb"))==NULL)
\r
185 cinfo.err = jpeg_std_error(&jerr);
\r
186 jpeg_create_decompress(&cinfo);
\r
187 jpeg_stdio_src(&cinfo,f);
\r
188 jpeg_read_header(&cinfo, TRUE);
\r
190 // Get image dimensions
\r
192 if (global.max_image_width<cinfo.image_width) global.max_image_width = cinfo.image_width;
\r
193 if (global.max_image_height<cinfo.image_height) global.max_image_height = cinfo.image_height;
\r
195 jpeg_destroy_decompress(&cinfo);
\r
201 int args_callback_option(char*arg,char*val)
\r
203 if (arg[1]) res = -1;
\r
204 else switch (arg[0])
\r
206 if (val) global.quality = atoi(val);
\r
207 if ((global.quality<1)||(global.quality>100))
\r
208 { if (VERBOSE(1)) fprintf(stderr,"Error: You must specify a valid quality between 1 and 100.\n");
\r
215 if (val) global.framerate = atoi(val);
\r
216 if ((global.framerate<1)||(global.framerate>5000))
\r
217 { if (VERBOSE(1)) fprintf(stderr,"Error: You must specify a valid framerate between 1 and 10000.\n");
\r
224 if (val) global.outfile = val; res = 1; break;
\r
227 if (val) global.verbose = atoi(val); res = 1; break;
\r
230 if (val) global.force_width = atoi(val); res = 1; break;
\r
233 if (val) global.force_height = atoi(val); res = 1; break;
\r
236 printf("jpeg2swf - part of swftools 0.0.1\n");exit(0);
\r
244 { if (VERBOSE(1)) fprintf(stderr,"Unknown option: -v%s\n",arg);
\r
251 { char*shortoption;
\r
264 int args_callback_longoption(char*name,char*val) {
\r
266 for(t=0;t<sizeof(options)/sizeof(struct options_t);t++)
\r
267 if(!strcmp(options[t].longoption, name))
\r
268 return args_callback_option(options[t].shortoption,val);
\r
269 if (VERBOSE(1)) fprintf(stderr, "Unknown option: --%s\n", name);
\r
273 int args_callback_command(char*arg,char*next) // actually used as filename
\r
275 if (CheckInputFile(arg,&s)<0)
\r
276 { if (VERBOSE(1)) fprintf(stderr, "Unable to open input file: %s\n",arg);
\r
280 { global.files[global.nfiles] = s;
\r
282 if (global.nfiles>=MAX_INPUT_FILES)
\r
283 { if (VERBOSE(1)) fprintf(stderr, "Error: Too many input files.\n");
\r
290 void args_callback_usage(char*name)
\r
291 { fprintf(stderr,"Usage: %s imagefiles[.jpg]|[.jpeg] [...] [-options [value]]\n",name);
\r
292 fprintf(stderr,"-q quality (quality) Set JPEG compression quality (1-100)\n");
\r
293 fprintf(stderr,"-r framerate (rate) Set movie framerate (100/sec)\n");
\r
294 fprintf(stderr,"-o outputfile (output) Set name for SWF output file\n");
\r
295 fprintf(stderr,"-v level (verbose) Set verbose level (0=quiet, 1=default, 2=debug)\n");
\r
296 fprintf(stderr,"-X pixel (width) Force movie width to scale (default: autodetect)\n");
\r
297 fprintf(stderr,"-Y pixel (height) Force movie height to scale (default: autodetect)\n");
\r
298 fprintf(stderr,"-V (version) Print version information and exit\n");
\r
302 int main(int argc, char ** argv)
\r
306 memset(&global,0x00,sizeof(global));
\r
308 global.quality = 60;
\r
309 global.framerate = 100;
\r
310 global.verbose = 1;
\r
312 processargs(argc, argv);
\r
314 if (VERBOSE(2)) fprintf(stderr,"Processing %i file(s)...\n",global.nfiles);
\r
316 t = MovieStart(&swf,global.framerate,
\r
317 global.force_width?global.force_width:global.max_image_width,
\r
318 global.force_height?global.force_height:global.max_image_height);
\r
321 for (i=0;i<global.nfiles;i++)
\r
322 { if (VERBOSE(3)) fprintf(stderr,"[%03i] %s\n",i,global.files[i]);
\r
323 t = MovieAddFrame(&swf,t,global.files[i],global.quality,(i*2)+1);
\r
324 free(global.files[i]);
\r
328 MovieFinish(&swf,t,global.outfile);
\r
334 // Old main routine
\r
337 int ConvertJPEG2SWF(char * sname,char * dname,int quality)
\r
347 struct jpeg_decompress_struct cinfo;
\r
348 struct jpeg_error_mgr jerr;
\r
355 cinfo.err = jpeg_std_error(&jerr);
\r
356 jpeg_create_decompress(&cinfo);
\r
358 if ((f=fopen(sname,"rb"))==NULL)
\r
359 { fprintf(stderr,"Read access failed: %s\n",sname);
\r
363 jpeg_stdio_src(&cinfo,f);
\r
364 jpeg_read_header(&cinfo, TRUE);
\r
365 jpeg_start_decompress(&cinfo);
\r
367 memset(&swf,0x00,sizeof(SWF));
\r
369 swf.FileVersion = 4;
\r
370 swf.FrameRate = 0x1000;
\r
371 swf.MovieSize.xmax = cinfo.output_width*20;
\r
372 swf.MovieSize.ymax = cinfo.output_height*20;
\r
374 printf("dx = %i, dy = %i\n",cinfo.output_width,cinfo.output_height);
\r
376 t = swf.FirstTag = InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
\r
378 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
\r
381 t = InsertTag(t,ST_DEFINEBITSJPEG2);
\r
385 out = SetJPEGBitsStart(t,cinfo.output_width,cinfo.output_height,quality);
\r
386 scanline = (U8*)malloc(4*cinfo.output_width);
\r
390 U8 * js = scanline;
\r
391 for (y=0;y<cinfo.output_height;y++)
\r
392 { jpeg_read_scanlines(&cinfo,&js,1);
\r
393 SetJPEGBitsLines(out,(U8**)&js,1);
\r
398 SetJPEGBitsFinish(out);
\r
400 printf("JPEG Tag-Length: %06x\n",GetDataSize(t));
\r
402 t = InsertTag(t,ST_DEFINESHAPE);
\r
405 GetMatrix(NULL,&m);
\r
409 fs = ShapeAddBitmapFillStyle(s,&m,1,0);
\r
410 // fs = ShapeAddSolidFillStyle(s,&rgb);
\r
413 SetRect(t,&swf.MovieSize);
\r
414 SetShapeHeader(t,s);
\r
416 ShapeSetAll(t,s,0,0,0,fs,0);
\r
417 ShapeSetLine(t,s,swf.MovieSize.xmax,0);
\r
418 ShapeSetLine(t,s,0,swf.MovieSize.ymax);
\r
419 ShapeSetLine(t,s,-swf.MovieSize.xmax,0);
\r
420 ShapeSetLine(t,s,0,-swf.MovieSize.ymax);
\r
424 t = InsertTag(t,ST_PLACEOBJECT2);
\r
426 ObjectPlace(t,2,1,NULL,NULL,NULL);
\r
428 t = InsertTag(t,ST_SHOWFRAME);
\r
430 t = InsertTag(t,ST_END);
\r
432 jpeg_finish_decompress(&cinfo);
\r
435 handle = open(dname,O_RDWR|O_CREAT|O_TRUNC,0666);
\r
436 if FAILED(WriteSWF(handle,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
\r