Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct {
- unsigned char r, g, b;
- } pixel;
- int main() {
- FILE *fp;
- char buf[16];
- int w, h, max;
- pixel *image;
- fp = fopen("image.ppm", "r");
- if (fp == NULL) {
- fprintf(stderr, "Error: Could not open file\n");
- exit(1);
- }
- fgets(buf, sizeof(buf), fp);
- if (buf[0] != 'P' || buf[1] != '6') {
- fprintf(stderr, "Error: Invalid file format\n");
- exit(1);
- }
- fscanf(fp, "%d %d", &w, &h);
- fscanf(fp, "%d", &max);
- if (max != 255) {
- fprintf(stderr, "Error: Unsupported color depth\n");
- exit(1);
- }
- fgetc(fp); // skip the newline character
- image = (pixel*)malloc(w * h * sizeof(pixel));
- if (image == NULL) {
- fprintf(stderr, "Error: Out of memory\n");
- exit(1);
- }
- fread(image, sizeof(pixel), w * h, fp);
- fclose(fp);
- // Render the image using OpenGL or other graphics library
- // ...
- free(image);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement