1 /* $Id: s_zoom.c,v 1.18 2002/10/24 23:57:24 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 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
)
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
;
64 if (format
== GL_RGBA
|| format
== GL_RGB
) {
66 zoomed
.zStep
= span
->z
;
67 zoomed
.fog
= span
->fog
;
68 zoomed
.fogStep
= span
->fogStep
;
69 zoomed
.interpMask
= span
->interpMask
& ~SPAN_RGBA
;
70 zoomed
.arrayMask
|= SPAN_RGBA
;
72 else if (format
== GL_COLOR_INDEX
) {
74 zoomed
.zStep
= span
->z
;
75 zoomed
.fog
= span
->fog
;
76 zoomed
.fogStep
= span
->fogStep
;
77 zoomed
.interpMask
= span
->interpMask
& ~SPAN_INDEX
;
78 zoomed
.arrayMask
|= SPAN_INDEX
;
82 * Compute which columns to draw: [c0, c1)
85 c1
= (GLint
) (span
->x
+ span
->end
* ctx
->Pixel
.ZoomX
);
104 zoomed
.end
= c1
- c0
;
107 if (zoomed
.end
> maxWidth
)
108 zoomed
.end
= maxWidth
;
111 * Compute which rows to draw: [r0, r1)
114 r0
= y0
+ (GLint
) (row
* ctx
->Pixel
.ZoomY
);
115 r1
= y0
+ (GLint
) ((row
+1) * ctx
->Pixel
.ZoomY
);
130 * Trivial clip rejection testing.
132 if (r1
< 0) /* below window */
134 if (r0
>= (GLint
) ctx
->DrawBuffer
->Height
) /* above window */
136 if (c1
< 0) /* left of window */
138 if (c0
>= (GLint
) ctx
->DrawBuffer
->Width
) /* right of window */
141 /* zoom the span horizontally */
142 if (format
== GL_RGBA
) {
143 if (ctx
->Pixel
.ZoomX
== -1.0F
) {
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
]);
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
);
156 i
= span
->end
+ i
- 1;
157 COPY_CHAN4(zoomed
.array
->rgba
[j
], rgba
[i
]);
161 else if (format
== GL_RGB
) {
162 if (ctx
->Pixel
.ZoomX
== -1.0F
) {
164 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
165 i
= span
->end
- (j
+ skipCol
) - 1;
166 zoomed
.array
->rgba
[j
][0] = rgb
[i
][0];
167 zoomed
.array
->rgba
[j
][1] = rgb
[i
][1];
168 zoomed
.array
->rgba
[j
][2] = rgb
[i
][2];
169 zoomed
.array
->rgba
[j
][3] = CHAN_MAX
;
173 /* general solution */
174 const GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
175 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
176 i
= (GLint
) ((j
+ skipCol
) * xscale
);
178 i
= span
->end
+ i
- 1;
179 zoomed
.array
->rgba
[j
][0] = rgb
[i
][0];
180 zoomed
.array
->rgba
[j
][1] = rgb
[i
][1];
181 zoomed
.array
->rgba
[j
][2] = rgb
[i
][2];
182 zoomed
.array
->rgba
[j
][3] = CHAN_MAX
;
186 else if (format
== GL_COLOR_INDEX
) {
187 if (ctx
->Pixel
.ZoomX
== -1.0F
) {
189 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
190 i
= span
->end
- (j
+ skipCol
) - 1;
191 zoomed
.array
->index
[j
] = indexes
[i
];
195 /* general solution */
196 const GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
197 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
198 i
= (GLint
) ((j
+ skipCol
) * xscale
);
200 i
= span
->end
+ i
- 1;
201 zoomed
.array
->index
[j
] = indexes
[i
];
206 /* write the span in rows [r0, r1) */
207 if (format
== GL_RGBA
|| format
== GL_RGB
) {
208 /* Writing the span may modify the colors, so make a backup now if we're
209 * going to call _mesa_write_zoomed_span() more than once.
212 MEMCPY(rgbaSave
, zoomed
.array
->rgba
, zoomed
.end
* 4 * sizeof(GLchan
));
214 for (zoomed
.y
= r0
; zoomed
.y
< r1
; zoomed
.y
++) {
215 _mesa_write_rgba_span(ctx
, &zoomed
);
217 /* restore the colors */
218 MEMCPY(zoomed
.array
->rgba
, rgbaSave
, zoomed
.end
*4 * sizeof(GLchan
));
222 else if (format
== GL_COLOR_INDEX
) {
224 MEMCPY(indexSave
, zoomed
.array
->index
, zoomed
.end
* sizeof(GLuint
));
226 for (zoomed
.y
= r0
; zoomed
.y
< r1
; zoomed
.y
++) {
227 _mesa_write_index_span(ctx
, &zoomed
);
229 /* restore the colors */
230 MEMCPY(zoomed
.array
->index
, indexSave
, zoomed
.end
* sizeof(GLuint
));
238 _mesa_write_zoomed_rgba_span( GLcontext
*ctx
, const struct sw_span
*span
,
239 CONST GLchan rgba
[][4], GLint y0
)
241 zoom_span(ctx
, span
, (const GLvoid
*) rgba
, y0
, GL_RGBA
);
246 _mesa_write_zoomed_rgb_span( GLcontext
*ctx
, const struct sw_span
*span
,
247 CONST GLchan rgb
[][3], GLint y0
)
249 zoom_span(ctx
, span
, (const GLvoid
*) rgb
, y0
, GL_RGB
);
254 _mesa_write_zoomed_index_span( GLcontext
*ctx
, const struct sw_span
*span
,
257 zoom_span(ctx
, span
, (const GLvoid
*) span
->array
->index
, y0
, GL_COLOR_INDEX
);
262 * As above, but write stencil values.
265 _mesa_write_zoomed_stencil_span( GLcontext
*ctx
,
266 GLuint n
, GLint x
, GLint y
,
267 const GLstencil stencil
[], GLint y0
)
270 GLint r0
, r1
, row
, r
;
272 GLstencil zstencil
[MAX_WIDTH
]; /* zoomed stencil values */
273 GLint maxwidth
= MIN2( ctx
->DrawBuffer
->Width
, MAX_WIDTH
);
275 /* compute width of output row */
276 m
= (GLint
) ABSF( n
* ctx
->Pixel
.ZoomX
);
280 if (ctx
->Pixel
.ZoomX
<0.0) {
281 /* adjust x coordinate for left/right mirroring */
285 /* compute which rows to draw */
287 r0
= y0
+ (GLint
) (row
* ctx
->Pixel
.ZoomY
);
288 r1
= y0
+ (GLint
) ((row
+1) * ctx
->Pixel
.ZoomY
);
298 /* return early if r0...r1 is above or below window */
303 if (r0
>= (GLint
) ctx
->DrawBuffer
->Height
&&
304 r1
>= (GLint
) ctx
->DrawBuffer
->Height
) {
309 /* check if left edge is outside window */
315 /* make sure span isn't too long or short */
323 ASSERT( m
<= MAX_WIDTH
);
325 /* zoom the span horizontally */
326 if (ctx
->Pixel
.ZoomX
==-1.0F
) {
329 i
= n
- (j
+skipcol
) - 1;
330 zstencil
[j
] = stencil
[i
];
334 GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
336 i
= (GLint
) ((j
+skipcol
) * xscale
);
337 if (i
<0) i
= n
+ i
- 1;
338 zstencil
[j
] = stencil
[i
];
343 for (r
=r0
; r
<r1
; r
++) {
344 _mesa_write_stencil_span( ctx
, m
, x
+skipcol
, r
, zstencil
);