Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. int ReadImage (Image *I, char* FileName){
  2. /* Function: Reads in the image I (graylevel or color) from File
  3. The image header (the second of the file) might
  4. include a comment line, e.g. "# Created by Paint Shop Pro 7"
  5. Your code should be able to ignore it.
  6. Preconditions: None
  7. Postconditions: All the image pixels are read in and the image
  8. width and height recorded in the appropriate fields.
  9. If the image is a gray level one, then all the three
  10. color values of each pixel is set to intensity value of
  11. that pixel. It returns 1 for success and 0 for failure.
  12. ---------------------------------------------------------------------*/
  13.  
  14. int cnt;
  15. int Height = 0, Width = 0, ColorCap = 0;
  16. char str[80];
  17. FILE *fp;
  18.  
  19. fp = fopen(FileName, "r");
  20. fscanf(fp, "%s", str);
  21.  
  22. if (strcmp (str, "P6") == 0) {
  23. fscanf (fp, "%s", str);
  24. if (str[0] == '#') {//Comment line
  25. while (getc(fp) != '\n') {};
  26. fscanf(fp, "%s", str);
  27. Width = atoi(str);
  28. I->Width = Width;
  29. fscanf(fp, "%s", str);
  30. Height = atoi(str);
  31. I->Height = Height;
  32. }
  33. else {
  34. Width = atoi(str);
  35. fscanf(fp, "%s", str);
  36. }
  37. fscanf(fp, "%s", str);
  38. ColorCap = atoi(str);
  39.  
  40. cnt = 0;
  41. while(cnt < Height*Width*3){
  42. I->Value[cnt] = (unsigned int) (getc(fp));
  43. cnt++;
  44. }
  45.  
  46. fclose(fp);
  47.  
  48. return 1;
  49. }
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement