File: Image_pm.PL

package info (click to toggle)
libgd-perl 2.83-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,764 kB
  • sloc: perl: 2,639; pascal: 336; sh: 18; makefile: 9
file content (447 lines) | stat: -rw-r--r-- 11,573 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!perl
use Config;
use File::Basename qw(&basename &dirname);
use Cwd;

my $DEFINES = '';
my $VERSION = '';
if (open F,".config.cache") {
  chomp($DEFINES = <F>);
  close F;
}

my $origdir = cwd;
chdir dirname($0);
my $file = 'Image.pm';

open OUT,">",$file or die "Can't create $file: $!";

print "Extracting $file (with variable substitutions)\n";

print OUT <<"!GROK!THIS!";
# DO NOT EDIT! THIS FILE IS AUTOGENERATED BY $0
!GROK!THIS!

print OUT << '!NO!SUBS!';
package GD::Image;

use strict;
use GD;
use Symbol 'gensym','qualify_to_ref';
use vars '$VERSION';
$VERSION = '2.83';

=head1 NAME

GD::Image - Image class for the GD image library

=head1 SYNOPSIS

See L<GD>

=head1 DESCRIPTION

Supported Image formats:

=over 4

!NO!SUBS!

print OUT "=item Png\n\n" if $DEFINES =~ /HAVE_PNG/;
print OUT "=item Gif\n\n" if $DEFINES =~ /HAVE_GIF/;
print OUT "=item Gd\n\n=item Gd2\n\n" if $DEFINES =~ /HAVE_GD2/;
print OUT "=item Jpeg\n\n" if $DEFINES =~ /HAVE_JPEG/;
print OUT "=item Tiff\n\n" if $DEFINES =~ /HAVE_TIFF/;
print OUT "=item Xpm\n\n" if $DEFINES =~ /HAVE_XPM/;
print OUT "=item Xbm\n\n" if 1 or $DEFINES =~ /HAVE_XBM/;
print OUT "=item WBMP\n\n" if 1 or $DEFINES =~ /HAVE_WBMP/;
print OUT "=item BMP\n\n" if $DEFINES =~ /HAVE_BMP/;
print OUT "=item GifAnim\n\n" if $DEFINES =~ /HAVE_GIFANIM/;
print OUT "=item Webp\n\n" if $DEFINES =~ /HAVE_WEBP/;
print OUT "=item Heif\n\n" if $DEFINES =~ /HAVE_HEIF/;
print OUT "=item Avif\n\n" if $DEFINES =~ /HAVE_AVIF/;

print OUT << '!NO!SUBS!';
=back

Unsupported Image formats:

=over 4

!NO!SUBS!

print OUT "=item Png\n\n" if $DEFINES !~ /HAVE_PNG/;
print OUT "=item Gif\n\n" if $DEFINES !~ /HAVE_GIF/;
print OUT "=item Gd\n\n=item Gd2\n\n" if $DEFINES !~ /HAVE_GD2/;
print OUT "=item Jpeg\n\n" if $DEFINES !~ /HAVE_JPEG/;
print OUT "=item Tiff\n\n" if $DEFINES !~ /HAVE_TIFF/;
print OUT "=item Xpm\n\n" if $DEFINES !~ /HAVE_XPM/;
print OUT "=item GifAnim\n\n" if $DEFINES !~ /HAVE_GIFANIM/;
print OUT "=item Webp\n\n" if $DEFINES !~ /HAVE_WEBP/;
print OUT "=item Heif\n\n" if $DEFINES !~ /HAVE_HEIF/;
print OUT "=item Avif\n\n" if $DEFINES !~ /HAVE_AVIF/;
print OUT "=item BMP\n\n" if $DEFINES !~ /HAVE_BMP/;

print OUT << '!NO!SUBS!';
=back

See L<GD>

=head1 AUTHOR

The GD.pm interface is copyright 1995-2005, Lincoln D. Stein.  It is
distributed under the same terms as Perl itself.  See the "Artistic
License" in the Perl source code distribution for licensing terms.

