Class: Tessel::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/tessel.rb,
sig/tessel.rbs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, fill: [0, 0, 0, 0], metadata: {}) ⇒ Image

Returns a new instance of Image.

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
# File 'lib/tessel.rb', line 62

def initialize(width, height, fill: [0, 0, 0, 0], metadata: {})
  @width = Integer(width)
  @height = Integer(height)
  raise ArgumentError, "width and height must be non-negative" if @width.negative? || @height.negative?

  @data = Color.pack(fill) * (@width * @height)
   = .transform_keys(&:to_s).transform_values(&:to_s)
end

Instance Attribute Details

#height ⇒ Object (readonly)

Returns the value of attribute height.



60
61
62
# File 'lib/tessel.rb', line 60

def height
  @height
end

#metadata ⇒ Object (readonly)

Returns the value of attribute metadata.



60
61
62
# File 'lib/tessel.rb', line 60

def 
  
end

#width ⇒ Object (readonly)

Returns the value of attribute width.



60
61
62
# File 'lib/tessel.rb', line 60

def width
  @width
end

Class Method Details

.from_rgba(width, height, bytes, metadata: {}) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tessel.rb', line 71

def self.from_rgba(width, height, bytes, metadata: {})
  width = Integer(width)
  height = Integer(height)
  raise ArgumentError, "width and height must be non-negative" if width.negative? || height.negative?
  bytes = String(bytes).b
  expected = width * height * 4
  raise ArgumentError, "RGBA byte length must be #{expected}" unless bytes.bytesize == expected

  image = allocate
  image.instance_variable_set(:@width, width)
  image.instance_variable_set(:@height, height)
  image.instance_variable_set(:@data, bytes.dup)
  image.instance_variable_set(:@metadata, .transform_keys(&:to_s).transform_values(&:to_s))
  image
end

Instance Method Details

#[](x, y) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/tessel.rb', line 93

def [](x, y)
  x = Integer(x)
  y = Integer(y)
  return nil unless x.between?(0, @width - 1) && y.between?(0, @height - 1)

  @data.byteslice((y * @width + x) * 4, 4).unpack("C4")
end

#[]=(x, y, color) ⇒ Object

Raises:

  • (FrozenError)


101
102
103
104
105
106
107
108
109
110
# File 'lib/tessel.rb', line 101

def []=(x, y, color)
  raise FrozenError, "can't modify frozen image" if frozen?

  x = Integer(x)
  y = Integer(y)
  return color unless x.between?(0, @width - 1) && y.between?(0, @height - 1)

  @data[((y * @width + x) * 4), 4] = Color.pack(color)
  color
end

#blit(source, dx, dy, sx: 0, sy: 0, w: nil, h: nil, blend: :alpha) ⇒ Object

Raises:

  • (FrozenError)


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/tessel.rb', line 144

def blit(source, dx, dy, sx: 0, sy: 0, w: nil, h: nil, blend: :alpha)
  raise FrozenError, "can't modify frozen image" if frozen?
  raise TypeError, "source must be a Tessel::Image" unless source.is_a?(Image)
  raise ArgumentError, "unknown blend: #{blend}" unless i[copy alpha].include?(blend)

  source = source.dup if source.equal?(self)

  sx, sy, dx, dy = Integer(sx), Integer(sy), Integer(dx), Integer(dy)
  w = source.width - sx if w.nil?
  h = source.height - sy if h.nil?
  w, h = Integer(w), Integer(h)
  (0...h).each do |row|
    source_x = sx
    target_x = dx
    count = w
    source_y = sy + row
    target_y = dy + row
    next unless source_y.between?(0, source.height - 1) && target_y.between?(0, @height - 1)

    if target_x.negative?
      source_x -= target_x
      count += target_x
      target_x = 0
    end
    if source_x.negative?
      target_x -= source_x
      count += source_x
      source_x = 0
    end
    count = [count, @width - target_x, source.width - source_x].min
    next if count <= 0

    if blend == :copy
      @data[((target_y * @width + target_x) * 4), count * 4] = source.instance_variable_get(:@data).byteslice((source_y * source.width + source_x) * 4, count * 4)
    else
      (0...count).each do |offset|
        source_pixel = source.instance_variable_get(:@data).byteslice((source_y * source.width + source_x + offset) * 4, 4)
        blend_pixel(target_x + offset, target_y, source_pixel)
      end
    end
  end
  self
end

#blit_mask(mask, dx, dy, color) ⇒ Object

Raises:

  • (FrozenError)


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/tessel.rb', line 188

def blit_mask(mask, dx, dy, color)
  raise FrozenError, "can't modify frozen image" if frozen?
  raise TypeError, "mask must respond to width, height and bytes" unless mask.respond_to?(:width) && mask.respond_to?(:height) && mask.respond_to?(:bytes)

  packed = Color.pack(color)
  mask.bytes.bytes.each_with_index do |coverage, index|
    next if coverage.zero?

    x = Integer(dx) + index % mask.width
    y = Integer(dy) + index / mask.width
    next unless x.between?(0, @width - 1) && y.between?(0, @height - 1)

    source = packed.dup
    source.setbyte(3, packed.getbyte(3) * coverage / 255)
    blend_pixel(x, y, source)
  end
  self
end

#bytes ⇒ Object Also known as: to_rgba_bytes



87
88
89
# File 'lib/tessel.rb', line 87

def bytes
  @data.dup.freeze
end

#clear(color) ⇒ Object

Raises:

  • (FrozenError)


112
113
114
115
116
117
# File 'lib/tessel.rb', line 112

def clear(color)
  raise FrozenError, "can't modify frozen image" if frozen?

  @data = Color.pack(color) * (@width * @height)
  self
end

#crop(x, y, width, height) ⇒ Object



207
208
209
210
211
# File 'lib/tessel.rb', line 207

def crop(x, y, width, height)
  result = Image.new(width, height)
  result.blit(self, 0, 0, sx: x, sy: y, w: width, h: height, blend: :copy)
  result
end

#fill_rect(x, y, width, height, color, blend: :copy) ⇒ Object

Raises:

  • (FrozenError)


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/tessel.rb', line 123

def fill_rect(x, y, width, height, color, blend: :copy)
  raise FrozenError, "can't modify frozen image" if frozen?
  raise ArgumentError, "unknown blend: #{blend}" unless i[copy alpha].include?(blend)

  x, y, width, height = Integer(x), Integer(y), Integer(width), Integer(height)
  x0 = [x, 0].max
  y0 = [y, 0].max
  x1 = [x + width, @width].min
  y1 = [y + height, @height].min
  return self if x0 >= x1 || y0 >= y1

  packed = Color.pack(color)
  if blend == :copy || packed.getbyte(3) == 255
    row = packed * (x1 - x0)
    (y0...y1).each { |row_y| @data[((row_y * @width + x0) * 4), row.bytesize] = row }
  elsif packed.getbyte(3).positive?
    (y0...y1).each { |row_y| (x0...x1).each { |pixel_x| blend_pixel(pixel_x, row_y, packed) } }
  end
  self
end

#flip_vertical ⇒ Object



213
214
215
216
217
218
219
# File 'lib/tessel.rb', line 213

def flip_vertical
  result = Image.new(@width, @height, metadata: )
  (0...@height).each do |y|
    result.instance_variable_get(:@data)[y * @width * 4, @width * 4] = @data.byteslice((@height - y - 1) * @width * 4, @width * 4)
  end
  result
end

#hspan(x0, x1, y, color, blend: :copy) ⇒ Object



119
120
121
# File 'lib/tessel.rb', line 119

def hspan(x0, x1, y, color, blend: :copy)
  fill_rect(x0, y, Integer(x1) - Integer(x0) + 1, 1, color, blend: blend)
end

#initialize_copy(other) ⇒ Object



253
254
255
256
257
# File 'lib/tessel.rb', line 253

def initialize_copy(other)
  super
  @data = other.instance_variable_get(:@data).dup
   = other..dup
end

#scale_nearest(width, height) ⇒ Object

Raises:

  • (ArgumentError)


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/tessel.rb', line 221

def scale_nearest(width, height)
  width, height = Integer(width), Integer(height)
  raise ArgumentError, "dimensions must be non-negative" if width.negative? || height.negative?

  result = Image.new(width, height, metadata: )
  return result if width.zero? || height.zero? || @width.zero? || @height.zero?

  (0...height).each do |y|
    source_y = y * @height / height
    (0...width).each do |x|
      source_x = x * @width / width
      result.instance_variable_get(:@data)[(y * width + x) * 4, 4] = @data.byteslice((source_y * @width + source_x) * 4, 4)
    end
  end
  result
end

#to_rgb_bytes ⇒ Object



238
239
240
# File 'lib/tessel.rb', line 238

def to_rgb_bytes
  @data.unpack("C*").each_slice(4).flat_map { |r, g, b, _a| [r, g, b] }.pack("C*").b
end

#write(path, **options) ⇒ Object



242
243
244
245
246
247
248
249
250
251
# File 'lib/tessel.rb', line 242

def write(path, **options)
  filename = path.respond_to?(:to_path) ? path.to_path : String(path)
  case File.extname(filename).downcase
  when ".png" then File.binwrite(filename, PNG.encode(self, **options))
  when ".ppm", ".pnm" then File.binwrite(filename, PPM.encode(self, **options))
  when ".bmp" then File.binwrite(filename, BMP.encode(self, **options))
  else raise UnsupportedError, "unknown image format: #{filename}"
  end
  filename
end