/usr/share/doc/ruby-gd/examples/webpng.rb is in ruby-gd 0.8.0-7build5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #! /usr/bin/env ruby
=begin
webpng.rb -
=end
require 'getopts'
require 'GD'
def usage
$stderr.print <<EOS
Usage: webpng.rb [-i y|n ] [-l] [-t index|none ] [-d] pngname.png
-i [y|n] Turns on/off interlace
-l Prints the table of color indexes
-t [index] Set the transparent color to the specified index (0-255 or "none")
-d Reports the dimensions and other characteristics of the image.
If you specify '-' as the input file, stdin/stdout will be used input/output.
(e.g. cat demoin.png | ./webpng.rb -i y - > demoout.png)
EOS
exit 1
end
usage if ARGV.size < 2
if ARGV[-1] == '-'
useStdinStdout = true
end
# option parse
usage unless getopts("ld", "i:", "t:", "help")
usage if $OPT_help
if $OPT_i
usage unless $OPT_i == "y" or $OPT_i == "n"
end
if $OPT_t
usage unless $OPT_t == "none" or
($OPT_t.to_i >= 0 and $OPT_t.to_i <= 255)
end
if useStdinStdout
in_file = $stdin
else
in_fname = ARGV.pop
in_file = File.open(in_fname, "rb")
end
im = GD::Image.newFromPng(in_file)
in_file.close
# options for information displaying and exiting
if $OPT_l
printf("Index\tRed\tGreen\tBlue\n")
for j in 0 .. im.colorsTotal
rgb_ary = im.rgb(j)
printf("%d\t%d\t%d\t%d\n", j, rgb_ary[0], rgb_ary[1], rgb_ary[2])
end
im.destroy
exit 0
end
if $OPT_d
printf("Width: %d Height: %d Colors: %d\n",im.width, im.height, im.colorsTotal)
t = im.getTransparent
if t != -1
printf("Transparent index: %d\n", t)
else
puts "Transparent index: none"; # -1 means the image is not transparent.
end
if im.interlace
puts "Interlaced: yes";
else
puts "Interlaced: no";
end
im.destroy
exit 0
end
# options for involving for output part
if $OPT_i
if $OPT_i == 'y'
im.interlace = true # Set interlace
else
im.interlace = false # Clear interlace
end
end
if $OPT_t
if $OPT_t == 'none'
im.transparent(-1) # -1 means not transparent
else
im.transparent($OPT_t.to_i) # OK, get an integer and set the index
end
end
if useStdinStdout
im.png $stdout
else
o_fname = "webpng.tmp"+$$.to_s
ofile = open(o_fname, "wb")
im.png ofile
ofile.close
File.delete in_fname
File.rename o_fname, in_fname
end
im.destroy
|