Class: PNM::Image
- Inherits:
-
Object
- Object
- PNM::Image
- Defined in:
- lib/pnm/image.rb
Overview
Abstract base class for PBM
, PGM
, and PPM
images.
Images can be created from pixel values, see PNM.create, or read from a file or I/O stream, see PNM.read.
See PNM module for examples.
Instance Attribute Summary collapse
-
#comment ⇒ Object
readonly
An optional multiline comment string (or
nil
). -
#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.
-
#width ⇒ Object
readonly
The width of the image in pixels.
Class Method Summary collapse
-
.create(pixels, options = {}) ⇒ Object
Creates an image from a two-dimensional array of bilevel, gray, or RGB values.
Instance Method Summary collapse
-
#info ⇒ Object
(also: #to_s)
Returns a string with a short image format description.
-
#inspect ⇒ Object
Returns a string representation for debugging.
-
#type ⇒ Object
The type of the image.
-
#write(file, encoding = :binary) ⇒ Object
Writes the image to
file
(a filename or an IO object), using the specified encoding.
Instance Attribute Details
#comment ⇒ Object (readonly)
An optional multiline comment string (or nil
).
31 32 33 |
# File 'lib/pnm/image.rb', line 31 def comment @comment end |
#height ⇒ Object (readonly)
The height of the image in pixels.
20 21 22 |
# File 'lib/pnm/image.rb', line 20 def height @height end |
#maxgray ⇒ Object (readonly)
The maximum gray or color value (for PBM always set to 1). See PNM.create for details.
24 25 26 |
# File 'lib/pnm/image.rb', line 24 def maxgray @maxgray end |
#pixels ⇒ Object (readonly)
The pixel data, given as a two-dimensional array. See PNM.create for details.
28 29 30 |
# File 'lib/pnm/image.rb', line 28 def pixels @pixels end |
#width ⇒ Object (readonly)
The width of the image in pixels.
17 18 19 |
# File 'lib/pnm/image.rb', line 17 def width @width end |
Class Method Details
.create(pixels, options = {}) ⇒ Object
Creates an image from a two-dimensional array of bilevel, gray, or RGB values.
This method should be called as PNM.create. See there for a description of pixel data formats and available options.
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 |
# File 'lib/pnm/image.rb', line 39 def self.create(pixels, = {}) assert_valid_array(pixels) assert_valid_maxgray([:maxgray]) assert_valid_comment([:comment]) type = sanitize_and_assert_valid_type([:type]) type ||= detect_type(pixels, [:maxgray]) # except for type detection, the maxgray option must be ignored for PBM if type == :pbm maxgray = nil else maxgray = [:maxgray] end image_class = case type when :pbm PBMImage when :pgm PGMImage when :ppm PPMImage end image_class.new(pixels, maxgray, [:comment]) end |
Instance Method Details
#info ⇒ Object Also known as: to_s
Returns a string with a short image format description.
106 107 108 |
# File 'lib/pnm/image.rb', line 106 def info "#{type.to_s.upcase} #{width}x#{height} #{type_string}" end |
#inspect ⇒ Object
Returns a string representation for debugging.
113 114 115 |
# File 'lib/pnm/image.rb', line 113 def inspect # implemented by subclasses end |
#type ⇒ Object
The type of the image. See PNM.create for details.
12 13 14 |
# File 'lib/pnm/image.rb', line 12 def type # implemented by subclasses 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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/pnm/image.rb', line 90 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 |