Expand description
Read and write quantized DCT coefficients in baseline JPEG files.
This crate provides direct access to the quantized DCT coefficients stored in the entropy-coded data of a baseline JPEG. It is useful for steganography, watermarking, forensic analysis, and JPEG-domain signal processing where you need to read or modify coefficients without fully decoding the image to pixel values.
§What this crate does
JPEG compresses images by dividing them into 8×8 pixel blocks, applying a Discrete Cosine Transform (DCT) to each block, quantizing the resulting coefficients, and then entropy-coding them with Huffman coding. This crate parses the entropy-coded stream, decodes the Huffman symbols, reconstructs the quantized coefficient values, and lets you read or modify them before re-encoding everything back into a valid JPEG byte stream.
§What this crate does NOT do
- It does not decode pixel values (no IDCT, no dequantisation).
- It does not support progressive JPEG (SOF2), lossless JPEG (SOF3), or arithmetic coding (SOF9). Passing such files returns an error.
- It does not support JPEG 2000.
§Supported JPEG variants
- Baseline DCT (SOF0) — the most common variant
- Extended sequential DCT (SOF1) — treated identically to SOF0
- Grayscale (1 component) and colour (3 components, typically YCbCr)
- All standard chroma subsampling ratios (4:4:4, 4:2:2, 4:2:0, etc.)
- EXIF and JFIF headers
- Restart markers (DRI / RST0–RST7)
§Example
use dct_io::{read_coefficients, write_coefficients};
let jpeg = std::fs::read("photo.jpg").unwrap();
let mut coeffs = read_coefficients(&jpeg).unwrap();
// Flip the LSB of every eligible AC coefficient in the first component.
for block in &mut coeffs.components[0].blocks {
for coeff in block[1..].iter_mut() {
if *coeff != 0 {
*coeff ^= 1;
}
}
}
let modified = write_coefficients(&jpeg, &coeffs).unwrap();
std::fs::write("photo_modified.jpg", modified).unwrap();Structs§
- Component
Coefficients - Quantized DCT coefficients for a single component (Y, Cb, or Cr).
- Component
Info - Metadata for a single image component, as read from the SOF marker.
- Jpeg
Coefficients - Quantized DCT coefficients for all components in a JPEG image.
- Jpeg
Info - Image metadata extracted from a JPEG without decoding the entropy stream.
Enums§
- DctError
- Errors returned by this crate.
Functions§
- block_
count - Return the number of 8×8 DCT blocks per component in a JPEG.
- eligible_
ac_ count - Count the number of AC coefficients with
|v| >= 2across all components. - inspect
- Inspect a JPEG and return image metadata without decoding the entropy stream.
- read_
coefficients - Decode the quantized DCT coefficients from a baseline JPEG.
- write_
coefficients - Re-encode a JPEG with modified DCT coefficients.