Involved Source Filesfdct.gohuffman.goidct.go
Package jpeg implements a JPEG image decoder and encoder.
JPEG is defined in ITU-T T.81: https://www.w3.org/Graphics/JPEG/itu-t81.pdf.
scan.gowriter.go
Package-Level Type Names (total 15, in which 4 are exported)
Options are the encoding parameters.
Quality ranges from 1 to 100 inclusive, higher is better.
Qualityint
func Encode(w io.Writer, m image.Image, o *Options) error
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.
// accumulator.
// mask. m==1<<(n-1) when n>0, with m==0 when n==0.
// the number of unread bits in a.
adobeTransformuint8adobeTransformValidbool
As per section 4.5, there are four modes of operation (selected by the
SOF? markers): sequential DCT, progressive DCT, lossless and
hierarchical, although this implementation does not support the latter
two non-DCT modes. Sequential DCT is further split into baseline and
extended, as per section 4.11.
bitsbitsblackPix[]byteblackStrideint
bytes is a byte buffer, similar to a bufio.Reader, except that it
has to be able to unread more than 1 byte, due to byte stuffing.
Byte stuffing is specified in section F.1.2.3.
comp[4]component
// End-of-Band run, specified in section G.1.2.2.
heightinthuff[2][4]huffmanimg1*image.Grayimg3*image.YCbCrjfifboolnCompint
// Saved state between progressive-mode scans.
progressivebool
// Quantization tables, in zig-zag order.
rio.Reader
// Restart Interval.
tmp[128]bytewidthint
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.
(*T) convertToRGB() (image.Image, error)
decode reads a JPEG image from r and returns it as an image.Image.
(*T) decodeBit() (bool, error)(*T) decodeBits(n int32) (uint32, error)
decodeHuffman returns the next Huffman-coded value from the bit-stream,
decoded according to h.
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.
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.
ignore ignores the next n bytes.
(*T) isRGB() bool
makeImg allocates and initializes the destination image.
(*T) processApp0Marker(n int) error(*T) 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.
Specified in section B.2.4.1.
Specified in section B.2.4.4.
Specified in section B.2.2.
Specified in section B.2.3.
readByte returns the next byte, whether buffered or not buffered. It does
not care about byte stuffing.
readByteStuffedByte is like readByte but is for byte-stuffed Huffman data.
readFull reads exactly len(p) bytes into p. It does not care about byte
stuffing.
receiveExtend is the composition of RECEIVE and EXTEND, specified in section
F.2.2.1.
reconstructBlock dequantizes, performs the inverse DCT and stores the block
to the image.
(*T) reconstructProgressiveImage() error
refine decodes a successive approximation refinement block, as specified in
section G.1.2.
refineNonZeroes refines non-zero entries of b in zig-zag order. If nz >= 0,
the first nz zero entries are skipped over.
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.
encoder encodes an image to the JPEG format.
bits and nBits are accumulated bits to write to w.
buf is a scratch buffer.
errerror
bits and nBits are accumulated bits to write to w.
quant is the scaled quantization tables, in zig-zag order.
w is the writer to write to. err is the first error encountered during
writing. All attempted writes after the first error become no-ops.
emit emits the least significant nBits bits of bits to the bit-stream.
The precondition is bits < 1<<nBits && nBits <= 16.
emitHuff emits the given value with the given Huffman encoder.
emitHuffRLE emits a run of runLength copies of value encoded with the given
Huffman encoder.
(*T) flush()(*T) 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.
(*T) writeByte(b byte)
writeDHT writes the Define Huffman Table marker.
writeDQT writes the Define Quantization Table marker.
writeMarkerHeader writes the header for a marker with the given length.
writeSOF0 writes the Start Of Frame (Baseline Sequential) marker.
writeSOS writes the StartOfScan marker.
huffman is a Huffman decoder, specified in section C.
lut is the look-up table for the next lutSize bits in the bit-stream.
The high 8 bits of the uint16 are the encoded value. The low 8 bits
are 1 plus the code length, or 0 if the value is too large to fit in
lutSize bits.
maxCodes[i] is the maximum code of length i, or -1 if there are no
codes of that length.
minCodes[i] is the minimum code of length i, or -1 if there are no
codes of that length.
length is the number of codes in the tree.
vals are the decoded values, sorted by their encoding.
valsIndices[i] is the index into vals of minCodes[i].
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.
(*T) init(s huffmanSpec)
huffmanSpec specifies a Huffman encoding.
count[i] is the number of codes of length i bits.
value[i] is the decoded value of the i'th codeword.
Package-Level Functions (total 14, in which 3 are exported)
Decode reads a JPEG image from r and returns it as an image.Image.
DecodeConfig returns the color model and dimensions of a JPEG image without
decoding the entire image.
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.
div returns a/b rounded to the nearest integer, instead of rounded to zero.
fdct performs a forward DCT on an 8x8 block of coefficients, including a
level shift.
grayToY stores the 8x8 region of m whose top-left corner is p in yBlock.
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.
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.
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.
theHuffmanLUT are compiled representations of theHuffmanSpec.
theHuffmanSpec is the Huffman encoding specifications.
This encoder uses the same Huffman encoding for all images.
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 after converting from natural to
zig-zag order.
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).
Package-Level Constants (total 65, in which 1 are exported)
DefaultQuality is the default quality encoding parameter.
See https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#Adobe
See https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#Adobe
See https://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html#Adobe
"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
The pages are generated with Goldsv0.3.2-preview. (GOOS=darwin GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.