]> git.tdb.fi Git - ext/subsurface.git/blob - profile.c
Define all colors in one place
[ext/subsurface.git] / profile.c
1 /* profile.c */
2 /* creates all the necessary data for drawing the dive profile 
3  * uses cairo to draw it
4  */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdarg.h>
8 #include <string.h>
9 #include <time.h>
10
11 #include "dive.h"
12 #include "display.h"
13 #include "divelist.h"
14
15 int selected_dive = 0;
16
17 typedef enum { STABLE, SLOW, MODERATE, FAST, CRAZY } velocity_t;
18 /* Plot info with smoothing, velocity indication
19  * and one-, two- and three-minute minimums and maximums */
20 struct plot_info {
21         int nr;
22         int maxtime;
23         int meandepth, maxdepth;
24         int maxpressure;
25         int mintemp, maxtemp;
26         struct plot_data {
27                 unsigned int same_cylinder:1;
28                 unsigned int cylinderindex;
29                 int sec;
30                 /* pressure[0] is sensor pressure
31                  * pressure[1] is interpolated pressure */
32                 int pressure[2];
33                 int temperature;
34                 /* Depth info */
35                 int depth;
36                 int smoothed;
37                 velocity_t velocity;
38                 struct plot_data *min[3];
39                 struct plot_data *max[3];
40                 int avg[3];
41         } entry[];
42 };
43 #define SENSOR_PR 0
44 #define INTERPOLATED_PR 1
45 #define SENSOR_PRESSURE(_entry) (_entry)->pressure[SENSOR_PR]
46 #define INTERPOLATED_PRESSURE(_entry) (_entry)->pressure[INTERPOLATED_PR]
47 #define GET_PRESSURE(_entry) (SENSOR_PRESSURE(_entry) ? : INTERPOLATED_PRESSURE(_entry))
48
49 typedef enum { TEXT_BACKGROUND, TRIANGULAR_BG, TRIANGULAR_FG, EVENTS, SAMPLE_DEEP, SAMPLE_SHALLOW,
50         SMOOTHED, MINUTE, TIME_MARKER, TIME_TEXT, DEPTH_MARKER, MEAN_DEPTH, DEPTH_TOP,
51         DEPTH_BOTTOM, DEPTH_FILL_PRINTER, DEPTH_STROKE_PRINTER, TEMP_TEXT, TEMP_PLOT, SAC_DEFAULT,
52         BOUNDING_BOX, PRESSURE_TEXT, VELO_STABLE, VELO_SLOW, VELO_MODERATE, VELO_FAST, VELO_CRAZY,
53         SAC_1, SAC_2, SAC_3, SAC_4, SAC_5, SAC_6, SAC_7, SAC_8, SAC_9, BACKGROUND } color_t;
54
55 /* convert velocity to colors */
56 typedef struct { double r, g, b; } rgb_t;
57 static const color_t velocity_color[] = {
58         [STABLE]   = VELO_STABLE,
59         [SLOW]     = VELO_SLOW,
60         [MODERATE] = VELO_MODERATE,
61         [FAST]     = VELO_FAST,
62         [CRAZY]    = VELO_CRAZY,
63 };
64
65 static const rgb_t profile_color[] = {
66         [TEXT_BACKGROUND] = {0.95, 0.95, 0.95},
67         [TRIANGULAR_BG]   = {1.0, 1.0, 0.1},
68         [TRIANGULAR_FG]   = {0.0, 0.0, 0.0},
69         [EVENTS]          = {1.0, 0.2, 0.2},
70         [SAMPLE_DEEP]     = {0.8, 0.2, 0.2},
71         [SAMPLE_SHALLOW]  = {0.8, 0.2, 0.2},
72         [SMOOTHED]        = {1.0, 0.2, 0.2},
73         [MINUTE]          = {0.7, 0.2, 0.7},
74         [TIME_MARKER]     = {1.0, 1.0, 1.0},
75         [TIME_TEXT]       = {0.1, 0.5, 0.1},
76         [DEPTH_MARKER]    = {1.0, 1.0, 1.0},
77         [MEAN_DEPTH]      = {1.0, 0.2, 0.2},
78         [DEPTH_TOP]       = {0.2, 0.2, 0.8},
79         [DEPTH_BOTTOM]    = {0.9, 0.9, 0.9},
80         [DEPTH_FILL_PRINTER] = {1.0, 1.0, 1.0},
81         [DEPTH_STROKE_PRINTER] = {1.0, 1.0, 1.0},
82         [TEMP_TEXT]       = {0.2, 0.2, 0.7},
83         [TEMP_PLOT]       = {0.2, 0.2, 0.9},
84         [SAC_DEFAULT]     = {1.0, 1.0, 1.0},
85         [BOUNDING_BOX]    = {1.0, 1.0, 1.0},
86         [PRESSURE_TEXT]   = {0.2, 0.4, 0.2},
87         [VELO_STABLE]     = {0.0, 0.4, 0.0},
88         [VELO_SLOW]       = {0.4, 0.8, 0.0},
89         [VELO_MODERATE]   = {0.8, 0.8, 0.0},
90         [VELO_FAST]       = {0.8, 0.5, 0.0},
91         [VELO_CRAZY]      = {1.0, 0.0, 0.0},
92         [SAC_1]           = {0.0, 0.4, 0.2},
93         [SAC_2]           = {0.2, 0.6, 0.2},
94         [SAC_3]           = {0.4, 0.8, 0.2},
95         [SAC_4]           = {0.6, 0.8, 0.2},
96         [SAC_5]           = {0.8, 0.8, 0.2},
97         [SAC_6]           = {0.8, 0.6, 0.2},
98         [SAC_7]           = {0.8, 0.4, 0.2},
99         [SAC_8]           = {0.9, 0.3, 0.2},
100         [SAC_9]           = {1.0, 0.2, 0.2},
101         [BACKGROUND]      = {0.95, 0.95, 0.9},
102 };
103
104 #define SAC_COLORS 9
105 static const color_t sac_color[SAC_COLORS] = {
106         SAC_1, SAC_2, SAC_3, SAC_4, SAC_5, SAC_6, SAC_7, SAC_8, SAC_9
107 };
108
109 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
110
111 /* Scale to 0,0 -> maxx,maxy */
112 #define SCALEX(gc,x)  (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
113 #define SCALEY(gc,y)  (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
114 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
115
116 static void move_to(struct graphics_context *gc, double x, double y)
117 {
118         cairo_move_to(gc->cr, SCALE(gc, x, y));
119 }
120
121 static void line_to(struct graphics_context *gc, double x, double y)
122 {
123         cairo_line_to(gc->cr, SCALE(gc, x, y));
124 }
125
126 static void set_source_rgba(struct graphics_context *gc, color_t c, double a)
127 {
128         const rgb_t *rgb = &profile_color[c];
129         double r = rgb->r;
130         double g = rgb->g;
131         double b = rgb->b;
132         /*
133          * For printers, we still honor 'a', but ignore colors
134          * for now. Black is white and white is black
135          */
136         if (gc->printer) {
137                 double sum = r+g+b;
138                 if (sum > 0.8)
139                         r = g = b = 0;
140                 else
141                         r = g = b = 1;
142         }
143         cairo_set_source_rgba(gc->cr, r, g, b, a);
144 }
145
146 static void set_source_rgb(struct graphics_context *gc, color_t c)
147 {
148         set_source_rgba(gc, c, 1);
149 }
150
151 void init_profile_background(struct graphics_context *gc)
152 {
153         set_source_rgb(gc, BACKGROUND);
154 }
155
156 void pattern_add_color_stop_rgba(cairo_pattern_t *pat, double o, color_t c, double a)
157 {
158         const rgb_t *rgb = &profile_color[c];
159         cairo_pattern_add_color_stop_rgba (pat, o, rgb->r, rgb->g, rgb->b, a);
160 }
161
162 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
163
164 /* debugging tool - not normally used */
165 static void dump_pi (struct plot_info *pi)
166 {
167         int i;
168
169         printf("pi:{nr:%d maxtime:%d meandepth:%d maxdepth:%d \n"
170                 "    maxpressure:%d mintemp:%d maxtemp:%d\n",
171                 pi->nr, pi->maxtime, pi->meandepth, pi->maxdepth,
172                 pi->maxpressure, pi->mintemp, pi->maxtemp);
173         for (i = 0; i < pi->nr; i++)
174                 printf("    entry[%d]:{same_cylinder:%d cylinderindex:%d sec:%d pressure:{%d,%d}\n"
175                         "                temperature:%d depth:%d smoothed:%d}\n",
176                         i, pi->entry[i].same_cylinder, pi->entry[i].cylinderindex, pi->entry[i].sec,
177                         pi->entry[i].pressure[0], pi->entry[i].pressure[1],
178                         pi->entry[i].temperature, pi->entry[i].depth, pi->entry[i].smoothed);
179         printf("   }\n");
180 }
181
182 /*
183  * When showing dive profiles, we scale things to the
184  * current dive. However, we don't scale past less than
185  * 30 minutes or 90 ft, just so that small dives show
186  * up as such.
187  * we also need to add 180 seconds at the end so the min/max
188  * plots correctly
189  */
190 static int get_maxtime(struct plot_info *pi)
191 {
192         int seconds = pi->maxtime;
193         /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
194         return MAX(30*60, ROUND_UP(seconds+150, 60*5));
195 }
196
197 static int get_maxdepth(struct plot_info *pi)
198 {
199         unsigned mm = pi->maxdepth;
200         /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
201         return MAX(30000, ROUND_UP(mm+3000, 10000));
202 }
203
204 typedef struct {
205         int size;
206         color_t color;
207         double hpos, vpos;
208 } text_render_options_t;
209
210 #define RIGHT (-1.0)
211 #define CENTER (-0.5)
212 #define LEFT (0.0)
213
214 #define TOP (1)
215 #define MIDDLE (0)
216 #define BOTTOM (-1)
217
218 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
219                       double x, double y, const char *fmt, ...)
220 {
221         cairo_t *cr = gc->cr;
222         cairo_font_extents_t fe;
223         cairo_text_extents_t extents;
224         double dx, dy;
225         char buffer[80];
226         va_list args;
227
228         va_start(args, fmt);
229         vsnprintf(buffer, sizeof(buffer), fmt, args);
230         va_end(args);
231
232         cairo_set_font_size(cr, tro->size);
233         cairo_font_extents(cr, &fe);
234         cairo_text_extents(cr, buffer, &extents);
235         dx = tro->hpos * extents.width + extents.x_bearing;
236         dy = tro->vpos * extents.height + fe.descent;
237
238         move_to(gc, x, y);
239         cairo_rel_move_to(cr, dx, dy);
240
241         cairo_text_path(cr, buffer);
242         set_source_rgba(gc, TEXT_BACKGROUND, 0.95);
243         cairo_stroke(cr);
244
245         move_to(gc, x, y);
246         cairo_rel_move_to(cr, dx, dy);
247
248         set_source_rgb(gc, tro->color);
249         cairo_show_text(cr, buffer);
250 }
251
252 struct ev_select {
253         char *ev_name;
254         gboolean plot_ev;
255 };
256 static struct ev_select *ev_namelist;
257 static int evn_allocated;
258 static int evn_used;
259
260 void evn_foreach(void (*callback)(const char *, int *, void *), void *data)
261 {
262         int i;
263
264         for (i = 0; i < evn_used; i++) {
265                 callback(ev_namelist[i].ev_name, &ev_namelist[i].plot_ev, data);
266         }
267 }
268
269 void remember_event(const char *eventname)
270 {
271         int i=0, len;
272
273         if (!eventname || (len = strlen(eventname)) == 0)
274                 return;
275         while (i < evn_used) {
276                 if (!strncmp(eventname,ev_namelist[i].ev_name,len))
277                         return;
278                 i++;
279         }
280         if (evn_used == evn_allocated) {
281                 evn_allocated += 10;
282                 ev_namelist = realloc(ev_namelist, evn_allocated * sizeof(struct ev_select));
283                 if (! ev_namelist)
284                         /* we are screwed, but let's just bail out */
285                         return;
286         }
287         ev_namelist[evn_used].ev_name = strdup(eventname);
288         ev_namelist[evn_used].plot_ev = TRUE;
289         evn_used++;
290 }
291
292 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
293 {
294         int i, depth = 0;
295         int x,y;
296
297         /* is plotting this event disabled? */
298         if (event->name) {
299                 for (i = 0; i < evn_used; i++) {
300                         if (! strcmp(event->name, ev_namelist[i].ev_name)) {
301                                 if (ev_namelist[i].plot_ev)
302                                         break;
303                                 else
304                                         return;
305                         }
306                 }
307         }
308         for (i = 0; i < pi->nr; i++) {
309                 struct plot_data *data = pi->entry + i;
310                 if (event->time.seconds < data->sec)
311                         break;
312                 depth = data->depth;
313         }
314         /* draw a little tirangular marker and attach tooltip */
315         x = SCALEX(gc, event->time.seconds);
316         y = SCALEY(gc, depth);
317         set_source_rgba(gc, TRIANGULAR_BG, 0.8);
318         cairo_move_to(gc->cr, x-15, y+6);
319         cairo_line_to(gc->cr, x-3  , y+6);
320         cairo_line_to(gc->cr, x-9, y-6);
321         cairo_line_to(gc->cr, x-15, y+6);
322         cairo_stroke_preserve(gc->cr);
323         cairo_fill(gc->cr);
324         set_source_rgba(gc, TRIANGULAR_FG, 0.8);
325         cairo_move_to(gc->cr, x-9, y-3);
326         cairo_line_to(gc->cr, x-9, y+1);
327         cairo_move_to(gc->cr, x-9, y+4);
328         cairo_line_to(gc->cr, x-9, y+4);
329         cairo_stroke(gc->cr);
330         attach_tooltip(x-15, y-6, 12, 12, event->name);
331 }
332
333 static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
334 {
335         static const text_render_options_t tro = {14, EVENTS, CENTER, TOP};
336         struct event *event = dive->events;
337
338         if (gc->printer)
339                 return;
340
341         while (event) {
342                 plot_one_event(gc, pi, event, &tro);
343                 event = event->next;
344         }
345 }
346
347 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
348 {
349         int sec = entry->sec, decimals;
350         double d;
351
352         d = get_depth_units(entry->depth, &decimals, NULL);
353
354         plot_text(gc, tro, sec, entry->depth, "%.*f", decimals, d);
355 }
356
357 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
358 {
359         static const text_render_options_t deep = {14, SAMPLE_DEEP, CENTER, TOP};
360         static const text_render_options_t shallow = {14, SAMPLE_SHALLOW, CENTER, BOTTOM};
361         int i;
362         int last = -1;
363
364         for (i = 0; i < pi->nr; i++) {
365                 struct plot_data *entry = pi->entry + i;
366
367                 if (entry->depth < 2000)
368                         continue;
369
370                 if ((entry == entry->max[2]) && entry->depth != last) {
371                         render_depth_sample(gc, entry, &deep);
372                         last = entry->depth;
373                 }
374
375                 if ((entry == entry->min[2]) && entry->depth != last) {
376                         render_depth_sample(gc, entry, &shallow);
377                         last = entry->depth;
378                 }
379
380                 if (entry->depth != last)
381                         last = -1;
382         }
383 }
384
385 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
386 {
387         int maxtime, maxdepth;
388
389         /* Get plot scaling limits */
390         maxtime = get_maxtime(pi);
391         maxdepth = get_maxdepth(pi);
392
393         gc->leftx = 0; gc->rightx = maxtime;
394         gc->topy = 0; gc->bottomy = maxdepth;
395
396         plot_text_samples(gc, pi);
397 }
398
399 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
400 {
401         int i;
402         struct plot_data *entry = pi->entry;
403
404         set_source_rgba(gc, SMOOTHED, 0.20);
405         move_to(gc, entry->sec, entry->smoothed);
406         for (i = 1; i < pi->nr; i++) {
407                 entry++;
408                 line_to(gc, entry->sec, entry->smoothed);
409         }
410         cairo_stroke(gc->cr);
411 }
412
413 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
414                                 int index, double a)
415 {
416         int i;
417         struct plot_data *entry = pi->entry;
418
419         set_source_rgba(gc, MINUTE, a);
420         move_to(gc, entry->sec, entry->min[index]->depth);
421         for (i = 1; i < pi->nr; i++) {
422                 entry++;
423                 line_to(gc, entry->sec, entry->min[index]->depth);
424         }
425         for (i = 1; i < pi->nr; i++) {
426                 line_to(gc, entry->sec, entry->max[index]->depth);
427                 entry--;
428         }
429         cairo_close_path(gc->cr);
430         cairo_fill(gc->cr);
431 }
432
433 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
434 {
435         if (gc->printer)
436                 return;
437         plot_minmax_profile_minute(gc, pi, 2, 0.1);
438         plot_minmax_profile_minute(gc, pi, 1, 0.1);
439         plot_minmax_profile_minute(gc, pi, 0, 0.1);
440 }
441
442 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
443 {
444         int i, incr;
445         cairo_t *cr = gc->cr;
446         int sec, depth;
447         struct plot_data *entry;
448         int maxtime, maxdepth, marker;
449         int increments[4] = { 5*60, 10*60, 15*60, 30*60 };
450
451         /* Get plot scaling limits */
452         maxtime = get_maxtime(pi);
453         maxdepth = get_maxdepth(pi);
454
455         /* Time markers: at most every 5 min, but no more than 12 markers
456          * and for convenience we do 5, 10, 15 or 30 min intervals.
457          * This allows for 6h dives - enough (I hope) for even the craziest
458          * divers - but just in case, for those 8h depth-record-breaking dives,
459          * we double the interval if this still doesn't get us to 12 or fewer
460          * time markers */
461         i = 0;
462         while (maxtime / increments[i] > 12 && i < 4)
463                 i++;
464         incr = increments[i];
465         while (maxtime / incr > 12)
466                 incr *= 2;
467
468         gc->leftx = 0; gc->rightx = maxtime;
469         gc->topy = 0; gc->bottomy = 1.0;
470         set_source_rgba(gc, TIME_MARKER, 0.5);
471         cairo_set_line_width(gc->cr, 1);
472
473         for (i = incr; i < maxtime; i += incr) {
474                 move_to(gc, i, 0);
475                 line_to(gc, i, 1);
476         }
477         cairo_stroke(cr);
478
479         /* now the text on every second time marker */
480         text_render_options_t tro = {10, TIME_TEXT, CENTER, TOP};
481         for (i = incr; i < maxtime; i += 2 * incr)
482                 plot_text(gc, &tro, i, 1, "%d", i/60);
483
484         /* Depth markers: every 30 ft or 10 m*/
485         gc->leftx = 0; gc->rightx = 1.0;
486         gc->topy = 0; gc->bottomy = maxdepth;
487         switch (output_units.length) {
488         case METERS: marker = 10000; break;
489         case FEET: marker = 9144; break;        /* 30 ft */
490         }
491
492         set_source_rgba(gc, DEPTH_MARKER, 0.5);
493         for (i = marker; i < maxdepth; i += marker) {
494                 move_to(gc, 0, i);
495                 line_to(gc, 1, i);
496         }
497         cairo_stroke(cr);
498
499         /* Show mean depth */
500         if (! gc->printer) {
501                 set_source_rgba(gc, MEAN_DEPTH, 0.40);
502                 move_to(gc, 0, pi->meandepth);
503                 line_to(gc, 1, pi->meandepth);
504                 cairo_stroke(cr);
505         }
506
507         gc->leftx = 0; gc->rightx = maxtime;
508
509         /*
510          * These are good for debugging text placement etc,
511          * but not for actual display..
512          */
513         if (0) {
514                 plot_smoothed_profile(gc, pi);
515                 plot_minmax_profile(gc, pi);
516         }
517
518         /* Do the depth profile for the neat fill */
519         gc->topy = 0; gc->bottomy = maxdepth;
520
521         cairo_pattern_t *pat;
522         pat = cairo_pattern_create_linear (0.0, 0.0,  0.0, 256.0);
523         pattern_add_color_stop_rgba (pat, 1, DEPTH_TOP, 0.6);
524         pattern_add_color_stop_rgba (pat, 0, DEPTH_BOTTOM, 0.6);
525
526         cairo_set_source(gc->cr, pat);
527         cairo_pattern_destroy(pat);
528         cairo_set_line_width(gc->cr, 2);
529
530         entry = pi->entry;
531         move_to(gc, 0, 0);
532         for (i = 0; i < pi->nr; i++, entry++)
533                 line_to(gc, entry->sec, entry->depth);
534         cairo_close_path(gc->cr);
535         if (gc->printer) {
536                 set_source_rgba(gc, DEPTH_FILL_PRINTER, 0.2);
537                 cairo_fill_preserve(cr);
538                 set_source_rgb(gc, DEPTH_STROKE_PRINTER);
539                 cairo_stroke(cr);
540                 return;
541         }
542         cairo_fill(gc->cr);
543
544         /* Now do it again for the velocity colors */
545         entry = pi->entry;
546         for (i = 1; i < pi->nr; i++) {
547                 entry++;
548                 sec = entry->sec;
549                 /* we want to draw the segments in different colors
550                  * representing the vertical velocity, so we need to
551                  * chop this into short segments */
552                 depth = entry->depth;
553                 set_source_rgb(gc, velocity_color[entry->velocity]);
554                 move_to(gc, entry[-1].sec, entry[-1].depth);
555                 line_to(gc, sec, depth);
556                 cairo_stroke(cr);
557         }
558 }
559
560 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
561 {
562         int maxtime, mintemp, maxtemp, delta;
563
564         /* Get plot scaling limits */
565         maxtime = get_maxtime(pi);
566         mintemp = pi->mintemp;
567         maxtemp = pi->maxtemp;
568
569         gc->leftx = 0; gc->rightx = maxtime;
570         /* Show temperatures in roughly the lower third, but make sure the scale
571            is at least somewhat reasonable */
572         delta = maxtemp - mintemp;
573         if (delta > 3000) { /* more than 3K in fluctuation */
574                 gc->topy = maxtemp + delta*2;
575                 gc->bottomy = mintemp - delta/2;
576         } else {
577                 gc->topy = maxtemp + 1500 + delta*2;
578                 gc->bottomy = mintemp - delta/2;
579         }
580
581         return maxtemp > mintemp;
582 }
583
584 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
585 {
586         double deg;
587         const char *unit;
588         static const text_render_options_t tro = {12, TEMP_TEXT, LEFT, TOP};
589
590         deg = get_temp_units(mkelvin, &unit);
591
592         plot_text(gc, &tro, sec, mkelvin, "%d%s", (int)(deg + 0.5), unit);
593 }
594
595 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
596 {
597         int i;
598         int last = -300, sec = 0;
599         int last_temperature = 0, last_printed_temp = 0;
600
601         if (!setup_temperature_limits(gc, pi))
602                 return;
603
604         for (i = 0; i < pi->nr; i++) {
605                 struct plot_data *entry = pi->entry+i;
606                 int mkelvin = entry->temperature;
607
608                 if (!mkelvin)
609                         continue;
610                 last_temperature = mkelvin;
611                 sec = entry->sec;
612                 /* don't print a temperature
613                  * if it's been less than 5min and less than a 2K change OR
614                  * if it's been less than 2min OR if the change from the
615                  * last print is less than .4K (and therefore less than 1F */
616                 if (((sec < last + 300) && (abs(mkelvin - last_printed_temp) < 2000)) ||
617                         (sec < last + 120) ||
618                         (abs(mkelvin - last_printed_temp) < 400))
619                         continue;
620                 last = sec;
621                 plot_single_temp_text(gc,sec,mkelvin);
622                 last_printed_temp = mkelvin;
623         }
624         /* it would be nice to print the end temperature, if it's
625          * different or if the last temperature print has been more
626          * than a quarter of the dive back */
627         if ((abs(last_temperature - last_printed_temp) > 500) ||
628                 ((double)last / (double)sec < 0.75))
629                 plot_single_temp_text(gc, sec, last_temperature);
630 }
631
632 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
633 {
634         int i;
635         cairo_t *cr = gc->cr;
636         int last = 0;
637
638         if (!setup_temperature_limits(gc, pi))
639                 return;
640
641         cairo_set_line_width(gc->cr, 2);
642         set_source_rgba(gc, TEMP_PLOT, 0.8);
643         for (i = 0; i < pi->nr; i++) {
644                 struct plot_data *entry = pi->entry + i;
645                 int mkelvin = entry->temperature;
646                 int sec = entry->sec;
647                 if (!mkelvin) {
648                         if (!last)
649                                 continue;
650                         mkelvin = last;
651                 }
652                 if (last)
653                         line_to(gc, sec, mkelvin);
654                 else
655                         move_to(gc, sec, mkelvin);
656                 last = mkelvin;
657         }
658         cairo_stroke(cr);
659 }
660
661 /* gets both the actual start and end pressure as well as the scaling factors */
662 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
663 {
664         gc->leftx = 0;
665         gc->rightx = get_maxtime(pi);
666
667         gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
668         return pi->maxpressure != 0;
669 }
670
671 /* set the color for the pressure plot according to temporary sac rate
672  * as compared to avg_sac; the calculation simply maps the delta between
673  * sac and avg_sac to indexes 0 .. (SAC_COLORS - 1) with everything
674  * more than 6000 ml/min below avg_sac mapped to 0 */
675
676 static void set_sac_color(struct graphics_context *gc, int sac, int avg_sac)
677 {
678         int sac_index = 0;
679         int delta = sac - avg_sac + 7000;
680
681         if (!gc->printer) {
682                 sac_index = delta / 2000;
683                 if (sac_index < 0)
684                         sac_index = 0;
685                 if (sac_index > SAC_COLORS - 1)
686                         sac_index = SAC_COLORS - 1;
687                 set_source_rgb(gc, sac_color[sac_index]);
688         } else {
689                 set_source_rgb(gc, SAC_DEFAULT);
690         }
691 }
692
693 /* calculate the current SAC in ml/min and convert to int */
694 #define GET_LOCAL_SAC(_entry1, _entry2, _dive)  (int)                           \
695         ((GET_PRESSURE((_entry1)) - GET_PRESSURE((_entry2))) *                  \
696                 (_dive)->cylinder[(_entry1)->cylinderindex].type.size.mliter /  \
697                 (((_entry2)->sec - (_entry1)->sec) / 60.0) /                    \
698                 (1 + ((_entry1)->depth + (_entry2)->depth) / 20000.0) /         \
699                 1000.0)
700
701 #define SAC_WINDOW 45   /* sliding window in seconds for current SAC calculation */
702
703 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi,
704                                 struct dive *dive)
705 {
706         int i;
707         int last = -1;
708         int lift_pen = FALSE;
709         int first_plot = TRUE;
710         int sac = 0;
711         struct plot_data *last_entry = NULL;
712
713         if (!get_cylinder_pressure_range(gc, pi))
714                 return;
715
716         cairo_set_line_width(gc->cr, 2);
717
718         for (i = 0; i < pi->nr; i++) {
719                 int mbar;
720                 struct plot_data *entry = pi->entry + i;
721
722                 mbar = GET_PRESSURE(entry);
723                 if (!entry->same_cylinder) {
724                         lift_pen = TRUE;
725                         last_entry = NULL;
726                 }
727                 if (!mbar) {
728                         lift_pen = TRUE;
729                         continue;
730                 }
731                 if (!last_entry) {
732                         last = i;
733                         last_entry = entry;
734                         sac = GET_LOCAL_SAC(entry, pi->entry + i + 1, dive);
735                 } else {
736                         int j;
737                         sac = 0;
738                         for (j = last; j < i; j++)
739                                 sac += GET_LOCAL_SAC(pi->entry + j, pi->entry + j + 1, dive);
740                         sac /= (i - last);
741                         if (entry->sec - last_entry->sec >= SAC_WINDOW) {
742                                 last++;
743                                 last_entry = pi->entry + last;
744                         }
745                 }
746                 set_sac_color(gc, sac, dive->sac);
747                 if (lift_pen) {
748                         if (!first_plot && entry->same_cylinder) {
749                                 /* if we have a previous event from the same tank,
750                                  * draw at least a short line */
751                                 int prev_pr;
752                                 prev_pr = GET_PRESSURE(entry - 1);
753                                 move_to(gc, (entry-1)->sec, prev_pr);
754                                 line_to(gc, entry->sec, mbar);
755                         } else {
756                                 first_plot = FALSE;
757                                 move_to(gc, entry->sec, mbar);
758                         }
759                         lift_pen = FALSE;
760                 } else {
761                         line_to(gc, entry->sec, mbar);
762                 }
763                 cairo_stroke(gc->cr);
764                 move_to(gc, entry->sec, mbar);
765         }
766 }
767
768 static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec,
769                                 int xalign, int yalign)
770 {
771         int pressure;
772         const char *unit;
773
774         pressure = get_pressure_units(mbar, &unit);
775         text_render_options_t tro = {10, PRESSURE_TEXT, xalign, yalign};
776         plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit);
777 }
778
779 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
780 {
781         int i;
782         int mbar, cyl;
783         int seen_cyl[MAX_CYLINDERS] = { FALSE, };
784         int last_pressure[MAX_CYLINDERS] = { 0, };
785         int last_time[MAX_CYLINDERS] = { 0, };
786         struct plot_data *entry;
787
788         if (!get_cylinder_pressure_range(gc, pi))
789                 return;
790
791         /* only loop over the actual events from the dive computer
792          * plus the second synthetic event at the start (to make sure
793          * we get "time=0" right)
794          * sadly with a recent change that first entry may no longer
795          * have any pressure reading - in that case just grab the
796          * pressure from the second entry */
797         if (GET_PRESSURE(pi->entry + 1) == 0 && GET_PRESSURE(pi->entry + 2) !=0)
798                 INTERPOLATED_PRESSURE(pi->entry + 1) = GET_PRESSURE(pi->entry + 2);
799         for (i = 1; i < pi->nr; i++) {
800                 entry = pi->entry + i;
801
802                 if (!entry->same_cylinder) {
803                         cyl = entry->cylinderindex;
804                         if (!seen_cyl[cyl]) {
805                                 mbar = GET_PRESSURE(entry);
806                                 plot_pressure_value(gc, mbar, entry->sec, LEFT, BOTTOM);
807                                 seen_cyl[cyl] = TRUE;
808                         }
809                         if (i > 2) {
810                                 /* remember the last pressure and time of
811                                  * the previous cylinder */
812                                 cyl = (entry - 1)->cylinderindex;
813                                 last_pressure[cyl] = GET_PRESSURE(entry - 1);
814                                 last_time[cyl] = (entry - 1)->sec;
815                         }
816                 }
817         }
818         cyl = entry->cylinderindex;
819         if (GET_PRESSURE(entry))
820                 last_pressure[cyl] = GET_PRESSURE(entry);
821         last_time[cyl] = entry->sec;
822
823         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
824                 if (last_time[cyl]) {
825                         plot_pressure_value(gc, last_pressure[cyl], last_time[cyl], CENTER, TOP);
826                 }
827         }
828 }
829
830 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
831 {
832         struct plot_data *p = entry;
833         int time = entry->sec;
834         int seconds = 90*(index+1);
835         struct plot_data *min, *max;
836         int avg, nr;
837
838         /* Go back 'seconds' in time */
839         while (p > first) {
840                 if (p[-1].sec < time - seconds)
841                         break;
842                 p--;
843         }
844
845         /* Then go forward until we hit an entry past the time */
846         min = max = p;
847         avg = p->depth;
848         nr = 1;
849         while (++p < last) {
850                 int depth = p->depth;
851                 if (p->sec > time + seconds)
852                         break;
853                 avg += depth;
854                 nr ++;
855                 if (depth < min->depth)
856                         min = p;
857                 if (depth > max->depth)
858                         max = p;
859         }
860         entry->min[index] = min;
861         entry->max[index] = max;
862         entry->avg[index] = (avg + nr/2) / nr;
863 }
864
865 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
866 {
867         analyze_plot_info_minmax_minute(entry, first, last, 0);
868         analyze_plot_info_minmax_minute(entry, first, last, 1);
869         analyze_plot_info_minmax_minute(entry, first, last, 2);
870 }
871
872 static velocity_t velocity(int speed)
873 {
874         velocity_t v;
875
876         if (speed < -304) /* ascent faster than -60ft/min */
877                 v = CRAZY;
878         else if (speed < -152) /* above -30ft/min */
879                 v = FAST;
880         else if (speed < -76) /* -15ft/min */
881                 v = MODERATE;
882         else if (speed < -25) /* -5ft/min */
883                 v = SLOW;
884         else if (speed < 25) /* very hard to find data, but it appears that the recommendations
885                                 for descent are usually about 2x ascent rate; still, we want 
886                                 stable to mean stable */
887                 v = STABLE;
888         else if (speed < 152) /* between 5 and 30ft/min is considered slow */
889                 v = SLOW;
890         else if (speed < 304) /* up to 60ft/min is moderate */
891                 v = MODERATE;
892         else if (speed < 507) /* up to 100ft/min is fast */
893                 v = FAST;
894         else /* more than that is just crazy - you'll blow your ears out */
895                 v = CRAZY;
896
897         return v;
898 }
899 static struct plot_info *analyze_plot_info(struct plot_info *pi)
900 {
901         int i;
902         int nr = pi->nr;
903
904         /* Do pressure min/max based on the non-surface data */
905         for (i = 0; i < nr; i++) {
906                 struct plot_data *entry = pi->entry+i;
907                 int pressure = GET_PRESSURE(entry);
908                 int temperature = entry->temperature;
909
910                 if (pressure) {
911                         if (pressure > pi->maxpressure)
912                                 pi->maxpressure = pressure;
913                 }
914
915                 if (temperature) {
916                         if (!pi->mintemp || temperature < pi->mintemp)
917                                 pi->mintemp = temperature;
918                         if (temperature > pi->maxtemp)
919                                 pi->maxtemp = temperature;
920                 }
921         }
922
923         /* Smoothing function: 5-point triangular smooth */
924         for (i = 2; i < nr; i++) {
925                 struct plot_data *entry = pi->entry+i;
926                 int depth;
927
928                 if (i < nr-2) {
929                         depth = entry[-2].depth + 2*entry[-1].depth + 3*entry[0].depth + 2*entry[1].depth + entry[2].depth;
930                         entry->smoothed = (depth+4) / 9;
931                 }
932                 /* vertical velocity in mm/sec */
933                 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
934                 if (entry[0].sec - entry[-1].sec) {
935                         entry->velocity = velocity((entry[0].depth - entry[-1].depth) / (entry[0].sec - entry[-1].sec));
936                         /* if our samples are short and we aren't too FAST*/
937                         if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
938                                 int past = -2;
939                                 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
940                                         past--;
941                                 entry->velocity = velocity((entry[0].depth - entry[past].depth) / 
942                                                         (entry[0].sec - entry[past].sec));
943                         }
944                 } else
945                         entry->velocity = STABLE;
946         }
947
948         /* One-, two- and three-minute minmax data */
949         for (i = 0; i < nr; i++) {
950                 struct plot_data *entry = pi->entry +i;
951                 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
952         }
953         
954         return pi;
955 }
956
957 /*
958  * simple structure to track the beginning and end tank pressure as
959  * well as the integral of depth over time spent while we have no
960  * pressure reading from the tank */
961 typedef struct pr_track_struct pr_track_t;
962 struct pr_track_struct {
963         int start;
964         int end;
965         int t_start;
966         int t_end;
967         double pressure_time;
968         pr_track_t *next;
969 };
970
971 static pr_track_t *pr_track_alloc(int start, int t_start) {
972         pr_track_t *pt = malloc(sizeof(pr_track_t));
973         pt->start = start;
974         pt->t_start = t_start;
975         pt->end = 0;
976         pt->t_end = 0;
977         pt->pressure_time = 0.0;
978         pt->next = NULL;
979         return pt;
980 }
981
982 /* poor man's linked list */
983 static pr_track_t *list_last(pr_track_t *list)
984 {
985         pr_track_t *tail = list;
986         if (!tail)
987                 return NULL;
988         while (tail->next) {
989                 tail = tail->next;
990         }
991         return tail;
992 }
993
994 static pr_track_t *list_add(pr_track_t *list, pr_track_t *element)
995 {
996         pr_track_t *tail = list_last(list);
997         if (!tail)
998                 return element;
999         tail->next = element;
1000         return list;
1001 }
1002
1003 static void list_free(pr_track_t *list)
1004 {
1005         if (!list)
1006                 return;
1007         list_free(list->next);
1008         free(list);
1009 }
1010
1011 static void dump_pr_track(pr_track_t **track_pr)
1012 {
1013         int cyl;
1014         pr_track_t *list;
1015
1016         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1017                 list = track_pr[cyl];
1018                 while (list) {
1019                         printf("cyl%d: start %d end %d t_start %d t_end %d pt %6.3f\n", cyl,
1020                                 list->start, list->end, list->t_start, list->t_end, list->pressure_time);
1021                         list = list->next;
1022                 }
1023         }
1024 }
1025
1026 static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi,
1027                                         pr_track_t **track_pr)
1028 {
1029         pr_track_t *list = NULL;
1030         pr_track_t *nlist = NULL;
1031         double pt, magic;
1032         int cyl, i;
1033         struct plot_data *entry;
1034         int cur_pr[MAX_CYLINDERS];
1035
1036         if (0) {
1037                 /* another great debugging tool */
1038                 dump_pr_track(track_pr);
1039         }
1040         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1041                 cur_pr[cyl] = track_pr[cyl]->start;
1042         }
1043
1044         /* The first two are "fillers", but in case we don't have a sample
1045          * at time 0 we need to process the second of them here */
1046         for (i = 1; i < pi->nr; i++) {
1047                 entry = pi->entry + i;
1048                 if (SENSOR_PRESSURE(entry)) {
1049                         cur_pr[entry->cylinderindex] = SENSOR_PRESSURE(entry);
1050                 } else {
1051                         if(!list || list->t_end < entry->sec) {
1052                                 nlist = track_pr[entry->cylinderindex];
1053                                 list = NULL;
1054                                 while (nlist && nlist->t_start <= entry->sec) {
1055                                         list = nlist;
1056                                         nlist = list->next;
1057                                 }
1058                                 /* there may be multiple segments - so
1059                                  * let's assemble the length */
1060                                 nlist = list;
1061                                 pt = list->pressure_time;
1062                                 while (!nlist->end) {
1063                                         nlist = nlist->next;
1064                                         if (!nlist) {
1065                                                 /* oops - we have no end pressure,
1066                                                  * so this means this is a tank without
1067                                                  * gas consumption information */
1068                                                 break;
1069                                         }
1070                                         pt += nlist->pressure_time;
1071                                 }
1072                                 if (!nlist) {
1073                                         /* just continue without calculating
1074                                          * interpolated values */
1075                                         INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1076                                         list = NULL;
1077                                         continue;
1078                                 }
1079                                 magic = (nlist->end - cur_pr[entry->cylinderindex]) / pt;
1080                         }
1081                         if (pt != 0.0) {
1082                                 double cur_pt = (entry->sec - (entry-1)->sec) *
1083                                         (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1084                                 INTERPOLATED_PRESSURE(entry) =
1085                                         cur_pr[entry->cylinderindex] + cur_pt * magic + 0.5;
1086                                 cur_pr[entry->cylinderindex] = INTERPOLATED_PRESSURE(entry);
1087                         } else
1088                                 INTERPOLATED_PRESSURE(entry) = cur_pr[entry->cylinderindex];
1089                 }
1090         }
1091 }
1092
1093 static int get_cylinder_index(struct dive *dive, struct event *ev)
1094 {
1095         int i;
1096
1097         /*
1098          * Try to find a cylinder that matches the O2 percentage
1099          * in the gas change event 'value' field.
1100          *
1101          * Crazy suunto gas change events. We really should do
1102          * this in libdivecomputer or something.
1103          */
1104         for (i = 0; i < MAX_CYLINDERS; i++) {
1105                 cylinder_t *cyl = dive->cylinder+i;
1106                 int o2 = (cyl->gasmix.o2.permille + 5) / 10;
1107                 if (o2 == ev->value)
1108                         return i;
1109         }
1110
1111         return 0;
1112 }
1113
1114 static struct event *get_next_gaschange(struct event *event)
1115 {
1116         while (event) {
1117                 if (!strcmp(event->name, "gaschange"))
1118                         return event;
1119                 event = event->next;
1120         }
1121         return event;
1122 }
1123
1124 static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end)
1125 {
1126         while (i < pi->nr) {
1127                 struct plot_data *entry = pi->entry+i;
1128                 if (entry->sec > end)
1129                         break;
1130                 if (entry->cylinderindex != cylinderindex) {
1131                         entry->cylinderindex = cylinderindex;
1132                         entry->pressure[0] = 0;
1133                 }
1134                 i++;
1135         }
1136         return i;
1137 }
1138
1139 static void check_gas_change_events(struct dive *dive, struct plot_info *pi)
1140 {
1141         int i = 0, cylinderindex = 0;
1142         struct event *ev = get_next_gaschange(dive->events);
1143
1144         if (!ev)
1145                 return;
1146
1147         do {
1148                 i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds);
1149                 cylinderindex = get_cylinder_index(dive, ev);
1150                 ev = get_next_gaschange(ev->next);
1151         } while (ev);
1152         set_cylinder_index(pi, i, cylinderindex, ~0u);
1153 }
1154
1155 /* for computers that track gas changes through events */
1156 static int count_gas_change_events(struct dive *dive)
1157 {
1158         int count = 0;
1159         struct event *ev = get_next_gaschange(dive->events);
1160
1161         while (ev) {
1162                 count++;
1163                 ev = get_next_gaschange(ev->next);
1164         }
1165         return count;
1166 }
1167
1168 /*
1169  * Create a plot-info with smoothing and ranged min/max
1170  *
1171  * This also makes sure that we have extra empty events on both
1172  * sides, so that you can do end-points without having to worry
1173  * about it.
1174  */
1175 static struct plot_info *create_plot_info(struct dive *dive, int nr_samples, struct sample *dive_sample)
1176 {
1177         int cylinderindex = -1;
1178         int lastdepth, lastindex;
1179         int i, pi_idx, nr, sec, cyl;
1180         size_t alloc_size;
1181         struct plot_info *pi;
1182         pr_track_t *track_pr[MAX_CYLINDERS] = {NULL, };
1183         pr_track_t *pr_track, *current;
1184         gboolean missing_pr = FALSE;
1185         struct plot_data *entry = NULL;
1186         struct event *ev;
1187
1188         /* we want to potentially add synthetic plot_info elements for the gas changes */
1189         nr = nr_samples + 4 + 2 * count_gas_change_events(dive);
1190         alloc_size = plot_info_size(nr);
1191         pi = malloc(alloc_size);
1192         if (!pi)
1193                 return pi;
1194         memset(pi, 0, alloc_size);
1195         pi->nr = nr;
1196         pi_idx = 2; /* the two extra events at the start */
1197         /* check for gas changes before the samples start */
1198         ev = get_next_gaschange(dive->events);
1199         while (ev && ev->time.seconds < dive_sample->time.seconds) {
1200                 entry = pi->entry + pi_idx;
1201                 entry->sec = ev->time.seconds;
1202                 entry->depth = 0; /* is that always correct ? */
1203                 pi_idx++;
1204                 ev = get_next_gaschange(ev->next);
1205         }
1206         if (ev && ev->time.seconds == dive_sample->time.seconds) {
1207                 /* we already have a sample at the time of the event */
1208                 ev = get_next_gaschange(ev->next);
1209         }
1210         sec = 0;
1211         lastindex = 0;
1212         lastdepth = -1;
1213         for (i = 0; i < nr_samples; i++) {
1214                 int depth;
1215                 int delay = 0;
1216                 struct sample *sample = dive_sample+i;
1217
1218                 entry = pi->entry + i + pi_idx;
1219                 while (ev && ev->time.seconds < sample->time.seconds) {
1220                         /* insert two fake plot info structures for the end of
1221                          * the old tank and the start of the new tank */
1222                         if (ev->time.seconds == sample->time.seconds - 1) {
1223                                 entry->sec = ev->time.seconds - 1;
1224                                 (entry+1)->sec = ev->time.seconds;
1225                         } else {
1226                                 entry->sec = ev->time.seconds;
1227                                 (entry+1)->sec = ev->time.seconds + 1;
1228                         }
1229                         /* we need a fake depth - let's interpolate */
1230                         if (i) {
1231                                 entry->depth = sample->depth.mm -
1232                                         (sample->depth.mm - (sample-1)->depth.mm) / 2;
1233                         } else
1234                                 entry->depth = sample->depth.mm;
1235                         (entry+1)->depth = entry->depth;
1236                         pi_idx += 2;
1237                         entry = pi->entry + i + pi_idx;
1238                         ev = get_next_gaschange(ev->next);
1239                 }
1240                 if (ev && ev->time.seconds == sample->time.seconds) {
1241                         /* we already have a sample at the time of the event
1242                          * just add a new one for the old tank and delay the
1243                          * real even by one second (to keep time monotonous) */
1244                         entry->sec = ev->time.seconds;
1245                         entry->depth = sample->depth.mm;
1246                         pi_idx++;
1247                         entry = pi->entry + i + pi_idx;
1248                         ev = get_next_gaschange(ev->next);
1249                         delay = 1;
1250                 }
1251                 sec = entry->sec = sample->time.seconds + delay;
1252                 depth = entry->depth = sample->depth.mm;
1253                 entry->cylinderindex = sample->cylinderindex;
1254                 SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
1255                 entry->temperature = sample->temperature.mkelvin;
1256
1257                 if (depth || lastdepth)
1258                         lastindex = i + pi_idx;
1259
1260                 lastdepth = depth;
1261                 if (depth > pi->maxdepth)
1262                         pi->maxdepth = depth;
1263         }
1264         entry = pi->entry + i + pi_idx;
1265         /* are there still unprocessed gas changes? that would be very strange */
1266         while (ev) {
1267                 entry->sec = ev->time.seconds;
1268                 entry->depth = 0; /* why are there gas changes after the dive is over? */
1269                 pi_idx++;
1270                 entry = pi->entry + i + pi_idx;
1271                 ev = get_next_gaschange(ev->next);
1272         }
1273         nr = nr_samples + pi_idx - 2;
1274         check_gas_change_events(dive, pi);
1275
1276         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */
1277                 track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1);
1278         current = track_pr[pi->entry[2].cylinderindex];
1279         for (i = 0; i < nr + 1; i++) {
1280                 entry = pi->entry + i + 1;
1281
1282                 entry->same_cylinder = entry->cylinderindex == cylinderindex;
1283                 cylinderindex = entry->cylinderindex;
1284
1285                 /* track the segments per cylinder and their pressure/time integral */
1286                 if (!entry->same_cylinder) {
1287                         current->end = SENSOR_PRESSURE(entry-1);
1288                         current->t_end = (entry-1)->sec;
1289                         current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1290                         track_pr[cylinderindex] = list_add(track_pr[cylinderindex], current);
1291                 } else { /* same cylinder */
1292                         if ((!SENSOR_PRESSURE(entry) && SENSOR_PRESSURE(entry-1)) ||
1293                                 (SENSOR_PRESSURE(entry) && !SENSOR_PRESSURE(entry-1))) {
1294                                 /* transmitter changed its working status */
1295                                 current->end = SENSOR_PRESSURE(entry-1);
1296                                 current->t_end = (entry-1)->sec;
1297                                 current = pr_track_alloc(SENSOR_PRESSURE(entry), entry->sec);
1298                                 track_pr[cylinderindex] =
1299                                         list_add(track_pr[cylinderindex], current);
1300                         }
1301                 }
1302                 /* finally, do the discrete integration to get the SAC rate equivalent */
1303                 current->pressure_time += (entry->sec - (entry-1)->sec) *
1304                         (1 + (entry->depth + (entry-1)->depth) / 20000.0);
1305                 missing_pr |= !SENSOR_PRESSURE(entry);
1306         }
1307
1308         if (entry)
1309                 current->t_end = entry->sec;
1310
1311         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */
1312                 int pr = dive->cylinder[cyl].end.mbar;
1313                 if (pr && track_pr[cyl]) {
1314                         pr_track = list_last(track_pr[cyl]);
1315                         pr_track->end = pr;
1316                 }
1317         }
1318         /* Fill in the last two entries with empty values but valid times
1319          * without creating a false cylinder change event */
1320         i = nr + 2;
1321         pi->entry[i].sec = sec + 20;
1322         pi->entry[i].same_cylinder = 1;
1323         pi->entry[i].cylinderindex = pi->entry[i-1].cylinderindex;
1324         INTERPOLATED_PRESSURE(pi->entry + i) = GET_PRESSURE(pi->entry + i - 1);
1325         pi->entry[i+1].sec = sec + 40;
1326         pi->entry[i+1].same_cylinder = 1;
1327         pi->entry[i+1].cylinderindex = pi->entry[i-1].cylinderindex;
1328         INTERPOLATED_PRESSURE(pi->entry + i + 1) = GET_PRESSURE(pi->entry + i - 1);
1329         /* the number of actual entries - some computers have lots of
1330          * depth 0 samples at the end of a dive, we want to make sure
1331          * we have exactly one of them at the end */
1332         pi->nr = lastindex+1;
1333         while (pi->nr <= i+2 && pi->entry[pi->nr-1].depth > 0)
1334                 pi->nr++;
1335         pi->maxtime = pi->entry[lastindex].sec;
1336
1337         /* Analyze_plot_info() will do the sample max pressures,
1338          * this handles the manual pressures
1339          */
1340         pi->maxpressure = 0;
1341         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) {
1342                 unsigned int mbar = dive->cylinder[cyl].start.mbar;
1343                 if (mbar > pi->maxpressure)
1344                         pi->maxpressure = mbar;
1345         }
1346
1347         pi->meandepth = dive->meandepth.mm;
1348
1349         if (missing_pr) {
1350                 fill_missing_tank_pressures(dive, pi, track_pr);
1351         }
1352         for (cyl = 0; cyl < MAX_CYLINDERS; cyl++)
1353                 list_free(track_pr[cyl]);
1354         if (0) /* awesome for debugging - not useful otherwise */
1355                 dump_pi(pi);
1356         return analyze_plot_info(pi);
1357 }
1358
1359 void plot(struct graphics_context *gc, cairo_rectangle_int_t *drawing_area, struct dive *dive)
1360 {
1361         struct plot_info *pi;
1362         static struct sample fake[4];
1363         struct sample *sample = dive->sample;
1364         int nr = dive->samples;
1365
1366         if (!nr) {
1367                 int duration = dive->duration.seconds;
1368                 int maxdepth = dive->maxdepth.mm;
1369                 sample = fake;
1370                 fake[1].time.seconds = duration * 0.05;
1371                 fake[1].depth.mm = maxdepth;
1372                 fake[2].time.seconds = duration * 0.95;
1373                 fake[2].depth.mm = maxdepth;
1374                 fake[3].time.seconds = duration * 1.00;
1375                 nr = 4;
1376         }
1377
1378         pi = create_plot_info(dive, nr, sample);
1379
1380         cairo_translate(gc->cr, drawing_area->x, drawing_area->y);
1381         cairo_set_line_width(gc->cr, 1);
1382         cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
1383         cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
1384
1385         /*
1386          * We can use "cairo_translate()" because that doesn't
1387          * scale line width etc. But the actual scaling we need
1388          * do set up ourselves..
1389          *
1390          * Snif. What a pity.
1391          */
1392         gc->maxx = (drawing_area->width - 2*drawing_area->x);
1393         gc->maxy = (drawing_area->height - 2*drawing_area->y);
1394
1395         /* Temperature profile */
1396         plot_temperature_profile(gc, pi);
1397
1398         /* Depth profile */
1399         plot_depth_profile(gc, pi);
1400         plot_events(gc, pi, dive);
1401
1402         /* Cylinder pressure plot */
1403         plot_cylinder_pressure(gc, pi, dive);
1404
1405         /* Text on top of all graphs.. */
1406         plot_temperature_text(gc, pi);
1407         plot_depth_text(gc, pi);
1408         plot_cylinder_pressure_text(gc, pi);
1409
1410         /* Bounding box last */
1411         gc->leftx = 0; gc->rightx = 1.0;
1412         gc->topy = 0; gc->bottomy = 1.0;
1413
1414         set_source_rgb(gc, BOUNDING_BOX);
1415         cairo_set_line_width(gc->cr, 1);
1416         move_to(gc, 0, 0);
1417         line_to(gc, 0, 1);
1418         line_to(gc, 1, 1);
1419         line_to(gc, 1, 0);
1420         cairo_close_path(gc->cr);
1421         cairo_stroke(gc->cr);
1422
1423         free(pi);
1424 }