sw_span can now hold x/y arrays of fragment positions - getting ready to
[mesa.git] / src / mesa / swrast / s_zoom.c
1 /* $Id: s_zoom.c,v 1.13 2002/02/02 17:24:11 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27 #include "glheader.h"
28 #include "macros.h"
29 #include "mem.h"
30 #include "colormac.h"
31
32 #include "s_context.h"
33 #include "s_span.h"
34 #include "s_stencil.h"
35 #include "s_zoom.h"
36
37
38 /*
39 * Helper function called from _mesa_write_zoomed_rgba/rgb/index_span().
40 */
41 static void
42 zoom_span( GLcontext *ctx, const struct sw_span *span,
43 const GLvoid *src, GLint y0, GLenum format )
44 {
45 GLint r0, r1, row;
46 GLint c0, c1, skipCol;
47 GLint i, j;
48 const GLint maxWidth = MIN2( ctx->DrawBuffer->Width, MAX_WIDTH );
49 GLchan rgbaSave[MAX_WIDTH][4];
50 GLuint indexSave[MAX_WIDTH];
51 struct sw_span zoomed;
52 const GLchan (*rgba)[4] = (const GLchan (*)[4]) src;
53 const GLchan (*rgb)[3] = (const GLchan (*)[3]) src;
54 const GLuint *indexes = (const GLuint *) src;
55
56 /* no pixel arrays! */
57 ASSERT((span->arrayMask & SPAN_XY) == 0);
58
59 INIT_SPAN(zoomed);
60 if (format == GL_RGBA || format == GL_RGB) {
61 zoomed.z = span->z;
62 zoomed.zStep = span->z;
63 zoomed.fog = span->fog;
64 zoomed.fogStep = span->fogStep;
65 zoomed.interpMask = span->interpMask & ~SPAN_RGBA;
66 zoomed.arrayMask |= SPAN_RGBA;
67 }
68 else if (format == GL_COLOR_INDEX) {
69 zoomed.z = span->z;
70 zoomed.zStep = span->z;
71 zoomed.fog = span->fog;
72 zoomed.fogStep = span->fogStep;
73 zoomed.interpMask = span->interpMask & ~SPAN_INDEX;
74 zoomed.arrayMask |= SPAN_INDEX;
75 }
76
77 /*
78 * Compute which columns to draw: [c0, c1)
79 */
80 c0 = (GLint) span->x;
81 c1 = (GLint) (span->x + span->end * ctx->Pixel.ZoomX);
82 if (c0 == c1) {
83 return;
84 }
85 else if (c1 < c0) {
86 /* swap */
87 GLint ctmp = c1;
88 c1 = c0;
89 c0 = ctmp;
90 }
91 if (c0 < 0) {
92 zoomed.x = 0;
93 zoomed.start = 0;
94 zoomed.end = c1;
95 skipCol = -c0;
96 }
97 else {
98 zoomed.x = c0;
99 zoomed.start = 0;
100 zoomed.end = c1 - c0;
101 skipCol = 0;
102 }
103 if (zoomed.end > maxWidth)
104 zoomed.end = maxWidth;
105
106 /*
107 * Compute which rows to draw: [r0, r1)
108 */
109 row = span->y - y0;
110 r0 = y0 + (GLint) (row * ctx->Pixel.ZoomY);
111 r1 = y0 + (GLint) ((row+1) * ctx->Pixel.ZoomY);
112 if (r0 == r1) {
113 return;
114 }
115 else if (r1 < r0) {
116 /* swap */
117 GLint rtmp = r1;
118 r1 = r0;
119 r0 = rtmp;
120 }
121
122 ASSERT(r0 < r1);
123 ASSERT(c0 < c1);
124
125 /*
126 * Trivial clip rejection testing.
127 */
128 if (r1 < 0) /* below window */
129 return;
130 if (r0 >= ctx->DrawBuffer->Height) /* above window */
131 return;
132 if (c1 < 0) /* left of window */
133 return;
134 if (c0 >= ctx->DrawBuffer->Width) /* right of window */
135 return;
136
137 /* zoom the span horizontally */
138 if (format == GL_RGBA) {
139 if (ctx->Pixel.ZoomX == -1.0F) {
140 /* common case */
141 for (j = zoomed.start; j < zoomed.end; j++) {
142 i = span->end - (j + skipCol) - 1;
143 COPY_CHAN4(zoomed.color.rgba[j], rgba[i]);
144 }
145 }
146 else {
147 /* general solution */
148 const GLfloat xscale = 1.0F / ctx->Pixel.ZoomX;
149 for (j = zoomed.start; j < zoomed.end; j++) {
150 i = (GLint) ((j + skipCol) * xscale);
151 if (i < 0)
152 i = span->end + i - 1;
153 COPY_CHAN4(zoomed.color.rgba[j], rgba[i]);
154 }
155 }
156 }
157 else if (format == GL_RGB) {
158 if (ctx->Pixel.ZoomX == -1.0F) {
159 /* common case */
160 for (j = zoomed.start; j < zoomed.end; j++) {
161 i = span->end - (j + skipCol) - 1;
162 zoomed.color.rgba[j][0] = rgb[i][0];
163 zoomed.color.rgba[j][1] = rgb[i][1];
164 zoomed.color.rgba[j][2] = rgb[i][2];
165 zoomed.color.rgba[j][3] = CHAN_MAX;
166 }
167 }
168 else {
169 /* general solution */
170 const GLfloat xscale = 1.0F / ctx->Pixel.ZoomX;
171 for (j = zoomed.start; j < zoomed.end; j++) {
172 i = (GLint) ((j + skipCol) * xscale);
173 if (i < 0)
174 i = span->end + i - 1;
175 zoomed.color.rgba[j][0] = rgb[i][0];
176 zoomed.color.rgba[j][1] = rgb[i][1];
177 zoomed.color.rgba[j][2] = rgb[i][2];
178 zoomed.color.rgba[j][3] = CHAN_MAX;
179 }
180 }
181 }
182 else if (format == GL_COLOR_INDEX) {
183 if (ctx->Pixel.ZoomX == -1.0F) {
184 /* common case */
185 for (j = zoomed.start; j < zoomed.end; j++) {
186 i = span->end - (j + skipCol) - 1;
187 zoomed.color.index[j] = indexes[i];
188 }
189 }
190 else {
191 /* general solution */
192 const GLfloat xscale = 1.0F / ctx->Pixel.ZoomX;
193 for (j = zoomed.start; j < zoomed.end; j++) {
194 i = (GLint) ((j + skipCol) * xscale);
195 if (i < 0)
196 i = span->end + i - 1;
197 zoomed.color.index[j] = indexes[i];
198 }
199 }
200 }
201
202 /* write the span in rows [r0, r1) */
203 if (format == GL_RGBA || format == GL_RGB) {
204 /* Writing the span may modify the colors, so make a backup now if we're
205 * going to call _mesa_write_zoomed_span() more than once.
206 */
207 if (r1 - r0 > 1) {
208 MEMCPY(rgbaSave, zoomed.color.rgba, zoomed.end * 4 * sizeof(GLchan));
209 }
210 for (zoomed.y = r0; zoomed.y < r1; zoomed.y++) {
211 _mesa_write_rgba_span(ctx, &zoomed, GL_BITMAP);
212 if (r1 - r0 > 1) {
213 /* restore the colors */
214 MEMCPY(zoomed.color.rgba, rgbaSave, zoomed.end*4 * sizeof(GLchan));
215 }
216 }
217 }
218 else if (format == GL_COLOR_INDEX) {
219 if (r1 - r0 > 1) {
220 MEMCPY(indexSave, zoomed.color.index, zoomed.end * sizeof(GLuint));
221 }
222 for (zoomed.y = r0; zoomed.y < r1; zoomed.y++) {
223 _mesa_write_index_span(ctx, &zoomed, GL_BITMAP);
224 if (r1 - r0 > 1) {
225 /* restore the colors */
226 MEMCPY(zoomed.color.index, indexSave, zoomed.end * sizeof(GLuint));
227 }
228 }
229 }
230 }
231
232
233 void
234 _mesa_write_zoomed_rgba_span( GLcontext *ctx, const struct sw_span *span,
235 CONST GLchan rgba[][4], GLint y0 )
236 {
237 zoom_span(ctx, span, (const GLvoid *) rgba, y0, GL_RGBA);
238 }
239
240
241 void
242 _mesa_write_zoomed_rgb_span( GLcontext *ctx, const struct sw_span *span,
243 CONST GLchan rgb[][3], GLint y0 )
244 {
245 zoom_span(ctx, span, (const GLvoid *) rgb, y0, GL_RGB);
246 }
247
248
249 void
250 _mesa_write_zoomed_index_span( GLcontext *ctx, const struct sw_span *span,
251 GLint y0 )
252 {
253 zoom_span(ctx, span, (const GLvoid *) span->color.index, y0, GL_COLOR_INDEX);
254 }
255
256
257 /*
258 * As above, but write stencil values.
259 */
260 void
261 _mesa_write_zoomed_stencil_span( GLcontext *ctx,
262 GLuint n, GLint x, GLint y,
263 const GLstencil stencil[], GLint y0 )
264 {
265 GLint m;
266 GLint r0, r1, row, r;
267 GLint i, j, skipcol;
268 GLstencil zstencil[MAX_WIDTH]; /* zoomed stencil values */
269 GLint maxwidth = MIN2( ctx->DrawBuffer->Width, MAX_WIDTH );
270
271 /* compute width of output row */
272 m = (GLint) ABSF( n * ctx->Pixel.ZoomX );
273 if (m==0) {
274 return;
275 }
276 if (ctx->Pixel.ZoomX<0.0) {
277 /* adjust x coordinate for left/right mirroring */
278 x = x - m;
279 }
280
281 /* compute which rows to draw */
282 row = y-y0;
283 r0 = y0 + (GLint) (row * ctx->Pixel.ZoomY);
284 r1 = y0 + (GLint) ((row+1) * ctx->Pixel.ZoomY);
285 if (r0==r1) {
286 return;
287 }
288 else if (r1<r0) {
289 GLint rtmp = r1;
290 r1 = r0;
291 r0 = rtmp;
292 }
293
294 /* return early if r0...r1 is above or below window */
295 if (r0<0 && r1<0) {
296 /* below window */
297 return;
298 }
299 if (r0>=ctx->DrawBuffer->Height && r1>=ctx->DrawBuffer->Height) {
300 /* above window */
301 return;
302 }
303
304 /* check if left edge is outside window */
305 skipcol = 0;
306 if (x<0) {
307 skipcol = -x;
308 m += x;
309 }
310 /* make sure span isn't too long or short */
311 if (m>maxwidth) {
312 m = maxwidth;
313 }
314 else if (m<=0) {
315 return;
316 }
317
318 ASSERT( m <= MAX_WIDTH );
319
320 /* zoom the span horizontally */
321 if (ctx->Pixel.ZoomX==-1.0F) {
322 /* n==m */
323 for (j=0;j<m;j++) {
324 i = n - (j+skipcol) - 1;
325 zstencil[j] = stencil[i];
326 }
327 }
328 else {
329 GLfloat xscale = 1.0F / ctx->Pixel.ZoomX;
330 for (j=0;j<m;j++) {
331 i = (GLint) ((j+skipcol) * xscale);
332 if (i<0) i = n + i - 1;
333 zstencil[j] = stencil[i];
334 }
335 }
336
337 /* write the span */
338 for (r=r0; r<r1; r++) {
339 _mesa_write_stencil_span( ctx, m, x+skipcol, r, zstencil );
340 }
341 }