Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. #include <stdio.h>
  2.   2
  3.   3 // Struct to hold each pixel value
  4.   4    struct pixel
  5.   5    {
  6.   6       unsigned char red;
  7.   7       unsigned char green;
  8.   8       unsigned char blue;
  9.   9    };
  10.  10
  11.  11 // Struct to hold the image type values
  12.  12    struct imageType
  13.  13    {
  14.  14       char type[3];
  15.  15       char comment[256];
  16.  16       int width, height;
  17.  17       int maxcolor;
  18.  18       struct pixel image[1000][1000];
  19.  19    };
  20.  20
  21.  21    int main ()
  22.  22    {
  23.  23
  24.  24 // Opening the input file, reading in image filenames during execution
  25.  25       FILE *inputfile;
  26.  26       char filename[30];
  27.  27       fprintf(stderr, "Enter the image file name.\n");
  28.  28       fscanf(stdin, "%s", filename);
  29.  29       inputfile=fopen(filename, "r");
  30.  30
  31.  31       struct imageType imageA;
  32.  32       char newlinechar;
  33.  33
  34.  34 // Reading in the image type values
  35.  35
  36.  36       fscanf(inputfile, "%[^\n]%c", imageA.type, &newlinechar);
  37.  37       fscanf(inputfile, "%[^\n]%c", imageA.comment, &newlinechar);
  38.  38       fscanf(inputfile, "%i", &imageA.width);
  39.  39       fscanf(inputfile, "%i", &imageA.height);
  40.  40       fscanf(inputfile, "%i", &imageA.maxcolor);
  41.  41
  42.  42 // Read in and store the pixel color values
  43.  43       int j,i;
  44.  44       for(j=0; j<=imageA.height-1; j++)
  45.  45       {
  46.  46          for(i=0; i<=imageA.width-1; i++)
  47.  47          {
  48.  48             fscanf(inputfile, "%hhu", &imageA.image[j][i].red);
  49.  49             fscanf(inputfile, "%hhu", &imageA.image[j][i].green);
  50.  50             fscanf(inputfile, "%hhu", &imageA.image[j][i].blue);
  51.  51          }
  52.  52       }
  53.  53
  54.  54 // closing the input file
  55.  55       fclose(inputfile);
  56.  56
  57.  57 // Opening the output file
  58.  58    FILE *outputfile;
  59.  59    outputfile=fopen("newimgBerg.ppm", "w");
  60.  60 // Creating the P6 image type
  61.  61    fprintf(outputfile, "P6\n");
  62.  62    fprintf(outputfile, "%s\n", imageA.comment);
  63.  63    fprintf(outputfile, "%i %i\n", imageA.width, imageA.height);
  64.  64    fprintf(outputfile, "%i\n", imageA.maxcolor);
  65.  65
  66.  66 //mirror image vertically
  67.  67
  68.  68
  69.  69 // Horizontal flip
  70.  70    for(j=0; j<=imageA.height-1; j++)
  71.  71       {
  72.  72          for(i=0; i<=imageA.width-1; i++)
  73.  73          {
  74.  74             struct pixel temp = imageA.image[j][i];
  75.  75             imageA.image[j][i]=imageA.image[imageA.height-(j+1)][i];
  76.  76             imageA.image[imageA.height-(j+1)][i]=temp;
  77.  77             fprintf(outputfile, "%c%c%c", imageA.image[j][i].red, imageA.image[j][i].green, imageA.image[j][i].blue);
  78.  78          }
  79.  79       }
  80.  80 // Closing the output file
  81.  81    fclose(outputfile);
  82.  82       return 0;
  83.  83    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement