Class: Pruim::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/pruim/image.rb

Overview

An image consists of one or more pages. Whether a page is a layer, a frame in an animation or both, is determined by the properties of the page.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(w, h, extra = {}) ⇒ Image

Returns a new instance of Image.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pruim/image.rb', line 36

def initialize(w, h, extra = {})
  @w        = w
  @h        = h
  @info     = {}
  @palette  = extra[:palette]
  @mode     = extra[:mode]      
  @mode   ||= (@palette ? :palette : :rgba)
  @palette  = Palette.new if !@palette && @mode == :palette
  @depth    = extra[:depth]
  @depth  ||= (@palette ? 8 : 32)
  @pages    = [] 
  @ordered  = {} 
  @active   = nil
  # Construct many pages.
  if extra[:pages]
    data = extra[:data] || []
    extra[:pages].times { |i| self.new_page(@w, @h, :data => data[i]) }
  # Construct a single page if data is given nevertheless
  elsif extra[:data]
    self.new_page(@w, @h, :data => extra[:data])
  end
  # Set comments if any
  self.comment = extra[:comment]
end

Instance Attribute Details

#activeObject (readonly)

Currently “active” page.



13
14
15
# File 'lib/pruim/image.rb', line 13

def active
  @active
end

#depthObject (readonly)

Color depth, may be one of 1, 2, 4, 8, 16, 24, 32



17
18
19
# File 'lib/pruim/image.rb', line 17

def depth
  @depth
end

#hObject (readonly)

Returns the value of attribute h.



9
10
11
# File 'lib/pruim/image.rb', line 9

def h
  @h
end

#infoObject (readonly)

Extra information data hash table.



20
21
22
# File 'lib/pruim/image.rb', line 20

def info
  @info
end

#modeObject (readonly)

image mode, may be one of :monochrome, :palette, :grayscale, :rgba



15
16
17
# File 'lib/pruim/image.rb', line 15

def mode
  @mode
end

#pagesObject (readonly)

Returns the value of attribute pages.



11
12
13
# File 'lib/pruim/image.rb', line 11

def pages
  @pages
end

#paletteObject (readonly)

Returns the value of attribute palette.



10
11
12
# File 'lib/pruim/image.rb', line 10

def palette
  @palette
end

#wObject (readonly)

Returns the value of attribute w.



8
9
10
# File 'lib/pruim/image.rb', line 8

def w
  @w
end

Class Method Details

.depth_for_colors(ncolors) ⇒ Object



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

def self.depth_for_colors(ncolors)
  (Math.log(ncolors) / Math.log(2)).to_i
end

.load_from(filename, codecname = nil) ⇒ Object

Loads the image from the given filename using the given codec If codec is missig, it’s determined from the filename’s extension.



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/pruim/image.rb', line 122

def self.load_from(filename, codecname = nil)
  codec = Codec.new_for_filename_name(filename, codecname)
  return false unless codec
  io    = File.open(filename, 'rb+')
  res   = nil
  if codec.can_decode?(io)
    res   = codec.decode(io)
  else
    raise "Malformed file #{filename} cannot be decoded as a #{codec} file."
  end
  io.close
  return res
end

Instance Method Details

#activate(index) ⇒ Object

Sets the page with the given index as active if it exists.



66
67
68
# File 'lib/pruim/image.rb', line 66

def activate(index)
  @active = @pages[index]
end

#add_page(page) ⇒ Object

Adds a page to this image. The page is also set as the active page.



80
81
82
83
84
85
86
87
# File 'lib/pruim/image.rb', line 80

def add_page(page)
  @pages << page
  @ordered[page.frame]             = {} unless @ordered[page.frame]
  @ordered[page.frame][page.layer] = [] unless @ordered[page.frame][page.layer] 
  @ordered[page.frame][page.layer] << page
  @active = page
  return page
end

#commentObject

gets the comment for this image



32
33
34
# File 'lib/pruim/image.rb', line 32

def comment
  @info[:comment]
end

#comment=(comm) ⇒ Object

Sets the comment for this image



27
28
29
# File 'lib/pruim/image.rb', line 27

def comment=(comm)
  @info[:comment] = comm
end

#fill(color) ⇒ Object

Fills the current active page, if any.



105
106
107
# File 'lib/pruim/image.rb', line 105

def fill(color)
  @active.fill(color)
end

#getpixel(x, y) ⇒ Object

Gets a pixel from the current active page, if any.



95
96
97
# File 'lib/pruim/image.rb', line 95

def getpixel(x, y)
  @active.getpixel!(x, y)
end

#new_page(w = nil, h = nil, extra = {}) ⇒ Object

Create a new a page and adds it to this image. The page is also set as the active page.



72
73
74
75
76
77
# File 'lib/pruim/image.rb', line 72

def new_page(w = nil, h = nil, extra = {})
  w       ||= self.w
  h       ||= self.h
  page      = Page.new(self, w, h, extra)
  return self.add_page(page)
end

#new_rgb(r, g, b) ⇒ Object

Creates a new rgb color for use with this image. If the image is palleted, the color is added to the palette and the index is returned, otherwise the color is returned



139
140
141
142
143
144
145
# File 'lib/pruim/image.rb', line 139

def new_rgb(r, g, b)
  if palette?
    return @palette.new_rgb
  else
    return Color.rgb(r, g, b)
  end     
end

#new_rgba(r, g, b, a) ⇒ Object

Creates a new rgba color for use with this image. If the image is palleted, the color is added to the palette and the index is returned, otherwise the color is returned



150
151
152
153
154
155
156
# File 'lib/pruim/image.rb', line 150

def new_rgba(r, g, b, a)
  if palette?
    return @palette.new_rgba
  else
    return Color.rgba(r, g, b, a)
  end
end

#pages_at(frame = 0, layer = 0) ⇒ Object

Returns an array of all pages at the given frame and layer



90
91
92
# File 'lib/pruim/image.rb', line 90

def pages_at(frame = 0, layer = 0)
  @ordered[frame, layer]
end

#palette?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/pruim/image.rb', line 61

def palette?
  return !(@palette.nil?)
end

#putpixel(x, y, color) ⇒ Object

Sets a pixel to the current active page, if any.



100
101
102
# File 'lib/pruim/image.rb', line 100

def putpixel(x, y, color)
  @active.putpixel!(x, y, color)
end

#save_as(filename, codecname = nil) ⇒ Object

Saves the image to the given filename using the given codec If codec is missig, it’s determined from the filename’s extension.



111
112
113
114
115
116
117
118
# File 'lib/pruim/image.rb', line 111

def save_as(filename, codecname = nil)
  codec = Codec.new_for_filename_name(filename, codecname)
  return false unless codec
  io    = File.open(filename, 'wb+')
  res   = codec.encode(self, io)
  io.close
  return res
end