📄 ezw.cpp
字号:
1<<Log2(coder->Magnitude[n][i]));
}
i++; /* next coefficient */
}
}
}
/* highpass */
for (n = coder->nSubbands-4; n > 0; n--){
i = 0;
/* descendants subband index */
cn = n+3;
cXSize = coder->SubbandXSize[cn];
for (y=0; y<coder->SubbandYSize[n]; y++){
for (x=0; x<coder->SubbandXSize[n]; x++){
index = EZWQuantize(coder->SubbandPtr[n][i], 1.0);
coder->Magnitude[n][i] = abs(index);
if (SIGN(index) > 0){
coder->State[n][i] = SETBIT(coder->State[n][i], SIGN_CTX_BIT);
}
if (coder->Magnitude[n][i]>0){
coder->MaxMagnitude[n][i] = SETBIT(coder->MaxMagnitude[n][i],
1<<Log2(coder->Magnitude[n][i]));
}
bx = x<<1;
by = y<<1;
coder->MaxMagnitude[n][i] |= (coder->MaxMagnitude[cn][(by)*cXSize+bx] |
coder->MaxMagnitude[cn][(by)*cXSize+bx+1] |
coder->MaxMagnitude[cn][(by+1)*cXSize+bx] |
coder->MaxMagnitude[cn][(by+1)*cXSize+bx+1]);
i++; /* next coefficient */
}
} /* end subband */
}
/* baseband */
i = 0;
for (y=0; y<coder->SubbandYSize[0]; y++){
for (x=0; x<coder->SubbandXSize[0]; x++){
index = EZWQuantize(coder->SubbandPtr[n][i], 1.0);
coder->Magnitude[0][i] = abs(index);
if (SIGN(index) > 0){
coder->State[0][i] = SETBIT(coder->State[0][i], SIGN_CTX_BIT);
}
if (coder->Magnitude[0][i]>0){
coder->MaxMagnitude[0][i] = SETBIT(coder->MaxMagnitude[0][i],
1<<Log2(coder->Magnitude[0][i]));
}
coder->MaxMagnitude[0][i] |= (coder->MaxMagnitude[1][i] |
coder->MaxMagnitude[2][i] |
coder->MaxMagnitude[3][i]);
i++; /* next coefficient */
}
}
/* find the maximum magnitude */
MaxMag = 0;
for(i=0; i < coder->SubbandSize[0]; i++){
if (MaxMag < coder->MaxMagnitude[0][i]){
MaxMag = coder->MaxMagnitude[0][i];
}
if (MaxMag<coder->Magnitude[0][i]){
MaxMag = coder->Magnitude[0][i];
}
}
coder->MaxBitPlane = Log2(MaxMag);
coder->CurrentBitPlane = coder->MaxBitPlane;
coder->MaxThreshold = 1<<(coder->MaxBitPlane);
coder->CurrentThreshold = coder->MaxThreshold;
fprintf(stdout, "Max Bitplane = %d\n", coder->MaxBitPlane);
}
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
int EZWWSymbol(Coder *coder,int symbol)
{
if(coder->CodedSymbols < coder->TargetBits)
{
fprintf(coder->file,"%d ",symbol);
coder->CodedSymbols++;
return 0;
}
else{
return 1;
}
}
int EZWRSymbol(Coder *coder,int *symbol)
{
if (coder->CodedSymbols < coder->TargetBits)
{
fscanf(coder->file,"%d ",symbol);
coder->CodedSymbols++;
return 0;
}
else{
return 1;
}
}
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
void EZWDeleteData(void *data)
{
free((ListData *)data);
}
/*--------------------------------------------------------------------------------------------*/
ListData *EZWCreateData(int i, int x, int y, char n)
{
ListData *data;
if ( (data = (ListData *)malloc(sizeof(ListData)))==NULL){
Error("CreateData: Fail to create data\n");
}
data->i = i;
data->x = x;
data->y = y;
data->n = n;
return data;
}
/*--------------------------------------------------------------------------------------------*/
void EZWChildrenPareCtxBitUpdate(Coder *coder, int n, int x, int y)
{
int i, j;
if (n==0){
// 3 offsprings at LH, HL & HH
coder->State[1][y*coder->SubbandXSize[1]+x]
= SETBIT(coder->State[1][y*coder->SubbandXSize[1]+x], PARE_CTX_BIT);
coder->State[2][y*coder->SubbandXSize[3]+x]
= SETBIT(coder->State[2][y*coder->SubbandXSize[2]+x], PARE_CTX_BIT);
coder->State[3][y*coder->SubbandXSize[3]+x]
= SETBIT(coder->State[3][y*coder->SubbandXSize[3]+x], PARE_CTX_BIT);
}
else if (n<coder->nSubbands-3){
n=n+3;
x<<=1;
y<<=1;
i = y*coder->SubbandXSize[n]+x;
j = i + coder->SubbandXSize[n];
coder->State[n][i] = SETBIT(coder->State[n][i], PARE_CTX_BIT);
coder->State[n][i+1] = SETBIT(coder->State[n][i+1], PARE_CTX_BIT);
coder->State[n][j] = SETBIT(coder->State[n][j], PARE_CTX_BIT);
coder->State[n][j+1] = SETBIT(coder->State[n][j+1], PARE_CTX_BIT);
}
}
/*--------------------------------------------------------------------------------------------*/
void EZWCSigContextUpdate(Coder *coder, int i, int x, int y, int n)
{
EZWChildrenPareCtxBitUpdate(coder, n, x, y);
}
/*--------------------------------------------------------------------------------------------*/
int EZWCSigContext(Coder *coder, int i, int n, int LastScanSig)
{
return (LastScanSig<<1) + CHKBIT(coder->State[n][i], PARE_CTX_BIT);
}
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//一些常用的数学函数
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
int sign(Real x)
{
return (x > 0 ? 1 : x < 0 ? -1 : 0);
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* maximum power of 2 integer less than or equal x */
/* 0 <= x < 2, return 0 */
/* 2 <= x < 4, return 1 */
/* 4 <= x < 8, return 2 */
/* 8 <= x < 16, return 3 */
/* and so on... */
int Log2(int x)
{
int count = 0;
while (x > 1) {
x >>= 1;
count++;
}
return count;
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/* function for fixing the precision of floating number. Used in storing the floating point
* parameters. These functions should be used with care. Overflow may occur and we
* have no mechanism to guard this yet.
*
*/
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
void Error(char *fmt, ...)
{
va_list argptr;
va_start( argptr, fmt );
fprintf(stderr, "Error: " );
vprintf( fmt, argptr );
va_end( argptr );
exit( -1 );
}
/*--------------------------------------------------------------------------------------------*/
/* Note that if we want to use array notations, we need to allocate memory for pointers. */
/* So we just do a linear array and use arithmetic to access the memory. */
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//数型结构中用到
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------------------------------*/
void EZWDListInit(DList *dlist, void (*destroy)(void *data),
int (*match)(const void *key1, const void *key2),
void (*print)(int i, const void *data))
{
dlist->size = 0;
dlist->destroy = destroy;
dlist->match = match;
dlist->print = print;
dlist->head = NULL;
dlist->tail = NULL;
return;
}
/*--------------------------------------------------------------------------------------------*/
void EZWDListDestroy(DList *dlist)
{
void *data;
/* Remove each element */
while (DListSize(dlist) > 0){
if (EZWDListRemove(dlist, DListTail(dlist), (void **)&data) == 0
&& dlist->destroy != NULL){
/* Call a user-defined function to free dynamically allocated data */
dlist->destroy(data);
}
}
memset(dlist, 0, sizeof(DList));
return;
}
/*--------------------------------------------------------------------------------------------*/
int EZWDListAppend(DList *dlist, const void *data)
{
return EZWDListInsertNext(dlist, DListTail(dlist), data);
}
/*--------------------------------------------------------------------------------------------*/
int EZWDListInsertAsHead(DList *dlist, const void *data)
{
return EZWDListInsertPrev(dlist, DListHead(dlist), data);
}
/*--------------------------------------------------------------------------------------------*/
int EZWDListInsertNext(DList *dlist, DListElement *element, const void *data)
{
DListElement *NewElement;
if ((NewElement = (DListElement *)malloc(sizeof(DListElement))) == NULL){
return -1;
}
NewElement->data = (void *)data;
if (DListSize(dlist) == 0){
/* Handle insertion when the list is empty */
dlist->head = NewElement;
dlist->head->prev = NULL;
dlist->head->next = NULL;
dlist->tail = NewElement;
}
else{
/* handle insertion when list is not empty */
NewElement->next = element->next;
NewElement->prev = element;
if(element->next == NULL){
dlist->tail = NewElement;
}
else{
element->next->prev = NewElement;
}
element->next = NewElement;
}
/* adjust the size of the list to account for the inserted element */
dlist->size++;
return 0;
}
/*--------------------------------------------------------------------------------------------*/
int EZWDListInsertPrev(DList *dlist, DListElement *element, const void *data)
{
DListElement *NewElement;
if ((NewElement = (DListElement *)malloc(sizeof(DListElement))) == NULL){
return -1;
}
NewElement->data = (void *)data;
if (DListSize(dlist) == 0){
/* Handle insertion when the list is empty */
dlist->head = NewElement;
dlist->head->prev = NULL;
dlist->head->next = NULL;
dlist->tail = NewElement;
}
else{
/* handle insertion when list is not empty */
NewElement->next = element;
NewElement->prev = element->prev;
if (element->prev == NULL){
dlist->head = NewElement;
}
else{
element->prev->next = NewElement;
}
element->prev = NewElement;
}
/* adjust the size of the list to account for the inserted element */
dlist->size++;
return 0;
}
/*--------------------------------------------------------------------------------------------*/
/* insert a sub list before the element into the main list
*
* author : mow-song, ng
* date : 25 Apr. 2004
*
*/
int EZWDListInsertListPrev(DList *mainlist, DList *sublist, DListElement *element)
{
DListElement *prev;
if (mainlist->match != sublist->match || mainlist->print != sublist->print ||
mainlist->destroy != sublist->destroy){
return -1;
}
if (DListSize(sublist)==0){
/* handle insertion when sublist is empty */
/* NOP */
}
else if ((DListSize(mainlist)==0)){
/* handle insertion when mainlist is empty */
mainlist->head = sublist->head;
mainlist->tail = sublist->tail;
mainlist->size = sublist->size;
}
else{
/* connect sublist to previous element */
if (element == DListHead(mainlist)){
// no previous element
mainlist->head = sublist->head;
}
else{
prev = element->prev;
prev->next = sublist->head;
sublist->head->prev = prev;
}
/* connect the tail of the sublist to element */
sublist->tail->next = element;
element->prev = sublist->tail;
mainlist->size += sublist->size;
}
return 0;
}
/*--------------------------------------------------------------------------------------------*/
/* insert a sub list after the element into the main list
*
* author : mow-song, ng
* date : 25 Apr. 2004
*
*/
int EZWDListInsertListNext(DList *mainlist, DList *sublist, DListElement *element)
{
DListElement *next;
if (mainlist->match != sublist->match || mainlist->print != sublist->print ||
mainlist->destroy != sublist->destroy){
return -1;
}
if (DListSize(sublist)==0){
/* handle insertion when sublist is empty */
/* NOP */
}
else if ((DListSize(mainlist)==0)){
/* handle insertion when mainlist is empty */
mainlist->head = sublist->head;
mainlist->tail = sublist->tail;
mainlist->size = sublist->size;
}
else{
/* connect tail of sublist to next element */
if (element == DListTail(mainlist)){
// no next element
mainlist->tail = sublist->tail;
}
else{
next = element->next;
next->prev = sublist->tail;
sublist->tail->next = next;
}
/* connect the head of sublist to the element */
element->next = sublist->head;
sublist->head->prev = element;
mainlist->size += sublist->size;
}
return 0;
}
/*--------------------------------------------------------------------------------------------*/
int EZWDListRemove(DList *dlist, DListElement *element, void **data)
{
if (element == NULL || DListSize(dlist) == 0){
return -1;
}
/* return the data pointer */
*data = element->data;
/* remove the element from the list */
if (element == dlist->head){
dlist->head = element->next;
if (dlist->head == NULL){
dlist->tail = NULL;
}
else{
element->next->prev = NULL;
}
}
else{
element->prev->next = element->next;
if (element->next == NULL){
dlist->tail = element->prev;
}
else{
element->next->prev = element->prev;
}
}
/* free the list element */
free(element);
/* adjust the list size */
dlist->size--;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -