Advertisement
Kyrexar

Examen 2011 - 1

May 25th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. /* Programa en C que maneje un fichero .jag (16x16 valores entre 0 y 255) */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define FIL 16 // filas
  7. #define COL 16 // columnas
  8.  
  9. /* Lee y almacena la imagen en una matriz de enteros a partir de
  10. un fichero en formato jag cuyo nombre proporciona el usuario. */
  11.  
  12. // Define la función LeeImagen (Apartado a)
  13. int LeeImagen( int img[FIL][COL] ){
  14.     FILE *f;
  15.     int i, j;
  16.     char nombre_f[80];
  17.  
  18.     gets(nombre_f); // nombre del fichero a leer
  19.     f=fopen(nombre_f,"r");
  20.  
  21.     if(f==NULL) return -1;
  22.     else{
  23.         for( i=0 ; i<FIL ; i++ )
  24.             for( j=0 ; j<COL ; j++ )
  25.                 fscanf(f,"%d",&img[i][j]);
  26.  
  27.         fclose(f);
  28.         return 0;
  29.     }
  30. }
  31.  
  32. /* Devuelve el valor mínimo y el valor máximo (por referencia)
  33. entre los valores de los pixeles de la imagen. */
  34.  
  35. // Define la función MinMaxImagen (Apartado b)
  36. void MinMaxImagen( int img[FIL][COL], int *min, int *max){
  37.     int i, j, minimo, maximo;
  38.  
  39.     minimo=img[0][0];
  40.     maximo=img[0][0];
  41.    
  42.     for( i=0 ; i<FIL ; i++ )
  43.         for( j=0 ; j<COL ; j++ ){
  44.             if( img[i][j]<minimo) minimo=img[i][j];
  45.             if( img[i][j]>maximo) maximo=img[i][j];
  46.         }
  47.  
  48.     *min=minimo;
  49.     *max=maximo;
  50. }
  51.  
  52. /* Aplica la transformación:
  53. Pixel transformado=( (Valor original - minimo)*255 ) / ( max-min ) */
  54.  
  55. // Define la función TransformaImagen (Apartado c)
  56. void TransformaImagen( int img[FIL][COL], int min, int max ){
  57.     int i, j;
  58.    
  59.     for( i=0 ; i<FIL ; i++ )
  60.         for( j=0 ; j<COL ; j++ )
  61.             img[i][j]=((img[i][j]-min)*255)/(max-min);
  62. }
  63.  
  64. int main(){
  65.     int img[FIL][COL];
  66.     int error, min, max;
  67.  
  68.     if (LeeImagen(img) == -1){
  69.         printf("Error abriendo fichero imagen\n");
  70.         return -1;
  71.     }
  72.    
  73.     MinMaxImagen(img, &min, &max);
  74.     TransformaImagen(img, min, max);
  75.    
  76.     system("PAUSE");
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement