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