Imports #
"image"
"image/color"
"image/internal/imageutil"
"io"
"image"
"bufio"
"errors"
"image"
"image/color"
"io"
"io"
"image"
"image/color"
"image/internal/imageutil"
"io"
"image"
"bufio"
"errors"
"image"
"image/color"
"io"
"io"
DefaultQuality is the default quality encoding parameter.
const DefaultQuality = 75
const acTable = 1
See https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#Adobe
const adobeTransformUnknown = 0
See https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#Adobe
const adobeTransformYCbCr = 1
See https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#Adobe
const adobeTransformYCbCrK = 2
"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
const app14Marker = 0xee
const app15Marker = 0xef
bitCount counts the number of bits needed to hold an integer.
var bitCount = [256]byte{...}
const blockSize = 64
const centerJSample = 128
const comMarker = 0xfe
const constBits = 13
const dcTable = 0
const dhtMarker = 0xc4
const dqtMarker = 0xdb
const driMarker = 0xdd
const eoiMarker = 0xd9
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 means that an unexpected EOF occurred while decoding Huffman data.
var errShortHuffmanData = *ast.CallExpr
var errUnsupportedSubsamplingRatio = *ast.CallExpr
Trigonometric constants in 13-bit fixed point format.
const fix_0_298631336 = 2446
Trigonometric constants in 13-bit fixed point format.
const fix_0_390180644 = 3196
Trigonometric constants in 13-bit fixed point format.
const fix_0_541196100 = 4433
Trigonometric constants in 13-bit fixed point format.
const fix_0_765366865 = 6270
Trigonometric constants in 13-bit fixed point format.
const fix_0_899976223 = 7373
Trigonometric constants in 13-bit fixed point format.
const fix_1_175875602 = 9633
Trigonometric constants in 13-bit fixed point format.
const fix_1_501321110 = 12299
Trigonometric constants in 13-bit fixed point format.
const fix_1_847759065 = 15137
Trigonometric constants in 13-bit fixed point format.
const fix_1_961570560 = 16069
Trigonometric constants in 13-bit fixed point format.
const fix_2_053119869 = 16819
Trigonometric constants in 13-bit fixed point format.
const fix_2_562915447 = 20995
Trigonometric constants in 13-bit fixed point format.
const fix_3_072711026 = 25172
const huffIndexChrominanceAC
const huffIndexChrominanceDC
const huffIndexLuminanceAC
const huffIndexLuminanceDC huffIndex = iota
lutSize is the log-2 size of the Huffman decoder's look-up table.
const lutSize = 8
maxCodeLength is the maximum (inclusive) number of bits in a Huffman code.
const maxCodeLength = 16
const maxComponents = 4
maxNCodes is the maximum (inclusive) number of codes in a Huffman tree.
const maxNCodes = 256
const maxTc = 1
const maxTh = 3
const maxTq = 3
const nHuffIndex
const nQuantIndex
const pass1Bits = 2
const quantIndexChrominance
const quantIndexLuminance quantIndex = iota
const r2 = 181
const rst0Marker = 0xd0
const rst7Marker = 0xd7
const sof0Marker = 0xc0
const sof1Marker = 0xc1
const sof2Marker = 0xc2
const soiMarker = 0xd8
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 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{...}
const sosMarker = 0xda
theHuffmanLUT are compiled representations of theHuffmanSpec.
var theHuffmanLUT [4]huffmanLUT
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 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 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{...}
const w1 = 2841
const w1mw7 = *ast.BinaryExpr
const w1pw7 = *ast.BinaryExpr
const w2 = 2676
const w2mw6 = *ast.BinaryExpr
const w2pw6 = *ast.BinaryExpr
const w3 = 2408
const w3mw5 = *ast.BinaryExpr
const w3pw5 = *ast.BinaryExpr
const w5 = 1609
const w6 = 1108
const w7 = 565
A FormatError reports that the input is not a valid JPEG.
type FormatError string
An UnsupportedError reports that the input uses a valid but unimplemented JPEG feature.
type UnsupportedError string
type block [blockSize]int32
type huffIndex int
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
type quantIndex int
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 is a buffered writer.
type writer interface {
Flush() error
io.Writer
io.ByteWriter
}
Options are the encoding parameters. Quality ranges from 1 to 100 inclusive, higher is better.
type Options struct {
Quality int
}
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 specification, specified in section B.2.2.
type component struct {
h int
v int
c uint8
tq uint8
}
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 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 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 specifies a Huffman encoding.
type huffmanSpec struct {
count [16]byte
value []byte
}
Decode reads a JPEG image from r and returns it as an [image.Image].
func Decode(r io.Reader) (image.Image, error)
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 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
func (e FormatError) Error() string
func (e UnsupportedError) Error() string
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)
func (d *decoder) convertToRGB() (image.Image, error)
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)
func (d *decoder) decodeBit() (bool, error)
func (d *decoder) decodeBits(n int32) (uint32, error)
decodeHuffman returns the next Huffman-coded value from the bit-stream, decoded according to h.
func (d *decoder) decodeHuffman(h *huffman) (uint8, error)
div returns a/b rounded to the nearest integer, instead of rounded to zero.
func div(a int32, b int32) int32
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 emits the given value with the given Huffman encoder.
func (e *encoder) emitHuff(h huffIndex, value int32)
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 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 performs a forward DCT on an 8x8 block of coefficients, including a level shift.
func fdct(b *block)
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 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
func (e *encoder) flush()
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 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 ignores the next n bytes.
func (d *decoder) ignore(n int) error
func init()
func init()
func (h *huffmanLUT) init(s huffmanSpec)
func (d *decoder) isRGB() bool
makeImg allocates and initializes the destination image.
func (d *decoder) makeImg(mxx int, myy int)
func (d *decoder) processApp0Marker(n int) error
func (d *decoder) processApp14Marker(n int) error
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
Specified in section B.2.4.1.
func (d *decoder) processDQT(n int) error
Specified in section B.2.4.4.
func (d *decoder) processDRI(n int) error
Specified in section B.2.2.
func (d *decoder) processSOF(n int) error
Specified in section B.2.3.
func (d *decoder) processSOS(n int) error
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 is like readByte but is for byte-stuffed Huffman data.
func (d *decoder) readByteStuffedByte() (x byte, err error)
readFull reads exactly len(p) bytes into p. It does not care about byte stuffing.
func (d *decoder) readFull(p []byte) error
receiveExtend is the composition of RECEIVE and EXTEND, specified in section F.2.2.1.
func (d *decoder) receiveExtend(t uint8) (int32, error)
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
func (d *decoder) reconstructProgressiveImage() error
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 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 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 scales the 16x16 region represented by the 4 src blocks to the 8x8 dst block.
func scale(dst *block, src *[4]block)
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 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()
func (e *encoder) write(p []byte)
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
func (e *encoder) writeByte(b byte)
writeDHT writes the Define Huffman Table marker.
func (e *encoder) writeDHT(nComponent int)
writeDQT writes the Define Quantization Table marker.
func (e *encoder) writeDQT()
writeMarkerHeader writes the header for a marker with the given length.
func (e *encoder) writeMarkerHeader(marker uint8, markerlen int)
writeSOF0 writes the Start Of Frame (Baseline Sequential) marker.
func (e *encoder) writeSOF0(size image.Point, nComponent int)
writeSOS writes the StartOfScan marker.
func (e *encoder) writeSOS(m image.Image)
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