| 1 | package Image::TextMode::Renderer::GD; | ||
| 2 | |||
| 3 | use Moo; | ||
| 4 | use Module::Runtime (); | ||
| 5 | use GD; | ||
| 6 | use Image::TextMode::Palette::ANSI; | ||
| 7 | use Carp 'croak'; | ||
| 8 | use File::ShareDir; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Image::TextMode::Renderer::GD - A GD-based renderer for text mode images | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | use Image::TextMode::Format::ANSI; | ||
| 17 | use Image::TextMode::Renderer::GD; | ||
| 18 | |||
| 19 | my $ansi = Image::TextMode::Format::ANSI->new; | ||
| 20 | $ansi->read( $file ); | ||
| 21 | |||
| 22 | my $renderer = Image::TextMode::Renderer::GD->new; | ||
| 23 | |||
| 24 | # render fullscale version | ||
| 25 | print $renderer->fullscale( $ansi ); | ||
| 26 | |||
| 27 | # render thumnail version | ||
| 28 | print $renderer->thumbnail( $ansi ); | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | This module allows you to render your text mode image though the GD | ||
| 33 | graphics library. | ||
| 34 | |||
| 35 | =head1 METHODS | ||
| 36 | |||
| 37 | =head2 new( %args ) | ||
| 38 | |||
| 39 | Creates a new instance. | ||
| 40 | |||
| 41 | =head2 thumbnail( $source, \%options ) | ||
| 42 | |||
| 43 | Renders a thumbnail-sized version of the image. This is mostly a pass-through to | ||
| 44 | C<fullscale()> with the resulting image being scaled down to 1 pixel width per | ||
| 45 | 1 character column. Options specific to this method are: | ||
| 46 | |||
| 47 | =over 4 | ||
| 48 | |||
| 49 | =item * zoom - a zoom factor for the thumbnail (default: n/a) | ||
| 50 | |||
| 51 | =back | ||
| 52 | |||
| 53 | See C<fullscale> for all of the other available options. | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub thumbnail { | ||
| 58 | my ( $self, $source, $options ) = @_; | ||
| 59 | |||
| 60 | $options = { %{ $source->render_options }, $options ? %$options : () }; | ||
| 61 | |||
| 62 | if ( $source->can( 'frames' ) ) { | ||
| 63 | return $self->_render_animated_thumbnail( $source, $options ); | ||
| 64 | } | ||
| 65 | |||
| 66 | my $image_l = do { | ||
| 67 | local $options->{ format } = 'object'; ## no critic (Variables::ProhibitLocalVar) | ||
| 68 | $self->fullscale( $source, $options ); | ||
| 69 | }; | ||
| 70 | |||
| 71 | my ( $width, $height ) = _thumbnail_size( $source, $image_l, $options ); | ||
| 72 | |||
| 73 | my $image = GD::Image->new( $width, $height, 1 ); | ||
| 74 | $image->copyResampled( $image_l, 0, 0, 0, 0, $width, $height, | ||
| 75 | $image_l->getBounds ); | ||
| 76 | |||
| 77 | my $output = $options->{ format } || 'png'; | ||
| 78 | |||
| 79 | return $image if $output eq 'object'; | ||
| 80 | return $image->$output; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _render_animated_thumbnail { | ||
| 84 | my ( $self, $source, $options ) = @_; | ||
| 85 | my @frames = @{ $source->frames }; | ||
| 86 | $options->{ format } = 'object'; | ||
| 87 | $self->_prepare_options( $source, $options ); | ||
| 88 | my $frame = $self->_render_frame( $frames[ 0 ], $options ); | ||
| 89 | my ( $width, $height ) = _thumbnail_size( $source, $frame, $options ); | ||
| 90 | my $master = GD::Image->new( $width, $height, 1 ); | ||
| 91 | |||
| 92 | $master->copyResampled( $frame, 0, 0, 0, 0, $width, $height, | ||
| 93 | $frame->getBounds ); | ||
| 94 | my $output = $master->gifanimbegin( 0 ); | ||
| 95 | $output .= $master->gifanimadd( 1, 0, 0, 15, 1 ); | ||
| 96 | |||
| 97 | shift @frames; | ||
| 98 | for my $canvas ( @frames ) { | ||
| 99 | $frame = $self->_render_frame( $canvas, $options ); | ||
| 100 | $master->copyResampled( $frame, 0, 0, 0, 0, $width, $height, | ||
| 101 | $frame->getBounds ); | ||
| 102 | $output .= $master->gifanimadd( 1, 0, 0, 15, 1 ); | ||
| 103 | } | ||
| 104 | |||
| 105 | $output .= $master->gifanimend; | ||
| 106 | |||
| 107 | return $output; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub _thumbnail_size { | ||
| 111 | my ( $source, $image, $options ) = @_; | ||
| 112 | |||
| 113 | my ( $width, $height ) = $source->dimensions; | ||
| 114 | $height = $image->height / int( $image->width / $width + 0.5 ); | ||
| 115 | |||
| 116 | if ( my $zoom = $options->{ zoom } ) { | ||
| 117 | $width *= $zoom; | ||
| 118 | $height *= $zoom; | ||
| 119 | } | ||
| 120 | |||
| 121 | return ( $width, $height ); | ||
| 122 | } | ||
| 123 | |||
| 124 | =head2 fullscale( $source, \%options ) | ||
| 125 | |||
| 126 | Renders a pixel-by-pixel representation of the text mode image. You may use the | ||
| 127 | following options to change the output: | ||
| 128 | |||
| 129 | =over 4 | ||
| 130 | |||
| 131 | =item * crop - limit the display to this many rows (default: no limit) | ||
| 132 | |||
| 133 | =item * blink_mode - when false, use the 8th bit of an attribute byte as part of the background color (aka iCEColor) (default: differs by format) | ||
| 134 | |||
| 135 | =item * truecolor - set this to true to enable true color image output (default: false) | ||
| 136 | |||
| 137 | =item * 9th_bit - compatibility option to enable a ninth column in the font (default: false) | ||
| 138 | |||
| 139 | =item * dos_apsect - emulate the aspect ratio from DOS (default: false) | ||
| 140 | |||
| 141 | =item * ced - CED mode (black text on gray background) (default: false) | ||
| 142 | |||
| 143 | =item * font - override the image font; either a font object, or the last part of the clas name (e.g. 8x8) | ||
| 144 | |||
| 145 | =item * palette - override the image palette; either a palette object, or the last part of the class name (e.g. VGA) | ||
| 146 | |||
| 147 | =item * format - the output format (default: png) | ||
| 148 | |||
| 149 | =back | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | sub fullscale { | ||
| 154 | my ( $self, $source, $options ) = @_; | ||
| 155 | |||
| 156 | $options = { %{ $source->render_options }, $options ? %$options : () }; | ||
| 157 | |||
| 158 | $self->_prepare_options( $source, $options ); | ||
| 159 | |||
| 160 | if ( $source->can( 'frames' ) ) { | ||
| 161 | $options->{ format } = 'object'; | ||
| 162 | my $master = GD::Image->new( @{ $options->{ full_dimensions } } ); | ||
| 163 | my $output = $master->gifanimbegin( 0 ); | ||
| 164 | for my $canvas ( @{ $source->frames } ) { | ||
| 165 | my $obj = $self->_render_frame( $canvas, $options ); | ||
| 166 | |||
| 167 | $output .= $obj->gifanimadd( 1, 0, 0, 15, 1 ); | ||
| 168 | } | ||
| 169 | $output .= $master->gifanimend; | ||
| 170 | return $output; | ||
| 171 | } | ||
| 172 | |||
| 173 | return $self->_render_frame( $source, $options ); | ||
| 174 | } | ||
| 175 | |||
| 176 | sub _prepare_options { | ||
| 177 | my ( $self, $source, $options ) = @_; | ||
| 178 | |||
| 179 | $options->{ font } ||= $source->font; | ||
| 180 | $options->{ palette } ||= $source->palette; | ||
| 181 | $options->{ palette } = Image::TextMode::Palette::ANSI->new | ||
| 182 | if $options->{ ced }; | ||
| 183 | |||
| 184 | for ( qw( font palette ) ) { | ||
| 185 | next if ref $options->{ $_ }; | ||
| 186 | |||
| 187 | my $class | ||
| 188 | = 'Image::TextMode::' . ucfirst( $_ ) . q(::) . $options->{ $_ }; | ||
| 189 | |||
| 190 | if ( !eval { Module::Runtime::require_module( $class ) } ) { | ||
| 191 | croak sprintf( "Unable to load ${_} '%s'", $options->{ $_ } ); | ||
| 192 | } | ||
| 193 | |||
| 194 | $options->{ $_ } = $class->new; | ||
| 195 | |||
| 196 | } | ||
| 197 | |||
| 198 | $options->{ font } = _font_to_gd( $options->{ font }, | ||
| 199 | { '9th_bit' => $options->{ '9th_bit' } } ); | ||
| 200 | |||
| 201 | $options->{ truecolor } ||= 0; | ||
| 202 | $options->{ format } ||= 'png'; | ||
| 203 | |||
| 204 | my ( $width, $height ) = $source->dimensions; | ||
| 205 | $height = $options->{ crop } if $options->{ crop }; | ||
| 206 | $options->{ full_dimensions } = [ | ||
| 207 | $width * $options->{ font }->width, | ||
| 208 | $height * $options->{ font }->height | ||
| 209 | ]; | ||
| 210 | |||
| 211 | if ( $options->{ dos_aspect } ) { | ||
| 212 | my $ratio = $options->{ '9th_bit' } ? 1.35 : 1.2; | ||
| 213 | $options->{ full_dimensions }->[ 1 ] | ||
| 214 | = int( $options->{ full_dimensions }->[ 1 ] * $ratio + 0.5 ); | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | sub _render_frame { | ||
| 219 | my ( $self, $canvas, $options ) = @_; | ||
| 220 | |||
| 221 | my ( $width, $height ) = $canvas->dimensions; | ||
| 222 | $height = $options->{ crop } if $options->{ crop }; | ||
| 223 | |||
| 224 | my $font = $options->{ font }; | ||
| 225 | my $ftwidth = $font->width; | ||
| 226 | my $ftheight = $font->height; | ||
| 227 | my $ced = $options->{ ced }; | ||
| 228 | |||
| 229 | my $image = GD::Image->new( @{ $options->{ full_dimensions } }, | ||
| 230 | $options->{ truecolor } ); | ||
| 231 | |||
| 232 | my $colors = _fill_gd_palette( $options->{ palette }, $image ); | ||
| 233 | |||
| 234 | $image->fill( 0, 0, 7 ) if $ced; | ||
| 235 | |||
| 236 | for my $y ( 0 .. $height - 1 ) { | ||
| 237 | for my $x ( 0 .. $width - 1 ) { | ||
| 238 | my $pixel = $canvas->getpixel_obj( $x, $y, $options ); | ||
| 239 | |||
| 240 | next unless $pixel; | ||
| 241 | |||
| 242 | if ( defined $pixel->bg ) { | ||
| 243 | $image->filledRectangle( | ||
| 244 | $x * $ftwidth, | ||
| 245 | $y * $ftheight, | ||
| 246 | ( $x + 1 ) * $ftwidth - 1, | ||
| 247 | ( $y + 1 ) * $ftheight - 1, | ||
| 248 | $colors->[ $ced ? 7 : $pixel->bg ] | ||
| 249 | ); | ||
| 250 | } | ||
| 251 | |||
| 252 | $image->string( | ||
| 253 | $font, | ||
| 254 | $x * $ftwidth, | ||
| 255 | $y * $ftheight, | ||
| 256 | $pixel->char, $colors->[ $ced ? 0 : $pixel->fg ] | ||
| 257 | ); | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | if ( $options->{ dos_aspect } ) { | ||
| 262 | my $resized = GD::Image->new( $image->getBounds, 1 ); | ||
| 263 | $resized->copyResampled( $image, 0, 0, 0, 0, | ||
| 264 | @{ $options->{ full_dimensions } }, | ||
| 265 | $image->width, $height * $ftheight ); | ||
| 266 | $image = $resized; | ||
| 267 | } | ||
| 268 | |||
| 269 | my $output = $options->{ format }; | ||
| 270 | |||
| 271 | return $image if $output eq 'object'; | ||
| 272 | return $image->$output; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub _font_to_gd { | ||
| 276 | my ( $font, $options ) = @_; | ||
| 277 | my $ninth = $options->{ '9th_bit' }; | ||
| 278 | my $name = lc( ( split( /\::/s, ref $font ) )[ -1 ] ); | ||
| 279 | |||
| 280 | if (my $fn = eval { | ||
| 281 | File::ShareDir::dist_file( 'Image-TextMode', | ||
| 282 | $name . ( $ninth ? '_9b' : '' ) . '.fnt' ); | ||
| 283 | } | ||
| 284 | ) | ||
| 285 | { | ||
| 286 | return GD::Font->load( $fn ); | ||
| 287 | } | ||
| 288 | |||
| 289 | require File::Temp; | ||
| 290 | my $temp = File::Temp->new; | ||
| 291 | _save_gd_font( $font, $options, $temp ); | ||
| 292 | close $temp or croak "Unable to close temp file: $!"; | ||
| 293 | |||
| 294 | return GD::Font->load( $temp->filename ); | ||
| 295 | } | ||
| 296 | |||
| 297 | sub _save_gd_font { | ||
| 298 | my ( $font, $options, $fh ) = @_; | ||
| 299 | |||
| 300 | binmode( $fh ); | ||
| 301 | |||
| 302 | my $ninth = $options->{ '9th_bit' }; | ||
| 303 | my $chars = $font->chars; | ||
| 304 | my $font_size = @$chars; | ||
| 305 | |||
| 306 | print $fh pack( 'VVVV', | ||
| 307 | $font_size, 0, $font->width + ( $ninth ? 1 : 0 ), | ||
| 308 | $font->height ); | ||
| 309 | |||
| 310 | for my $charval ( 0 .. $font_size ) { | ||
| 311 | my $char = $chars->[ $charval ]; | ||
| 312 | for ( @$char ) { | ||
| 313 | my @binary = split( //s, sprintf( '%08b', $_ ) ); | ||
| 314 | |||
| 315 | if ( $ninth ) { | ||
| 316 | push @binary, | ||
| 317 | ( $charval >= 0xc0 | ||
| 318 | and $charval <= 0xdf ? $binary[ -1 ] : 0 ); | ||
| 319 | } | ||
| 320 | |||
| 321 | print $fh pack( 'C*', @binary ); | ||
| 322 | } | ||
| 323 | } | ||
| 324 | } | ||
| 325 | |||
| 326 | sub _fill_gd_palette { | ||
| 327 | my ( $palette, $image ) = @_; | ||
| 328 | my @allocations | ||
| 329 | = map { $image->colorAllocate( @$_ ) } @{ $palette->colors }; | ||
| 330 | return \@allocations; | ||
| 331 | } | ||
| 332 | |||
| 333 | =head1 AUTHOR | ||
| 334 | |||
| 335 | Brian Cassidy E<lt>bricas@cpan.orgE<gt> | ||
| 336 | |||
| 337 | =head1 COPYRIGHT AND LICENSE | ||
| 338 | |||
| 339 | Copyright 2008-2014 by Brian Cassidy | ||
| 340 | |||
| 341 | This library is free software; you can redistribute it and/or modify | ||
| 342 | it under the same terms as Perl itself. | ||
| 343 | |||
| 344 | =cut | ||
| 345 | |||
| 346 | 1; |