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

📄 jbigimage.c

📁 硬件jbig 压缩算法
💻 C
字号:
/*
*  Start of Zoran Standard Header
*  Copyright (c) 2003 Zoran Corporation
*
*
*  All rights reserved.  Proprietary and confidential.
*
*  DESCRIPTION for jbigimage.c
*  	<Enter file description here>
*
*  NEW HISTORY COMMENT (description must be followed by a blank line)
*  include memmgr.h

*  ===== HISTORY of changes in //depot/misc/projects/tps/srcsnk/jbigimage.c
*
*  14/Apr/04 #4 bdodge quiet down
*  25/Oct/03 #3 emiller Revert previous change: strdup is not in our string.h
*  25/Oct/03 #2 emiller strdup()'s definition is in string.h
*
*  End of Zoran Standard Header
*/
#undef PLUS
#include "sys.h"
#define PLUS

#include "string.h"
#include "stdlib.h"
#include "mallocaligned.h"
#include "jbigimage.h"
#include "ipitf.h"
#include "memmgr.h"

#define JBIGSTATIC static

/*
	A band is a buffer of encoded data.
*/

typedef struct _tag_jbig_band
{
	Uint32 h;
	Uint32 w;
	Uint8* data;
	Uint32 EncodedSize;
	Uint32 UnEncodedSize;
	Uint32 AllocedSize;
	struct _tag_jbig_band* next;
}
JBIGBAND, *PJBIGBAND;

#define MAX_JBIG_IMAGE_NAME 64

/*
	Jbig encoded image is a list of jbig encoded bands.
*/

typedef struct _tag_jbig_image
{
	Uint32 		h, w;			// dimensions
	Uint32 		EncodedSize;	// bytes as compressed
	Uint32		UnEncodedSize; 	// original size
	PJBIGBAND	bands;			// linked list of bands (Cellming: point to the first band)
	PJBIGBAND   curBand;		// "cursor" for inserion (Cellming: point to the last band)
	
	char   name[MAX_JBIG_IMAGE_NAME];		// name of image
	struct _tag_jbig_image* next;			// link for listing
}
JBIGIMAGE, *PJBIGIMAGE;

JBIGSTATIC PJBIGIMAGE g_jbig_images = NULL;

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

/*************************************************************************/
JBIGSTATIC PJBIGBAND JbigBandNew(Uint32 UnEncodedSize, Uint32 h,Uint32 w,Uint32 EncodedSize, Uint8 *src)
{
	PJBIGBAND band = (PJBIGBAND)malloc(sizeof(JBIGBAND));
	ASSERT(band);
	
#if	0				// by Cellming
	// this forces copy of encoded data to this band
	// which is a huge performance penalty if the
	// original allocation can be reused
	//
	band->data = MallocAligned(EncodedSize, 8);
	band->AllocedSize = EncodedSize;
	ASSERT(band->data);
	memcpy(band->data, src, EncodedSize);
#else
	band->data			= src;
	band->AllocedSize	= 0;
#endif
	band->EncodedSize	= EncodedSize;
	band->UnEncodedSize	= UnEncodedSize;
	band->h 			= h;
	band->w 			= w;
	band->next 			= NULL;

	
	return (void*)band;
}

JBIGSTATIC PJBIGBAND JbigBandNew_cm(Uint32 UnEncodedSize, Uint32 h,Uint32 w,Uint32 EncodedSize, Uint8 *src)
{
	PJBIGBAND band = (PJBIGBAND)malloc(sizeof(JBIGBAND));
	ASSERT(band);
	
#if	1				// by Cellming
	// this forces copy of encoded data to this band
	// which is a huge performance penalty if the
	// original allocation can be reused
	//
	band->data = MallocAligned(EncodedSize, 8);
	band->AllocedSize = EncodedSize;
	ASSERT(band->data);
	memcpy(band->data, src, EncodedSize);
#else
	band->data			= src;
	band->AllocedSize	= 0;
#endif
	band->EncodedSize	= EncodedSize;
	band->UnEncodedSize	= UnEncodedSize;
	band->h 			= h;
	band->w 			= w;
	band->next 			= NULL;

	
	return (void*)band;
}

