Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.80 KB | None | 0 0
  1. void readInput(const char * fileName, image *img) {
  2.     FILE *in;
  3.     in = fopen(fileName, "rb");
  4.     char  type[2];
  5.  
  6.     fscanf(in, "%s\n", type);
  7.     if (strcmp(type, "P5\n")) {
  8.         img->type = 5;
  9.     }
  10.     else {
  11.         img->type = 6;
  12.     }
  13.     fscanf(in, "%d %d\n", &(*img).height, &(*img).width);
  14.     fscanf(in, "%d\n", &(*img).maxval);
  15.  
  16.     unsigned char** image;
  17.  
  18.     if (img->type == 5) {
  19.         //P5
  20.         image = malloc(img->height * sizeof(char*));
  21.         for (int i = 0; i < img->height; i++) {
  22.             image[i] = malloc(img->width * sizeof(char));
  23.             fread(image[i], 1, img->width, in);
  24.         }
  25.     }
  26.     else {
  27.         //P6
  28.         image = malloc(img->height * sizeof(char*));
  29.         for (int i = 0; i < img->height; i++) {
  30.             image[i] = malloc(3 * img->width * sizeof(char));
  31.             fread(image[i], 3, img->width, in);
  32.         }
  33.     }
  34.     img->image = image;
  35.     fclose(in);
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement