Class: PNM::Image

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

PBMImage, PGMImage, PPMImage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commentObject (readonly)

An optional multiline comment string (or nil).



33
34
35
# File 'lib/pnm/image.rb', line 33

def comment
  @comment
end

#heightObject (readonly)

The height of the image in pixels.



22
23
24
# File 'lib/pnm/image.rb', line 22

def height
  @height
end

#maxgrayObject (readonly)

The maximum gray or color value (for PBM always set to 1). See PNM.create for details.



26
27
28
# File 'lib/pnm/image.rb', line 26

def maxgray
  @maxgray
end

#pixelsObject (readonly)

The pixel data, given as a two-dimensional array. See PNM.create for details.



30
31
32
# File 'lib/pnm/image.rb', line 30

def pixels
  @pixels
end

#widthObject (readonly)

The width of the image in pixels.



19
20
21
# File 'lib/pnm/image.rb', line 19

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.



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
# File 'lib/pnm/image.rb', line 41

def self.create(pixels, options = {})
  assert_valid_array(pixels)
  assert_valid_maxgray(options[:maxgray])
  assert_valid_comment(options[:comment])

  type = sanitize_and_assert_valid_type(options[:type])
  type ||= detect_type(pixels, options[:maxgray])

  # except for type detection, the maxgray option must be ignored for PBM
  maxgray = if type == :pbm
              nil
            else
              options[:maxgray]
            end

  image_class = case type
                when :pbm
                  PBMImage
                when :pgm
                  PGMImage
                when :ppm
                  PPMImage
                end

  image_class.new(pixels, maxgray, options[:comment])
end

Instance Method Details

#==(other) ⇒ Object

Equality — Two images are considered equal if they have the same pixel values, type, maxgray, and comments.



133
134
135
136
137
138
# File 'lib/pnm/image.rb', line 133

def ==(other)
  return true  if other.equal?(self)
  return false  unless other.instance_of?(self.class)

  type == other.type && maxgray == other.maxgray && comment == other.comment && pixels == other.pixels
end

#infoObject Also known as: to_s

Returns a string with a short image format description.



120
121
122
# File 'lib/pnm/image.rb', line 120

def info
  "#{type.to_s.upcase} #{width}x#{height} #{type_string}"
end

#inspectObject

Returns a string representation for debugging.



127
128
129
# File 'lib/pnm/image.rb', line 127

def inspect
  # implemented by subclasses
end

#typeObject

The type of the image. See PNM.create for details.



14
15
16
# File 'lib/pnm/image.rb', line 14

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.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/pnm/image.rb', line 95

def write(file, encoding = :binary)
  content = if encoding == :ascii
              to_ascii
            elsif encoding == :binary
              to_binary
            end

  if file.is_a?(String)
    File.binwrite(file, content)
  else
    file.binmode
    file.write content
  end
end

#write_with_extension(basename, *args) ⇒ Object

Adds the appropriate file extension to basename (.pbm, .pgm, or .ppm) and writes the image to the resulting filename.

Any options are passed on to #write, which is used internally.



115
116
117
# File 'lib/pnm/image.rb', line 115

def write_with_extension(basename, *args)
  write("#{basename}.#{type}", *args)
end