]> git.tdb.fi Git - ext/subsurface.git/blob - profile.c
Fix the profile coloring
[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 minpressure, maxpressure;
25         int mintemp, maxtemp;
26         struct plot_data {
27                 int sec;
28                 int pressure, temperature;
29                 /* Depth info */
30                 int val;
31                 int smoothed;
32                 velocity_t velocity;
33                 struct plot_data *min[3];
34                 struct plot_data *max[3];
35                 int avg[3];
36         } entry[];
37 };
38
39 /* convert velocity to colors */
40 typedef struct { double r, g, b; } rgb_t;
41 static const rgb_t rgb[] = {
42         [STABLE]   = {0.0, 0.4, 0.0},
43         [SLOW]     = {0.4, 0.8, 0.0},
44         [MODERATE] = {0.8, 0.8, 0.0},
45         [FAST]     = {0.8, 0.5, 0.0},
46         [CRAZY]    = {1.0, 0.0, 0.0},
47 };
48
49 #define plot_info_size(nr) (sizeof(struct plot_info) + (nr)*sizeof(struct plot_data))
50
51 /* Scale to 0,0 -> maxx,maxy */
52 #define SCALEX(gc,x)  (((x)-gc->leftx)/(gc->rightx-gc->leftx)*gc->maxx)
53 #define SCALEY(gc,y)  (((y)-gc->topy)/(gc->bottomy-gc->topy)*gc->maxy)
54 #define SCALE(gc,x,y) SCALEX(gc,x),SCALEY(gc,y)
55
56 static void move_to(struct graphics_context *gc, double x, double y)
57 {
58         cairo_move_to(gc->cr, SCALE(gc, x, y));
59 }
60
61 static void line_to(struct graphics_context *gc, double x, double y)
62 {
63         cairo_line_to(gc->cr, SCALE(gc, x, y));
64 }
65
66 static void set_source_rgba(struct graphics_context *gc, double r, double g, double b, double a)
67 {
68         /*
69          * For printers, we still honor 'a', but ignore colors
70          * for now. Black is white and white is black
71          */
72         if (gc->printer) {
73                 double sum = r+g+b;
74                 if (sum > 0.8)
75                         r = g = b = 0;
76                 else
77                         r = g = b = 1;
78         }
79         cairo_set_source_rgba(gc->cr, r, g, b, a);
80 }
81
82 void set_source_rgb(struct graphics_context *gc, double r, double g, double b)
83 {
84         set_source_rgba(gc, r, g, b, 1);
85 }
86
87 #define ROUND_UP(x,y) ((((x)+(y)-1)/(y))*(y))
88
89 /*
90  * When showing dive profiles, we scale things to the
91  * current dive. However, we don't scale past less than
92  * 30 minutes or 90 ft, just so that small dives show
93  * up as such.
94  * we also need to add 180 seconds at the end so the min/max
95  * plots correctly
96  */
97 static int get_maxtime(struct plot_info *pi)
98 {
99         int seconds = pi->maxtime;
100         /* min 30 minutes, rounded up to 5 minutes, with at least 2.5 minutes to spare */
101         return MAX(30*60, ROUND_UP(seconds+150, 60*5));
102 }
103
104 static int get_maxdepth(struct plot_info *pi)
105 {
106         unsigned mm = pi->maxdepth;
107         /* Minimum 30m, rounded up to 10m, with at least 3m to spare */
108         return MAX(30000, ROUND_UP(mm+3000, 10000));
109 }
110
111 typedef struct {
112         int size;
113         double r,g,b;
114         double hpos, vpos;
115 } text_render_options_t;
116
117 #define RIGHT (-1.0)
118 #define CENTER (-0.5)
119 #define LEFT (0.0)
120
121 #define TOP (1)
122 #define MIDDLE (0)
123 #define BOTTOM (-1)
124
125 static void plot_text(struct graphics_context *gc, const text_render_options_t *tro,
126                       double x, double y, const char *fmt, ...)
127 {
128         cairo_t *cr = gc->cr;
129         cairo_font_extents_t fe;
130         cairo_text_extents_t extents;
131         double dx, dy;
132         char buffer[80];
133         va_list args;
134
135         va_start(args, fmt);
136         vsnprintf(buffer, sizeof(buffer), fmt, args);
137         va_end(args);
138
139         cairo_set_font_size(cr, tro->size);
140         cairo_font_extents(cr, &fe);
141         cairo_text_extents(cr, buffer, &extents);
142         dx = tro->hpos * extents.width + extents.x_bearing;
143         dy = tro->vpos * extents.height + fe.descent;
144
145         move_to(gc, x, y);
146         cairo_rel_move_to(cr, dx, dy);
147
148         cairo_text_path(cr, buffer);
149         set_source_rgb(gc, 0, 0, 0);
150         cairo_stroke(cr);
151
152         move_to(gc, x, y);
153         cairo_rel_move_to(cr, dx, dy);
154
155         set_source_rgb(gc, tro->r, tro->g, tro->b);
156         cairo_show_text(cr, buffer);
157 }
158
159 static void plot_one_event(struct graphics_context *gc, struct plot_info *pi, struct event *event, const text_render_options_t *tro)
160 {
161         int i, depth = 0;
162
163         for (i = 0; i < pi->nr; i++) {
164                 struct plot_data *data = pi->entry + i;
165                 if (event->time.seconds < data->sec)
166                         break;
167                 depth = data->val;
168         }
169         plot_text(gc, tro, event->time.seconds, depth, "%s", event->name);
170 }
171
172 static void plot_events(struct graphics_context *gc, struct plot_info *pi, struct dive *dive)
173 {
174         static const text_render_options_t tro = {14, 1.0, 0.2, 0.2, CENTER, TOP};
175         struct event *event = dive->events;
176
177         if (gc->printer)
178                 return;
179
180         while (event) {
181                 plot_one_event(gc, pi, event, &tro);
182                 event = event->next;
183         }
184 }
185
186 static void render_depth_sample(struct graphics_context *gc, struct plot_data *entry, const text_render_options_t *tro)
187 {
188         int sec = entry->sec, decimals;
189         double d;
190
191         d = get_depth_units(entry->val, &decimals, NULL);
192
193         plot_text(gc, tro, sec, entry->val, "%.*f", decimals, d);
194 }
195
196 static void plot_text_samples(struct graphics_context *gc, struct plot_info *pi)
197 {
198         static const text_render_options_t deep = {14, 1.0, 0.2, 0.2, CENTER, TOP};
199         static const text_render_options_t shallow = {14, 1.0, 0.2, 0.2, CENTER, BOTTOM};
200         int i;
201
202         for (i = 0; i < pi->nr; i++) {
203                 struct plot_data *entry = pi->entry + i;
204
205                 if (entry->val < 2000)
206                         continue;
207
208                 if (entry == entry->max[2])
209                         render_depth_sample(gc, entry, &deep);
210
211                 if (entry == entry->min[2])
212                         render_depth_sample(gc, entry, &shallow);
213         }
214 }
215
216 static void plot_depth_text(struct graphics_context *gc, struct plot_info *pi)
217 {
218         int maxtime, maxdepth;
219
220         /* Get plot scaling limits */
221         maxtime = get_maxtime(pi);
222         maxdepth = get_maxdepth(pi);
223
224         gc->leftx = 0; gc->rightx = maxtime;
225         gc->topy = 0; gc->bottomy = maxdepth;
226
227         plot_text_samples(gc, pi);
228 }
229
230 static void plot_smoothed_profile(struct graphics_context *gc, struct plot_info *pi)
231 {
232         int i;
233         struct plot_data *entry = pi->entry;
234
235         set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
236         move_to(gc, entry->sec, entry->smoothed);
237         for (i = 1; i < pi->nr; i++) {
238                 entry++;
239                 line_to(gc, entry->sec, entry->smoothed);
240         }
241         cairo_stroke(gc->cr);
242 }
243
244 static void plot_minmax_profile_minute(struct graphics_context *gc, struct plot_info *pi,
245                                 int index, double a)
246 {
247         int i;
248         struct plot_data *entry = pi->entry;
249
250         set_source_rgba(gc, 1, 0.2, 1, a);
251         move_to(gc, entry->sec, entry->min[index]->val);
252         for (i = 1; i < pi->nr; i++) {
253                 entry++;
254                 line_to(gc, entry->sec, entry->min[index]->val);
255         }
256         for (i = 1; i < pi->nr; i++) {
257                 line_to(gc, entry->sec, entry->max[index]->val);
258                 entry--;
259         }
260         cairo_close_path(gc->cr);
261         cairo_fill(gc->cr);
262 }
263
264 static void plot_minmax_profile(struct graphics_context *gc, struct plot_info *pi)
265 {
266         if (gc->printer)
267                 return;
268         plot_minmax_profile_minute(gc, pi, 2, 0.1);
269         plot_minmax_profile_minute(gc, pi, 1, 0.1);
270         plot_minmax_profile_minute(gc, pi, 0, 0.1);
271 }
272
273 static void plot_depth_profile(struct graphics_context *gc, struct plot_info *pi)
274 {
275         int i;
276         cairo_t *cr = gc->cr;
277         int sec, depth;
278         struct plot_data *entry;
279         int maxtime, maxdepth, marker;
280
281         /* Get plot scaling limits */
282         maxtime = get_maxtime(pi);
283         maxdepth = get_maxdepth(pi);
284
285         /* Time markers: every 5 min */
286         gc->leftx = 0; gc->rightx = maxtime;
287         gc->topy = 0; gc->bottomy = 1.0;
288         for (i = 5*60; i < maxtime; i += 5*60) {
289                 move_to(gc, i, 0);
290                 line_to(gc, i, 1);
291         }
292
293         /* Depth markers: every 30 ft or 10 m*/
294         gc->leftx = 0; gc->rightx = 1.0;
295         gc->topy = 0; gc->bottomy = maxdepth;
296         switch (output_units.length) {
297         case METERS: marker = 10000; break;
298         case FEET: marker = 9144; break;        /* 30 ft */
299         }
300
301         set_source_rgba(gc, 1, 1, 1, 0.5);
302         for (i = marker; i < maxdepth; i += marker) {
303                 move_to(gc, 0, i);
304                 line_to(gc, 1, i);
305         }
306         cairo_stroke(cr);
307
308         /* Show mean depth */
309         if (! gc->printer) {
310                 set_source_rgba(gc, 1, 0.2, 0.2, 0.40);
311                 move_to(gc, 0, pi->meandepth);
312                 line_to(gc, 1, pi->meandepth);
313                 cairo_stroke(cr);
314         }
315
316         gc->leftx = 0; gc->rightx = maxtime;
317
318         /*
319          * These are good for debugging text placement etc,
320          * but not for actual display..
321          */
322         if (0) {
323                 plot_smoothed_profile(gc, pi);
324                 plot_minmax_profile(gc, pi);
325         }
326
327         set_source_rgba(gc, 1, 0.2, 0.2, 0.80);
328
329         /* Do the depth profile for the neat fill */
330         gc->topy = 0; gc->bottomy = maxdepth;
331         set_source_rgba(gc, 1, 0.2, 0.2, 0.20);
332
333         entry = pi->entry;
334         move_to(gc, 0, 0);
335         for (i = 0; i < pi->nr; i++, entry++)
336                 line_to(gc, entry->sec, entry->val);
337         cairo_close_path(gc->cr);
338         if (gc->printer) {
339                 set_source_rgba(gc, 1, 1, 1, 0.2);
340                 cairo_fill_preserve(cr);
341                 set_source_rgb(gc, 1, 1, 1);
342                 cairo_stroke(cr);
343                 return;
344         }
345         cairo_fill(gc->cr);
346
347         /* Now do it again for the velocity colors */
348         entry = pi->entry;
349         for (i = 1; i < pi->nr; i++) {
350                 entry++;
351                 sec = entry->sec;
352                 /* we want to draw the segments in different colors
353                  * representing the vertical velocity, so we need to
354                  * chop this into short segments */
355                 rgb_t color = rgb[entry->velocity];
356                 depth = entry->val;
357                 set_source_rgb(gc, color.r, color.g, color.b);
358                 move_to(gc, entry[-1].sec, entry[-1].val);
359                 line_to(gc, sec, depth);
360                 cairo_stroke(cr);
361         }
362 }
363
364 static int setup_temperature_limits(struct graphics_context *gc, struct plot_info *pi)
365 {
366         int maxtime, mintemp, maxtemp, delta;
367
368         /* Get plot scaling limits */
369         maxtime = get_maxtime(pi);
370         mintemp = pi->mintemp;
371         maxtemp = pi->maxtemp;
372
373         gc->leftx = 0; gc->rightx = maxtime;
374         /* Show temperatures in roughly the lower third, but make sure the scale
375            is at least somewhat reasonable */
376         delta = maxtemp - mintemp;
377         if (delta > 3000) { /* more than 3K in fluctuation */
378                 gc->topy = maxtemp + delta*2;
379                 gc->bottomy = mintemp - delta/2;
380         } else {
381                 gc->topy = maxtemp + 1500 + delta*2;
382                 gc->bottomy = mintemp - delta/2;
383         }
384
385         return maxtemp > mintemp;
386 }
387
388 static void plot_single_temp_text(struct graphics_context *gc, int sec, int mkelvin)
389 {
390         int deg;
391         const char *unit;
392         static const text_render_options_t tro = {12, 0.2, 0.2, 1.0, LEFT, TOP};
393         temperature_t temperature = { mkelvin };
394
395         if (output_units.temperature == FAHRENHEIT) {
396                 deg = to_F(temperature);
397                 unit = UTF8_DEGREE "F";
398         } else {
399                 deg = to_C(temperature);
400                 unit = UTF8_DEGREE "C";
401         }
402         plot_text(gc, &tro, sec, temperature.mkelvin, "%d%s", deg, unit);
403 }
404
405 static void plot_temperature_text(struct graphics_context *gc, struct plot_info *pi)
406 {
407         int i;
408         int last = 0, sec = 0;
409         int last_temperature = 0, last_printed_temp = 0;
410
411         if (!setup_temperature_limits(gc, pi))
412                 return;
413
414         for (i = 0; i < pi->nr; i++) {
415                 struct plot_data *entry = pi->entry+i;
416                 int mkelvin = entry->temperature;
417
418                 if (!mkelvin)
419                         continue;
420                 last_temperature = mkelvin;
421                 sec = entry->sec;
422                 if (sec < last + 300)
423                         continue;
424                 last = sec;
425                 plot_single_temp_text(gc,sec,mkelvin);
426                 last_printed_temp = mkelvin;
427         }
428         /* it would be nice to print the end temperature, if it's different */
429         if (abs(last_temperature - last_printed_temp) > 500)
430                 plot_single_temp_text(gc, sec, last_temperature);
431 }
432
433 static void plot_temperature_profile(struct graphics_context *gc, struct plot_info *pi)
434 {
435         int i;
436         cairo_t *cr = gc->cr;
437         int last = 0;
438
439         if (!setup_temperature_limits(gc, pi))
440                 return;
441
442         set_source_rgba(gc, 0.2, 0.2, 1.0, 0.8);
443         for (i = 0; i < pi->nr; i++) {
444                 struct plot_data *entry = pi->entry + i;
445                 int mkelvin = entry->temperature;
446                 int sec = entry->sec;
447                 if (!mkelvin) {
448                         if (!last)
449                                 continue;
450                         mkelvin = last;
451                 }
452                 if (last)
453                         line_to(gc, sec, mkelvin);
454                 else
455                         move_to(gc, sec, mkelvin);
456                 last = mkelvin;
457         }
458         cairo_stroke(cr);
459 }
460
461 /* gets both the actual start and end pressure as well as the scaling factors */
462 static int get_cylinder_pressure_range(struct graphics_context *gc, struct plot_info *pi)
463 {
464         gc->leftx = 0;
465         gc->rightx = get_maxtime(pi);
466
467         gc->bottomy = 0; gc->topy = pi->maxpressure * 1.5;
468         return pi->maxpressure != 0;
469 }
470
471 static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info *pi)
472 {
473         int i;
474
475         if (!get_cylinder_pressure_range(gc, pi))
476                 return;
477
478         set_source_rgba(gc, 0.2, 1.0, 0.2, 0.80);
479
480         move_to(gc, 0, pi->maxpressure);
481         for (i = 1; i < pi->nr; i++) {
482                 int mbar;
483                 struct plot_data *entry = pi->entry + i;
484
485                 mbar = entry->pressure;
486                 if (!mbar)
487                         continue;
488                 line_to(gc, entry->sec, mbar);
489         }
490         line_to(gc, pi->maxtime, pi->minpressure);
491         cairo_stroke(gc->cr);
492 }
493
494 static int mbar_to_PSI(int mbar)
495 {
496         pressure_t p = {mbar};
497         return to_PSI(p);
498 }
499
500 static void plot_cylinder_pressure_text(struct graphics_context *gc, struct plot_info *pi)
501 {
502         if (get_cylinder_pressure_range(gc, pi)) {
503                 int start, end;
504                 const char *unit = "bar";
505
506                 switch (output_units.pressure) {
507                 case PASCAL:
508                         start = pi->maxpressure * 100;
509                         end = pi->minpressure * 100;
510                         unit = "pascal";
511                         break;
512                 case BAR:
513                         start = (pi->maxpressure + 500) / 1000;
514                         end = (pi->minpressure + 500) / 1000;
515                         unit = "bar";
516                         break;
517                 case PSI:
518                         start = mbar_to_PSI(pi->maxpressure);
519                         end = mbar_to_PSI(pi->minpressure);
520                         unit = "psi";
521                         break;
522                 }
523
524                 text_render_options_t tro = {10, 0.2, 1.0, 0.2, LEFT, TOP};
525                 plot_text(gc, &tro, 0, pi->maxpressure, "%d %s", start, unit);
526                 plot_text(gc, &tro, pi->maxtime, pi->minpressure,
527                           "%d %s", end, unit);
528         }
529 }
530
531 static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
532 {
533         struct plot_data *p = entry;
534         int time = entry->sec;
535         int seconds = 90*(index+1);
536         struct plot_data *min, *max;
537         int avg, nr;
538
539         /* Go back 'seconds' in time */
540         while (p > first) {
541                 if (p[-1].sec < time - seconds)
542                         break;
543                 p--;
544         }
545
546         /* Then go forward until we hit an entry past the time */
547         min = max = p;
548         avg = p->val;
549         nr = 1;
550         while (++p < last) {
551                 int val = p->val;
552                 if (p->sec > time + seconds)
553                         break;
554                 avg += val;
555                 nr ++;
556                 if (val < min->val)
557                         min = p;
558                 if (val > max->val)
559                         max = p;
560         }
561         entry->min[index] = min;
562         entry->max[index] = max;
563         entry->avg[index] = (avg + nr/2) / nr;
564 }
565
566 static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
567 {
568         analyze_plot_info_minmax_minute(entry, first, last, 0);
569         analyze_plot_info_minmax_minute(entry, first, last, 1);
570         analyze_plot_info_minmax_minute(entry, first, last, 2);
571 }
572
573 static velocity_t velocity(int speed)
574 {
575         velocity_t v;
576
577         if (speed < -304) /* ascent faster than -60ft/min */
578                 v = CRAZY;
579         else if (speed < -152) /* above -30ft/min */
580                 v = FAST;
581         else if (speed < -76) /* -15ft/min */
582                 v = MODERATE;
583         else if (speed < -25) /* -5ft/min */
584                 v = SLOW;
585         else if (speed < 25) /* very hard to find data, but it appears that the recommendations
586                                 for descent are usually about 2x ascent rate; still, we want 
587                                 stable to mean stable */
588                 v = STABLE;
589         else if (speed < 152) /* between 5 and 30ft/min is considered slow */
590                 v = SLOW;
591         else if (speed < 304) /* up to 60ft/min is moderate */
592                 v = MODERATE;
593         else if (speed < 507) /* up to 100ft/min is fast */
594                 v = FAST;
595         else /* more than that is just crazy - you'll blow your ears out */
596                 v = CRAZY;
597
598         return v;
599 }
600 static struct plot_info *analyze_plot_info(struct plot_info *pi)
601 {
602         int i;
603         int nr = pi->nr;
604
605         /* Do pressure min/max based on the non-surface data */
606         for (i = 0; i < nr; i++) {
607                 struct plot_data *entry = pi->entry+i;
608                 int pressure = entry->pressure;
609                 int temperature = entry->temperature;
610
611                 if (pressure) {
612                         if (!pi->minpressure || pressure < pi->minpressure)
613                                 pi->minpressure = pressure;
614                         if (pressure > pi->maxpressure)
615                                 pi->maxpressure = pressure;
616                 }
617
618                 if (temperature) {
619                         if (!pi->mintemp || temperature < pi->mintemp)
620                                 pi->mintemp = temperature;
621                         if (temperature > pi->maxtemp)
622                                 pi->maxtemp = temperature;
623                 }
624         }
625
626         /* Smoothing function: 5-point triangular smooth */
627         for (i = 2; i < nr; i++) {
628                 struct plot_data *entry = pi->entry+i;
629                 int val;
630
631                 if (i < nr-2) {
632                         val = entry[-2].val + 2*entry[-1].val + 3*entry[0].val + 2*entry[1].val + entry[2].val;
633                         entry->smoothed = (val+4) / 9;
634                 }
635                 /* vertical velocity in mm/sec */
636                 /* Linus wants to smooth this - let's at least look at the samples that aren't FAST or CRAZY */
637                 if (entry[0].sec - entry[-1].sec) {
638                         entry->velocity = velocity((entry[0].val - entry[-1].val) / (entry[0].sec - entry[-1].sec));
639                         /* if our samples are short and we aren't too FAST*/
640                         if (entry[0].sec - entry[-1].sec < 15 && entry->velocity < FAST) {
641                                 int past = -2;
642                                 while (i+past > 0 && entry[0].sec - entry[past].sec < 15)
643                                         past--;
644                                 entry->velocity = velocity((entry[0].val - entry[past].val) / 
645                                                         (entry[0].sec - entry[past].sec));
646                         }
647                 } else
648                         entry->velocity = STABLE;
649         }
650
651         /* One-, two- and three-minute minmax data */
652         for (i = 0; i < nr; i++) {
653                 struct plot_data *entry = pi->entry +i;
654                 analyze_plot_info_minmax(entry, pi->entry, pi->entry+nr);
655         }
656         
657         return pi;
658 }
659
660 /*
661  * Create a plot-info with smoothing and ranged min/max
662  *
663  * This also makes sure that we have extra empty events on both
664  * sides, so that you can do end-points without having to worry
665  * about it.
666  */
667 static struct plot_info *create_plot_info(struct dive *dive)
668 {
669         int lastdepth, lastindex;
670         int i, nr = dive->samples + 4, sec;
671         size_t alloc_size = plot_info_size(nr);
672         struct plot_info *pi;
673
674         pi = malloc(alloc_size);
675         if (!pi)
676                 return pi;
677         memset(pi, 0, alloc_size);
678         pi->nr = nr;
679         sec = 0;
680         lastindex = 0;
681         lastdepth = -1;
682         for (i = 0; i < dive->samples; i++) {
683                 int depth;
684                 struct sample *sample = dive->sample+i;
685                 struct plot_data *entry = pi->entry + i + 2;
686
687                 sec = entry->sec = sample->time.seconds;
688                 depth = entry->val = sample->depth.mm;
689                 entry->pressure = sample->cylinderpressure.mbar;
690                 entry->temperature = sample->temperature.mkelvin;
691
692                 if (depth || lastdepth)
693                         lastindex = i+2;
694
695                 lastdepth = depth;
696                 if (depth > pi->maxdepth)
697                         pi->maxdepth = depth;
698         }
699         if (lastdepth)
700                 lastindex = i + 2;
701         /* Fill in the last two entries with empty values but valid times */
702         i = dive->samples + 2;
703         pi->entry[i].sec = sec + 20;
704         pi->entry[i+1].sec = sec + 40;
705
706         pi->nr = lastindex+1;
707         pi->maxtime = pi->entry[lastindex].sec;
708
709         pi->minpressure = dive->cylinder[0].end.mbar;
710         pi->maxpressure = dive->cylinder[0].start.mbar;
711
712         pi->meandepth = dive->meandepth.mm;
713
714         return analyze_plot_info(pi);
715 }
716
717 void plot(struct graphics_context *gc, int w, int h, struct dive *dive)
718 {
719         double topx, topy;
720         struct plot_info *pi = create_plot_info(dive);
721
722         topx = w / 20.0;
723         topy = h / 20.0;
724         cairo_translate(gc->cr, topx, topy);
725         cairo_set_line_width(gc->cr, 2);
726         cairo_set_line_cap(gc->cr, CAIRO_LINE_CAP_ROUND);
727         cairo_set_line_join(gc->cr, CAIRO_LINE_JOIN_ROUND);
728
729         /*
730          * We can use "cairo_translate()" because that doesn't
731          * scale line width etc. But the actual scaling we need
732          * do set up ourselves..
733          *
734          * Snif. What a pity.
735          */
736         gc->maxx = (w - 2*topx);
737         gc->maxy = (h - 2*topy);
738
739         /* Temperature profile */
740         plot_temperature_profile(gc, pi);
741
742         /* Cylinder pressure plot */
743         plot_cylinder_pressure(gc, pi);
744
745         /* Depth profile */
746         plot_depth_profile(gc, pi);
747         plot_events(gc, pi, dive);
748
749         /* Text on top of all graphs.. */
750         plot_temperature_text(gc, pi);
751         plot_depth_text(gc, pi);
752         plot_cylinder_pressure_text(gc, pi);
753
754         /* Bounding box last */
755         gc->leftx = 0; gc->rightx = 1.0;
756         gc->topy = 0; gc->bottomy = 1.0;
757
758         set_source_rgb(gc, 1, 1, 1);
759         move_to(gc, 0, 0);
760         line_to(gc, 0, 1);
761         line_to(gc, 1, 1);
762         line_to(gc, 1, 0);
763         cairo_close_path(gc->cr);
764         cairo_stroke(gc->cr);
765
766         free(pi);
767 }