module

CrImage::JPEG

JPEG module implements a JPEG image decoder and encoder. The JPEG specification is at https://www.w3.org/Graphics/JPEG/

Supported Features

  • Baseline DCT-based JPEG images (SOF0)
  • Grayscale (1 component)
  • YCbCr color space (3 components)
  • CMYK color space (4 components)
  • Standard Huffman tables
  • Quality-based quantization

Limitations

  • No arithmetic coding support
  • No hierarchical JPEG support
  • No lossless JPEG support

Constants

JPEG_MAGIC = "\xFF\xD8\xFF".to_slice

JPEG magic bytes - Start of Image (SOI) marker

STANDARD_AC_CHROMINANCE_BITS = [0_u8, 2_u8, 1_u8, 2_u8, 4_u8, 4_u8, 3_u8, 4_u8, 7_u8, 5_u8, 4_u8, 4_u8, 0_u8, 1_u8, 2_u8, 119_u8]

Standard JPEG Huffman table for AC coefficients (chrominance) From JPEG spec Annex K.3.2

STANDARD_AC_CHROMINANCE_VALUES = [0_u8, 1_u8, 2_u8, 3_u8, 17_u8, 4_u8, 5_u8, 33_u8, 49_u8, 6_u8, 18_u8, 65_u8, 81_u8, 7_u8, 97_u8, 113_u8, 19_u8, 34_u8, 50_u8, 129_u8, 8_u8, 20_u8, 66_u8, 145_u8, 161_u8, 177_u8, 193_u8, 9_u8, 35_u8, 51_u8, 82_u8, 240_u8, 21_u8, 98_u8, 114_u8, 209_u8, 10_u8, 22_u8, 36_u8, 52_u8, 225_u8, 37_u8, 241_u8, 23_u8, 24_u8, 25_u8, 26_u8, 38_u8, 39_u8, 40_u8, 41_u8, 42_u8, 53_u8, 54_u8, 55_u8, 56_u8, 57_u8, 58_u8, 67_u8, 68_u8, 69_u8, 70_u8, 71_u8, 72_u8, 73_u8, 74_u8, 83_u8, 84_u8, 85_u8, 86_u8, 87_u8, 88_u8, 89_u8, 90_u8, 99_u8, 100_u8, 101_u8, 102_u8, 103_u8, 104_u8, 105_u8, 106_u8, 115_u8, 116_u8, 117_u8, 118_u8, 119_u8, 120_u8, 121_u8, 122_u8, 130_u8, 131_u8, 132_u8, 133_u8, 134_u8, 135_u8, 136_u8, 137_u8, 138_u8, 146_u8, 147_u8, 148_u8, 149_u8, 150_u8, 151_u8, 152_u8, 153_u8, 154_u8, 162_u8, 163_u8, 164_u8, 165_u8, 166_u8, 167_u8, 168_u8, 169_u8, 170_u8, 178_u8, 179_u8, 180_u8, 181_u8, 182_u8, 183_u8, 184_u8, 185_u8, 186_u8, 194_u8, 195_u8, 196_u8, 197_u8, 198_u8, 199_u8, 200_u8, 201_u8, 202_u8, 210_u8, 211_u8, 212_u8, 213_u8, 214_u8, 215_u8, 216_u8, 217_u8, 218_u8, 226_u8, 227_u8, 228_u8, 229_u8, 230_u8, 231_u8, 232_u8, 233_u8, 234_u8, 242_u8, 243_u8, 244_u8, 245_u8, 246_u8, 247_u8, 248_u8, 249_u8, 250_u8]
STANDARD_AC_LUMINANCE_BITS = [0_u8, 2_u8, 1_u8, 3_u8, 3_u8, 2_u8, 4_u8, 3_u8, 5_u8, 5_u8, 4_u8, 4_u8, 0_u8, 0_u8, 1_u8, 125_u8]

Standard JPEG Huffman table for AC coefficients (luminance) From JPEG spec Annex K.3.1