/*************************************************************************/
JBIGSTATIC void JbigBandDelete(PJBIGBAND band)
{
	if((band->AllocedSize > 0) && band->data)
		FreeAligned(band->data);
	free(band);
}


/*************************************************************************/
void* JbigImageNew(char *name)
{
	PJBIGIMAGE ei = malloc(sizeof(JBIGIMAGE));
	ASSERT(ei);
	
	memset(ei,0,sizeof(JBIGIMAGE));
	ASSERT(strlen(name) < MAX_JBIG_IMAGE_NAME);
	strcpy(ei->name, name);
	ei->bands	= NULL;
	ei->next	= NULL;
	ei->curBand = NULL;

	
	return (void*)ei;
}

/*************************************************************************/
void JbigImageDelete(void *_ei)
{
	PJBIGIMAGE jei = (PJBIGIMAGE)_ei;
	PJBIGBAND  band;
	
	ASSERT(jei);
	
	// delete bands in image
	for(jei->curBand = jei->bands; jei->curBand;)
		{
		band = jei->curBand;
		jei->curBand = jei->curBand->next;
		// JbigBandDelete(band);		// by Cellming (2006-06-02)
		}
	// and image itself
	free(jei);
	return;
}

/*************************************************************************/
void JbigImageDumpInfo(void *ei)
{
#if 0
	JBIGIMAGE *jei = (JBIGIMAGE *)ei;
	
	printf("JbigImage %s\n"\
	       "\t{\n"\
	       "\th = %d\n"\
	       "\tw = %d\n"\
	       "\tEncodedSize = %d\n"\
	       "\tUnEncodedSize = %d\n"\
	       "\t}\n",
	       jei->name,
	       jei->h,
	       jei->w,
	       jei->EncodedSize,
	       jei->UnEncodedSize);
#endif
}

/*  External API
*/
/*************************************************************************/
void JbigImageSetHeight(void *ei, Uint32 h)
{
	PJBIGIMAGE jei = (JBIGIMAGE *)ei;
	ASSERT(jei);
	jei->h = h;
}

/*************************************************************************/
void JbigImageSetPixelWidth(void *ei, Uint32 w)
{
	PJBIGIMAGE jei = (JBIGIMAGE *)ei;
	ASSERT(jei);
	jei->w = w;
}

/*************************************************************************/
Uint32 JbigImageGetHeight(void *ei)
{
	PJBIGIMAGE jei = (JBIGIMAGE *)ei;
	ASSERT(jei);
	return jei->h;
}

/*************************************************************************/
Uint32 JbigImageGetPixelWidth(void *ei)
{
	PJBIGIMAGE jei = (JBIGIMAGE *)ei;
	ASSERT(jei);
	return jei->w;
}

/*************************************************************************/


/*************************************************************************/
void JbigImageAddBand(void *ei, Uint32 UnEncodedSize, Uint32 h, Uint32 w, Uint32 EncodedSize, Uint8 *src)
{
	PJBIGIMAGE 	jei	= (JBIGIMAGE *)ei;
	PJBIGBAND	band= JbigBandNew(UnEncodedSize, h, w, EncodedSize, src);
	
	ASSERT(jei);
	ASSERT(band);
	
	if(jei->curBand)
		jei->curBand->next = band;
	else
		jei->bands = band;
	jei->curBand = band;
	
	jei->EncodedSize	+= EncodedSize;
	jei->UnEncodedSize 	+= UnEncodedSize;
}

void JbigImageAddBand_cm(void *ei, Uint32 UnEncodedSize, Uint32 h, Uint32 w, Uint32 EncodedSize, Uint8 *src)
{
	PJBIGIMAGE 	jei	= (JBIGIMAGE *)ei;
	PJBIGBAND	band= JbigBandNew_cm(UnEncodedSize, h, w, EncodedSize, src);
	
	ASSERT(jei);
	ASSERT(band);
	
	if(jei->curBand)
		jei->curBand->next = band;
	else
		jei->bands = band;
	jei->curBand = band;
	
	jei->EncodedSize	+= EncodedSize;
	jei->UnEncodedSize 	+= UnEncodedSize;
}

