Advertisement
Guest User

Untitled

a guest
Jan 25th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "linkedlist.h"
  4.  
  5. #define MAX_COMMENT_LENGTH 256
  6. #define MAX_COMMENTS 100
  7. #define MAX_IMAGE_WIDTH 600
  8. #define MAX_IMAGE_HEIGHT 600
  9.  
  10. struct Pixel {
  11. int r;
  12. int g;
  13. int b;
  14. } Pixel;
  15.  
  16. struct PPM {
  17. char format[2 + 1]; //2 letter code for PPM format, +1 for 2 spaces
  18. List *comments;
  19. int width; //number of columns
  20. int height; //number of rows
  21. int max; //maximum colour value (usually 255)
  22. struct Pixel **rgb;
  23. } PPM;
  24.  
  25. struct PPM * getPPM(FILE *fd);
  26. void showPPM(struct PPM *i);
  27.  
  28. int main() {
  29. FILE *file = fopen("black.ppm", "r");
  30. if(file == NULL) return 1;
  31. else {
  32. struct PPM *newPPM = getPPM(file);
  33. showPPM(newPPM);
  34. }
  35. return 0;
  36. }
  37.  
  38. struct PPM * getPPM(FILE *fd) {
  39. if(fd == NULL) {
  40. perror("File does not exist!");
  41. return NULL;
  42. }
  43.  
  44. struct PPM *newPPMFile = malloc(sizeof(PPM));
  45. List *commentList = List_create();
  46.  
  47. fscanf(fd, "%2c\n", newPPMFile->format);
  48. newPPMFile->format[2] = '\0'; //Need to end the string with \0
  49. //printf("%s\n", newPPMFile->format);
  50.  
  51. char comment[MAX_COMMENTS][MAX_COMMENT_LENGTH];
  52. int i = 0;
  53. while(fscanf(fd, "# %79[^\n] \n", comment[i]) == 1) {
  54. comment[i][MAX_COMMENT_LENGTH - 1] = '\0';
  55. List_append(commentList, comment[i]);
  56. i++;
  57. }
  58. newPPMFile->comments = commentList;
  59. //List_print(newPPMFile->comments);
  60.  
  61. //Read width and height
  62. if (fscanf(fd, "%d %d", &newPPMFile->width, &newPPMFile->height) != 2) {
  63. perror("Error reading width and height!");
  64. return NULL; //Error
  65. }
  66. //printf("%d %d\n", newPPMFile->width, newPPMFile->height);
  67.  
  68. //Read max rgb value
  69. if(fscanf(fd, "%d", &newPPMFile->max) != 1) {
  70. perror("Error reading max colour value!");
  71. return NULL; //Error
  72. }
  73. //printf("%d\n", newPPMFile->max);
  74.  
  75. //new array of width rows, height columns
  76. struct Pixel rgbArray[newPPMFile->height][newPPMFile->width];
  77. int x, y;
  78. for(y = 0; y < newPPMFile->height; y++) {
  79. for(x = 0; x < newPPMFile->width; x++) {
  80. struct Pixel newPixel;
  81. if (fscanf(fd, "%d%d%d\n", &newPixel.r, &newPixel.g, &newPixel.b) == 3) {
  82. rgbArray[y][x] = newPixel;
  83. //printf("%d %d %d\n", rgbArray[y][x].r, rgbArray[y][x].g, rgbArray[y][x].b);
  84. } else {
  85. //Handle error...
  86. perror("NO RGB??");
  87. }
  88. }
  89. }
  90. newPPMFile->rgb = rgbArray;
  91.  
  92. return newPPMFile;
  93. }
  94.  
  95. void showPPM(struct PPM *i) {
  96. /* Print PPM format */
  97. printf("%s\n", i->format);
  98.  
  99. /* Print comments */
  100. List_print(i->comments);
  101.  
  102. /* Print width and height */
  103. printf("%d %d\n", i->width, i->height);
  104.  
  105. /* Print max rgb value */
  106. printf("%d\n", i->max);
  107.  
  108. int x, y;
  109. for(y = 0; y < i->height; y++) {
  110. for(x = 0; x < i->width; x++) {
  111. struct Pixel newPixel = i->rgb[y][x];
  112. printf("%d %d %d\n", newPixel.r, newPixel.g, newPixel.b);
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement