- Optification is done by auto builder now
[gnuplot] / demo / image.dem
1 # demo for plotting images using pixels and binary 2d data
2 #
3 # This demo can be used for terminals supporting image display.
4 # Currently supported are:  X11, Aqua, postscript, png, pdf, svg,
5 # (e)pslatex, pstex.
6
7 # Prepared by Dan Sebald
8 # History:
9 #       - 9.23. 2003 ds: redone in response to discussion list feedback
10 #       - 5. 3. 2003 ds: 1st version
11
12 if ((GPVAL_VERSION == 4.1 || GPVAL_VERSION == 4.2) \
13 &&  (!strstrt(GPVAL_COMPILE_OPTIONS,"+IMAGE"))) \
14     print ">>> Skipping demo <<<\n" ; \
15     print "This copy of gnuplot was built without support for plotting images" ; \
16     exit ;
17
18 print ""
19 print "The plotting styles \`image\` and \`rgbimage\` are intended for plotting"
20 print "images described in a data file either in the convential ASCII format or"
21 print "in a binary format described by the qualifiers \`binary\` and \`using\`."
22 print "All pixels have an (x,y) or (x,y,z) coordinate.  These values can be"
23 print "included in the data file or implicitly determined with the sampling"
24 print "\'array\' key word and sampling periods \'dx\' and \'dy\'.  The key words"
25 print "\'rotate\' and, for 3d plots, \'perpendicular\' control orientation."
26 print "\nThe data for this image was stored as RGB triples, one byte per channel,"
27 print "without (x,y) coordinate information.  This yields a most compact file."
28 print "The plotting command is displayed on the graph."
29 print ""
30 reset
31 set title "Larry Ewing's GIMP penguin on vacation basking in\nthe balmy waters off the coast of Murmansk"
32 set xrange [-10:137]
33 set yrange [-10:157]
34 set label "\"I flew here... by plane.  Why?  For the halibut.\"" at 64,135 center
35 plot 'blutux.rgb' binary array=128x128 flipy format='%uchar' with rgbimage
36 pause -1 "Hit return to continue"
37 reset
38
39 print ""
40 print "Images are typically stored in a file with the first datum being the"
41 print "top, left pixel.  Without the ability to translate coordinates, the"
42 print "the result would be an upside down image."
43 print ""
44 print "The key word \'array\' means an implied sample array is applied"
45 print "to generate the locations of file data using the sampling periods"
46 print "dx, dy and dz.  The x-dimension is always the contiguous points in"
47 print "a binary file.  The y-dimension is the line number which is incremented"
48 print "upon the x-dimension reaching the line length.  The z-dimension is"
49 print "the plane number which is incremented upon the y-dimension reaching"
50 print "the number of lines per plane."
51 print ""
52 print "To alter the location of the binary data when displayed via the \'plot\'"
53 print "command, use the key word \'rotate\' along with changing the sign of dx, dy"
54 print "and dz."
55 print ""
56 set title "Translations of position variables via 'using'"
57 set xrange [-10:275]
58 set yrange [-10:300]
59 unset key
60 set label "\"Time for a dip...\"" at 132,285 center
61 plot 'blutux.rgb' binary array=128x128 flipy center=( 64,201 ) format='%uchar' with rgbimage, \
62      'blutux.rgb' binary array=128x128 flipy rotation=-90d center=(201,201) format='%uchar' with rgbimage, \
63      'blutux.rgb' binary array=128x128 flip=y rotate=3.1415 center=(201,64) format='%uchar' with rgbimage, \
64      'blutux.rgb' binary array=128x128 flip=y rot=0.5pi center=(64,64) format='%uchar' using 1:2:3 with rgbimage
65 pause -1 "Hit return to continue"
66 reset
67
68 print ""
69 print "There is the ability to plot both color images and palette based"
70 print "images.  This is controlled by the styles \`image\`, which derives"
71 print "color information from the current palette, and \`rgbimage\`,"
72 print "which requires three components representing the red, blue and"
73 print "green primary color scheme."
74 print ""
75 print "By the way, if you have a mouse active, click the right button"
76 print "inside the image to isolate a portion of the image and see what"
77 print "happens."
78 print ""
79 unset key
80 set xrange [-10:137]
81 set yrange [-10:157]
82 set label 1 "\"I am the penguin, GOO GOO GOO JOOB.\"" at 63,140 center
83 set title "Palette mode 'image' used to produce psychedelic bird"
84 unset colorbox
85 plot 'blutux.rgb' binary array=128x128 flipy format='%uchar%uchar%uchar' using ($1+$2+$3) with image
86 pause -1 "Hit return to continue"
87 reset
88
89 print ""
90 print "Naturally, as with 3d color surfaces, the palette may be changed."
91 print "This is an example of gray scale."
92 print ""
93 print "Also, notice in the plot command the key word \'flipy\'.  This"
94 print "means to change the direction of the sample along the y dimension"
95 print "and is useful for the situation where images or other data are"
96 print "stored in some direction other than that of the Cartesion system."
97 print "Alone, \'flipD\' means do flipping in the D (x y or z) direction"
98 print "for all records.  Individual records can be controlled using the"
99 print "syntax \'flipD=#,...,#\', where # is \'0\' or \'1\'."
100 print ""
101 unset key
102 set xrange [-10:137]
103 set yrange [-10:157]
104 set tics out
105 set label 1 "\"This picture was taken by my friend Ansel Adams.\"" at 63,140 center
106 set title "The palette can be changed from color to gray scale"
107 set palette gray
108 unset colorbox
109 plot 'blutux.rgb' binary array=128x128 flipy format='%uchar%uchar%uchar' using ($1+$2+$3)/3 with image
110 pause -1 "Hit return to continue"
111 unset label 1
112
113 print ""
114 print "Also, similar to 3d color surface plots, a color box showing the"
115 print "palette mapping scheme can be added to the plot.  The default"
116 print "location is the right edge of the plot.  The location can be set"
117 print "manually using \`set colorbox\` and \`set margin\`."
118 print ""
119 print "As a prelude to the next graph, resize the plot window to judge"
120 print "the refresh speed of the image drawing routine.  Notice that when"
121 print "the window is smaller, the image refresh is faster.  There is more"
122 print "decimation in the data of the original image and less data to plot."
123 print "Furthermore, the window continues to refresh at a reasonable rate"
124 print "even when the input image size becomes large (e.g., 1024 x 1024)"
125 print "because the number of pixels on the screen remains about the same"
126 print "while much of the hi resolution data is decimated."
127 print ""
128 unset key
129 set pm3d map
130 set xrange [10:117]
131 set yrange [10:117]
132 set tics out
133 set colorbox
134 set cbrange [0:255]
135 set title "As with 3d color surfaces, a color box may be added to the plot"
136 splot 'blutux.rgb' binary array=128x128 flipy format='%uchar%uchar%uchar' using ($1+$2+$3)/3 with image
137 pause -1 "Hit return to continue"
138
139 print ""
140 print "The \'rotation\' key word works not only with angles of integer"
141 print "multiples of 90 degrees but also arbitrary rotations.  When"
142 print "constructing an image, Gnuplot verifies that pixel locations"
143 print "form a valid grid.  Pixel widths are based upon the grid"
144 print "spacing.  If the image orientation is aligned with the view"
145 print "axes, Gnuplot uses an efficient image driver routine.  Otherwise,"
146 print "individual pixels are drawn using polygon shapes."
147 print ""
148 print "Resize this window and compare the plot's refresh rate to that of"
149 print "the previous and next plot.  Notice how in this example if the"
150 print "window is small the image refresh does not speed up.  Unlike the"
151 print "image routine where image data is decimated, all color rectangles"
152 print "must be redrawn no matter the size of the output image."
153 print ""
154 print "Also notice how the center of the image matches the tuple specified"
155 print "with the key word \'center\' in the plot command.  Before doing the"
156 print "rotation, the image's natural center is subtracted, and after doing"
157 print "the rotation, the specified center is added."
158 print ""
159 set key
160 set xrange [-10:138]
161 set yrange [-10:138]
162 set tics out
163 set title "Polygons used to draw pixels for rotated images\nNotice the slower refresh rate than for the next plot"
164 unset colorbox
165 plot 'blutux.rgb' binary array=128x128 dx=0.70711 dy=0.70711 flipy rotation=45d center=(63.5,63.5) format='%uchar' using ($1+$2+$3)/3 with image
166 pause -1 "Hit return to continue"
167 reset
168
169 print ""
170 print "The image of this plot is rotated 90 degrees and can utilize the"
171 print "efficient image functions of terminal drivers.  The plot refresh"
172 print "is faster than the previous plot."
173 print ""
174 print "Furthermore, the image location in this case is specified via the"
175 print "\'origin\' key word.  As such, the rotation is done about the origin"
176 print "as opposed to the center of the image.  Notice the difference in"
177 print "axis ranges."
178 print ""
179 unset key
180 set xrange [-138:10]
181 set yrange [-10:138]
182 set tics out
183 set title "Terminal image routine used to draw plot rotated about origin\nNotice the faster refresh rate than for the previous plot"
184 unset colorbox
185 plot 'blutux.rgb' binary array=128x128 dx=1 flip=y rotation=0.5pi origin=(0,0) format='%uchar' using ($1+$2+$3)/3 with image
186 pause -1 "Hit return to continue"
187 reset
188
189 print ""
190 print "Algebraic manipulation of the input variables can select various"
191 print "components of the image.  Here are three examples where two"
192 print "channels--or analogous to the ASCII file, data \"columns\"--are ignored"
193 print "This is done by using \`*\` in the format to indicate that a variable"
194 print "of a certain size should be discarded.  For example, to select the"
195 print "green channel, \`%*uchar%uchar%*uchar\` is one alternative."
196 print ""
197 set size 1.0, 1.0
198 set origin 0.0, 0.0
199 set multiplot
200 set size 0.5,0.48
201 set origin 0.0,0.47
202 unset key
203 set xrange [-0.5:127.5]
204 set yrange [-0.5:127.5]
205 set tics out
206 set label 1 "Selection of the input channels via \`using\`" at 140,160 center
207 set title '"I do impersonations..."' offset 0,-0.5
208 plot 'blutux.rgb' binary array=128x128 flip=y format='%uchar' using 1:2:3 with rgbimage
209 unset label 1
210 set size 0.5,0.48
211 set origin 0.5,0.47
212 set title '"A cardinal."'
213 plot 'blutux.rgb' binary array=128x128 flip=y format='%uchar%*uchar%*uchar' using 1:(0):(0) with rgbimage
214 set size 0.5,0.48
215 set origin 0.0,0.0
216 set title '"A parrot."'
217 plot 'blutux.rgb' binary array=128x128 flipy format='%*uchar%uchar%*uchar' using (0):1:(0) with rgbimage
218 set size 0.5,0.48
219 set origin 0.5,0.0
220 set title '"A bluebird."'
221 plot 'blutux.rgb' binary array=128x128 flipy format='%*uchar%*uchar%uchar' using (0):(0):1 with rgbimage
222 unset multiplot
223 pause -1 "Hit return to continue"
224 reset
225
226 print ""
227 print "The range of valid pixel values can be set via \`cbrange\`.  If the"
228 print "\`cbrange\` is in autoscale mode, Gnuplot will attempt to adjust the"
229 print "luminance based upon the maximum pixel value in the image.  Previous"
230 print "graphs all had the \`cbrange\' in autoscale mode.  However, there are"
231 print "instances where one may want to force the image luminance or palette"
232 print "to be of a certain range.  For example, each channel of the penguin"
233 print "image has an 8-bit depth, and hence the valid range is from 0 to 255."
234 print "The logical choice of \`cbrange\' is [0:255].  This is done in the"
235 print "upper left image.  However, the image can be saturated by choosing a"
236 print "smaller range, as done in the upper right image.  Expanding the color"
237 print "box range will darken the image, as shown in the bottom examples."
238 print ""
239 set size 1.0, 1.0
240 set origin 0.0, 0.0
241 set multiplot
242 set size 0.5,0.48
243 set origin 0.0,0.47
244 unset key
245 set xrange [-0.5:127.5]
246 set yrange [-0.5:127.5]
247 set tics out
248 set label 1 "Luminance adjustment via \`cbrange\`" at 140,160 center
249 set title 'Lake Mendota, "or Wonk-sheck-ho-mik-la!"' offset 0,-0.5
250 set cbrange [*:*]
251 plot 'blutux.rgb' binary array=128x128 flipy format='%uchar' using 1:2:3 with rgbimage
252 unset label 1
253 set size 0.5,0.48
254 set origin 0.5,0.47
255 set title '"Lucky I brought sunscreen."'
256 set cbrange [0:200]
257 plot 'blutux.rgb' binary array=128x128 flip=y format='%uchar' using 1:2:3 with rgbimage
258 set size 0.5,0.48
259 set origin 0.0,0.0
260 set title 'Sunset on the Terrace'
261 set cbrange [0:400]
262 plot 'blutux.rgb' binary array=128x128 flip=y format='%uchar' using (1.5*$1):2:3 with rgbimage
263 set size 0.5,0.48
264 set origin 0.5,0.0
265 set title 'Sultry evening'
266 set cbrange [0:400]
267 plot 'blutux.rgb' binary array=128x128 flipy format='%uchar' using 1:2:3 with rgbimage, '-' w points pt 7 ps 6 lt -2, '-' w points pt 7 ps 0.65 lt -2
268 110 100
269 e
270 10 95
271 38 120
272 82 102
273 26 82
274 93 108
275 41 99
276 123 84
277 e
278 unset multiplot
279 pause -1 "Hit return to continue"
280 reset
281
282 # Repeat of the 'using.dem' demo, but with binary 2d data.  'using.bin' is
283 # in the format int32, int8, int16/100, int8, int16/100, int16/100.
284 #
285 print ""
286 print "Not only can the 2d binary data mode be used for image data."
287 print "Here is an example that repeats the \`using.dem\` demo with the"
288 print "same data, but stored in binary format of differing sizes.  It"
289 print "uses different format specifiers within the \'format\' string."
290 print "There are machine dependent and machine independent specifiers,"
291 print "display by the command \'show datafile binary datasizes\':"
292 print ""
293 show datafile binary datasizes
294 print ""
295 set title "Convex     November 1-7 1989    Circadian"
296 set key left box
297 set xrange[-1:24]
298 plot 'using.bin' binary format='%*int32%int8%*int16%int8%*int16%*int16' using 1:2 title "Logged in" with impulses,\
299      'using.bin' binary format='%*int32%int8%*int16%int8%*int16%*int16' using 1:2 title "Logged in" with points
300 pause -1 "Hit return to continue"
301 reset
302
303 print ""
304 print "Again, a different format specification for \`using\` can be"
305 print "used to select different \"columns\" within the file."
306 print ""
307 set xrange [1:8]
308 set title "Convex     November 1-7 1989"
309 set key below
310 set label "(Weekend)" at 5,25 center
311 plot 'using.bin' binary format='%*int32%*int8%int16%int8%*int16%*int16' using ($1/100):2 title "Logged in" with impulses,\
312      'using.bin' binary format='%*int32%*int8%int16%*int8%int16%*int16' using ($1/100):($2/100) t "Load average" with points,\
313      'using.bin' binary format='%*int32%*int8%int16%*int8%*int16%int16' using ($1/100):($2/100) t "%CPU used" with lines
314 unset label
315 pause -1 "Hit return to continue"
316 reset
317
318 print ""
319 print "Here is another example, one repeating the \`scatter.dem\`"
320 print "demo.  With binary data we cannot have blank lines to"
321 print "indicate a break in data, as is done with ASCII files."
322 print "Instead, we can specify the record lenths in the command."
323 print "In this case, the data file contains the (x,y,z) coordinate"
324 print "information, hence implicit derivation of that information"
325 print "is not desired.  Instead, the record lengths can be specified"
326 print "using the keyword \'record\', which behaves the same as"
327 print "\'array\' but does not generate coordinates.  The command is"
328 print "displayed on the graph."
329 print ""
330 set title "2d binary data example where record length is part of command"
331 splot 'scatter2.bin' binary endian=little record=30:30:29:26 using 1:2:3
332 pause -1 "Hit return to continue"
333 reset
334
335 print ""
336 print "For binary data, the byte endian format of the file and of the"
337 print "compiler often require attention.  Therefore, the key word"
338 print "\'endian\' is provided for setting or interchanging byte"
339 print "order.  The allowable types are \'little\', \'big\', and"
340 print "depending upon how your version of Gnuplot was compiled,"
341 print "\'middle\' (or \'pdp\') for those still living in the medieval"
342 print "age of computers.  These types refer to the file's endian."
343 print "Gnuplot arranges bytes according to this endian and what it"
344 print "determines to be the compiler's endian."
345 print ""
346 print "There are also the special types \'default\' and \'swap\' (or"
347 print "\'swab\') for those who don't know the file type but realize"
348 print "their data looks incorrect and want to change the byte read"
349 print "order."
350 print ""
351 print "Here is an example showing the \`scatter.dem\` data plotted"
352 print "with correct and incorrect byte order.  The file is known"
353 print "to be little endian, so the upper left plot is correct"
354 print "appearance and the upper right plot is incorrect appearance."
355 print "The lower two plots are default and swapped bytes.  If the"
356 print "plots within the columns match, your compiler uses little"
357 print "endian.  If diagonal plots match then your compiler uses"
358 print "big endian.  If neither of the bottom plots matches the"
359 print "upper plots, Tux says you're living in the past."
360 print ""
361 set size 1.0, 1.0
362 set origin 0.0, 0.0
363 set multiplot
364 set size 0.5,0.48
365 set origin 0.0,0.47
366 unset key
367 set label 1 "If plots in columns match, your compiler is little endian" at 100,1200,3.0 center
368 set title 'Little endian' offset 0,-0.5
369 splot 'scatter2.bin' binary record=30:30:29:26 endian=little using 1:2:3
370 unset label 1
371 set size 0.5,0.48
372 set origin 0.5,0.47
373 set title 'Big endian'
374 splot 'scatter2.bin' binary record=30:30:29:26 endian=big using 1:2:3
375 set size 0.5,0.48
376 set origin 0.0,0.0
377 set title 'Default'
378 splot 'scatter2.bin' binary record=30:30:29:26 endian=default using 1:2:3
379 set size 0.5,0.48
380 set origin 0.5,0.0
381 set title 'Swapped'
382 splot 'scatter2.bin' binary record=30:30:29:26 endian=swap using 1:2:3
383 unset multiplot
384 pause -1 "Hit return to continue"
385 reset
386
387 print ""
388 print "This close up of a 2x2 image illustrates how pixels surround the"
389 print "sampling grid points.  This behavior is slightly different than"
390 print "that for pm3d where the four grid points would be used to create"
391 print "a single polygon element using an average, or similar mathematical"
392 print "combination, of the four values at those points."
393 print ""
394 set title "Close up of pixels having grid points (0,0), (0,2), (2,0) and (2,2)"
395 set xrange [-1.5:3.5]
396 set yrange [-1.5:3.5]
397 set cbrange [0:1]
398 unset key
399 plot '-' with rgbimage, '-' with points pt 7 ps 2 lt -1
400 0 0 0.0 0.0 0.5
401 0 2 0.0 0.5 0.0
402 2 0 0.0 0.5 0.0
403 2 2 0.0 0.0 0.5
404 e
405 0 0
406 0 2
407 2 0
408 2 2
409 e
410 pause -1 "Hit return to continue"
411 reset
412
413 print ""
414 print "Lower dimensional data may be extended to higher dimensional plots"
415 print "via a number of simple, logical rules.  The first step Gnuplot does"
416 print "is sets the components for higher than the natural dimension of the"
417 print "input data to zero.  For example, a two dimensional image can be"
418 print "displayed in the three dimensional plot as shown.  Without"
419 print "translation, the image lies in the x/y-plane."
420 print ""
421 set title "Simple extension of a two dimensional image into three dimensions"
422 set xrange [-10:137]
423 set yrange [-10:157]
424 set cbrange [0:255]
425 splot 'blutux.rgb' binary array=128x128 flip=y format='%uchar%uchar%uchar' using ($1+$2+$3)/3 with image
426 pause -1 "Hit return to continue"
427 reset
428
429 print ""
430 print "The key words \'rotate\' and \'center\' still apply in \'splot\' with"
431 print "rules similar to their use in \'plot\'.  However, the center must be"
432 print "specified as a three element tuple."
433 print ""
434 unset colorbox
435 set title "Orientation operations from \'plot\' also apply to to \'splot\'"
436 splot 'blutux.rgb' binary array=128x128 flipy rotate=90d center = (63.5,63.5,50) format='%uchar%uchar%uchar' using ($1+$2+$3) with image
437 pause -1 "Hit return to continue"
438 reset
439
440 print ""
441 print "To have full degrees of freedom in orienting the image, an additional"
442 print "key word, \'perpendicular\', can translate the x/y-plane of the 2d"
443 print "data so that it lies orthogonal to a vector given as a three element"
444 print "tuple.  The default vector is, of course, (0,0,1).  The vector need"
445 print "not be of unit length, as this example illustrates.  Viewing this"
446 print "plot with the mouse active can help visualize the image's orientation"
447 print "by panning the axes."
448 print ""
449 set title "The key word \'perpendicular\' applies only to \'splot\'"
450 set cbrange [0:255]
451 set style line 1 lt -1 lw 1
452 set style line 2 lt -1 lw 1
453 set style arrow 1 nohead ls 1
454 set style arrow 2 head filled size screen 0.025,30,45 ls 2
455 set arrow from -10,0,0 to 10,0,0 as 1
456 set arrow from 0,-10,0 to 0,10,0 as 1
457 set arrow from 0,0,-10 to 0,0,10 as 1
458 set arrow from 0,0,0 to 30,30,30 as 2
459 splot 'blutux.rgb' binary array=128x128 flipy rot=1.0pi center = (63.5,63.5,50) perp=(1,1,1) format='%uchar%uchar%uchar' using ($1+$2+$3)/3 with image
460 pause -1 "Hit return to continue"
461 reset
462
463 print ""
464 print "These concepts of extending lower dimensional data also apply"
465 print "to temporal-like signals.  For example, a uniformly sampled"
466 print "sinusoid, sin(1.75*pi*x), in a binary file having no data for"
467 print "the independent variable can be displayed along any direction"
468 print "for both \'plot\'..."
469 print ""
470 set size 1.0, 1.0
471 set origin 0.0, 0.0
472 set multiplot
473 set size 0.5,0.48
474 set origin 0.0,0.47
475 unset key
476 set xrange [0:2]
477 set yrange [-1:1]
478 set label 1 "Temporal data having one generated coordinate" at 2.25,1.5 center
479 set title 'Along the x-axis' offset 0,-0.5
480 plot 'sine.bin' binary endian=little array=201 dx=0.01 origin=(0,0) format='%f' using 1 with lines
481 unset label 1
482 set size 0.5,0.48
483 set origin 0.5,0.47
484 set xrange [-1:1]
485 set yrange [0:2]
486 set title 'Along the y-axis'
487 plot 'sine.bin' binary endian=little array=201 dx=0.01 origin=(0,0) rotate=0.5pi format='%f' with lines
488 set size 0.5,0.48
489 set origin 0.25,0.0
490 set xrange [-2.2:0.7]
491 set yrange [-2.2:0.7]
492 set title 'Along a 225 degree projection'
493 plot 'sine.bin' binary endian=little array=201 dx=0.01 rotate=225d origin=(0,0) format='%f' using 1 with lines
494 unset multiplot
495 pause -1 "Hit return to continue"
496 reset
497
498 print ""
499 print "...and \'splot\'.  Here is the \'scatter.dem\' example again,"
500 print "but this simulates the case of the redundant x coordinates not"
501 print "being in the binary file.  The first \"column\" of the binary"
502 print "file is ignored and reconstructed by orienting the various"
503 print "data records."
504 print ""
505 set title "2d binary data example where x coordinate is ignored then generated"
506 set xrange [20:100]
507 set yrange [0:800]
508 set zrange [0.2:1.8]
509 splot 'scatter2.bin' binary endian=little record=30:30:29:26 origin=(25,0,0):(50,0,0):(75,0,0):(100,0,0) format='%f%f' using (0):2:3
510 pause -1 "Hit return to continue"
511 reset
512
513 print ""
514 print "Some binary data files have headers, which may be skipped via"
515 print "the \'skip\' key word.  Here is the \'scatter.dem\' example"
516 print "again, but this time the first and third traces are skipped."
517 print "The first trace is 30 samples of three floats so takes up 360"
518 print "bytes of space.  Similarly, the third trace takes up 348 bytes."
519 print ""
520 set title "The key word \'skip\' used to ignore some data"
521 set xrange [20:100]
522 set yrange [0:800]
523 set zrange [0.2:1.8]
524 splot 'scatter2.bin' binary endian=little record=30:26 skip=360:348 origin=(50,0,0):(100,0,0) format='%f%f' using (0):2:3
525 pause -1 "Hit return to continue"
526 reset
527
528 print ""
529 print "Generating uniformly spaced coordinates is valid for polar"
530 print "plots as well.  This is useful for data acquired by machines"
531 print "sampling in a circular fashion.  Here the sinusoidal data"
532 print "of the previous 2D plot put on a polar plot.  Note the"
533 print "pseudonyms \'dt\' meaning sample period along the angular,"
534 print "or theta, direction.  In Gnuplot, cylindrical coordinate"
535 print "notation is (t,r,z).  [Different from common math convention"
536 print "(r,t,z).]"
537 print ""
538 set size ratio 1
539 set title "Uniform sampling in the polar coordinate system"
540 set polar
541 set grid polar
542 set xrange [-1.3:1.3]
543 set yrange [-1.3:1.3]
544 # The sinusoid has period T=8/7.  Also, dx=0.01.  So solving 8/7 dt = 2/3 pi dx, we get dt = 0.018326.
545 plot 'sine.bin' binary endian=little array=201 dt=0.018326 origin=(0,0) format='%f' using 1 with lines
546 pause -1 "Hit return to continue"
547 reset
548
549 print ""
550 print "Binary data stored in matrix format (i.e., gnuplot binary)"
551 print "may also be translated with similar syntax.  However, the"
552 print "binary keywords \`format\`, \`array\` and \`record\` do not apply"
553 print "because gnuplot binary has the requirements of float data"
554 print "and grid information as part of the file.  Here is an"
555 print "example of a single matrix binary file input four times,"
556 print "each translated to a different location with different"
557 print "orientation."
558 print ""
559 set style data lines
560 set hidden3d
561 set ticslevel 0
562 set key box
563 set title "Matrix binary data (gnuplot binary) translated"
564 set vi 70,10
565 set xrange [-3:15]
566 set yrange [-3:15]
567 set zrange [-0.2:1.2]
568 splot "binary3" binary center=(1.5,1.5,0), \
569   "binary3" binary center=(10.5,1.5,0) rotate=0.5pi u 1:2:3, \
570   "binary3" binary center=(10.5,10.5,0) rotate=1.0pi u 1:2:3, \
571   "binary3" binary center=(1.5,10.5,0) rotate=1.5pi u 1:2:3
572 pause -1 "Hit return to continue"
573 reset
574
575 print ""
576 print "As with ASCII data, decimation in various directions can"
577 print "be achieved via the \`every\` keyword.  (Note that no down-"
578 print "sampling filter is applied such that you risk aliasing data"
579 print "with the \`every\` keyword."
580 print ""
581 print "Here is a series of plots with increasing decimation."
582 print ""
583 set style data lines
584 set hidden3d
585 set ticslevel 0
586 set key box
587 set title "Non-decimated matrix data file"
588 set view 70,45
589 set xrange [-3:3]
590 set yrange [-3:3]
591 splot "binary2" binary
592 pause -1 "Hit return to continue"
593 set title "Decimate by two in first dimension"
594 splot "binary2" binary every 2
595 pause -1 "Hit return to continue"
596 set title "Decimate by three in second dimension"
597 splot "binary2" binary every :3
598 pause -1 "Hit return to continue"
599 set title "Decimate by four in both dimensions"
600 splot "binary2" binary every 4:4
601 pause -1 "Hit return to continue"
602 reset
603
604 print ""
605 print "Decimation works on general binary data files as well.  Here is the"
606 print "image file with increasing decimation."
607 print ""
608 set title "Decimation works on general binary data files as well.\nLet Tux have his fun..."
609 set xrange [28:98]
610 set yrange [55:125]
611 set label 1 "\"Can I do that?\"" at 64,116 center
612 plot 'blutux.rgb' binary array=128x128 flipy format='%uchar' every 1:1:43:15:83:65 with rgbimage
613 pause -1 "Hit return to continue"
614 set title "... Sure, go ahead."
615 set xrange [-10:130]
616 set yrange [-8:157]
617 set label 1 "\"Picasso's \'Penguin on Beach\'\"" at 64,139 center
618 plot 'blutux.rgb' binary array=128x128 flipy format='%uchar' every 8:8 with rgbimage
619 pause -1 "Hit return to continue"
620 reset
621
622 print ""
623 print "Gnuplot understands a few common binary formats.  Internally"
624 print "a function is linked with various extensions.  When the"
625 print "extension is specified at the command line or recognized via"
626 print "a special file type called \'auto\', Gnuplot will call the"
627 print "function that sets up the necessary binary information.  The"
628 print "known extensions are displayed using the \'show filetype\'"
629 print "command.  E.g.,"
630 print ""
631 show datafile binary filetypes
632 print "Here's an example where an EDF file is recognized when Gnuplot"
633 print "is in \'auto\' mode.  Details are pulled from the header of"
634 print "file itself and not specified at the command line.  The command"
635 print "line can still be used to over-ride in-file attributes."
636 print ""
637 set title "Automatically recognizing file type and extracting file information"
638 plot 'demo.edf' binary filetype=auto with image
639 pause -1 "Hit return to continue"
640 reset
641
642 print ""
643 print "The \'flip\', \'rotate\' and \'perpendicular\' qualifiers"
644 print "should provide adequate freedom to orient data as desired."
645 print "However, there is an additional key words \'scan\' which may"
646 print "offer a more direct and intuitive manner of orienting data"
647 print "depending upon the user's application and perspective."
648 print ""
649 print "\'scan\' is a 2 or 3 letter string representing how Gnuplot"
650 print "should derive (x,y), (x,y,z), (t,r) or (t,r,z) from the"
651 print "the datafile's scan order.  The first letter pertains to the"
652 print "fastest rate or point-by-point increment.  The second letter"
653 print "pertains to the medium rate or line-by-line increment.  If"
654 print "there is a third letter, it pertains to the slowest rate or"
655 print "plane-by-plane increment.  The default or inherent scan order"
656 print "is \'scan=xyz\'."
657 print ""
658 print "The pseudonym \'transpose\' is equivalent to \'scan=yx\' when"
659 print "generating 2D coordinates and \'scan=yxz\' when generating"
660 print "3D coordinates."
661 print ""
662 print "There is a subtle difference between the behavior of \'scan\'"
663 print "when dimension info is taken from the file itself as opposed"
664 print "to entered at the command line.  When information is gathered"
665 print "from the file, internal scanning is unaltered so that issuing"
666 print "the \'scan\' command may cause the number of samples along"
667 print "the various dimensions to change.  However, when the qualifier"
668 print "\'array\' is entered at the command line, the array dimensions"
669 print "adjust so that \'array=XxYxZ\' is always the number of samples"
670 print "along the Cartesian x, y and z directions, respectively."
671 print "
672 set size 1.0, 1.0
673 set origin 0.0, 0.0
674 set multiplot
675 set size 0.5,0.5
676 set origin 0.0,0.5
677 unset key
678 set xrange [-5:635]
679 set yrange [-5:635]
680 set tics out
681 set title 'Details read from file' offset 0,-0.5
682 plot 'demo.edf' binary filetype=auto with image
683 set size 0.5,0.5
684 set origin 0.5,0.5
685 set title 'Transpose of file-read axes parameters'
686 plot 'demo.edf' binary filetype=auto transpose with image
687 set size 0.5,0.5
688 set origin 0.0,0.0
689 set title 'Details specified at command line'
690 set label 1 "array=32x64 dx=10 dy=5 scan=xy" at 315,500 center
691 plot 'demo.edf' binary array=32x64 dx=10 dy=5 scan=xy skip=1024 format="%*float%float" using 1 with image
692 set size 0.5,0.5
693 set origin 0.5,0.0
694 set title 'Transpose of command line axes parameters'
695 set label 1 "array=64x32 dx=5 dy=10 scan=yx" at 315,500 center
696 plot 'demo.edf' binary array=64x32 dx=5 dy=10 scan=yx skip=1024 format="%*float%float" using 1 with image
697 unset label 1
698 unset multiplot
699 pause -1 "Hit return to continue"
700 reset
701
702 print ""
703 print "It is possible to enter binary data at the command line.  Of"
704 print "course, the limitation to this approach is that keyboards will"
705 print "allow entering only a limited subset of the possible character"
706 print "values necessary to represent general binary data.  For this"
707 print "reason, the primary application for binary data at the command"
708 print "line is using Gnuplot through a pipe.  For example, if a pipe"
709 print "is established with a C program, the function \'fputs()\' can"
710 print "send ASCII strings containing the Gnuplot commands while the"
711 print "function \'fwrite()\' can send binary data."
712 print ""
713 print "Furthermore, there can be no special ending character such as"
714 print "in the case of ASCII data entry where \'e\' represents the end"
715 print "of data for the special file \'-\'.  It is important to note"
716 print "that when \'binary\' is specified, Gnuplot will continue"
717 print "reading until reaching the number of elements specified via"
718 print "the \'array\' or \'record\' command."
719 print ""
720 print "Here is an example of binary data at the command line where"
721 print "keyboard input has been side stepped by copying 48 bytes from"
722 print "a pre-existing binary file into this demo file."
723 print ""
724 set title "Binary data specified at the command line, intended for use through pipe"
725 set xrange [-1.5:3.5]
726 set yrange [-1.5:3.5]
727 set cbrange [0:1]
728 plot '-' binary endian=little array=2x2 dx=2 format="%float" using 1:2:3 with rgbimage,\
729      '-' binary endian=little record=4 format="%char" using 1:2 with points pt 7 ps 2 lt -1
730 \0\0\0\0\0\0\0\0\0\0\0?\0\0\0\0\0\0\0?\0\0\0\0\0\0\0\0\0\0\0?\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0?\0\0\0\ 2\ 2\0\ 2\ 2
731 pause -1 "Hit return to continue"
732 reset
733
734 print ""
735 print "ASCII data files have a matrix variant.  Unlike matrix binary,"
736 print "ASCII binary may have multiple matrices per file, separted"
737 print "by a blank line.  The keyword \`index\` can select the desired"
738 print "matrix to plot."
739 print ""
740 set style data lines
741 set hidden3d
742 set ticslevel 0
743 set key box
744 set view 70,45
745 set multiplot
746 set size 0.5,0.5
747 set origin 0.0,0.5
748 unset key
749 set tics out
750 set title "'asciimat.dat' matrix index 0"
751 splot 'asciimat.dat' matrix index 0
752 set size 0.5,0.5
753 set origin 0.5,0.5
754 set title "'asciimat.dat' matrix index 1"
755 splot 'asciimat.dat' matrix index 1
756 set size 0.5,0.5
757 set origin 0.0,0.0
758 set title "'asciimat.dat' matrix index 2"
759 splot 'asciimat.dat' matrix index 2
760 set size 0.5,0.5
761 set origin 0.5,0.0
762 set title "'asciimat.dat' matrix index 2 using 1:(2*$2):($3*$3)"
763 splot 'asciimat.dat' matrix index 2 using 1:(2*$2):($3*$3)
764 unset multiplot
765 pause -1 "Hit return to continue"
766 reset
767
768 print ""
769 print "Tux says \"bye-bye\"."
770 print ""
771 set xrange [-0.5:137.5]
772 set yrange [-0.5:137.5]
773 unset border
774 unset xtics
775 unset ytics
776 unset ztics
777 unset key
778 unset colorbox
779 set tmargin 0
780 set bmargin 0
781 set rmargin 0
782 set lmargin 0
783 set view 60, 45, 1, 1
784 set label 1 "\"Mirror mirror on the wall,\nwho's the GNUiest penguin of all?\"" at 140,0,40 center
785 splot 'blutux.rgb' binary array=128x128 flip=y rot=90d origin=(0,0,0) perp = (1,0,0) format='%uchar' using ($1+$2+$3) with image, \
786       'blutux.rgb' binary array=128x128 flipy rot=90d origin=(+137,+137,0) perp=(0,1,0) format='%uchar' using ($1+$2+$3) with image
787 pause -1 "Hit return to continue"
788 reset
789
790 print ""
791 print "End of image demo..."