jpeg

Imports

Imports #

"image"
"image/color"
"image/internal/imageutil"
"io"
"image"
"bufio"
"errors"
"image"
"image/color"
"io"
"io"

Constants & Variables

DefaultQuality const #

DefaultQuality is the default quality encoding parameter.

const DefaultQuality = 75

acTable const #

const acTable = 1

adobeTransformUnknown const #

See https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#Adobe

const adobeTransformUnknown = 0

adobeTransformYCbCr const #

See https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#Adobe

const adobeTransformYCbCr = 1

adobeTransformYCbCrK const #

See https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#Adobe

const adobeTransformYCbCrK = 2

app0Marker const #

"APPlication specific" markers aren't part of the JPEG spec per se, but in practice, their use is described at https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html

const app0Marker = 0xe0

app14Marker const #

const app14Marker = 0xee

app15Marker const #

const app15Marker = 0xef

bitCount var #

bitCount counts the number of bits needed to hold an integer.

var bitCount = [256]byte{...}

blockSize const #

const blockSize = 64

centerJSample const #

const centerJSample = 128

comMarker const #

const comMarker = 0xfe

constBits const #

const constBits = 13

dcTable const #

const dcTable = 0

dhtMarker const #

const dhtMarker = 0xc4

dqtMarker const #

const dqtMarker = 0xdb

driMarker const #

const driMarker = 0xdd

eoiMarker const #

const eoiMarker = 0xd9

errMissingFF00 var #

errMissingFF00 means that readByteStuffedByte encountered an 0xff byte (a marker byte) that wasn't the expected byte-stuffed sequence 0xff, 0x00.

var errMissingFF00 = *ast.CallExpr

errShortHuffmanData var #

errShortHuffmanData means that an unexpected EOF occurred while decoding Huffman data.

var errShortHuffmanData = *ast.CallExpr

errUnsupportedSubsamplingRatio var #

var errUnsupportedSubsamplingRatio = *ast.CallExpr

fix_0_298631336 const #

Trigonometric constants in 13-bit fixed point format.

const fix_0_298631336 = 2446

fix_0_390180644 const #

Trigonometric constants in 13-bit fixed point format.

const fix_0_390180644 = 3196

fix_0_541196100 const #

Trigonometric constants in 13-bit fixed point format.

const fix_0_541196100 = 4433

fix_0_765366865 const #

Trigonometric constants in 13-bit fixed point format.

const fix_0_765366865 = 6270

fix_0_899976223 const #

Trigonometric constants in 13-bit fixed point format.

const fix_0_899976223 = 7373

fix_1_175875602 const #

Trigonometric constants in 13-bit fixed point format.

const fix_1_175875602 = 9633

fix_1_501321110 const #

Trigonometric constants in 13-bit fixed point format.

const fix_1_501321110 = 12299

fix_1_847759065 const #

Trigonometric constants in 13-bit fixed point format.

const fix_1_847759065 = 15137

fix_1_961570560 const #

Trigonometric constants in 13-bit fixed point format.

const fix_1_961570560 = 16069

fix_2_053119869 const #

Trigonometric constants in 13-bit fixed point format.

const fix_2_053119869 = 16819

fix_2_562915447 const #

Trigonometric constants in 13-bit fixed point format.

const fix_2_562915447 = 20995

fix_3_072711026 const #

Trigonometric constants in 13-bit fixed point format.

const fix_3_072711026 = 25172

huffIndexChrominanceAC const #

const huffIndexChrominanceAC

huffIndexChrominanceDC const #

const huffIndexChrominanceDC

huffIndexLuminanceAC const #

const huffIndexLuminanceAC

huffIndexLuminanceDC const #

const huffIndexLuminanceDC huffIndex = iota

lutSize const #

lutSize is the log-2 size of the Huffman decoder's look-up table.

const lutSize = 8

maxCodeLength const #

maxCodeLength is the maximum (inclusive) number of bits in a Huffman code.

const maxCodeLength = 16

maxComponents const #

