Class: PNM::Image
- Inherits:
-
Object
- Object
- PNM::Image
- Defined in:
- lib/pnm/image.rb
Overview
Class for PBM
, PGM
, and PPM
images. See PNM module for examples.
Instance Attribute Summary collapse
-
#comment ⇒ Object
readonly
An optional multiline comment string.
-
#height ⇒ Object
readonly
The height of the image in pixels.
-
#maxgray ⇒ Object
readonly
The maximum gray or color value (for PBM always set to 1).
-
#pixels ⇒ Object
readonly
The pixel data, given as a two-dimensional array.
-
#type ⇒ Object
readonly
The type of the image.
-
#width ⇒ Object
readonly
The width of the image in pixels.
Instance Method Summary collapse
-
#info ⇒ Object
(also: #to_s)
Returns a string with a short image format description.
-
#initialize(pixels, options = {}) ⇒ Image
constructor
Creates an image from a two-dimensional array of bilevel, gray, or RGB values.
-
#write(file, encoding = :binary) ⇒ Object
Writes the image to
file
(a filename or an IO object), using the specified encoding.
Constructor Details
#initialize(pixels, options = {}) ⇒ Image
Creates an image from a two-dimensional array of bilevel, gray, or RGB values.
pixels
-
The pixel data, given as a two-dimensional array of
-
for PBM: bilevel values (0 or 1),
-
for PGM: gray values between 0 and
maxgray
, -
for PPM: an array of 3 values between 0 and
maxgray
, corresponding to red, green, and blue (RGB).
PPM also accepts an array of bilevel or gray values.
A value of 0 means that the color is turned off.
-
Optional settings that can be specified in the options
hash:
type
-
The type of the image (
:pbm
,:pgm
, or:ppm
). By default, the type is guessed from the provided pixel data, unless this option is used. maxgray
-
The maximum gray or color value. For PGM and PPM,
maxgray
must be less or equal 255 (the default value). For PBM pixel data, settingmaxgray
implies a conversion to:pgm
. Iftype
is explicitly set to:pbm
, themaxgray
setting is disregarded. comment
-
A multiline comment string (default: empty string).
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/pnm/image.rb', line 52 def initialize(pixels, = {}) @type = [:type] @width = pixels.first.size @height = pixels.size @maxgray = [:maxgray] @comment = ([:comment] || '').chomp @pixels = pixels @type ||= detect_type(@pixels, @maxgray) if @type == :pbm @maxgray = 1 else @maxgray ||= 255 end if type == :ppm && !pixels.first.first.kind_of?(Array) @pixels = pixels.map {|row| row.map {|pixel| gray_to_rgb(pixel) } } end end |
Instance Attribute Details
#comment ⇒ Object (readonly)
An optional multiline comment string.
24 25 26 |
# File 'lib/pnm/image.rb', line 24 def comment @comment end |
#height ⇒ Object (readonly)
The height of the image in pixels.
13 14 15 |
# File 'lib/pnm/image.rb', line 13 def height @height end |
#maxgray ⇒ Object (readonly)
The maximum gray or color value (for PBM always set to 1). See ::new for details.
17 18 19 |
# File 'lib/pnm/image.rb', line 17 def maxgray @maxgray end |
#pixels ⇒ Object (readonly)
The pixel data, given as a two-dimensional array. See ::new for details.
21 22 23 |
# File 'lib/pnm/image.rb', line 21 def pixels @pixels end |
#type ⇒ Object (readonly)
The type of the image. See ::new for details.
7 8 9 |
# File 'lib/pnm/image.rb', line 7 def type @type end |
#width ⇒ Object (readonly)
The width of the image in pixels.
10 11 12 |
# File 'lib/pnm/image.rb', line 10 def width @width end |
Instance Method Details
#info ⇒ Object Also known as: to_s
Returns a string with a short image format description.
94 95 96 |
# File 'lib/pnm/image.rb', line 94 def info "#{type.to_s.upcase} #{width}x#{height} #{type_string}" end |
#write(file, encoding = :binary) ⇒ Object
Writes the image to file
(a filename or an IO object), using the specified encoding. Valid encodings are :binary
(default) and :ascii
.
Returns the number of bytes written.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/pnm/image.rb', line 78 def write(file, encoding = :binary) content = if encoding == :ascii to_ascii elsif encoding == :binary to_binary end if file.kind_of?(String) File.binwrite(file, content) else file.binmode file.write content end end |