From e02ffc1226bdc741eed43e528ec0f0366c987fcc Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Mon, 6 Mar 2000 23:56:21 +0000 Subject: [PATCH] now save Targa image. misc clean-up --- progs/demos/osdemo.c | 141 +++++++++++++++++++++++++++---------------- 1 file changed, 89 insertions(+), 52 deletions(-) diff --git a/progs/demos/osdemo.c b/progs/demos/osdemo.c index 92bbd18491b..83f29af0703 100644 --- a/progs/demos/osdemo.c +++ b/progs/demos/osdemo.c @@ -1,4 +1,4 @@ -/* $Id: osdemo.c,v 1.2 2000/01/15 06:11:33 rjfrank Exp $ */ +/* $Id: osdemo.c,v 1.3 2000/03/06 23:56:21 brianp Exp $ */ /* * Demo of off-screen Mesa rendering @@ -17,27 +17,14 @@ */ -/* - * $Log: osdemo.c,v $ - * Revision 1.2 2000/01/15 06:11:33 rjfrank - * Added test for the occlusion test code. - * - * Revision 1.1.1.1 1999/08/19 00:55:40 jtg - * Imported sources - * - * Revision 3.0 1998/02/14 18:42:29 brianp - * initial rev - * - */ - - - #include #include #include "GL/osmesa.h" #include "GL/glut.h" +#define SAVE_TARGA + #define WIDTH 400 #define HEIGHT 400 @@ -143,65 +130,115 @@ static void render_image( void ) -int main( int argc, char *argv[] ) +static void +write_targa(const char *filename, const GLubyte *buffer, int width, int height) { - OSMesaContext ctx; - void *buffer; - - /* Create an RGBA-mode context */ - ctx = OSMesaCreateContext( GL_RGBA, NULL ); - - /* Allocate the image buffer */ - buffer = malloc( WIDTH * HEIGHT * 4 ); - - /* Bind the buffer to the context and make it current */ - OSMesaMakeCurrent( ctx, buffer, GL_UNSIGNED_BYTE, WIDTH, HEIGHT ); + FILE *f = fopen( filename, "w" ); + if (f) { + int i, x, y; + const GLubyte *ptr = buffer; + printf ("osdemo, writing tga file \n"); + fputc (0x00, f); /* ID Length, 0 => No ID */ + fputc (0x00, f); /* Color Map Type, 0 => No color map included */ + fputc (0x02, f); /* Image Type, 2 => Uncompressed, True-color Image */ + fputc (0x00, f); /* Next five bytes are about the color map entries */ + fputc (0x00, f); /* 2 bytes Index, 2 bytes length, 1 byte size */ + fputc (0x00, f); + fputc (0x00, f); + fputc (0x00, f); + fputc (0x00, f); /* X-origin of Image */ + fputc (0x00, f); + fputc (0x00, f); /* Y-origin of Image */ + fputc (0x00, f); + fputc (WIDTH & 0xff, f); /* Image Width */ + fputc ((WIDTH>>8) & 0xff, f); + fputc (HEIGHT & 0xff, f); /* Image Height */ + fputc ((HEIGHT>>8) & 0xff, f); + fputc (0x18, f); /* Pixel Depth, 0x18 => 24 Bits */ + fputc (0x20, f); /* Image Descriptor */ + fclose(f); + f = fopen( filename, "ab" ); /* reopen in binary append mode */ + for (y=height-1; y>=0; y--) { + for (x=0; x1) { - /* write PPM file */ - FILE *f = fopen( argv[1], "w" ); - if (f) { - int i, x, y; - GLubyte *ptr = (GLubyte *) buffer; -#define BINARY 0 -#if BINARY +static void +write_ppm(const char *filename, const GLubyte *buffer, int width, int height) +{ + const int binary = 0; + FILE *f = fopen( filename, "w" ); + if (f) { + int i, x, y; + const GLubyte *ptr = buffer; + if (binary) { fprintf(f,"P6\n"); - fprintf(f,"# ppm-file created by %s\n", argv[0]); - fprintf(f,"%i %i\n", WIDTH,HEIGHT); + fprintf(f,"# ppm-file created by osdemo.c\n"); + fprintf(f,"%i %i\n", width,height); fprintf(f,"255\n"); fclose(f); - f = fopen( argv[1], "ab" ); /* reopen in binary append mode */ - for (y=HEIGHT-1; y>=0; y--) { - for (x=0; x=0; y--) { + for (x=0; x=0; y--) { - for (x=0; x=0; y--) { + for (x=0; x1) { +#ifdef SAVE_TARGA + write_targa(argv[1], buffer, WIDTH, HEIGHT); +#else + write_ppm(argv[1], buffer, WIDTH, HEIGHT); +#endif } else { - printf("Specify a filename if you want to make a ppm file\n"); + printf("Specify a filename if you want to make an image file\n"); } printf("all done\n"); -- 2.30.2