8
A
回答
3
PPM文件由9个部分构成,由空格分隔。
- 打开文件
- 读,直到第一个空格,并检查你有
P6
。然后跳过其他空格。 - 直到下一个空白为止,将缓冲区转换为整数宽度。然后跳过其他空格
- 读取直到下一个空格,将缓冲区转换为整数高度。然后跳到其它的空格
- 分配整数的高度*宽度
- 的尺寸的二维阵列读取由线最大-VAL
- 读取线和填充所述阵列
+2
我认为你需要澄清这个答案是怎么回事,这不是很清楚。 – 2010-04-22 19:52:56
+1
许多PNM文件会失败,因为它没有对注释行进行说明。 – deegee 2013-09-21 23:41:30
18
下面的代码显示如何读取,更改像素颜色并以 PPM格式写入图像。我希望它有帮助。
#include<stdio.h>
#include<stdlib.h>
typedef struct {
unsigned char red,green,blue;
} PPMPixel;
typedef struct {
int x, y;
PPMPixel *data;
} PPMImage;
#define CREATOR "RPFELGUEIRAS"
#define RGB_COMPONENT_COLOR 255
static PPMImage *readPPM(const char *filename)
{
char buff[16];
PPMImage *img;
FILE *fp;
int c, rgb_comp_color;
//open PPM file for reading
fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Unable to open file '%s'\n", filename);
exit(1);
}
//read image format
if (!fgets(buff, sizeof(buff), fp)) {
perror(filename);
exit(1);
}
//check the image format
if (buff[0] != 'P' || buff[1] != '6') {
fprintf(stderr, "Invalid image format (must be 'P6')\n");
exit(1);
}
//alloc memory form image
img = (PPMImage *)malloc(sizeof(PPMImage));
if (!img) {
fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
//check for comments
c = getc(fp);
while (c == '#') {
while (getc(fp) != '\n') ;
c = getc(fp);
}
ungetc(c, fp);
//read image size information
if (fscanf(fp, "%d %d", &img->x, &img->y) != 2) {
fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
exit(1);
}
//read rgb component
if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
exit(1);
}
//check rgb component depth
if (rgb_comp_color!= RGB_COMPONENT_COLOR) {
fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
exit(1);
}
while (fgetc(fp) != '\n') ;
//memory allocation for pixel data
img->data = (PPMPixel*)malloc(img->x * img->y * sizeof(PPMPixel));
if (!img) {
fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
//read pixel data from file
if (fread(img->data, 3 * img->x, img->y, fp) != img->y) {
fprintf(stderr, "Error loading image '%s'\n", filename);
exit(1);
}
fclose(fp);
return img;
}
void writePPM(const char *filename, PPMImage *img)
{
FILE *fp;
//open file for output
fp = fopen(filename, "wb");
if (!fp) {
fprintf(stderr, "Unable to open file '%s'\n", filename);
exit(1);
}
//write the header file
//image format
fprintf(fp, "P6\n");
//comments
fprintf(fp, "# Created by %s\n",CREATOR);
//image size
fprintf(fp, "%d %d\n",img->x,img->y);
// rgb component depth
fprintf(fp, "%d\n",RGB_COMPONENT_COLOR);
// pixel data
fwrite(img->data, 3 * img->x, img->y, fp);
fclose(fp);
}
void changeColorPPM(PPMImage *img)
{
int i;
if(img){
for(i=0;i<img->x*img->y;i++){
img->data[i].red=RGB_COMPONENT_COLOR-img->data[i].red;
img->data[i].green=RGB_COMPONENT_COLOR-img->data[i].green;
img->data[i].blue=RGB_COMPONENT_COLOR-img->data[i].blue;
}
}
}
int main(){
PPMImage *image;
image = readPPM("can_bottom.ppm");
changeColorPPM(image);
writePPM("can_bottom2.ppm",image);
printf("Press any key...");
getchar();
}
相关问题
- 1. 从文本文件中读取内容,并将其存储到一个数组
- 2. Java读取文本文件并将其存储为数组
- 3. Java:读取文件并将其数据存储在ArrayList中?
- 4. 读取文件并存储到数组
- 5. 如何读取文件行并将其存储? C++
- 6. 如何读取文件夹中的多个texfile并将每个文本文件存储在数组中C#
- 7. C程序 - 如何读取文件并将其文本存储在变量中?
- 8. C++从文件中读取文本并将其存储在独立的变量
- 9. 读取文件,并分割其内容,并在一个数组
- 10. Java读取文本文件2列并存储在数组中
- 11. 从文本文件中读取数据并将其存储在对象中
- 12. 从数据库中读取一行数据并将其存储到数组中
- 13. Java - 读取并存储在数组中
- 14. 从文件中读取并存储在数组中
- 15. 如何读取文本文件并将数据存储在数组中
- 16. 读取文件并保存在一个数组中
- 17. C编程:读取文件并存储在struct
- 18. 读取txt文件内容并存储在数组中
- 19. 如何读取C文件中的内容并使用fgets()将其存储到数组中?
- 20. 接收用户输入并将其存储在一个数组中C
- 21. 读取外部文本文件并存储到数组中
- 22. 如何读取整数并将其存储在java中的数组中
- 23. 读取txt文件中的数字列表并将其存储到数组中C
- 24. 从文件读取数据并将每行存储在数组中?
- 25. Android从sdcsrd读取文本并将其存储在ArrayList中
- 26. 如何从键盘读取数据并将其存储在文件中,shellscript
- 27. 如何读取一个txt文件并存储在NSArray中?
- 28. 以字节读取文件并将其存储到一个确定性的8位值数组中
- 29. 文本更改值,并将其存储字符数组用C
- 30. 读取一个字符串并将其存入(int)中C
你有什么麻烦?如果你发布了一个不适合的尝试,找到这个bug很容易。 – caf 2010-04-23 00:48:09