Documentation
¶
Overview ¶
Package codec provides the JPEG adapter layer for the steganalysis package.
This package wraps the github.com/0verkilll/jpeg package to implement JPEG encoding, decoding, DCT coefficient extraction, and image processing operations required by the steganalysis pipeline.
The adapters follow the Adapter Pattern to bridge the jpeg package's concrete types with the steganalysis package's interface abstractions, maintaining the Dependency Inversion Principle.
Adapters provided:
- JPEGCodec: JPEG encoding, decoding, quality detection, and baseline check
- CoefficientExtractor: DCT coefficient extraction from JPEG data
- ImageProcessor: Color space conversion, cropping, and blur operations
This package also contains dimension validation helpers (overflow checks, buffer length validation) used by the adapter methods.
Factory functions:
- NewJPEGCodec() creates a JPEGCodec adapter
- NewCoefficientExtractor() creates a CoefficientExtractor adapter
- NewImageProcessor() creates an ImageProcessor adapter
Index ¶
Constants ¶
const ( // ErrCodeDecodeFailed is the error code returned when JPEG decoding fails // due to invalid or corrupted image data. ErrCodeDecodeFailed = "decode_failed" // ErrCodeNotBaseline is the error code returned when the JPEG image is not // baseline encoded (SOF0), which is required for F5 steganalysis. ErrCodeNotBaseline = "not_baseline_jpeg" // ErrCodeInvalidDimensions is the error code returned when image width or // height is zero or negative. ErrCodeInvalidDimensions = "invalid_dimensions" // ErrCodeNilPixelData is the error code returned when nil pixel data is // passed to an encoding or processing operation. ErrCodeNilPixelData = "nil_pixel_data" // ErrCodeInvalidDimensionsChannels is the error code returned when image // dimensions or channel count are invalid (zero or negative). ErrCodeInvalidDimensionsChannels = "invalid_dimensions_or_channels" // ErrCodePixelLengthMismatch is the error code returned when the pixel // buffer length does not match the expected size for the given dimensions. ErrCodePixelLengthMismatch = "pixel_length_mismatch" // ErrCodeQualityOutOfRange is the error code returned when the JPEG // quality factor is outside the valid [1, 100] range. ErrCodeQualityOutOfRange = "quality_out_of_range" // ErrCodeEncoderQualityFailed is the error code returned when setting the // encoder quality factor fails. ErrCodeEncoderQualityFailed = "encoder_quality_failed" // ErrCodeEncodingFailed is the error code returned when JPEG encoding // fails after quality and dimension validation have passed. ErrCodeEncodingFailed = "encoding_failed" // ErrCodeNilRGBPixels is the error code returned when nil RGB pixel data // is passed to a color space conversion operation. ErrCodeNilRGBPixels = "nil_rgb_pixels" // ErrCodeNilYCbCrPixels is the error code returned when nil YCbCr pixel // data is passed to a color space conversion operation. ErrCodeNilYCbCrPixels = "nil_ycbcr_pixels" // ErrCodeNegativeCropSize is the error code returned when a negative crop // size is specified for image cropping. ErrCodeNegativeCropSize = "negative_crop_size" // ErrCodeCropTooLarge is the error code returned when the crop size would // remove all pixels from the image (cropSize >= width or height for the // top-left-only crop semantics; see CropImage doc). ErrCodeCropTooLarge = "crop_too_large" // ErrCodeCoeffExtraction is the error code returned when DCT coefficient // extraction from JPEG data fails. ErrCodeCoeffExtraction = "coeff_extraction" // ErrCodeNilEncoder is the error code returned when the JPEG encoder // instance is nil, indicating initialization failure. ErrCodeNilEncoder = "nil_encoder" // ErrCodeDimensionOverflow is the error code returned when dimension // calculations (width * height * channels) would cause integer overflow. ErrCodeDimensionOverflow = "dimension_overflow" // ErrCodeDimensionTooLarge is the error code returned when a single // dimension (width or height) exceeds MaxImageDimension. ErrCodeDimensionTooLarge = "dimension_too_large" // ErrCodeInvalidEpsilon is the error code returned when the blur epsilon // parameter is NaN, Inf, or outside the valid [0, 1] range. ErrCodeInvalidEpsilon = "invalid_epsilon" // ErrCodeInvalidChannels is the error code returned when the channel // count is zero or negative. ErrCodeInvalidChannels = "invalid_channels" // ErrCodePixelCountTooLarge is the error code returned when the total // pixel count (width * height) exceeds MaxPixelCount. ErrCodePixelCountTooLarge = "pixel_count_too_large" // ErrCodeInvalidQuantTable is the error code returned when an explicit // quantization table passed to EncodeWithTables does not have 64 entries. ErrCodeInvalidQuantTable = "invalid_quant_table" )
const MaxImageDimension = 65535
MaxImageDimension is the maximum supported image dimension (width or height). This prevents integer overflow in width * height calculations.
const MaxPixelCount = 100_000_000
MaxPixelCount is the maximum total number of pixels (width * height) allowed. This prevents excessive memory allocation for images with extreme aspect ratios.
Variables ¶
This section is empty.
Functions ¶
func CheckDimensionOverflow ¶
CheckDimensionOverflow validates image dimensions and checks for integer overflow in the calculation width * height * channels.
func GetTranslator ¶
func GetTranslator() translator
GetTranslator returns the current translator, or nil if not set.
func SetTranslator ¶
func SetTranslator(t translator)
SetTranslator sets the translator for this package. Pass nil to disable translations and use default English messages. The root steganalysis package calls this to propagate its translator.
Types ¶
type CoefficientExtractor ¶
type CoefficientExtractor interface {
// Extract retrieves all DCT coefficients from a JPEG file.
//
// The returned coefficients include position information for per-mode analysis.
// Coefficients are returned for all color components (Y, Cb, Cr).
//
// Parameters:
// - data: Complete JPEG file data
//
// Returns:
// - coefficients: Slice of DCT coefficients with position metadata
// - error: If extraction fails or data is not valid JPEG
Extract(data []byte) ([]DCTCoefficient, error)
}
CoefficientExtractor extracts DCT coefficients from JPEG data.
DCT coefficient access is essential for steganalysis but not available through standard JPEG libraries. This interface abstracts coefficient extraction to support different JPEG parsers.
Implementations must be safe for concurrent use.
func NewCoefficientExtractor ¶
func NewCoefficientExtractor() CoefficientExtractor
NewCoefficientExtractor creates a new CoefficientExtractor adapter that wraps the jpeg package's DCT coefficient extraction functionality.
type DCTCoefficient ¶
DCTCoefficient represents a single DCT coefficient with its position information in the 8x8 DCT block. This is a codec-local copy of the root steganalysis.DCTCoefficient type to avoid circular imports. The root package converts between this type and steganalysis.DCTCoefficient when wiring the facade.
type Error ¶
Error represents an error from the codec package. It carries a code string for programmatic handling and a human-readable message.
type ImageProcessor ¶
type ImageProcessor interface {
// ConvertToYCbCr converts RGB pixels to YCbCr color space.
ConvertToYCbCr(rgbPixels []byte, width, height int) (ycbcrPixels []byte, err error)
// ConvertToRGB converts YCbCr pixels to RGB color space.
ConvertToRGB(ycbcrPixels []byte, width, height int) (rgbPixels []byte, err error)
// CropImage removes pixels from image edges.
CropImage(pixels []byte, width, height, channels, cropSize int) (
croppedPixels []byte, newWidth, newHeight int, err error,
)
// ApplyBlur applies a smoothing blur to reduce quantization noise.
ApplyBlur(pixels []byte, width, height, channels int, epsilon float64) (
blurredPixels []byte, err error,
)
}
ImageProcessor provides image manipulation operations.
These operations are used in the crop-and-recompress pipeline to estimate the cover-image histogram. The interface abstracts image processing to enable different implementations and testing.
Implementations must be safe for concurrent use.
func NewImageProcessor ¶
func NewImageProcessor() ImageProcessor
NewImageProcessor creates a new ImageProcessor adapter that provides image manipulation operations using the jpeg package's color conversion.
type JPEGCodec ¶
type JPEGCodec interface {
// Decode decompresses JPEG data to raw pixel values.
//
// The returned pixels are in RGB format, row-major order:
// - pixels[y*width*3 + x*3 + 0] = Red
// - pixels[y*width*3 + x*3 + 1] = Green
// - pixels[y*width*3 + x*3 + 2] = Blue
//
// Parameters:
// - data: Complete JPEG file data
//
// Returns:
// - pixels: Raw RGB pixel data
// - width: Image width in pixels
// - height: Image height in pixels
// - error: If decoding fails or data is not valid JPEG
Decode(data []byte) (pixels []byte, width, height int, err error)
// Encode compresses raw pixel data to JPEG format.
//
// The input pixels must be in RGB format, row-major order.
//
// Parameters:
// - pixels: Raw RGB pixel data
// - width: Image width in pixels
// - height: Image height in pixels
// - quality: JPEG quality factor (1-100)
//
// Returns:
// - data: Complete JPEG file data
// - error: If encoding fails or dimensions are invalid
Encode(pixels []byte, width, height, quality int) (data []byte, err error)
// QualityFactor returns the detected quality factor of the last decoded image.
//
// This method must be called after Decode() to get meaningful results.
// The quality factor is estimated from the quantization tables.
//
// Returns:
// - quality: Estimated quality factor (1-100), or 0 if not detected
QualityFactor() int
// IsBaseline returns whether the last decoded image was baseline JPEG.
//
// This method must be called after Decode() to get meaningful results.
// F5 steganalysis only works on baseline JPEGs (SOF0).
//
// Returns:
// - baseline: True if baseline JPEG, false otherwise
IsBaseline() bool
// QuantizationTables returns the luminance and chrominance quantization
// tables of the last decoded image. Each is a 64-entry slice in ZIGZAG
// order (as stored in the JPEG DQT marker). Either may be nil if no image
// has been decoded or the table is absent (e.g. chroma on a grayscale JPEG).
//
// Must be called after Decode(). These tables are intended to be passed to
// EncodeWithTables so a re-compression reuses the source image's actual
// quantization rather than a standard table scaled by an estimated scalar
// quality (Fridrich §3.2 cover-histogram estimation).
QuantizationTables() (luma, chroma []int)
// EncodeWithTables compresses raw RGB pixel data to JPEG format using the
// supplied quantization tables instead of deriving them from a scalar
// quality factor.
//
// lumaTable and chromaTable are 64-entry slices in ZIGZAG order (the order
// returned by QuantizationTables / the JPEG DQT marker). A nil table falls
// back to the standard-scaled table at the given fallbackQuality for that
// channel. fallbackQuality is also used for any encoder bookkeeping that
// still needs a scalar.
//
// Chroma subsampling and all other encoder behavior match Encode().
EncodeWithTables(
pixels []byte, width, height, fallbackQuality int,
lumaTable, chromaTable []int,
) (data []byte, err error)
}
JPEGCodec provides JPEG encoding, decoding, and analysis operations.
This interface abstracts the JPEG codec to enable dependency injection and testing with mock implementations. It combines encoding/decoding with quality detection to provide a unified JPEG processing interface.
Implementations must be safe for concurrent use.
func NewJPEGCodec ¶
func NewJPEGCodec() JPEGCodec
NewJPEGCodec creates a new JPEGCodec adapter that wraps the jpeg package.
The returned codec provides JPEG encoding and decoding operations suitable for the steganalysis pipeline. Decoding uses full IDCT reconstruction via the jpeg package's ImageDecoder for pixel-perfect image recovery.