Advertisement
okpalan

texture.c

Nov 9th, 2023 (edited)
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | Source Code | 0 0
  1.     #include <stdio.h>
  2.     #include <stdlib.h>
  3.  
  4.     typedef struct {
  5.         unsigned char r, g, b;
  6.     } pixel;
  7.  
  8.     int main() {
  9.         FILE *fp;
  10.         char buf[16];
  11.         int w, h, max;
  12.         pixel *image;
  13.  
  14.         fp = fopen("image.ppm", "r");
  15.         if (fp == NULL) {
  16.             fprintf(stderr, "Error: Could not open file\n");
  17.             exit(1);
  18.         }
  19.  
  20.         fgets(buf, sizeof(buf), fp);
  21.         if (buf[0] != 'P' || buf[1] != '6') {
  22.             fprintf(stderr, "Error: Invalid file format\n");
  23.             exit(1);
  24.         }
  25.  
  26.         fscanf(fp, "%d %d", &w, &h);
  27.         fscanf(fp, "%d", &max);
  28.  
  29.         if (max != 255) {
  30.             fprintf(stderr, "Error: Unsupported color depth\n");
  31.             exit(1);
  32.         }
  33.  
  34.         fgetc(fp); // skip the newline character
  35.  
  36.         image = (pixel*)malloc(w * h * sizeof(pixel));
  37.         if (image == NULL) {
  38.             fprintf(stderr, "Error: Out of memory\n");
  39.             exit(1);
  40.         }
  41.  
  42.         fread(image, sizeof(pixel), w * h, fp);
  43.         fclose(fp);
  44.  
  45.         // Render the image using OpenGL or other graphics library
  46.         // ...
  47.  
  48.         free(image);
  49.  
  50.         return 0;
  51.     }
  52.  
Tags: opengl ppm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement