0
votes

So my code involves reading in a PPM image and then storing it in an array so it can be saved in an new ppm file. Though I think there is a problem with my pointers that means its not actually reading the file. Also my code ends after allocating the memory for the array. Any help is much appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define maxheight 1080
#define maxwidth 1920
#define RGB_COMPONENT_COLOUR 255
#define pgmtype "P2"
#define ppmtype "P3"

typedef struct
{
    int red, green, blue;
} PPMPixel;

typedef struct
{
    int x, y;
} PPMImage;

typedef struct 
{
    int rgb_comp_colour;
    char filetype[3];
    int height;
    int width;
} PPMHead;


/*PPMHead head[3];
{
    head[3].filetype;
    head[3].height;
    head[3].width;
}*/

PPMHead head;
PPMHead* head_ptr = &head;

PPMPixel p;
PPMPixel* p_ptr = &p;

PPMPixel *data; //Defines pointer to PPMPixel
int **Array; //Double pointer defines as a pointer pointing to a pointer that is pointing to an integer
PPMPixel **RGBArray; //Double pointer defines as a pointer pointing to a pointer that is pointing to the PPMPixel structure
FILE *fp;
int r, g, b;

void headercheck ()
{

    fscanf(fp, "%s %d %d %d", head.filetype, &head.width, &head.height, &head.rgb_comp_colour);
    printf("%s %d %d %d", head.filetype, head.width, head.height, head.rgb_comp_colour);

    if (head.width > maxwidth || head.height > maxheight)
    {
        printf("\tInvalid image size. The maximum value of image is 1920x1080.\n");
        printf("\tImage size is %d x %d\n", head.width, head.height);
    }
    else
    {
        printf("\tImage size is valid\n");
        printf("\tImage size is %d x %d\n", head.width, head.height);
    }

    if ((strcmp (head.filetype, pgmtype)!=0) && (strcmp (head.filetype, ppmtype)!=0))
    {
        printf("\tInvalid filetype\n");
    }
    else
    {
        if(strcmp (head.filetype, pgmtype)==0)
        {
            printf("\t File is PGM type image\n");
        }
        else
        {
            if(strcmp (head.filetype, ppmtype)==0)
            {
                printf("\t File is PPM type image\n");
            }
        }
    }

    if ((head.rgb_comp_colour == RGB_COMPONENT_COLOUR))
    {
        printf("\t Image is 8 bit\n");
    }
    else
    {
        if (head.rgb_comp_colour > RGB_COMPONENT_COLOUR)
        {
            printf("Maximum bit-depth is 8 bits\n");
        }
        else
        {
            printf("\tImage is not 8 bit\n");
        }
    }
}

int main(void)
{
    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");
    }                        
    else    
    {
        printf("\tReading in %s\n", fname);
    }

    headercheck();

    if (strcmp (head.filetype, ppmtype)==0)
    {
        RGBArray = (PPMPixel **)malloc(head.height*sizeof(PPMPixel*)); //Points to malloc
            if((RGBArray == NULL))
                {
                    printf("Error allocating memory to the array");
                }
                else
                {
                    printf("Memory allocated to the PPM array sucessfully");
                }
        for (int i=0;i<head.width;i++)
        {
            RGBArray[i] = (PPMPixel *)malloc(head.width*sizeof(PPMPixel));
        }
        
        printf("Error 2");
        //Initialising each element
        for (int j=0;j<head.height;j++)
        {
            for (int i=0;i<head.width;i++)
            {
                fscanf(fp, "%3d %3d %3d ", &p.red, &p.green, &p.blue); //Scans in integers of the address pointer to PPMPixel
                data = &RGBArray[i][j]; //Defines data pointer pointing to address of RGBArray[i][j]
                data->red = p.red; //Access member of PPMPixel structure to equal one of the three RGB channels
                data->green = p.green;
                data->blue = p.blue;
            }
        }
    }
    fclose(fp);
    
    //Save PPM Array Into New PPM File
    FILE *pf;
    int i, j;
    char fname2[100];
    printf("Enter file name: ");
    scanf("%s", fname2);
    fseek(stdin,0,SEEK_END);
    pf = fopen(fname2, "w");
    if (pf == NULL)
    {   
        printf("\tError while opening the file\n");
    }
    else    
    {
        printf("\tWriting in %s\n", fname2);
    }

    for(j=0;j<head.height;j++)
    {
        fprintf(pf, "\n");
        for(i=0;i<head.width;i++)
        {
            fprintf(pf, "%3d ", RGBArray[i][j].red);
            fprintf(pf, "%3d ", RGBArray[i][j].green);
            fprintf(pf, "%3d ", RGBArray[i][j].blue);
            //fprintf(pf, "%3d ", (RGBArray+j*head.width + i)*r);
            //fprintf(pf, "%3d ", (RGBArray+j*head.width + i)*g);
            //fprintf(pf, "%3d ", (RGBArray+j*head.width + i)*b);
        }
    }

    fclose(pf);
    free(RGBArray);
    RGBArray = NULL;
    for(int i=0;i<head.width;i++)
        {
            free(RGBArray[i]);
            RGBArray[i] = NULL;
        }
    return 0;
}