Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.68 KB | None | 0 0
  1. #include "ppm.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. PPM *ppm_alloc(int r, int c) {
  6.   PPM *img;
  7.  
  8.   img = (PPM *)malloc(sizeof(PPM));
  9.   img->ptype = 6;
  10.   img->w = c;
  11.   img->h = r;
  12.   img->bpp = 3;
  13.   img->max = 255;
  14.   img->name = NULL;
  15.  
  16.   img->rpix = (float *)malloc(r * c * sizeof(float));
  17.   img->gpix = (float *)malloc(r * c * sizeof(float));
  18.   img->bpix = (float *)malloc(r * c * sizeof(float));
  19.  
  20.   if (!(img->rpix = (float *)malloc(r * c * sizeof(float)))) {
  21.     fprintf(stderr, "out of memory\n");
  22.     return NULL;
  23.   }
  24.  
  25.   if (!(img->gpix = (float *)malloc(r * c * sizeof(float)))) {
  26.     fprintf(stderr, "out of memory\n");
  27.     return NULL;
  28.   }
  29.  
  30.   if (!(img->bpix = (float *)malloc(r * c * sizeof(float)))) {
  31.     fprintf(stderr, "out of memory\n");
  32.     return NULL;
  33.   }
  34.  
  35.   return img;
  36. }
  37.  
  38. void ppm_free(PPM **img) {
  39.   if ((*img)) {
  40.  
  41.     if ((*img)->name)
  42.       free((*img)->name);
  43.     if ((*img)->rpix)
  44.       free((*img)->rpix);
  45.     if ((*img)->gpix)
  46.       free((*img)->gpix);
  47.     if ((*img)->bpix)
  48.       free((*img)->bpix);
  49.  
  50.     free(*img);
  51.   }
  52.  
  53.   (*img) = NULL;
  54. }
  55.  
  56. PPM *ppm_read(const char *file) {
  57.   int rows = 0, cols = 0, ptype, max;
  58.   unsigned char byte;
  59.   PPM *img;
  60.   FILE *fp;
  61.  
  62.   if(!(fp = fopen(file, "r"))) {
  63.       fprintf(stderr, "couldn't open file\n");
  64.       return(NULL);
  65.   }
  66.  
  67.   fscanf(fp, "P%d\n", &ptype);
  68.   fscanf(fp, "%d %d\n", &cols, &rows);
  69.   fscanf(fp, "%d\n", &max);
  70.  
  71.   if (!(img = ppm_alloc(rows, cols))) {
  72.     fprintf(stderr, "couldn't allocate image\n");
  73.     return (NULL);
  74.   }
  75.  
  76.   img->h = rows;
  77.   img->w = cols;
  78.   img->max = max;
  79.   img->ptype = ptype;
  80.  
  81.  
  82.   for (int i = 0; i < cols; ++i) {
  83.     for (int j = 0; j < rows; ++j) {
  84.       fread(&byte, sizeof(unsigned char), 1, fp);
  85.       img->rpix[i * cols + j] = (float)byte / (float)img->max;
  86.       img->gpix[i * cols + j] = (float)byte / (float)img->max;
  87.       img->bpix[i * cols + j] = (float)byte / (float)img->max;
  88.     }
  89.   }
  90.  
  91.     fclose(fp);
  92.   return (img);
  93. }
  94.  
  95. void ppm_write(PPM *img, const char *file) {
  96.   int rows = img->h, cols = img->w;
  97.   FILE *fp = fopen(file, "w");
  98.   unsigned char byte;
  99.  
  100.   fprintf(fp, "P%d\n", img->ptype);
  101.   fprintf(fp, "#made by Lemuel Reid\n");
  102.   fprintf(fp, "%d %d\n", img->w, img->h);
  103.   fprintf(fp, "%d\n", img->max);
  104.  
  105.   // write pixels
  106.   for (int i = 0; i < rows; i++) {
  107.     for (int j = 0; j < cols; ++j) {
  108.       byte = (unsigned char)(img->rpix[i * img->w + j] * (float)img->max);
  109.       byte = (unsigned char)(img->gpix[i * img->w + j] * (float)img->max);
  110.       byte = (unsigned char)(img->bpix[i * img->w + j] * (float)img->max);
  111.       fwrite(&byte, sizeof(unsigned char), 1, fp);
  112.     }
  113.   }
  114.  
  115.   fclose(fp);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement