Advertisement
pushrbx

Untitled

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