Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.70 KB | None | 0 0
  1.     #include <stdio.h>
  2.     #include <stdlib.h>
  3.    
  4.     typedef struct{
  5.         int c;
  6.         int l;
  7.         unsigned char maximo;
  8.         unsigned char **imagem;
  9.     } PGM;
  10.    
  11.     PGM *LerPGM(char* entrada);
  12.    
  13.     int main()
  14.     {
  15.         PGM *imgconv;
  16.         int i, j;
  17.    
  18.         imgconv=LerPGM("entrada.pgm");
  19.         printf("\n");
  20.         for(i=0; i<imgconv->l; i++){
  21.             for(j=0; j<imgconv->c; j++){
  22.                 printf("%d ", imgconv->imagem[i][j]);
  23.             }
  24.             printf("\n");
  25.         }
  26.    
  27.         return 0;
  28.     }
  29.    
  30.     PGM *LerPGM(char* entrada){
  31.         PGM *img;
  32.         img=malloc(sizeof(PGM));
  33.         char tipo[3];
  34.         int i, j;
  35.    
  36.         FILE *arq;
  37.         arq = fopen(entrada, "r");
  38.         if(arq == NULL){
  39.             printf("Arquivo nao encontrado.");
  40.             return 0;
  41.         }
  42.    
  43.         fscanf(arq, "%s %d %d %d", &tipo, &img->c, &img->l, &img->maximo);
  44.         if(strcmp(tipo, "P2")){
  45.             printf("O arquivo nao e PGM.");
  46.             return 0;
  47.         }
  48.    
  49.         img->imagem = malloc(img->l * sizeof(char *));
  50.         for(i=0; i<img->c; i++) img->imagem[i] = malloc(img->c * sizeof(char));
  51.         if(img->imagem == NULL){
  52.             printf("Falha na alocacao de memoria.");
  53.             return 0;
  54.         }
  55.    
  56.         for(i=0; i<img->l; i++){
  57.             for(j=0; j<img->c; j++){
  58.                 fscanf(arq, "%d", &img->imagem[i][j]);
  59.             }
  60.         }
  61.    
  62.         fclose(arq);
  63.    
  64.         for(i=0; i<img->l; i++){
  65.             for(j=0; j<img->c; j++){
  66.                 printf("%d ", img->imagem[i][j]);
  67.             }
  68.             printf("\n");
  69.         }
  70.         return img;
  71.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement