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

📄 jbigsrc.c

📁 硬件jbig 压缩算法
💻 C
字号:
/*Copyright (c) 2002, 2005 Zoran Corporation All rights reserved.      */

/** @file jbigsrc.c
* The source provides an interface to get un-compressed data from a
* jbig encoded image created by a jbig sink.
*
* Bands come out of the source in the same order they were added to
* the sink and in the same format they were added to the sink.
*/
#undef PLUS
#include "sys.h"
#define PLUS

#include "qio.h"
#include "stdlib.h"
#include "jbig.h"
#include "jbigimage.h"
#include "jbigsrc.h"
#include "mallocaligned.h"
#include "memmgr.h"
#include "dbg.h"









/**A structure to kepp tack a JBIG image source.*/

typedef struct
{
	int CurrentBand;	// The band we are currently decoding.
	
	void *eimage;		// A handle to the JBIG image.
	void *decoder;		// A handle to a JBIG decoder.
}
JbigSrc;


extern void * gACC_jbig_images[16];	// by Cellming.Chen (2006-11-10)



void QsrcJbigInit(void)
{
#ifndef OLD_SCHOOL_API

	QIO_STATUS sts;
	
	sts = QsrcAddSrc( "/jbig",
					  QsrcJbigOpen,
					  QsrcJbigClose,
					  QsrcJbigSet,
					  QsrcJbigGet,
					  QsrcJbigAbort,
					  QsrcJbigBufGet,
					  QsrcJbigConsumed );
	
	ASSERT(sts==QIO_SUCCESS);
	
#endif
}




/** Opens a JBIG image data source.
*
* @param name - The name of the image to open.  Same as name passed
*               to QsnkJbigOpen() to create the stored image.
* @param flag - Not used set to zero.
* @param mode - Not used set to zero.
*
* @return  NULL on failure.  Handle to JbigSrc on success.
*/
QIO_STATUS QsrcJbigOpenNEW(char *name, int flag, int mode, void **fd)
{
	// ~~~ step 01 ~~~

	JbigSrc *j;	
	j = (JbigSrc *)malloc(sizeof(JbigSrc));
	ASSERT(j);

	// ~~~ step 02 ~~~

	// padding 1111	
	j->eimage = JbigImageListFind(name);	
	ASSERT(j->eimage);
	// padding 2222	
	j->decoder = JbigDecNew();
	// padding 3333	
	j->CurrentBand = 0;

	// ~~~ step 03 ~~~
	
	*fd = (void*)j;

	
	
	return QIO_SUCCESS;
}

QIO_STATUS QsrcJbigDirectOpen(char *name, int flag, int mode, void **fd)
{
	// ~~~ step 01 ~~~

	JbigSrc *j;	
	j = (JbigSrc *)malloc(sizeof(JbigSrc));
	ASSERT(j);

	// ~~~ step 02 ~~~

	// padding 1111
	j->eimage = JbigImageNew(name);
	ASSERT(j->eimage);
	// padding 2222	
	j->decoder = JbigDecNew();
	// padding 3333	
	j->CurrentBand = 0;

	// ~~~ step 03 ~~~
	
	*fd = (void*)j;


	return QIO_SUCCESS;
}



/** Close a JBIG image data source.  This doesn't delete the image
* from memory.  A call to QsnkJbigDeleteImage(name) will delete the
* image from memory.
*
* @param _j - A handle to an open JbigSrc.
*
* @return  QIO_STATUS
*/
QIO_STATUS QsrcJbigCloseNEW(void *_j)
{
	JbigSrc *j = (JbigSrc *)_j;
	
	JbigDecDelete(j->decoder);
	
	free(j);
	
	return QIO_SUCCESS;
}

/** set and get source parameters
*
* @param _j - A handle to an open jbig sink.
* @param option - The paremter to set/get.
* @param args
*
* @return  QIO_STATUS
*/
QIO_STATUS QsrcJbigSetNEW(void *_j, QIO_OPTION option, void *args)
{
	JbigSrc *j = (JbigSrc *)_j;
	ASSERT(j);
	
	switch(option)
		{
		default:
			return QIO_NOT_IMPLEMENTED;
		}
	
	/*  return(QIO_SUCCESS); */
}

QIO_STATUS QsrcJbigGetNEW(void *_j, QIO_OPTION option, void *args)
{
	JbigSrc *j = (JbigSrc *)_j;
	ASSERT(j);
	
	switch(option)
		{
		default:
			return QIO_NOT_IMPLEMENTED;
		case QIO_OPT_HEIGHT:
			*(Uint32*)args = JbigImageGetHeight(j->eimage);
			break;
		case QIO_OPT_WIDTH:
			*(Uint32*)args = JbigImageGetPixelWidth(j->eimage);
			break;
		case QIO_OPT_UNENCODEDSIZE:
			*(Uint32*)args = JbigImageGetUnEncodedSize(j->eimage);
			break;
		}
	
	
	return QIO_SUCCESS;
	
}

