File: draw.c

package info (click to toggle)
fitscut 1.4.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 924 kB
  • ctags: 465
  • sloc: ansic: 6,024; sh: 767; makefile: 132
file content (389 lines) | stat: -rw-r--r-- 12,818 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* -*- mode:C; indent-tabs-mode:nil; tab-width:8; c-basic-offset:8; -*-
 *
 * Fitscut drawing functions
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: draw.c,v 1.6 2004/04/21 21:28:47 mccannwj Exp $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <errno.h>
#include "getopt.h"
#include <signal.h>
#include <ctype.h>

#include <float.h>
#include <math.h>

#ifdef	STDC_HEADERS
#include <stdlib.h>
#else	/* Not STDC_HEADERS */
extern void exit ();
extern char *malloc ();
#endif	/* STDC_HEADERS */

#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif

#include "fitscut.h"
#include "image_scale.h"
#include "draw.h"

#ifndef DEGTORAD
#define DEGTORAD(x) ((x)*M_PI/180.0)
#endif 

static void
rotate_vector (float *invec, float angle, float *outvec)
{
        float cosa, sina;
        float temp;

        cosa = cos (DEGTORAD (angle));
        sina = sin (DEGTORAD (angle));
 
        /* use a temp variable in case invec and outvec are the same */
        temp = invec[0] * cosa + invec[1] * sina;
        outvec[1] = -invec[0] * sina + invec[1] * cosa;
        outvec[0] = temp;
}

static void
ImageSetPixel (FitsCutImage *Image, int channel, int x, int y, float value)
{
        float *arrayp;
        int ncols = Image->ncols[channel];

        arrayp = Image->data[channel];

        arrayp[ncols * y + x] = value;
}

static int
drawCompareInt (const void *a, const void *b)
{
        return (*(const int *)a) - (*(const int *)b);
}

void
draw_filled_polygon (FitsCutImage *Image, int channel, drawPointPtr p, int n, int c)
{
        /* adapted from gdImageFilledPolygon */
        int i;
        int y;
        int miny, maxy;
        int x1, y1;
        int x2, y2;
        int ind1, ind2;
        int ints;
        int *polyInts;

        if (!n)
                return;

        polyInts = (int *) malloc (sizeof (int) * n);

        miny = p[0].y;
        maxy = p[0].y;
        for (i = 1; i < n; i++) {
                if (p[i].y < miny)
                        miny = p[i].y;
                if (p[i].y > maxy)
                        maxy = p[i].y;
        }
        /* Fix in 1.3: count a vertex only once */
        for (y = miny; y <= maxy; y++) {
                /*1.4		int interLast = 0; */
                /*		int dirLast = 0; */
                /*		int interFirst = 1; */
                ints = 0;
                for (i = 0; (i < n); i++) {
                        if (!i) {
                                ind1 = n - 1;
                                ind2 = 0;
                        } else {
                                ind1 = i - 1;
                                ind2 = i;
                        }
                        y1 = p[ind1].y;
                        y2 = p[ind2].y;
                        if (y1 < y2) {
                                x1 = p[ind1].x;
                                x2 = p[ind2].x;
                        } else if (y1 > y2) {
                                y2 = p[ind1].y;
                                y1 = p[ind2].y;
                                x2 = p[ind1].x;
                                x1 = p[ind2].x;
                        } else {
                                continue;
                        }
                        if ((y >= y1) && (y < y2)) {
                                polyInts[ints++] = (y-y1) * (x2-x1) / (y2-y1) + x1;
                        } else if ((y == maxy) && (y > y1) && (y <= y2)) {
                                polyInts[ints++] = (y-y1) * (x2-x1) / (y2-y1) + x1;
                        }
                }
                qsort (polyInts, ints, sizeof (int), drawCompareInt);
    
                for (i = 0; (i < (ints)); i += 2) {
                        draw_line (Image, channel, polyInts[i], y,
                                  polyInts[i+1], y, c);
                }
        }
}

void
draw_line (FitsCutImage *Image, int channel, int x1, int y1, int x2, int y2, int value)
{
        /* adapted from GD gdImageLine */
        int dx, dy, incr1, incr2, d, x, y, xend, yend, xdirflag, ydirflag;
        dx = abs (x2-x1);
        dy = abs (y2-y1);
        if (dy <= dx) {
                d = 2 * dy - dx;
                incr1 = 2 * dy;
                incr2 = 2 * (dy - dx);
                if (x1 > x2) {
                        x = x2;
                        y = y2;
                        ydirflag = (-1);
                        xend = x1;
                } else {
                        x = x1;
                        y = y1;
                        ydirflag = 1;
                        xend = x2;
                }
                ImageSetPixel (Image, channel, x, y, value);
                if (((y2 - y1) * ydirflag) > 0) {
                        while (x < xend) {
                                x++;
                                if (d <0) {
                                        d += incr1;
                                } else {
                                        y++;
                                        d+=incr2;
                                }
                                ImageSetPixel (Image, channel, x, y, value);
                        }
                } else {
                        while (x < xend) {
                                x++;
                                if (d <0) {
                                        d+=incr1;
                                } else {
                                        y--;
                                        d+=incr2;
                                }
                                ImageSetPixel (Image, channel, x, y, value);
                        }
                }		
        } else {
                d = 2 * dx - dy;
                incr1 = 2 * dx;
                incr2 = 2 * (dx - dy);
                if (y1 > y2) {
                        y = y2;
                        x = x2;
                        yend = y1;
                        xdirflag = (-1);
                } else {
                        y = y1;
                        x = x1;
                        yend = y2;
                        xdirflag = 1;
                }
                ImageSetPixel (Image, channel, x, y, value);
                if (((x2 - x1) * xdirflag) > 0) {
                        while (y < yend) {
                                y++;
                                if (d <0) {
                                        d+=incr1;
                                } else {
                                        x++;
                                        d+=incr2;
                                }
                                ImageSetPixel (Image, channel, x, y, value);
                        }
                } 
                else {
                        while (y < yend) {
                                y++;
                                if (d <0) {
                                        d+=incr1;
                                } else {
                                        x--;
                                        d+=incr2;
                                }
                                ImageSetPixel (Image, channel, x, y, value);
                        }
                }
        }
}

static void
draw_arrow (FitsCutImage *Image,
            int channel,
            int x,
            int y,
            float angle, 
            int length,
            int width,
            int tip_length,
            int value)
{
        float tip_angle;
        float vec[2], outvec[2];
        int num_tip_points;
        int x0, y0, x1, y1, tipx, tipy;
        drawPointPtr arrow;
        drawPointPtr shaft;

        tip_angle = 20.0;
  
        /* rotate from horizontal */
        vec[0] = length;
        vec[1] = 0;
        rotate_vector (vec,angle,outvec);
        tipx = floor (x + outvec[0] + 0.5);
        tipy = floor (y + outvec[1] + 0.5);

        /* find arrow tips */
        vec[0] = -tip_length;
        vec[1] = 0;
        rotate_vector (vec, angle + tip_angle, outvec);
        x0 = floor (tipx + outvec[0] + 0.5);
        y0 = floor (tipy + outvec[1] + 0.5);
        rotate_vector (vec, angle - tip_angle, outvec);
        x1 = floor (tipx + outvec[0] + 0.5);
        y1 = floor (tipy + outvec[1] + 0.5);
        if (tip_length > 10) {
                num_tip_points = (tip_length > 10) ? 4 : 3;

                arrow = (drawPoint *) malloc (sizeof (drawPoint) * num_tip_points);

                arrow[0].x = tipx;
                arrow[0].y = tipy;
                arrow[1].x = x0;
                arrow[1].y = y0;

                arrow[num_tip_points-1].x = x1;
                arrow[num_tip_points-1].y = y1;

                if (num_tip_points > 3) {
                        /* find the new tip of the arrow shaft */
                        vec[0] = length - tip_length * 0.8;
                        vec[1] = 0;
                        rotate_vector (vec,angle,outvec);
                        tipx = floor (x + outvec[0] + 0.5);
                        tipy = floor (y + outvec[1] + 0.5);
                        arrow[2].x = tipx;
                        arrow[2].y = tipy;
                }

                /* draw the arrow head */
                draw_filled_polygon (Image, channel, arrow, num_tip_points, value);
                free (arrow);
        }
        else {
                draw_line (Image, channel, x0, y0, tipx, tipy, value);
                draw_line (Image, channel, x1, y1, tipx, tipy, value);
        }

        /* draw arrow shaft */
        if (width > 2) {
                shaft = (drawPoint *) malloc (sizeof (drawPoint) * 4);
                vec[0] = 0;
                vec[1] = width / 2;
                rotate_vector (vec, angle, outvec);
                shaft[0].x = x + outvec[0];
                shaft[0].y = y + outvec[1];
                shaft[1].x = tipx + outvec[0];
                shaft[1].y = tipy + outvec[1];
                vec[0] = 0;
                vec[1] = -width / 2;
                rotate_vector (vec, angle, outvec);
                shaft[3].x = x + outvec[0];
                shaft[3].y = y + outvec[1];
                shaft[2].x = tipx + outvec[0];
                shaft[2].y = tipy + outvec[1];
                draw_filled_polygon (Image, channel, shaft, 4, value);
                free (shaft);
        }
        else {
                draw_line (Image, channel, x, y, tipx, tipy, value);
        }
  
}

void
draw_wcs_compass (FitsCutImage *Image, float north_pa, float east_pa)
{
        float arrow_length;
        long center[2];
        int channel = 0;
        int draw_channels;
        float east_size_factor = 0.6;
        float line_value = 255;
        float arrow_size_factor;
        int thickness;
        int tip_size;
        int min_size;
        center[0] = Image->ncols[0] * 0.8;
        center[1] = Image->nrows[0] * 0.8;

        min_size = (Image->ncols[0] > Image->nrows[0]) ? Image->nrows[0] : Image->ncols[0];

        if (min_size > 1024)
                arrow_size_factor = 0.05;
        else if (min_size > 512)
                arrow_size_factor = 0.10;
        else if (min_size > 128)
                arrow_size_factor = 0.20;
        else
                arrow_size_factor = 0.30;

        arrow_length = arrow_size_factor * min_size;

        thickness = 2; /*floor(arrow_length * 0.05 + 0.5);*/
        if (thickness < 1)
                thickness = 1;

        fitscut_message (1, "drawing WCS compass north: %f east: %f thickness: %d\n",
                         north_pa, east_pa, thickness);

        tip_size = floor (arrow_length * 0.2 + 0.5);

        draw_channels = (Image->channels > 1) ? 2 : 1;
        for (channel = 0; channel < draw_channels; channel++) {
                /* draw north line */
                draw_arrow (Image, channel, center[0], center[1], north_pa,
                            arrow_length, thickness, tip_size, line_value);
    
                /* draw east line */
                draw_arrow (Image, channel, center[0], center[1], east_pa,
                            arrow_length * east_size_factor, thickness, tip_size, line_value);
        }
}