1#!/usr/bin/perl
2
3use strict;
4
5use SWF::File;
6use SWF::Element;
7
8my ($imagefile, $swffile) = @ARGV;
9
10unless (defined $imagefile) {
11    print STDERR <<USAGE;
12jpg2swf.plx - convert JPEG to swf.
13  perl jpg2swf.plx JPEGfile [swffile]
14USAGE
15
16    exit(1);
17}
18
19($swffile = $imagefile) =~s/\.[^.]+$/.swf/ unless defined $swffile;
20
21# read image to ImageMagick and get size.
22
23open my $image, $ARGV[0];
24binmode $image;
25undef $/;
26my ($jpegdata) = <$image>;
27
28my $pos = 2;
29while((my $s=substr($jpegdata, $pos, 2)) ne "\xff\xc0" and $pos < length($jpegdata)) {
30    $pos += 2+unpack('n', substr($jpegdata, $pos+2,2));
31}
32
33die "Can't get the width and height of $imagefile.\n" if $pos>=length($jpegdata);
34my $width = unpack('n', substr($jpegdata, $pos+7,2));
35my $height = unpack('n', substr($jpegdata, $pos+5,2));
36
37# create SWF.
38
39my $swf = SWF::File->new($swffile);
40$swf->FrameRate(15);
41$swf->FrameSize(0,0,$width*20,$height*20);   # It can't set the same size???
42
43SWF::Element::Tag::SetBackgroundColor->new(
44     BackgroundColor => [
45      Red => 255,
46      Green => 255,
47      Blue => 255,         # white
48     ],
49)->pack($swf);
50
51SWF::Element::Tag::DefineBitsJPEG2->new(
52     CharacterID => 1,
53     JPEGData => SWF::Element::BinData->new($jpegdata),
54)->pack($swf);
55
56# define same size rectangle filled with the bitmap.
57
58SWF::Element::Tag::DefineShape2->new(
59     ShapeID => 2,
60     ShapeBounds => [
61      Xmin => 0,
62      Ymin => 0,
63      Xmax => $width,
64      Ymax => $height
65     ],
66     Shapes => [
67      FillStyles => [
68       FillStyleType => 0x40,
69       BitmapID => 1,
70      ],
71      ShapeRecords => [
72       [MoveDeltaX => 0, MoveDeltaY => 0, FillStyle0 => 1],
73       [DeltaX => $width],
74       [DeltaY => $height],
75       [DeltaX => -$width],
76       [DeltaY => -$height],
77      ],
78     ],
79)->pack($swf);
80
81# place the rectangle.
82
83SWF::Element::Tag::PlaceObject2->new(
84     CharacterID => 2,
85     Depth => 1,
86     Matrix => [
87      TranslateX => 0,
88      TranslateY => 0,
89      ScaleX => 20,
90      ScaleY => 20,
91     ],
92)->pack($swf);
93SWF::Element::Tag::ShowFrame->new->pack($swf);
94SWF::Element::Tag::DoAction->new(
95     Actions => [[Tag => 'ActionStop']],
96)->pack($swf);
97
98SWF::Element::Tag::ShowFrame->new->pack($swf);
99SWF::Element::Tag::End->new->pack($swf);
100
101$swf->close;
102