Class: DiscreetProxy::Proxy

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

Overview

This class represents a proxy.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT) ⇒ Proxy

Returns a new instance of Proxy.



59
60
61
62
63
# File 'lib/discreet_proxy.rb', line 59

def initialize(w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT)
  @width, @height = w.to_i, h.to_i
  # Blank out the pixel values with black
  generate_black
end

Instance Attribute Details

#heightObject (readonly)

Image dimensions, standard is 126x92



53
54
55
# File 'lib/discreet_proxy.rb', line 53

def height
  @height
end

#rowsObject (readonly)

Array of rows with each row being an array of packed color integers (you can unpack them with ChunkyPNG::Color)



57
58
59
# File 'lib/discreet_proxy.rb', line 57

def rows
  @rows
end

#widthObject (readonly)

Image dimensions, standard is 126x92



53
54
55
# File 'lib/discreet_proxy.rb', line 53

def width
  @width
end

Instance Method Details

#[](left, top) ⇒ Object

Get an array of the [r,g,b] pixel values at the specific coordinate



77
78
79
80
# File 'lib/discreet_proxy.rb', line 77

def [](left, top)
  png_color_int = @rows[top][left]
  unpack_rgb(png_color_int)
end

#[]=(x, y, *rgb) ⇒ Object

Set the color value at the specific coordinate. If the passed value is a single integer, it gets interpreted as a PNG color value. If a triplet array with three components is passed it’s interpreted as RGB



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/discreet_proxy.rb', line 85

def []=(x, y, *rgb)
  color = rgb.flatten
  
  # Check for raw pixel value
  if color.length == 1 && color[0].is_a?(Numeric)
    @rows[y][x] = color[0]
  else
    r, g, b = color.map{|e| e.to_i }
    @rows[y][x] = pack_rgb(r, g ,b)
  end
end

#fill_pixbuf(io) ⇒ Object

Once the proxy metadata is known, this method can parse out the actual pixel data from the passed IO



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/discreet_proxy.rb', line 120

def fill_pixbuf(io)
  @rows = []
  
  # Data comes in row per row, starting on bottom left because of endianness
  per_row = (@width.to_i + row_pad) * 3
  total_size = ((per_row + row_pad) * @height) + 1
  blob = StringIO.new(io.read(total_size))
  
  @height.times do
    row = []
    row_data = blob.read(@width.to_i * 3)
    row_data = StringIO.new(row_data.to_s)
    
    # Read 3x8bit for each pixel
    @width.times do
      rgb = (row_data.read(3) || "AAA").unpack("CCC")
      row.push(pack_rgb(*rgb))
    end
    
    # At the end of each row (thus at the beginning byteswap),
    # 2 bytes contain garbage since rows are aligned
    # to start at 8-complement byte offsets. If they are not discarded this disturbs
    # the RGB cadence of the other values.
    blob.seek(blob.pos + row_pad)
    
    # Since the file is actually BE, the rows are ordered top to bottom in the file
    @rows.unshift(row)
  end
end

#save(filename) ⇒ Object

Save out the .p file



151
152
153
# File 'lib/discreet_proxy.rb', line 151

def save(filename)
  File.open(filename, 'wb') { |io| io.write(to_dotp) }
end

#save_png(filename) ⇒ Object

Save out the PNG version of the file



156
157
158
# File 'lib/discreet_proxy.rb', line 156

def save_png(filename)
  to_png.save(filename)
end

#to_dotpObject

Compose a string with the entire contents of a proxy file



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/discreet_proxy.rb', line 98

def to_dotp
  # Pack the header
  buf = StringIO.new(0xFF.chr * 40)
  header = [MAGIC, VERSION_BSWAP, width, height, PROXY_DEPTH].pack("na6nnn")
  buf.write(header)
  buf.seek(40)
  
  # Write out all the rows starting with the last one
  @rows.reverse.each do | row |
    row.each do | pix |
      rgb = unpack_rgb(pix).pack("CCC")
      buf.write(rgb) 
    end
    # Then write the padding
    buf.write(0x00.chr * row_pad)
  end
  
  buf.string
end

#to_pngObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/discreet_proxy.rb', line 65

def to_png
  png = ChunkyPNG::Image.new(@width, @height)
  png.["Software"] = "Ruby DiscreetProxy converter/chunky_png"
  @rows.each_with_index do | row, y |
    png.replace_row!(y, row)
  end
  # Bump it to the default icon size
  png.resample_bilinear!(DEFAULT_WIDTH, DEFAULT_HEIGHT)
  png
end