| 1 | package IO::SWF::Lossless; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Compress::Zlib; | ||
| 7 | use Image::Magick; | ||
| 8 | |||
| 9 | sub BitmapData2Lossless { | ||
| 10 | my ($bitmap_id, $bitmap_data) = @_; | ||
| 11 | |||
| 12 | my $magick = Image::Magick->new(); | ||
| 13 | $magick->BlobToImage( $bitmap_data ); | ||
| 14 | |||
| 15 | my $colortable_size = $magick->Get('colors'); | ||
| 16 | my ($width, $height) = $magick->Get( 'width', 'height' ); | ||
| 17 | my ($format, $transparent_exists); | ||
| 18 | my $colortable = ''; | ||
| 19 | my $pixeldata = ''; | ||
| 20 | my $rgb_delimiter = 257; | ||
| 21 | |||
| 22 | if (($magick->Get('type') !~ /^TrueColor/) && ($colortable_size <= 256)) { | ||
| 23 | $format = 3; # palette format | ||
| 24 | $transparent_exists = 0; | ||
| 25 | for (my $i = 0 ; $i < $colortable_size ; $i++) { | ||
| 26 | my ($r,$g,$b,$alpha) = split /,/, $magick->Get("colormap[$i]"); | ||
| 27 | if ($alpha > 0) { | ||
| 28 | $transparent_exists = 1; | ||
| 29 | last; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | if (!$transparent_exists) { | ||
| 33 | for (my $i = 0 ; $i < $colortable_size ; $i++) { | ||
| 34 | my ($r,$g,$b,$alpha) = split /,/, $magick->Get("colormap[$i]"); | ||
| 35 | $colortable .= chr($r/$rgb_delimiter); | ||
| 36 | $colortable .= chr($g/$rgb_delimiter); | ||
| 37 | $colortable .= chr($b/$rgb_delimiter); | ||
| 38 | } | ||
| 39 | } else { | ||
| 40 | for (my $i = 0 ; $i < $colortable_size ; $i++) { | ||
| 41 | my ($r,$g,$b,$alpha) = split /,/, $magick->Get("colormap[$i]"); | ||
| 42 | $alpha /= $rgb_delimiter; | ||
| 43 | $colortable .= chr($r/$rgb_delimiter * $alpha / 255); | ||
| 44 | $colortable .= chr($g/$rgb_delimiter * $alpha / 255); | ||
| 45 | $colortable .= chr($b/$rgb_delimiter * $alpha / 255); | ||
| 46 | $colortable .= chr($alpha); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | my $i = 0; | ||
| 50 | |||
| 51 | for (my $y = 0 ; $y < $height ; $y++) { | ||
| 52 | for (my $x = 0 ; $x < $width ; $x++) { | ||
| 53 | $pixeldata .= chr($magick->Get("index[$x,$y]")); | ||
| 54 | $i++; | ||
| 55 | } | ||
| 56 | while (($i % 4) != 0) { | ||
| 57 | $pixeldata .= chr(0); | ||
| 58 | $i++; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } else { # truecolor | ||
| 62 | $format = 5; # trurcolor format | ||
| 63 | $transparent_exists = 0; | ||
| 64 | |||
| 65 | |||
| 66 | for (my $y = 0 ; $y < $height ; $y++) { | ||
| 67 | for (my $x = 0 ; $x < $width ; $x++) { | ||
| 68 | my $i = $magick->Get("index[$x,$y]"); | ||
| 69 | my ($r,$g,$b,$alpha) = split /,/, $magick->Get("colormap[$i]"); | ||
| 70 | |||
| 71 | if ($alpha > 0) { | ||
| 72 | $transparent_exists = 1; | ||
| 73 | last; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | if (!$transparent_exists) { | ||
| 78 | for (my $y = 0 ; $y < $height ; $y++) { | ||
| 79 | for (my $x = 0 ; $x < $width ; $x++) { | ||
| 80 | my $i = $magick->Get("index[$x,$y]"); | ||
| 81 | my ($r,$g,$b,$alpha) = split /,/, $magick->Get("colormap[$i]"); | ||
| 82 | $pixeldata .= 0; # Always 0 | ||
| 83 | $pixeldata .= chr($r/$rgb_delimiter); | ||
| 84 | $pixeldata .= chr($g/$rgb_delimiter); | ||
| 85 | $pixeldata .= chr($b/$rgb_delimiter); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } else { | ||
| 89 | for (my $y = 0 ; $y < $height ; $y++) { | ||
| 90 | for (my $x = 0 ; $x < $width ; $x++) { | ||
| 91 | my $i = $magick->Get("index[$x,$y]"); | ||
| 92 | my ($r,$g,$b,$alpha) = split /,/, $magick->Get("colormap[$i]"); | ||
| 93 | $alpha /= $rgb_delimiter; | ||
| 94 | $pixeldata .= chr($alpha); | ||
| 95 | $pixeldata .= chr($r/$rgb_delimiter * $alpha / 255); | ||
| 96 | $pixeldata .= chr($g/$rgb_delimiter * $alpha / 255); | ||
| 97 | $pixeldata .= chr($b/$rgb_delimiter * $alpha / 255); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | my $tagCode; | ||
| 104 | my $content = pack('v', $bitmap_id).chr($format).pack('v', $width).pack('v', $height); | ||
| 105 | if ($format == 3) { | ||
| 106 | $content .= chr($colortable_size - 1).Compress::Zlib::memGzip($colortable.$pixeldata); | ||
| 107 | } else { | ||
| 108 | $content .= Compress::Zlib::memGzip($pixeldata); | ||
| 109 | } | ||
| 110 | |||
| 111 | if (!$transparent_exists) { | ||
| 112 | $tagCode = 20; # DefineBitsLossless | ||
| 113 | } else { | ||
| 114 | $tagCode = 36; # DefineBitsLossless2 | ||
| 115 | } | ||
| 116 | my %tag = ('Code' => 21,#$tagCode, | ||
| 117 | 'width' => $width, | ||
| 118 | 'height' => $height, | ||
| 119 | 'Content' => $content); | ||
| 120 | return %tag; | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; |