// Filename : huff.h
//
// This file was initially generated by SpaceStudio but is meant to be modified
// by the user.
#ifndef HUFF_H
#define HUFF_H

#include "spacecomp.h"
#include "abstract_module.h"
#include "application_definitions.h"

typedef struct DC_HUFFMAN_STRUCTURE {
	signed long	valptr_minus_mincode_table [17];
	signed long	maxcode_table [17];
	unsigned char	huffsize_table [256];
	uint8_t	huffval_table [12];
} DC_HUFFMAN_STRUCTURE;


typedef struct AC_HUFFMAN_STRUCTURE {
	signed long	valptr_minus_mincode_table [17];
	signed long	maxcode_table [17];
	unsigned char	huffsize_table [256];
	uint8_t	huffval_table [162];
} AC_HUFFMAN_STRUCTURE;

class huff: public abstract_module {
	SPACECOMP_START(huff, INDEX, huff);

public:
	huff(SPACECOMP_CTOR_PARAMS(huff, INDEX));

	void thread(SPACECOMP_THREAD_PARAMS(huff, INDEX, thread));

private:

	void communication_write64pixelblock(unsigned int, int16_t*);
	void initialize();

	unsigned char  m_state;				// state of the jpeg decompression
	unsigned long	m_inputAdd;				// latest jpeg data address, multiple of 4 and 0x20 afetr header

	uint8_t launch_huffman();
	//
	//  Huffman
	//

	DC_HUFFMAN_STRUCTURE dc_huffman_structure[2];
	AC_HUFFMAN_STRUCTURE ac_huffman_structure[2];
	uint32_t lcode;
	unsigned char lsize;
	int16_t ldc[3];
	int32_t getHuffmanTableElement(int selector, int id, int index);
	uint16_t comm_read_16huff_bits();

	unsigned char m_currentHuffmanComponent;


	//
	// JPEG related
	//
	uint8_t read_dht_marker();
	uint8_t read_8_bits();
	uint16_t read_16_bits();
	uint8_t read_huffman_table(signed long* valptr_minus_mincode_table, signed long* maxcode_table, unsigned char* huffsize_table, uint8_t* huffval_table, unsigned short& huffman_table_definition_length);

	SPACECOMP_END(huff, INDEX, huff);
};

#endif