/** Abort does nothing yet.
*
* @param _j - A handle to an open jbig sink.
*
* @return  QIO_STATUS
*/
QIO_STATUS QsrcJbigAbortNEW(void *_j, int flags)
{
	JbigSrc *j = (JbigSrc *)_j;
	ASSERT(j);
	return(QIO_SUCCESS);
}



/** Get a band of raw image data.  Get a JBIG encoded band from the
* image; decode it; and return it to the caller.  The requested_size
* is ignored and should be set to zero. BufGet will always return a
* full band.
*
* @param _j - A handle to an open JbigSrc.
*
* @return  QIO_STATUS
*/


//	by Cellming
//	2006-04-18


QIO_STATUS QsrcJbigBufGetNEW( void *_j,
							  Uint8* dataBuf,
							  Uint32 numBytesReq,
							  Uint32* numBytesRead,
							  JBIG_BIH* BankJbigHeader )
{
	JbigSrc *j = (JbigSrc *)_j;
	//Uint32 *dst;	// by Cellming (2006-05-25)
	Uint8 *	p_Buf;	// by Cellming (2006-05-25)
	Uint8 *src;
	Uint32 dstused, srcused;
	Uint32 h,w,UnEncodedSize, EncodedSize;
	JbigStatus sts;
	int try;
	
	JbigImageGetBand( j->eimage,
					  j->CurrentBand,
					  &UnEncodedSize, &h, &w,
					  &EncodedSize, &src );
	
	if(src==NULL)
		{
		*numBytesRead = 0;
		*dataBuf = NULL;
		return QIO_EOP;
		}

	/*
	dst = (Uint32*)MallocAligned(UnEncodedSize + 4, 8);
	ASSERT(dst);
	
	*dst++ = UnEncodedSize;
	
	*dataBuf = (Uint8*)dst;
	*/

	//AllocRudeDataBand(&p_Buf, UnEncodedSize, &cur_rudedata_band[Index_PrintPage]);	// cm (2006-09-08)
	//*dataBuf = p_Buf;																	// cm (2006-09-08)

	p_Buf = dataBuf;
	
	
	sts=JbigError;
	try = 0;
	
	do	{
		//JbigDecInit(j->decoder, h, w, h);				// by Cellming
		JbigDecoderInit(j->decoder, BankJbigHeader);	// by Cellming
		
		sts = JbigDecDecodeStripe( j->decoder,
								   p_Buf,
								   UnEncodedSize,
								   src,
								   EncodedSize,
								   &dstused,
								   &srcused );
		}while(sts!=JbigSuccess && (try++ < 4));
	
	if(try>1)
		PSPRINTF("JBIGSRC:  Got sts = %d after %d tries.\n", sts, try);
	
	*numBytesRead = dstused;
	
	ASSERT(*numBytesRead==UnEncodedSize);				// by Cellming
	
	j->CurrentBand++;
	
	return QIO_SUCCESS;
}

/** Calling Consumed tells the source the user is done with this band
* and it is safe to free it. The size must be equal to the size
* returned in the corresponding BufGet call.
*
* @param _j       A handle to a jbig qsrc.
* @param dataBuf  A pointer to data buffer than has been consumed.
* @param bufSize  The size of consumed data buffer in bytes.
*
* @return      QIO_STATUS
*/
QIO_STATUS QsrcJbigConsumedNEW(void *_j, Uint8 *dataBuf, Uint32 bufSize)
{
	JbigSrc *j = (JbigSrc *)_j;
	Uint32 *size;
	
	ASSERT(j);
	size = (Uint32*)dataBuf;
	
	size--;
	
	ASSERT(*size==bufSize);
	
	
	FreeAligned(size);
	return QIO_SUCCESS;
	
}


// bandsize__i:		one band image size (byte unit)
// p_enc:			pointer to one band jbig data
// bandsize_enc:	size of one band jbig data

QIO_STATUS QsrcJbigParserOneBand(void *_j, Uint32 bandsize__i, Uint8 *p_enc, Uint32 bandsize_enc)
{
	JbigSrc *j = (JbigSrc *)_j;
	
	Uint32 h, w;
	Uint32 bitSize = bandsize__i * 8;
	
	w = JbigImageGetPixelWidth(j->eimage);
	
	ASSERT(w);
	
	h = bitSize/w;
	
	ASSERT(w<0x10000);
	ASSERT(h<0x10000);
	

	
	
	JbigImageAddBand(j->eimage, bandsize__i, h, w, bandsize_enc, p_enc);
	

	//FreeAligned(p_enc);	// by Cellming (2006-05-24)

	
	
	return(QIO_SUCCESS);
}

