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

get_state(case=None)[source]

Helper method to get the state of the codec if necessary

Return type

any

set_state(*args, case=None)[source]

Helper method to set the state of the codec if necessary

Return type

None

exception jpegdna.coders.AutomataException[source]

Bases: Exception

exception jpegdna.coders.AutomataGetterException[source]

Bases: jpegdna.coders.AutomataException

exception jpegdna.coders.AutomataGetterExceptionDecode[source]

Bases: jpegdna.coders.AutomataGetterException

exception jpegdna.coders.AutomataGetterExceptionEncode[source]

Bases: jpegdna.coders.AutomataGetterException

exception jpegdna.coders.AutomataSetterException[source]

Bases: jpegdna.coders.AutomataException

exception jpegdna.coders.AutomataSetterExceptionDecode[source]

Bases: jpegdna.coders.AutomataSetterException

exception jpegdna.coders.AutomataSetterExceptionEncode[source]

Bases: jpegdna.coders.AutomataSetterException

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

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

get_state(case=None)[source]

Return new state after decoding

Returns

The number of bits it took in the stream and the length of the codeword for this category

Return type

int, 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

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

get_state(case=None)[source]

Return new state after decoding

Returns

The number of bits it took in the stream and the length of the codeword for this category

Return type

int, int

exception jpegdna.coders.categorycoder.NonDecodableCategory[source]

Bases: KeyError

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.categorycoder.find_category_ac(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

jpegdna.coders.categorycoder.find_category_dc(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.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
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

get_state(case=None)[source]

Return new state

Returns

The length of the codeword for this category (case 0: after encoding) or The number of zeros in the stream, number of bits it took in the stream, if it is the end of the block (case 1: after decoding)

Return type

int | int, int, bool

set_state(*args, case=None)[source]

Sets the state of the coder

Parameters

gold_code (str) – goldman code

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
decode(code)[source]

Function for decoding a DC coefficient

Parameters

code (str) – Sequence to be decoded

encode(inp)[source]

Function for coding a DC coefficient

Parameters

inp (int) – Value to encode

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

get_state(case=None)[source]

Return new state

Returns

The length of the codeword for this category (case 0: after encoding) or The number of bits it took in the stream (case 1: after decoding)

Return type

int

set_state(*args, case=None)[source]

Set new state

Parameters

gold_code (str) – Goldman code

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

decode(code)[source]

Function that decodes DNA data using Goldman coder

Parameters

code (str) – Signal to be decoded

Returns

Decoded signal

Return type

str

encode(inp)[source]

Function that encodes data into DNA using Goldman coder

Parameters

inp (str) – Signal to be encoded

Returns

Encoded signal

Return type

str

class jpegdna.coders.goldmancoder.GoldmanCoderDNA[source]

Bases: jpegdna.coders.goldmancoder.GoldmanCoder

DNA Goldman Coder

exception jpegdna.coders.goldmancoder.NonDecodableGoldman[source]

Bases: KeyError

jpegdna.coders.hexcoder module

Hexadecimal converter

class jpegdna.coders.hexcoder.HexCoder[source]

Bases: jpegdna.coders.AbstractCoder

Hexadecimal coder

decode(code)[source]

Decoding method

Parameters

code (list|str) – Input to decode

Returns

Decoded message

Return type

list|str

encode(inp)[source]

Encoding method

Parameters

inp (list|str) – Input to encode

Returns

Encoded message

Return type

list|str

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

encode(inp)[source]

Encode a signal using a Huffman n-ary dictionnary

Parameters

inp (list) – Signal to be encoded

Returns

Encoded signal

Return type

str

find_codeword_key(word)[source]

Find the symbol associated with a codeword in the huffman dictionnary

Parameters

word (str) – codeword for which we want the symbol

Returns

Symbol in the alphabet corresonding to the codeword

Return type

str

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.

print()[source]

Print the tree rooted at the node in tabular form. :returns: None

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.huffmancoder.indicies_to_code(path, digits)[source]

Convert the path into a string.

We join the indices directly, from most to least significant, keeping leading zeroes. Examples: [1, 2, 3] -> “123” [7, 2, 10] -> “72a” [0, 2, 1] -> “021”

jpegdna.coders.huffmancoder.x_in_y(query, base)[source]

Checks if query is a subsequence of base

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

decode(code)[source]

Decode a value

Parameters

code (str) – Sequence to be decoded

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

get_state(case=None)[source]

Return new state after encoding

Returns

The number of bits it took in the stream and the length of the codeword for this category

Return type

int, int

set_state(*args, case=None)[source]

Sets the state for the decoding

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

jpegdna.coders.valuecoder.compute_min_value(ad_bits)[source]

Compute the min value for the codebook with codewords of length ad_bits

Parameters

ad_bits (int) – codeword length

Returns

min value

Return type

int

jpegdna.coders.valuecoder.get_codebook(ad_bits, codebook)[source]

Returns the exhaustive codebooks for a given codeword length

Parameters

ad_bits (int) – codewor length

Returns

Codebook

Return type

list(str)