The latest versions of GD.pm are available on CPAN:

  http://www.cpan.org

=head1 SEE ALSO

L<GD>
L<GD::Polyline>,
L<GD::SVG>,
L<GD::Simple>,
L<Image::Magick>

=cut

# Copyright 1995 Lincoln D. Stein.  See accompanying README file for
# usage information

*stringTTF = \&GD::Image::stringFT;

sub _make_filehandle {
  shift;  # get rid of class
  no strict 'refs';
  my $thing = shift;
  return $thing if defined(fileno $thing);

  # otherwise try qualifying it into caller's package
  my $fh;
  {   
    local $^W = 0;  # to avoid uninitialized variable warning from Symbol.pm
    my $pkg = caller(2);
    $pkg = "main" unless defined $pkg;;
    $fh = qualify_to_ref($thing,$pkg);
  }
  return $fh if defined(fileno $fh);

  # otherwise treat it as a file to open
  $fh = gensym;
  if (!open($fh,$thing)) {
    die "$thing not found: $!";
    return undef;
  }
  return $fh;
}

sub new {
  my $pack = shift;
  if (@_ == 1) {
    if (my $type = _image_type($_[0])) {
      my $method = "newFrom${type}Data";
      return unless $pack->can($method);
      return $pack->$method($_[0]);
    } elsif (-f $_[0] and $_[0] =~ /\.gd$/) {
      my $type = 'Gd';
      return unless my $fh = $pack->_make_filehandle($_[0]);
      my $method = "newFrom${type}";
      return unless $pack->can($method);
      return $pack->$method($fh);
    } elsif (-f $_[0] and $_[0] =~ /\.gd2$/) {
      my $type = 'Gd2';
      return unless my $fh = $pack->_make_filehandle($_[0]);
      my $method = "newFrom${type}";
      return unless $pack->can($method);
      return $pack->$method($fh);
    } elsif (-f $_[0] and $_[0] =~ /\.wbmp$/) {
      my $type = 'WBMP';
      return unless my $fh = $pack->_make_filehandle($_[0]);
      my $method = "newFrom${type}";
      return unless $pack->can($method);
      return $pack->$method($fh);
    } elsif (-f $_[0] and $_[0] =~ /\.xpm$/) {
      my $type = 'Xpm';
      my $method = "newFrom${type}";
      return unless $pack->can($method);
      return $pack->$method($_[0]);
    }
    return unless my $fh = $pack->_make_filehandle($_[0]);
    my $magic;
    return unless read($fh,$magic,64);
    return unless my $type = _image_type($magic);
    seek($fh,0,0);
    my $method = "newFrom${type}";
    if ($type eq 'Xpm') {
      return $pack->$method($_[0]);
    } else {
      return $pack->$method($fh);
    }
  }
  return $pack->_new(@_);
}

sub newTrueColor {
  my $pack = shift;
  return $pack->_new(@_, 1);
}

sub newPalette {
  my $pack = shift;
  return $pack->_new(@_, 0);
}

sub ellipse ($$$$$) {
  my ($self,$cx,$cy,$width,$height,$color) = @_;
  $self->arc($cx,$cy,$width,$height,0,360,$color);
}

# draws closed polygon with the specified color
sub polygon {
    my $self = shift;
    my($p,$c) = @_;
    $self->openPolygon($p, $c);
    $self->line( @{$p->{'points'}->[0]},
	    @{$p->{'points'}->[$p->{'length'}-1]}, $c);
}

sub width {
  my $self = shift;
  my @bounds = $self->getBounds;
  $bounds[0];
}

sub height {
  my $self = shift;
  my @bounds = $self->getBounds;
  $bounds[1];
}

sub _image_type {
  my $data = shift;
  my $magic = substr($data,0,4);
  return 'Png'  if $magic eq "\x89PNG";
  return 'Jpeg' if ((substr($data,0,3) eq "\377\330\377") &&
    ord(substr($data,3,1)) >= 0xc0);
  return 'Gif'  if $magic eq "GIF8";
  return 'Gd2'  if $magic eq "gd2\000";
  return 'Tiff' if $magic eq "\x4d\x4d\x00\x2a" or
    $magic eq "\x49\x49\x2a\x00" or
    $magic eq "IIN1";
  return 'Bmp' if $magic eq "BMF\000";
  return 'Webp' if $magic eq "RIFF" and substr($data,8,4) eq "WEBP";
  if (substr($data,4,4) eq "ftyp") { #possibly ISOBMFF-compliant container like HEIF which us used for AVIF and HEIC
    #first 4 bytes (they are now in $magic) must contain 32-bit Big Endian size of the 'ftyp' box (including size field and 'ftyp' mark)
    my $boxsize = unpack("N", $magic);
    if($boxsize>=16 && ($boxsize & 0x3)==0) { #minimum size of 'ftyp' box is 16 bytes and it must be multiple of 4
      #Structure of 'ftyp' box (from offset 8):
      #  uint32 major_brand;
      #  uint32 minor_version;
      #  uint32 compatible_brands[]; to end of the box
      my $brand = substr($data,8,4); #major_brand
      my %compat;
      if($boxsize>16) { #compatible_brands list is not empty
        %compat = map {$_=>1} unpack("(A4)*", substr($data,16,$boxsize-16));
      }
      return 'Avif' if $brand eq 'avif' || $compat{'avif'};
        #Consider recognizing 'avis' brand meaning AV1 image sequence

      return 'Heif' if $brand eq 'mif1' || $brand eq 'heic' || $brand eq 'heix' || $compat{'heic'} || $compat{'heix'} || $compat{'mif1'};
        #'mif1' stands for 'Multiple Image Format' and is general for the HEIF image container with any codec
        #'heic' indicates that HEVC Main Profile is utilized
        #'heix' indicates that HEVC Main 10 profile is utilized
        #Consider recognizing:
        #  'msf1' brand meaning 'Multiple Sequence Format' for general image sequence in HEIF
        #  'hevc' brand for HEVC Main Profile sequence
        #  'hevx' brand for HEVC Main 10 Profile sequence
    }
  }
  return 'Xpm'  if substr($data,0,9) eq "/* XPM */";
  return 'Xbm'  if substr($data,0,8) eq "#define ";
  return;
}


sub clone {
  croak("Usage: clone(\$image)") unless @_ == 1;
  my $self = shift;
  my ($x,$y) = $self->getBounds;
  my $new = $self->new($x,$y);
  return unless $new;
  $new->copy($self,0,0,0,0,$x,$y);
  return $new;
}

!NO!SUBS!

if ($DEFINES =~ /HAVE_PNG/) {
  print OUT <<'!NO!SUBS!'
sub newFromPng {
    croak("Usage: newFromPng(class,filehandle,[truecolor])") unless @_>=2;
    my($class) = shift;
    my($f)     = shift;
    my $fh = $class->_make_filehandle($f);
    binmode($fh);
    $class->_newFromPng($fh,@_);
}

!NO!SUBS!
}

if ($DEFINES =~ /HAVE_GD2/) {
  print OUT <<'!NO!SUBS!'
sub newFromGd {
    croak("Usage: newFromGd(class,filehandle)") unless @_==2;
    my($class,$f) = @_;
    my $fh = $class->_make_filehandle($f);
    binmode($fh);
    $class->_newFromGd($fh);
}

sub newFromGd2 {
    croak("Usage: newFromGd2(class,filehandle)") unless @_==2;
    my($class,$f) = @_;
    my $fh = $class->_make_filehandle($f);
    binmode($fh);
    $class->_newFromGd2($fh);
}

sub newFromGd2Part {
    croak("Usage: newFromGd2(class,filehandle,srcX,srcY,width,height)") unless @_==6;
    my($class,$f) = splice(@_,0,2);
    my $fh = $class->_make_filehandle($f);
    binmode($fh);
    $class->_newFromGd2Part($fh,@_);
}
!NO!SUBS!
}

