Function:ss2gpix

From S2PLOT

Jump to: navigation, search

ss2gpix

Fetch the current (r,g,b) buffer.

Prototype

unsigned char *ss2gpix(unsigned int *width, unsigned int *height);

Description

Fetch the current frame image to an RGB buffer. The buffer is of length height * width * 3 bytes, with each pixel an RGB triplet with colour values between 0 and 255. Note that this is not the same as other texture buffers in S2PLOT, which have an alpha channel.

Pixels are returned in row order from the lowest to the highest row, left to right in each row. The first pixel is the bottom left corner of the image. This function should only be used in non-stereo modes. The user is responsible for freeing the memory returned.


See Also

ss2ctCreate a texture with specified height and width.

Code Example

#include <stdio.h>
#include <stdlib.h>
#include "s2plot.h"

void cb(double *t, int *kc)
{
   unsigned int sw, sh;                 /* Screen buffer size */
   int w, h;                            /* Texture size: square texture */
   static unsigned char *screen = NULL; /* Memory for RGB pixel map */
   static char *texture = NULL;         /* Billboard texture to make */
   int i, j;                            /* Loop variables */
   static int id = -1;                  /* ID of texture: default value */

   COLOUR col = { 1, 1, 1 };            /* Billboard texture colour */
   XYZ pos = { 0, 0, 0 };               /* Billboard texture location */
   XYZ is  = { 0, 0, 0 };               /* Billboard stretch factor */
   int idx1, idx2;                      /* Index into texture arrays */

   screen = ss2gpix(&sw, &sh);          /* Capture the screen */

   if (sw < sh) { h = sw; w = sw; }     /* Choose a square region */
   else { w = sh; h = sh; }

   int dx = (sw - w)/2;                 /* Find offset from screen centre */
   int dy = (sh - h)/2;

   id = ss2ct(w,h);                     /* Create a new texture */

   texture = ss2gt(id, &w, &h);         /* Get memory for this texture */

   for (i=0;i<h;i++) {                  /* Loop over height */
      for (j=0;j<w;j++) {               /* Loop over width */
         idx1 = (j*w + i)*4;            /* Index to write into new texture */
         idx2 = ((j+dy)*sw + (i+dx))*3; /* Index to copy from screen texture */
         texture[idx1  ] = screen[idx2  ];
         texture[idx1+1] = screen[idx2+1];
         texture[idx1+2] = screen[idx2+2];
      }
   }
   ss2pt(id);                           /* Restore the texture for use */

   ss2tsc("c");                         /* Change to screen coordinates */
   float xmin, xmax, ymin, ymax;        /* Boundaries of window */
   xmin = 0.01; xmax = 0.99;
   ymin = 0.01; ymax = 0.99;

   float ar = ss2qar();                 /* Get screen aspect ratio */
   if (ar > 1.0) {                      /* Fiddle coordinates to get */
      xmin = 0.5-(0.49)/ar;             /*  square region in window centre */
      xmax = 0.5+(0.49)/ar;
   } else {
      ymin = 0.5-(0.49)*ar;
      ymax = 0.5+(0.49)*ar;
   }
                                        /* Draw square with thick lines */
   ns2thline(xmin,ymin,0.01, xmax,ymin,0.01, 1,0,1, 3);
   ns2thline(xmin,ymax,0.01, xmax,ymax,0.01, 1,0,1, 3);
   ns2thline(xmin,ymin,0.01, xmin,ymax,0.01, 1,0,1, 3);
   ns2thline(xmax,ymin,0.01, xmax,ymax,0.01, 1,0,1, 3);
   ss2tsc("");                          /* Return to world coordinates */

   ds2vbb(pos, is, 0.05, col, id, 1.0, 'o');    /* Draw billboard texture */

   if (screen != NULL) {                /* Free screen texture memory */
      free(screen);
      screen = NULL;
   }
   if (texture != NULL) {               /* Free billboard texture memory */
      free(texture);
      texture = NULL;
   }

}

int main(int argc, char *argv[])
{
   fprintf(stderr,"Use +/- to stare infinity in the face...\n");

   s2opend("/s2mono", argc, argv);              /* Open in mono mode */
   s2swin(-1.,1., -1.,1., -1.,1.);              /* Set window coordinates */
   s2box("BCDET",0,0,"BCDET",0,0,"BCDET",0,0);  /* Draw coordinate box */

   cs2scb(&cb);                                 /* Install dynamic callback */

   s2show(1);                                   /* Open the s2plot window */

   return 1;
}

Back to S2PLOT function list.


Personal tools