⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configured_jbig.c

📁 基于ADI BLACKFIN的jbig压缩和解压缩程序
💻 C
📖 第 1 页 / 共 3 页
字号:
 * however make sure that no zero byte is removed which directly * follows a 0xff byte (i.e., keep MARKER_ESC MARKER_STUFF sequences * intact). This function is used to remove any redundant final zero * bytes from a PSCD. */ #pragma optimize_for_speedstatic void jbg_buf_remove_zeros(struct jbg_enc_state *s){		while(!s->sde[--s->sde_index]);  /*   * If the final non-zero byte is 0xff (MARKER_ESC), then we just have   * removed a MARKER_STUFF and we will append it again now in order   * to preserve PSCD status of byte stream.   */  if (s->sde[s->sde_index] == MARKER_ESC)  {  	s->sde_index++;  	    WRITE_BYTE(MARKER_STUFF, s);	}  else	  s->sde_index++;  return;}/* * The jbg_buf list which starts with block *new_prefix is concatenated * with the list which starts with block **start and *start will then point * to the first block of the new list. *//* * Send the contents of a jbg_buf list that starts with block **head to * the call back function data_out and return the blocks of the jbg_buf * list to the freelist from which these jbg_buf blocks have been taken. * After the call, *head == NULL. *//* * Calculate y = ceil(x/2) applied n times, which is equivalent to * y = ceil(x/(2^n)). This function is used to * determine the number of pixels per row or column after n resolution * reductions. E.g. X[d-1] = jbg_ceil_half(X[d], 1) and X[0] = * jbg_ceil_half(X[d], d) as defined in clause 6.2.3 of T.82. */ // top(x / (2 ^ n))		@chenyi.srf#pragma optimize_for_speedunsigned long jbg_ceil_half(unsigned long x, int n){  unsigned long mask;    assert(n >= 0 && n < 32);  mask = (1UL << n) - 1;     /* the lowest n bits are 1 here */  return (x >> n) + ((mask & x) != 0);}#pragma optimize_for_speedstatic unsigned long jbg_stripes(unsigned long l0, unsigned long yd){  unsigned long y0 = yd;  return y0 / l0 + (y0 % l0 != 0);}/* * Initialize the status struct for the encoder. */#pragma optimize_for_speedvoid jbg_enc_init(struct jbg_enc_state *s, struct jbg_arenc_state *se,					unsigned long x, unsigned long y,                  unsigned char *src_image, unsigned char *dest){  	unsigned char buf[20];		s->xd = x;	s->yd = y;	s->l0 = 8;	s->stripes = jbg_stripes(s->l0, y);  	s->lhp = src_image;  	s->highres[0] = 0;  	s->sde = dest;		s->sde_index = 0;		s->ltp_old = 0;		s->s = se;  	/* prepare BIH */  	buf[0] = 0;  	buf[1] = 0;  	buf[2] = 1;  	buf[3] = 0;  	//xd = jbg_ceil_half(s->xd, s->d - s->dh);  	//yd = jbg_ceil_half(s->yd1, s->d - s->dh);  	buf[4] = (unsigned char)(s->xd >> 24);  	buf[5] = (unsigned char)((s->xd >> 16) & 0xff);  	buf[6] = (unsigned char)((s->xd >> 8) & 0xff); 	buf[7] = (unsigned char)(s->xd & 0xff); 	buf[8] = (unsigned char)(s->yd >> 24); 	buf[9] = (unsigned char)((s->yd >> 16) & 0xff); 	buf[10] = (unsigned char)((s->yd >> 8) & 0xff); 	buf[11] = (unsigned char)(s->yd & 0xff); 	buf[12] = (unsigned char)(s->l0 >> 24); 	buf[13] = (unsigned char)((s->l0 >> 16) & 0xff); 	buf[14] = (unsigned char)((s->l0 >> 8) & 0xff); 	buf[15] = (unsigned char)(s->l0 & 0xff); 	buf[16] = 0;  	buf[17] = 0;  	buf[18] = 0;  	buf[19] = (unsigned char)((JBG_TPBON | JBG_LRLTWO) & 0x7f);  /* output BIH */	WriteBufString(s, buf, 20);		arith_encode_init(se, 0);		se->s = s;		jbig_encode_init(s, se);	return;}/* * This function actually does all the tricky work involved in producing * a SDE, which is stored in the appropriate s->sde[][][] element * for later output in the correct order. */ #pragma optimize_for_speedstatic void encode_sde(struct jbg_enc_state *s, long stripe){	struct jbg_arenc_state *se;	unsigned char *hp, *p1, *q1;	unsigned long  hx, hy, hbpl;	unsigned long line_h1 = 0,line_h2, line_h3;	unsigned long i, j, y;	unsigned a, t;	int ltp, ltp_old;	/* initialize arithmetic encoder */		se = s->s;			arith_encode_init(se, stripe != 0);			se->s = s;		//#if 0	  /* current line number in highres image */	y = stripe * s->l0;  /* number of pixels in highres image */	// in the current layer // @chenyi.srf	hx = s->xd;	hy = s->yd;  /* bytes per line in highres and lowres image */	hbpl = s->xd >> 3;//jbg_ceil_half(hx, 3);  /* pointer to first image byte of highres stripe */	hp = s->lhp + stripe * s->l0 * hbpl;	/* initialize typical prediction */	ltp = 0;		if (stripe == 0)		ltp_old = 0;	else 	{		ltp_old = 1;		p1 = hp - hbpl;		if (y > 1) 		{			q1 = p1 - hbpl;	      // Why can't we do the comparison with 4 bytes?			while (p1 < hp && (ltp_old = (*p1++ == *q1++)) != 0);		}		else			while (p1 < hp && (ltp_old = (*p1++ == 0)) != 0);	}    /*     *  Encode lowest resolution layer     */    for (i = 0; i < s->l0 && y < hy; i++, y++)     { 		/* typical prediction */				ltp = 1;			p1 = hp;			if (y > 0)		// When current number of line is above zero // @chenyi.srf 			{	  			q1 = hp - hbpl;	  			while (q1 < hp && (ltp = (*p1++ == *q1++)) != 0);			}			else	  			while (p1 < hp + hbpl && (ltp = (*p1++ == 0)) != 0);			arith_encode(se, 0x0e5, ltp == ltp_old);	// 0x0e5 means contexts for TP special pixels			ltp_old = ltp;			if (ltp)		// If the current byte of pixels is equal to			{				// the old one, there's no need to do the following things. // @chenyi.srf	  		/* skip next line */	  			hp += hbpl;	  			continue;			}		//		extern void typical_predict(unsigned long);		//		typical_predict((unsigned long)&s->sde_index);      /*       * Layout of the variables line_h1, line_h2, line_h3, which contain       * as bits the neighbour pixels of the currently coded pixel X:       *       *          76543210765432107654321076543210     line_h3       *          76543210765432107654321076543210     line_h2       *  76543210765432107654321X76543210             line_h1       */				line_h1 = line_h2 = 0;				if (y > 0) line_h2 = (unsigned long)*(hp - hbpl) << 8;	// (unsigned long)(*(hp-hbpl)) << 8            /* encode line */		for (j = 0; j < hx; hp++)		{      	// Combine current byte and the next byte. // @chenyi.srf			line_h1 |= *hp;			if (j < hbpl * 8 - 8 && y > 0)	  			line_h2 |= *(hp - hbpl + 1);					 /* three line template */	  		do	  		{	    		line_h1 <<= 1;  line_h2 <<= 1;;	 			   	      	    arith_encode(se, (((line_h2 >> 10) & 0x3f0) |								((line_h1 >>  9) & 0x00f)),			  					 (line_h1 >> 8) & 1);			  				  				  				  	#ifdef DEBUG	    encoded_pixels++;#endif			}			while (++j & 7/* && j < hx*/);		}	}//#endif//	extern void encode_layer(struct jbg_enc_state *s, struct jbg_arenc_state *se, long stripe);	//	encode_layer(s, se, stripe);			arith_encode_flush(se);	jbg_buf_remove_zeros(s);		WRITE_BYTE(MARKER_ESC, s);	WRITE_BYTE(MARKER_SDNORM, s);}/*  * This function is called inside the three loops of jbg_enc_out() in * order to write the next SDE. It has first to generate the required * SDE and all SDEs which have to be encoded before this SDE can be * created. The problem here is that if we want to output a lower * resolution layer, we have to allpy the resolution reduction * algorithm in order to get it. As we try to safe as much memory as * possible, the resolution reduction will overwrite previous higher * resolution bitmaps. Consequently, we have to encode and buffer SDEs * which depend on higher resolution layers before we can start the * resolution reduction. All this logic about which SDE has to be * encoded before resolution reduction is allowed is handled here. * This approach might be a little bit more complex than alternative * ways to do it, but it allows us to do the encoding with the minimal * possible amount of temporary memory. *//* * Convert the table which controls the deterministic prediction * process from the internal format into the representation required * for the 1728 byte long DPTABLE element of a BIH. * * The bit order of the DPTABLE format (see also ITU-T T.82 figure 13) is * * high res:   4  5  6     low res:  0  1 *             7  8  9               2  3 *            10 11 12 * * were 4 table entries are packed into one byte, while we here use * internally an unpacked 6912 byte long table indexed by the following * bit order: * * high res:   7  6  5     high res:   8  7  6     low res:  1  0 * (phase 0)   4  .  .     (phase 1)   5  4  .               3  2 *             .  .  .                 .  .  . * * high res:  10  9  8     high res:  11 10  9 * (phase 2)   7  6  5     (phase 3)   8  7  6 *             4  .  .                 5  4  . */void jbg_int2dppriv(unsigned char *dptable, const char *internal){  int i, j, k;  int trans0[ 8] = { 1, 0, 3, 2, 7, 6, 5, 4 };  int trans1[ 9] = { 1, 0, 3, 2, 8, 7, 6, 5, 4 };  int trans2[11] = { 1, 0, 3, 2, 10, 9, 8, 7, 6, 5, 4 };  int trans3[12] = { 1, 0, 3, 2, 11, 10, 9, 8, 7, 6, 5, 4 };    for (i = 0; i < 1728; dptable[i++] = 0);#define FILL_TABLE1(offset, len, trans) \  for (i = 0; i < len; i++) { \    k = 0; \    for (j = 0; j < 8; j++) \      k |= ((i >> j) & 1) << trans[j]; \    dptable[(i + offset) >> 2] |= \      (internal[k + offset] & 3) << ((3 - (i&3)) << 1); \  }  FILL_TABLE1(   0,  256, trans0);  FILL_TABLE1( 256,  512, trans1);  FILL_TABLE1( 768, 2048, trans2);  FILL_TABLE1(2816, 4096, trans3);  return;}#if 0/* * Convert the table which controls the deterministic prediction * process from the 1728 byte long DPTABLE format into the 6912 byte long * internal format. */void jbg_dppriv2int(char *internal, const unsigned char *dptable){  int i, j, k;  int trans0[ 8] = { 1, 0, 3, 2, 7, 6, 5, 4 };  int trans1[ 9] = { 1, 0, 3, 2, 8, 7, 6, 5, 4 };  int trans2[11] = { 1, 0, 3, 2, 10, 9, 8, 7, 6, 5, 4 };  int trans3[12] = { 1, 0, 3, 2, 11, 10, 9, 8, 7, 6, 5, 4 };  #define FILL_TABLE2(offset, len, trans) \  for (i = 0; i < len; i++) { \    k = 0; \    for (j = 0; j < 8; j++) \      k |= ((i >> j) & 1) << trans[j]; \    internal[k + offset] = \      (dptable[(i + offset) >> 2] >> ((3 - (i & 3)) << 1)) & 3; \  }  FILL_TABLE2(   0,  256, trans0);  FILL_TABLE2( 256,  512, trans1);  FILL_TABLE2( 768, 2048, trans2);  FILL_TABLE2(2816, 4096, trans3);  return;}#endif/* * Encode one full BIE and pass the generated data to the specified * call-back function */unsigned char* jbg_enc_out_single(struct jbg_enc_state *s, unsigned long stripe){  	encode_sde(s, stripe);  	  	return s->sde;}#pragma optimize_for_speedunsigned char* jbg_enc_out(struct jbg_enc_state *s){	int i;		for(i=0; i<s->stripes; i++)		encode_sde(s, i);			return s->sde;}void jbg_enc_free(struct jbg_enc_state *s){	(void)s;	return;}/* * Convert the error codes used by jbg_dec_in() into a string * written in the selected language and character set. */const char *jbg_strerror(int errnum, int language){  if (errnum < 0 || errnum >= NEMSG)    return "Unknown error code passed to jbg_strerror()";  if (language < 0 || language >= NEMSG_LANG)    return "Unknown language code passed to jbg_strerror()";  return errmsg[language][errnum];}/* * The constructor for a decoder  */void jbg_dec_init(struct jbg_dec_state *s){  s->order = 0;  s->d = -1;  s->bie_len = 0;  s->buf_len = 0;  s->xmax = 4294967295UL;  s->ymax = 4294967295UL;  s->dmax = 256;  s->s = NULL;  return;}/* * Specify a maximum image size for the decoder. If the JBIG file has * the order bit ILEAVE, but not the bit SEQ set, then the decoder * will abort to decode after the image has reached the maximal * resolution layer which is still not wider than xmax or higher than * ymax. */void jbg_dec_maxsize(struct jbg_dec_state *s, unsigned long xmax,		     unsigned long ymax){  if (xmax > 0) s->xmax = xmax;  if (ymax > 0) s->ymax = ymax;  return;}/* * Decode the new len PSDC bytes to which data points and add them to * the current stripe. Return the number of bytes which have actually * been read (this will be less than len if a marker segment was  * part of the data or if the final byte was 0xff were this code * can not determine, whether we have a marker segment. */static size_t decode_pscd(struct jbg_dec_state *s, unsigned char *data,			  size_t len){  unsigned long stripe;  unsigned long hl, y, hx, hy, hbpl;  unsigned char *hp, *p1, *q1;  register unsigned long line_h1, line_h2, line_h3;    struct jbg_ardec_state *se;  unsigned long x;  long o;  unsigned a;  int n;  int pix, slntp, tx;  /* SDE loop variables */  stripe = s->ii;  /* forward data to arithmetic decoder */  se = s->s;  se->pscd_ptr = data;  se->pscd_end = data + len;    /* number of lines per stripe in highres image */  hl = s->l0;  /* current line number in highres image */  y = stripe * hl + s->i;  /* number of pixels in highres image */  hx = s->xd;  hy = s->yd;  /* bytes per line in highres and lowres image */  hbpl = jbg_ceil_half(hx, 3);  /* pointer to highres and lowres image bytes */  hp  = s->lhp[0] + (stripe * hl + s->i) * hbpl +    (s->x >> 3);  /* restore a few local variables */  line_h1 = s->line_h1;  line_h2 = s->line_h2;  line_h3 = s->line_h3;    x = s->x;  if (s->x == 0 && s->i == 0 &&      (stripe == 0 || s->reset)) {    s->tx = s->ty = 0;    if (s->pseudo)      s->lntp = 1;  }#ifdef DEBUG

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -