Revision 87a0729f
Added by David Sorber about 8 years ago
| software/packJPG_library/lib_src/packjpg.cpp | ||
|---|---|---|
|
#include "pjpgtbl.h"
|
||
|
#include "dct8x8.h"
|
||
|
|
||
|
#if defined BUILD_DLL // define BUILD_LIB from the compiler options if you want to compile a DLL!
|
||
|
#define BUILD_LIB
|
||
|
#endif
|
||
|
|
||
|
#include "packjpg.h"
|
||
|
#include "packjpglib.h"
|
||
|
|
||
|
#define static
|
||
|
|
||
|
#define INIT_MODEL_S(a,b,c) new model_s( a, b, c, 255 )
|
||
|
#define INIT_MODEL_B(a,b) new model_b( a, b, 255 )
|
||
|
#define INIT_MODEL_S(a, b, c) new model_s(a, b, c, 255)
|
||
|
#define INIT_MODEL_B(a, b) new model_b(a, b, 255)
|
||
|
|
||
|
// #define USE_PLOCOI // uncomment to use loco-i predictor instead of 1DDCT predictor
|
||
|
// #define DEV_BUILD // uncomment to include developer functions
|
||
|
// #define DEV_INFOS // uncomment to include developer information
|
||
|
|
||
|
#define QUANT(cm,bp) ( cmpnfo[cm].qtable[ bp ] )
|
||
|
#define MAX_V(cm,bp) ( ( QUANT(cm,bp) > 0 ) ? ( ( freqmax[bp] + QUANT(cm,bp) - 1 ) / QUANT(cm,bp) ) : 0 )
|
||
|
#define QUANT(cm, bp) (cmpnfo[cm].qtable[ bp ])
|
||
|
#define MAX_V(cm, bp) (( QUANT(cm,bp) > 0 ) ? ( ( freqmax[bp] + QUANT(cm,bp) - 1 ) / QUANT(cm,bp) ) : 0)
|
||
|
// #define QUN_V(v,cm,bp) ( ( QUANT(cm,bp) > 0 ) ? ( ( v > 0 ) ? ( v + (QUANT(cm,bp)/2) ) / QUANT(cm,bp) : ( v - (QUANT(cm,bp)/2) ) / QUANT(cm,bp) ) : 0 )
|
||
|
|
||
|
#define ENVLI(s,v) ( ( v > 0 ) ? v : ( v - 1 ) + ( 1 << s ) )
|
||
|
#define DEVLI(s,n) ( ( n >= ( 1 << (s - 1) ) ) ? n : n + 1 - ( 1 << s ) )
|
||
|
#define E_ENVLI(s,v) ( v - ( 1 << s ) )
|
||
|
#define E_DEVLI(s,n) ( n + ( 1 << s ) )
|
||
|
|
||
|
#define ABS(v1) ( (v1 < 0) ? -v1 : v1 )
|
||
|
#define ABSDIFF(v1,v2) ( (v1 > v2) ? (v1 - v2) : (v2 - v1) )
|
||
|
#define IPOS(w,v,h) ( ( v * w ) + h )
|
||
|
#define NPOS(n1,n2,p) ( ( ( p / n1 ) * n2 ) + ( p % n1 ) )
|
||
|
#define ROUND_F(v1) ( (v1 < 0) ? (int) (v1 - 0.5) : (int) (v1 + 0.5) )
|
||
|
#define DIV_INT(v1,v2) ( (v1 < 0) ? (v1 - (v2>>1)) / v2 : (v1 + (v2>>1)) / v2 )
|
||
|
#define B_SHORT(v1,v2) ( ( ((int) v1) << 8 ) + ((int) v2) )
|
||
|
#define BITLEN1024P(v) ( pbitlen_0_1024[ v ] )
|
||
|
#define BITLEN2048N(v) ( (pbitlen_n2048_2047+2048)[ v ] )
|
||
|
#define CLAMPED(l,h,v) ( ( v < l ) ? l : ( v > h ) ? h : v )
|
||
|
|
||
|
#define MEM_ERRMSG "out of memory error"
|
||
|
#define FRD_ERRMSG "could not read file / file not found: %s"
|
||
|
#define FWR_ERRMSG "could not write file / file write-protected: %s"
|
||
|
#define MSG_SIZE 128
|
||
|
#define BARLEN 36
|
||
|
#define ENVLI(s, v) (( v > 0 ) ? v : ( v - 1 ) + ( 1 << s ))
|
||
|
#define DEVLI(s, n) (( n >= ( 1 << (s - 1) ) ) ? n : n + 1 - ( 1 << s ))
|
||
|
#define E_ENVLI(s, v) (v - ( 1 << s ))
|
||
|
#define E_DEVLI(s, n) (n + ( 1 << s ))
|
||
|
|
||
|
#define ABS(v1) ((v1 < 0) ? -v1 : v1)
|
||
|
#define ABSDIFF(v1,v2) ((v1 > v2) ? (v1 - v2) : (v2 - v1))
|
||
|
#define IPOS(w,v,h) (( v * w ) + h )
|
||
|
#define NPOS(n1,n2,p) (( ( p / n1 ) * n2 ) + ( p % n1 ))
|
||
|
#define ROUND_F(v1) ((v1 < 0) ? (int) (v1 - 0.5) : (int) (v1 + 0.5))
|
||
|
#define DIV_INT(v1,v2) ((v1 < 0) ? (v1 - (v2 >> 1)) / v2 : (v1 + (v2 >> 1)) / v2)
|
||
|
#define B_SHORT(v1,v2) (( ((int) v1) << 8 ) + ((int) v2))
|
||
|
#define BITLEN1024P(v) (pbitlen_0_1024[v])
|
||
|
#define BITLEN2048N(v) ((pbitlen_n2048_2047 + 2048)[v])
|
||
|
#define CLAMPED(l,h,v) (( v < l ) ? l : ( v > h ) ? h : v)
|
||
|
|
||
|
|
||
|
|
||
|
// special realloc with guaranteed free() of previous memory
|
||
|
static inline void* frealloc(void* ptr, size_t size)
|
||
| ... | ... | |
|
|
||
|
|
||
|
|
||
|
/* -----------------------------------------------
|
||
|
struct declarations
|
||
|
----------------------------------------------- */
|
||
|
|
||
|
struct componentInfo
|
||
|
{
|
||
|
unsigned short* qtable; // quantization table
|
||
|
int huffdc; // no of huffman table (DC)
|
||
|
int huffac; // no of huffman table (AC)
|
||
|
int sfv; // sample factor vertical
|
||
|
int sfh; // sample factor horizontal
|
||
|
int mbs; // blocks in mcu
|
||
|
int bcv; // block count vertical (interleaved)
|
||
|
int bch; // block count horizontal (interleaved)
|
||
|
int bc; // block count (all) (interleaved)
|
||
|
int ncv; // block count vertical (non interleaved)
|
||
|
int nch; // block count horizontal (non interleaved)
|
||
|
int nc; // block count (all) (non interleaved)
|
||
|
int sid; // statistical identity
|
||
|
int jid; // jpeg internal id
|
||
|
};
|
||
|
|
||
|
struct huffCodes
|
||
|
{
|
||
|
unsigned short cval[ 256 ];
|
||
|
unsigned short clen[ 256 ];
|
||
|
unsigned short max_eobrun;
|
||
|
};
|
||
|
|
||
|
struct huffTree
|
||
|
{
|
||
|
unsigned short l[ 256 ];
|
||
|
unsigned short r[ 256 ];
|
||
|
};
|
||
|
|
||
|
|
||
|
/* -----------------------------------------------
|
||
| ... | ... | |
|
// in any way to compress jpg or decompress pjg
|
||
|
|
||
|
|
||
|
/* -----------------------------------------------
|
||
|
global variables: library only variables
|
||
|
----------------------------------------------- */
|
||
|
int lib_in_type = -1;
|
||
|
int lib_out_type = -1;
|
||
|
|
||
|
|
||
|
/* -----------------------------------------------
|
||
|
global variables: data storage
|
||
|
----------------------------------------------- */
|
||
|
|
||
|
unsigned short qtables[4][64]; // quantization tables
|
||
|
huffCodes hcodes[2][4]; // huffman codes
|
||
|
huffTree htrees[2][4]; // huffman decoding trees
|
||
|
unsigned char htset[2][4]; // 1 if huffman table is set
|
||
|
|
||
|
unsigned char* grbgdata = NULL; // garbage data
|
||
|
unsigned char* hdrdata = NULL; // header data
|
||
|
unsigned char* huffdata = NULL; // huffman coded data
|
||
|
int hufs = 0 ; // size of huffman data
|
||
|
int hdrs = 0 ; // size of header
|
||
|
int grbs = 0 ; // size of garbage
|
||
|
|
||
|
unsigned int* rstp = NULL; // restart markers positions in huffdata
|
||
|
unsigned int* scnp = NULL; // scan start positions in huffdata
|
||
|
int rstc = 0 ; // count of restart markers
|
||
|
int scnc = 0 ; // count of scans
|
||
|
int rsti = 0 ; // restart interval
|
||
|
char padbit = -1 ; // padbit (for huffman coding)
|
||
|
unsigned char* rst_err = NULL; // number of wrong-set RST markers per scan
|
||
|
|
||
|
unsigned char* zdstdata[4] = { NULL }; // zero distribution (# of non-zeroes) lists (for higher 7x7 block)
|
||
|
unsigned char* eobxhigh[4] = { NULL }; // eob in x direction (for higher 7x7 block)
|
||
|
unsigned char* eobyhigh[4] = { NULL }; // eob in y direction (for higher 7x7 block)
|
||
|
unsigned char* zdstxlow[4] = { NULL }; // # of non zeroes for first row
|
||
|
unsigned char* zdstylow[4] = { NULL }; // # of non zeroes for first collumn
|
||
|
signed short* colldata[4][64] = {{NULL}}; // collection sorted DCT coefficients
|
||
|
|
||
|
unsigned char* freqscan[4] = { NULL }; // optimized order for frequency scans (only pointers to scans)
|
||
|
unsigned char zsrtscan[4][64]; // zero optimized frequency scan
|
||
|
|
||
|
int adpt_idct_8x8[ 4 ][ 8 * 8 * 8 * 8 ]; // precalculated/adapted values for idct (8x8)
|
||
|
int adpt_idct_1x8[ 4 ][ 1 * 1 * 8 * 8 ]; // precalculated/adapted values for idct (1x8)
|
||
|
int adpt_idct_8x1[ 4 ][ 8 * 8 * 1 * 1 ]; // precalculated/adapted values for idct (8x1)
|
||
|
|
||
|
|
||
|
|
||
|
/* -----------------------------------------------
|
||
| ... | ... | |
|
const char pjg_magic[] = { 'J', 'S' };
|
||
|
|
||
|
|
||
|
/* -----------------------------------------------
|
||
|
main-function
|
||
|
----------------------------------------------- */
|
||
|
|
||
|
packJPG::packJPG()
|
||
|
: lib_in_type(-1),
|
||
|
lib_out_type(-1),
|
||
|
grbgdata(nullptr),
|
||
|
hdrdata(nullptr),
|
||
|
huffdata(nullptr),
|
||
|
hufs(0),
|
||
|
hdrs(0),
|
||
|
grbs(0),
|
||
|
rstp(nullptr),
|
||
|
scnp(nullptr),
|
||
|
rstc(0),
|
||
|
scnc(0),
|
||
|
rsti(0),
|
||
|
padbit(-1),
|
||
|
rst_err(nullptr),
|
||
|
zdstdata(),
|
||
|
eobxhigh(),
|
||
|
eobyhigh(),
|
||
|
zdstxlow(),
|
||
|
zdstylow(),
|
||
|
colldata(),
|
||
|
freqscan(),
|
||
|
zsrtscan()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
packJPG::~packJPG()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
#if 0
|
||
|
|
||
|
|
||
|
/* ----------------------- Begin of library only functions -------------------------- */
|
||
| ... | ... | |
|
|
||
|
/* ----------------------- End of file -------------------------- */
|
||
|
|
||
|
#endif
|
||
| software/packJPG_library/lib_src/packjpg.h | ||
|---|---|---|
|
#ifndef PACKJPG_H
|
||
|
#define PACKJPG_H
|
||
|
|
||
|
|
||
|
#define MEM_ERRMSG "out of memory error"
|
||
|
#define FRD_ERRMSG "could not read file / file not found: %s"
|
||
|
#define FWR_ERRMSG "could not write file / file write-protected: %s"
|
||
|
#define MSG_SIZE 128
|
||
|
#define BARLEN 36
|
||
|
|
||
|
/* -----------------------------------------------
|
||
|
struct declarations
|
||
|
----------------------------------------------- */
|
||
|
|
||
|
struct componentInfo
|
||
|
{
|
||
|
unsigned short* qtable; // quantization table
|
||
|
int huffdc; // no of huffman table (DC)
|
||
|
int huffac; // no of huffman table (AC)
|
||
|
int sfv; // sample factor vertical
|
||
|
int sfh; // sample factor horizontal
|
||
|
int mbs; // blocks in mcu
|
||
|
int bcv; // block count vertical (interleaved)
|
||
|
int bch; // block count horizontal (interleaved)
|
||
|
int bc; // block count (all) (interleaved)
|
||
|
int ncv; // block count vertical (non interleaved)
|
||
|
int nch; // block count horizontal (non interleaved)
|
||
|
int nc; // block count (all) (non interleaved)
|
||
|
int sid; // statistical identity
|
||
|
int jid; // jpeg internal id
|
||
|
};
|
||
|
|
||
|
struct huffCodes
|
||
|
{
|
||
|
unsigned short cval[256];
|
||
|
unsigned short clen[256];
|
||
|
unsigned short max_eobrun;
|
||
|
};
|
||
|
|
||
|
struct huffTree
|
||
|
{
|
||
|
unsigned short l[256];
|
||
|
unsigned short r[256];
|
||
|
};
|
||
|
|
||
|
class packJPG
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
packJPG();
|
||
|
|
||
|
~packJPG();
|
||
|
|
||
|
private:
|
||
|
|
||
|
/* -----------------------------------------------
|
||
|
global variables: library only variables
|
||
|
----------------------------------------------- */
|
||
|
int lib_in_type;
|
||
|
int lib_out_type;
|
||
|
|
||
|
/* -----------------------------------------------
|
||
|
global variables: data storage
|
||
|
----------------------------------------------- */
|
||
|
unsigned short qtables[4][64]; // quantization tables
|
||
|
huffCodes hcodes[2][4]; // huffman codes
|
||
|
huffTree htrees[2][4]; // huffman decoding trees
|
||
|
unsigned char htset[2][4]; // 1 if huffman table is set
|
||
|
|
||
|
unsigned char* grbgdata; // garbage data
|
||
|
unsigned char* hdrdata ; // header data
|
||
|
unsigned char* huffdata; // huffman coded data
|
||
|
int hufs; // size of huffman data
|
||
|
int hdrs; // size of header
|
||
|
int grbs; // size of garbage
|
||
|
|
||
|
unsigned int* rstp; // restart markers positions in huffdata
|
||
|
unsigned int* scnp; // scan start positions in huffdata
|
||
|
int rstc; // count of restart markers
|
||
|
int scnc; // count of scans
|
||
|
int rsti; // restart interval
|
||
|
char padbit; // padbit (for huffman coding)
|
||
|
unsigned char* rst_err; // number of wrong-set RST markers per scan
|
||
|
|
||
|
unsigned char* zdstdata[4]; // zero distribution (# of non-zeroes) lists (for higher 7x7 block)
|
||
|
unsigned char* eobxhigh[4]; // eob in x direction (for higher 7x7 block)
|
||
|
unsigned char* eobyhigh[4]; // eob in y direction (for higher 7x7 block)
|
||
|
unsigned char* zdstxlow[4]; // # of non zeroes for first row
|
||
|
unsigned char* zdstylow[4]; // # of non zeroes for first collumn
|
||
|
signed short* colldata[4][64]; // collection sorted DCT coefficients
|
||
|
|
||
|
unsigned char* freqscan[4]; // optimized order for frequency scans (only pointers to scans)
|
||
|
unsigned char zsrtscan[4][64]; // zero optimized frequency scan
|
||
|
|
||
|
int adpt_idct_8x8[4][8 * 8 * 8 * 8]; // precalculated/adapted values for idct (8x8)
|
||
|
int adpt_idct_1x8[4][1 * 1 * 8 * 8]; // precalculated/adapted values for idct (1x8)
|
||
|
int adpt_idct_8x1[4][8 * 8 * 1 * 1]; // precalculated/adapted values for idct (8x1)
|
||
|
|
||
|
};
|
||
|
|
||
|
|
||
|
#endif
|
||
| software/packJPG_library/lib_src/packjpglib.h | ||
|---|---|---|
|
// packJPGlib.h - function declarations for the packJPG library
|
||
|
#if defined BUILD_DLL
|
||
|
#define EXPORT __declspec( dllexport )
|
||
|
#else
|
||
|
#define EXPORT extern
|
||
|
#endif
|
||
|
|
||
|
/* -----------------------------------------------
|
||
|
function declarations: library only functions
|
||
|
----------------------------------------------- */
|
||
|
|
||
|
EXPORT bool pjglib_convert_stream2stream(char* msg);
|
||
|
EXPORT bool pjglib_convert_file2file(char* in, char* out, char* msg);
|
||
|
EXPORT bool pjglib_convert_stream2mem(unsigned char** out_file, unsigned int* out_size, char* msg);
|
||
|
EXPORT void pjglib_init_streams(void* in_src, int in_type, int in_size, void* out_dest, int out_type);
|
||
|
EXPORT const char* pjglib_version_info(void);
|
||
|
EXPORT const char* pjglib_short_name(void);
|
||
|
bool pjglib_convert_stream2stream(char* msg);
|
||
|
bool pjglib_convert_file2file(char* in, char* out, char* msg);
|
||
|
bool pjglib_convert_stream2mem(unsigned char** out_file, unsigned int* out_size, char* msg);
|
||
|
void pjglib_init_streams(void* in_src, int in_type, int in_size, void* out_dest, int out_type);
|
||
|
const char* pjglib_version_info(void);
|
||
|
const char* pjglib_short_name(void);
|
||
|
|
||
|
/* a short reminder about input/output stream types
|
||
|
for the pjglib_init_streams() function
|
||
WIP for libpackjpg reorganization.