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