3 JPEG to SWF converter tool
5 Part of the swftools package.
7 Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
9 This file is distributed under the GPL, see file COPYING for details
17 #include "../lib/rfxswf.h"
18 #include "../lib/args.h" // not really a header ;-)
20 #define MAX_INPUT_FILES 1024
21 #define VERBOSE(x) (global.verbose>=x)
40 } image[MAX_INPUT_FILES];
42 TAG * MovieStart(SWF * swf,int framerate,int dx,int dy)
46 memset(swf,0x00,sizeof(SWF));
49 swf->frameRate = (25600/framerate);
50 swf->movieSize.xmax = dx*20;
51 swf->movieSize.ymax = dy*20;
53 t = swf->firstTag = swf_InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
55 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
61 int MovieFinish(SWF * swf,TAG * t,char * sname)
62 { int handle, so = fileno(stdout);
63 t = swf_InsertTag(t,ST_END);
65 if ((!isatty(so))&&(!sname)) handle = so;
67 { if (!sname) sname = "out.swf";
68 handle = open(sname,O_RDWR|O_CREAT|O_TRUNC,0666);
70 if FAILED(swf_WriteSWF(handle,swf)) if (VERBOSE(1)) fprintf(stderr,"Unable to write output file: %s\n",sname);
71 if (handle!=so) close(handle);
77 TAG * MovieAddFrame(SWF * swf,TAG * t,char * sname,int quality,int scale,int id)
83 struct jpeg_decompress_struct cinfo;
84 struct jpeg_error_mgr jerr;
89 if ((f=fopen(sname,"rb"))==NULL)
90 { if (VERBOSE(1)) fprintf(stderr,"Read access failed: %s\n",sname);
94 cinfo.err = jpeg_std_error(&jerr);
95 jpeg_create_decompress(&cinfo);
96 jpeg_stdio_src(&cinfo,f);
97 jpeg_read_header(&cinfo, TRUE);
100 { cinfo.scale_num = 1;
101 cinfo.scale_denom = scale;
104 jpeg_start_decompress(&cinfo);
106 t = swf_InsertTag(t,ST_DEFINEBITSJPEG2);
108 swf_SetU16(t,id); // id
110 out = swf_SetJPEGBitsStart(t,cinfo.output_width,cinfo.output_height,quality);
111 scanline = (U8*)malloc(4*cinfo.output_width);
116 for (y=0;y<cinfo.output_height;y++)
117 { jpeg_read_scanlines(&cinfo,&js,1);
118 swf_SetJPEGBitsLines(out,(U8**)&js,1);
123 swf_SetJPEGBitsFinish(out);
125 t = swf_InsertTag(t,ST_DEFINESHAPE);
128 swf_GetMatrix(NULL,&m);
131 fs = swf_ShapeAddBitmapFillStyle(s,&m,id,0);
133 swf_SetU16(t,id+1); // id
137 r.xmax = cinfo.output_width*20;
138 r.ymax = cinfo.output_height*20;
141 swf_SetShapeHeader(t,s);
143 swf_ShapeSetAll(t,s,0,0,0,fs,0);
144 swf_ShapeSetLine(t,s,r.xmax,0);
145 swf_ShapeSetLine(t,s,0,r.ymax);
146 swf_ShapeSetLine(t,s,-r.xmax,0);
147 swf_ShapeSetLine(t,s,0,-r.ymax);
151 t = swf_InsertTag(t,ST_REMOVEOBJECT2);
152 swf_SetU16(t,1); // depth
154 t = swf_InsertTag(t,ST_PLACEOBJECT2);
156 swf_GetMatrix(NULL,&m);
157 m.tx = (swf->movieSize.xmax-(int)cinfo.output_width*20)/2;
158 m.ty = (swf->movieSize.ymax-(int)cinfo.output_height*20)/2;
159 swf_ObjectPlace(t,id+1,1,&m,NULL,NULL);
161 t = swf_InsertTag(t,ST_SHOWFRAME);
163 jpeg_finish_decompress(&cinfo);
169 int CheckInputFile(char * fname,char ** realname)
170 { struct jpeg_decompress_struct cinfo;
171 struct jpeg_error_mgr jerr;
173 char * s = malloc(strlen(fname)+5);
180 // Check whether file exists (with typical extensions)
182 if ((f=fopen(s,"rb"))==NULL)
183 { sprintf(s,"%s.jpg",fname);
184 if ((f=fopen(s,"rb"))==NULL)
185 { sprintf(s,"%s.jpeg",fname);
186 if ((f=fopen(s,"rb"))==NULL)
187 { sprintf(s,"%s.JPG",fname);
188 if ((f=fopen(s,"rb"))==NULL)
189 { sprintf(s,"%s.JPEG",fname);
190 if ((f=fopen(s,"rb"))==NULL)
197 cinfo.err = jpeg_std_error(&jerr);
198 jpeg_create_decompress(&cinfo);
199 jpeg_stdio_src(&cinfo,f);
200 jpeg_read_header(&cinfo, TRUE);
202 // Apply scaling (scale option can be used several times to set different scales)
204 if (global.prescale>1)
205 { cinfo.scale_num = 1;
206 cinfo.scale_denom = global.prescale;
208 jpeg_calc_output_dimensions(&cinfo);
210 width = cinfo.output_width;
211 height = cinfo.output_height;
214 { width = cinfo.image_width;
215 height = cinfo.image_height;
218 // Get image dimensions
220 if (global.max_image_width<width) global.max_image_width = width;
221 if (global.max_image_height<height) global.max_image_height = height;
223 jpeg_destroy_decompress(&cinfo);
229 int args_callback_option(char*arg,char*val)
231 if (arg[1]) res = -1;
234 if (val) global.quality = atoi(val);
235 if ((global.quality<1)||(global.quality>100))
236 { if (VERBOSE(1)) fprintf(stderr,"Error: You must specify a valid quality between 1 and 100.\n");
243 if (val) global.framerate = atoi(val);
244 if ((global.framerate<1)||(global.framerate>5000))
245 { if (VERBOSE(1)) fprintf(stderr,"Error: You must specify a valid framerate between 1 and 10000.\n");
252 if (val) global.prescale = atoi(val);
253 if (!((global.prescale==1)||(global.prescale==2)||(global.prescale==4)||(global.prescale==8)))
254 { if (VERBOSE(1)) fprintf(stderr,"Error: Prescale denominator is limited to 2, 4 or 8\n");
261 if (val) global.outfile = val; res = 1; break;
264 if (val) global.verbose = atoi(val); res = 1; break;
267 if (val) global.force_width = atoi(val); res = 1; break;
270 if (val) global.force_height = atoi(val); res = 1; break;
273 printf("jpeg2swf - part of %s %s\n", PACKAGE, VERSION);exit(0);
281 { if (VERBOSE(1)) fprintf(stderr,"Unknown option: -v%s\n",arg);
287 struct options_t options[] =
298 int args_callback_longoption(char*name,char*val) {
299 return args_long2shortoption(options, name, val);
302 int args_callback_command(char*arg,char*next) // actually used as filename
305 if (CheckInputFile(arg,&s)<0)
306 { if (VERBOSE(1)) fprintf(stderr, "Unable to open input file: %s\n",arg);
310 { image[global.nfiles].filename = s;
311 image[global.nfiles].scale = global.prescale;
312 image[global.nfiles].quality = global.quality;
314 if (global.nfiles>=MAX_INPUT_FILES)
315 { if (VERBOSE(1)) fprintf(stderr, "Error: Too many input files.\n");
322 void args_callback_usage(char*name)
323 { fprintf(stderr,"Usage: %s [-options [value]] imagefiles[.jpg]|[.jpeg] [...]\n",name);
324 fprintf(stderr,"-q quality (quality) Set JPEG compression quality (1-100)\n");
325 fprintf(stderr,"-s denominator (scale) 2, 4 or 8: Reduce image size to 1/2, 1/4, 1/8\n");
326 fprintf(stderr,"-r framerate (rate) Set movie framerate (100/sec)\n");
327 fprintf(stderr,"-o outputfile (output) Set name for SWF output file\n");
328 fprintf(stderr,"-X pixel (width) Force movie width to scale (default: autodetect)\n");
329 fprintf(stderr,"-Y pixel (height) Force movie height to scale (default: autodetect)\n");
330 fprintf(stderr,"-v level (verbose) Set verbose level (0=quiet, 1=default, 2=debug)\n");
331 fprintf(stderr,"-V (version) Print version information and exit\n");
332 fprintf(stderr,"The following options can be set independently for each image: -q -s\n");
336 int main(int argc, char ** argv)
340 memset(&global,0x00,sizeof(global));
343 global.framerate = 100;
347 processargs(argc, argv);
349 if (VERBOSE(2)) fprintf(stderr,"Processing %i file(s)...\n",global.nfiles);
351 t = MovieStart(&swf,global.framerate,
352 global.force_width?global.force_width:global.max_image_width,
353 global.force_height?global.force_height:global.max_image_height);
356 for (i=0;i<global.nfiles;i++)
357 { if (VERBOSE(3)) fprintf(stderr,"[%03i] %s (%i%%, 1/%i)\n",i,image[i].filename,image[i].quality,image[i].scale);
358 t = MovieAddFrame(&swf,t,image[i].filename,
360 image[i].scale,(i*2)+1);
361 free(image[i].filename);
365 MovieFinish(&swf,t,global.outfile);
374 int ConvertJPEG2SWF(char * sname,char * dname,int quality)
384 struct jpeg_decompress_struct cinfo;
385 struct jpeg_error_mgr jerr;
392 cinfo.err = jpeg_std_error(&jerr);
393 jpeg_create_decompress(&cinfo);
395 if ((f=fopen(sname,"rb"))==NULL)
396 { fprintf(stderr,"Read access failed: %s\n",sname);
400 jpeg_stdio_src(&cinfo,f);
401 jpeg_read_header(&cinfo, TRUE);
402 jpeg_start_decompress(&cinfo);
404 memset(&swf,0x00,sizeof(SWF));
407 swf.FrameRate = 0x1000;
408 swf.MovieSize.xmax = cinfo.output_width*20;
409 swf.MovieSize.ymax = cinfo.output_height*20;
411 printf("dx = %i, dy = %i\n",cinfo.output_width,cinfo.output_height);
413 t = swf.FirstTag = InsertTag(NULL,ST_SETBACKGROUNDCOLOR);
415 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
418 t = InsertTag(t,ST_DEFINEBITSJPEG2);
422 out = SetJPEGBitsStart(t,cinfo.output_width,cinfo.output_height,quality);
423 scanline = (U8*)malloc(4*cinfo.output_width);
428 for (y=0;y<cinfo.output_height;y++)
429 { jpeg_read_scanlines(&cinfo,&js,1);
430 SetJPEGBitsLines(out,(U8**)&js,1);
435 SetJPEGBitsFinish(out);
437 printf("JPEG Tag-Length: %06x\n",GetDataSize(t));
439 t = InsertTag(t,ST_DEFINESHAPE);
446 fs = ShapeAddBitmapFillStyle(s,&m,1,0);
447 // fs = ShapeAddSolidFillStyle(s,&rgb);
450 SetRect(t,&swf.MovieSize);
453 ShapeSetAll(t,s,0,0,0,fs,0);
454 ShapeSetLine(t,s,swf.MovieSize.xmax,0);
455 ShapeSetLine(t,s,0,swf.MovieSize.ymax);
456 ShapeSetLine(t,s,-swf.MovieSize.xmax,0);
457 ShapeSetLine(t,s,0,-swf.MovieSize.ymax);
461 t = InsertTag(t,ST_PLACEOBJECT2);
463 ObjectPlace(t,2,1,NULL,NULL,NULL);
465 t = InsertTag(t,ST_SHOWFRAME);
467 t = InsertTag(t,ST_END);
469 jpeg_finish_decompress(&cinfo);
472 handle = open(dname,O_RDWR|O_CREAT|O_TRUNC,0666);
473 if FAILED(WriteSWF(handle,&swf)) fprintf(stderr,"WriteSWF() failed.\n");