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
250 { char*shortoption;
\r
263 int args_callback_longoption(char*name,char*val) {
\r
265 for(t=0;t<sizeof(options)/sizeof(struct options_t);t++)
\r
266 if(!strcmp(options[t].longoption, name))
\r
267 return args_callback_option(options[t].shortoption,val);
\r
268 if (VERBOSE(1)) fprintf(stderr, "Unknown option: --%s\n", name);
\r
272 int args_callback_command(char*arg,char*next) // actually used as filename
\r
274 if (CheckInputFile(arg,&s)<0)
\r
275 { if (VERBOSE(1)) fprintf(stderr, "Unable to open input file: %s\n",arg);
\r
279 { global.files[global.nfiles] = s;
\r
281 if (global.nfiles>=MAX_INPUT_FILES)
\r
282 { if (VERBOSE(1)) fprintf(stderr, "Error: Too many input files.\n");
\r
289 void args_callback_usage(char*name)
\r
290 { fprintf(stderr,"Usage: %s imagefiles[.jpg]|[.jpeg] [...] [-options [value]]\n",name);
\r
291 fprintf(stderr,"-q quality (quality) Set JPEG compression quality (1-100)\n");
\r
292 fprintf(stderr,"-r framerate (rate) Set movie framerate (100/sec)\n");
\r
293 fprintf(stderr,"-o outputfile (output) Set name for SWF output file\n");
\r
294 fprintf(stderr,"-v level (verbose) Set verbose level (0=quiet, 1=default, 2=debug)\n");
\r
295 fprintf(stderr,"-X pixel (width) Force movie width to scale (default: autodetect)\n");
\r
296 fprintf(stderr,"-Y pixel (height) Force movie height to scale (default: autodetect)\n");
\r
297 fprintf(stderr,"-V (version) Print version information and exit\n");
\r
301 int main(int argc, char ** argv)
\r
305 memset(&global,0x00,sizeof(global));
\r
307 global.quality = 60;
\r
308 global.framerate = 100;
\r
309 global.verbose = 1;
\r
311 processargs(argc, argv);
\r
313 if (VERBOSE(2)) fprintf(stderr,"Processing %i file(s)...\n",global.nfiles);
\r
315 t = MovieStart(&swf,global.framerate,
\r
316 global.force_width?global.force_width:global.max_image_width,
\r
317 global.force_height?global.force_height:global.max_image_height);
\r
320 for (i=0;i<global.nfiles;i++)
\r
321 { if (VERBOSE(3)) fprintf(stderr,"[%03i] %s\n",i,global.files[i]);
\r
322 t = MovieAddFrame(&swf,t,global.files[i],global.quality,(i*2)+1);
\r
323 free(global.files[i]);
\r
327 MovieFinish(&swf,t,global.outfile);
\r
333 // Old main routine
\r
336 int ConvertJPEG2SWF(char * sname,char * dname,int quality)
\r
346 struct jpeg_decompress_struct cinfo;
\r
347 struct jpeg_error_mgr jerr;
\r
354 cinfo.err = jpeg_std_error(&jerr);
\r
355 jpeg_create_decompress(&cinfo);
\r
357 if ((f=fopen(sname,"rb"))==NULL)
\r
358 { fprintf(stderr,"Read access failed: %s\n",sname);
\r
362 jpeg_stdio_src(&cinfo,f);
\r
363 jpeg_read_header(&cinfo, TRUE);
\r
364 jpeg_start_decompress(&cinfo);
\r
366 memset(&swf,0x00,sizeof(SWF));
\r
368 swf.FileVersion = 4;
\r
369 swf.FrameRate = 0x1000;
\r
370 swf.MovieSize.xmax = cinfo.output_width*20;
\r
371 swf.MovieSize.ymax = cinfo.output_height*20;
\r
373 printf("dx = %i, dy = %i\n",cinfo.output_width,cinfo.output_height);
\r
375 t = swf.FirstTag = InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
\r
377 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
\r
380 t = InsertTag(t,ST_DEFINEBITSJPEG2);
\r
384 out = SetJPEGBitsStart(t,cinfo.output_width,cinfo.output_height,quality);
\r
385 scanline = (U8*)malloc(4*cinfo.output_width);
\r
389 U8 * js = scanline;
\r
390 for (y=0;y<cinfo.output_height;y++)
\r
391 { jpeg_read_scanlines(&cinfo,&js,1);
\r
392 SetJPEGBitsLines(out,(U8**)&js,1);
\r
397 SetJPEGBitsFinish(out);
\r
399 printf("JPEG Tag-Length: %06x\n",GetDataSize(t));
\r
401 t = InsertTag(t,ST_DEFINESHAPE);
\r
404 GetMatrix(NULL,&m);
\r
408 fs = ShapeAddBitmapFillStyle(s,&m,1,0);
\r
409 // fs = ShapeAddSolidFillStyle(s,&rgb);
\r
412 SetRect(t,&swf.MovieSize);
\r
413 SetShapeHeader(t,s);
\r
415 ShapeSetAll(t,s,0,0,0,fs,0);
\r
416 ShapeSetLine(t,s,swf.MovieSize.xmax,0);
\r
417 ShapeSetLine(t,s,0,swf.MovieSize.ymax);
\r
418 ShapeSetLine(t,s,-swf.MovieSize.xmax,0);
\r
419 ShapeSetLine(t,s,0,-swf.MovieSize.ymax);
\r
423 t = InsertTag(t,ST_PLACEOBJECT2);
\r
425 ObjectPlace(t,2,1,NULL,NULL,NULL);
\r
427 t = InsertTag(t,ST_SHOWFRAME);
\r
429 t = InsertTag(t,ST_END);
\r
431 jpeg_finish_decompress(&cinfo);
\r
434 handle = open(dname,O_RDWR|O_CREAT|O_TRUNC,0666);
\r
435 if FAILED(WriteSWF(handle,&swf)) fprintf(stderr,"WriteSWF() failed.\n");
\r