STANDARD_AC_LUMINANCE_VALUES = [1_u8, 2_u8, 3_u8, 0_u8, 4_u8, 17_u8, 5_u8, 18_u8, 33_u8, 49_u8, 65_u8, 6_u8, 19_u8, 81_u8, 97_u8, 7_u8, 34_u8, 113_u8, 20_u8, 50_u8, 129_u8, 145_u8, 161_u8, 8_u8, 35_u8, 66_u8, 177_u8, 193_u8, 21_u8, 82_u8, 209_u8, 240_u8, 36_u8, 51_u8, 98_u8, 114_u8, 130_u8, 9_u8, 10_u8, 22_u8, 23_u8, 24_u8, 25_u8, 26_u8, 37_u8, 38_u8, 39_u8, 40_u8, 41_u8, 42_u8, 52_u8, 53_u8, 54_u8, 55_u8, 56_u8, 57_u8, 58_u8, 67_u8, 68_u8, 69_u8, 70_u8, 71_u8, 72_u8, 73_u8, 74_u8, 83_u8, 84_u8, 85_u8, 86_u8, 87_u8, 88_u8, 89_u8, 90_u8, 99_u8, 100_u8, 101_u8, 102_u8, 103_u8, 104_u8, 105_u8, 106_u8, 115_u8, 116_u8, 117_u8, 118_u8, 119_u8, 120_u8, 121_u8, 122_u8, 131_u8, 132_u8, 133_u8, 134_u8, 135_u8, 136_u8, 137_u8, 138_u8, 146_u8, 147_u8, 148_u8, 149_u8, 150_u8, 151_u8, 152_u8, 153_u8, 154_u8, 162_u8, 163_u8, 164_u8, 165_u8, 166_u8, 167_u8, 168_u8, 169_u8, 170_u8, 178_u8, 179_u8, 180_u8, 181_u8, 182_u8, 183_u8, 184_u8, 185_u8, 186_u8, 194_u8, 195_u8, 196_u8, 197_u8, 198_u8, 199_u8, 200_u8, 201_u8, 202_u8, 210_u8, 211_u8, 212_u8, 213_u8, 214_u8, 215_u8, 216_u8, 217_u8, 218_u8, 225_u8, 226_u8, 227_u8, 228_u8, 229_u8, 230_u8, 231_u8, 232_u8, 233_u8, 234_u8, 241_u8, 242_u8, 243_u8, 244_u8, 245_u8, 246_u8, 247_u8, 248_u8, 249_u8, 250_u8]
STANDARD_CHROMINANCE_QUANT_TABLE = [17_u16, 18_u16, 24_u16, 47_u16, 99_u16, 99_u16, 99_u16, 99_u16, 18_u16, 21_u16, 26_u16, 66_u16, 99_u16, 99_u16, 99_u16, 99_u16, 24_u16, 26_u16, 56_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 47_u16, 66_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16, 99_u16]

Standard JPEG quantization table for chrominance (Cb and Cr components) Based on Annex K of the JPEG specification

STANDARD_DC_CHROMINANCE_BITS = [0_u8, 3_u8, 1_u8, 1_u8, 1_u8, 1_u8, 1_u8, 1_u8, 1_u8, 1_u8, 1_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8]

Standard JPEG Huffman table for DC coefficients (chrominance) From JPEG spec Annex K.3.2

STANDARD_DC_CHROMINANCE_VALUES = [0_u8, 1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8, 8_u8, 9_u8, 10_u8, 11_u8]
STANDARD_DC_LUMINANCE_BITS = [0_u8, 1_u8, 5_u8, 1_u8, 1_u8, 1_u8, 1_u8, 1_u8, 1_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8]

Standard JPEG Huffman table for DC coefficients (luminance) From JPEG spec Annex K.3.1

STANDARD_DC_LUMINANCE_VALUES = [0_u8, 1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8, 8_u8, 9_u8, 10_u8, 11_u8]
STANDARD_LUMINANCE_QUANT_TABLE = [16_u16, 11_u16, 10_u16, 16_u16, 24_u16, 40_u16, 51_u16, 61_u16, 12_u16, 12_u16, 14_u16, 19_u16, 26_u16, 58_u16, 60_u16, 55_u16, 14_u16, 13_u16, 16_u16, 24_u16, 40_u16, 57_u16, 69_u16, 56_u16, 14_u16, 17_u16, 22_u16, 29_u16, 51_u16, 87_u16, 80_u16, 62_u16, 18_u16, 22_u16, 37_u16, 56_u16, 68_u16, 109_u16, 103_u16, 77_u16, 24_u16, 35_u16, 55_u16, 64_u16, 81_u16, 104_u16, 113_u16, 92_u16, 49_u16, 64_u16, 78_u16, 87_u16, 103_u16, 121_u16, 120_u16, 101_u16, 72_u16, 92_u16, 95_u16, 98_u16, 112_u16, 100_u16, 103_u16, 99_u16]

Standard JPEG quantization table for luminance (Y component) Based on Annex K of the JPEG specification

Class methods

read(path : String) : CrImage::Image

read and decode the entire image

Source
read(io : IO) : CrImage::Image
Source
read_config(path : String) : CrImage::Config

read and decode the configurations like color model, dimensions and does not decode entire image. This returns CrImage::Config instead.

Source
read_config(io : IO) : CrImage::Config
Source
scale_quant_table(standard_table : Array(UInt16), quality : Int32) : Array(UInt16)

Scale a quantization table based on quality factor Quality ranges from 1 (worst) to 100 (best) Quality 50 uses the standard tables Quality < 50 increases quantization (lower quality, smaller file) Quality > 50 decreases quantization (higher quality, larger file)

Source
write(path : String, image : CrImage::Image, quality : Int32 = 75) : Nil

write the Image to file in JPEG format

Source
write(io : IO, image : CrImage::Image, quality : Int32 = 75) : Nil

write the Image to IO in JPEG format

Source

Nested types