Class: PureJPEG::Source::ChunkyPNGSource

Inherits:
Object
  • Object
show all
Defined in:
lib/pure_jpeg/source/chunky_png_source.rb

Overview

Pixel source adapter for ChunkyPNG::Image.

Wraps a ChunkyPNG::Image so it can be passed directly to PureJPEG.encode. Requires the chunky_png gem.

Examples:

image = ChunkyPNG::Image.from_file("photo.png")
source = PureJPEG::Source::ChunkyPNGSource.new(image)
PureJPEG.encode(source).write("photo.jpg")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image, background: nil) ⇒ ChunkyPNGSource

Returns a new instance of ChunkyPNGSource.

Parameters:

  • image (ChunkyPNG::Image)

    the source PNG image

  • background (Array<Integer>, nil) (defaults to: nil)

    optional [r, g, b] background color to composite transparent pixels against before encoding



23
24
25
26
27
28
29
30
31
# File 'lib/pure_jpeg/source/chunky_png_source.rb', line 23

def initialize(image, background: nil)
  @width = image.width
  @height = image.height
  @packed_pixels = if background.nil?
                     image.pixels
                   else
                     composite_pixels(image.pixels, background)
                   end
end

Instance Attribute Details

#heightInteger (readonly)

Returns image height in pixels.

Returns:

  • (Integer)

    image height in pixels



18
19
20
# File 'lib/pure_jpeg/source/chunky_png_source.rb', line 18

def height
  @height
end

#packed_pixelsArray<Integer> (readonly)

Returns flat row-major array of packed RGBA integers.

Returns:

  • (Array<Integer>)

    flat row-major array of packed RGBA integers



34
35
36
# File 'lib/pure_jpeg/source/chunky_png_source.rb', line 34

def packed_pixels
  @packed_pixels
end

#widthInteger (readonly)

Returns image width in pixels.

Returns:

  • (Integer)

    image width in pixels



16
17
18
# File 'lib/pure_jpeg/source/chunky_png_source.rb', line 16

def width
  @width
end

Instance Method Details

#[](x, y) ⇒ Pixel

Retrieve a pixel at the given coordinate.

Parameters:

  • x (Integer)

    column (0-based)

  • y (Integer)

    row (0-based)

Returns:



41
42
43
44
45
46
47
48
# File 'lib/pure_jpeg/source/chunky_png_source.rb', line 41

def [](x, y)
  color = @packed_pixels[y * @width + x]
  Pixel.new(
    (color >> 24) & 0xFF,
    (color >> 16) & 0xFF,
    (color >> 8) & 0xFF
  )
end