/*************************************************************************/
void JbigImageGetBand(void *ei, Uint32 index, Uint32 *UnEncodedSize, Uint32 *h, Uint32 *w, Uint32 *EncodedSize, Uint8 **src)
{
	PJBIGIMAGE 	jei = (PJBIGIMAGE)ei;
	PJBIGBAND   band;
	
	ASSERT(jei);
	
	for(band = jei->bands; band && index > 0; index--)
		band = band->next;
	
	if(band==NULL)
		{
		*UnEncodedSize 	= 0;
		*EncodedSize 	= 0;
		*h 				= 0;
		*w 				= 0;
		*src 			= NULL;
		}
	else
		{
		*UnEncodedSize 	= band->UnEncodedSize;
		*EncodedSize 	= band->EncodedSize;
		*h 				= band->h;
		*w 				= band->w;
		*src 			= band->data;
		}
}


/*************************************************************************/
void JbigImageListAdd(void *_ei)
{
	PJBIGIMAGE 	jei = (PJBIGIMAGE)_ei;
	
	ASSERT(jei);
	jei->next = g_jbig_images;
	g_jbig_images = jei;
}

/*************************************************************************/
void *JbigImageListFind(char *name)
{
	PJBIGIMAGE 	jei = g_jbig_images;
	
	while(jei)
		{
		if(! strcmp(name, jei->name))
			break;
		jei = jei->next;
		}
	return jei;
}

#if 0 // this function is not needed, but keep if jbig dynamic lists are needed
/*************************************************************************/
void *JbigImageListRemove(void *_ei)
{
	PJBIGIMAGE jei = (PJBIGIMAGE)ei;
	PJBIGIMAGE nxt;
	
	ASSERT(jei);
	
	if(jei == g_jbig_images)
		{
		g_jbig_images = g_jbig_images->next;
		return _ei;
		}
	for(nxt = g_jbig_images; nxt && nxt->next != jei;)
		nxt = nxt->next;
	if(nxt)	{
		nxt->next = jei->next;
		}
	return _ei;
}
#endif

/**delete all the images from the image list*/
/*************************************************************************/
void JbigImageDeleteAll(void)
{
	PJBIGIMAGE jei;
	
	while(g_jbig_images)
		{
		jei 			= g_jbig_images;
		g_jbig_images 	= g_jbig_images->next;
		JbigImageDelete(jei);
		}
}

/*************************************************************************/
/** external API for setting up global compressed data destination
*  block, for persistent storage of jbig image band data. This version
*  handles all the allocs needed, etc. but it is conceivable that a
*  pointer should/could be passed in from above
*/
JBIGSTATIC Uint8* g_perst_store = NULL;
JBIGSTATIC Uint32 g_perst_size  = 0;
JBIGSTATIC Uint8* g_perst_ptr   = NULL;
JBIGSTATIC Uint32 g_perst_free  = 0;
JBIGSTATIC Uint32 g_perst_reserved = 0;

/*************************************************************************/
Bool JbigSetPersistentStore(Uint32 maxsize)
{
	// insist that former storage has been released
	//
	ASSERT(g_perst_store == NULL);
	
	// attempt to allocate maxsize, reducing by 0.5 MB
	// allocation succeedes
	//
	g_perst_size = maxsize;
	
	do	{
		g_perst_store = MallocAligned(g_perst_size, 8);
		
		if(! g_perst_store)
			{
			g_perst_size -= 0x80000;
			
			// if can't get at least 10k, why bother, I expect
			// to get multiple MB here, not peanuts
			//
			if(g_perst_size < 10240)
				{
				g_perst_size = 0;
				return FALSE;
				}
			}
		}
	while(! g_perst_store);
	
	IPPrintf("JBIG - alloc %d for perst bands @ %p\n", g_perst_size, g_perst_store);
	
	g_perst_ptr  	 = g_perst_store;
	g_perst_free 	 = g_perst_size;
	g_perst_reserved = 0;
	return TRUE;
}

