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

📄 jbigsnk.c

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

/** @file jbigsnk.c
* The sink provides and interface for creating a jbig compressed image in memory.

- A sink is created for each image/page.
- A sink stores a list of jbig encoded bands in memory.
- Each band is compressed as an individual image, so there are no dependencies between bands.
- A band can be any size.
- A band can be made up of any type of data, formatted, raw, rgb, bitonal, etc.
- A band is added to the encoded image with each call to QsnkJbigProduced().
- Bands are stored in the order of calls to QsnkJbigProduced().
*/
#undef PLUS
#include "sys.h"
#define PLUS

#include "stdlib.h"
#include "jbig.h"
#include "string.h"
#include "jbigimage.h"
#include "qio.h"
#include "jbigsnk.h"
#include "stdio.h"
#include "mallocaligned.h"
#include "memmgr.h"





// [for test]  by Cellming.Chen (2006-11-10)	-----Start
#if	1
#define		JBIG_COPY_BAND
#else
#endif
// [for test]  by Cellming.Chen (2006-11-10)	-----End


/**A structure to store the state of the sink.*/
typedef struct
{
	void *eimage;		// A JBIG image handle.
	void *encoder;		// A JBIG encoder handle.
	
	Uint8*	pSinkBase;	// Base Location of memory to allocate for images
}
JbigSink;






/**Install the JBIG sink into the source/sink manager.*/
void QsnkJbigInit(void)
{
#ifndef OLD_SCHOOL_API

	QIO_STATUS sts;
	
	sts = QsnkAddSnk( "/jbig",
					  QsnkJbigOpen,
					  QsnkJbigClose,
					  QsnkJbigSet,
					  QsnkJbigGet,
					  QsnkJbigAbort,
					  QsnkJbigBufGet,
					  QsnkJbigProduced );
	
	ASSERT(sts==QIO_SUCCESS);
	
#endif
}


/** Opens a JBIG image data sink.  The sink is used to store a JBIG
* compressed image in memory.
*
* @param name - The name of the image to open.
* @param flag - Not used set to zero.
* @param mode - Not used set to zero.
*
* @return  QIO_STATUS
*/
QIO_STATUS QsnkJbigOpenNEW(char *name, int flags, int mode, void **fd)
{
	JbigSink *j = (JbigSink *)malloc(sizeof(JbigSink));
	
	ASSERT(j);
	
	j->encoder = JbigEncNew();
	j->eimage = JbigImageNew(name);
	
	*fd = (void*)j;
	
	return QIO_SUCCESS;
}




/** Close a JBIG image sink. Closing the sink adds the image to list
*  of encoded images.  The image can be later decprossed with the
*  QioJsrc module.
*
* @param _j - A handle to an open jbig sink.
*
* @return  QIO_STATUS
*/
QIO_STATUS  QsnkJbigCloseNEW(void *_j)
{
	JbigSink *j = (JbigSink *)_j;
	
	JbigEncDelete(j->encoder);
	
	JbigImageListAdd(j->eimage);
	
	/*JbigImageDumpInfo(j->eimage);*/
	free(j);
	
	return (QIO_SUCCESS);
}

/**
*
* @param _j - A handle to an open jbig sink.
*
* @return  QIO_STATUS
*/
QIO_STATUS QsnkJbigSetNEW(void *_j, QIO_OPTION option, void *args)
{
	JbigSink *j = (JbigSink *)_j;
	ASSERT(j);
	
	switch(option)
		{
		case QIO_OPT_HEIGHT:
			JbigImageSetHeight(j->eimage, (Uint32)args);
			break;
		case QIO_OPT_WIDTH:
			JbigImageSetPixelWidth(j->eimage, (Uint32)args);
			break;
		default:
			//for now don't die just ignore unknow options
			//  return QIO_NOT_IMPLEMENTED;
			break;
		}
	
	
	return(QIO_SUCCESS);
}

QIO_STATUS QsnkJbigGetNEW(void *_j, QIO_OPTION option, void *args)
{
	JbigSink *j = (JbigSink *)_j;
	ASSERT(j);
	/*fix me - doing this just so we can look like the printer.*/
	
	*(Uint32*)args = -1;
	return QIO_SUCCESS;
	
	/*  return QIO_NOT_IMPLEMENTED; */
	
}

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

/** Creates an aligned, dynamically allocated buffer of size
*  size_in_bytes in rawdata.  Fill this buffer with image data and
*  then call QsnkJbigProduced.
*
* @param _j - A handle to an open jbig sink.
*
* @param dataBuf - Returns pointer to buffer to fill.
*
* @param sizeOfBuffer- Size in bytes of the buffer.
*
*  @return - QIO_STATUS
*/
QIO_STATUS QsnkJbigBufGetNEW(void *_j, Uint8** dataBuf, Uint32 sizeOfBuffer)
{
	JbigSink *j = (JbigSink *)_j;
	
	ASSERT(j);
	*dataBuf = (Uint8*)MallocAligned((size_t)sizeOfBuffer, 8);
	
	if (*dataBuf == NULL)
		return (QIO_OUT_OF_MEM);
	
	return QIO_SUCCESS;
	
}

/** Encodes the buffer and adds it to the list of bands for this
* image.  The argument bufSize must be the same as the value returned
* in the corresponding call to GetBuf.
*
* @param dataBuf - Pointer to buffer given by QsnkJbigBufGet.
*
* @param bufSize - Size in bytes of the buffer.
*
* @return - QIO_STATUS
*/
QIO_STATUS QsnkJbigProducedNEW(void *_j, Uint8* dataBuf, Uint32 bufSize)
{
	JbigSink *j = (JbigSink *)_j;
	Uint8 *dst;
	Uint32 dstused, srcused;
	Uint32 h, w;
	Uint32 bitSize = bufSize * 8;
	
	w =  JbigImageGetPixelWidth(j->eimage);
	
	ASSERT(w);
	
	h = bitSize/w;
	
	ASSERT(w<0x10000);
	ASSERT(h<0x10000);
	
#ifdef JBIG_COPY_BAND
	// jbig images copy band data, so just alloc
	// band out of regular memory
	dst = MallocAligned((size_t)bufSize, 8);
#else
	// make sure at least bufSize is available in
	// the pool of jbig band memory
	dst = JbigReservePersistent(bufSize);
#endif
	
	ASSERT(dst);
	
	JbigEncInit(j->encoder, h, w,(Uint16)h);
	
	JbigEncEncodeStripe(j->encoder, dst, bufSize,
			    dataBuf, bufSize,
			    &dstused, &srcused);
	
	FreeAligned(dataBuf);
	
	JbigImageAddBand(j->eimage, bufSize, h, w, dstused, dst);
	
#ifdef JBIG_COPY_BAND
	// only free the dst if jbigimage.c copies it
	FreeAligned(dst);
#else
	// move the jbig band pool free ptr and size
	// to the actual used portion of the jbig dst buffer
	//
	JbigAllocPersistent(dstused);
#endif
	
	//printf("QsnkJbigProduced %d -> %d (%d)\n", bufSize, dstused, (dstused * 100)/bufSize);
	
	return(QIO_SUCCESS);
}





/*Old school API*/
#ifdef OLD_SCHOOL_API

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



API_RET QsnkJbigClose(void)
{
	return (API_RET)QsnkJbigCloseNEW(sJbigSnkHandle);
}

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


API_RET QsnkJbigStatus(void *statusStruct)
{
	return API_OK;
}


API_RET QsnkJbigAbort(int flags)
{
	QsnkJbigAbortNEW(sJbigSnkHandle, flags);
	return API_OK;
}

int QsnkJbigBufGet(Uint8** dataBuf, Uint32 sizeOfBuffer)
{
	return QsnkJbigBufGetNEW(sJbigSnkHandle, dataBuf, sizeOfBuffer);
}


API_RET QsnkJbigProduced(Uint8* dataBuf, Uint32 bufSize)
{
	return (API_RET)QsnkJbigProducedNEW(sJbigSnkHandle, dataBuf, bufSize);
}

