24 #include <cairo-pdf.h>
61 #define PRINT_USAGE(rc) \
62 fprintf(stderr, "Usage: %s [ -f format ] [ -s xscale yscale ] " \
63 "GEOMETRY_TABLE OUTPUT_PLOT\n", argv[0]); \
64 cpl_end(); return (rc);
66 int main(
int argc,
char **argv)
68 cpl_init(CPL_INIT_DEFAULT);
82 unsigned char oformat = PLOT_FORMAT_PDF;
86 for (i = 1; i < argc; i++) {
87 if (strncmp(argv[i],
"-f", 3) == 0) {
91 if (!strncmp(argv[i],
"pdf", 4)) {
92 oformat = PLOT_FORMAT_PDF;
93 }
else if (!strncmp(argv[i],
"png", 4)) {
94 oformat = PLOT_FORMAT_PNG;
101 }
else if (strncmp(argv[i],
"-s", 3) == 0) {
102 if (++i + 1 < argc) {
103 xscale = atof(argv[i++]);
104 yscale = atof(argv[i]);
105 if (xscale <= 0 || yscale <= 0) {
111 }
else if (strncmp(argv[i],
"-", 1) == 0) {
114 if (tiname && oname) {
128 cpl_table *table = cpl_table_load(tiname, 1, 1);
133 if (!cpl_table_has_column(table, MUSE_GEOTABLE_X) ||
134 !cpl_table_has_column(table, MUSE_GEOTABLE_Y) ||
135 !cpl_table_has_column(table, MUSE_GEOTABLE_WIDTH) ||
136 !cpl_table_has_column(table, MUSE_GEOTABLE_ANGLE)) {
137 cpl_table_delete(table);
142 double wmax = cpl_table_get_column_max(table, MUSE_GEOTABLE_WIDTH),
143 xmin = cpl_table_get_column_min(table, MUSE_GEOTABLE_X) - wmax / 2.,
144 ymin = cpl_table_get_column_min(table, MUSE_GEOTABLE_Y) - 2,
145 xmax = cpl_table_get_column_max(table, MUSE_GEOTABLE_X) + wmax / 2.,
146 ymax = cpl_table_get_column_max(table, MUSE_GEOTABLE_Y) + 2,
147 sx = xmax - xmin + 1,
148 sy = ymax - ymin + 1;
149 printf(
"Using scaling of %.3f in x- and %.3f in y-direction\n", xscale, yscale);
152 printf(
"Translating coordinates by %.3f in x- and %.3f in y-direction\n "
153 "(extrema: %f / %f ... %f / %f)\n", xt, yt, xmin, ymin, xmax, ymax);
156 cairo_status_t rc = CAIRO_STATUS_SUCCESS;
158 if (oformat == PLOT_FORMAT_PDF) {
159 cs = cairo_pdf_surface_create(oname, xscale * sx, yscale * sy);
160 rc = cairo_surface_status(cs);
161 if (rc != CAIRO_STATUS_SUCCESS) {
162 fprintf(stderr,
"opening PDF (\"%s\") returned %d!\n", oname, rc);
165 fprintf(stdout,
"writing PDF to \"%s\"\n", oname);
168 cs = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
169 xscale * sx, yscale * sy);
171 cairo_t *cr = cairo_create(cs);
173 cairo_set_source_rgb(cr, 0.5, 0.5, 0.5);
176 cairo_scale(cr, xscale, -yscale);
177 cairo_translate(cr, xt, yt);
179 cairo_matrix_t *cm = cpl_calloc(1,
sizeof(cairo_matrix_t));
180 cairo_get_matrix(cr, cm);
181 cairo_status_t cstat = cairo_status(cr);
182 fprintf(stderr,
"cm = %p, cstat = %d/%s, size is %f,%f\n",
183 cm, cstat, cairo_status_to_string(cstat), xscale * sx, yscale * sy);
184 double xtt = xmin, ytt = ymin + 2;
185 cairo_matrix_transform_point(cm, &xtt, &ytt);
186 fprintf(stderr,
"%f,%f is transformed to %f,%f\n", xmin, ymin + 2, xtt, ytt);
187 xtt = xmin, ytt = ymax - 2;
188 cairo_matrix_transform_point(cm, &xtt, &ytt);
189 fprintf(stderr,
"%f,%f is transformed to %f,%f\n", xmin, ymax - 2, xtt, ytt);
190 xtt = xmax, ytt = ymin + 2;
191 cairo_matrix_transform_point(cm, &xtt, &ytt);
192 fprintf(stderr,
"%f,%f is transformed to %f,%f\n", xmax, ymin + 2, xtt, ytt);
193 xtt = xmax, ytt = ymax - 2;
194 cairo_matrix_transform_point(cm, &xtt, &ytt);
195 fprintf(stderr,
"%f,%f is transformed to %f,%f\n", xmax, ymax - 2, xtt, ytt);
200 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
202 cairo_move_to(cr, -1000., 0.);
203 cairo_rel_line_to(cr, +5000, 0.);
204 cairo_scale(cr, 1., 1. / yscale);
208 cairo_move_to(cr, 0., -1000);
209 cairo_rel_line_to(cr, 0., +5000);
210 cairo_scale(cr, 1. / xscale, 1.);
215 cairo_scale(cr, 1. / xscale, 1. / yscale);
216 cairo_set_source_rgb(cr, 0.0, 0.0, 0.9);
217 cairo_arc(cr, 0, 0, 5., 0, CPL_MATH_2PI);
223 int n = cpl_table_get_nrow(table);
224 for (i = 0; i < n; i++) {
225 double x = cpl_table_get(table, MUSE_GEOTABLE_X, i, NULL),
226 y = cpl_table_get(table, MUSE_GEOTABLE_Y, i, NULL),
227 width = cpl_table_get(table, MUSE_GEOTABLE_WIDTH, i, NULL),
228 angrad = cpl_table_get(table, MUSE_GEOTABLE_ANGLE, i, NULL)
230 unsigned short ifu = cpl_table_get_int(table, MUSE_GEOTABLE_FIELD, i, NULL);
231 if ((ifu - 0) % 3 == 0) {
232 cairo_set_source_rgba(cr, 0.2, 0.2, 0.4, 0.5);
233 }
else if ((ifu - 1) % 3 == 0) {
234 cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.5);
235 }
else if ((ifu - 2) % 3 == 0) {
236 cairo_set_source_rgba(cr, 1.0, 0.843, 0.0, 0.5);
238 cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.5);
241 printf(
"ifu %hu slicesky %hu angle %f\n",
242 ifu, cpl_table_get_int(table, MUSE_GEOTABLE_SKY, i, NULL),
243 angrad * CPL_MATH_DEG_RAD);
247 cairo_translate(cr, x, y);
248 cairo_rotate(cr, angrad);
249 cairo_rectangle(cr, -width / 2., -0.5, width, 1.);
255 cairo_set_source_rgba(cr, 0.0, 0.0, 0.9, 0.8);
256 cairo_move_to(cr, - width / 2., - 0.5);
257 cairo_set_line_width(cr, 1. / yscale);
258 cairo_rel_line_to(cr, width, 0.);
259 cairo_set_line_width(cr, 1. / xscale);
260 cairo_rel_line_to(cr, 0., 1.);
261 cairo_set_line_width(cr, 1. / yscale);
262 cairo_rel_line_to(cr, -width, 0.);
263 cairo_close_path(cr);
269 if (oformat == PLOT_FORMAT_PNG) {
270 cairo_surface_write_to_png(cs, oname);
271 if (rc != CAIRO_STATUS_SUCCESS) {
272 fprintf(stderr,
"writing PNG (\"%s\") returned %d!\n", oname, rc);
275 fprintf(stdout,
"written PNG to \"%s\"\n", oname);
279 cairo_surface_destroy(cs);
280 cairo_debug_reset_static_data();
281 cpl_table_delete(table);
282 cpl_errorstate_dump(0,0,0);