Class: Pura::Image::Wrapper

Inherits:
Object
  • Object
show all
Includes:
Operations
Defined in:
lib/pura/image/processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Operations

#grayscale, #resize_and_pad, #resize_to_cover, #resize_to_fill, #resize_to_fit, #resize_to_limit, #rotate, #strip

Constructor Details

#initialize(width, height, pixels) ⇒ Wrapper

Returns a new instance of Wrapper.



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

def initialize(width, height, pixels)
  @width = width
  @height = height
  @pixels = pixels.dup
  @pixels.force_encoding(Encoding::BINARY)
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



131
132
133
# File 'lib/pura/image/processor.rb', line 131

def height
  @height
end

#pixelsObject (readonly)

Returns the value of attribute pixels.



131
132
133
# File 'lib/pura/image/processor.rb', line 131

def pixels
  @pixels
end

#widthObject (readonly)

Returns the value of attribute width.



131
132
133
# File 'lib/pura/image/processor.rb', line 131

def width
  @width
end

Instance Method Details

#crop(x, y, w, h) ⇒ Object



180
181
182
183
184
# File 'lib/pura/image/processor.rb', line 180

def crop(x, y, w, h)
  raw = Pura::Jpeg::Image.new(@width, @height, @pixels)
  cropped = raw.crop(x, y, w, h)
  self.class.new(cropped.width, cropped.height, cropped.pixels)
end

#pixel_at(x, y) ⇒ Object

Raises:

  • (IndexError)


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

def pixel_at(x, y)
  raise IndexError, "Coordinates out of bounds" if x.negative? || x >= @width || y.negative? || y >= @height

  offset = ((y * @width) + x) * 3
  [@pixels.getbyte(offset), @pixels.getbyte(offset + 1), @pixels.getbyte(offset + 2)]
end

#resize(new_width, new_height) ⇒ Object



162
163
164
165
166
# File 'lib/pura/image/processor.rb', line 162

def resize(new_width, new_height)
  raw = Pura::Jpeg::Image.new(@width, @height, @pixels)
  resized = raw.resize(new_width, new_height)
  self.class.new(resized.width, resized.height, resized.pixels)
end

#resize_fill(fill_width, fill_height) ⇒ Object



174
175
176
177
178
# File 'lib/pura/image/processor.rb', line 174

def resize_fill(fill_width, fill_height)
  raw = Pura::Jpeg::Image.new(@width, @height, @pixels)
  filled = raw.resize_fill(fill_width, fill_height)
  self.class.new(filled.width, filled.height, filled.pixels)
end

#resize_fit(max_width, max_height) ⇒ Object



168
169
170
171
172
# File 'lib/pura/image/processor.rb', line 168

def resize_fit(max_width, max_height)
  raw = Pura::Jpeg::Image.new(@width, @height, @pixels)
  fitted = raw.resize_fit(max_width, max_height)
  self.class.new(fitted.width, fitted.height, fitted.pixels)
end

#to_ppmObject



158
159
160
# File 'lib/pura/image/processor.rb', line 158

def to_ppm
  "P6\n#{@width} #{@height}\n255\n" + @pixels
end

#to_rgb_arrayObject



147
148
149
150
151
152
153
154
155
156
# File 'lib/pura/image/processor.rb', line 147

def to_rgb_array
  result = []
  i = 0
  size = @pixels.bytesize
  while i < size
    result << [@pixels.getbyte(i), @pixels.getbyte(i + 1), @pixels.getbyte(i + 2)]
    i += 3
  end
  result
end