Advertisement
pushrbx

Untitled

Nov 13th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.54 KB | None | 0 0
  1. // ConsoleApplication6.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "pgm.h"
  6. #include <memory>
  7.  
  8. void getPGMfile (char filename[], PGMImage *img)
  9. {
  10.     FILE *in_file;
  11.     char ch;
  12.     int row, col, type;
  13.     int ch_int;
  14.  
  15.     in_file = fopen(filename, "r");
  16.     if (in_file == NULL)
  17.     {
  18.         fprintf(stderr, "Error: Unable to open file %s\n\n", filename);
  19.         exit(8);
  20.     }
  21.  
  22.     printf("\nReading image file: %s\n", filename);
  23.  
  24.     /*determine pgm image type (only type three can be used)*/
  25.     ch = getc(in_file);
  26.     if(ch != 'P')
  27.     {
  28.         printf("ERROR(1): Not valid pgm/ppm file type\n");
  29.         exit(1);
  30.     }
  31.     ch = getc(in_file);
  32.     /*convert the one digit integer currently represented as a character to
  33.     an integer(48 == '0')*/
  34.     type = ch - 48;
  35.     if((type != 2) && (type != 3) && (type != 5) && (type != 6))
  36.     {
  37.         printf("ERROR(2): Not valid pgm/ppm file type\n");
  38.         exit(1);
  39.     }
  40.  
  41.     while(getc(in_file) != '\n');             /* skip to end of line*/
  42.  
  43.     while (getc(in_file) == '#')              /* skip comment lines */
  44.     {
  45.         while (getc(in_file) != '\n');          /* skip to end of comment line */
  46.     }
  47.  
  48.     /*there seems to be a difference between color and b/w.  This line is needed
  49.     by b/w but doesn't effect color reading...*/
  50.     fseek(in_file, -1, SEEK_CUR);             /* backup one character*/
  51.  
  52.     fscanf(in_file,"%d", &((*img).width));
  53.     fscanf(in_file,"%d", &((*img).height));
  54.     fscanf(in_file,"%d", &((*img).maxVal));
  55.  
  56.     printf("\n width  = %d",(*img).width);
  57.     printf("\n height = %d",(*img).height);
  58.     printf("\n maxVal = %d",(*img).maxVal);
  59.     printf("\n");
  60.  
  61.     if (((*img).width  > MAX) || ((*img).height  > MAX))
  62.     {
  63.         printf("\n\n***ERROR - image too big for current image structure***\n\n");
  64.         exit(1);
  65.     }
  66.  
  67.     if(type == 2) /*uncompressed ascii file (B/W)*/
  68.     {
  69.         for (row=(*img).height-1; row >=0; row--)
  70.             for (col=0; col< (*img).width; col++)
  71.             {
  72.                 fscanf(in_file,"%d", &ch_int);
  73.                 (*img).data[row][col].red = ch_int;
  74.                 (*img).data[row][col].green = ch_int;
  75.                 (*img).data[row][col].blue = ch_int;
  76.             }
  77.     }
  78.     else if(type == 3) /*uncompressed ascii file (color)*/
  79.     {
  80.         for (row=(*img).height-1; row >=0; row--)
  81.             for (col=0; col< (*img).width; col++)
  82.             {
  83.  
  84.                 fscanf(in_file,"%d", &ch_int);
  85.                 ((*img).data[row][col].red) = (unsigned char)ch_int;
  86.  
  87.                 fscanf(in_file,"%d", &ch_int);
  88.                 ((*img).data[row][col].green) = (unsigned char)ch_int;
  89.  
  90.                 fscanf(in_file,"%d", &ch_int);
  91.                 ((*img).data[row][col].blue) = (unsigned char)ch_int;
  92.             }
  93.     }
  94.     else if(type == 5) /*compressed file (B/W)*/
  95.         /*note: this type remains untested at this time...*/
  96.     {
  97.         while(getc(in_file) != '\n'); /*skip to end of line*/
  98.  
  99.         for (row=(*img).height-1; row >=0; row--)
  100.             for (col=0; col< (*img).width; col++)
  101.             {
  102.                 ch = getc(in_file);
  103.                 (*img).data[row][col].red = ch;
  104.                 (*img).data[row][col].green = ch;
  105.                 (*img).data[row][col].blue = ch;
  106.             }
  107.     }
  108.  
  109.     else if(type == 6) /*compressed file (color)*/
  110.     {
  111.         while(getc(in_file) != '\n'); /*skip to end of line*/
  112.  
  113.         for (row=(*img).height-1; row >=0; row--)
  114.             for (col=0; col< (*img).width; col++)
  115.             {
  116.                 (*img).data[row][col].red = getc(in_file);
  117.                 (*img).data[row][col].green = getc(in_file);
  118.                 (*img).data[row][col].blue = getc(in_file);
  119.             }
  120.     }
  121.  
  122.     fclose(in_file);
  123.     printf("\nDone reading file.\n");
  124. }
  125.  
  126.  
  127. void save(PGMImage *img)
  128. {
  129.     int i, j, nr, nc, k;
  130.     int red, green, blue;
  131.     FILE *iop;
  132.  
  133.     nr = img->height;
  134.     nc = img->width;
  135.  
  136.     iop = fopen("image1.pgm", "w");
  137.     fprintf(iop, "P6\n");
  138.     fprintf(iop, "%d %d\n", nc, nr);
  139.     fprintf(iop, "255\n");
  140.  
  141.     k = 1;
  142.     for(i = nr - 1; i  >= 0; i--)
  143.     {
  144.         for(j = 0; j <  nc; j++)
  145.         {
  146.             red = img->data[i][j].red;
  147.             green = img->data[i][j].green;
  148.             blue = img->data[i][j].blue;
  149.             if(red <  0)
  150.             {
  151.                 printf("IMG_WRITE: Found value %d at row %d col %d\n", red, i, j);
  152.                 printf("           Setting red to zero\n");
  153.                 red = 0;
  154.             }
  155.             if(green <  0)  
  156.             {
  157.                 printf("IMG_WRITE: Found value %d at row %d col %d\n", green,i, j);
  158.                 printf("           Setting green to zero\n");
  159.                 green = 0;
  160.             }
  161.             if(blue <  0)  
  162.             {
  163.                 printf("IMG_WRITE: Found value %d at row %d col %d\n", blue, i, j);
  164.                 printf("           Setting green to zero\n");
  165.                 blue = 0;
  166.             }
  167.             if(red  > 255)
  168.             {  
  169.                 printf("IMG_WRITE: Found value %d at row %d col %d\n", red, i, j);
  170.                 printf("           Setting red to 255\n");
  171.                 red = 255;
  172.             }
  173.             if(green  > 255)
  174.             {
  175.                 printf("IMG_WRITE: Found value %d at row %d col %d\n", green,i, j);
  176.                 printf("           Setting green to 255\n");
  177.                 green = 255;
  178.             }
  179.             if(blue  > 255)
  180.             {
  181.                 printf("IMG_WRITE: Found value %d at row %d col %d\n", blue, i, j);
  182.                 printf("           Setting blue to 255\n");
  183.                 blue = 255;
  184.             }
  185.  
  186.             putc(red, iop);
  187.             putc(green, iop);
  188.             putc(blue, iop);
  189.         }
  190.     }
  191.     fprintf(iop, "\n");
  192.     fclose(iop);
  193. }
  194.  
  195. void read_grid_from_file( int** grid, const size_t row, const size_t column, FILE* inf ) {
  196.     size_t x, y;
  197.     for( x = 0; x < column; ++x ) {
  198.         for( y = 0; y < row; ++y ) {
  199.             fscanf( inf, "%d", &grid[x][y] );
  200.             printf( "%d ", grid[x][y] );
  201.         }
  202.         printf( "\n" );
  203.     }
  204. }
  205.  
  206. int main(int argc, char* argv[])
  207. {
  208.     PGMImage img;
  209.     getPGMfile("progalap.pgm", &img);
  210.    
  211.     int x, y;
  212.     for(x = 0; x < img.width; x++)
  213.     {
  214.         for(y = 0; y < img.height; x++)
  215.         {
  216.             RGB_INT temp = {0};
  217.             memcpy(&temp, &img.data[x][y], sizeof(RGB_INT));
  218.             int index = img.height - y;
  219.             img.data[x][y] = img.data[index][y];
  220.             img.data[index][y] = temp;
  221.         }
  222.     }
  223.    
  224.     save(&img);
  225.     return 0;
  226.  
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement