read_write.h
来自「基于块方向的图像无损压缩代码」· C头文件 代码 · 共 135 行
H
135 行
/*++*
* Copyright (c) 1997 University of British Columbia. All rights reserved.
*
* File: read_write.h
* Pourpose: contains the I/O functions.
*
* Author: Ismaeil R. Ismaeil, Aug. 1997
*
*--*/
/*************************************************
2dmtx.c : Make matrix of given size nr x nc.
nr & nc : number of rows and cols
size : sizeof(element_size)
**************************************************/
char **dim2(int nr,int nc,unsigned size)
{
int i;
char **prow, *pdata;
pdata = (char *) calloc(nr * nc,size);
if (pdata == (char *) NULL)
{
fprintf(stderr,"No heap space for data\n");
exit(1);
}
prow = (char **) malloc(nr * sizeof(char *));
if (prow == (char **) NULL)
{
fprintf(stderr, "No heap space for row pointers\n");
exit(1);
}
for (i = 0; i < nr ; i++)
{
prow[i] = pdata; /* store pointers to rows */
pdata += size * nc; /* move to next row */
}
return prow; /* pointer to 2D array */
}
/*********************************************
free2.c: Free all elements of the matrix.
**********************************************/
void free2c(char **pa)
{
free(*pa); /* free the data */
free(pa); /* free pointer to row pointers */
}
void free2i(int **pa)
{
free(*pa); /* free the data */
free(pa); /* free pointer to row pointers */
}
void free2f(float **pa)
{
free(*pa);
free(pa);
}
void free2d(double **pa)
{
free(*pa); /* free the data */
free(pa); /* free pointer to row pointers */
}
void GetData(short int **ImageData,int nl,int nc,int **frame)
{
int i,j;
for (i = 1; i <= nl; i++)
for (j = 1; j <= nc; j++)
frame[i][j] = ImageData[i-1][j-1];
}
/*************************************************************
write_pic.c : This routine save an image in the disk.
nl & nc : number of rows and cols
frame : saved matrix (in disk)
fp : pointer to a file (image out)
*************************************************************/
void write_pic(FILE *fp,int nl,int nc,int **frame)
{
unsigned char *c;
int i,j;
c = (unsigned char *) calloc(nc,sizeof(unsigned char));
for (i = 1 ; i <= nl ; i++)
{
for (j = 1; j <= nc; j++)
{
if(frame[i][j] < 0)
c[j-1] = 0;
else if(frame[i][j] > 255)
c[j-1] = 255;
else
c[j-1] = frame[i][j];
}
fwrite(c,nc * sizeof(unsigned char),1,fp);
}
free(c);
}
/************************************************************
write_pic.c : This routine save an image in the disk.
nl & nc : number of rows and cols
frame : saved matrix (in disk)
fp : pointer to a file (image out)
************************************************************/
void write_int_pic(FILE *fp,int nl,int nc,int **frame)
{
unsigned short *c;
int i,j;
c = (unsigned short *) calloc(nc,sizeof(unsigned short));
for (i = 1 ; i <= nl ; i++)
{
for (j = 1; j <= nc; j++)
{
c[j-1] = frame[i][j];
}
fwrite(c,sizeof(unsigned short),nc,fp);
}
free(c);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?