Now I'm trying to implement the binary data read and manipulate by using ANSI C. I've define the struct type as the follow.
typedef struct {
int width;
int height;
int max;
unsigned char **dummy;
unsigned char **pixels;
} PPMImage;
then in the main function, I can read the binary data.
int main(int argc, char **argv)
{
FILE *fpIn, *fpOut;
PPMImage img;
if (fnReadPPM(argv[1], &img) != TRUE){
return -1;
} until here success
but when I run this code, I've got a access violation. I don't know the reason, How can I access the structure type in call by reference?
if (fnProc_grayPPM(&img) != TRUE){
return -1;
}
...
}
int fnReadPPM(char* fileNm, PPMImage* img)
{
FILE* fp;
if (fileNm == NULL){
return FALSE;
}
fp = fopen(fileNm, "rb"); // binary mode
if (fp == NULL){
return FALSE;
}
img->pixels = (unsigned char**)calloc(img->height, sizeof(unsigned char*));
img->luminance = (unsigned char**)calloc(img->height, sizeof(unsigned char*));
for (int i = 0; i<img->height; i++){
img->pixels[i] = (unsigned char*)calloc(img->width * 3, sizeof(unsigned char));
img->luminance[i] = (unsigned char*)calloc(img->width , sizeof(unsigned char));
}
for (int i = 0; i<img->height; i++){
for (int j = 0; j<img->width * 3; j++){
fread(&img->pixels[i][j], sizeof(unsigned char), 1, fp);
}
}
fclose(fp);
return TRUE;
}
int fnProc_grayPPM(PPMImage* img)
{
for (int i = 0; i < img->height; i++){
for (int j = 0; j < img->width ; j += 3){
img->luminance[i][j] = (img->pixels[i][j]); //<--- have a violation.
}
}
return TRUE;
}