Class: PureJPEG::Encoder

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

Overview

Baseline JPEG encoder.

Encodes a pixel source into JPEG using DCT, quantization, and Huffman coding. Supports grayscale (1 component) and YCbCr color (3 components, 4:2:0 chroma subsampling).

Use encode for a convenient entry point.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, quality: 85, grayscale: false, chroma_quality: nil, luminance_table: nil, chrominance_table: nil, quantization_modifier: nil, scramble_quantization: false, optimize_huffman: false) ⇒ Encoder

Create a new encoder for the given pixel source.

Parameters:

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

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

  • quality (Integer) (defaults to: 85)

    overall compression quality, 1-100 (default 85)

  • grayscale (Boolean) (defaults to: false)

    encode as single-channel grayscale (default false)

  • chroma_quality (Integer, nil) (defaults to: nil)

    independent quality for Cb/Cr channels, 1-100 (defaults to quality)

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

    custom 64-element quantization table in raster order for the Y channel; overrides quality for luma

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

    custom 64-element quantization table in raster order for Cb/Cr channels; overrides chroma_quality

  • quantization_modifier (Proc, nil) (defaults to: nil)

    a proc receiving (table, channel) where channel is :luminance or :chrominance, returning a modified table; applied after quality scaling but before encoding

  • scramble_quantization (Boolean) (defaults to: false)

    write quantization tables in raster order instead of zigzag (non-spec-compliant; recreates the “early digicam” artifact look when decoded by standard viewers)

  • optimize_huffman (Boolean) (defaults to: false)

    build image-specific Huffman tables with an additional analysis pass (default false)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pure_jpeg/encoder.rb', line 41

def initialize(source, quality: 85, grayscale: false, chroma_quality: nil,
               luminance_table: nil, chrominance_table: nil,
               quantization_modifier: nil, scramble_quantization: false,
               optimize_huffman: false)
  validate_quality!(quality, "quality")
  validate_quality!(chroma_quality, "chroma_quality") if chroma_quality

  @source = source
  @quality = quality
  @grayscale = grayscale
  @optimize_huffman = optimize_huffman
  @chroma_quality = chroma_quality || quality
  validate_qtable!(luminance_table, "luminance_table") if luminance_table
  validate_qtable!(chrominance_table, "chrominance_table") if chrominance_table
  @luminance_table = luminance_table
  @chrominance_table = chrominance_table
  @quantization_modifier = quantization_modifier
  @scramble_quantization = scramble_quantization
end

Instance Attribute Details

#grayscaleBoolean (readonly)

Returns whether grayscale mode is enabled.

Returns:

  • (Boolean)

    whether grayscale mode is enabled



17
18
19
# File 'lib/pure_jpeg/encoder.rb', line 17

def grayscale
  @grayscale
end

#optimize_huffmanBoolean (readonly)

Returns whether image-specific Huffman tables are generated.

Returns:

  • (Boolean)

    whether image-specific Huffman tables are generated



19
20
21
# File 'lib/pure_jpeg/encoder.rb', line 19

def optimize_huffman
  @optimize_huffman
end

#qualityInteger (readonly)

Returns the quality level (1-100).

Returns:

  • (Integer)

    the quality level (1-100)



15
16
17
# File 'lib/pure_jpeg/encoder.rb', line 15

def quality
  @quality
end

#source#width, ... (readonly)

Returns the pixel source being encoded.

Returns:

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

    the pixel source being encoded



13
14
15
# File 'lib/pure_jpeg/encoder.rb', line 13

def source
  @source
end

Instance Method Details

#to_bytesString

Return the encoded JPEG as a binary string.

Returns:

  • (String)

    raw JPEG bytes



72
73
74
75
76
# File 'lib/pure_jpeg/encoder.rb', line 72

def to_bytes
  io = StringIO.new("".b)
  encode(io)
  io.string
end

#write(path) ⇒ void

This method returns an undefined value.

Write the encoded JPEG to a file.

Parameters:

  • path (String)

    output file path



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

def write(path)
  File.binwrite(path, to_bytes)
end