Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PGM *pgm_read(const char *file) {
- int rows = 0, cols = 0;
- unsigned char byte;
- PGM *img;
- FILE *fp;
- int ptype;
- int max;
- if(!(fp = fopen(file, "r"))) {
- fprintf(stderr, "couldn't open file\n");
- return(NULL);
- }
- // READS IN HEADER INFO
- fscanf(fp, "P%d\n", &ptype);
- fscanf(fp, "%d %d\n", &cols, &rows);
- fscanf(fp, "%d\n", &max);
- if (!(img = pgm_alloc(rows, cols))) {
- fprintf(stderr, "couldn't allocate image\n");
- return (NULL);
- }
- img->h = rows;
- img->w = cols;
- img->max = max;
- img->ptype = ptype;
- //READS PIXELS
- for (int i = 0; i < cols; ++i) {
- for (int j = 0; j < rows; ++j) {
- fread(&byte, sizeof(unsigned char), 1, fp);
- img->gpix[i * cols + j] = (float)byte / (float)img->max;
- }
- }
- fclose(fp);
- return (img);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement