It says to use -> but I've already used ->?
It says to use -> but I've already used ->?
Last edited by DGB_684; 12-31-2021 at 05:59 AM.
Your image cannot be opened. Please post the code in [CODE] blocks.
I've edited the post. It should be visible now.
Its quite long but this is all my code.
Code:#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; PPMPixel *data; } PPMImage; typedef struct { int rgb_comp_colour; char buff[16]; char filetype[2]; int height; int width; } PPMHead; PPMHead head[3]; int **Array; PPMPixel **RGBArray; FILE *fp; void headercheck () { fscanf(fp, "%s %d %d %d", head[0].filetype, &head[0].width, &head[0].height, &head[0].rgb_comp_colour); printf("%s %d %d %d", head[0].filetype, head[0].width, head[0].height, head[0].rgb_comp_colour); if (head[0].width > maxwidth || head[0].height > maxheight) { printf("\tInvalid image size. The maximum value of image is 1920x1080.\n"); printf("\tImage size is %d x %d\n", head[0].width, head[0].height); } else { printf("\tImage size is valid\n"); printf("\tImage size is %d x %d\n", head[0].width, head[0].height); } if ((strcmp (head[0].filetype, pgmtype)!=0) && (strcmp (head[0].filetype, ppmtype)!=0)) { printf("\tInvalid filetype\n"); } else { if(strcmp (head[0].filetype, pgmtype)==0) { printf("\t File is PGM type image\n"); } else { if(strcmp (head[0].filetype, ppmtype)==0) { printf("\t File is PPM type image\n"); } } } if ((head[0].rgb_comp_colour, RGB_COMPONENT_COLOUR)==0) { printf("\t Image is 8 bit\n"); } else { if (head[0].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) { PPMImage *img; int d = 0; 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[0].filetype, pgmtype)==0) { printf("\tReading in PGM image...\n"); Array = malloc(head[0].width*sizeof(int*)); for (int i=0;i<head[0].width;i++) Array[i] = malloc(head[0].height*sizeof(int)); { if(Array = NULL) { printf("Error allocating memory to the array"); } else { printf("Memory allocated to the PGM array sucessfully"); } } for(int j=0;j<head[0].height;j++) { for(int i=0;i<head[0].width;i++) { fscanf(fp, "%3d ", &Array[i][j]); } } } if (strcmp (head[0].filetype, ppmtype)==0) { RGBArray = (PPMPixel **)malloc(head[0].height*sizeof(PPMPixel*)); for (int i=0;i<head[0].width;i++) RGBArray[i] = (PPMPixel *)malloc(head[0].width*sizeof(PPMPixel)); { if(RGBArray = NULL) { printf("Error allocating memory to the array"); } else { printf("Memory allocated to the PPM array sucessfully"); } } PPMPixel p; //Initialising each element for (int j=0;j<head[0].height;j++) { for (int i=0;i<head[0].width;i++) { fscanf(fp, "%3d %3d %3d ", &p.red, &p.green, &p.blue); PPMPixel *data = &RGBArray[i][j]; data->red = p.red; 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); } if(strcmp(head[0].filetype, pgmtype)==0) { for(j=0;j<head[0].height;j++) { fprintf(pf, "\n"); for(i=0;i<head[0].width;i++) { fprintf(pf, "%3d ", *(Array+j*head[0].width + i)); } } } else { for(j=0;j<head[0].height;j++) { fprintf(pf, "\n"); for(i=0;i<head[0].width;i++) { fprintf(pf, "%3d ", (RGBArray+j*head[0].width + i)->red); fprintf(pf, "%3d ", (RGBArray+j*head[0].width + i)->green); fprintf(pf, "%3d ", (RGBArray+j*head[0].width + i)->blue); } } } printf("Yes"); fclose(pf); free(Array); free(RGBArray); return 0; }
You first need to increase your warning level to the highest level in the compiler, then fix some of the errors before the ones you posted earlier:
Code:arraycheck.c: In function ‘headercheck’: arraycheck.c:80:33: warning: left-hand operand of comma expression has no effect [-Wunused-value] 80 | if ((head[0].rgb_comp_colour, RGB_COMPONENT_COLOUR)==0) | ^ arraycheck.c: In function ‘main’: arraycheck.c:128:16: warning: suggest parentheses around assignment used as truth value [-Wparentheses] 128 | if(Array = NULL) | ^~~~~ arraycheck.c:150:9: warning: this ‘for’ clause does not guard... [-Wmisleading-indentation] 150 | for (int i=0;i<head[0].width;i++) | ^~~ arraycheck.c:152:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘for’ 152 | { | ^ arraycheck.c:153:20: warning: suggest parentheses around assignment used as truth value [-Wparentheses] 153 | if(RGBArray = NULL) | ^~~~~~~~ arraycheck.c:209:32: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat=] 209 | fprintf(pf, "%3d ", *(Array+j*head[0].width + i)); | ~~^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | | | | int int * | %3ls arraycheck.c:222:67: error: ‘*(RGBArray + ((sizetype)(j * head[0].width) + (sizetype)i) * 8)’ is a pointer; did you mean to use ‘->’? 222 | fprintf(pf, "%3d ", (RGBArray+j*head[0].width + i)->red); | ^~ | -> arraycheck.c:223:67: error: ‘*(RGBArray + ((sizetype)(j * head[0].width) + (sizetype)i) * 8)’ is a pointer; did you mean to use ‘->’? 223 | fprintf(pf, "%3d ", (RGBArray+j*head[0].width + i)->green); | ^~ | -> arraycheck.c:224:67: error: ‘*(RGBArray + ((sizetype)(j * head[0].width) + (sizetype)i) * 8)’ is a pointer; did you mean to use ‘->’? 224 | fprintf(pf, "%3d ", (RGBArray+j*head[0].width + i)->blue); | ^~ | -> arraycheck.c:102:9: warning: unused variable ‘d’ [-Wunused-variable] 102 | int d = 0; | ^ arraycheck.c:101:15: warning: unused variable ‘img’ [-Wunused-variable] 101 | PPMImage *img; | ^~~
Great. Thanks. Would you have any idea how to do that in visual studio code?
I don't use any IDE, especially anything from Mickey$oft, but look at this link. If this does not work do a Google search for the version you are using, and/or any online manual for Visual Studio.
Ok. I think I managed it. But I still have the three original errors popping up despite resolving the rest.
Cool. Thanks. Here is the updated code. But the errors were mostly minor so not much has really changed.
Code:#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; PPMPixel *data; } PPMImage; typedef struct { int rgb_comp_colour; char buff[16]; char filetype[2]; int height; int width; } PPMHead; PPMHead head[3]; int **Array; PPMPixel **RGBArray; FILE *fp; void headercheck () { fscanf(fp, "%s %d %d %d", head[0].filetype, &head[0].width, &head[0].height, &head[0].rgb_comp_colour); printf("%s %d %d %d", head[0].filetype, head[0].width, head[0].height, head[0].rgb_comp_colour); if (head[0].width > maxwidth || head[0].height > maxheight) { printf("\tInvalid image size. The maximum value of image is 1920x1080.\n"); printf("\tImage size is %d x %d\n", head[0].width, head[0].height); } else { printf("\tImage size is valid\n"); printf("\tImage size is %d x %d\n", head[0].width, head[0].height); } if ((strcmp (head[0].filetype, pgmtype)!=0) && (strcmp (head[0].filetype, ppmtype)!=0)) { printf("\tInvalid filetype\n"); } else { if(strcmp (head[0].filetype, pgmtype)==0) { printf("\t File is PGM type image\n"); } else { if(strcmp (head[0].filetype, ppmtype)==0) { printf("\t File is PPM type image\n"); } } } if ((head[0].rgb_comp_colour == RGB_COMPONENT_COLOUR)) { printf("\t Image is 8 bit\n"); } else { if (head[0].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[0].filetype, pgmtype)==0) { printf("\tReading in PGM image...\n"); Array = malloc(head[0].width*sizeof(int*)); for (int i=0;i<head[0].width;i++) Array[i] = malloc(head[0].height*sizeof(int)); { if((Array = NULL)) { printf("Error allocating memory to the array"); } else { printf("Memory allocated to the PGM array sucessfully"); } } for(int j=0;j<head[0].height;j++) { for(int i=0;i<head[0].width;i++) { fscanf(fp, "%3d ", &Array[i][j]); } } } if (strcmp (head[0].filetype, ppmtype)==0) { printf("Error 1"); RGBArray = (PPMPixel **)malloc(head[0].height*sizeof(PPMPixel*)); for (int i=0;i<head[0].width;i++) { RGBArray[i] = (PPMPixel *)malloc(head[0].width*sizeof(PPMPixel)); } { if((RGBArray = NULL)) { printf("Error allocating memory to the array"); } else { printf("Memory allocated to the PPM array sucessfully"); } } PPMPixel p; printf("Error 2"); //Initialising each element for (int j=0;j<head[0].height;j++) { for (int i=0;i<head[0].width;i++) { fscanf(fp, "%3d %3d %3d ", &p.red, &p.green, &p.blue); PPMPixel *data = &RGBArray[i][j]; data->red = p.red; data->green = p.green; data->blue = p.blue; } printf("Erroe"); } } printf("Error 3"); fclose(fp); printf("Error 4"); //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); } if(strcmp(head[0].filetype, pgmtype)==0) { for(j=0;j<head[0].height;j++) { fprintf(pf, "\n"); for(i=0;i<head[0].width;i++) { fprintf(pf, "%3d ", **(Array+j*head[0].width + i)); } } } else { for(j=0;j<head[0].height;j++) { fprintf(pf, "\n"); for(i=0;i<head[0].width;i++) { fprintf(pf, "%3d ", (RGBArray+j*head[0].width + i)->red); fprintf(pf, "%3d ", (RGBArray+j*head[0].width + i)->green); fprintf(pf, "%3d ", (RGBArray+j*head[0].width + i)->blue); } } } printf("Yes"); fclose(pf); free(Array); free(RGBArray); return 0; }
First of all, please look at lines 122 & 123, followed by a block. Is this correct?
Second, I am not that familiar with image processing so I might yield to someone that is more familiar with the code.
I think that is an accident. The malloc should be in the for loop. Yes I guess image processing not everyone is familiar with which is why I've found it so hard to get help on this. Its more to do with dynamically allocating arrays and pointers tbh. But thanks for the help so far.
Still warnings and errors left. VS 2019 with C17 standard and /W4
Error E0132 expression must have pointer-to-struct-or-union type but it has type "PPMPixel **" C_CONSOLE.c 232
Error E0132 expression must have pointer-to-struct-or-union type but it has type "PPMPixel **" C_CONSOLE.c 233
Error E0132 expression must have pointer-to-struct-or-union type but it has type "PPMPixel **" C_CONSOLE.c 234
Warning C6031 Return value ignored: 'fscanf'. C_CONSOLE.c 48
Warning C6031 Return value ignored: 'fscanf'. C_CONSOLE.c 142
Warning C6031 Return value ignored: 'fscanf'. C_CONSOLE.c 176
Warning C6031 Return value ignored: 'scanf'. C_CONSOLE.c 199
Warning C6054 String 'fname2' might not be zero-terminated. C_CONSOLE.c 201
Warning C6387 'pf' could be '0': this does not adhere to the specification for the function 'fprintf'. C_CONSOLE.c 229
Warning C6387 'pf' could be '0': this does not adhere to the specification for the function 'fclose'. C_CONSOLE.c 241
Error C2223 left of '->red' must point to struct/union C_CONSOLE.c 232
Warning C4473 'fprintf' : not enough arguments passed for format string C_CONSOLE.c 232
Error C2223 left of '->green' must point to struct/union C_CONSOLE C_CONSOLE.c 233
Warning C4473 'fprintf' : not enough arguments passed for format string C_CONSOLE.c 233
Error C2223 left of '->blue' must point to struct/union C_CONSOLE.c 234
Warning C4473 'fprintf' : not enough arguments passed for format string C_CONSOLE.c 234