Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. //Name: image_read.c
  2. //Author: Price Poston
  3. //Purpose: To open an image file and read integer values.
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. void read_header(FILE *fp, int *x, int *y);
  8.  
  9. void read_image_body(FILE *fp, int **img);
  10.  
  11. int **read_image(char *img, int x, int y);
  12.  
  13. int get_image_pixel(int **img, int x, int y);
  14.  
  15. void set_image_pixel(int **img, int x, int y, int set);
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19. int x = 0, y = 0, **img;
  20. //FILE *fp;
  21. //fp = fopen(argv[1], "r+");
  22. /*if(fp == NULL)
  23. {
  24. printf("Failed to open file");
  25. return -1;//Checks to see if file was opened.
  26. }
  27. printf("file opened\n");*/
  28. img = read_image(argv[1], x, y);
  29. while((x!=-1)&&(y!=-1))
  30. {
  31. printf("Please enter a pixel value (x, y): ");
  32. scanf("%d%d", &x, &y);
  33. //printf("%d\n", img[x][y]);
  34. }
  35.  
  36. //fclose(fp);
  37. return 0;
  38. }
  39.  
  40. void read_header(FILE *fp, int *x, int *y)
  41. {
  42. int max;
  43. //Reads the header of the file.
  44. while(fgetc(fp)!='\n');
  45. while(fgetc(fp)!='\n');
  46. //fscanf(fp, "%d%d", x, y);
  47. fscanf(fp, "%d", x);
  48. fscanf(fp, "%d", y);
  49. }
  50.  
  51. void read_image_body(FILE *fp, int **img)
  52. {
  53. //Reads the body of the image.
  54. int col=0, row=0;
  55. read_header(fp, &col, &row);
  56. for(int i = 0;i<row;i++)
  57. {
  58. for(int j = 0; j<col;j++)
  59. {
  60. fscanf(fp, "%d", &img[i][j]);
  61. }
  62. }
  63. }
  64.  
  65. int **read_image(char *img, int x, int y)
  66. {
  67. //Reads the entire image file and returns the body of the image
  68. int **body;
  69. printf("read image\n");
  70. //body = malloc(sizeof(int)*24*7);
  71. FILE *fp;
  72. fp = fopen(img, "r");
  73. /*if(fp == NULL)
  74. {
  75. printf("file not opened\n");
  76. return &&-1;//Checks to see if file was opened.
  77. }*/
  78. printf("file opened\n");
  79. read_image_body(fp, body);
  80.  
  81. fclose(fp);
  82. return body;
  83. }
  84.  
  85. int get_image_pixel(int **img, int x, int y)
  86. {
  87. //gets the image pixel.
  88. return img[x][y];
  89. }
  90.  
  91. void set_image_pixel(int **img, int x, int y, int set)
  92. {
  93. //sets the image pixel.
  94. img[x][y] = 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement