• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

mem.c

Go to the documentation of this file.
00001 //
00002 // $Id: mem.c 11282 2010-10-28 16:26:09Z airwin $
00003 //
00004 // PLplot MEM (in user-supplied memory) device driver.
00005 // The idea here is that the user will specify the Y by X by RGB
00006 // area in which to plot using the plsmem function (added by me).
00007 //
00008 // This is a bare-bones driver which allows one to plot on an existing
00009 // image in memory.  This is useful if the user has an image in memory
00010 // that he wants to decorate with PLPLOT.
00011 //
00012 // Contributed by Doug Hunt
00013 // Included in PLplot by Rafael Laboissiere on Sat Feb 22 18:34:06 CET 2003
00014 //
00015 
00016 #include "plDevs.h"
00017 
00018 #ifdef PLD_mem
00019 
00020 #include "plplotP.h"
00021 #include "drivers.h"
00022 
00023 // Device info
00024 PLDLLIMPEXP_DRIVER const char* plD_DEVICE_INFO_mem = "mem:User-supplied memory device:-1:mem:46:mem\n";
00025 
00026 void plD_dispatch_init_mem( PLDispatchTable *pdt );
00027 
00028 void plD_init_mem( PLStream * );
00029 void plD_line_mem( PLStream *, short, short, short, short );
00030 void plD_polyline_mem( PLStream *, short *, short *, PLINT );
00031 void plD_eop_mem( PLStream * );
00032 void plD_bop_mem( PLStream * );
00033 void plD_tidy_mem( PLStream * );
00034 void plD_state_mem( PLStream *, PLINT );
00035 void plD_esc_mem( PLStream *, PLINT, void * );
00036 
00037 #undef MAX
00038 #undef ABS
00039 #define MAX( a, b )    ( ( a > b ) ? a : b )
00040 #define ABS( a )       ( ( a < 0 ) ? -a : a )
00041 
00042 #define MAX_INTENSITY    255
00043 
00044 void plD_dispatch_init_mem( PLDispatchTable *pdt )
00045 {
00046 #ifndef ENABLE_DYNDRIVERS
00047     pdt->pl_MenuStr = "User-supplied memory device";
00048     pdt->pl_DevName = "mem";
00049 #endif
00050     pdt->pl_type     = plDevType_Null;
00051     pdt->pl_seq      = 45;
00052     pdt->pl_init     = (plD_init_fp) plD_init_mem;
00053     pdt->pl_line     = (plD_line_fp) plD_line_mem;
00054     pdt->pl_polyline = (plD_polyline_fp) plD_polyline_mem;
00055     pdt->pl_eop      = (plD_eop_fp) plD_eop_mem;
00056     pdt->pl_bop      = (plD_bop_fp) plD_bop_mem;
00057     pdt->pl_tidy     = (plD_tidy_fp) plD_tidy_mem;
00058     pdt->pl_state    = (plD_state_fp) plD_state_mem;
00059     pdt->pl_esc      = (plD_esc_fp) plD_esc_mem;
00060 }
00061 
00062 //--------------------------------------------------------------------------
00063 // plD_init_mem()
00064 //
00065 // Initialize device (terminal).
00066 //--------------------------------------------------------------------------
00067 
00068 void
00069 plD_init_mem( PLStream *pls )
00070 {
00071     // plsmem must have already been called to set pls->dev to the
00072     // user supplied plotting area.  The dimensions of the plot area
00073     // have also been set by plsmem.  Verify this.
00074     //
00075 
00076     if ( ( pls->phyxma == 0 ) || ( pls->dev == NULL ) )
00077     {
00078         plexit( "Must call plsmem first to set user plotting area!" );
00079     }
00080 
00081     if ( pls->dev_mem_alpha == 1 )
00082     {
00083         plexit( "The mem driver does not support alpha values! Use plsmem!" );
00084     }
00085 
00086     plP_setpxl( (PLFLT) 4, (PLFLT) 4 ); // rough pixels/mm on *my* screen
00087 
00088 
00089     pls->color     = 1;         // Is a color device
00090     pls->dev_fill0 = 0;         // Handle solid fills
00091     pls->dev_fill1 = 0;         // Use PLplot core fallback for pattern fills
00092     pls->nopause   = 1;         // Don't pause between frames
00093 }
00094 
00095 #define sign( a )    ( ( a < 0 ) ? -1 : ( ( a == 0 ) ? 0 : 1 ) )
00096 
00097 // Modified version of the ljii routine (see ljii.c)
00098 void
00099 plD_line_mem( PLStream *pls, short x1a, short y1a, short x2a, short y2a )
00100 {
00101     int           i;
00102     PLINT         idx;
00103     int           x1 = x1a, y1 = y1a, x2 = x2a, y2 = y2a;
00104     PLINT         x1b, y1b, x2b, y2b;
00105     PLFLT         length, fx, fy, dx, dy;
00106     unsigned char *mem = (unsigned char *) pls->dev;
00107     PLINT         xm   = pls->phyxma;
00108     PLINT         ym   = pls->phyyma;
00109 
00110     // Take mirror image, since (0,0) must be at top left
00111 
00112     y1 = ym - ( y1 - 0 );
00113     y2 = ym - ( y2 - 0 );
00114 
00115     x1b    = x1, x2b = x2, y1b = y1, y2b = y2;
00116     length = (PLFLT) sqrt( (double)
00117         ( ( x2b - x1b ) * ( x2b - x1b ) + ( y2b - y1b ) * ( y2b - y1b ) ) );
00118 
00119     if ( length == 0. )
00120         length = 1.;
00121     dx = ( x2 - x1 ) / length;
00122     dy = ( y2 - y1 ) / length;
00123 
00124     fx = x1;
00125     fy = y1;
00126     mem[3 * xm * y1 + 3 * x1 + 0] = pls->curcolor.r;
00127     mem[3 * xm * y1 + 3 * x1 + 1] = pls->curcolor.g;
00128     mem[3 * xm * y1 + 3 * x1 + 2] = pls->curcolor.b;
00129 
00130     mem[3 * xm * y2 + 3 * x2 + 0] = pls->curcolor.r;
00131     mem[3 * xm * y2 + 3 * x2 + 1] = pls->curcolor.g;
00132     mem[3 * xm * y2 + 3 * x2 + 2] = pls->curcolor.b;
00133 
00134     for ( i = 1; i <= (int) length; i++ )
00135     {
00136         fx          += dx;
00137         fy          += dy;
00138         idx          = 3 * xm * (PLINT) fy + 3 * (PLINT) fx;
00139         mem[idx + 0] = pls->curcolor.r;
00140         mem[idx + 1] = pls->curcolor.g;
00141         mem[idx + 2] = pls->curcolor.b;
00142     }
00143 }
00144 
00145 void
00146 plD_polyline_mem( PLStream *pls, short *xa, short *ya, PLINT npts )
00147 {
00148     int i;
00149     for ( i = 0; i < npts - 1; i++ )
00150         plD_line_mem( pls, xa[i], ya[i], xa[i + 1], ya[i + 1] );
00151 }
00152 
00153 void
00154 plD_eop_mem( PLStream *pls )
00155 {
00156     // Set the 'dev' member (which holds the user supplied memory image)
00157     // to NULL here so it won't be freed when PLplot is closed.
00158     // (the user is responsible for freeing it when ready).
00159     //
00160     pls->dev = NULL;
00161 }
00162 
00163 void
00164 plD_bop_mem( PLStream *pls )
00165 {
00166 // Nothing to do here
00167 }
00168 
00169 void
00170 plD_tidy_mem( PLStream *pls )
00171 {
00172 // Nothing to do here
00173 }
00174 
00175 void
00176 plD_state_mem( PLStream *pls, PLINT op )
00177 {
00178 // Nothing to do here
00179 }
00180 
00181 void
00182 plD_esc_mem( PLStream *pls, PLINT op, void *ptr )
00183 {
00184 // Nothing to do here
00185 }
00186 
00187 #endif                          // PLD_mem

Generated on Wed Oct 12 2011 20:42:21 for PLplot by  doxygen 1.7.1