/*************************************************************************/
void JbigReleaseResources(void)
{
	IPPrintf("JBIG - deleting all images\n");
	JbigImageDeleteAll();
	
	IPPrintf("JBIG - releasing perst bands\n");
	
	if(g_perst_store)
		{
		FreeAligned(g_perst_store);
		}
	g_perst_store 		= NULL;
	g_perst_free 		= 0;
	g_perst_reserved	= 0;
	g_perst_ptr   		= NULL;
}

/*************************************************************************/
Uint8* JbigReservePersistent(int size)
{
	Uint8* pRet = g_perst_ptr;
	
	ASSERT(g_perst_reserved == 0);
	//IPPrintf("JBIG - reserving %d\n", size);
	if(g_perst_free < size) return NULL;
	g_perst_reserved = size;
	return pRet;
}

/*************************************************************************/
Uint8* JbigAllocPersistent(int size)
{
	Uint8* pRet = g_perst_ptr;
	
	ASSERT(g_perst_free >= size);
#if 0
	ASSERT(g_perst_reserved >= size);
#else
	if(g_perst_reserved < size)
		IPPrintf("JBIG - OVERFLOW!!!!!!!!!!!!!!\n");
#endif
	//IPPrintf("JBIG - actually use %d of %d (%d%%)\n",
	//		size, g_perst_reserved, (size * 100)/g_perst_reserved);
	
	g_perst_free -= size;
	g_perst_ptr  += size;
	g_perst_reserved = 0;
	
	return pRet;
}

/*************************************************************************/
Uint32 JbigGetPersistentStatus(Uint32* pnTot, Uint32* pnUsed, Uint32* pnRes)
{
	if(pnTot)  *pnTot  = g_perst_size;
	if(pnUsed) *pnUsed = g_perst_size - g_perst_free + g_perst_reserved;
	if(pnRes)  *pnRes  = g_perst_reserved;
	return g_perst_free - g_perst_reserved;
}


// ------------------------------------------------------------------------
Uint32 JbigImageGetUnEncodedSize(void *ei)
{
	PJBIGIMAGE jei = (JBIGIMAGE *)ei;
	ASSERT(jei);
	return jei->UnEncodedSize;
}
JBIGSTATIC PJBIGBAND JbigBandNew_zy(Uint32 UnEncodedSize, Uint32 h,Uint32 w,Uint32 EncodedSize, Uint8 *src)
{
	PJBIGBAND band = (PJBIGBAND)malloc(sizeof(JBIGBAND));
	ASSERT(band);
	band->data			= src;
	band->AllocedSize	= EncodedSize;
	band->EncodedSize	= EncodedSize;
	band->UnEncodedSize	= UnEncodedSize;
	band->h 			= h;
	band->w 			= w;
	band->next 			= NULL;
	return (void*)band;
}
JBIGSTATIC void JbigBandDelete_zy(PJBIGBAND band)
{
	if((band->AllocedSize > 0) && band->data)
		free(band->data);
	free(band);
	return;
}
void JbigImageDelete_zy(void *_ei)
{
	PJBIGIMAGE jei = (PJBIGIMAGE)_ei;
	PJBIGBAND  band;
	
	ASSERT(jei);
	
	// delete bands in image
	for(jei->curBand = jei->bands; jei->curBand;)
		{
		band = jei->curBand;
		jei->curBand = jei->curBand->next;
		JbigBandDelete_zy(band);		
		}
	// and image itself
	free(jei);
	return;
}


void JbigImageAddBand_zy(void *ei, Uint32 UnEncodedSize, Uint32 h, Uint32 w, Uint32 EncodedSize, Uint8 *src)
{
	PJBIGIMAGE 	jei	= (JBIGIMAGE *)ei;
	PJBIGBAND	band= JbigBandNew_zy(UnEncodedSize, h, w, EncodedSize, src);
	
	ASSERT(jei);
	ASSERT(band);
	
	if(jei->curBand)
		jei->curBand->next = band;
	else
		jei->bands = band;

	jei->curBand = band;
	jei->EncodedSize	+= EncodedSize;
	jei->UnEncodedSize 	+= UnEncodedSize;
}


⌨️ 快捷键说明

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