📄 io.c
字号:
/*****************************************************************************/
/* Copyright 1999, Sharp Labs. of America. */
/* All rights reserved */
/* File: "io.c" */
/* Description: i/o routines for color reindexing */
/* Author: Wenjun Zeng */
/* Affiliation: Sharp Labs. of America., zengw@sharplabs.com */
/* Version: VM6.0 */
/* Last Revised: 4 Jan, 2000 */
/*****************************************************************************/
#include <stdio.h>
int pbm_getc (FILE * infile)
/* Read next char, skipping over any comments */
/* A comment/newline sequence is returned as a newline */
{
register int ch;
ch = getc(infile);
if (ch == '#') {
do {
ch = getc(infile);
} while (ch != '\n' && ch != EOF);
}
return ch;
}
unsigned int read_pbm_integer (FILE * infile)
/* Read an unsigned decimal integer from the PBM file */
/* Swallows one trailing character after the integer */
/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
/* This should not be a problem in practice. */
{
register int ch;
register unsigned int val;
/* Skip any leading whitespace */
do {
ch = pbm_getc(infile);
if (ch == EOF)
{
printf("no integer available\n");
exit(-1);
}
} while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
if (ch < '0' || ch > '9')
{
printf("not numerical value\n");
exit(-1);
}
val = ch - '0';
while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
val *= 10;
val += ch - '0';
}
return val;
}
/* OPEN pgm file and read header */
FILE *read_pgm_header(filename,prows,pcols)
char *filename;
unsigned int *prows,*pcols;
{
int idum,c;
FILE *in;
/* The "r" is for "read" */
if((in=fopen(filename,"r"))==NULL)
{
printf("Cannot open input file %s\n",filename);
exit(-1);
}
/* This line distinguishes pgm format from raw
Raw contains only pixel values
*/
if (getc(in) != 'P')
{
printf("%s is not a pgm/ppm format file\n",filename);
exit(-1);
}
c = getc(in); /* save format discriminator for a sec */
/* fetch the remaining header info */
*pcols = read_pbm_integer(in);
*prows = read_pbm_integer(in);
idum = read_pbm_integer(in);
return(in);
}
/* OPEN pgx file and read header */
FILE *read_pgx_header(filename,prows,pcols)
char *filename;
unsigned int *prows,*pcols;
{
int c;
FILE *in;
if((in=fopen(filename,"r"))==NULL)
{
printf("Cannot open input file %s\n",filename);
exit(-1);
}
/* This line distinguishes pgx format from raw
Raw contains only pixel values
*/
if (getc(in) != 'P')
{
printf("%s is not a pgm/ppm/pgx format file\n",filename);
exit(-1);
}
c = getc(in); /* save format discriminator for a sec */
/* Skip any leading whitespace */
do {
c = pbm_getc(in);
if (c == EOF)
{
printf("no integer available\n");
exit(-1);
}
} while (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c < '0' || c > '9');
/* fetch the remaining header info */
*pcols = read_pbm_integer(in);
*prows = read_pbm_integer(in);
return(in);
}
/* OPEN tbl file and read header */
FILE *read_tbl_header(filename,entries,comp)
char *filename;
unsigned int *entries,*comp;
{
FILE *in;
if((in=fopen(filename,"r"))==NULL)
{
printf("Cannot open input file %s\n",filename);
exit(-1);
}
/* fetch the remaining header info */
*entries = read_pbm_integer(in);
*comp = read_pbm_integer(in);
return(in);
}
/* READ 8 bits data */
void read_int_1D_raw(pt,picture,length)
int length;
FILE *pt;
int *picture;
{
int i;
unsigned char *buff;
if((buff=(unsigned char*)malloc(length*sizeof(char)))==NULL)
{
printf("error on allocation \n");
exit(1);
}
if(fread(buff,sizeof(char),length,pt)!=length)
{
printf("Error reading image from file\n");
exit(-1);
}
/* Close the file */
fclose(pt);
free(buff);
for(i=0;i<length;i++)
picture[i]=(int)buff[i];
}
/* WRITE 8-bit image in pgm format */
void write_int_1D_pgm(filename,picture,rows,cols)
int *picture;
char *filename;
int rows,cols;
{
FILE *out;
int i, length;
unsigned char *buff;
double var;
if((out=fopen(filename,"w"))==NULL)
{
printf("Cannot open output file\n");
exit(-1);
}
length=rows*cols;
if((buff=(unsigned char*)malloc(length*sizeof(char)))==NULL){
printf("error on allocation \n");
exit(1);
}
for(i=0;i<length;i++)
{
var=picture[i];
if(var>=255.0) var=255.0;
if(var<=0.0 ) var=0.0;
buff[i]=(unsigned char)var;
if(var-buff[i]>0.5) buff[i]= buff[i]+1;
}
/* This line distinguishes pgm format from raw */
fprintf(out,"P5\n%d %d\n255\n",cols,rows);
if(fwrite(buff,sizeof(char),length,out)!=length)
{
printf("Error on write\n");
exit(-1);
}
fclose(out);
free(buff);
}
/* WRITE 8-bit image in pgx format */
/* (i.e., bitdepth of pgx file <=8) */
void write_int_1D_pgx(filename,picture,rows,cols, bitdepth)
int *picture;
char *filename;
int rows,cols;
int bitdepth;
{
FILE *out;
int i, length;
unsigned char *buff;
double var;
if((out=fopen(filename,"w"))==NULL)
{
printf("Cannot open output file\n");
exit(-1);
}
length=rows*cols;
if((buff=(unsigned char*)malloc(length*sizeof(char)))==NULL){
printf("error on allocation \n");
exit(1);
}
for(i=0;i<length;i++)
{
var=picture[i];
if(var>=255.0) var=255.0;
if(var<=0.0 ) var=0.0;
buff[i]=(unsigned char)var;
if(var-buff[i]>0.5) buff[i]= buff[i]+1;
}
/* This line distinguishes pgx format from raw */
fprintf(out,"PG LM +%d %d %d\n",bitdepth, cols,rows);
if(fwrite(buff,sizeof(char),length,out)!=length)
{
printf("Error on write\n");
exit(-1);
}
fclose(out);
free(buff);
}
/* WRITE 24-bit image in ppm format */
void write_int_1D_ppm(filename,picture,rows,cols)
int *picture;
char *filename;
int rows,cols;
{
FILE *out;
int i, length;
unsigned char *buff;
double var;
if((out=fopen(filename,"w"))==NULL)
{
printf("Cannot open output file\n");
exit(-1);
}
length=rows*cols*3;
if((buff=(unsigned char*)malloc(length*sizeof(char)))==NULL){
printf("error on allocation \n");
exit(1);
}
for(i=0;i<length;i++)
{
var=picture[i];
if(var>=255.0) var=255.0;
if(var<=0.0 ) var=0.0;
buff[i]=(unsigned char)var;
if(var-buff[i]>0.5) buff[i]= buff[i]+1;
}
/* This line distinguishes ppm format from raw */
fprintf(out,"P6\n%d %d\n255\n",cols,rows);
if(fwrite(buff,sizeof(char),length,out)!=length)
{
printf("Error on write\n");
exit(-1);
}
fclose(out);
free(buff);
}
int *alloc_int1(length)
int length;
{
int *ptr;
if((ptr = (int *)calloc(length,sizeof(int)))==NULL)
{
printf("Cannot allocate memory for int array.");
exit(-2);
}
return(ptr);
}
/* RELEASE memory */
void free_int1(ptr)
int *ptr;
{
free(ptr);
}
int **alloc_int2(rows,cols)
int rows,cols;
{
int i;
int **ptr;
if((ptr = (int **)calloc(rows,sizeof(int *)))==NULL)
{
printf("Cannot allocate memory for pointers.");
exit(-2);
}
if((ptr[0] = (int *)calloc(rows*cols,sizeof(int)))==NULL)
{
printf("Cannot allocate memory for int array.");
exit(-2);
}
for(i = 1; i < rows; i++)
ptr[i] = &(ptr[0][i*cols]);
return(ptr);
}
void free_int2(ptr)
int **ptr;
{
free(*ptr);
free(ptr);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -