1 /* $Id: s_zoom.c,v 1.22 2003/02/25 19:26:02 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
32 #include "s_context.h"
34 #include "s_stencil.h"
39 * Helper function called from _mesa_write_zoomed_rgba/rgb/index_span().
42 zoom_span( GLcontext
*ctx
, const struct sw_span
*span
,
43 const GLvoid
*src
, GLint y0
, GLenum format
, GLint skipPixels
)
46 GLint c0
, c1
, skipCol
;
48 const GLuint maxWidth
= MIN2( ctx
->DrawBuffer
->Width
, MAX_WIDTH
);
49 GLchan rgbaSave
[MAX_WIDTH
][4];
50 GLuint indexSave
[MAX_WIDTH
];
51 const GLchan (*rgba
)[4] = (const GLchan (*)[4]) src
;
52 const GLchan (*rgb
)[3] = (const GLchan (*)[3]) src
;
53 const GLuint
*indexes
= (const GLuint
*) src
;
54 struct sw_span zoomed
;
55 struct span_arrays zoomed_arrays
; /* this is big! */
57 /* no pixel arrays! */
58 ASSERT((span
->arrayMask
& SPAN_XY
) == 0);
59 ASSERT(span
->primitive
== GL_BITMAP
);
61 INIT_SPAN(zoomed
, GL_BITMAP
, 0, 0, 0);
62 zoomed
.array
= &zoomed_arrays
;
65 zoomed
.zStep
= span
->zStep
; /* span->zStep == 0 */
66 zoomed
.fog
= span
->fog
;
67 zoomed
.fogStep
= span
->fogStep
;
68 if (format
== GL_RGBA
|| format
== GL_RGB
) {
69 zoomed
.interpMask
= span
->interpMask
& ~SPAN_RGBA
;
70 zoomed
.arrayMask
|= SPAN_RGBA
;
72 else if (format
== GL_COLOR_INDEX
) {
73 zoomed
.interpMask
= span
->interpMask
& ~SPAN_INDEX
;
74 zoomed
.arrayMask
|= SPAN_INDEX
;
78 * Compute which columns to draw: [c0, c1)
82 c1
= (GLint
) (span
->x
+ span
->end
* ctx
->Pixel
.ZoomX
);
84 c0
= (GLint
) (span
->x
+ skipPixels
* ctx
->Pixel
.ZoomX
);
85 c1
= (GLint
) (span
->x
+ (skipPixels
+ span
->end
) * ctx
->Pixel
.ZoomX
);
105 zoomed
.end
= c1
- c0
;
108 if (zoomed
.end
> maxWidth
)
109 zoomed
.end
= maxWidth
;
112 * Compute which rows to draw: [r0, r1)
115 r0
= y0
+ (GLint
) (row
* ctx
->Pixel
.ZoomY
);
116 r1
= y0
+ (GLint
) ((row
+1) * ctx
->Pixel
.ZoomY
);
131 * Trivial clip rejection testing.
133 if (r1
< 0) /* below window */
135 if (r0
>= (GLint
) ctx
->DrawBuffer
->Height
) /* above window */
137 if (c1
< 0) /* left of window */
139 if (c0
>= (GLint
) ctx
->DrawBuffer
->Width
) /* right of window */
142 /* zoom the span horizontally */
143 if (format
== GL_RGBA
) {
144 if (ctx
->Pixel
.ZoomX
== -1.0F
) {
146 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
147 i
= span
->end
- (j
+ skipCol
) - 1;
148 COPY_CHAN4(zoomed
.array
->rgba
[j
], rgba
[i
]);
152 /* general solution */
153 const GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
154 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
155 i
= (GLint
) ((j
+ skipCol
) * xscale
);
156 if (ctx
->Pixel
.ZoomX
< 0.0) {
158 i
= span
->end
+ i
- 1;
161 ASSERT(i
< (GLint
) span
->end
);
162 COPY_CHAN4(zoomed
.array
->rgba
[j
], rgba
[i
]);
166 else if (format
== GL_RGB
) {
167 if (ctx
->Pixel
.ZoomX
== -1.0F
) {
169 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
170 i
= span
->end
- (j
+ skipCol
) - 1;
171 zoomed
.array
->rgba
[j
][0] = rgb
[i
][0];
172 zoomed
.array
->rgba
[j
][1] = rgb
[i
][1];
173 zoomed
.array
->rgba
[j
][2] = rgb
[i
][2];
174 zoomed
.array
->rgba
[j
][3] = CHAN_MAX
;
178 /* general solution */
179 const GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
180 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
181 i
= (GLint
) ((j
+ skipCol
) * xscale
);
182 if (ctx
->Pixel
.ZoomX
< 0.0) {
184 i
= span
->end
+ i
- 1;
187 ASSERT(i
< (GLint
) span
->end
);
188 zoomed
.array
->rgba
[j
][0] = rgb
[i
][0];
189 zoomed
.array
->rgba
[j
][1] = rgb
[i
][1];
190 zoomed
.array
->rgba
[j
][2] = rgb
[i
][2];
191 zoomed
.array
->rgba
[j
][3] = CHAN_MAX
;
195 else if (format
== GL_COLOR_INDEX
) {
196 if (ctx
->Pixel
.ZoomX
== -1.0F
) {
198 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
199 i
= span
->end
- (j
+ skipCol
) - 1;
200 zoomed
.array
->index
[j
] = indexes
[i
];
204 /* general solution */
205 const GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
206 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
207 i
= (GLint
) ((j
+ skipCol
) * xscale
);
208 if (ctx
->Pixel
.ZoomX
< 0.0) {
210 i
= span
->end
+ i
- 1;
213 ASSERT(i
< (GLint
) span
->end
);
214 zoomed
.array
->index
[j
] = indexes
[i
];
219 /* write the span in rows [r0, r1) */
220 if (format
== GL_RGBA
|| format
== GL_RGB
) {
221 /* Writing the span may modify the colors, so make a backup now if we're
222 * going to call _mesa_write_zoomed_span() more than once.
223 * Also, clipping may change the span end value, so store it as well.
225 const GLint end
= zoomed
.end
; /* save */
227 MEMCPY(rgbaSave
, zoomed
.array
->rgba
, zoomed
.end
* 4 * sizeof(GLchan
));
229 for (zoomed
.y
= r0
; zoomed
.y
< r1
; zoomed
.y
++) {
230 _mesa_write_rgba_span(ctx
, &zoomed
);
231 zoomed
.end
= end
; /* restore */
233 /* restore the colors */
234 MEMCPY(zoomed
.array
->rgba
, rgbaSave
, zoomed
.end
*4 * sizeof(GLchan
));
238 else if (format
== GL_COLOR_INDEX
) {
239 const GLint end
= zoomed
.end
; /* save */
241 MEMCPY(indexSave
, zoomed
.array
->index
, zoomed
.end
* sizeof(GLuint
));
243 for (zoomed
.y
= r0
; zoomed
.y
< r1
; zoomed
.y
++) {
244 _mesa_write_index_span(ctx
, &zoomed
);
245 zoomed
.end
= end
; /* restore */
247 /* restore the colors */
248 MEMCPY(zoomed
.array
->index
, indexSave
, zoomed
.end
* sizeof(GLuint
));
256 _mesa_write_zoomed_rgba_span( GLcontext
*ctx
, const struct sw_span
*span
,
257 CONST GLchan rgba
[][4], GLint y0
,
260 zoom_span(ctx
, span
, (const GLvoid
*) rgba
, y0
, GL_RGBA
, skipPixels
);
265 _mesa_write_zoomed_rgb_span( GLcontext
*ctx
, const struct sw_span
*span
,
266 CONST GLchan rgb
[][3], GLint y0
,
269 zoom_span(ctx
, span
, (const GLvoid
*) rgb
, y0
, GL_RGB
, skipPixels
);
274 _mesa_write_zoomed_index_span( GLcontext
*ctx
, const struct sw_span
*span
,
275 GLint y0
, GLint skipPixels
)
277 zoom_span(ctx
, span
, (const GLvoid
*) span
->array
->index
, y0
,
278 GL_COLOR_INDEX
, skipPixels
);
283 * As above, but write stencil values.
286 _mesa_write_zoomed_stencil_span( GLcontext
*ctx
,
287 GLuint n
, GLint x
, GLint y
,
288 const GLstencil stencil
[], GLint y0
,
292 GLint r0
, r1
, row
, r
;
294 GLstencil zstencil
[MAX_WIDTH
]; /* zoomed stencil values */
295 GLint maxwidth
= MIN2( ctx
->DrawBuffer
->Width
, MAX_WIDTH
);
297 (void) skipPixels
; /* XXX this shouldn't be ignored */
299 /* compute width of output row */
300 m
= (GLint
) ABSF( n
* ctx
->Pixel
.ZoomX
);
304 if (ctx
->Pixel
.ZoomX
<0.0) {
305 /* adjust x coordinate for left/right mirroring */
309 /* compute which rows to draw */
311 r0
= y0
+ (GLint
) (row
* ctx
->Pixel
.ZoomY
);
312 r1
= y0
+ (GLint
) ((row
+1) * ctx
->Pixel
.ZoomY
);
322 /* return early if r0...r1 is above or below window */
327 if (r0
>= (GLint
) ctx
->DrawBuffer
->Height
&&
328 r1
>= (GLint
) ctx
->DrawBuffer
->Height
) {
333 /* check if left edge is outside window */
339 /* make sure span isn't too long or short */
347 ASSERT( m
<= MAX_WIDTH
);
349 /* zoom the span horizontally */
350 if (ctx
->Pixel
.ZoomX
==-1.0F
) {
353 i
= n
- (j
+skipcol
) - 1;
354 zstencil
[j
] = stencil
[i
];
358 GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
360 i
= (GLint
) ((j
+skipcol
) * xscale
);
361 if (i
<0) i
= n
+ i
- 1;
362 zstencil
[j
] = stencil
[i
];
367 for (r
=r0
; r
<r1
; r
++) {
368 _mesa_write_stencil_span( ctx
, m
, x
+skipcol
, r
, zstencil
);