Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #define LINE_SIZE 32
- /* CP2 Laboratory number 3
- * Library functions and binary files
- *
- * TASKS:
- *
- * 0. Open the documentation of the stdio.h, stdlib.h and string.h libraries.
- *
- * 1. Read about the PPM graphics format - it is very simple: the header contains only three lines of text:
- * P6
- * [width] [height]
- * [size]
- * where [width] and [height] are strings containing image dimensions and [size] is the maximal number of pixel value - in this case it is 255;
- * after the header there is a raster of pixels - written as binary data: [r][g][b] - one byte of red color, one byte of green and one byte of blue color - and these repeat for each pixel.
- *
- * 2. Try to open the "image.ppm" file in your operating system - if you do not have an appropriate application, you can use the online converters to convert it to other more popular formats;
- * try to open the file with a hex editor or even the text editor to see its contents - the first three lines should containt the aforementioned header.
- *
- * 3. Read about converting the colorful image (in RGB format) to a grayscale image - the operation consists of calcuating the average.
- *
- * 4. Write a program that converts the given PPM file containing a colorful image into the PPM file that contains a grayscale image - use the hints inside the main function.
- *
- * 5. Use the online converter once again to see the result if you have problems with opening the resulting file in your computer.
- */
- int main()
- {
- /*
- a) open the "image.ppm" file in a binary mode for reading and open the "result.ppm" file in binary mode for writing;
- b) check whether the "image.ppm" file exists - print a message and exit the program if not;
- */
- bool check_1;
- FILE *fp;
- FILE *fp2;
- fp = fopen("C:\\Users\\pumpk\\Desktop\\git\\4156b601-gr13-repo\\lab3\\image.ppm", "r");
- fp2 = fopen("C:\\Users\\pumpk\\Desktop\\git\\4156b601-gr13-repo\\lab3\\result.ppm", "w");
- if(fp) {
- printf("File found, opening...\n");
- check_1 = true;
- }
- else
- printf("No file found, sorry but we are closing\n");
- if(check_1 && fp2)
- printf("New file created\n");
- // this would be a buffer for each line of the header
- char line[LINE_SIZE];
- char chunk[128];
- fgets(line, chunk, fp);
- printf("%s", line);
- fputs(line, fp2);
- fgets(line, chunk, fp);
- printf("%s",line);
- fputs(line, fp2);
- char width[50];
- char height[50];
- strcpy(width, strtok(line, " "));
- strcpy(height, strtok(NULL," "));
- printf("%s - %s", width, height);
- int w = atoi(width);
- int h = atoi(height);
- printf("%d - %d\n",w,h);
- fgets(line, chunk, fp);
- printf("%s", line);
- fputs(line, fp2);
- /*
- c) read one line from the "image.ppm" file - use "fgets" function and the "line" buffer created above;
- d) write that line to the "result.ppm" file - use "fputs" funcion;
- e) read another line from the "image.ppm" file - it should contain the image dimensions;
- f) write that line to the "result.ppm" file;
- g) parse that line using the "strtok" function - your task is to split it into 2 substrings containing the [width] and [height] values;
- h) convert these substrings into integers - use the "atoi" function - name the variables "w" and "h" - they would be required in the loops below;
- i) read another line from the "image.ppm" file - it is the last line of the header
- j) write that line to the "result.ppm" file
- */
- // an array for one pixel (three bytes: [r][g][b])
- unsigned char rgb[3];
- // one byte for averaged grayscale value
- unsigned char gray;
- // first iterate over height
- for (int i = 0; i < h; ++i)
- {
- // than iterate over width
- for (int j = 0; j < w; ++j)
- {
- //Something is wrong here
- fread(rgb, 3, 1,fp);
- gray = (rgb[0] + rgb[1] + rgb[2])/3;
- memset(rgb, gray, sizeof(rgb));
- fwrite(&rgb, 3, 1, fp2);
- //k) read from the "image.ppm" file the whole "rgb" array using the "fread" function - it reads binary data from a file;
- //l) calculate the average of three RGB components - warning: use "int" and "double" for calculations, because "unsigned char" is not sufficient;
- //m) cast the average (double value) to the "gray" variable;
- //n) write the "gray" variable 3 times into the "result.ppm" file - the same value is used for all pixels - use the "fwrite" function that writes binary data to a file;
- }
- }
- /*
- o) close the two files;
- */
- fclose(fp);
- fclose(fp2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement