aboutsummaryrefslogtreecommitdiffstats
path: root/src/glut/glx/glut_ppm.c
blob: 49dca8532fdb54597ca9a7875c6d7d23aba138da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * PPM file output
 * Brian Paul
 * 8 Dec 2008
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "glutint.h"


static void
write_ppm_file(const char *filename, const GLubyte *buffer,
               int width, int height)
{
   const int binary = 1;
   FILE *f = fopen( filename, "w" );
   if (f) {
      const GLubyte *ptr = buffer;
      int i, x, y;
      if (binary) {
         fprintf(f,"P6\n");
         fprintf(f,"# ppm-file created by GLUT\n");
         fprintf(f,"%i %i\n", width, height);
         fprintf(f,"255\n");
         fclose(f);
         f = fopen( filename, "ab" );  /* reopen in binary append mode */
         for (y = height - 1; y >= 0; y--) {
            for (x = 0; x < width; x++) {
               i = (y * width + x) * 4;
               fputc(ptr[i], f);   /* write red */
               fputc(ptr[i+1], f); /* write green */
               fputc(ptr[i+2], f); /* write blue */
            }
         }
      }
      else {
         /*ASCII*/
         int counter = 0;
         fprintf(f,"P3\n");
         fprintf(f,"# ascii ppm file created by GLUT\n");
         fprintf(f,"%i %i\n", width, height);
         fprintf(f,"255\n");
         for (y = height - 1; y >= 0; y--) {
            for (x = 0; x < width; x++) {
               i = (y * width + x) * 4;
               fprintf(f, " %3d %3d %3d", ptr[i], ptr[i+1], ptr[i+2]);
               counter++;
               if (counter % 5 == 0)
                  fprintf(f, "\n");
            }
         }
      }
      fclose(f);
   }
}


/**
 * Called from SwapBuffers if the GLUT_PPM_FILE env var is set.
 */
void __glutWritePPMFile(void)
{
   int w = glutGet(GLUT_WINDOW_WIDTH);
   int h = glutGet(GLUT_WINDOW_HEIGHT);
   GLubyte *buf;

   assert(__glutPPMFile);

   buf = (GLubyte *) malloc(w * h * 4);
   if (buf) {
      /* XXX save/restore pixel packing */
      glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buf);
      write_ppm_file(__glutPPMFile, buf, w, h);
      free(buf);
   }

   __glutPPMFile = NULL; /* only write one file */
}