Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include <stdbool.h>
  6.  
  7. #define LINE_SIZE 32
  8.  
  9. /* CP2 Laboratory number 3
  10. * Library functions and binary files
  11. *
  12. * TASKS:
  13. *
  14. * 0. Open the documentation of the stdio.h, stdlib.h and string.h libraries.
  15. *
  16. * 1. Read about the PPM graphics format - it is very simple: the header contains only three lines of text:
  17. * P6
  18. * [width] [height]
  19. * [size]
  20. * where [width] and [height] are strings containing image dimensions and [size] is the maximal number of pixel value - in this case it is 255;
  21. * 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.
  22. *
  23. * 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;
  24. * 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.
  25. *
  26. * 3. Read about converting the colorful image (in RGB format) to a grayscale image - the operation consists of calcuating the average.
  27. *
  28. * 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.
  29. *
  30. * 5. Use the online converter once again to see the result if you have problems with opening the resulting file in your computer.
  31. */
  32.  
  33. int main()
  34. {
  35. /*
  36. a) open the "image.ppm" file in a binary mode for reading and open the "result.ppm" file in binary mode for writing;
  37. b) check whether the "image.ppm" file exists - print a message and exit the program if not;
  38. */
  39. bool check_1;
  40. FILE *fp;
  41. FILE *fp2;
  42. fp = fopen("C:\\Users\\pumpk\\Desktop\\git\\4156b601-gr13-repo\\lab3\\image.ppm", "r");
  43. fp2 = fopen("C:\\Users\\pumpk\\Desktop\\git\\4156b601-gr13-repo\\lab3\\result.ppm", "w");
  44.  
  45. if(fp) {
  46. printf("File found, opening...\n");
  47. check_1 = true;
  48. }
  49. else
  50. printf("No file found, sorry but we are closing\n");
  51.  
  52. if(check_1 && fp2)
  53. printf("New file created\n");
  54.  
  55. // this would be a buffer for each line of the header
  56. char line[LINE_SIZE];
  57. char chunk[128];
  58. fgets(line, chunk, fp);
  59. printf("%s", line);
  60. fputs(line, fp2);
  61. fgets(line, chunk, fp);
  62. printf("%s",line);
  63. fputs(line, fp2);
  64. char width[50];
  65. char height[50];
  66. strcpy(width, strtok(line, " "));
  67. strcpy(height, strtok(NULL," "));
  68. printf("%s - %s", width, height);
  69. int w = atoi(width);
  70. int h = atoi(height);
  71. printf("%d - %d\n",w,h);
  72. fgets(line, chunk, fp);
  73. printf("%s", line);
  74. fputs(line, fp2);
  75. /*
  76. c) read one line from the "image.ppm" file - use "fgets" function and the "line" buffer created above;
  77. d) write that line to the "result.ppm" file - use "fputs" funcion;
  78. e) read another line from the "image.ppm" file - it should contain the image dimensions;
  79. f) write that line to the "result.ppm" file;
  80. g) parse that line using the "strtok" function - your task is to split it into 2 substrings containing the [width] and [height] values;
  81. h) convert these substrings into integers - use the "atoi" function - name the variables "w" and "h" - they would be required in the loops below;
  82. i) read another line from the "image.ppm" file - it is the last line of the header
  83. j) write that line to the "result.ppm" file
  84. */
  85.  
  86. // an array for one pixel (three bytes: [r][g][b])
  87. unsigned char rgb[3];
  88.  
  89. // one byte for averaged grayscale value
  90. unsigned char gray;
  91.  
  92. // first iterate over height
  93. for (int i = 0; i < h; ++i)
  94. {
  95. // than iterate over width
  96. for (int j = 0; j < w; ++j)
  97. {
  98. //Something is wrong here
  99. fread(rgb, 3, 1,fp);
  100. gray = (rgb[0] + rgb[1] + rgb[2])/3;
  101. memset(rgb, gray, sizeof(rgb));
  102. fwrite(&rgb, 3, 1, fp2);
  103.  
  104. //k) read from the "image.ppm" file the whole "rgb" array using the "fread" function - it reads binary data from a file;
  105. //l) calculate the average of three RGB components - warning: use "int" and "double" for calculations, because "unsigned char" is not sufficient;
  106. //m) cast the average (double value) to the "gray" variable;
  107. //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;
  108.  
  109. }
  110. }
  111.  
  112. /*
  113. o) close the two files;
  114. */
  115. fclose(fp);
  116. fclose(fp2);
  117.  
  118. return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement