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)
48 typedef struct _image {
54 image_t image[MAX_INPUT_FILES];
58 TAG *MovieStart(SWF * swf, float framerate, int dx, int dy)
63 memset(swf, 0x00, sizeof(SWF));
65 swf->fileVersion = global.version;
66 swf->frameRate = (int)(256.0 * framerate);
67 swf->movieSize.xmax = dx * 20;
68 swf->movieSize.ymax = dy * 20;
70 t = swf->firstTag = swf_InsertTag(NULL, ST_SETBACKGROUNDCOLOR);
72 rgb.r = rgb.g = rgb.b = rgb.a = 0x00;
76 t = swf_InsertTag(t, ST_DEFINEVIDEOSTREAM);
77 swf_SetU16(t, 0xf00d);
78 swf_SetVideoStreamDefine(t, &stream, 65535, dx, dy);
84 int MovieFinish(SWF * swf, TAG * t, char *sname)
86 int handle, so = fileno(stdout);
87 t = swf_InsertTag(t, ST_END);
89 if ((!isatty(so)) && (!sname))
94 handle = open(sname, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
96 if(global.version >= 6) {
97 if (swf_WriteSWC(handle, swf)<0)
98 fprintf(stderr, "Unable to write output file: %s\n", sname);
100 if (swf_WriteSWF(handle, swf)<0)
101 fprintf(stderr, "Unable to write output file: %s\n", sname);
111 int getJPEG(char*filename, int* width, int* height, RGBA**pic2)
113 struct jpeg_decompress_struct cinfo;
114 struct jpeg_error_mgr jerr;
115 struct jpeg_source_mgr mgr;
121 if ((f=fopen(filename,"rb"))==NULL) {
122 fprintf(stderr, "rfxswf: file open error\n");
126 cinfo.err = jpeg_std_error(&jerr);
127 jpeg_create_decompress(&cinfo);
128 jpeg_stdio_src(&cinfo, f);
129 jpeg_read_header(&cinfo, TRUE);
130 jpeg_start_decompress(&cinfo);
132 pic = malloc(cinfo.output_width*cinfo.output_height*sizeof(RGBA));
133 buf = malloc(cinfo.output_width*4);
134 memset(pic, 255, cinfo.output_width*cinfo.output_height*sizeof(RGBA));
137 *width = cinfo.output_width;
138 *height = cinfo.output_height;
140 for (y=0;y<cinfo.output_height;y++) {
142 jpeg_read_scanlines(&cinfo,&buf,1);
144 if(cinfo.out_color_space == JCS_GRAYSCALE) {
145 for(x=0;x<cinfo.output_width;x++) {
146 js[x].r = js[x].g = js[x].b = buf[x];
148 } else if(cinfo.out_color_space == JCS_RGB) {
149 for (x=0;x<cinfo.output_width;x++)
151 js[x].r = buf[x*3+0];
152 js[x].g = buf[x*3+1];
153 js[x].b = buf[x*3+2];
155 } else if(cinfo.out_color_space == JCS_YCCK) {
157 fprintf(stderr, "Error: Can't convert YCCK to RGB.\n");
159 } else if(cinfo.out_color_space == JCS_YCbCr) {
160 for(x=0;x<cinfo.output_width;x++) {
164 js[x].r = y + ((360*(v-128))>>8);
165 js[x].g = y - ((88*(u-128)+183*(v-128))>>8);
166 js[x].b = y + ((455 * (u-128))>>8);
169 else if(cinfo.out_color_space == JCS_CMYK)
171 for(x=0;x<cinfo.output_width;x++) {
172 int white = 255 - buf[x*4+3];
173 js[x].r = white - ((buf[x*4]*white)>>8);
174 js[x].g = white - ((buf[x*4+1]*white)>>8);
175 js[x].b = white - ((buf[x*4+2]*white)>>8);
178 js += cinfo.output_width;
181 jpeg_finish_decompress(&cinfo);
182 jpeg_destroy_decompress(&cinfo);
191 TAG *MovieAddFrame(SWF * swf, TAG * t, char *sname, int quality,
192 int id, int width, int height)
205 getJPEG(sname, &sizex, &sizey, &pic2);
206 if(sizex != stream.owidth || sizey != stream.oheight) {
207 fprintf(stderr, "All images must have the same dimensions if using -m!");
211 t = swf_InsertTag(t, ST_VIDEOFRAME);
212 swf_SetU16(t, 0xf00d);
213 quant = 1+(30-(30*quality)/100);
215 swf_SetVideoStreamIFrame(t, &stream, pic2, quant);
217 swf_SetVideoStreamPFrame(t, &stream, pic2, quant);
220 t = swf_InsertTag(t, ST_PLACEOBJECT2);
221 swf_GetPlaceObject(0, &obj);
230 swf_SetPlaceObject(t,&obj);
232 t = swf_InsertTag(t, ST_SHOWFRAME);
234 t = swf_InsertTag(t, ST_DEFINEBITSJPEG2);
235 swf_SetU16(t, id); // id
236 swf_SetJPEGBits(t,sname,quality);
238 t = swf_InsertTag(t, ST_DEFINESHAPE);
240 swf_GetMatrix(NULL, &m);
245 fs = swf_ShapeAddBitmapFillStyle(s, &m, id, 0);
246 swf_SetU16(t, id + 1); // id
249 r.ymax = height * 20;
251 swf_SetShapeHeader(t, s);
252 swf_ShapeSetAll(t, s, 0, 0, 0, fs, 0);
253 swf_ShapeSetLine(t, s, r.xmax, 0);
254 swf_ShapeSetLine(t, s, 0, r.ymax);
255 swf_ShapeSetLine(t, s, -r.xmax, 0);
256 swf_ShapeSetLine(t, s, 0, -r.ymax);
260 t = swf_InsertTag(t, ST_REMOVEOBJECT2);
261 swf_SetU16(t, 1); // depth
264 t = swf_InsertTag(t, ST_PLACEOBJECT2);
265 swf_GetMatrix(NULL, &m);
266 m.tx = (swf->movieSize.xmax - (int) width * 20) / 2;
267 m.ty = (swf->movieSize.ymax - (int) height * 20) / 2;
268 swf_ObjectPlace(t, id + 1, 1, &m, NULL, NULL);
270 t = swf_InsertTag(t, ST_SHOWFRAME);
277 int CheckInputFile(image_t* i, char *fname, char **realname)
279 struct jpeg_decompress_struct cinfo;
280 struct jpeg_error_mgr jerr;
282 char *s = malloc(strlen(fname) + 5);
290 // Check whether file exists (with typical extensions)
292 if ((f = fopen(s, "rb")) == NULL) {
293 sprintf(s, "%s.jpg", fname);
294 if ((f = fopen(s, "rb")) == NULL) {
295 sprintf(s, "%s.jpeg", fname);
296 if ((f = fopen(s, "rb")) == NULL) {
297 sprintf(s, "%s.JPG", fname);
298 if ((f = fopen(s, "rb")) == NULL) {
299 sprintf(s, "%s.JPEG", fname);
300 if ((f = fopen(s, "rb")) == NULL)
307 cinfo.err = jpeg_std_error(&jerr);
308 jpeg_create_decompress(&cinfo);
309 jpeg_stdio_src(&cinfo, f);
310 jpeg_read_header(&cinfo, TRUE);
312 width = cinfo.image_width;
313 height = cinfo.image_height;
318 // Get image dimensions
320 if (global.max_image_width < width)
321 global.max_image_width = width;
322 if (global.max_image_height < height)
323 global.max_image_height = height;
325 jpeg_destroy_decompress(&cinfo);
331 int args_callback_option(char *arg, char *val)
340 global.quality = atoi(val);
341 if ((global.quality < 1) ||(global.quality > 100)) {
344 "Error: You must specify a valid quality between 1 and 100.\n");
352 global.framerate = atof(val);
353 if ((global.framerate < 1.0/256) || (global.framerate >= 256.0)) {
356 "Error: You must specify a valid framerate between 1 and 10000.\n");
364 global.outfile = val;
370 global.verbose = atoi(val);
376 global.force_width = atoi(val);
391 global.force_height = atoi(val);
396 printf("jpeg2swf - part of %s %s\n", PACKAGE, VERSION);
406 fprintf(stderr, "Unknown option: -%s\n", arg);
413 static struct options_t options[] = {
426 int args_callback_longoption(char *name, char *val)
428 return args_long2shortoption(options, name, val);
431 int args_callback_command(char *arg, char *next) // actually used as filename
434 image_t* i = &image[global.nfiles];
435 if (CheckInputFile(i, arg, &s) < 0) {
437 fprintf(stderr, "Unable to open input file: %s\n", arg);
441 i->quality = global.quality;
443 if (global.nfiles >= MAX_INPUT_FILES) {
445 fprintf(stderr, "Error: Too many input files.\n");
452 void args_callback_usage(char *name)
455 printf("Usage: %s [-options [value]] imagefiles[.jpg]|[.jpeg] [...]\n", name);
457 printf("-o , --output <outputfile> Explicitly specify output file. (otherwise, output.swf will be used)\n");
458 printf("-m , --mx Use Flash MX H.263 compression (use for correlated images)\n");
459 printf("-q , --quality <quality> Set compression quality (1-100, 1=worst, 100=best)\n");
460 printf("-r , --rate <framerate> Set movie framerate (frames per second)\n");
461 printf("-z , --zlib <zlib> Enable Flash 6 (MX) Zlib Compression\n");
462 printf("-X , --width <width> Force movie width to <width> (default: autodetect)\n");
463 printf("-Y , --height <height> Force movie height to <height> (default: autodetect)\n");
464 printf("-v , --verbose <level> Set verbose level to <level> (0=quiet, 1=default, 2=debug)\n");
465 printf("-V , --version Print version information and exit\n");
470 int main(int argc, char **argv)
475 memset(&global, 0x00, sizeof(global));
478 global.framerate = 1.0;
482 processargs(argc, argv);
485 fprintf(stderr, "Processing %i file(s)...\n", global.nfiles);
487 t = MovieStart(&swf, global.framerate,
488 global.force_width ? global.force_width : global.
490 global.force_height ? global.force_height : global.
495 for (i = 0; i < global.nfiles; i++) {
497 fprintf(stderr, "[%03i] %s (%i%%, 1/%i)\n", i,
498 image[i].filename, image[i].quality);
499 t = MovieAddFrame(&swf, t, image[i].filename, image[i].quality,
501 image[i].width, image[i].height);
502 free(image[i].filename);
506 MovieFinish(&swf, t, global.outfile);