PLplot  5.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
mem.c
Go to the documentation of this file.
1 //
2 // $Id: mem.c 12008 2011-10-28 12:50:46Z andrewross $
3 //
4 // PLplot MEM (in user-supplied memory) device driver.
5 // The idea here is that the user will specify the Y by X by RGB
6 // area in which to plot using the plsmem function (added by me).
7 //
8 // This is a bare-bones driver which allows one to plot on an existing
9 // image in memory. This is useful if the user has an image in memory
10 // that he wants to decorate with PLPLOT.
11 //
12 // Contributed by Doug Hunt
13 // Included in PLplot by Rafael Laboissiere on Sat Feb 22 18:34:06 CET 2003
14 //
15 
16 #include "plDevs.h"
17 
18 #ifdef PLD_mem
19 
20 #include "plplotP.h"
21 #include "drivers.h"
22 
23 // Device info
24 PLDLLIMPEXP_DRIVER const char* plD_DEVICE_INFO_mem = "mem:User-supplied memory device:-1:mem:46:mem\n";
25 
27 
28 void plD_init_mem( PLStream * );
29 void plD_line_mem( PLStream *, short, short, short, short );
30 void plD_polyline_mem( PLStream *, short *, short *, PLINT );
31 void plD_eop_mem( PLStream * );
32 void plD_bop_mem( PLStream * );
33 void plD_tidy_mem( PLStream * );
34 void plD_state_mem( PLStream *, PLINT );
35 void plD_esc_mem( PLStream *, PLINT, void * );
36 
37 #undef MAX
38 #undef ABS
39 #define MAX( a, b ) ( ( a > b ) ? a : b )
40 #define ABS( a ) ( ( a < 0 ) ? -a : a )
41 
42 #define MAX_INTENSITY 255
43 
45 {
46 #ifndef ENABLE_DYNDRIVERS
47  pdt->pl_MenuStr = "User-supplied memory device";
48  pdt->pl_DevName = "mem";
49 #endif
50  pdt->pl_type = plDevType_Null;
51  pdt->pl_seq = 45;
52  pdt->pl_init = (plD_init_fp) plD_init_mem;
53  pdt->pl_line = (plD_line_fp) plD_line_mem;
54  pdt->pl_polyline = (plD_polyline_fp) plD_polyline_mem;
55  pdt->pl_eop = (plD_eop_fp) plD_eop_mem;
56  pdt->pl_bop = (plD_bop_fp) plD_bop_mem;
57  pdt->pl_tidy = (plD_tidy_fp) plD_tidy_mem;
58  pdt->pl_state = (plD_state_fp) plD_state_mem;
59  pdt->pl_esc = (plD_esc_fp) plD_esc_mem;
60 }
61 
62 //--------------------------------------------------------------------------
63 // plD_init_mem()
64 //
65 // Initialize device (terminal).
66 //--------------------------------------------------------------------------
67 
68 void
69 plD_init_mem( PLStream *pls )
70 {
71  // plsmem must have already been called to set pls->dev to the
72  // user supplied plotting area. The dimensions of the plot area
73  // have also been set by plsmem. Verify this.
74  //
75 
76  if ( ( pls->phyxma == 0 ) || ( pls->dev == NULL ) )
77  {
78  plexit( "Must call plsmem first to set user plotting area!" );
79  }
80 
81  if ( pls->dev_mem_alpha == 1 )
82  {
83  plexit( "The mem driver does not support alpha values! Use plsmem!" );
84  }
85 
86  plP_setpxl( (PLFLT) 4, (PLFLT) 4 ); // rough pixels/mm on *my* screen
87 
88 
89  pls->color = 1; // Is a color device
90  pls->dev_fill0 = 0; // Handle solid fills
91  pls->dev_fill1 = 0; // Use PLplot core fallback for pattern fills
92  pls->nopause = 1; // Don't pause between frames
93 }
94 
95 #define sign( a ) ( ( a < 0 ) ? -1 : ( ( a == 0 ) ? 0 : 1 ) )
96 
97 // Modified version of the ljii routine (see ljii.c)
98 void
99 plD_line_mem( PLStream *pls, short x1a, short y1a, short x2a, short y2a )
100 {
101  int i;
102  PLINT idx;
103  int x1 = x1a, y1 = y1a, x2 = x2a, y2 = y2a;
104  PLINT x1b, y1b, x2b, y2b;
105  PLFLT length, fx, fy, dx, dy;
106  unsigned char *mem = (unsigned char *) pls->dev;
107  PLINT xm = pls->phyxma;
108  PLINT ym = pls->phyyma;
109 
110  // Take mirror image, since (0,0) must be at top left
111 
112  y1 = ym - ( y1 - 0 );
113  y2 = ym - ( y2 - 0 );
114 
115  x1b = x1, x2b = x2, y1b = y1, y2b = y2;
116  length = (PLFLT) sqrt( (double)
117  ( ( x2b - x1b ) * ( x2b - x1b ) + ( y2b - y1b ) * ( y2b - y1b ) ) );
118 
119  if ( length == 0. )
120  length = 1.;
121  dx = ( x2 - x1 ) / length;
122  dy = ( y2 - y1 ) / length;
123 
124  fx = x1;
125  fy = y1;
126  mem[3 * xm * y1 + 3 * x1 + 0] = pls->curcolor.r;
127  mem[3 * xm * y1 + 3 * x1 + 1] = pls->curcolor.g;
128  mem[3 * xm * y1 + 3 * x1 + 2] = pls->curcolor.b;
129 
130  mem[3 * xm * y2 + 3 * x2 + 0] = pls->curcolor.r;
131  mem[3 * xm * y2 + 3 * x2 + 1] = pls->curcolor.g;
132  mem[3 * xm * y2 + 3 * x2 + 2] = pls->curcolor.b;
133 
134  for ( i = 1; i <= (int) length; i++ )
135  {
136  fx += dx;
137  fy += dy;
138  idx = 3 * xm * (PLINT) fy + 3 * (PLINT) fx;
139  mem[idx + 0] = pls->curcolor.r;
140  mem[idx + 1] = pls->curcolor.g;
141  mem[idx + 2] = pls->curcolor.b;
142  }
143 }
144 
145 void
146 plD_polyline_mem( PLStream *pls, short *xa, short *ya, PLINT npts )
147 {
148  int i;
149  for ( i = 0; i < npts - 1; i++ )
150  plD_line_mem( pls, xa[i], ya[i], xa[i + 1], ya[i + 1] );
151 }
152 
153 void
154 plD_eop_mem( PLStream *pls )
155 {
156  // Set the 'dev' member (which holds the user supplied memory image)
157  // to NULL here so it won't be freed when PLplot is closed.
158  // (the user is responsible for freeing it when ready).
159  //
160  pls->dev = NULL;
161 }
162 
163 void
164 plD_bop_mem( PLStream * PL_UNUSED( pls ) )
165 {
166 // Nothing to do here
167 }
168 
169 void
170 plD_tidy_mem( PLStream * PL_UNUSED( pls ) )
171 {
172 // Nothing to do here
173 }
174 
175 void
176 plD_state_mem( PLStream * PL_UNUSED( pls ), PLINT PL_UNUSED( op ) )
177 {
178 // Nothing to do here
179 }
180 
181 void
182 plD_esc_mem( PLStream *PL_UNUSED( pls ), PLINT PL_UNUSED( op ), void * PL_UNUSED( ptr ) )
183 {
184 // Nothing to do here
185 }
186 
187 #endif // PLD_mem