const maxComponents = 4

maxNCodes const #

maxNCodes is the maximum (inclusive) number of codes in a Huffman tree.

const maxNCodes = 256

maxTc const #

const maxTc = 1

maxTh const #

const maxTh = 3

maxTq const #

const maxTq = 3

nHuffIndex const #

const nHuffIndex

nQuantIndex const #

const nQuantIndex

pass1Bits const #

const pass1Bits = 2

quantIndexChrominance const #

const quantIndexChrominance

quantIndexLuminance const #

const quantIndexLuminance quantIndex = iota

r2 const #

const r2 = 181

rst0Marker const #

const rst0Marker = 0xd0

rst7Marker const #

const rst7Marker = 0xd7

sof0Marker const #

const sof0Marker = 0xc0

sof1Marker const #

const sof1Marker = 0xc1

sof2Marker const #

const sof2Marker = 0xc2

soiMarker const #

const soiMarker = 0xd8

sosHeaderY var #

sosHeaderY is the SOS marker "\xff\xda" followed by 8 bytes: - the marker length "\x00\x08", - the number of components "\x01", - component 1 uses DC table 0 and AC table 0 "\x01\x00", - the bytes "\x00\x3f\x00". Section B.2.3 of the spec says that for sequential DCTs, those bytes (8-bit Ss, 8-bit Se, 4-bit Ah, 4-bit Al) should be 0x00, 0x3f, 0x00<<4 | 0x00.

var sosHeaderY = []byte{...}

sosHeaderYCbCr var #

sosHeaderYCbCr is the SOS marker "\xff\xda" followed by 12 bytes: - the marker length "\x00\x0c", - the number of components "\x03", - component 1 uses DC table 0 and AC table 0 "\x01\x00", - component 2 uses DC table 1 and AC table 1 "\x02\x11", - component 3 uses DC table 1 and AC table 1 "\x03\x11", - the bytes "\x00\x3f\x00". Section B.2.3 of the spec says that for sequential DCTs, those bytes (8-bit Ss, 8-bit Se, 4-bit Ah, 4-bit Al) should be 0x00, 0x3f, 0x00<<4 | 0x00.

var sosHeaderYCbCr = []byte{...}

sosMarker const #

const sosMarker = 0xda

theHuffmanLUT var #

theHuffmanLUT are compiled representations of theHuffmanSpec.

var theHuffmanLUT [4]huffmanLUT

theHuffmanSpec var #

theHuffmanSpec is the Huffman encoding specifications. This encoder uses the same Huffman encoding for all images. It is also the same Huffman encoding used by section K.3 of the spec. The DC tables have 12 decoded values, called categories. The AC tables have 162 decoded values: bytes that pack a 4-bit Run and a 4-bit Size. There are 16 valid Runs and 10 valid Sizes, plus two special R|S cases: 0|0 (meaning EOB) and F|0 (meaning ZRL).

var theHuffmanSpec = [nHuffIndex]huffmanSpec{...}

unscaledQuant var #

unscaledQuant are the unscaled quantization tables in zig-zag order. Each encoder copies and scales the tables according to its quality parameter. The values are derived from section K.1 of the spec, after converting from natural to zig-zag order.

var unscaledQuant = [nQuantIndex][blockSize]byte{...}

unzig var #

unzig maps from the zig-zag ordering to the natural ordering. For example, unzig[3] is the column and row of the fourth element in zig-zag order. The value is 16, which means first column (16%8 == 0) and third row (16/8 == 2).

var unzig = [blockSize]int{...}

w1 const #

const w1 = 2841

w1mw7 const #

const w1mw7 = *ast.BinaryExpr

w1pw7 const #

const w1pw7 = *ast.BinaryExpr

w2 const #

const w2 = 2676

w2mw6 const #

const w2mw6 = *ast.BinaryExpr

w2pw6 const #

const w2pw6 = *ast.BinaryExpr

w3 const #

const w3 = 2408

w3mw5 const #

const w3mw5 = *ast.BinaryExpr

w3pw5 const #

const w3pw5 = *ast.BinaryExpr

w5 const #

const w5 = 1609

w6 const #

const w6 = 1108

w7 const #

const w7 = 565

Type Aliases

FormatError type #

A FormatError reports that the input is not a valid JPEG.

type FormatError string

UnsupportedError type #

An UnsupportedError reports that the input uses a valid but unimplemented JPEG feature.

type UnsupportedError string

block type #

type block [blockSize]int32

huffIndex type #

type huffIndex int

huffmanLUT type #

huffmanLUT is a compiled look-up table representation of a huffmanSpec. Each value maps to a uint32 of which the 8 most significant bits hold the codeword size in bits and the 24 least significant bits hold the codeword. The maximum codeword size is 16 bits.

type huffmanLUT []uint32

quantIndex type #

type quantIndex int

Interfaces

Reader interface #

Deprecated: Reader is not used by the [image/jpeg] package and should not be used by others. It is kept for compatibility.

type Reader interface {
io.ByteReader
io.Reader
}

writer interface #

writer is a buffered writer.

type writer interface {
Flush() error
io.Writer
io.ByteWriter
}

Structs

Options struct #

Options are the encoding parameters. Quality ranges from 1 to 100 inclusive, higher is better.

type Options struct {
Quality int
}

bits struct #

bits holds the unprocessed bits that have been taken from the byte-stream. The n least significant bits of a form the unread bits, to be read in MSB to LSB order.

type bits struct {
a uint32
m uint32
n int32
}

component struct #

Component specification, specified in section B.2.2.

type component struct {
h int
v int
c uint8
tq uint8
}

decoder struct #

type decoder struct {
r io.Reader
bits bits
bytes struct{...}
width int
height int
img1 *image.Gray
img3 *image.YCbCr
blackPix []byte
blackStride int
ri int
nComp int
baseline bool
progressive bool
jfif bool
adobeTransformValid bool
adobeTransform uint8
eobRun uint16
comp [maxComponents]component
progCoeffs [maxComponents][]block
huff [*ast.BinaryExpr][*ast.BinaryExpr]huffman
quant [*ast.BinaryExpr]block
tmp [*ast.BinaryExpr]byte
}

encoder struct #

encoder encodes an image to the JPEG format.

type encoder struct {
w writer
err error
buf [16]byte
bits uint32
nBits uint32
quant [nQuantIndex][blockSize]byte
}

huffman struct #

huffman is a Huffman decoder, specified in section C.

type huffman struct {
nCodes int32
lut [*ast.BinaryExpr]uint16
vals [maxNCodes]uint8
minCodes [maxCodeLength]int32
maxCodes [maxCodeLength]int32
valsIndices [maxCodeLength]int32
}

huffmanSpec struct #

huffmanSpec specifies a Huffman encoding.

type huffmanSpec struct {
count [16]byte
value []byte
}

Functions

Decode function #

Decode reads a JPEG image from r and returns it as an [image.Image].

func Decode(r io.Reader) (image.Image, error)

DecodeConfig function #

DecodeConfig returns the color model and dimensions of a JPEG image without decoding the entire image.

func DecodeConfig(r io.Reader) (image.Config, error)

Encode function #

Encode writes the Image m to w in JPEG 4:2:0 baseline format with the given options. Default parameters are used if a nil *[Options] is passed.

func Encode(w io.Writer, m image.Image, o *Options) error

Error method #

func (e FormatError) Error() string

Error method #

func (e UnsupportedError) Error() string

applyBlack method #

applyBlack combines d.img3 and d.blackPix into a CMYK image. The formula used depends on whether the JPEG image is stored as CMYK or YCbCrK, indicated by the APP14 (Adobe) metadata. Adobe CMYK JPEG images are inverted, where 255 means no ink instead of full ink, so we apply "v = 255 - v" at various points. Note that a double inversion is a no-op, so inversions might be implicit in the code below.

func (d *decoder) applyBlack() (image.Image, error)

convertToRGB method #

