File: gsimpath.c

package info (click to toggle)
ghostscript 8.71~dfsg2-9%2Bsqueeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 79,896 kB
  • ctags: 80,654
  • sloc: ansic: 501,432; sh: 25,689; python: 4,853; cpp: 3,633; perl: 3,597; tcl: 1,480; makefile: 1,187; lisp: 407; asm: 284; xml: 263; awk: 66; csh: 17; yacc: 15
file content (181 lines) | stat: -rw-r--r-- 5,765 bytes parent folder | download | duplicates (3)
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
/* Copyright (C) 2001-2006 Artifex Software, Inc.
   All Rights Reserved.
  
   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* $Id: gsimpath.c 8250 2007-09-25 13:31:24Z giles $ */
/* Image to outline conversion for Ghostscript library */
#include "gx.h"
#include "gserrors.h"
#include "gsmatrix.h"
#include "gspaint.h"		/* for gs_imagepath prototype */
#include "gsstate.h"
#include "gspath.h"

/* Define the state of the conversion process. */
typedef struct {
    /* The following are set at the beginning of the conversion. */
    gs_state *pgs;
    const byte *data;		/* image data */
    int width, height, raster;
    /* The following are updated dynamically. */
    int dx, dy;			/* X/Y increment of current run */
    int count;			/* # of steps in current run */
} status;

/* Define the scaling for the path tracer. */
/* It must be even. */
#define outline_scale 4
/* Define the length of the short strokes for turning corners. */
#define step 1

/* Forward declarations */
static int get_pixel(const status *, int, int);
static int trace_from(status *, int, int, int);
static int add_dxdy(status *, int, int, int);

#define add_deltas(s, dx, dy, n)\
  if ( (code = add_dxdy(s, dx, dy, n)) < 0 ) return code
/* Append an outline derived from an image to the current path. */
int
gs_imagepath(gs_state * pgs, int width, int height, const byte * data)
{
    status stat;
    status *out = &stat;
    int code, x, y;

    /* Initialize the state. */
    stat.pgs = pgs;
    stat.data = data;
    stat.width = width;
    stat.height = height;
    stat.raster = (width + 7) / 8;
    /* Trace the cells to form an outline.  The trace goes in clockwise */
    /* order, always starting by going west along a bottom edge. */
    for (y = height - 1; y >= 0; y--)
	for (x = width - 1; x >= 0; x--) {
	    if (get_pixel(out, x, y) && !get_pixel(out, x, y - 1) &&
	      (!get_pixel(out, x + 1, y) || get_pixel(out, x + 1, y - 1)) &&
		!trace_from(out, x, y, 1)
		) {		/* Found a starting point */
		stat.count = 0;
		stat.dx = stat.dy = 0;
		if ((code = trace_from(out, x, y, 0)) < 0)
		    return code;
		add_deltas(out, 0, 0, 1);	/* force out last segment */
		if ((code = gs_closepath(pgs)) < 0)
		    return code;
	    }
	}
    return 0;
}

/* Get a pixel from the data.  Return 0 if outside the image. */
static int
get_pixel(register const status * out, int x, int y)
{
    if (x < 0 || x >= out->width || y < 0 || y >= out->height)
	return 0;
    return (out->data[y * out->raster + (x >> 3)] >> (~x & 7)) & 1;
}

/* Trace a path.  If detect is true, don't draw, just return 1 if we ever */
/* encounter a starting point whose x,y follows that of the initial point */
/* in x-then-y scan order; if detect is false, actually draw the outline. */
static int
trace_from(register status * out, int x0, int y0, int detect)
{
    int x = x0, y = y0;
    int dx = -1, dy = 0;	/* initially going west */
    int part = 0;		/* how far along edge we are; */

    /* initialized only to pacify gcc */
    int code;

    if (!detect) {
	part = (get_pixel(out, x + 1, y - 1) ?
		outline_scale - step : step);
	code = gs_moveto(out->pgs,
			 x + 1 - part / (float)outline_scale,
			 (float)y);
	if (code < 0)
	    return code;
    }
    while (1) {			/* Relative to the current direction, */
	/* -dy,dx is at +90 degrees (counter-clockwise); */
	/* tx,ty is at +45 degrees; */
	/* ty,-tx is at -45 degrees (clockwise); */
	/* dy,-dx is at -90 degrees. */
	int tx = dx - dy, ty = dy + dx;

	if (get_pixel(out, x + tx, y + ty)) {	/* Cell at 45 degrees is full, */
	    /* go counter-clockwise. */
	    if (!detect) {	/* If this is a 90 degree corner set at a */
		/* 45 degree angle, avoid backtracking. */
		if (out->dx == ty && out->dy == -tx) {
#define half_scale (outline_scale / 2 - step)
		    out->count -= half_scale;
		    add_deltas(out, tx, ty, outline_scale / 2);
#undef half_scale
		} else {
		    add_deltas(out, dx, dy, step - part);
		    add_deltas(out, tx, ty, outline_scale - step);
		}
		part = outline_scale - step;
	    }
	    x += tx, y += ty;
	    dx = -dy, dy += tx;
	} else if (!get_pixel(out, x + dx, y + dy)) {	/* Cell straight ahead is empty, go clockwise. */
	    if (!detect) {
		add_deltas(out, dx, dy, outline_scale - step - part);
		add_deltas(out, ty, -tx, step);
		part = step;
	    }
	    dx = dy, dy -= ty;
	} else {		/* Neither of the above, go in same direction. */
	    if (!detect) {
		add_deltas(out, dx, dy, outline_scale);
	    }
	    x += dx, y += dy;
	}
	if (dx == -step && dy == 0 && !(tx == -step && ty == -step)) {	/* We just turned a corner and are going west, */
	    /* so the previous pixel is a starting point pixel. */
	    if (x == x0 && y == y0)
		return 0;
	    if (detect && (y > y0 || (y == y0 && x > x0)))
		return 1;
	}
    }
}

/* Add a (dx, dy) pair to the path being formed. */
/* Accumulate successive segments in the same direction. */
static int
add_dxdy(register status * out, int dx, int dy, int count)
{
    if (count != 0) {
	if (dx == out->dx && dy == out->dy)
	    out->count += count;
	else {
	    if (out->count != 0) {
		int code = gs_rlineto(out->pgs,
				out->dx * out->count / (float)outline_scale,
			       out->dy * out->count / (float)outline_scale);

		if (code < 0)
		    return code;
	    }
	    out->dx = dx, out->dy = dy;
	    out->count = count;
	}
    }
    return 0;
}