/*Old school API*/
#ifdef OLD_SCHOOL_API

/*Keep this static around so we can run the TIFF source and its new
function in compatabilty mode with the old system.*/
void *sJbigHandle = NULL;



API_RET QsrcJbigClose(void)
{
	return (API_RET)QsrcJbigCloseNEW(sJbigHandle);
}

/*pass the name of the jbig image in the flag*/
API_RET QsrcJbigOpen(int flag, int mode)
{
	char *name = (char*)flag;
	ASSERT(name);
	return (API_RET)QsrcJbigOpenNEW(name, 0, 0, &sJbigHandle);
}


API_RET QsrcJbigSetup(SRCSNK_SETUP_INFO *info, void *setupStruct)
{
	
	QsrcJbigSetNEW(sJbigHandle, QIO_OPT_WIDTH, (void*)info->desc.widthPix);
	QsrcJbigSetNEW(sJbigHandle, QIO_OPT_HEIGHT, (void*)info->desc.lengthPix);
	
	return API_OK;
	
}

API_RET QsrcJbigStatus(void *statusStruct)
{
	Uint32 w, h;
	
	QsrcJbigGetNEW(sJbigHandle, QIO_OPT_WIDTH, (void*)&w);
	QsrcJbigGetNEW(sJbigHandle, QIO_OPT_HEIGHT, (void*)&h);
	
	return API_OK;
}

API_RET QsrcJbigAbort(int flags)
{
	QsrcJbigAbortNEW(sJbigHandle, flags);
	return API_OK;
}


int QsrcJbigBufGet(Uint8** dataBuf, Uint32 numBytesReq, Uint32* numBytesRead)
{
	//return QsrcJbigBufGetNEW(sJbigHandle, dataBuf, numBytesReq, numBytesRead);	// by Cellming
}


API_RET QsrcJbigConsumed(Uint8 *dataBuf, Uint32 bufSize)
{
	return (API_RET)QsrcJbigConsumedNEW(sJbigHandle, dataBuf, bufSize);
}

#endif

/** Ioctl does nothing yet.  Might be a better way to do setup/staus
* than the current setup/status calls.
*
* @param _j - A handle to an open jbig sink.
*
* @return  QIO_STATUS
*/
QIO_STATUS QsrcJbigIoctl(void *_j, QioIoctl ioctl, Uint32 *args)
{
	JbigSrc *j = (JbigSrc *)_j;
	ASSERT(j);
	
	switch(ioctl)
		{
		case QioGetNumberOfPlanes:
			break;

		case QioGetImageHeight:
			*args = JbigImageGetHeight(j->eimage);
			break;
			
		case QioGetImagePixelWidth:
			*args = JbigImageGetPixelWidth(j->eimage);
			break;

		case QioSetImageHeight:
			JbigImageSetHeight(j->eimage, *args);
			break;
			
		case QioSetImagePixelWidth:
			JbigImageSetPixelWidth(j->eimage, *args);
			break;
			
		default:
			return QIO_NOT_IMPLEMENTED;
		}
	
	
	return(QIO_SUCCESS);
}


QIO_STATUS QsrcJbigDelete(void *_j)
{
	JbigSrc *j = (JbigSrc *)_j;
	
	JbigImageDelete(j->eimage);
	
}



// [for test]  by Cellming.Chen (2006-11-10)	-----Start
#if	1
QIO_STATUS ACC_QsrcJbigOpenNEW(char *name, int flag, int mode, void **fd, Uint8 PageIndex)
{
	// ~~~ step 01 ~~~

	JbigSrc *j;	
	j = (JbigSrc *)malloc(sizeof(JbigSrc));
	ASSERT(j);

	// ~~~ step 02 ~~~

	// padding 1111
		
// [for test]  by Cellming.Chen (2006-11-10)	-----Start
#if	0
	j->eimage = JbigImageNew(name);
#else
	j->eimage = gACC_jbig_images[PageIndex];
#endif
// [for test]  by Cellming.Chen (2006-11-10)	-----End

	ASSERT(j->eimage);
	// padding 2222	
	j->decoder = JbigDecNew();
	// padding 3333	
	j->CurrentBand = 0;

	// ~~~ step 03 ~~~
	
	*fd = (void*)j;


	return QIO_SUCCESS;
}
#else
QIO_STATUS ACC_QsrcJbigOpenNEW(char *name, int flag, int mode, void **fd)
{
	// ~~~ step 01 ~~~

	JbigSrc *j;	
	j = (JbigSrc *)malloc(sizeof(JbigSrc));
	ASSERT(j);

	// ~~~ step 02 ~~~

	// padding 1111	
	j->eimage = JbigImageListFind(name);	
	ASSERT(j->eimage);
	// padding 2222	
	j->decoder = JbigDecNew();
	// padding 3333	
	j->CurrentBand = 0;

	// ~~~ step 03 ~~~
	
	*fd = (void*)j;

	
	
	return QIO_SUCCESS;
}
#endif
// [for test]  by Cellming.Chen (2006-11-10)	-----End


QIO_STATUS ACC_QsrcJbigBufGetNEW( void *_j,
							  Uint8* dataBuf,
							  Uint32 numBytesReq,
							  Uint32* numBytesRead )
{
	JbigSrc *j = (JbigSrc *)_j;
	//Uint32 *dst;	// by Cellming (2006-05-25)
	Uint8 *	p_Buf;	// by Cellming (2006-05-25)
	Uint8 *src;
	Uint32 dstused, srcused;
	Uint32 h,w,UnEncodedSize, EncodedSize;
	JbigStatus sts;
	int try;
	
	JbigImageGetBand( j->eimage,
					  j->CurrentBand,
					  &UnEncodedSize, &h, &w,
					  &EncodedSize, &src );
	
	if(src==NULL)
		{
		*numBytesRead = 0;
		*dataBuf = NULL;
		return QIO_EOP;
		}

	/*
	dst = (Uint32*)MallocAligned(UnEncodedSize + 4, 8);
	ASSERT(dst);
	
	*dst++ = UnEncodedSize;
	
	*dataBuf = (Uint8*)dst;
	*/

	//AllocRudeDataBand(&p_Buf, UnEncodedSize, &cur_rudedata_band[Index_PrintPage]);	// cm (2006-09-08)
	//*dataBuf = p_Buf;																	// cm (2006-09-08)

	p_Buf = dataBuf;
	
	
	sts=JbigError;
	try = 0;
	
	do	{
		JbigDecInit_ACC(j->decoder, h, w, h);				// by Cellming
		//JbigDecoderInit(j->decoder, BankJbigHeader);	// by Cellming
		
		sts = JbigDecDecodeStripe( j->decoder,
								   p_Buf,
								   UnEncodedSize,
								   src,
								   EncodedSize,
								   &dstused,
								   &srcused );
		}while(sts!=JbigSuccess && (try++ < 4));
	
	if(try>1)
		PSPRINTF("JBIGSRC:  Got sts = %d after %d tries.\n", sts, try);
	
	*numBytesRead = dstused;
	
	ASSERT(*numBytesRead==UnEncodedSize);				// by Cellming
	
	j->CurrentBand++;
	
	return QIO_SUCCESS;
}

// -------------------------------------------------------------------
API_RET QsrcJbigStatus_zy(Uint32 *w, Uint32 *h, Uint32 *UnEncodedsize, void *handle)
{
	//QsrcJbigGetNEW(sJbigHandle, QIO_OPT_WIDTH, (void*)w);
	//QsrcJbigGetNEW(sJbigHandle, QIO_OPT_HEIGHT, (void*)h);
	//QsrcJbigGetNEW(sJbigHandle, QIO_OPT_UNENCODEDSIZE, (void*)UnEncodedsize);	

	QsrcJbigGetNEW(handle, QIO_OPT_WIDTH, (void*)w);
	QsrcJbigGetNEW(handle, QIO_OPT_HEIGHT, (void*)h);
	QsrcJbigGetNEW(handle, QIO_OPT_UNENCODEDSIZE, (void*)UnEncodedsize);	
	return API_OK;
}

QIO_STATUS QsrcJbigDelete_zy(void *_j)
{
	JbigSrc *j = (JbigSrc *)_j;
	
	JbigImageDelete_zy(j->eimage);
	
}

QIO_STATUS QsrcJbigOpen_zy(char *name, int flag, int mode, void **fd)
{
	JbigSrc *j;	
	j = (JbigSrc *)malloc(sizeof(JbigSrc));
	ASSERT(j);

	j->eimage = JbigImageNew(name);
	ASSERT(j->eimage);
	j->decoder = JbigDecNew();
	j->CurrentBand = 0;
	*fd = (void*)j;
	return QIO_SUCCESS;
}

void QsrcJbigAddBand(void *ei, Uint32 UnEncodedSize, Uint32 h, Uint32 w, Uint32 EncodedSize, Uint8 *src)
{
	JbigSrc *j = (JbigSrc *)ei;
	JbigImageAddBand_zy(j->eimage, UnEncodedSize, h, w, EncodedSize, src);
}

⌨️ 快捷键说明

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