API_RET QsnkJbigSetup(SRCSNK_SETUP_INFO *info, void *setupStruct)
{
	Uint32 *setup_parm;
	
	QsnkJbigSetNEW(sJbigSnkHandle, QIO_OPT_HEIGHT, (void *)info->desc.lengthPix);
	QsnkJbigSetNEW(sJbigSnkHandle, QIO_OPT_WIDTH, (void *)info->desc.widthPix);
	
	setup_parm = setupStruct;
	return(API_OK);
}

API_RET QsnkJbigDiscardData(Uint32 numBytesToDiscard)
{
	return(API_OK);
}




#endif






// [for test]  by Cellming.Chen (2006-11-09)	-----Start
#if	0
#else

QIO_STATUS	ACC_QsnkJbigOpenNEW(char *name, int flags, int mode, void **fd, void **ei)
{
	JbigSink *j = (JbigSink *)malloc(sizeof(JbigSink));
	
	ASSERT(j);
	
	j->encoder = JbigEncNew();
	j->eimage = JbigImageNew(name);


	

	*ei		= j->eimage;

	
	
	*fd = (void*)j;
	
	return QIO_SUCCESS;
}

QIO_STATUS  ACC_QsnkJbigCloseNEW(void *_j)
{
	JbigSink *j = (JbigSink *)_j;
	
	JbigEncDelete(j->encoder);
	
	JbigImageListAdd(j->eimage);
	
	/*JbigImageDumpInfo(j->eimage);*/
	free(j);
	
	return (QIO_SUCCESS);
}

QIO_STATUS	ACC_QsnkJbigProducedNEW(void *_j, Uint8* dataBuf, Uint32 bufSize)
{
	JbigSink *j = (JbigSink *)_j;
	Uint8 *dst;
	Uint32 dstused, srcused;
	Uint32 h, w;
	Uint32 bitSize = bufSize * 8;
	
	w =  JbigImageGetPixelWidth(j->eimage);
	
	ASSERT(w);
	
	h = bitSize/w;
	
	ASSERT(w<0x10000);
	ASSERT(h<0x10000);
	
#ifdef JBIG_COPY_BAND
	// jbig images copy band data, so just alloc
	// band out of regular memory
	dst = MallocAligned((size_t)bufSize, 8);
#else
	// make sure at least bufSize is available in
	// the pool of jbig band memory
	dst = JbigReservePersistent(bufSize);
#endif
	
	ASSERT(dst);
	
	JbigEncInit(j->encoder, h, w,(Uint16)h);
	
	JbigEncEncodeStripe(j->encoder, dst, bufSize,
			    dataBuf, bufSize,
			    &dstused, &srcused);
	
	//FreeAligned(dataBuf);		// by Cellming.Chen (2006-11-10)
	
	JbigImageAddBand_cm(j->eimage, bufSize, h, w, dstused, dst);
	
#ifdef JBIG_COPY_BAND
	// only free the dst if jbigimage.c copies it
	FreeAligned(dst);
#else
	// move the jbig band pool free ptr and size
	// to the actual used portion of the jbig dst buffer
	//
	JbigAllocPersistent(dstused);
#endif
	
	//printf("QsnkJbigProduced %d -> %d (%d)\n", bufSize, dstused, (dstused * 100)/bufSize);
	
	return(QIO_SUCCESS);
}



QIO_STATUS QsnkJbigIoctl(void *_j, QioIoctl ioctl, void *args)
{
  JbigSink *j = (JbigSink *)_j;
  ASSERT(j);

  switch(ioctl)
    {
    case QioSetImageHeight:
      JbigImageSetHeight(j->eimage, (Uint32)args);
      break;
    case QioSetImagePixelWidth:
      JbigImageSetPixelWidth(j->eimage, (Uint32)args);
      break;
    default:
      return QIO_NOT_IMPLEMENTED;
    }


  return(QIO_SUCCESS);
}

#endif
// [for test]  by Cellming.Chen (2006-11-09)	-----End




⌨️ 快捷键说明

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