📄 ppm2index.c
字号:
/*****************************************************************************/
/* Copyright 1999, Sharp Labs. of America. */
/* All rights reserved */
/* File: "ppm2index.c" */
/* Description: convert ppm (<=256 colors) to index map and color table */
/* Author: Wenjun Zeng */
/* Affiliation: Sharp Labs. of America., zengw@sharplabs.com */
/* Version: VM6.0 */
/* Last Revised: 4 Jan, 2000 */
/*****************************************************************************/
/*_______________________________________________________________
| |
| palettize a color image (in ppm format) with random indexing |
| output the index image in pgx format |
| and the color palette table in raw format. |
| currently limited to max. of 256 colors |
_______________________________________________________________*/
#include <stdio.h>
#include "palette.h"
main( int argc, char **argv)
{
FILE *ptin, *out;
unsigned int rows, cols;
int *buff_rgb,*ptr_rgb,*ptr_idx,*buff_idx;
unsigned char *buff_tbl,*ptr_tbl;
int i, cnt,bitdepth, product;
int entry, comp, length;
/* CheckUsage */
if (argc!=4)
{
fprintf(stderr,"usage: ppm2index input(.ppm) index_image(.pgx) table(.tbl) \n");
exit(-1);
}
comp=3;
/* open input image in ppm format */
ptin =read_pgm_header (argv[1],&rows,&cols);
buff_rgb=(unsigned int *)alloc_int1(rows*cols*comp);
read_int_1D_raw(ptin,buff_rgb,rows*cols*comp);
if((buff_tbl=(unsigned char*)malloc(rows*cols*comp*sizeof(char)))==NULL)
{
fprintf(stderr,"error on allocation \n");
exit(1);
}
buff_idx=(unsigned int *)alloc_int1(rows*cols);
ptr_tbl=buff_tbl;
entry=0;
ptr_rgb=buff_rgb;
ptr_idx=buff_idx;
for (i=0; i<rows*cols; i++)
{
cnt=entry;
ptr_tbl=buff_tbl;
while (((*ptr_tbl != *ptr_rgb) || (*(ptr_tbl+1) != *(ptr_rgb+1)) || (*(ptr_tbl+2) != *(ptr_rgb+2))) && (cnt!=0))
{
ptr_tbl+=3;
cnt--;
}
*ptr_idx++=entry-cnt;
if (cnt==0)
{
entry++;
*ptr_tbl = *ptr_rgb;
*(ptr_tbl+1) = *(ptr_rgb+1);
*(ptr_tbl+2) = *(ptr_rgb+2);
}
ptr_rgb+=3;
}
/* output index image in pgx format */
bitdepth=1;
product=2;
while (product<=entry-1)
{
bitdepth++;
product*=2;
}
/* currently limited to max. of 256 colors */
if(bitdepth>8)
{
fprintf(stderr,"Sorry, # of colors >256, not supported yet! \n");
exit(-1);
}
write_int_1D_pgx(argv[2],buff_idx,rows,cols, bitdepth);
/* Output table in raw format */
if((out=fopen(argv[3],"w"))==NULL)
{
fprintf(stderr,"Cannot open output file \n");
exit(-1);
}
fprintf(out,"%d %d\n",entry,comp);
length=entry*comp;
if(fwrite(buff_tbl,sizeof(char),length,out)!=length)
{
fprintf(stderr,"Error on write \n");
exit(-1);
}
fclose(out);
free(buff_tbl);
free_int1(buff_rgb);
free_int1(buff_idx);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -