资源简介
该程序用于pgm格式的图像读取与保存,C++编写,可直接运行,希望能够帮助到有需要的人。PGM格式的图像由头信息和数据信息构成。
代码片段和文件信息
#include < stdio.h >
#include < stdlib.h >
#include “pgm.h“
/**********************File I/O functions*******************************/
/***********************************************************************/
/*Gets an ascii pgm image file store as a color pgm.*/
void getPGMfile (char filename[] PGMImage *img)
{
FILE *in_file;
char ch;
int row col type;
int ch_int;
in_file = fopen(filename “r“);
if (in_file == NULL)
{
fprintf(stderr “Error: Unable to open file %s\n\n“ filename);
exit(8);
}
printf(“\nReading image file: %s\n“ filename);
/*determine pgm image type (only type three can be used)*/
ch = getc(in_file);
if(ch != ‘P‘)
{
printf(“ERROR(1): Not valid pgm/ppm file type\n“);
exit(1);
}
ch = getc(in_file);
/*convert the one digit integer currently represented as a character to
an integer(48 == ‘0‘)*/
type = ch - 48;
if((type != 2) && (type != 3) && (type != 5) && (type != 6))
{
printf(“ERROR(2): Not valid pgm/ppm file type\n“);
exit(1);
}
while(getc(in_file) != ‘\n‘); /* skip to end of line*/
while (getc(in_file) == ‘#‘) /* skip comment lines */
{
while (getc(in_file) != ‘\n‘); /* skip to end of comment line */
}
/*there seems to be a difference between color and b/w. This line is needed
by b/w but doesn‘t effect color reading...*/
fseek(in_file -1 SEEK_CUR); /* backup one character*/
fscanf(in_file“%d“ &((*img).width));
fscanf(in_file“%d“ &((*img).height));
fscanf(in_file“%d“ &((*img).maxVal));
printf(“\n width = %d“(*img).width);
printf(“\n height = %d“(*img).height);
printf(“\n maxVal = %d“(*img).maxVal);
printf(“\n“);
if (((*img).width > MAX) || ((*img).height > MAX))
{
printf(“\n\n***ERROR - image too big for current image structure***\n\n“);
exit(1);
}
if(type == 2) /*uncompressed ascii file (B/W)*/
{
for (row=(*img).height-1; row >=0; row--)
for (col=0; col< (*img).width; col++)
{
fscanf(in_file“%d“ &ch_int);
(*img).data[row][col].red = ch_int;
(*img).data[row][col].green = ch_int;
(*img).data[row][col].blue = ch_int;
}
}
else if(type == 3) /*uncompressed ascii file (color)*/
{
for (row=(*img).height-1; row >=0; row--)
for (col=0; col< (*img).width; col++)
{
fscanf(in_file“%d“ &ch_int);
((*img).data[row][col].red) = (unsigned char)ch_int;
fscanf(in_file“%d“ &ch_int);
((*img).data[row][col].green) = (unsigned char)ch_int;
fscanf(in_file“%d“ &ch_int);
((*img).data[row][col].blue) = (unsigned char)ch_int;
}
}
else if(type == 5) /*compressed file (B/W)*/
/*note: this type remains untested at this time...*/
{
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-10-10 14:56 pgm_read_save_c\
文件 5884 2018-10-10 14:55 pgm_read_save_c\pgm.c
文件 766 2018-10-10 14:55 pgm_read_save_c\pgm.h
相关资源
- C流程图生成器,用C语言代码 生成C语
- C++纯文字DOS超小RPG游戏
- 将图片转换为C语言数组的程序
- 电子时钟 有C语言程序,PROTEUS仿真图
- 用C语言进行数字图像处理
- Wi-Fi IoT智能家居套件-Hi3861(原理图
- Qt画图工具源码(qgraphics draw)
- Qt 实现心电图
- Qt实现 屏幕截图
- 使用QWT库实现接收串口数据,并根据
- 使用wxWidgets进行跨平台程序开发
- QT视频播放器(基于FFmpeg)
- ADS1263驱动程序+电路图
- 基于esp32的摄像头采集图像代码
- stm32f103c8t6开发板原理图+pcb
- 基于arduino的红绿灯程序,包附原理图
- 数据结构 图的遍历源代码
- qt处理图形
- H.264标准详细图解
- CreatBitmap图片合成源码
- 易语言png 多行多列切图
- basler相机图像采集和显示
- 交互式计算机图形学 第六版 OpenGL源代
- c++ 画图(14Qt-XPS)
- 图形学简单绘图系统
- mfc绘图大全(画直线、矩形、椭圆)
- C++调用百度地图案例
- opencv图片扫描以及校正
- MFC文档_视图_框架_模板结构体系深入
- 图像预处理五种滤波
川公网安备 51152502000135号
评论
共有 条评论