Class: PureJPEG::Image
- Inherits:
-
Object
- Object
- PureJPEG::Image
- Defined in:
- lib/pure_jpeg/image.rb
Overview
A decoded JPEG image with pixel-level access.
Internally stores pixels as packed integers (+r << 16 | g << 8 | b+) to avoid per-pixel object allocation. Implements the same pixel source interface (width, height, [x, y]) as encoder inputs, so a decoded image can be passed directly to encode for re-encoding.
Instance Attribute Summary collapse
-
#height ⇒ Integer
readonly
Image height in pixels.
-
#icc_profile ⇒ String?
readonly
Raw ICC color profile data, if present in the source JPEG.
-
#packed_pixels ⇒ Array<Integer>
readonly
Flat row-major array of packed RGB integers.
-
#width ⇒ Integer
readonly
Image width in pixels.
Instance Method Summary collapse
-
#[](x, y) ⇒ Source::Pixel
Retrieve a pixel by coordinate.
-
#[]=(x, y, pixel) ⇒ Source::Pixel
Set a pixel by coordinate.
-
#each_pixel {|x, y, pixel| ... } ⇒ void
Iterate over every pixel in the image.
-
#each_rgb {|x, y, r, g, b| ... } ⇒ void
Iterate over every pixel without allocating Pixel structs.
-
#initialize(width, height, packed_pixels, icc_profile: nil) ⇒ Image
constructor
A new instance of Image.
Constructor Details
#initialize(width, height, packed_pixels, icc_profile: nil) ⇒ Image
Returns a new instance of Image.
28 29 30 31 32 33 |
# File 'lib/pure_jpeg/image.rb', line 28 def initialize(width, height, packed_pixels, icc_profile: nil) @width = width @height = height @packed_pixels = packed_pixels @icc_profile = icc_profile end |
Instance Attribute Details
#height ⇒ Integer (readonly)
Returns image height in pixels.
14 15 16 |
# File 'lib/pure_jpeg/image.rb', line 14 def height @height end |
#icc_profile ⇒ String? (readonly)
Returns raw ICC color profile data, if present in the source JPEG.
21 22 23 |
# File 'lib/pure_jpeg/image.rb', line 21 def icc_profile @icc_profile end |
#packed_pixels ⇒ Array<Integer> (readonly)
Returns flat row-major array of packed RGB integers. Format: (r << 16) | (g << 8) | b.
18 19 20 |
# File 'lib/pure_jpeg/image.rb', line 18 def packed_pixels @packed_pixels end |
#width ⇒ Integer (readonly)
Returns image width in pixels.
12 13 14 |
# File 'lib/pure_jpeg/image.rb', line 12 def width @width end |
Instance Method Details
#[](x, y) ⇒ Source::Pixel
Retrieve a pixel by coordinate.
40 41 42 43 44 |
# File 'lib/pure_jpeg/image.rb', line 40 def [](x, y) validate_coordinates!(x, y) color = @packed_pixels[y * @width + x] Source::Pixel.new((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF) end |
#[]=(x, y, pixel) ⇒ Source::Pixel
Set a pixel by coordinate.
52 53 54 55 56 |
# File 'lib/pure_jpeg/image.rb', line 52 def []=(x, y, pixel) validate_coordinates!(x, y) @packed_pixels[y * @width + x] = (pixel.r << 16) | (pixel.g << 8) | pixel.b pixel end |
#each_pixel {|x, y, pixel| ... } ⇒ void
This method returns an undefined value.
Iterate over every pixel in the image.
64 65 66 67 68 69 70 |
# File 'lib/pure_jpeg/image.rb', line 64 def each_pixel @height.times do |y| @width.times do |x| yield x, y, self[x, y] end end end |
#each_rgb {|x, y, r, g, b| ... } ⇒ void
This method returns an undefined value.
Iterate over every pixel without allocating Pixel structs.
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/pure_jpeg/image.rb', line 80 def each_rgb i = 0 @height.times do |y| @width.times do |x| color = @packed_pixels[i] yield x, y, (color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF i += 1 end end end |