Jump to content

Manipulações de imagens


Basic

Recommended Posts

Boas

to a tentar fazer um programa para manipular imagens do formato .pgm mas  quando tento rodar a imagen ela fica distrocida, e não entendo o porque

for(j=0;j<img->largura;j++){
    for(i=0;i<img->altura;i++){                         
       fprintf(f2,"%c",vect[(img->altura-1)-i][j]);
               }
               
}

 

a imagen so fica distrocida caso a imagem original seja retangular, se for uma imagen quadrada dá na boa

por isso não to entendendo

se alguem me poer dar uma ajudinha agradecia  😄

Link to comment
Share on other sites

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct pgm{
        char ident[5];
        char comt[256];
        int largura;
         int altura;
        int maximo;
        char **vect;
}PGM;
void pgmanipolacoes (PGM *img);
int lerpgm(PGM *img, FILE *f1, char caminho[50]);
void rodar90pgm (PGM *img, FILE *f2);


main() {
PGM img;
FILE *f1,* f2;
    system("cls");   
    if(lerpgm (img, f1, caminho)==1){                         
                           rodar90pgm(img,f2);                               
    }
    else printf("adeus");    
}  
  
  
  
int lerpgm(PGM *img, FILE *f1, char caminho[50]){
char aux[256];
int i, j, k;
     f1=fopen (caminho,"rb");
     if (f1==NULL)
         printf("Erro a ler a imagem \n");
      else {
            fgets(img->ident,5,f1);//identificador
            fgets(img->comt,256,f1); //possivel comentario
            if(img->comt[0] != '#'){//nao ha comentarios
                     sscanf(img->comt,"%d %d",&img->largura, &img->altura);
                     strcpy(img->comt,"#Nao existe comentario");
            }
            else if(img->comt[0] =='#'){ //ha comentario
                  do {
                      fgets(aux,256,f1);
                      if(aux[0]=='#')
                             strcat(img->comt,aux);
                  }while (aux[0]=='#');
                  sscanf(aux,"%d %d",&img->largura,&img->altura);
      }
      fscanf(f1,"%d",&img->maximo);
      
      img->vect=(char**)malloc(img->largura*sizeof(char*));
      for(i=0;i<img->largura;i++)
              img->vect[i]=(char*)malloc(img->altura*sizeof(char));     

	for(j=0;j<img->largura;j++){
		for(k=0;k<img->altura;k++){
	    	fscanf(f1,"%c",&img->vect[j][k]);			
         }      
        }
        return(1);        
   }
   return(0);
}

                       
void rodar90pgm (PGM *img, FILE *f2){
int i, j;
     system("cls");
     printf("\n\t Rodar imagen 90\n");
     f2=fopen ("rodar90pgm.pgm","w+b");  
     if(f2==NULL)
        printf("erro leitura do ficheiro");
     else
     fprintf(f2,"%s",img->ident);    
     fprintf(f2,"%s\n",img->comt);    
     fprintf(f2,"%d",img->altura);      
     fprintf(f2," %d\n",img->largura);         
     fprintf(f2,"%d\n",img->maximo);      
     for(i=0;i<img->largura;i++){ 
                 for(j=0;j<img->altura;j++){ 
                            vect[i][j]=img->vect[i][j];                    
                }          
     }
    
    for(j=0;j<img->altura;j++){
              for(i=0;i<img->largura;i++){                     
                   fprintf(f2,"%c",vect[(img->largura-1)-i][j]);
               }
     fclose(f2);
     printf("done\n");
     system("pause");
     main();   
}  
Link to comment
Share on other sites

Nem mais.

Este código vai contra um grande número de regras de programação em C.

Sem ser muito picuinhas, vai aqui um resumo:

C:\>gcc test.c -Wall -o test
test.c:18: warning: return-type defaults to `int'
test.c: In function `main':
test.c:22: `caminho' undeclared (first use in this function)
test.c:22: (Each undeclared identifier is reported only once
test.c:22: for each function it appears in.)
test.c:22: incompatible type for argument 1 of `lerpgm'
test.c:23: incompatible type for argument 1 of `rodar90pgm'
test.c:26: warning: control reaches end of non-void function
test.c: In function `rodar90pgm':
test.c:83: `vect' undeclared (first use in this function)
test.c:96: parse error at end of input

Caso compilemos com -ansi -pedantic, aí ficamos demasiado assustados com o número de erros e desistimos..

C:\>gcc test.c -Wall -o test -ansi -pedantic
test.c:18: warning: return-type defaults to `int'
test.c: In function `main':
test.c:22: `caminho' undeclared (first use in this function)
test.c:22: (Each undeclared identifier is reported only once
test.c:22: for each function it appears in.)
test.c:22: incompatible type for argument 1 of `lerpgm'
test.c:23: incompatible type for argument 1 of `rodar90pgm'
test.c:26: warning: control reaches end of non-void function
test.c: In function `lerpgm':
test.c:37: parse error before `/'
test.c:43: parse error before `else'
test.c:32: warning: unused variable `k'
test.c:32: warning: unused variable `j'
test.c:32: warning: unused variable `i'
test.c:48: warning: control reaches end of non-void function
test.c: At top level:
test.c:48: parse error before `while'
test.c:49: parse error before string constant
test.c:49: warning: type defaults to `int' in declaration of `sscanf'
test.c:49: conflicting types for `sscanf'
test.c:49: A parameter list with an ellipsis can't match an empty parameter name
list declaration.
C:\Programas\DevC\Bin\..\lib\gcc-lib\i386-mingw32msvc\2.95.2\..\..\..\..\include
\stdio.h:227: previous declaration of `sscanf'
test.c:49: ANSI C forbids data definition with no type or storage class
test.c:51: parse error before string constant
test.c:51: warning: type defaults to `int' in declaration of `fscanf'
test.c:51: conflicting types for `fscanf'
test.c:51: A parameter list with an ellipsis can't match an empty parameter name
list declaration.
C:\Programas\DevC\Bin\..\lib\gcc-lib\i386-mingw32msvc\2.95.2\..\..\..\..\include
\stdio.h:225: previous declaration of `fscanf'
test.c:51: ANSI C forbids data definition with no type or storage class
test.c:53: parse error before `->'
test.c: In function `rodar90pgm':
test.c:83: `vect' undeclared (first use in this function)
test.c:96: parse error at end of input
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.