func (d *decoder) convertToRGB() (image.Image, error)

decode method #

decode reads a JPEG image from r and returns it as an image.Image.

func (d *decoder) decode(r io.Reader, configOnly bool) (image.Image, error)

decodeBit method #

func (d *decoder) decodeBit() (bool, error)

decodeBits method #

func (d *decoder) decodeBits(n int32) (uint32, error)

decodeHuffman method #

decodeHuffman returns the next Huffman-coded value from the bit-stream, decoded according to h.

func (d *decoder) decodeHuffman(h *huffman) (uint8, error)

div function #

div returns a/b rounded to the nearest integer, instead of rounded to zero.

func div(a int32, b int32) int32

emit method #

emit emits the least significant nBits bits of bits to the bit-stream. The precondition is bits < 1<

func (e *encoder) emit(bits uint32, nBits uint32)

emitHuff method #

emitHuff emits the given value with the given Huffman encoder.

func (e *encoder) emitHuff(h huffIndex, value int32)

emitHuffRLE method #

emitHuffRLE emits a run of runLength copies of value encoded with the given Huffman encoder.

func (e *encoder) emitHuffRLE(h huffIndex, runLength int32, value int32)

ensureNBits method #

ensureNBits reads bytes from the byte buffer to ensure that d.bits.n is at least n. For best performance (avoiding function calls inside hot loops), the caller is the one responsible for first checking that d.bits.n < n.

func (d *decoder) ensureNBits(n int32) error

fdct function #

fdct performs a forward DCT on an 8x8 block of coefficients, including a level shift.

func fdct(b *block)

fill method #

fill fills up the d.bytes.buf buffer from the underlying io.Reader. It should only be called when there are no unread bytes in d.bytes.

func (d *decoder) fill() error

findRST method #

findRST advances past the next RST restart marker that matches expectedRST. Other than I/O errors, it is also an error if we encounter an {0xFF, M} two-byte marker sequence where M is not 0x00, 0xFF or the expectedRST. This is similar to libjpeg's jdmarker.c's next_marker function. https://github.com/libjpeg-turbo/libjpeg-turbo/blob/2dfe6c0fe9e18671105e94f7cbf044d4a1d157e6/jdmarker.c#L892-L935 Precondition: d.tmp[:2] holds the next two bytes of JPEG-encoded input (input in the d.readFull sense).

func (d *decoder) findRST(expectedRST uint8) error

flush method #

func (e *encoder) flush()

grayToY function #

grayToY stores the 8x8 region of m whose top-left corner is p in yBlock.

func grayToY(m *image.Gray, p image.Point, yBlock *block)

idct function #

idct performs a 2-D Inverse Discrete Cosine Transformation. The input coefficients should already have been multiplied by the appropriate quantization table. We use fixed-point computation, with the number of bits for the fractional component varying over the intermediate stages. For more on the actual algorithm, see Z. Wang, "Fast algorithms for the discrete W transform and for the discrete Fourier transform", IEEE Trans. on ASSP, Vol. ASSP- 32, pp. 803-816, Aug. 1984.

func idct(src *block)

ignore method #

ignore ignores the next n bytes.

func (d *decoder) ignore(n int) error

init function #

func init()

init function #

func init()

init method #

func (h *huffmanLUT) init(s huffmanSpec)

isRGB method #

func (d *decoder) isRGB() bool

makeImg method #

makeImg allocates and initializes the destination image.

func (d *decoder) makeImg(mxx int, myy int)

processApp0Marker method #

func (d *decoder) processApp0Marker(n int) error

processApp14Marker method #

func (d *decoder) processApp14Marker(n int) error

processDHT method #

processDHT processes a Define Huffman Table marker, and initializes a huffman struct from its contents. Specified in section B.2.4.2.

func (d *decoder) processDHT(n int) error

processDQT method #

Specified in section B.2.4.1.

func (d *decoder) processDQT(n int) error

processDRI method #

Specified in section B.2.4.4.

func (d *decoder) processDRI(n int) error

