im doing this program where i analyze colors in a ppm file
now im having a hard time storing the data into arrays in this order
P3
600 339
255
44 5 8
44 5 8
43 4 7
42 3 6
42 3 4
44 5 6
...
so far i tried using fscanf()
-----------------------------------------------------------
------------------------------------------Code:#include <stdio.h> #include <stdlib.h> #include <math.h> #include <stdbool.h> #define DEBUG true #define CHECK true #define C_LIMIT 3 int main() { int operand_1[99][C_LIMIT]; int row1 = 0; int column1; int input_x; int row2 = 0; int column2 = 0; int input_y; int row3; int column3; int clear; int clear1 = 0; int clear2 = 0; int clear3 = 0; int clear4 = 0; int until; int count = 0; int zeros = 0; int num; int reverse; int original; int reverse_sum; int organize_1; int result; int organize_2; int debug; int str1, str2, str3; int year; FILE *fp; fp = fopen ("n44f - Copy.ppm", "r"); //rewind(fp); do { for (column1=0; column1 < 3; column1++) { fscanf(fp, "%d", &num); operand_1[row1][column1] = num; } column1=0; row1++; }while(operand_1[row1][column1] != EOF); for(row2 = 0; row2 <= row1;){ if(row2 != row1){ if(column2!=3){ printf("row %d column %d |%d|\t",row2,column2,operand_1[row2][column2]); column2++; } else if((column2==3)){ column2=0; ++row2; printf("\n"); } } else{ for(;column2 < column1;){ printf("row %d column %d |%d|\t",row2,column2,operand_1[row2][column2]); column2++; } break; } } printf("\n"); if(DEBUG) printf("[DEBUG] column1 = %d\n", column1); if(DEBUG) printf("[DEBUG] row1 = %d\n", row1); if(DEBUG) printf("[DEBUG] column2 = %d\n", column2); if(DEBUG) printf("[DEBUG] row2 = %d\n", row2); if(DEBUG) printf("[DEBUG] count = %d\n", count); if(DEBUG) printf("[DEBUG] zeros = %d\n", zeros); if(DEBUG) printf("[DEBUG] clear = %d\n", clear); if(DEBUG) printf("[DEBUG] until = %d\n", until); if(DEBUG) printf("[DEBUG] clear2 = %d\n", clear2); if(DEBUG) printf("[DEBUG] clear4 = %d\n", clear4); fclose(fp); return(0); }
and then i tried using fgetc()
------------------------------------------
-----------------------------------------------------Code:#include <stdio.h> #include <stdlib.h> #include <math.h> #include <stdbool.h> #define DEBUG true #define CHECK true #define C_LIMIT 3 int main() { int operand_1[99][C_LIMIT]; int row1; int column1; int input_x; int row2 = 0; int column2 = 0; int input_y; int row3; int column3; int clear; int clear1 = 0; int clear2 = 0; int clear3 = 0; int clear4 = 0; int until; int count = 0; int zeros = 0; int reverse; int original; int reverse_sum; int organize_1; int result; int organize_2; int debug; int str1, str2, str3; int year; FILE *fp; fp = fopen ("n44f - Copy.ppm", "r"); //rewind(fp); for (column1 = 0, row1 = 0;(input_x=fgetc(fp)) != EOF;){ if(column1!=3){ if(input_x!='\n'){ if(CHECK) printf("[CHECK] pass 1\n"); count++; operand_1[row1][column1]=input_x; zeros++; if(DEBUG) printf("[DEBUG] row1 = %d column1 = %d\n", row1, column1); } else if(input_x!=' '){ } else{ count++; zeros++; if(CHECK) printf("[CHECK] didnt pass 2\n"); clear2++; column1--; } ++column1; } else{ if(input_x!='\n' && input_x!=' '){ count++; if(CHECK) printf("[CHECK] pass 3\n"); column1=0; ++row1; operand_1[row1][column1]=input_x; zeros++; if(DEBUG) printf("[DEBUG] row1 = %d column1 = %d\n", row1, column1); column1++; } else{ count++; if(CHECK) printf("[CHECK] didnt pass (4)\n"); clear4++; ++row1; zeros++; column1=0; } } } for(row2 = 0; row2 <= row1;){ if(row2 != row1){ if(column2!=3){ printf("row %d column %d |%c|\t",row2,column2,operand_1[row2][column2]); column2++; } else if((column2==3)){ column2=0; ++row2; printf("\n"); } } else{ for(;column2 < column1;){ printf("row %d column %d |%c|\t",row2,column2,operand_1[row2][column2]); column2++; } break; } } printf("\n"); if(DEBUG) printf("[DEBUG] column1 = %d\n", column1); if(DEBUG) printf("[DEBUG] row1 = %d\n", row1); if(DEBUG) printf("[DEBUG] column2 = %d\n", column2); if(DEBUG) printf("[DEBUG] row2 = %d\n", row2); if(DEBUG) printf("[DEBUG] count = %d\n", count); if(DEBUG) printf("[DEBUG] zeros = %d\n", zeros); if(DEBUG) printf("[DEBUG] clear = %d\n", clear); if(DEBUG) printf("[DEBUG] until = %d\n", until); if(DEBUG) printf("[DEBUG] clear2 = %d\n", clear2); if(DEBUG) printf("[DEBUG] clear4 = %d\n", clear4); fclose(fp); return(0); }
im still having a hard time storing them the way i wanted
the second method i tried stored them in this order
P 3 6
0 0 3
3 9 2
5 5 4
4 5 8
4 4 5
8 4 3
...
i then realized that the order that the second method gave me will make it very hard for me to calculate the RGBs.
because they will be calculated like wise..
P3
600 339
255
44 5 8 = sum
44 5 8 = sum
43 4 7 = sum
42 3 6 = sum
42 3 4 = sum
44 5 6 = sum
...
but that isn't the issue right now..
please excuse my messy program i need help
thanks