Struct Huffman

Source
pub struct Huffman {
Show 23 fields pub buffer_put_bits: i32, pub buffer_put_buffer: i32, pub image_height: i32, pub image_width: i32, pub dc_matrix0: Vec<Vec<i32>>, pub ac_matrix0: Vec<Vec<i32>>, pub dc_matrix1: Vec<Vec<i32>>, pub ac_matrix1: Vec<Vec<i32>>, pub dc_matrix: Vec<Vec<Vec<i32>>>, pub ac_matrix: Vec<Vec<Vec<i32>>>, pub code: i32, pub num_of_dc_tables: i32, pub num_of_ac_tables: i32, pub bits_dc_luminance: Vec<i32>, pub val_dc_luminance: Vec<i32>, pub bits_dc_chrominance: Vec<i32>, pub val_dc_chrominance: Vec<i32>, pub bits_ac_luminance: Vec<i32>, pub val_ac_luminance: Vec<i32>, pub bits_ac_chrominance: Vec<i32>, pub val_ac_chrominance: Vec<i32>, pub bits: Vec<Vec<i32>>, pub val: Vec<Vec<i32>>,
}
Expand description

Represents a Huffman coding structure for encoding Discrete Cosine Transform coefficients.

This structure is used to perform Huffman encoding on quantized DCT coefficients obtained from image processing operations. It includes tables for DC and AC components, as well as methods for Huffman block encoding.

Fields§

§buffer_put_bits: i32

Number of bits in the buffer to be written.

§buffer_put_buffer: i32

Buffer containing bits to be written.

§image_height: i32

Height of the image.

§image_width: i32

Width of the image.

§dc_matrix0: Vec<Vec<i32>>

DC matrix for luminance component.

§ac_matrix0: Vec<Vec<i32>>

AC matrix for luminance component.

§dc_matrix1: Vec<Vec<i32>>

DC matrix for chrominance component.

§ac_matrix1: Vec<Vec<i32>>

AC matrix for chrominance component.

§dc_matrix: Vec<Vec<Vec<i32>>>

3D array storing DC matrices for luminance and chrominance components.

§ac_matrix: Vec<Vec<Vec<i32>>>

3D array storing AC matrices for luminance and chrominance components.

§code: i32

Huffman code.

§num_of_dc_tables: i32

Number of DC tables.

§num_of_ac_tables: i32

Number of AC tables.

§bits_dc_luminance: Vec<i32>

Bits for DC luminance component.

§val_dc_luminance: Vec<i32>

Values for DC luminance component.

§bits_dc_chrominance: Vec<i32>

Bits for DC chrominance component.

§val_dc_chrominance: Vec<i32>

Values for DC chrominance component.

§bits_ac_luminance: Vec<i32>

Bits for AC luminance component.

§val_ac_luminance: Vec<i32>

Values for AC luminance component.

§bits_ac_chrominance: Vec<i32>

Bits for AC chrominance component.

§val_ac_chrominance: Vec<i32>

Values for AC chrominance component.

§bits: Vec<Vec<i32>>

Vector storing bits for Huffman encoding.

§val: Vec<Vec<i32>>

Vector storing values for Huffman encoding.

Implementations§

Source§

impl Huffman

Source

pub fn new(width: i32, height: i32) -> Huffman

Creates a new Huffman struct with predefined tables and settings.

This method is used to initialize a Huffman struct with default values for Huffman encoding of Discrete Cosine Transform (DCT) coefficients. The Huffman tables for DC and AC components are pre-defined based on the JPEG standard.

§Arguments
  • width - Width of the image.
  • height - Height of the image.
§Returns

A Huffman struct initialized with default values for Huffman encoding.

§Examples
use stegano::jpeg::huff::Huffman;

// Assuming width and height are the dimensions of the image
let width = 640;
let height = 480;
let huffman_encoder = Huffman::new(width, height);

// Verify the initialized Huffman struct
println!("Huffman Encoder initialized: {:?}", huffman_encoder);

// Assertions for the initialized fields of the Huffman struct
assert_eq!(huffman_encoder.buffer_put_bits, 0);
assert_eq!(huffman_encoder.buffer_put_buffer, 0);
assert_eq!(huffman_encoder.image_height, 480);
assert_eq!(huffman_encoder.image_width, 640);
assert_eq!(huffman_encoder.dc_matrix0, vec![vec![0; 2]; 12]);
assert_eq!(huffman_encoder.ac_matrix0, vec![vec![0; 2]; 255]);
assert_eq!(huffman_encoder.dc_matrix1, vec![vec![0; 2]; 12]);
assert_eq!(huffman_encoder.ac_matrix1, vec![vec![0; 2]; 255]);
assert_eq!(huffman_encoder.dc_matrix, vec![vec![vec![0; 2]; 12]; 2]);
assert_eq!(huffman_encoder.ac_matrix, vec![vec![vec![0; 2]; 255]; 2]);
assert_eq!(huffman_encoder.code, 0);
assert_eq!(huffman_encoder.num_of_dc_tables, 0);
assert_eq!(huffman_encoder.num_of_ac_tables, 0);
assert_eq!(huffman_encoder.bits_dc_luminance, vec![0, 1, 5, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0]);
assert_eq!(huffman_encoder.val_dc_luminance, (0..12).collect::<Vec<_>>());
assert_eq!(huffman_encoder.bits_dc_chrominance, vec![0, 1, 3, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0]);
assert_eq!(huffman_encoder.val_dc_chrominance, (0..12).collect::<Vec<_>>());
assert_eq!(huffman_encoder.bits_ac_luminance, vec![0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 0, 0, 1]);
assert_eq!(huffman_encoder.val_ac_luminance.len(), 162);
assert_eq!(huffman_encoder.bits_ac_chrominance, vec![0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 0, 0, 0, 1]);
assert_eq!(huffman_encoder.val_ac_chrominance.len(), 162);
assert_eq!(huffman_encoder.bits.len(), 4);
assert_eq!(huffman_encoder.val.len(), 4);

