3 JPEG to SWF converter tool
5 Part of the swftools package.
7 Copyright (c) 2001 Rainer Böhme <rfxswf@reflex-studio.de>
8 Copyright (c) 2002,2003 Matthias Kramm <kramm@quiss.org>
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
28 #include "../lib/rfxswf.h"
29 #include "../lib/args.h" // not really a header ;-)
31 #define MAX_INPUT_FILES 1024
32 #define VERBOSE(x) (global.verbose>=x)
53 typedef struct _image {
59 image_t image[MAX_INPUT_FILES];
63 TAG *MovieStart(SWF * swf, float framerate, int dx, int dy)
68 memset(swf, 0x00, sizeof(SWF));
70 swf->fileVersion = global.version;
71 swf->frameRate = (int)(256.0 * framerate);
72 swf->movieSize.xmin = global.xoffset * 20;
73 swf->movieSize.ymin = global.yoffset * 20;
74 swf->movieSize.xmax = swf->movieSize.xmin + dx * 20;
75 swf->movieSize.ymax = swf->movieSize.ymin + dy * 20;
77 t = swf->firstTag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
79 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
83 t = swf_InsertTag(t, ST_DEFINEVIDEOSTREAM);
84 swf_SetU16(t, 0xf00d);
85 swf_SetVideoStreamDefine(t, &stream, 65535, dx, dy);
86 } else if (global.asset_name) {
87 t = swf_InsertTag(t, ST_DEFINESPRITE);
89 swf_SetU16(t, global.next_id++);
95 int MovieFinish(SWF * swf, TAG * t, char *sname)
97 int handle, so = fileno(stdout);
99 if (global.asset_name) {
102 t = swf_InsertTag(t, ST_END);
103 t = swf_InsertTag(t, ST_EXPORTASSETS);
106 swf_SetString(t, global.asset_name);
108 t = swf_InsertTag(t, ST_PLACEOBJECT2);
109 swf_GetPlaceObject(0, &obj);
112 swf_SetPlaceObject(t, &obj);
114 t = swf_InsertTag(t, ST_SHOWFRAME);
117 t = swf_InsertTag(t, ST_END);
119 if ((!isatty(so)) && (!sname))
123 sname = "output.swf";
124 handle = open(sname, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
126 if(global.version >= 6) {
127 if (swf_WriteSWC(handle, swf)<0)
128 fprintf(stderr, "Unable to write output file: %s\n", sname);
130 if (swf_WriteSWF(handle, swf)<0)
131 fprintf(stderr, "Unable to write output file: %s\n", sname);
141 int getJPEG(char*filename, int* width, int* height, RGBA**pic2)
143 struct jpeg_decompress_struct cinfo;
144 struct jpeg_error_mgr jerr;
145 struct jpeg_source_mgr mgr;
151 if ((f=fopen(filename,"rb"))==NULL) {
152 fprintf(stderr, "rfxswf: file open error\n");
156 cinfo.err = jpeg_std_error(&jerr);
157 jpeg_create_decompress(&cinfo);
158 jpeg_stdio_src(&cinfo, f);
159 jpeg_read_header(&cinfo, TRUE);
160 jpeg_start_decompress(&cinfo);
162 pic = malloc(cinfo.output_width*cinfo.output_height*sizeof(RGBA));
163 buf = malloc(cinfo.output_width*4);
164 memset(pic, 255, cinfo.output_width*cinfo.output_height*sizeof(RGBA));
167 *width = cinfo.output_width;
168 *height = cinfo.output_height;
170 for (y=0;y<cinfo.output_height;y++) {
172 jpeg_read_scanlines(&cinfo,&buf,1);
174 if(cinfo.out_color_space == JCS_GRAYSCALE) {
175 for(x=0;x<cinfo.output_width;x++) {
176 js[x].r = js[x].g = js[x].b = buf[x];
178 } else if(cinfo.out_color_space == JCS_RGB) {
179 for (x=0;x<cinfo.output_width;x++)
181 js[x].r = buf[x*3+0];
182 js[x].g = buf[x*3+1];
183 js[x].b = buf[x*3+2];
185 } else if(cinfo.out_color_space == JCS_YCCK) {
187 fprintf(stderr, "Error: Can't convert YCCK to RGB.\n");
189 } else if(cinfo.out_color_space == JCS_YCbCr) {
190 for(x=0;x<cinfo.output_width;x++) {
194 js[x].r = y + ((360*(v-128))>>8);
195 js[x].g = y - ((88*(u-128)+183*(v-128))>>8);
196 js[x].b = y + ((455 * (u-128))>>8);
199 else if(cinfo.out_color_space == JCS_CMYK)
201 for(x=0;x<cinfo.output_width;x++) {
202 int white = 255 - buf[x*4+3];
203 js[x].r = white - ((buf[x*4]*white)>>8);
204 js[x].g = white - ((buf[x*4+1]*white)>>8);
205 js[x].b = white - ((buf[x*4+2]*white)>>8);
208 js += cinfo.output_width;
211 jpeg_finish_decompress(&cinfo);
212 jpeg_destroy_decompress(&cinfo);
221 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int quality,
222 int width, int height)
228 int movie_width = swf->movieSize.xmax - swf->movieSize.xmin;
229 int movie_height = swf->movieSize.ymax - swf->movieSize.ymin;
236 getJPEG(sname, &sizex, &sizey, &pic2);
237 if(sizex != stream.owidth || sizey != stream.oheight) {
238 fprintf(stderr, "All images must have the same dimensions if using -m!");
242 t = swf_InsertTag(t, ST_VIDEOFRAME);
243 swf_SetU16(t, 0xf00d);
244 quant = 1+(30-(30*quality)/100);
246 swf_SetVideoStreamIFrame(t, &stream, pic2, quant);
248 swf_SetVideoStreamPFrame(t, &stream, pic2, quant);
251 t = swf_InsertTag(t, ST_PLACEOBJECT2);
252 swf_GetPlaceObject(0, &obj);
261 swf_SetPlaceObject(t,&obj);
263 t = swf_InsertTag(t, ST_SHOWFRAME);
265 t = swf_InsertTag(t, ST_DEFINEBITSJPEG2);
266 swf_SetU16(t, global.next_id); // id
267 swf_SetJPEGBits(t,sname,quality);
269 t = swf_InsertTag(t, ST_DEFINESHAPE);
271 swf_GetMatrix(NULL, &m);
272 if (global.fit_to_movie) {
273 m.sx = 0x10000 * movie_width / width;
274 m.sy = 0x10000 * movie_height / height;
275 width = movie_width / 20;
276 height = movie_height / 20;
281 m.tx = global.xoffset * 20;
282 m.ty = global.yoffset * 20;
283 fs = swf_ShapeAddBitmapFillStyle(s, &m, global.next_id, 1);
285 swf_SetU16(t, global.next_id); // id
286 r.xmin = global.xoffset * 20;
287 r.ymin = global.yoffset * 20;
288 r.xmax = r.xmin + width * 20;
289 r.ymax = r.ymin + height * 20;
291 swf_SetShapeHeader(t, s);
292 swf_ShapeSetAll(t, s, r.xmin, r.ymin, 0, fs, 0);
293 swf_ShapeSetLine(t, s, r.xmax - r.xmin, 0);
294 swf_ShapeSetLine(t, s, 0, r.ymax - r.ymin);
295 swf_ShapeSetLine(t, s, -r.xmax + r.xmin, 0);
296 swf_ShapeSetLine(t, s, 0, -r.ymax + r.ymin);
300 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
301 swf_SetU16(t, 1); // depth
304 t = swf_InsertTag(t, ST_PLACEOBJECT2);
305 swf_GetMatrix(NULL, &m);
306 m.tx = (movie_width - width * 20) / 2;
307 m.ty = (movie_height - height * 20) / 2;
308 swf_ObjectPlace(t, global.next_id, 1, &m, NULL, NULL);
310 t = swf_InsertTag(t, ST_SHOWFRAME);
317 int CheckInputFile(image_t* i, char *fname, char **realname)
319 struct jpeg_decompress_struct cinfo;
320 struct jpeg_error_mgr jerr;
322 char *s = malloc(strlen(fname) + 5);
330 // Check whether file exists (with typical extensions)
332 if ((f = fopen(s, "rb")) == NULL) {
333 sprintf(s, "%s.jpg", fname);
334 if ((f = fopen(s, "rb")) == NULL) {
335 sprintf(s, "%s.jpeg", fname);
336 if ((f = fopen(s, "rb")) == NULL) {
337 sprintf(s, "%s.JPG", fname);
338 if ((f = fopen(s, "rb")) == NULL) {
339 sprintf(s, "%s.JPEG", fname);
340 if ((f = fopen(s, "rb")) == NULL)
347 cinfo.err = jpeg_std_error(&jerr);
348 jpeg_create_decompress(&cinfo);
349 jpeg_stdio_src(&cinfo, f);
350 jpeg_read_header(&cinfo, TRUE);
352 width = cinfo.image_width;
353 height = cinfo.image_height;
358 // Get image dimensions
360 if (global.max_image_width < width)
361 global.max_image_width = width;
362 if (global.max_image_height < height)
363 global.max_image_height = height;
365 jpeg_destroy_decompress(&cinfo);
371 int args_callback_option(char *arg, char *val)
380 global.quality = atoi(val);
381 if ((global.quality < 1) ||(global.quality > 100)) {
384 "Error: You must specify a valid quality between 1 and 100.\n");
392 global.framerate = atof(val);
393 if ((global.framerate < 1.0/256) || (global.framerate >= 256.0)) {
396 "Error: You must specify a valid framerate between 1 and 10000.\n");
404 global.outfile = val;
410 global.verbose = atoi(val);
416 global.force_width = atoi(val);
431 global.force_height = atoi(val);
436 printf("jpeg2swf - part of %s %s\n", PACKAGE, VERSION);
441 global.asset_name = val;
447 global.xoffset = atoi(val);
454 global.yoffset = atoi(val);
460 global.fit_to_movie = 1;
471 fprintf(stderr, "Unknown option: -%s\n", arg);
478 static struct options_t options[] = {
489 {"f", "fit-to-movie"},
495 int args_callback_longoption(char *name, char *val)
497 return args_long2shortoption(options, name, val);
500 int args_callback_command(char *arg, char *next) // actually used as filename
503 image_t* i = &image[global.nfiles];
504 if (CheckInputFile(i, arg, &s) < 0) {
506 fprintf(stderr, "Unable to open input file: %s\n", arg);
510 i->quality = global.quality;
512 if (global.nfiles >= MAX_INPUT_FILES) {
514 fprintf(stderr, "Error: Too many input files.\n");
521 void args_callback_usage(char *name)
524 printf("Usage: %s [-options [value]] imagefiles[.jpg]|[.jpeg] [...]\n", name);
526 printf("-o , --output <outputfile> Explicitly specify output file. (otherwise, output.swf will be used)\n");
527 printf("-m , --mx Use Flash MX H.263 compression (use for correlated images)\n");
528 printf("-q , --quality <quality> Set compression quality (1-100, 1=worst, 100=best)\n");
529 printf("-r , --rate <framerate> Set movie framerate (frames per second)\n");
530 printf("-z , --zlib <zlib> Enable Flash 6 (MX) Zlib Compression\n");
531 printf("-x , --xoffset <offset> horizontally offset images by <offset>\n");
532 printf("-y , --yoffset <offset> vertically offset images by <offset>\n");
533 printf("-X , --width <width> Force movie width to <width> (default: autodetect)\n");
534 printf("-Y , --height <height> Force movie height to <height> (default: autodetect)\n");
535 printf("-f , --fit-to-movie Fit images to movie size\n");
536 printf("-e , --export <assetname> Make importable as asset with <assetname>\n");
537 printf("-v , --verbose <level> Set verbose level to <level> (0=quiet, 1=default, 2=debug)\n");
538 printf("-V , --version Print version information and exit\n");
543 int main(int argc, char **argv)
548 memset(&global, 0x00, sizeof(global));
551 global.framerate = 1.0;
554 global.asset_name = NULL;
558 global.fit_to_movie = 0;
560 processargs(argc, argv);
563 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
565 t = MovieStart(&swf, global.framerate,
566 global.force_width ? global.force_width : global.
568 global.force_height ? global.force_height : global.
573 for (i = 0; i < global.nfiles; i++) {
575 fprintf(stderr, "[%03i] %s (%i%%, 1/%i)\n", i,
576 image[i].filename, image[i].quality);
577 t = MovieAddFrame(&swf, t, image[i].filename, image[i].quality,
578 image[i].width, image[i].height);
579 free(image[i].filename);
583 MovieFinish(&swf, t, global.outfile);