#include #include #include #include #include "lib.h" IMAGE convolui(IMAGE img1,int N) { int a,b,i,j,t,x,npixels,temp; int aux, aux1, aux2, aux3, div; IMAGE img2; int cont=0, divisao; long int S = 0; npixels = img1->info->nc * img1->info->nr; img2=img1; divisao = (int) N / 2; for(i=0; i < img2->info->nr; i++) { for(j=0; j < img2->info->nc; j++) { aux = i - divisao; aux1 = j - divisao; aux2 = i + divisao; aux3 = j + divisao; if(aux>=0 && aux1>=0 && aux2info->nr && aux3info->nc) { S = 0; for(t=aux; t <= aux2; t++) { for(x=aux1; x <= aux3; x++) { S = S + img1->data[t][x]; } } div=S/(N*N); img2->data[i][j] = div; } } } return (img2); } /* Marca o tempo entre duas chamadas */ void MarkTime(double *t) { static struct timeval tstart; struct timeval tend; if (t == 0) gettimeofday(&tstart,NULL); else { gettimeofday(&tend,NULL); *t = (tend.tv_sec - tstart.tv_sec) * 1000000; *t += tend.tv_usec - tstart.tv_usec; } } /* Exibe o intervalo de tempo na tela */ void ShowTime(double dift) { int d, h, m, s, ms; ms = (int) ((int) dift % 1000000) / 1000; dift = dift / 1000000; d = (int) dift / (24 * 3600); /* Dias */ dift = ((int) dift) % (24 * 3600); h = (int) dift / 3600; /* Horas */ dift = ((int) dift) % 3600; m = (int) dift / 60; /* Minutos */ s = ((int) dift) % 60; /* Segundos */ printf("Tempo de processamento: %d dia(s) %dh %dm %ds %dms.",d , h , m, s, ms); } /*********************** main ********************/ int main( int argc, char** argv ){ //Imagem *img1,*img2; IMAGE im=0,img2; double tempo; if (argc != 4) { printf("Uso: convol entrada.p2 filtro.txt saida.p2\n"); printf("entrada.p2 : Imagem de entrada.\n"); printf("filtro.txt : filtro.\n"); printf("saida.p2 : Imagem de saida.\n"); exit(0); } /* Ler imagem de entrada */ im=Input_PBM(argv[1]); if (im == 0) { printf ("Nao ha arquivo de entrada('%s')\n", argv[1]); exit (2); } /* Convolui a imagem */ MarkTime(NULL); img2=convolui(im,atoi(argv[2])); MarkTime(&tempo); /* Grava imagem de saida */ //grava_imagem Output_PBM(img2,argv[3]); ShowTime(tempo); printf("\n"); }