1 /* $Id: s_zoom.c,v 1.16 2002/04/19 14:05:50 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 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
;
56 /* no pixel arrays! */
57 ASSERT((span
->arrayMask
& SPAN_XY
) == 0);
58 ASSERT(span
->primitive
== GL_BITMAP
);
60 INIT_SPAN((&zoomed
), GL_BITMAP
, 0, 0, 0);
61 if (format
== GL_RGBA
|| format
== GL_RGB
) {
63 zoomed
.zStep
= span
->z
;
64 zoomed
.fog
= span
->fog
;
65 zoomed
.fogStep
= span
->fogStep
;
66 zoomed
.interpMask
= span
->interpMask
& ~SPAN_RGBA
;
67 zoomed
.arrayMask
|= SPAN_RGBA
;
69 else if (format
== GL_COLOR_INDEX
) {
71 zoomed
.zStep
= span
->z
;
72 zoomed
.fog
= span
->fog
;
73 zoomed
.fogStep
= span
->fogStep
;
74 zoomed
.interpMask
= span
->interpMask
& ~SPAN_INDEX
;
75 zoomed
.arrayMask
|= SPAN_INDEX
;
79 * Compute which columns to draw: [c0, c1)
82 c1
= (GLint
) (span
->x
+ span
->end
* ctx
->Pixel
.ZoomX
);
101 zoomed
.end
= c1
- c0
;
104 if (zoomed
.end
> maxWidth
)
105 zoomed
.end
= maxWidth
;
108 * Compute which rows to draw: [r0, r1)
111 r0
= y0
+ (GLint
) (row
* ctx
->Pixel
.ZoomY
);
112 r1
= y0
+ (GLint
) ((row
+1) * ctx
->Pixel
.ZoomY
);
127 * Trivial clip rejection testing.
129 if (r1
< 0) /* below window */
131 if (r0
>= (GLint
) ctx
->DrawBuffer
->Height
) /* above window */
133 if (c1
< 0) /* left of window */
135 if (c0
>= (GLint
) ctx
->DrawBuffer
->Width
) /* right of window */
138 /* zoom the span horizontally */
139 if (format
== GL_RGBA
) {
140 if (ctx
->Pixel
.ZoomX
== -1.0F
) {
142 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
143 i
= span
->end
- (j
+ skipCol
) - 1;
144 COPY_CHAN4(zoomed
.color
.rgba
[j
], rgba
[i
]);
148 /* general solution */
149 const GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
150 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
151 i
= (GLint
) ((j
+ skipCol
) * xscale
);
153 i
= span
->end
+ i
- 1;
154 COPY_CHAN4(zoomed
.color
.rgba
[j
], rgba
[i
]);
158 else if (format
== GL_RGB
) {
159 if (ctx
->Pixel
.ZoomX
== -1.0F
) {
161 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
162 i
= span
->end
- (j
+ skipCol
) - 1;
163 zoomed
.color
.rgba
[j
][0] = rgb
[i
][0];
164 zoomed
.color
.rgba
[j
][1] = rgb
[i
][1];
165 zoomed
.color
.rgba
[j
][2] = rgb
[i
][2];
166 zoomed
.color
.rgba
[j
][3] = CHAN_MAX
;
170 /* general solution */
171 const GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
172 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
173 i
= (GLint
) ((j
+ skipCol
) * xscale
);
175 i
= span
->end
+ i
- 1;
176 zoomed
.color
.rgba
[j
][0] = rgb
[i
][0];
177 zoomed
.color
.rgba
[j
][1] = rgb
[i
][1];
178 zoomed
.color
.rgba
[j
][2] = rgb
[i
][2];
179 zoomed
.color
.rgba
[j
][3] = CHAN_MAX
;
183 else if (format
== GL_COLOR_INDEX
) {
184 if (ctx
->Pixel
.ZoomX
== -1.0F
) {
186 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
187 i
= span
->end
- (j
+ skipCol
) - 1;
188 zoomed
.color
.index
[j
] = indexes
[i
];
192 /* general solution */
193 const GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
194 for (j
= (GLint
) zoomed
.start
; j
< (GLint
) zoomed
.end
; j
++) {
195 i
= (GLint
) ((j
+ skipCol
) * xscale
);
197 i
= span
->end
+ i
- 1;
198 zoomed
.color
.index
[j
] = indexes
[i
];
203 /* write the span in rows [r0, r1) */
204 if (format
== GL_RGBA
|| format
== GL_RGB
) {
205 /* Writing the span may modify the colors, so make a backup now if we're
206 * going to call _mesa_write_zoomed_span() more than once.
209 MEMCPY(rgbaSave
, zoomed
.color
.rgba
, zoomed
.end
* 4 * sizeof(GLchan
));
211 for (zoomed
.y
= r0
; zoomed
.y
< r1
; zoomed
.y
++) {
212 _mesa_write_rgba_span(ctx
, &zoomed
);
214 /* restore the colors */
215 MEMCPY(zoomed
.color
.rgba
, rgbaSave
, zoomed
.end
*4 * sizeof(GLchan
));
219 else if (format
== GL_COLOR_INDEX
) {
221 MEMCPY(indexSave
, zoomed
.color
.index
, zoomed
.end
* sizeof(GLuint
));
223 for (zoomed
.y
= r0
; zoomed
.y
< r1
; zoomed
.y
++) {
224 _mesa_write_index_span(ctx
, &zoomed
);
226 /* restore the colors */
227 MEMCPY(zoomed
.color
.index
, indexSave
, zoomed
.end
* sizeof(GLuint
));
235 _mesa_write_zoomed_rgba_span( GLcontext
*ctx
, const struct sw_span
*span
,
236 CONST GLchan rgba
[][4], GLint y0
)
238 zoom_span(ctx
, span
, (const GLvoid
*) rgba
, y0
, GL_RGBA
);
243 _mesa_write_zoomed_rgb_span( GLcontext
*ctx
, const struct sw_span
*span
,
244 CONST GLchan rgb
[][3], GLint y0
)
246 zoom_span(ctx
, span
, (const GLvoid
*) rgb
, y0
, GL_RGB
);
251 _mesa_write_zoomed_index_span( GLcontext
*ctx
, const struct sw_span
*span
,
254 zoom_span(ctx
, span
, (const GLvoid
*) span
->color
.index
, y0
, GL_COLOR_INDEX
);
259 * As above, but write stencil values.
262 _mesa_write_zoomed_stencil_span( GLcontext
*ctx
,
263 GLuint n
, GLint x
, GLint y
,
264 const GLstencil stencil
[], GLint y0
)
267 GLint r0
, r1
, row
, r
;
269 GLstencil zstencil
[MAX_WIDTH
]; /* zoomed stencil values */
270 GLint maxwidth
= MIN2( ctx
->DrawBuffer
->Width
, MAX_WIDTH
);
272 /* compute width of output row */
273 m
= (GLint
) ABSF( n
* ctx
->Pixel
.ZoomX
);
277 if (ctx
->Pixel
.ZoomX
<0.0) {
278 /* adjust x coordinate for left/right mirroring */
282 /* compute which rows to draw */
284 r0
= y0
+ (GLint
) (row
* ctx
->Pixel
.ZoomY
);
285 r1
= y0
+ (GLint
) ((row
+1) * ctx
->Pixel
.ZoomY
);
295 /* return early if r0...r1 is above or below window */
300 if (r0
>= (GLint
) ctx
->DrawBuffer
->Height
&&
301 r1
>= (GLint
) ctx
->DrawBuffer
->Height
) {
306 /* check if left edge is outside window */
312 /* make sure span isn't too long or short */
320 ASSERT( m
<= MAX_WIDTH
);
322 /* zoom the span horizontally */
323 if (ctx
->Pixel
.ZoomX
==-1.0F
) {
326 i
= n
- (j
+skipcol
) - 1;
327 zstencil
[j
] = stencil
[i
];
331 GLfloat xscale
= 1.0F
/ ctx
->Pixel
.ZoomX
;
333 i
= (GLint
) ((j
+skipcol
) * xscale
);
334 if (i
<0) i
= n
+ i
- 1;
335 zstencil
[j
] = stencil
[i
];
340 for (r
=r0
; r
<r1
; r
++) {
341 _mesa_write_stencil_span( ctx
, m
, x
+skipcol
, r
, zstencil
);