gpt4 book ai didi

c - 基本 C 矩阵分段内存分配

转载 作者:行者123 更新时间:2023-11-30 15:16:17 26 4
gpt4 key购买 nike

我尝试通过终端运行该程序,但出现了此错误。“段错误:11”

我想知道为什么。该程序的作用是,它读取 .ppm 文件并将其信息保存在 Pixel 类型的矩阵变量中,因此,PPM 文件基本上由以下部分组成:第一行默认为“P3”,第二行矩阵的大小,第三行是像素属性可能的最大值,其他行将有 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;
}

最佳答案

程序失败,因为 newImg[] 的初始 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 图像的东西。

关于c - 基本 C 矩阵分段内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33160808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com