I'm trying to use malloc so that a PPM can be read into an array and then for that array to be saved into a file using a switch case statement. So far nothing really seems to happen. Any help is much appreciated. Thanks.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define height 1080
#define width 1920
#define RGB_COMPONENT_COLOUR 255
typedef struct
{
unsigned char red, green, blue;
} PPMPixel;
typedef struct
{
int x, y;
PPMPixel *data;
} PPMImage;
void clear_to_end(FILE *fp)
{
int c;
while ((c = fgetc(fp)) != EOF && c != '\n');
}
PPMImage* load_file()
{
FILE *fp;
PPMImage *img = malloc(sizeof(*img));
int d, rgb_comp_colour;
char buff[80];
char fname[100];
printf("Enter file name: ");
scanf("%s", fname);
fseek(stdin,0,SEEK_END);
fp = fopen(fname, "r");
if (fp == NULL)
{
printf("\tError while opening the file\n");
return NULL;
}
else
{
printf("\tReading in %s\n", fname);
}
//Checking for comments
//Skip whitespace and comments
d = getc(fp);
while (d=='#')
{
while(getc(fp)!='\n')
d=getc(fp);
}
ungetc(d,fp);
//Checking header
if (!fgets(buff,sizeof(buff),fp))
{
perror(fname);
exit(1);
}
if (buff[0] =='P' && (buff[1] == '2' || buff[1] == '3'))
{
printf("\tReading PGM/PPM image in...\n");
}
else
{
printf("\tIncorrect image format\n");
return NULL;
}
//Read image size information
if (fscanf(fp, "%u %u", &img->y, &img->x) !=2)
{
printf("\tInvalid image size\n");
printf("\tImage size is: %u x %u\n", img->y, img->x);
}
else
{
printf("\tImage size is valid\n");
printf("\tImage size is: %u x %u\n", img->y, img->x);
}
if (fscanf(fp, "%d", &rgb_comp_colour) !=1)
{
printf("Invalid RGB component for %s\n", fname);
}
else
{
printf("\tRGB component is valid\n");
printf("%d", rgb_comp_colour);
}
fscanf(fp, "%c ", &d);
//Check RGB component depth
if (rgb_comp_colour!=RGB_COMPONENT_COLOUR)
{
printf("\t'%s' is not 8 bit\n", fname);
}
else
{
printf("\t'%s' is 8 bit\n", fname);
}
int i, j;
int array[i][j];
**array = malloc (width*height*sizeof(array[i][j]));
for (i=0; i<width; i++)
{
*array[i] = malloc(width * sizeof(int));
for(j=0; j<height; j++)
{
fseek(fp, 0, SEEK_SET);
fscanf(fp, "%d ", &array[i][j]);
}
}
for (i=0; i<width; i++)
{
for(j=0; j<height; j++)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
fclose(fp);
return img;
}
void save_file(PPMImage *img)
{
//Save PPM Array Into New PPM File
}
int main()
{
char key = 0;
PPMImage* img = NULL;
do {
puts("\tPress r to read in an image in ppm format");
puts("\tPress s to save image in ppm format");
puts("\tPress q to quit");
scanf(" %c", &key);
clear_to_end(stdin);
switch (key) {
case 'r':
{
PPMImage *img = load_file("fname");
free(img);
}
break;
case 's':
{
save_file(img);
}
break;
case 'q':
{
puts("\tTerminating program...");
}
break;
default:
{
puts("\tInvalid Input");
}
break;
}
} while (key != 'q');
return 0;
}