C 由于文件大小和存储值而导致打印错误的解决方案是否正确?
第1部分:如果文件大小超过500 x 500个测量值(在顶部定义为最大宽度和高度),我需要打印一个错误。我C 由于文件大小和存储值而导致打印错误的解决方案是否正确?,c,arrays,printing,pixel,C,Arrays,Printing,Pixel,第1部分:如果文件大小超过500 x 500个测量值(在顶部定义为最大宽度和高度),我需要打印一个错误。我 // Make struct rgb match your data, details not supplied in the question struct rgb { uint8_t red; uint8_t green; uint8_t blue; } // Get width & height info as before uint32_t bu
// Make struct rgb match your data, details not supplied in the question
struct rgb {
uint8_t red;
uint8_t green;
uint8_t blue;
}
// Get width & height info as before
uint32_t buffer_size;
void* buffer;
load_file('filename', buffer, &buffer_size);
// Should verify that buffer_size == width * height * 3
struct rgb (*image_data)[width] = (struct rgb(*)[width])buffer;
// Note the above variable length array is a C99 feature
// Pre-C99 the same trick is a touch more ick
// Data can now be accessed as image_data[x][y].red; etc.
第2部分:另一部分是我必须从输入文件中读取像素信息并将其存储到2d数组中。每个像素有3个红色、绿色和蓝色的值,但我不确定这是否重要
// Make struct rgb match your data, details not supplied in the question
struct rgb {
uint8_t red;
uint8_t green;
uint8_t blue;
}
// Get width & height info as before
uint32_t buffer_size;
void* buffer;
load_file('filename', buffer, &buffer_size);
// Should verify that buffer_size == width * height * 3
struct rgb (*image_data)[width] = (struct rgb(*)[width])buffer;
// Note the above variable length array is a C99 feature
// Pre-C99 the same trick is a touch more ick
// Data can now be accessed as image_data[x][y].red; etc.
我的解决方案尝试:
// Make struct rgb match your data, details not supplied in the question
struct rgb {
uint8_t red;
uint8_t green;
uint8_t blue;
}
// Get width & height info as before
uint32_t buffer_size;
void* buffer;
load_file('filename', buffer, &buffer_size);
// Should verify that buffer_size == width * height * 3
struct rgb (*image_data)[width] = (struct rgb(*)[width])buffer;
// Note the above variable length array is a C99 feature
// Pre-C99 the same trick is a touch more ick
// Data can now be accessed as image_data[x][y].red; etc.
第1部分:
void check_file_size //I'm not sure what to put as arguments since width/height are global
{
if (width > 500 && height > 500)
{
perror("Error: File size too big.\n");
}
}
// Make struct rgb match your data, details not supplied in the question
struct rgb {
uint8_t red;
uint8_t green;
uint8_t blue;
}
// Get width & height info as before
uint32_t buffer_size;
void* buffer;
load_file('filename', buffer, &buffer_size);
// Should verify that buffer_size == width * height * 3
struct rgb (*image_data)[width] = (struct rgb(*)[width])buffer;
// Note the above variable length array is a C99 feature
// Pre-C99 the same trick is a touch more ick
// Data can now be accessed as image_data[x][y].red; etc.
第2部分:
#define max_width 500
#define max_height 500
int width, height
void read_header(FILE *new)
{
int max_color;
char P[10];
fgets(P, 10, new);
fscanf(new, "%d %d", &width, &height);
fscanf(new, "%d", &max_color);
}
void store_into_array(FILE *input)
{
int array[max_width][max_height];
for (x = 0; x < width; x++)
{
for (y = height; y >=0; y--)
{
fscanf(input, "%d", &array[x][y]);
}
}
}
// Make struct rgb match your data, details not supplied in the question
struct rgb {
uint8_t red;
uint8_t green;
uint8_t blue;
}
// Get width & height info as before
uint32_t buffer_size;
void* buffer;
load_file('filename', buffer, &buffer_size);
// Should verify that buffer_size == width * height * 3
struct rgb (*image_data)[width] = (struct rgb(*)[width])buffer;
// Note the above variable length array is a C99 feature
// Pre-C99 the same trick is a touch more ick
// Data can now be accessed as image_data[x][y].red; etc.
#定义最大宽度500
#定义最大高度500
整数宽度、高度
无效读取头(文件*新建)
{
int max_颜色;
charp[10];
fgets(第10页,新);
fscanf(新的、%d%d、&宽度和高度);
fscanf(新、%d、&max_颜色);
}
无效存储到数组中(文件*输入)
{
int数组[最大宽度][最大高度];
对于(x=0;x=0;y--)
{
fscanf(输入,“%d”和数组[x][y]);
}
}
}
第1部分
// Make struct rgb match your data, details not supplied in the question
struct rgb {
uint8_t red;
uint8_t green;
uint8_t blue;
}
// Get width & height info as before
uint32_t buffer_size;
void* buffer;
load_file('filename', buffer, &buffer_size);
// Should verify that buffer_size == width * height * 3
struct rgb (*image_data)[width] = (struct rgb(*)[width])buffer;
// Note the above variable length array is a C99 feature
// Pre-C99 the same trick is a touch more ick
// Data can now be accessed as image_data[x][y].red; etc.
第二部分
你可以像现在这样在数组中循环,但实际上作弊要好得多。
数组的C数组或直数组是同一件事,只是语法上略有不同
// Make struct rgb match your data, details not supplied in the question
struct rgb {
uint8_t red;
uint8_t green;
uint8_t blue;
}
// Get width & height info as before
uint32_t buffer_size;
void* buffer;
load_file('filename', buffer, &buffer_size);
// Should verify that buffer_size == width * height * 3
struct rgb (*image_data)[width] = (struct rgb(*)[width])buffer;
// Note the above variable length array is a C99 feature
// Pre-C99 the same trick is a touch more ick
// Data can now be accessed as image_data[x][y].red; etc.
// Make struct rgb match your data, details not supplied in the question
struct rgb {
uint8_t red;
uint8_t green;
uint8_t blue;
}
// Get width & height info as before
uint32_t buffer_size;
void* buffer;
load_file('filename', buffer, &buffer_size);
// Should verify that buffer_size == width * height * 3
struct rgb (*image_data)[width] = (struct rgb(*)[width])buffer;
// Note the above variable length array is a C99 feature
// Pre-C99 the same trick is a touch more ick
// Data can now be accessed as image_data[x][y].red; etc.
很抱歉使用了stdint.h变量,这是我无法(也不想)打破的习惯。你能给我一个像素数组吗。如果每种颜色的像素为1字节,则可以将其存储为十六进制值。例如rgb 0xrrggbb红色0xff0000黑色0x000000,白色0xffffff等等!对不起,我忘了在上面的信息中包括这个,嗯,说明书上没有说。如果没有特别说明,那是可能的!如果每种颜色的像素为2字节,则可以将其存储为十六进制值。例如rgb 0xrrggbb红色0xff0000黑色0x000000,白色0xffffff etcUmm,我想我不知道像素有多少字节。我还没有学会十六进制值。但输入文件中像素的最大值为255(对于每个颜色分量;红、绿、蓝)。并且这些值总是在输入文件中的新行上(ppm图像的标题之后)。
void check\u file\u size(void)
更好。为了获得更多乐趣,您可以使用内存映射文件(mmap)使用此模式进行随机像素访问,而无需读取磁盘上的全部数据。但这开始变得依赖操作系统。
// Make struct rgb match your data, details not supplied in the question
struct rgb {
uint8_t red;
uint8_t green;
uint8_t blue;
}
// Get width & height info as before
uint32_t buffer_size;
void* buffer;
load_file('filename', buffer, &buffer_size);
// Should verify that buffer_size == width * height * 3
struct rgb (*image_data)[width] = (struct rgb(*)[width])buffer;
// Note the above variable length array is a C99 feature
// Pre-C99 the same trick is a touch more ick
// Data can now be accessed as image_data[x][y].red; etc.