1 /*
2  # This file is part of the Astrometry.net suite.
3  # Licensed under a 3-clause BSD style license - see LICENSE
4  */
5 
6 #ifndef PLOTSTUFF_H
7 #define PLOTSTUFF_H
8 
9 #include <stdio.h>
10 #include <cairo.h>
11 
12 #include "astrometry/keywords.h"
13 #include "astrometry/sip.h"
14 #include "astrometry/bl.h"
15 #include "astrometry/anwcs.h"
16 #include "astrometry/an-bool.h"
17 
18 #define PLOTSTUFF_FORMAT_JPG 1
19 #define PLOTSTUFF_FORMAT_PNG 2
20 #define PLOTSTUFF_FORMAT_PPM 3
21 #define PLOTSTUFF_FORMAT_PDF 4
22 // Save the image as RGBA image "pargs->outimage"
23 #define PLOTSTUFF_FORMAT_MEMIMG 5
24 #define PLOTSTUFF_FORMAT_FITS 6
25 
26 struct plotter;
27 typedef struct plotter plotter_t;
28 
29 struct plot_args {
30     // the workers
31     plotter_t* plotters;
32     int NP;
33 
34     char* outfn;
35     FILE* fout;
36     int outformat;
37 
38     unsigned char* outimage;
39 
40     cairo_t* cairo;
41     cairo_surface_t* target;
42 
43     cairo_operator_t op;
44 
45     // functions to call instead of cairo_move_to / cairo_line_to.
46     void (*move_to)(struct plot_args* pargs, double x, double y, void* baton);
47     void* move_to_baton;
48     void (*line_to)(struct plot_args* pargs, double x, double y, void* baton);
49     void* line_to_baton;
50 
51     anwcs_t* wcs;
52 
53     int W, H;
54     float rgba[4];
55     float lw; // default: 1
56     int marker; // default: circle
57     float markersize; // default: 5
58 
59     float bg_rgba[4];
60     float bg_lw; // default: 3
61     int bg_box; // plot a rectangle for text backgrounds.
62 
63     float fontsize; // default: 20
64 
65     // text alignment
66     char halign; // L, R, C
67     char valign; // T, B, C
68 
69     double label_offset_x;
70     double label_offset_y;
71 
72     int text_bg_layer;
73     int text_fg_layer;
74     int marker_fg_layer;
75 
76     bl* cairocmds;
77 
78     // step size in pixels for drawing curved lines in RA,Dec; default 10
79     float linestep;
80 };
81 typedef struct plot_args plot_args_t;
82 
83 typedef void* (*plot_func_init_t)(plot_args_t* args);
84 typedef int   (*plot_func_init2_t)(plot_args_t* args, void* baton);
85 typedef int   (*plot_func_command_t)(const char* command, const char* cmdargs, plot_args_t* args, void* baton);
86 typedef int   (*plot_func_plot_t)(const char* command, cairo_t* cr, plot_args_t* args, void* baton);
87 typedef void  (*plot_func_free_t)(plot_args_t* args, void* baton);
88 
89 struct plotter {
90     // don't change the order of these fields!
91     char* name;
92     plot_func_init_t init;
93     plot_func_init2_t init2;
94     plot_func_command_t command;
95     plot_func_plot_t doplot;
96     plot_func_free_t free;
97     void* baton;
98 };
99 
100 //#define DECLARE_PLOTTER(name) plotter_t* plot_ ## name ## _new()
101 #define DECLARE_PLOTTER(name) void plot_ ## name ## _describe(plotter_t* p)
102 #define DEFINE_PLOTTER_BODY(pname)              \
103     p->name = #pname;                           \
104     p->init = plot_ ## pname ## _init;          \
105     p->command = plot_ ## pname ## _command;    \
106     p->doplot = plot_ ## pname ## _plot;        \
107     p->free = plot_ ## pname ## _free;
108 
109 #define DEFINE_PLOTTER(name) DECLARE_PLOTTER(name) {    \
110         DEFINE_PLOTTER_BODY(name)                       \
111             }
112 
113 /*
114  #define DEFINE_PLOTTER(name) void plot_ ## name ## _describe(plotter_t* p) { \
115  p->name = #name;												\
116  p->init = plot_ ## name ## _init;								\
117  p->command = plot_ ## name ## _command;							\
118  p->doplot = plot_ ## name ## _plot;								\
119  p->free = plot_ ## name ## _free;								\
120  }
121  */
122 
123 // return PLOTSTUFF_FORMAT_*, or -1 on error
124 int parse_image_format(const char* fmt);
125 int guess_image_format_from_filename(const char* fn);
126 const char* image_format_name_from_code(int code);
127 
128 int parse_color(const char* color, float* r, float* g, float* b, float* a);
129 int parse_color_rgba(const char* color, float* rgba);
130 int cairo_set_color(cairo_t* cairo, const char* color);
131 void cairo_set_rgba(cairo_t* cairo, const float* rgba);
132 
133 plot_args_t* plotstuff_new(void);
134 int plotstuff_init(plot_args_t* plotargs);
135 int plotstuff_read_and_run_command(plot_args_t* pargs, FILE* f);
136 int plotstuff_run_command(plot_args_t* pargs, const char* cmd);
137 
138 void plotstuff_set_text_bg_alpha(plot_args_t* pargs, float alpha);
139 
140 int plotstuff_plot_layer(plot_args_t* pargs, const char* layer);
141 
142 void* plotstuff_get_config(plot_args_t* pargs, const char* name);
143 
144 int plotstuff_set_color(plot_args_t* pargs, const char* name);
145 int plotstuff_set_bgcolor(plot_args_t* pargs, const char* name);
146 
147 float plotstuff_get_alpha(const plot_args_t* pargs);
148 
149 int plotstuff_set_alpha(plot_args_t* pargs, float alpha);
150 
151 int plotstuff_set_rgba(plot_args_t* pargs, const float* rgba);
152 
153 int plotstuff_set_rgba2(plot_args_t* pargs, float r, float g, float b, float a);
154 int plotstuff_set_bgrgba2(plot_args_t* pargs, float r, float g, float b, float a);
155 
156 int plotstuff_set_marker(plot_args_t* pargs, const char* name);
157 
158 int plotstuff_set_markersize(plot_args_t* pargs, double ms);
159 
160 int plotstuff_set_size(plot_args_t* pargs, int W, int H);
161 
162 // Sets the plot size from the WCS size.
163 int plotstuff_set_size_wcs(plot_args_t* pargs);
164 
165 int plotstuff_scale_wcs(plot_args_t* pargs, double scale);
166 
167 // in deg.
168 int plotstuff_rotate_wcs(plot_args_t* pargs, double angle);
169 
170 int plotstuff_set_wcs_box(plot_args_t* pargs, float ra, float dec, float width);
171 
172 int plotstuff_set_wcs_file(plot_args_t* pargs, const char* fn, int ext);
173 
174 int plotstuff_set_wcs(plot_args_t* pargs, anwcs_t* wcs);
175 
176 int plotstuff_set_wcs_tan(plot_args_t* pargs, tan_t* wcs);
177 
178 int plotstuff_set_wcs_sip(plot_args_t* pargs, sip_t* wcs);
179 
180 void plotstuff_builtin_apply(cairo_t* cairo, plot_args_t* args);
181 
182 // Would a marker plotted with the current markersize at x,y appear in the image?
183 anbool plotstuff_marker_in_bounds(plot_args_t* pargs, double x, double y);
184 
185 int
186 ATTRIB_FORMAT(printf,2,3)
187     plotstuff_run_commandf(plot_args_t* pargs, const char* fmt, ...);
188 
189 int plotstuff_output(plot_args_t* pargs);
190 void plotstuff_free(plot_args_t* pargs);
191 
192 /* Reset drawing surface with color (0,0,0) and alpha=0 */
193 void plotstuff_clear(plot_args_t* pargs);
194 
195 
196 void plotstuff_stack_marker(plot_args_t* pargs, double x, double y);
197 void plotstuff_stack_arrow(plot_args_t* pargs, double x, double y,
198                            double x2, double y2);
199 void plotstuff_stack_text(plot_args_t* pargs, cairo_t* cairo,
200                           const char* txt, double px, double py);
201 int plotstuff_plot_stack(plot_args_t* pargs, cairo_t* cairo);
202 
203 void plotstuff_get_maximum_rgba(plot_args_t* pargs,
204                                 int* p_r, int* p_g, int* p_b, int* p_a);
205 
206 /// WCS-related stuff:
207 
208 // in arcsec/pixel
209 double plotstuff_pixel_scale(plot_args_t* pargs);
210 
211 // RA,Dec in degrees
212 // x,y in pixels (cairo coordinates)
213 // Returns TRUE on success.
214 anbool plotstuff_radec2xy(plot_args_t* pargs, double ra, double dec,
215                           double* p_x, double* p_y);
216 
217 // RA,Dec in degrees
218 // x,y in pixels (FITS coordinates)
219 // Returns TRUE on success.
220 anbool plotstuff_xy2radec(plot_args_t* pargs, double x, double y,
221                           double* pre, double* pdec);
222 
223 // RA,Dec,radius in deg.
224 int plotstuff_get_radec_center_and_radius(plot_args_t* pargs, double* pra, double* pdec, double* pradius);
225 
226 void plotstuff_get_radec_bounds(const plot_args_t* pargs, int stepsize,
227                                 double* pramin, double* pramax,
228                                 double* pdecmin, double* pdecmax);
229 
230 anbool plotstuff_radec_is_inside_image(plot_args_t* pargs, double ra, double dec);
231 
232 int plotstuff_line_constant_ra(plot_args_t* pargs, double ra, double dec1, double dec2,
233                                anbool startwithmove);
234 int plotstuff_line_constant_dec(plot_args_t* pargs, double dec, double ra1, double ra2);
235 int plotstuff_line_constant_dec2(plot_args_t* pargs, double dec,
236                                  double ra1, double ra2, double stepra);
237 
238 int plotstuff_text_xy(plot_args_t* pargs, double ra, double dec, const char* label);
239 int plotstuff_text_radec(plot_args_t* pargs, double ra, double dec, const char* label);
240 int plotstuff_move_to_radec(plot_args_t* pargs, double ra, double dec);
241 int plotstuff_line_to_radec(plot_args_t* pargs, double ra, double dec);
242 int plotstuff_close_path(plot_args_t* pargs);
243 int plotstuff_stroke(plot_args_t* pargs);
244 int plotstuff_fill(plot_args_t* pargs);
245 int plotstuff_stroke_preserve(plot_args_t* pargs);
246 int plotstuff_fill_preserve(plot_args_t* pargs);
247 
248 void plotstuff_move_to(plot_args_t* pargs, double x, double y);
249 void plotstuff_line_to(plot_args_t* pargs, double x, double y);
250 
251 void plotstuff_marker(plot_args_t* pargs, double x, double y);
252 int plotstuff_marker_radec(plot_args_t* pargs, double ra, double dec);
253 
254 int plotstuff_append_doubles(const char* str, dl* lst);
255 
256 void plotstuff_set_dashed(plot_args_t* pargs, double dashlen);
257 void plotstuff_set_solid(plot_args_t* pargs);
258 
259 #endif
260