Class: Mooncats::Image::Composite

Inherits:
Object
  • Object
show all
Defined in:
lib/mooncats/composite.rb

Overview

nest Composite inside Image - why? why not?

Constant Summary collapse

CANVAS_WIDTH =

sorry - for now “hard-coded” / fixed - 24x24

24
CANVAS_HEIGHT =
24

Instance Method Summary collapse

Constructor Details

#initialize(cols = 100, rows = 255) ⇒ Composite

Returns a new instance of Composite.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mooncats/composite.rb', line 10

def initialize( cols=100, rows=255 )
    @composite = ChunkyPNG::Image.new( cols*CANVAS_WIDTH,
                                       rows*CANVAS_HEIGHT,
                                       ChunkyPNG::Color::WHITE ) # why? why not? - use TRANSPARENT (is default?)

    ## todo/check - find a better name for cols/rows - why? why not?
    @cols = cols
    @rows = rows

    @i = 0  # (track) current index (of added images)
end

Instance Method Details

#add(image) ⇒ Object Also known as: <<



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
# File 'lib/mooncats/composite.rb', line 23

def add( image )
  y, x =  @i.divmod( @cols )

  puts "    width: #{image.width}, height: #{image.height}"

  ## try to center image (identicon) in 24x24 canvas
  ##   the 4 formats are
  ##   - 21×17 - Standing
  ##   - 20×14 - Sleeping
  ##   - 17×22 - Pouncing
  ##   - 20×21 - Stalking
  ## e.g. add left padding (x_center) and
  ##          top padding (y_center)
  x_center, y_center = case [image.width, image.height]
                       when [21,17] then [1,3]
                       when [20,14] then [2,5]
                       when [17,22] then [3,1]
                       when [20,21] then [2,1]
                       ## todo/fix: report unknown image format/size!!!!
                       end

  ## note: image.image  - "unwrap" the "raw" ChunkyPNG::Image
  @composite.compose!( image.image, x*CANVAS_WIDTH+x_center, y*CANVAS_HEIGHT+y_center )
  @i += 1
end

#heightObject



61
# File 'lib/mooncats/composite.rb', line 61

def height()       @composite.height; end

#imageObject

return image ref - use a different name - why? why not?



64
# File 'lib/mooncats/composite.rb', line 64

def image()        @composite; end

#save(path, constraints = {}) ⇒ Object

(image) delegates

todo/check: add some more??


56
57
58
# File 'lib/mooncats/composite.rb', line 56

def save( path, constraints = {} )
  @composite.save( path, constraints )
end

#widthObject



60
# File 'lib/mooncats/composite.rb', line 60

def width()        @composite.width; end