Module: PureJPEG

Defined in:
lib/pure_jpeg.rb,
lib/pure_jpeg/dct.rb,
lib/pure_jpeg/info.rb,
lib/pure_jpeg/image.rb,
lib/pure_jpeg/zigzag.rb,
lib/pure_jpeg/decoder.rb,
lib/pure_jpeg/encoder.rb,
lib/pure_jpeg/version.rb,
lib/pure_jpeg/bit_reader.rb,
lib/pure_jpeg/bit_writer.rb,
lib/pure_jpeg/jfif_reader.rb,
lib/pure_jpeg/jfif_writer.rb,
lib/pure_jpeg/quantization.rb,
lib/pure_jpeg/huffman/tables.rb,
lib/pure_jpeg/huffman/decoder.rb,
lib/pure_jpeg/huffman/encoder.rb,
lib/pure_jpeg/source/interface.rb,
lib/pure_jpeg/source/raw_source.rb,
lib/pure_jpeg/source/chunky_png_source.rb

Overview

Pure Ruby JPEG encoder and decoder with no native dependencies.

Supports baseline DCT (SOF0) with 8-bit precision, grayscale and YCbCr color (4:2:0 chroma subsampling), and standard Huffman tables (Annex K).

Defined Under Namespace

Modules: DCT, Huffman, Quantization, Source, Zigzag Classes: BitReader, BitWriter, DecodeError, Decoder, Encoder, Image, Info, JFIFReader, JFIFWriter

Constant Summary collapse

MAX_DIMENSION =

Maximum image dimension (width or height) allowed for encoding and decoding.

8192
VERSION =
"0.3.3"

Class Method Summary collapse

Class Method Details

.encode(source, **opts) ⇒ Encoder

Encode a pixel source as a JPEG.

Parameters:

  • source (#width, #height, #[])

    any object responding to width, height, and [x, y] (returning an object with .r, .g, .b in 0-255)

  • opts (Hash)

    encoding options passed to PureJPEG::Encoder#initialize

Returns:

See Also:



43
44
45
# File 'lib/pure_jpeg.rb', line 43

def self.encode(source, **opts)
  Encoder.new(source, **opts)
end

.from_chunky_png(image, background: nil, **opts) ⇒ Encoder

Encode a ChunkyPNG::Image as a JPEG.

Convenience wrapper that adapts a ChunkyPNG::Image into a pixel source and passes it to encode.

Parameters:

  • image (ChunkyPNG::Image)

    the source image

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

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

  • opts (Hash)

    encoding options passed to PureJPEG::Encoder#initialize

Returns:



57
58
59
60
# File 'lib/pure_jpeg.rb', line 57

def self.from_chunky_png(image, background: nil, **opts)
  source = Source::ChunkyPNGSource.new(image, background: background)
  Encoder.new(source, **opts)
end

.info(path_or_data) ⇒ Info

Read JPEG dimensions and basic frame metadata without decoding scan data.

Parameters:

  • path_or_data (String)

    a file path or raw JPEG bytes

Returns:

  • (Info)

    image metadata parsed from the frame header

Raises:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pure_jpeg.rb', line 74

def self.info(path_or_data)
  data = if path_or_data.is_a?(String) && !path_or_data.start_with?("\xFF\xD8".b) && File.exist?(path_or_data)
           File.binread(path_or_data)
         else
           path_or_data.b
         end

  jfif = JFIFReader.new(data, stop_after_frame: true)
  raise DecodeError, "JPEG frame header not found" unless jfif.width && jfif.height

  Info.new(
    width: jfif.width,
    height: jfif.height,
    component_count: jfif.components.length,
    progressive: jfif.progressive,
    icc_profile: jfif.icc_profile
  )
end

.read(path_or_data) ⇒ Image

Decode a JPEG from a file path or binary string.

Parameters:

  • path_or_data (String)

    a file path or raw JPEG bytes

Returns:

  • (Image)

    decoded image with pixel access



66
67
68
# File 'lib/pure_jpeg.rb', line 66

def self.read(path_or_data)
  Decoder.decode(path_or_data)
end