14 write_ppm_file(const char *filename
, const GLubyte
*buffer
,
15 int width
, int height
)
18 FILE *f
= fopen( filename
, "w" );
20 const GLubyte
*ptr
= buffer
;
24 fprintf(f
,"# ppm-file created by GLUT\n");
25 fprintf(f
,"%i %i\n", width
, height
);
28 f
= fopen( filename
, "ab" ); /* reopen in binary append mode */
29 for (y
= height
- 1; y
>= 0; y
--) {
30 for (x
= 0; x
< width
; x
++) {
31 i
= (y
* width
+ x
) * 4;
32 fputc(ptr
[i
], f
); /* write red */
33 fputc(ptr
[i
+1], f
); /* write green */
34 fputc(ptr
[i
+2], f
); /* write blue */
42 fprintf(f
,"# ascii ppm file created by GLUT\n");
43 fprintf(f
,"%i %i\n", width
, height
);
45 for (y
= height
- 1; y
>= 0; y
--) {
46 for (x
= 0; x
< width
; x
++) {
47 i
= (y
* width
+ x
) * 4;
48 fprintf(f
, " %3d %3d %3d", ptr
[i
], ptr
[i
+1], ptr
[i
+2]);
61 * Called from SwapBuffers if the GLUT_PPM_FILE env var is set.
63 void __glutWritePPMFile(void)
65 int w
= glutGet(GLUT_WINDOW_WIDTH
);
66 int h
= glutGet(GLUT_WINDOW_HEIGHT
);
69 assert(__glutPPMFile
);
71 buf
= (GLubyte
*) malloc(w
* h
* 4);
73 /* XXX save/restore pixel packing */
74 glReadPixels(0, 0, w
, h
, GL_RGBA
, GL_UNSIGNED_BYTE
, buf
);
75 write_ppm_file(__glutPPMFile
, buf
, w
, h
);
79 __glutPPMFile
= NULL
; /* only write one file */