File: im_line.c

package info (click to toggle)
vips 8.16.1-2
  • links: PTS
  • area: main
  • in suites: sid
  • size: 51,940 kB
  • sloc: ansic: 169,179; cpp: 11,890; python: 4,620; xml: 4,353; sh: 732; perl: 40; makefile: 19
file content (151 lines) | stat: -rw-r--r-- 3,460 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
/* @#) line drawer. adapted to draw for graphics system
 * @(#) Modified to be compatible with the vasari library
 * @(#) In order to use this function, the input file should have been set by
 * @(#) im_mmapinrw()
 *
 * Copyright: N. Dessipris
 * Written: 02/01/1990
 * Modified :
 * 22/7/93 JC
 *	- im_incheck() added
 *	- externs removed
 * 16/8/94 JC
 *	- im_incheck() changed to im_makerw()
 * 5/12/06
 * 	- im_invalidate() after paint
 */

/*

	This file is part of VIPS.

	VIPS is free software; you can redistribute it and/or modify
	it under the terms of the GNU Lesser 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 Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
	02110-1301  USA

 */

/*

	These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <vips/vips.h>

int
im_line(IMAGE *image, int x1, int y1, int x2, int y2, int pelval)
{
	double x, y, dx, dy, m;
	long offset;
	double signx, signy;

	if (im_rwcheck(image))
		return -1;
	/* check coordinates */
	if ((x1 > image->Xsize) || (x1 < 0) ||
		(y1 > image->Ysize) || (y1 < 0) ||
		(x2 > image->Xsize) || (x2 < 0) ||
		(y2 > image->Ysize) || (y2 < 0)) {
		im_error("im_line", "%s", _("invalid line cooordinates"));
		return -1;
	}
	if ((pelval > 255) || (pelval < 0)) {
		im_error("im_line", "%s", _("line intensity between 0 and 255"));
		return -1;
	}

	if (image->Bands != 1) {
		im_error("im_line", "%s", _("image should have one band only"));
		return -1;
	}

	dx = (double) (x2 - x1);
	dy = (double) (y2 - y1);

	if (dx < 0.0)
		signx = -1.0;
	else
		signx = 1.0;

	if (dy < 0.0)
		signy = -1.0;
	else
		signy = 1.0;

	if (dx == 0.0) {
		x = x1;
		y = y1;
		while (y != y2) {
			offset = (int) (x + .5) + ((int) (y + .5)) * image->Xsize;
			*(image->data + offset) = (PEL) pelval;
			y += signy;
		}
		/* Draw point (x2, y2) */
		offset = x2 + y2 * image->Xsize;
		*(image->data + offset) = (PEL) pelval;
		return 0;
	}

	if (dy == 0.0) {
		y = y1;
		x = x1;
		while (x != x2) {
			offset = (int) (x + .5) + ((int) (y + .5)) * image->Xsize;
			*(image->data + offset) = (PEL) pelval;
			x += signx;
		}
		/* Draw point (x2, y2) */
		offset = x2 + y2 * image->Xsize;
		*(image->data + offset) = (PEL) pelval;
		return 0;
	}

	if (fabs(dy) < fabs(dx)) {
		m = fabs(dy / dx) * signy;
		y = y1;
		x = x1;
		while (x != x2) {
			offset = (int) (x + .5) + ((int) (y + .5)) * image->Xsize;
			*(image->data + offset) = (PEL) pelval;
			x += signx;
			y += m;
		}
	}
	else {
		m = fabs(dx / dy) * signx;
		x = x1;
		y = y1;
		while (y != y2) {
			offset = (int) (x + .5) + ((int) (y + .5)) * image->Xsize;
			*(image->data + offset) = (PEL) pelval;
			x += m;
			y += signy;
		}
	}
	/* Draw point (x2, y2) */
	offset = x2 + y2 * image->Xsize;
	*(image->data + offset) = (PEL) pelval;
	im_invalidate(image);
	return 0;
}