Guest User

Untitled

a guest
Jan 20th, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define COMMENT_LENGTH 256
  6. #define COMMENT_ARRAY_SIZE 10
  7. #define MAX_PIXEL_HEIGHT 480
  8. #define MAX_PIXEL_WIDTH 640
  9.  
  10. struct PPM {
  11. char format[2 + 1]; //2 letter code for PPM format, +1 for 2 spaces
  12. char comments[COMMENT_LENGTH + 1][COMMENT_ARRAY_SIZE]; //comment array - n+1 size array to store n values
  13. int width; //number of columns
  14. int height; //number of rows
  15. int max; //maximum colour value (usually 255)
  16. int rgbPixels[MAX_PIXEL_WIDTH * MAX_PIXEL_HEIGHT][3]; //integers between 0 and max for pixel i's RGB values
  17. }PPM;
  18.  
  19. struct PPM * getPPM(FILE *fd);
  20.  
  21. void showPPM(struct PPM *i);
  22.  
  23. int main(int argc, char **argv) {
  24. FILE *file = fopen("ape.ppm", "r");
  25. if(file == NULL) return 0;
  26. else {
  27. struct PPM *newPPM = getPPM(file);
  28.  
  29. if(newPPM == NULL) return 1;
  30.  
  31. printf("\n");
  32.  
  33. showPPM(newPPM);
  34.  
  35. return 0;
  36. }
  37. }
  38.  
  39. struct PPM * getPPM(FILE *fd) {
  40. if(fd == NULL) {
  41. return NULL;
  42. }
  43.  
  44. struct PPM *newPPMFile = /*(PPM *)*/ malloc(sizeof(PPM)); //casting result of malloc can potentially mask errors and is unnecessary
  45.  
  46. fscanf(fd, "%2s\n", newPPMFile->format);
  47. //printf("%2s\n", newPPMFile->format);
  48.  
  49. //check for comments
  50. int i = 0;
  51. while(i < COMMENT_ARRAY_SIZE && fscanf(fd, "# %79[^\n] \n", newPPMFile->comments[i]) == 1) {
  52. //printf("%s\n", newPPMFile->comments[i]);
  53. i++;
  54. }
  55.  
  56. //read width and height
  57. //fscanf(fd, "%d %d", &newPPMFile->width, &newPPMFile->height);
  58. if (fscanf(fd, "%d %d", &newPPMFile->width, &newPPMFile->height) != 2) {
  59. //printf("%s\n", "Error reading width & height\n");
  60. return NULL;
  61. }
  62. //printf("%d %d\n", newPPMFile->width, newPPMFile->height);
  63.  
  64. //read max
  65. if(fscanf(fd, "%d", &newPPMFile->max) != 1) {
  66. //printf("%s\n", "Error reading max\n");
  67. return NULL;
  68. }
  69. //printf("%d\n", newPPMFile->max);
  70.  
  71. //read rgb data in rgb array
  72. int counter = 0;
  73. while(counter < (MAX_PIXEL_HEIGHT * MAX_PIXEL_WIDTH) &&
  74. fscanf(fd, "%d\n%d\n%d\n", &newPPMFile->rgbPixels[counter][0], &newPPMFile->rgbPixels[counter][1], &newPPMFile->rgbPixels[counter][2]) != EOF) {
  75. //printf("%d %d %d\n", newPPMFile->rgbPixels[counter][0], newPPMFile->rgbPixels[counter][1], newPPMFile->rgbPixels[counter][2]);
  76. counter++;
  77. }
  78.  
  79. //close file
  80. fclose(fd);
  81.  
  82. //free unused memory??
  83. //free(newPPMFile);
  84.  
  85. return newPPMFile;
  86. }
  87.  
  88. void showPPM(struct PPM *i) {
  89. printf("%s\n", i->format);
  90.  
  91. //print comments array
  92. int x;
  93. for(x = 0; x < sizeof(i->comments) / sizeof(i->comments[0]); x++) {
  94. printf("# %s\n", i->comments[x]);
  95. }
  96.  
  97. printf("%d %d\n", i->width, i->height);
  98. printf("%d\n", i->max);
  99.  
  100. //print rgb values
  101. for(x = 0; x < 500; x++) {
  102. printf("%d %d %d\n", i->rgbPixels[x][0], i->rgbPixels[x][1], i->rgbPixels[x][2]);
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment