我试图通过终端运行此程序,此错误出现了。 "分段错误:11"
我想知道原因。这个程序的作用是,它读取.ppm文件并将其信息保存在Pixel类型的矩阵变量中,因此,PPM文件基本上由以下组成:第一行将是" P3"默认情况下,第二行是矩阵的大小,第三行是Pixel属性可能的最高值,其他行将有3个最大值为255的整数,因此对于矩阵的每个成员都会有一个像素R ,G,B。 我在save_image函数中尝试做的事情,首先要识别我们是否正在处理ppm文件(检查第一行是否有P3),然后读取矩阵的行数和列数,然后创建一个新的使用malloc函数的矩阵,然后它将文件行中的数据保存到变量myImg的.r和.g和.b中。 我对调试/编程很陌生,所以如果这些信息不充分,我很抱歉,但我尽了最大努力。
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int r;
int g;
int b;
}Pixel;
void save_image(FILE* img, Pixel ** newImg) {
int i;
int j;
int fcount;
int scount;
int count;
int dcc;
char init[3];
fscanf(img,"%s",init);
if(init[0]=='P' && init[1]=='3'){
printf("worked!\n");
fscanf(img,"%d %d",&j,&i);
fscanf(img, "%d",&dcc);
*newImg = (Pixel*)malloc(sizeof(Pixel) * i);
for ( count = 0; count < i ; ++count)
{
newImg[count] = (Pixel*)malloc(sizeof(Pixel) * j);
}
for (fcount = 0; fcount <= i ; ++fcount)
{
for (scount = 0; scount <= j; ++scount)
{
fscanf(img,"%i %i %i",&newImg[i][j].r,&newImg[i][j].g,&newImg[i][j].b);
}
}
}
else
printf("Type of file not recognized\n");
fclose(img);
}
int main(int argc, char const *argv[])
{
FILE* image;
Pixel myImg;
Pixel** newImg;
**newImg = myImg;
image = fopen(argv[1],"r");
save_image(image,newImg);
return 0;
}
答案 0 :(得分:1)
程序失败,因为newImg []的初始malloc是malloc'的Pixel大小的倍数,而不是指向Pixel的指针的大小,以及将指针传递给newImg作为save_image参数的问题()函数。请参阅我关于应该定义变量newImg的位置的注释以及对save_image()函数声明的理想修改
鉴于已发布的代码已写入,它似乎期待'普通'.ppm文件格式
并且发布的代码无法允许文件中的任何嵌入式注释
给出.ppm文件格式的描述:
格式定义如下。您可以使用libnetpbm C子例程库方便准确地读取和解释格式。
PPM文件由一个或多个PPM图像序列组成。图像之前,之后或之间没有数据,分隔符或填充。
每个PPM图像由以下内容组成:
A "magic number" for identifying the file type. A ppm image's magic number is the two characters "P6".
Whitespace (blanks, TABs, CRs, LFs).
A width, formatted as ASCII characters in decimal.
Whitespace.
A height, again in ASCII decimal.
Whitespace.
The maximum color value (Maxval), again in ASCII decimal. Must be less than 65536 and more than zero.
A single whitespace character (usually a newline).
A raster of Height rows, in order from top to bottom. Each row consists of Width pixels, in order from left to right. Each pixel is a triplet of red, green, and blue samples, in that order. Each sample is represented in pure binary by either 1 or 2 bytes. If the Maxval is less than 256, it is 1 byte. Otherwise, it is 2 bytes. The most significant byte is first.
A row of an image is horizontal. A column is vertical. The pixels in the image are square and contiguous.
In the raster, the sample values are "nonlinear." They are proportional to the intensity of the ITU-R Recommendation BT.709 red, green, and blue in the pixel, adjusted by the BT.709 gamma transfer function. (That transfer function specifies a gamma number of 2.2 and has a linear section for small intensities). A value of Maxval for all three samples represents CIE D65 white and the most intense color in the color universe of which the image is part (the color universe is all the colors in all images to which this image might be compared).
ITU-R Recommendation BT.709 is a renaming of the former CCIR Recommendation 709. When CCIR was absorbed into its parent organization, the ITU, ca. 2000, the standard was renamed. This document once referred to the standard as CIE Rec. 709, but it isn't clear now that CIE ever sponsored such a standard.
Note that another popular color space is the newer sRGB. A common variation on PPM is to substitute this color space for the one specified.
Note that a common variation on the PPM format is to have the sample values be "linear," i.e. as specified above except without the gamma adjustment. pnmgamma takes such a PPM variant as input and produces a true PPM as output.
以“#”开头的字符串可能是注释,与PBM相同。
请注意,您可以使用pamdepth在每个样本1个字节的格式和每个样本2个字节的格式之间进行转换。
此处提及的所有字符均以ASCII编码。 “换行符”是指ASCII中称为换行符或LF的字符。 “空格”字符是空格,CR,LF,TAB,VT或FF(即ANSI标准C isspace()函数称为空格)。 普通PPM
实际上还有另一种版本的PPM格式非常罕见:“普通”PPM格式。上述格式通常被认为是正常格式,称为“原始”PPM格式。有关普通格式和原始格式如何相互关联以及如何使用它们的一些评论,请参阅pbm。
普通格式的区别在于:
There is exactly one image in a file.
The magic number is P3 instead of P6.
Each sample in the raster is represented as an ASCII decimal number (of arbitrary size).
Each sample in the raster has white space before and after it. There must be at least one character of white space between any two samples, but there is no maximum. There is no particular separation of one pixel from another -- just the required separation between the blue sample of one pixel from the red sample of the next pixel.
No line should be longer than 70 characters.
以下是此格式的小图片示例。
P3
# feep.ppm
4 4
15
0 0 0 0 0 0 0 0 0 15 0 15
0 0 0 0 15 7 0 0 0 0 0 0
0 0 0 0 0 0 0 15 7 0 0 0
15 0 15 0 0 0 0 0 0 0 0 0
每行的末尾都有换行符。
读取此格式的程序应尽可能宽松,接受任何看起来像PPM图像的东西。