The resulting Huffman struct contains pre-defined Huffman tables for luminance and chrominance components, along with other necessary settings for Huffman encoding of DCT coefficients.

Source

pub fn huffman_block_encoder( &mut self, out_stream: &mut dyn Write, zigzag: &[i32; 64], prec: i32, dc_code: i32, ac_code: i32, )

Huffman block encoder for encoding DC and AC coefficients.

This method encodes a block of Discrete Cosine Transform (DCT) coefficients using Huffman encoding. It processes both the DC and AC components of the block and writes the encoded data to the specified output stream.

§Arguments
  • out_stream - A mutable reference to a trait object implementing the Write trait. The encoded data will be written to this stream.
  • zigzag - A reference to a slice containing the zigzag-ordered DCT coefficients for the block.
  • prec - The predictor value for DC encoding.
  • dc_code - The DC Huffman table index.
  • ac_code - The AC Huffman table index.
§Panics

This method panics if the provided Huffman table indices are out of bounds.

§Examples
use stegano::jpeg::huff::Huffman; // Replace with the actual crate name
use std::io::Cursor;

// Assuming huffman_encoder is a properly initialized Huffman struct
let mut huffman_encoder = Huffman::new(640, 480);

// Sample zigzag-ordered DCT coefficients and other required parameters
let zigzag_coefficients: [i32; 64] = [
    20, -2, 1, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0,
];
let predictor = 0;
let dc_table_index = 0;
let ac_table_index = 1;

// Create a buffer to hold the encoded data
let mut encoded_buffer = Vec::new();

// Encode the block
huffman_encoder.huffman_block_encoder(&mut encoded_buffer, &zigzag_coefficients, predictor, dc_table_index, ac_table_index);

// Verify the encoded data
assert!(!encoded_buffer.is_empty(), "Encoded buffer should not be empty");
Source

pub fn buffer_it(&mut self, out_stream: &mut dyn Write, code: i32, size: i32)

Writes a Huffman-encoded code to the output stream with specified size.

This function is responsible for buffering Huffman-encoded codes before writing them to the output stream.

§Arguments
  • out_stream - A mutable reference to a type implementing the Write trait, where the encoded data will be written.
  • code - The Huffman-encoded code to be written.
  • size - The size (number of bits) of the Huffman-encoded code.
§Panics

Panics if the output stream encounters an error during writing.

§Examples
use std::io::Cursor;
use stegano::jpeg::huff::Huffman;

let mut huffman_encoder = Huffman::new(8, 8);
let mut output_buffer = Cursor::new(Vec::new());

// Example Huffman-encoded code and size
let code = 0b110110; // 6 bits
let size = 6;

// Assuming code and size are properly initialized
huffman_encoder.buffer_it(&mut output_buffer, code, size);

assert_eq!(huffman_encoder.bits.len(), 4);
assert_eq!(huffman_encoder.buffer_put_bits, 6);
assert_eq!(huffman_encoder.buffer_put_buffer, 14155776);
Source

pub fn flush_buffer(&mut self, out_stream: &mut dyn Write)

Flushes the internal buffer to the output stream, writing any remaining bits.

This function is typically called at the end of encoding to ensure that all bits are written to the output stream.

§Arguments
  • out_stream - A mutable reference to a type implementing the Write trait, where the encoded data will be written.
§Examples
use std::io::Cursor;
use stegano::jpeg::huff::Huffman;

// Create a new Huffman instance
let mut huffman_encoder = Huffman::new(8, 8);

// Create a buffer for the output stream
let mut output_buffer = Cursor::new(Vec::new());

// Example Huffman-encoded code and size
let code = 0b110110; // 6 bits
let size = 6;

// Call the buffer_it function
huffman_encoder.buffer_it(&mut output_buffer, code, size);

// Flush the buffer
huffman_encoder.flush_buffer(&mut output_buffer);

// Check the buffered output length after flushing
let buffered_length = output_buffer.get_ref().len();

// Assertions for the fields of the Huffman struct after flushing
assert_eq!(huffman_encoder.buffer_put_bits, 0);
assert_eq!(huffman_encoder.buffer_put_buffer, 0);

// Assertions for the buffered output length after flushing
assert_eq!(buffered_length, 1);

Trait Implementations§

Source§

impl Clone for Huffman

Source§

fn clone(&self) -> Huffman

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Huffman

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.