📄 reindex.c
字号:
/*****************************************************************************/
/* Copyright 1999, Sharp Labs. of America. */
/* All rights reserved */
/* File: "reindex.c" */
/* Description: color reindexing for palette-based JPEG2000 */
/* Author: Wenjun Zeng */
/* Affiliation: Sharp Labs. of America., zengw@sharplabs.com */
/* Version: VM6.0 */
/* Last Revised: 4 Jan, 2000 */
/*****************************************************************************/
/*_______________________________________________________________
| |
| |
| suboptimal reindexing of color table for more efficient |
| lossless compression |
| This version only accepts .pgx file format (for index map) |
| It will also read in the color table, reorder it |
| correspondingly, and output a new color table |
| Currently limited to max. of 256 colors |
| (i.e., bitdepth of pgx file <=8) |
| |
_______________________________________________________________*/
#include <stdio.h>
#include "palette.h"
main( int argc, char **argv)
{
FILE *out, *ptin;
unsigned int rows, cols;
int *buff,*ptr, *buff_tbl, *ptr_tbl;
int i,j, m,n;
int *map, *imap, *assign, *ct, **count;
int max_val, min_val;
int ct_max, idx_max, lb,hb;
int iterate, candidate_Lmax, candidate_Rmax;
float weight,*lut, cost_L, cost_R, cost_Lmax, cost_Rmax;
unsigned char *buff_tbl_bak;
int entries, comp, length, bitdepth, product;
/* CheckUsage */
if (argc!=5)
{
fprintf(stderr,"usage: reindex ori_index(.pgx) ori_tbl(.tbl) new_index(.pgx) new_tbl(.tbl) \n");
exit(-1);
}
/* read original index image in pgx format */
ptin =read_pgx_header (argv[1],&rows,&cols);
buff=(unsigned int *)alloc_int1(rows*cols);
read_int_1D_raw(ptin,buff,rows*cols);
/* read original table file (one header line following by raw data) */
ptin =read_tbl_header(argv[2],&entries,&comp);
length=entries*comp;
buff_tbl=(unsigned int *)alloc_int1(length);
read_int_1D_raw(ptin,buff_tbl,length);
if((buff_tbl_bak=(unsigned char*)malloc(length*sizeof(char)))==NULL)
{
fprintf(stderr,"error on allocation \n");
exit(1);
}
ct=( int *)alloc_int1(entries);
assign=( int *)alloc_int1(entries);
map=( int *)alloc_int1(entries*2);
imap=( int *)alloc_int1(entries);
count=( int **)alloc_int2(entries,entries);
if((lut=(float*)malloc(length*sizeof(float)))==NULL)
{
fprintf(stderr,"error on allocation \n");
exit(1);
}
/* begin optimization */
for (i=0; i<entries; i++)
{
ct[i]=0;
assign[i]=0;
for (j=0; j<entries; j++) count[i][j]=0;
lut[i]=log(1+1.0/(i+1));
}
ptr=buff;
max_val=0;
min_val=99999;
for (i=0; i< rows*cols; i++, ptr++)
{
if (*ptr>max_val) max_val=*ptr;
if (*ptr<min_val) min_val=*ptr;
}
/*calculate the cross-count */
/* hori. direction */
ptr=buff;
for (i=0; i< rows; i++)
{
for (j=0; j<cols-1;j++)
{
m=*ptr++;
n=*ptr;
if (m!=n)
{
count[m][n]+=1;
ct[m]+=1;
ct[n]+=1;
}
}
ptr++;
}
/* vertical direction */
for (j=0; j<cols;j++)
{
ptr=buff+j;
for (i=0; i< rows-1; i++)
{
m=*ptr;
n=*(ptr+cols);
if (m!=n)
{
count[m][n]+=1;
ct[m]+=1;
ct[n]+=1;
}
ptr+=cols;
}
}
/* find the index with max. ct */
ct_max=0;
idx_max=0;
for (i=0; i<=max_val; i++)
{
if (ct[i]>ct_max)
{
ct_max=ct[i];
idx_max=i;
}
}
lb=max_val;
hb=max_val;
map[max_val]=idx_max;
assign[idx_max]=1;
iterate=max_val;
/*start reindexing, speedup possible */
while (iterate>0)
{
cost_Lmax=0;
cost_Rmax=0;
for (i=0;i<=max_val;i++)
{
cost_L=0;
cost_R=0;
if (assign[i]!=1)
{
for(j=lb; j<=hb; j++)
{
m=map[j];
/* simple weighting */
/*
cost_L+=(count[m][i]+count[i][m])/(j-lb+1);
cost_R+=(count[m][i]+count[i][m])/(hb-j+1);
*/
/*suboptimal weighting */
weight=lut[j-lb];
cost_L+=(count[m][i]+count[i][m])*weight;
weight=lut[hb-j];
cost_R+=(count[m][i]+count[i][m])*weight;
}
if (cost_L>cost_Lmax)
{
cost_Lmax=cost_L;
candidate_Lmax=i;
}
if (cost_R>cost_Rmax)
{
cost_Rmax=cost_R;
candidate_Rmax=i;
}
}
}
if (cost_Lmax>cost_Rmax)
{
lb--;
map[lb]=candidate_Lmax;
assign[candidate_Lmax]=1;
}
else
{
hb++;
map[hb]=candidate_Rmax;
assign[candidate_Rmax]=1;
}
iterate--;
}
for (i=lb;i<=hb; i++)
{
imap[map[i]]=i-lb;
}
/*reorder the index */
ptr=buff;
for (i=0; i<rows*cols; i++, ptr++)
{
*ptr=imap[*ptr];
}
/* output index image in pgx format */
bitdepth=1;
product=2;
while (product<=entries-1)
{
bitdepth++;
product*=2;
}
write_int_1D_pgx(argv[3],buff,rows,cols, bitdepth);
/* remap the table */
ptr_tbl=buff_tbl;
for (i=0;i<entries; i++)
{
for (j=0;j<comp;j++)
*(buff_tbl_bak+comp*imap[i]+j)=*ptr_tbl++;
}
/* output the remapped table */
if((out=fopen(argv[4],"w"))==NULL)
{
fprintf(stderr," Cannot open output file \n");
exit(-1);
}
fprintf(out,"%d %d\n",entries,comp);
if(fwrite(buff_tbl_bak,sizeof(char),length,out)!=length)
{
fprintf(stderr," Error on write \n");
exit(-1);
}
fclose(out);
free_int1(buff_tbl);
free(buff_tbl_bak);
free(lut);
free_int1(buff);
free_int1(map);
free_int1(imap);
free_int1(assign);
free_int1(ct);
free_int2(count);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -