Project

General

Profile

« Previous | Next » 

Revision 787ddd97

Added by David Sorber about 8 years ago

WIP for libpackjpg reorganization: moved all jpeg specific functions into the wrapper class

View differences:

software/packJPG_library/lib_src/packjpg.cpp
}
/* -----------------------------------------------
function declarations: jpeg-specific
----------------------------------------------- */
bool jpg_setup_imginfo(void);
bool jpg_parse_jfif(unsigned char type, unsigned int len, unsigned char* segment);
bool jpg_rebuild_header(void);
int jpg_decode_block_seq(BitReader* huffr, huffTree* dctree, huffTree* actree, short* block);
int jpg_encode_block_seq(BitWriter* huffw, huffCodes* dctbl, huffCodes* actbl, short* block);
int jpg_decode_dc_prg_fs(BitReader* huffr, huffTree* dctree, short* block);
int jpg_encode_dc_prg_fs(BitWriter* huffw, huffCodes* dctbl, short* block);
int jpg_decode_ac_prg_fs(BitReader* huffr, huffTree* actree, short* block,
int* eobrun, int from, int to);
int jpg_encode_ac_prg_fs(BitWriter* huffw, huffCodes* actbl, short* block,
int* eobrun, int from, int to);
int jpg_decode_dc_prg_sa(BitReader* huffr, short* block);
int jpg_encode_dc_prg_sa(BitWriter* huffw, short* block);
int jpg_decode_ac_prg_sa(BitReader* huffr, huffTree* actree, short* block,
int* eobrun, int from, int to);
int jpg_encode_ac_prg_sa(BitWriter* huffw, std::vector<std::uint8_t>& storw, huffCodes* actbl,
short* block, int* eobrun, int from, int to);
int jpg_decode_eobrun_sa(BitReader* huffr, short* block, int* eobrun, int from, int to);
int jpg_encode_eobrun(BitWriter* huffw, huffCodes* actbl, int* eobrun);
int jpg_encode_crbits(BitWriter* huffw, std::vector<std::uint8_t>& storw);
int jpg_next_huffcode(BitReader* huffw, huffTree* ctree);
int jpg_next_mcupos(int* mcu, int* cmp, int* csc, int* sub, int* dpos, int* rstw);
int jpg_next_mcuposn(int* cmp, int* dpos, int* rstw);
int jpg_skip_eobrun(int* cmp, int* dpos, int* rstw, int* eobrun);
void jpg_build_huffcodes(unsigned char* clen, unsigned char* cval,
huffCodes* hc, huffTree* ht);
/* -----------------------------------------------
function declarations: pjg-specific
----------------------------------------------- */
......
return pjglib_convert_stream2mem(nullptr, nullptr, msg);
}
/* -----------------------------------------------
DLL export converter function
----------------------------------------------- */
......
return pjglib_convert_stream2mem(nullptr, nullptr, msg);
}
/* -----------------------------------------------
DLL export converter function
----------------------------------------------- */
......
return true;
}
/* -----------------------------------------------
DLL export init input (file/mem)
----------------------------------------------- */
......
return true;
}
#if 0
/* -----------------------------------------------
Parse routines for JFIF segments
----------------------------------------------- */
bool jpg_parse_jfif(unsigned char type, unsigned int len, unsigned char* segment)
bool packJPG::jpg_parse_jfif(
unsigned char type,
unsigned int len,
unsigned char* segment)
{
unsigned int hpos = 4; // current position in segment, start after segment header
int lval, rval; // temporary variables
......
int cmp;
int i;
switch (type)
{
case 0xC4: // DHT segment
......
}
}
/* -----------------------------------------------
JFIF header rebuilding routine
----------------------------------------------- */
bool jpg_rebuild_header(void)
bool packJPG::jpg_rebuild_header(void)
{
MemoryWriter* hdrw; // new header writer
unsigned char type = 0x00; // type of current marker segment
unsigned int len = 0; // length of current marker segment
unsigned int hpos = 0; // position in header
// start headerwriter
hdrw = new MemoryWriter();
MemoryWriter* hdrw = new MemoryWriter(); // new header writer
// header parser loop
while ((int) hpos < hdrs)
......
free(hdrdata);
hdrdata = hdrw->get_c_data();
hdrs = hdrw->num_bytes_written();
delete (hdrw);
delete hdrw;
return true;
}
/* -----------------------------------------------
sequential block decoding routine
----------------------------------------------- */
int jpg_decode_block_seq(BitReader* huffr, huffTree* dctree, huffTree* actree, short* block)
int packJPG::jpg_decode_block_seq(
BitReader* huffr,
huffTree* dctree,
huffTree* actree,
short* block)
{
unsigned short n;
unsigned char s;
......
int bpos;
int hc;
// decode dc
hc = jpg_next_huffcode(huffr, dctree);
if (hc < 0)
......
}
}
// return position of eob
return eob;
}
/* -----------------------------------------------
sequential block encoding routine
----------------------------------------------- */
int jpg_encode_block_seq(BitWriter* huffw, huffCodes* dctbl, huffCodes* actbl, short* block)
int packJPG::jpg_encode_block_seq(
BitWriter* huffw,
huffCodes* dctbl,
huffCodes* actbl,
short* block)
{
unsigned short n;
unsigned char s;
......
int bpos;
int hc;
// encode DC
s = BITLEN2048N(block[0]);
n = ENVLI(s, block[0]);
......
huffw->write_u16(actbl->cval[0x00], actbl->clen[0x00]);
}
return 64 - z;
}
/* -----------------------------------------------
progressive DC decoding routine
----------------------------------------------- */
int jpg_decode_dc_prg_fs(BitReader* huffr, huffTree* dctree, short* block)
int packJPG::jpg_decode_dc_prg_fs(
BitReader* huffr,
huffTree* dctree,
short* block)
{
unsigned short n;
unsigned char s;
int hc;
// decode dc
hc = jpg_next_huffcode(huffr, dctree);
if (hc < 0)
......
n = huffr->read(s);
block[0] = DEVLI(s, n);
// return 0 if everything is ok
return 0;
}
/* -----------------------------------------------
progressive DC encoding routine
----------------------------------------------- */
int jpg_encode_dc_prg_fs(BitWriter* huffw, huffCodes* dctbl, short* block)
int packJPG::jpg_encode_dc_prg_fs(
BitWriter* huffw,
huffCodes* dctbl,
short* block)
{
unsigned short n;
unsigned char s;
// encode DC
s = BITLEN2048N(block[0]);
n = ENVLI(s, block[0]);
huffw->write_u16(dctbl->cval[s], dctbl->clen[s]);
huffw->write_u16(n, s);
// return 0 if everything is ok
return 0;
}
/* -----------------------------------------------
progressive AC decoding routine
----------------------------------------------- */
int jpg_decode_ac_prg_fs(BitReader* huffr, huffTree* actree, short* block, int* eobrun, int from, int to)
int packJPG::jpg_decode_ac_prg_fs(
BitReader* huffr,
huffTree* actree,
short* block,
int* eobrun,
int from,
int to)
{
unsigned short n;
unsigned char s;
......
int l;
int r;
// decode ac
for (bpos = from; bpos <= to;)
{
......
}
}
// return position of eob
return eob;
}
/* -----------------------------------------------
progressive AC encoding routine
----------------------------------------------- */
int jpg_encode_ac_prg_fs(BitWriter* huffw, huffCodes* actbl, short* block, int* eobrun, int from, int to)
int packJPG::jpg_encode_ac_prg_fs(
BitWriter* huffw,
huffCodes* actbl,
short* block,
int* eobrun,
int from,
int to)
{
unsigned short n;
unsigned char s;
......
}
}
/* -----------------------------------------------
progressive DC SA decoding routine
----------------------------------------------- */
int jpg_decode_dc_prg_sa(BitReader* huffr, short* block)
int packJPG::jpg_decode_dc_prg_sa(BitReader* huffr, short* block)
{
// decode next bit of dc coefficient
block[0] = huffr->read(1);
......
return 0;
}
/* -----------------------------------------------
progressive DC SA encoding routine
----------------------------------------------- */
int jpg_encode_dc_prg_sa(BitWriter* huffw, short* block)
int packJPG::jpg_encode_dc_prg_sa(BitWriter* huffw, short* block)
{
// enocode next bit of dc coefficient
huffw->write_u16(block[0], 1);
......
return 0;
}
/* -----------------------------------------------
progressive AC SA decoding routine
----------------------------------------------- */
int jpg_decode_ac_prg_sa(BitReader* huffr, huffTree* actree, short* block, int* eobrun, int from, int to)
int packJPG::jpg_decode_ac_prg_sa(
BitReader* huffr,
huffTree* actree,
short* block,
int* eobrun,
int from,
int to)
{
unsigned short n;
unsigned char s;
......
int l;
int r;
// decode AC succesive approximation bits
if ((*eobrun) == 0) while (bpos <= to)
{
// decode next
hc = jpg_next_huffcode(huffr, actree);
if (hc < 0)
{
return -1;
}
l = LBITS(hc, 4);
r = RBITS(hc, 4);
// analyse code
if ((l == 15) || (r > 0)) // decode run/level combination
{
// decode next
hc = jpg_next_huffcode(huffr, actree);
if (hc < 0)
z = l;
s = r;
if (s == 0)
{
return -1;
v = 0;
}
else if (s == 1)
{
n = huffr->read(1);
v = (n == 0) ? -1 : 1; // fast decode vli
}
else
{
return -1; // decoding error
}
l = LBITS(hc, 4);
r = RBITS(hc, 4);
// analyse code
if ((l == 15) || (r > 0)) // decode run/level combination
// write zeroes / write correction bits
while (true)
{
z = l;
s = r;
if (s == 0)
if (block[bpos] == 0) // skip zeroes / write value
{
v = 0;
if (z > 0)
{
z--;
}
else
{
block[bpos++] = v;
break;
}
}
else if (s == 1)
else // read correction bit
{
n = huffr->read(1);
v = (n == 0) ? -1 : 1; // fast decode vli
}
else
{
return -1; // decoding error
block[bpos] = (block[bpos] > 0) ? n : -n;
}
// write zeroes / write correction bits
while (true)
if (bpos++ >= to)
{
if (block[bpos] == 0) // skip zeroes / write value
{
if (z > 0)
{
z--;
}
else
{
block[bpos++] = v;
break;
}
}
else // read correction bit
{
n = huffr->read(1);
block[bpos] = (block[bpos] > 0) ? n : -n;
}
if (bpos++ >= to)
{
return -1; // error check
}
return -1; // error check
}
}
else // decode eobrun
{
eob = bpos;
s = l;
n = huffr->read(s);
(*eobrun) = E_DEVLI(s, n);
break;
}
}
else // decode eobrun
{
eob = bpos;
s = l;
n = huffr->read(s);
(*eobrun) = E_DEVLI(s, n);
break;
}
}
// read after eob correction bits
if ((*eobrun) > 0)
......
return eob;
}
/* -----------------------------------------------
progressive AC SA encoding routine
----------------------------------------------- */
int jpg_encode_ac_prg_sa(BitWriter* huffw, std::vector<std::uint8_t>& storw, huffCodes* actbl, short* block, int* eobrun, int from, int to)
int packJPG::jpg_encode_ac_prg_sa(
BitWriter* huffw,
std::vector<std::uint8_t>& storw,
huffCodes* actbl,
short* block,
int* eobrun,
int from,
int to)
{
unsigned short n;
unsigned char s;
......
return eob;
}
/* -----------------------------------------------
run of EOB SA decoding routine
----------------------------------------------- */
int jpg_decode_eobrun_sa(BitReader* huffr, short* block, int* eobrun, int from, int to)
int packJPG::jpg_decode_eobrun_sa(
BitReader* huffr,
short* block,
int* eobrun,
int from,
int to)
{
unsigned short n;
int bpos;
// fast eobrun decoding routine for succesive approximation
for (bpos = from; bpos <= to; bpos++)
{
......
}
}
return 0;
}
/* -----------------------------------------------
run of EOB encoding routine
----------------------------------------------- */
int jpg_encode_eobrun(BitWriter* huffw, huffCodes* actbl, int* eobrun)
int packJPG::jpg_encode_eobrun(
BitWriter* huffw,
huffCodes* actbl,
int* eobrun)
{
unsigned short n;
unsigned char s;
int hc;
if ((*eobrun) > 0)
{
while ((*eobrun) > actbl->max_eobrun)
......
(*eobrun) = 0;
}
return 0;
}
/* -----------------------------------------------
correction bits encoding routine
----------------------------------------------- */
int jpg_encode_crbits(BitWriter* huffw, std::vector<std::uint8_t>& storw)
int packJPG::jpg_encode_crbits(
BitWriter* huffw,
std::vector<std::uint8_t>& storw)
{
for (const std::uint8_t bit : storw)
{
......
return 0;
}
/* -----------------------------------------------
returns next code (from huffman-tree & -data)
----------------------------------------------- */
int jpg_next_huffcode(BitReader* huffw, huffTree* ctree)
int packJPG::jpg_next_huffcode(BitReader* huffw, huffTree* ctree)
{
int node = 0;
while (node < 256)
{
node = (huffw->read(1) == 1) ?
ctree->r[node] : ctree->l[node];
node = (huffw->read(1) == 1) ? ctree->r[node] : ctree->l[node];
if (node == 0)
{
break;
......
return (node - 256);
}
/* -----------------------------------------------
calculates next position for MCU
----------------------------------------------- */
int jpg_next_mcupos(int* mcu, int* cmp, int* csc, int* sub, int* dpos, int* rstw)
int packJPG::jpg_next_mcupos(
int* mcu,
int* cmp,
int* csc,
int* sub,
int* dpos,
int* rstw)
{
int sta = 0; // status
// increment all counts where needed
if ((++(*sub)) >= cmpnfo[(*cmp)].mbs)
{
......
(*dpos) = (*mcu);
}
return sta;
}
/* -----------------------------------------------
calculates next position (non interleaved)
----------------------------------------------- */
int jpg_next_mcuposn(int* cmp, int* dpos, int* rstw)
int packJPG::jpg_next_mcuposn(int* cmp, int* dpos, int* rstw)
{
// increment position
(*dpos)++;
......
return 1;
}
return 0;
}
/* -----------------------------------------------
skips the eobrun, calculates next position
----------------------------------------------- */
int jpg_skip_eobrun(int* cmp, int* dpos, int* rstw, int* eobrun)
int packJPG::jpg_skip_eobrun(int* cmp, int* dpos, int* rstw, int* eobrun)
{
if ((*eobrun) > 0) // error check for eobrun
{
......
return 0;
}
/* -----------------------------------------------
creates huffman-codes & -trees from dht-data
----------------------------------------------- */
void jpg_build_huffcodes(unsigned char* clen, unsigned char* cval, huffCodes* hc, huffTree* ht)
void packJPG::jpg_build_huffcodes(
unsigned char* clen,
unsigned char* cval,
huffCodes* hc,
huffTree* ht)
{
int nextfree;
int code;
int node;
int i, j, k;
// fill with zeroes
memset(hc->clen, 0, 256 * sizeof(short));
memset(hc->cval, 0, 256 * sizeof(short));
......
}
}
/* ----------------------- End of JPEG specific functions -------------------------- */
/* ------------------- End of JPEG specific functions ---------------------- */
/* ----------------------- End of PJG specific functions -------------------------- */
/* ------------------- End of PJG specific functions ----------------------- */
#if 0
/* -----------------------------------------------
encodes frequency scanorder to pjg
----------------------------------------------- */
software/packJPG_library/lib_src/packjpg.h
*************************************************************************/
bool jpg_setup_imginfo(void);
bool jpg_parse_jfif(
unsigned char type,
unsigned int len,
unsigned char* segment);
bool jpg_rebuild_header(void);
int jpg_decode_block_seq(
BitReader* huffr,
huffTree* dctree,
huffTree* actree,
short* block);
int jpg_encode_block_seq(
BitWriter* huffw,
huffCodes* dctbl,
huffCodes* actbl,
short* block);
int jpg_decode_dc_prg_fs(
BitReader* huffr,
huffTree* dctree,
short* block);
int jpg_encode_dc_prg_fs(
BitWriter* huffw,
huffCodes* dctbl,
short* block);
int jpg_decode_ac_prg_fs(
BitReader* huffr,
huffTree* actree,
short* block,
int* eobrun,
int from,
int to);
int jpg_encode_ac_prg_fs(
BitWriter* huffw,
huffCodes* actbl,
short* block,
int* eobrun,
int from,
int to);
int jpg_decode_dc_prg_sa(BitReader* huffr, short* block);
int jpg_encode_dc_prg_sa(BitWriter* huffw, short* block);
int jpg_decode_ac_prg_sa(
BitReader* huffr,
huffTree* actree,
short* block,
int* eobrun,
int from,
int to);
int jpg_encode_ac_prg_sa(
BitWriter* huffw,
std::vector<std::uint8_t>& storw,
huffCodes* actbl,
short* block,
int* eobrun,
int from,
int to);
int jpg_decode_eobrun_sa(
BitReader* huffr,
short* block,
int* eobrun,
int from,
int to);
int jpg_encode_eobrun(
BitWriter* huffw,
huffCodes* actbl,
int* eobrun);
int jpg_encode_crbits(
BitWriter* huffw,
std::vector<std::uint8_t>& storw);
int jpg_next_huffcode(BitReader* huffw, huffTree* ctree);
int jpg_next_mcupos(
int* mcu,
int* cmp,
int* csc,
int* sub,
int* dpos,
int* rstw);
int jpg_next_mcuposn(int* cmp, int* dpos, int* rstw);
int jpg_skip_eobrun(int* cmp, int* dpos, int* rstw, int* eobrun);
void jpg_build_huffcodes(
unsigned char* clen,
unsigned char* cval,
huffCodes* hc,
huffTree* ht);
/**************************************************************************
* PJG specific functions
*************************************************************************/
/**************************************************************************
* Member variables

Also available in: Unified diff