Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Name: image_read.c
- //Author: Price Poston
- //Purpose: To open an image file and read integer values.
- #include <stdio.h>
- #include <stdlib.h>
- void read_header(FILE *fp, int *x, int *y);
- void read_image_body(FILE *fp, int **img);
- int **read_image(char *img, int x, int y);
- int get_image_pixel(int **img, int x, int y);
- void set_image_pixel(int **img, int x, int y, int set);
- int main(int argc, char *argv[])
- {
- int x = 0, y = 0, **img;
- //FILE *fp;
- //fp = fopen(argv[1], "r+");
- /*if(fp == NULL)
- {
- printf("Failed to open file");
- return -1;//Checks to see if file was opened.
- }
- printf("file opened\n");*/
- img = read_image(argv[1], x, y);
- while((x!=-1)&&(y!=-1))
- {
- printf("Please enter a pixel value (x, y): ");
- scanf("%d%d", &x, &y);
- //printf("%d\n", img[x][y]);
- }
- //fclose(fp);
- return 0;
- }
- void read_header(FILE *fp, int *x, int *y)
- {
- int max;
- //Reads the header of the file.
- while(fgetc(fp)!='\n');
- while(fgetc(fp)!='\n');
- //fscanf(fp, "%d%d", x, y);
- fscanf(fp, "%d", x);
- fscanf(fp, "%d", y);
- }
- void read_image_body(FILE *fp, int **img)
- {
- //Reads the body of the image.
- int col=0, row=0;
- read_header(fp, &col, &row);
- for(int i = 0;i<row;i++)
- {
- for(int j = 0; j<col;j++)
- {
- fscanf(fp, "%d", &img[i][j]);
- }
- }
- }
- int **read_image(char *img, int x, int y)
- {
- //Reads the entire image file and returns the body of the image
- int **body;
- printf("read image\n");
- //body = malloc(sizeof(int)*24*7);
- FILE *fp;
- fp = fopen(img, "r");
- /*if(fp == NULL)
- {
- printf("file not opened\n");
- return &&-1;//Checks to see if file was opened.
- }*/
- printf("file opened\n");
- read_image_body(fp, body);
- fclose(fp);
- return body;
- }
- int get_image_pixel(int **img, int x, int y)
- {
- //gets the image pixel.
- return img[x][y];
- }
- void set_image_pixel(int **img, int x, int y, int set)
- {
- //sets the image pixel.
- img[x][y] = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement