jpegdna.coders package¶
Module contents¶
Codecs collection for jpegdna
- class jpegdna.coders.AbstractCoder[source]¶
Bases:
abc.ABC
Abstract class for codec definition
- abstract decode(code)[source]¶
Decoding method
- Parameters
code (list|str) – Input to decode
- Returns
Decoded message
- Return type
list|str
- abstract encode(inp)[source]¶
Encoding method
- Parameters
inp (list|str) – Input to encode
- Returns
Encoded message
- Return type
list|str
- full_decode(code, *args)[source]¶
Encoding method
- Parameters
code (list|str) – Input to decode
args (any) – Decoding arguments
- Returns
Decoded message, decoding state
- Return type
list|str, any
- full_encode(inp, *args)[source]¶
Encoding method
- Parameters
inp (list|str) – Input to encode
args (any) – Encoding arguments
- Returns
Encoded message, encoding state
- Return type
list|str, any
jpegdna.coders.categorycoder module¶
Coder for categories
- class jpegdna.coders.categorycoder.ACCategoryCoder(d, lut, verbose=False)[source]¶
Bases:
jpegdna.coders.AbstractCoder
AC Category value coder
- Parameters
d (dict) – Huffman n-ary dictionnary
lut (list) – Lut matrix
verbose – bool
- Variables
verbose – Verbosity enabler
goldman_coder (jpegdna.coders.goldmancoder.GoldmanCoderDNA) – Goldman Coder
ad_bits (int) – length of the codeword for the category (initial value: 0)
code_length (int) – length of the codeword for the value (initial value: 0)
- decode(code)[source]¶
Decode the category of an AC value
Stores the number of bits it took in the stream and stores the length of the codeword for this category
- Parameters
code (str) – Sequence to be decoded
- Returns
Decoded sequence
- Return type
str
- encode(inp)[source]¶
Encode the category of an AC value
- Parameters
inp (str) – Value to be encoded
- Returns
The encoded message for the category
- Return type
str
- static find_category(value)[source]¶
Find the category of an ac value
- Parameters
value (int) – Value for which we want the category
- Returns
Category corresponding to the value
- Return type
int
- class jpegdna.coders.categorycoder.DCCategoryCoder(d, verbose=False)[source]¶
Bases:
jpegdna.coders.AbstractCoder
DC Category value coder
- Parameters
d (dict) – Huffman n-ary dictionnary
verbose – bool
- Variables
verbose – Verbosity enabler
goldman_coder (jpegdna.coders.goldmancoder.GoldmanCoderDNA) – Goldman Coder
ad_bits (int) – length of the codeword for the category (initial value: 0)
code_length (int) – length of the codeword for the value (initial value: 0)
- decode(code)[source]¶
Decode the category of a DC value
- Parameters
code (str) – Sequence to be decoded
- Returns
The decoded category
- Return type
str
- encode(inp)[source]¶
Encode the category of an DC value
- Parameters
input (str) – Category to be encoded
- Returns
The encoded message for the category
- Return type
str
- static find_category(value)[source]¶
Find the category of a dc value
- Parameters
value (int) – Value for which we want the category
- Returns
Category corresponding to the value
- Return type
int
- jpegdna.coders.categorycoder.count_run_cat(seq_coeff, lut)[source]¶
Counts the number of categories
- Parameters
seq_coeff (list(int)) – Sequence of coefficients
lut (list) – list of hexadecimal codes for categories
jpegdna.coders.coefficientcoder module¶
General AC and DC coefficients coder
- class jpegdna.coders.coefficientcoder.ACCoefficientCoder(d, lut, codebook, verbose=False)[source]¶
Bases:
jpegdna.coders.AbstractCoder
AC Coefficient General Coder
- Parameters
d (dict) – Huffman n-ary dictionnary
lut (list) – Lut matrix
verbose – bool
- Variables
verbose – Verbosity enabler
category_coder (jpegdna.coders.categorycoder.ACCategoryCoder) – category coder associated with the value coder
value_coder (jpegdna.coders.valuecoder.ValueCoder) – value coder
gold_code (list) – goldman coded values for every hexa category value
count_runcat_len (int) – length of the encoded words for the categories
- decode(code)[source]¶
Function for decoding AC coefficients
- Parameters
code (str) – Sequence to be decoded
- encode(inp)[source]¶
Function for coding AC coefficients
- Parameters
inp (list(int)) – Input to be encoded
- full_decode(code, *args)[source]¶
Encoding method
- Parameters
code (list|str) – Input to decode
args (any) – Decoding arguments
- Returns
Decoded message, decoding state
- Return type
list|str, any
- full_encode(inp, *args)[source]¶
Encoding method
- Parameters
inp (list|str) – Input to encode
args (any) – Encoding arguments
- Returns
Encoded message, encoding state
- Return type
list|str, any
- class jpegdna.coders.coefficientcoder.DCCoefficientCoder(d, codebook, verbose=False)[source]¶
Bases:
jpegdna.coders.AbstractCoder
DC Coefficient General Coder
- Parameters
d (dict) – Huffman n-ary dictionnary
verbose – bool
- Variables
verbose – Verbosity enabler
category_coder (jpegdna.coders.categorycoder.DCCategoryCoder) – category coder associated with the value coder
value_coder (jpegdna.coders.valuecoder.ValueCoder) – value coder
gold_code (list) – goldman coded values for every hexa category value in the lut matrix
count_cat_len (int) – length of the encoded word for the category
- decode(code)[source]¶
Function for decoding a DC coefficient
- Parameters
code (str) – Sequence to be decoded
- full_decode(code, *args)[source]¶
Encoding method
- Parameters
code (list|str) – Input to decode
args (any) – Decoding arguments
- Returns
Decoded message, decoding state
- Return type
list|str, any
- full_encode(inp, *args)[source]¶
Encoding method
- Parameters
inp (list|str) – Input to encode
args (any) – Encoding arguments
- Returns
Encoded message, encoding state
- Return type
list|str, any
jpegdna.coders.goldmancoder module¶
Goldman coder
- class jpegdna.coders.goldmancoder.GoldmanCoder(alphabet, verbose=False)[source]¶
Bases:
jpegdna.coders.AbstractCoder
Goldman coder
- Parameters
alphabet (list) – alphabet for encoding
- Return type
None
- class jpegdna.coders.goldmancoder.GoldmanCoderDNA[source]¶
Bases:
jpegdna.coders.goldmancoder.GoldmanCoder
DNA Goldman Coder
jpegdna.coders.hexcoder module¶
Hexadecimal converter
- class jpegdna.coders.hexcoder.HexCoder[source]¶
Bases:
jpegdna.coders.AbstractCoder
Hexadecimal coder
jpegdna.coders.huffmancoder module¶
Huffman n-ary tree coder
- class jpegdna.coders.huffmancoder.HuffmanCoder(*args, verbose=False)[source]¶
Bases:
jpegdna.coders.AbstractCoder
Huffman n-ary tree coder
case 1:
- Parameters
alphabet (list) – List of elements that can be encoded
feqs – List of appearence frequencies for each element in the alphabet
n (int) – base of the n-ary tree
verbose – bool
debug – bool
- Variables
verbose – Verbosity enabler
debug – Verbosity for debug enabler
case 2:
- Parameters
dic (dict) – Huffman dictionnary
- decode(code)[source]¶
Decode a signal using the Huffman n-ary dictionnary
- Parameters
code (str) – Signal to be decoded
- Returns
Decoded signal
- Return type
list
- class jpegdna.coders.huffmancoder.TreeNode(key, data, children=None)[source]¶
Bases:
object
Node with an arbitrary number of children.
As such, it’s missing a lot of features, but that’s okay. Don’t use it for a “real” tree.
- jpegdna.coders.huffmancoder.base_n(num, b, numerals='0123456789abcdefghijklmnopqrstuvwxyz')[source]¶
- jpegdna.coders.huffmancoder.combine_and_replace(nodes, n)[source]¶
Combine n nodes from the front of the low-to-high list into one whose key is the sum of the merged nodes. The new node’s data is set to None, then inserted into its proper place in the list.
Note: The sum of keys made here is the smallest such combination.
In the contradictory style of Huffman, if any set of nodes were chosen except for the first n, then changing a node not in the first n to one that is from the first n would reduce the sum of their keys. Thus the smallest sum is made from the first n nodes.
- Nodes
A list of TreeNodes.
- N
Integer < len(nodes).
- Returns
Low-to-high list that combines the last n nodes into one.
- jpegdna.coders.huffmancoder.huffman_initial_count(message_count, digits)[source]¶
Return the number of messages that must be grouped in the first layer for Huffman Code generation.
- Message_count
Positive integral message count.
- Digits
Integer >= 2 representing how many digits are to be used in codes.
- Returns
The number of messages that _must_ be grouped in the first level to form a digit-ary Huffman tree.
- jpegdna.coders.huffmancoder.huffman_nary_tree(probabilities, digits, verbose=False)[source]¶
Return a Huffman tree using the given number of digits.
- Probabilities
List of tuples (symbol, probability) where probability is any floating point and symbol is any object.
- Digits
Integral number of digits to use in the Huffman encoding. Must be at least two.
- Returns
TreeNode that is the root of the Huffman tree.
- jpegdna.coders.huffmancoder.huffmandict(alphabet, freqs, n, verbose=False)[source]¶
Function that creates the dictionnary for the huffman coder
- Parameters
alphabet (list) – List of elements that can be encoded
freqs (list) – List of appearence frequencies for each element in the alphabet
n (int) – base of the n-ary tree
verbose – bool
debug – bool
- Variables
verbose – Verbosity enabler
debug – Verbosity for debug enabler
- Returns
Codewords for each element of the alphabet
- Return type
dict
jpegdna.coders.valuecoder module¶
Value coder
- class jpegdna.coders.valuecoder.ValueCoder(codebook, verbose=False)[source]¶
Bases:
jpegdna.coders.AbstractCoder
Value Coder
- Variables
verbose – Verbosity enabler
category (int) – category in which belongs the value
ad_bits (int) – length of the word coding the value
- Parameters
verbose – bool
- encode(inp)[source]¶
Encode a value according to a category
- Parameters
inp (int) – value to be encoded
- full_decode(code, *args)[source]¶
Encoding method
- Parameters
code (list|str) – Input to decode
args (any) – Decoding arguments
- Returns
Decoded message, decoding state
- Return type
list|str, any
- full_encode(inp, *args)[source]¶
Encoding method
- Parameters
inp (list|str) – Input to encode
args (any) – Encoding arguments
- Returns
Encoded message, encoding state
- Return type
list|str, any
- jpegdna.coders.valuecoder.compute_max_value(ad_bits)[source]¶
Compute the max value for the codebook with codewords of length ad_bits
- Parameters
ad_bits (int) – codeword length
- Returns
max value
- Return type
int