Class: PureJPEG::Image

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(width, height, packed_pixels, icc_profile: nil) ⇒ Image

Returns a new instance of Image.

Parameters:

  • width (Integer)
  • height (Integer)
  • packed_pixels (Array<Integer>)

    flat row-major array of packed RGB integers in the format (r << 16) | (g << 8) | b

  • icc_profile (String, nil) (defaults to: nil)

    raw ICC profile bytes



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

#heightInteger (readonly)

Returns image height in pixels.

Returns:

  • (Integer)

    image height in pixels



14
15
16
# File 'lib/pure_jpeg/image.rb', line 14

def height
  @height
end

#icc_profileString? (readonly)

Returns raw ICC color profile data, if present in the source JPEG.

Returns:

  • (String, nil)

    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_pixelsArray<Integer> (readonly)

Returns flat row-major array of packed RGB integers. Format: (r << 16) | (g << 8) | b.

Returns:

  • (Array<Integer>)

    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

#widthInteger (readonly)

Returns image width in pixels.

Returns:

  • (Integer)

    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.

Parameters:

  • x (Integer)

    column (0-based)

  • y (Integer)

    row (0-based)

Returns:



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.

Parameters:

  • x (Integer)

    column (0-based)

  • y (Integer)

    row (0-based)

  • pixel (Source::Pixel)

    replacement pixel

Returns:



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.

Yield Parameters:

  • x (Integer)

    column

  • y (Integer)

    row

  • pixel (Source::Pixel)

    the pixel at (x, y)



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.

Yield Parameters:

  • x (Integer)

    column

  • y (Integer)

    row

  • r (Integer)

    red component (0-255)

  • g (Integer)

    green component (0-255)

  • b (Integer)

    blue component (0-255)



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