processSOF method #

Specified in section B.2.2.

func (d *decoder) processSOF(n int) error

processSOS method #

Specified in section B.2.3.

func (d *decoder) processSOS(n int) error

readByte method #

readByte returns the next byte, whether buffered or not buffered. It does not care about byte stuffing.

func (d *decoder) readByte() (x byte, err error)

readByteStuffedByte method #

readByteStuffedByte is like readByte but is for byte-stuffed Huffman data.

func (d *decoder) readByteStuffedByte() (x byte, err error)

readFull method #

readFull reads exactly len(p) bytes into p. It does not care about byte stuffing.

func (d *decoder) readFull(p []byte) error

receiveExtend method #

receiveExtend is the composition of RECEIVE and EXTEND, specified in section F.2.2.1.

func (d *decoder) receiveExtend(t uint8) (int32, error)

reconstructBlock method #

reconstructBlock dequantizes, performs the inverse DCT and stores the block to the image.

func (d *decoder) reconstructBlock(b *block, bx int, by int, compIndex int) error

reconstructProgressiveImage method #

func (d *decoder) reconstructProgressiveImage() error

refine method #

refine decodes a successive approximation refinement block, as specified in section G.1.2.

func (d *decoder) refine(b *block, h *huffman, zigStart int32, zigEnd int32, delta int32) error

refineNonZeroes method #

refineNonZeroes refines non-zero entries of b in zig-zag order. If nz >= 0, the first nz zero entries are skipped over.

func (d *decoder) refineNonZeroes(b *block, zig int32, zigEnd int32, nz int32, delta int32) (int32, error)

rgbaToYCbCr function #

rgbaToYCbCr is a specialized version of toYCbCr for image.RGBA images.

func rgbaToYCbCr(m *image.RGBA, p image.Point, yBlock *block, cbBlock *block, crBlock *block)

scale function #

scale scales the 16x16 region represented by the 4 src blocks to the 8x8 dst block.

func scale(dst *block, src *[4]block)

toYCbCr function #

toYCbCr converts the 8x8 region of m whose top-left corner is p to its YCbCr values.

func toYCbCr(m image.Image, p image.Point, yBlock *block, cbBlock *block, crBlock *block)

unreadByteStuffedByte method #

unreadByteStuffedByte undoes the most recent readByteStuffedByte call, giving a byte of data back from d.bits to d.bytes. The Huffman look-up table requires at least 8 bits for look-up, which means that Huffman decoding can sometimes overshoot and read one or two too many bytes. Two-byte overshoot can happen when expecting to read a 0xff 0x00 byte-stuffed byte.

func (d *decoder) unreadByteStuffedByte()

write method #

func (e *encoder) write(p []byte)

writeBlock method #

writeBlock writes a block of pixel data using the given quantization table, returning the post-quantized DC value of the DCT-transformed block. b is in natural (not zig-zag) order.

func (e *encoder) writeBlock(b *block, q quantIndex, prevDC int32) int32

writeByte method #

func (e *encoder) writeByte(b byte)

writeDHT method #

writeDHT writes the Define Huffman Table marker.

func (e *encoder) writeDHT(nComponent int)

writeDQT method #

writeDQT writes the Define Quantization Table marker.

func (e *encoder) writeDQT()

writeMarkerHeader method #

writeMarkerHeader writes the header for a marker with the given length.

func (e *encoder) writeMarkerHeader(marker uint8, markerlen int)

writeSOF0 method #

writeSOF0 writes the Start Of Frame (Baseline Sequential) marker.

func (e *encoder) writeSOF0(size image.Point, nComponent int)

writeSOS method #

writeSOS writes the StartOfScan marker.

func (e *encoder) writeSOS(m image.Image)

yCbCrToYCbCr function #

yCbCrToYCbCr is a specialized version of toYCbCr for image.YCbCr images.

func yCbCrToYCbCr(m *image.YCbCr, p image.Point, yBlock *block, cbBlock *block, crBlock *block)

Generated with Arrow