BMP 파일의 픽셀 값을 2D 동적 할당 구조체 배열에 저장하려고하지만 세그먼트 오류가 계속 발생합니다. 다음은 지금까지 내가 가지고있는 것입니다 :BMP 파일의 픽셀 값 저장
#include <stdio.h>
#include <stdlib.h>
typedef struct PIXEL{
unsigned char Red, Green, Blue;
}*pixel;
int main (int argc, char *argv[])
{
//variable declarations and open the file
FILE* fin = fopen(argv[1], "rb");
if (fin == NULL){
printf("Error opening file.\n");
exit(0);
}
unsigned char info[54];
int width, height, i, j;
fread(info, sizeof(unsigned char), 54, fin); //read the header
width = *(int*)&info[18];
height = *(int*)&info[22];
pixel **image = (pixel **) malloc(sizeof(pixel *) * width); //reserve enough space for RGB for each pixel
for (i = 0; i < width; i++){
image[i] = (pixel *) malloc(sizeof(pixel) * height);
}
for (i = 0; i < width; i++){
for (j = 0; j < height; j++){
image[i][j]->Blue = getc(fin); //put the blue value of the pixel
image[i][j]->Green = getc(fin); //green value
image[i][j]->Red = getc(fin); //red value
printf("Pixel %d: [%d, %d, %d]\n", (i+1)*(j+1), image[i][j]->Blue, image[i][j]->Green, image[i][j]->Blue);
}
}
fclose(fin);
return 0;
}
printf 줄에는 image [i] [j] -> blue 대신에 image [i] [j] -> blue가 있어야합니다. – user3509534
어떤 줄로 오류가 발생합니까? Visual Studio와 같은 멋진 간단한 디버거를 사용하여 코드를 단계별로 실행하십시오. 또한 매우 작은 10 x 10 픽셀 비트 맵을 사용하여 코드를 실행 해보십시오. – UncleKing
아, bmp 파일을 열 때 오류가 발생했습니다. – user3509534