if ($DEFINES =~ /HAVE_JPEG/) {
  print OUT <<'!NO!SUBS!'
sub newFromJpeg {
    croak("Usage: newFromJpeg(class,filehandle,[truecolor])") unless @_>=2;
    my($class) = shift;
    my($f)     = shift;
    my $fh = $class->_make_filehandle($f);
    binmode($fh);
    $class->_newFromJpeg($fh,@_);
}

!NO!SUBS!
}

if ($DEFINES =~ /HAVE_GIF/) {
  print OUT <<'!NO!SUBS!'
sub newFromGif {
    croak("Usage: newFromGif(class,filehandle)") unless @_==2;
    my($class) = shift;
    my($f)     = shift;
    my $fh = $class->_make_filehandle($f);
    binmode($fh);
    $class->_newFromGif($fh,@_);
}

!NO!SUBS!
}

if ($DEFINES =~ /HAVE_TIFF/) {
  print OUT <<'!NO!SUBS!'
sub newFromTiff {
    croak("Usage: newFromTiff(class,filehandle)") unless @_==2;
    my($class,$f) = @_;
    my $fh = $class->_make_filehandle($f);
    binmode($fh);
    $class->_newFromTiff($fh);
}

!NO!SUBS!
}

print OUT <<'!NO!SUBS!';
sub newFromXbm {
    croak("Usage: newFromXbm(class,filehandle)") unless @_==2;
    my($class,$f) = @_;
    my $fh = $class->_make_filehandle($f);
    binmode($fh);
    $class->_newFromXbm($fh);
}

!NO!SUBS!

if ($DEFINES =~ /HAVE_WEBP/) {
  print OUT <<'!NO!SUBS!'
sub newFromWebp {
    croak("Usage: newFromWebp(class,filehandle)") unless @_==2;
    my($class,$f) = @_;
    my $fh = $class->_make_filehandle($f);
    binmode($fh);
    $class->_newFromWebp($fh);
}

!NO!SUBS!
}

if ($DEFINES =~ /HAVE_HEIF/) {
  print OUT <<'!NO!SUBS!'
sub newFromHeif {
    croak("Usage: newFromHeif(class,filehandle)") unless @_==2;
    my($class,$f) = @_;
    my $fh = $class->_make_filehandle($f);
    binmode($fh);
    $class->_newFromHeif($fh);
}

!NO!SUBS!
}

if ($DEFINES =~ /HAVE_AVIF/) {
  print OUT <<'!NO!SUBS!'
sub newFromAvif {
    croak("Usage: newFromAvif(class,filehandle)") unless @_==2;
    my($class,$f) = @_;
    my $fh = $class->_make_filehandle($f);
    binmode($fh);
    $class->_newFromAvif($fh);
}

!NO!SUBS!
}

if (1 or $DEFINES =~ /HAVE_WBMP/) {
  print OUT <<'!NO!SUBS!';
sub newFromWBMP {
    croak("Usage: newFromWBMP(class,filehandle)") unless @_==2;
    my($class) = shift;
    my($f)     = shift;
    my $fh = $class->_make_filehandle($f);
    binmode($fh);
    $class->_newFromWBMP($fh,@_);
}

!NO!SUBS!
}

if ($DEFINES =~ /HAVE_BMP/) {
  print OUT <<'!NO!SUBS!';
sub newFromBmp {
    croak("Usage: newFromBmp(class,filehandle)") unless @_==2;
    my($class) = shift;
    my($f)     = shift;
    my $fh = $class->_make_filehandle($f);
    binmode($fh);
    $class->_newFromBmp($fh,@_);
}

!NO!SUBS!
}

print OUT <<'!NO!SUBS!';
# Autoload methods go after __END__, and are processed by the autosplit program.
1;
__END__
!NO!SUBS!

close OUT or die "Can't close $file: $!";
chdir $origdir;