1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
int width = 0;
void flood(int test[][width], int color, int x, int y){
if(test[x][y] == 255 ){
test[x][y] = color;
flood(test,color,x+1,y);
flood(test,color,x,y+1);
flood(test,color,x-1,y);
flood(test,color,x,y-1);
}
}
int main(int argc, char * argv[]){
int color = atoi(argv[3]); //barva s katero zamenjamo polje
int x = atoi(argv[4]); //zaèetna x koordinata
int y = atoi(argv[5]); //zaèetna y koordinata
//najprej odpremo datoteko in preberemo vse v tabelo slika
char prebrano[1000];
char slika[1000][1000];
int stevec = 0;
FILE * f = fopen(argv[1],"r");
while(fscanf(f, "%s", prebrano) != EOF){
strncpy(slika[stevec],prebrano,1000);
stevec++;
}
fclose(f);
//zapišemo samo števila v tabelo vsaStevila
char vsaStevila[stevec][1000];
int s = 0;
int i;
for(i = 0; i < stevec; i++){
if(atof(slika[i]) > 0 || strcmp(slika[i],"0") == 0){
strncpy(vsaStevila[s],slika[i],1000);
s++;
}
}
//iz tabele dobimo sirino, visino in max barv
int sirina = atoi(vsaStevila[0]);
int visina = atoi(vsaStevila[1]);
int maxBarv = atoi(vsaStevila[2]);
//vsa ostala stevila zapisemo v drugo tabelo
char stevila[s][1000];
int st = 0;
int j;
for(j = 3; j < s; j++){
strncpy(stevila[st],vsaStevila[j],1000);
st++;
}
//iz teh stevil naredimo dvodimenzionalno tabelo, ki jo posljemo za floodfill
int test[visina][sirina];
int a;
int stVrstic = 0;
int stStolpec = 0;
for(a = 0; a < st; a++){
if(stStolpec < sirina){
test[stVrstic][stStolpec] = atoi(stevila[a]);
stStolpec++;
}
if(stStolpec == sirina){
stVrstic++;
stStolpec = 0;
test[stVrstic][stStolpec] = atoi(stevila[a]);
}
}
width = sirina;
flood(*test, color, x, y);
FILE * fi = fopen(argv[2],"w");
fprintf(fi,"P2\n");
fprintf(fi,"%d %d %d\n",sirina, visina, maxBarv);
int b;
int c;
for(b = 0; b < visina; b++){
for(c = 0; c < sirina; c++){
fprintf(fi,"%d ",test[b][c]);
}
fprintf(fi,"\n");
}
fclose(fi);
}
|