1 /* $Id: s_drawpix.c,v 1.38 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.
38 #include "s_context.h"
39 #include "s_drawpix.h"
40 #include "s_pixeltex.h"
42 #include "s_stencil.h"
43 #include "s_texture.h"
49 * Given the dest position, size and skipPixels and skipRows values
50 * for a glDrawPixels command, perform clipping of the image bounds
51 * so the result lies withing the context's buffer bounds.
52 * Return: GL_TRUE if image is ready for drawing
53 * GL_FALSE if image was completely clipped away (draw nothing)
56 _mesa_clip_pixelrect(const GLcontext
*ctx
,
57 GLint
*destX
, GLint
*destY
,
58 GLsizei
*width
, GLsizei
*height
,
59 GLint
*skipPixels
, GLint
*skipRows
)
61 const GLframebuffer
*buffer
= ctx
->DrawBuffer
;
64 if (*destX
< buffer
->_Xmin
) {
65 *skipPixels
+= (buffer
->_Xmin
- *destX
);
66 *width
-= (buffer
->_Xmin
- *destX
);
67 *destX
= buffer
->_Xmin
;
70 if (*destX
+ *width
> buffer
->_Xmax
)
71 *width
-= (*destX
+ *width
- buffer
->_Xmax
);
77 if (*destY
< buffer
->_Ymin
) {
78 *skipRows
+= (buffer
->_Ymin
- *destY
);
79 *height
-= (buffer
->_Ymin
- *destY
);
80 *destY
= buffer
->_Ymin
;
83 if (*destY
+ *height
> buffer
->_Ymax
)
84 *height
-= (*destY
+ *height
- buffer
->_Ymax
);
95 * Try to do a fast and simple RGB(a) glDrawPixels.
96 * Return: GL_TRUE if success, GL_FALSE if slow path must be used instead
99 fast_draw_pixels(GLcontext
*ctx
, GLint x
, GLint y
,
100 GLsizei width
, GLsizei height
,
101 GLenum format
, GLenum type
, const GLvoid
*pixels
)
103 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
104 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
107 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_RGBA
);
109 if (!ctx
->Current
.RasterPosValid
) {
110 return GL_TRUE
; /* no-op */
114 _mesa_span_default_z(ctx
, &span
);
115 if (ctx
->Fog
.Enabled
)
116 _mesa_span_default_fog(ctx
, &span
);
118 if ((SWRAST_CONTEXT(ctx
)->_RasterMask
& ~CLIP_BIT
) == 0
119 && ctx
->Texture
._EnabledUnits
== 0
120 && unpack
->Alignment
== 1
121 && !unpack
->SwapBytes
122 && !unpack
->LsbFirst
) {
126 GLint drawWidth
= width
; /* actual width drawn */
127 GLint drawHeight
= height
; /* actual height drawn */
128 GLint skipPixels
= unpack
->SkipPixels
;
129 GLint skipRows
= unpack
->SkipRows
;
131 GLdepth zSpan
[MAX_WIDTH
]; /* only used when zooming */
134 if (unpack
->RowLength
> 0)
135 rowLength
= unpack
->RowLength
;
139 /* If we're not using pixel zoom then do all clipping calculations
140 * now. Otherwise, we'll let the _mesa_write_zoomed_*_span() functions
141 * handle the clipping.
143 if (ctx
->Pixel
.ZoomX
==1.0F
&& ctx
->Pixel
.ZoomY
==1.0F
) {
144 /* horizontal clipping */
145 if (destX
< ctx
->DrawBuffer
->_Xmin
) {
146 skipPixels
+= (ctx
->DrawBuffer
->_Xmin
- destX
);
147 drawWidth
-= (ctx
->DrawBuffer
->_Xmin
- destX
);
148 destX
= ctx
->DrawBuffer
->_Xmin
;
150 if (destX
+ drawWidth
> ctx
->DrawBuffer
->_Xmax
)
151 drawWidth
-= (destX
+ drawWidth
- ctx
->DrawBuffer
->_Xmax
);
155 /* vertical clipping */
156 if (destY
< ctx
->DrawBuffer
->_Ymin
) {
157 skipRows
+= (ctx
->DrawBuffer
->_Ymin
- destY
);
158 drawHeight
-= (ctx
->DrawBuffer
->_Ymin
- destY
);
159 destY
= ctx
->DrawBuffer
->_Ymin
;
161 if (destY
+ drawHeight
> ctx
->DrawBuffer
->_Ymax
)
162 drawHeight
-= (destY
+ drawHeight
- ctx
->DrawBuffer
->_Ymax
);
166 else if (ctx
->Pixel
.ZoomX
==1.0F
&& ctx
->Pixel
.ZoomY
==-1.0F
) {
167 /* upside-down image */
168 /* horizontal clipping */
169 if (destX
< ctx
->DrawBuffer
->_Xmin
) {
170 skipPixels
+= (ctx
->DrawBuffer
->_Xmin
- destX
);
171 drawWidth
-= (ctx
->DrawBuffer
->_Xmin
- destX
);
172 destX
= ctx
->DrawBuffer
->_Xmin
;
174 if (destX
+ drawWidth
> ctx
->DrawBuffer
->_Xmax
)
175 drawWidth
-= (destX
+ drawWidth
- ctx
->DrawBuffer
->_Xmax
);
179 /* vertical clipping */
180 if (destY
> ctx
->DrawBuffer
->_Ymax
) {
181 skipRows
+= (destY
- ctx
->DrawBuffer
->_Ymax
);
182 drawHeight
-= (destY
- ctx
->DrawBuffer
->_Ymax
);
183 destY
= ctx
->DrawBuffer
->_Ymax
;
185 if (destY
- drawHeight
< ctx
->DrawBuffer
->_Ymin
)
186 drawHeight
-= (ctx
->DrawBuffer
->_Ymin
- (destY
- drawHeight
));
191 /* setup array of fragment Z value to pass to zoom function */
192 GLdepth z
= (GLdepth
) (ctx
->Current
.RasterPos
[2] * ctx
->DepthMaxF
);
194 ASSERT(drawWidth
< MAX_WIDTH
);
195 for (i
=0; i
<drawWidth
; i
++)
198 /* save Y value of first row */
199 zoomY0
= IROUND(ctx
->Current
.RasterPos
[1]);
205 * The window region at (destX, destY) of size (drawWidth, drawHeight)
206 * will be written to.
207 * We'll take pixel data from buffer pointed to by "pixels" but we'll
208 * skip "skipRows" rows and skip "skipPixels" pixels/row.
211 if (format
== GL_RGBA
&& type
== CHAN_TYPE
212 && ctx
->_ImageTransferState
==0) {
213 if (ctx
->Visual
.rgbMode
) {
214 GLchan
*src
= (GLchan
*) pixels
215 + (skipRows
* rowLength
+ skipPixels
) * 4;
216 if (ctx
->Pixel
.ZoomX
==1.0F
&& ctx
->Pixel
.ZoomY
==1.0F
) {
219 for (row
=0; row
<drawHeight
; row
++) {
220 (*swrast
->Driver
.WriteRGBASpan
)(ctx
, drawWidth
, destX
, destY
,
221 (CONST
GLchan (*)[4]) src
, NULL
);
222 src
+= rowLength
* 4;
226 else if (ctx
->Pixel
.ZoomX
==1.0F
&& ctx
->Pixel
.ZoomY
==-1.0F
) {
229 for (row
=0; row
<drawHeight
; row
++) {
231 (*swrast
->Driver
.WriteRGBASpan
)(ctx
, drawWidth
, destX
, destY
,
232 (CONST
GLchan (*)[4]) src
, NULL
);
233 src
+= rowLength
* 4;
239 for (row
=0; row
<drawHeight
; row
++) {
242 span
.end
= drawWidth
;
243 _mesa_write_zoomed_rgba_span(ctx
, &span
,
244 (CONST
GLchan (*)[4]) src
, zoomY0
);
245 src
+= rowLength
* 4;
252 else if (format
== GL_RGB
&& type
== CHAN_TYPE
253 && ctx
->_ImageTransferState
== 0) {
254 if (ctx
->Visual
.rgbMode
) {
255 GLchan
*src
= (GLchan
*) pixels
256 + (skipRows
* rowLength
+ skipPixels
) * 3;
257 if (ctx
->Pixel
.ZoomX
==1.0F
&& ctx
->Pixel
.ZoomY
==1.0F
) {
259 for (row
=0; row
<drawHeight
; row
++) {
260 (*swrast
->Driver
.WriteRGBSpan
)(ctx
, drawWidth
, destX
, destY
,
261 (CONST
GLchan (*)[3]) src
, NULL
);
262 src
+= rowLength
* 3;
266 else if (ctx
->Pixel
.ZoomX
==1.0F
&& ctx
->Pixel
.ZoomY
==-1.0F
) {
269 for (row
=0; row
<drawHeight
; row
++) {
271 (*swrast
->Driver
.WriteRGBSpan
)(ctx
, drawWidth
, destX
, destY
,
272 (CONST
GLchan (*)[3]) src
, NULL
);
273 src
+= rowLength
* 3;
279 for (row
=0; row
<drawHeight
; row
++) {
282 span
.end
= drawWidth
;
283 _mesa_write_zoomed_rgb_span(ctx
, &span
,
284 (CONST
GLchan (*)[3]) src
, zoomY0
);
285 src
+= rowLength
* 3;
292 else if (format
== GL_LUMINANCE
&& type
== CHAN_TYPE
293 && ctx
->_ImageTransferState
==0) {
294 if (ctx
->Visual
.rgbMode
) {
295 GLchan
*src
= (GLchan
*) pixels
296 + (skipRows
* rowLength
+ skipPixels
);
297 if (ctx
->Pixel
.ZoomX
==1.0F
&& ctx
->Pixel
.ZoomY
==1.0F
) {
300 ASSERT(drawWidth
< MAX_WIDTH
);
301 for (row
=0; row
<drawHeight
; row
++) {
303 for (i
=0;i
<drawWidth
;i
++) {
304 span
.array
->rgb
[i
][0] = src
[i
];
305 span
.array
->rgb
[i
][1] = src
[i
];
306 span
.array
->rgb
[i
][2] = src
[i
];
308 (*swrast
->Driver
.WriteRGBSpan
)(ctx
, drawWidth
, destX
, destY
,
309 (CONST
GLchan (*)[3]) span
.array
->rgb
, NULL
);
314 else if (ctx
->Pixel
.ZoomX
==1.0F
&& ctx
->Pixel
.ZoomY
==-1.0F
) {
317 ASSERT(drawWidth
< MAX_WIDTH
);
318 for (row
=0; row
<drawHeight
; row
++) {
320 for (i
=0;i
<drawWidth
;i
++) {
321 span
.array
->rgb
[i
][0] = src
[i
];
322 span
.array
->rgb
[i
][1] = src
[i
];
323 span
.array
->rgb
[i
][2] = src
[i
];
326 (*swrast
->Driver
.WriteRGBSpan
)(ctx
, drawWidth
, destX
, destY
,
327 (CONST
GLchan (*)[3]) span
.array
->rgb
, NULL
);
334 ASSERT(drawWidth
< MAX_WIDTH
);
335 for (row
=0; row
<drawHeight
; row
++) {
337 for (i
=0;i
<drawWidth
;i
++) {
338 span
.array
->rgb
[i
][0] = src
[i
];
339 span
.array
->rgb
[i
][1] = src
[i
];
340 span
.array
->rgb
[i
][2] = src
[i
];
344 span
.end
= drawWidth
;
345 _mesa_write_zoomed_rgb_span(ctx
, &span
,
346 (CONST
GLchan (*)[3]) span
.array
->rgb
, zoomY0
);
354 else if (format
== GL_LUMINANCE_ALPHA
&& type
== CHAN_TYPE
355 && ctx
->_ImageTransferState
== 0) {
356 if (ctx
->Visual
.rgbMode
) {
357 GLchan
*src
= (GLchan
*) pixels
358 + (skipRows
* rowLength
+ skipPixels
)*2;
359 if (ctx
->Pixel
.ZoomX
==1.0F
&& ctx
->Pixel
.ZoomY
==1.0F
) {
362 ASSERT(drawWidth
< MAX_WIDTH
);
363 for (row
=0; row
<drawHeight
; row
++) {
366 for (i
=0;i
<drawWidth
;i
++) {
367 span
.array
->rgba
[i
][0] = *ptr
;
368 span
.array
->rgba
[i
][1] = *ptr
;
369 span
.array
->rgba
[i
][2] = *ptr
++;
370 span
.array
->rgba
[i
][3] = *ptr
++;
372 (*swrast
->Driver
.WriteRGBASpan
)(ctx
, drawWidth
, destX
, destY
,
373 (CONST
GLchan (*)[4]) span
.array
->rgba
, NULL
);
378 else if (ctx
->Pixel
.ZoomX
==1.0F
&& ctx
->Pixel
.ZoomY
==-1.0F
) {
381 ASSERT(drawWidth
< MAX_WIDTH
);
382 for (row
=0; row
<drawHeight
; row
++) {
385 for (i
=0;i
<drawWidth
;i
++) {
386 span
.array
->rgba
[i
][0] = *ptr
;
387 span
.array
->rgba
[i
][1] = *ptr
;
388 span
.array
->rgba
[i
][2] = *ptr
++;
389 span
.array
->rgba
[i
][3] = *ptr
++;
392 (*swrast
->Driver
.WriteRGBASpan
)(ctx
, drawWidth
, destX
, destY
,
393 (CONST
GLchan (*)[4]) span
.array
->rgba
, NULL
);
400 ASSERT(drawWidth
< MAX_WIDTH
);
401 for (row
=0; row
<drawHeight
; row
++) {
404 for (i
=0;i
<drawWidth
;i
++) {
405 span
.array
->rgba
[i
][0] = *ptr
;
406 span
.array
->rgba
[i
][1] = *ptr
;
407 span
.array
->rgba
[i
][2] = *ptr
++;
408 span
.array
->rgba
[i
][3] = *ptr
++;
412 span
.end
= drawWidth
;
413 _mesa_write_zoomed_rgba_span(ctx
, &span
,
414 (CONST
GLchan (*)[4]) span
.array
->rgba
, zoomY0
);
422 else if (format
==GL_COLOR_INDEX
&& type
==GL_UNSIGNED_BYTE
) {
423 GLubyte
*src
= (GLubyte
*) pixels
+ skipRows
* rowLength
+ skipPixels
;
424 if (ctx
->Visual
.rgbMode
) {
425 /* convert CI data to RGBA */
426 if (ctx
->Pixel
.ZoomX
==1.0F
&& ctx
->Pixel
.ZoomY
==1.0F
) {
429 for (row
=0; row
<drawHeight
; row
++) {
430 ASSERT(drawWidth
< MAX_WIDTH
);
431 _mesa_map_ci8_to_rgba(ctx
, drawWidth
, src
, span
.array
->rgba
);
432 (*swrast
->Driver
.WriteRGBASpan
)(ctx
, drawWidth
, destX
, destY
,
433 (const GLchan (*)[4]) span
.array
->rgba
, NULL
);
439 else if (ctx
->Pixel
.ZoomX
==1.0F
&& ctx
->Pixel
.ZoomY
==-1.0F
) {
442 for (row
=0; row
<drawHeight
; row
++) {
443 ASSERT(drawWidth
< MAX_WIDTH
);
444 _mesa_map_ci8_to_rgba(ctx
, drawWidth
, src
, span
.array
->rgba
);
446 (*swrast
->Driver
.WriteRGBASpan
)(ctx
, drawWidth
, destX
, destY
,
447 (CONST
GLchan (*)[4]) span
.array
->rgba
, NULL
);
455 for (row
=0; row
<drawHeight
; row
++) {
456 ASSERT(drawWidth
< MAX_WIDTH
);
457 _mesa_map_ci8_to_rgba(ctx
, drawWidth
, src
, span
.array
->rgba
);
460 span
.end
= drawWidth
;
461 _mesa_write_zoomed_rgba_span(ctx
, &span
,
462 (CONST
GLchan (*)[4]) span
.array
->rgba
, zoomY0
);
469 else if (ctx
->_ImageTransferState
==0) {
470 /* write CI data to CI frame buffer */
472 if (ctx
->Pixel
.ZoomX
==1.0F
&& ctx
->Pixel
.ZoomY
==1.0F
) {
474 for (row
=0; row
<drawHeight
; row
++) {
475 (*swrast
->Driver
.WriteCI8Span
)(ctx
, drawWidth
, destX
, destY
,
489 /* can't handle this pixel format and/or data type here */
494 /* can't do a simple draw, have to use slow path */
501 * Do glDrawPixels of index pixels.
504 draw_index_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
505 GLsizei width
, GLsizei height
,
506 GLenum type
, const GLvoid
*pixels
)
508 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!=1.0 || ctx
->Pixel
.ZoomY
!=1.0;
509 const GLint desty
= y
;
510 GLint row
, drawWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
513 INIT_SPAN(span
, GL_BITMAP
, drawWidth
, 0, SPAN_INDEX
);
516 _mesa_span_default_z(ctx
, &span
);
517 if (ctx
->Fog
.Enabled
)
518 _mesa_span_default_fog(ctx
, &span
);
523 for (row
= 0; row
< height
; row
++, y
++) {
524 const GLvoid
*source
= _mesa_image_address(&ctx
->Unpack
,
525 pixels
, width
, height
, GL_COLOR_INDEX
, type
, 0, row
, 0);
526 _mesa_unpack_index_span(ctx
, drawWidth
, GL_UNSIGNED_INT
,
528 type
, source
, &ctx
->Unpack
,
529 ctx
->_ImageTransferState
);
532 span
.end
= drawWidth
;
534 _mesa_write_zoomed_index_span(ctx
, &span
, desty
);
536 _mesa_write_index_span(ctx
, &span
);
543 * Do glDrawPixels of stencil image. The image datatype may either
544 * be GLubyte or GLbitmap.
547 draw_stencil_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
548 GLsizei width
, GLsizei height
,
549 GLenum type
, const GLvoid
*pixels
)
551 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!=1.0 || ctx
->Pixel
.ZoomY
!=1.0;
552 const GLint desty
= y
;
553 GLint row
, drawWidth
;
555 if (type
!= GL_BYTE
&&
556 type
!= GL_UNSIGNED_BYTE
&&
558 type
!= GL_UNSIGNED_SHORT
&&
560 type
!= GL_UNSIGNED_INT
&&
563 _mesa_error( ctx
, GL_INVALID_ENUM
, "glDrawPixels(stencil type)");
567 if (ctx
->Visual
.stencilBits
== 0) {
568 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glDrawPixels(no stencil buffer)");
572 drawWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
574 for (row
= 0; row
< height
; row
++, y
++) {
575 GLstencil values
[MAX_WIDTH
];
576 GLenum destType
= (sizeof(GLstencil
) == sizeof(GLubyte
))
577 ? GL_UNSIGNED_BYTE
: GL_UNSIGNED_SHORT
;
578 const GLvoid
*source
= _mesa_image_address(&ctx
->Unpack
,
579 pixels
, width
, height
, GL_COLOR_INDEX
, type
, 0, row
, 0);
580 _mesa_unpack_index_span(ctx
, drawWidth
, destType
, values
,
581 type
, source
, &ctx
->Unpack
,
582 ctx
->_ImageTransferState
);
583 if (ctx
->_ImageTransferState
& IMAGE_SHIFT_OFFSET_BIT
) {
584 _mesa_shift_and_offset_stencil( ctx
, drawWidth
, values
);
586 if (ctx
->Pixel
.MapStencilFlag
) {
587 _mesa_map_stencil( ctx
, drawWidth
, values
);
591 _mesa_write_zoomed_stencil_span( ctx
, (GLuint
) drawWidth
, x
, y
,
595 _mesa_write_stencil_span( ctx
, (GLuint
) drawWidth
, x
, y
, values
);
602 * Do a glDrawPixels of depth values.
605 draw_depth_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
606 GLsizei width
, GLsizei height
,
607 GLenum type
, const GLvoid
*pixels
)
609 const GLboolean bias_or_scale
= ctx
->Pixel
.DepthBias
!=0.0 || ctx
->Pixel
.DepthScale
!=1.0;
610 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!=1.0 || ctx
->Pixel
.ZoomY
!=1.0;
611 const GLint desty
= y
;
612 GLint drawWidth
= (width
> MAX_WIDTH
) ? MAX_WIDTH
: width
;
615 INIT_SPAN(span
, GL_BITMAP
, drawWidth
, 0, SPAN_Z
);
618 && type
!= GL_UNSIGNED_BYTE
620 && type
!= GL_UNSIGNED_SHORT
622 && type
!= GL_UNSIGNED_INT
623 && type
!= GL_FLOAT
) {
624 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawPixels(type)");
628 _mesa_span_default_color(ctx
, &span
);
630 if (ctx
->Fog
.Enabled
)
631 _mesa_span_default_fog(ctx
, &span
);
632 if (ctx
->Texture
._EnabledUnits
)
633 _mesa_span_default_texcoords(ctx
, &span
);
635 if (type
==GL_UNSIGNED_SHORT
&& ctx
->Visual
.depthBits
== 16
636 && !bias_or_scale
&& !zoom
&& ctx
->Visual
.rgbMode
) {
637 /* Special case: directly write 16-bit depth values */
641 span
.end
= drawWidth
;
642 for (row
= 0; row
< height
; row
++, span
.y
++) {
643 const GLushort
*zptr
= (const GLushort
*)
644 _mesa_image_address(&ctx
->Unpack
, pixels
, width
, height
,
645 GL_DEPTH_COMPONENT
, type
, 0, row
, 0);
647 for (i
= 0; i
< drawWidth
; i
++)
648 span
.array
->z
[i
] = zptr
[i
];
649 _mesa_write_rgba_span(ctx
, &span
);
652 else if (type
==GL_UNSIGNED_INT
&& ctx
->Visual
.depthBits
== 32
653 && !bias_or_scale
&& !zoom
&& ctx
->Visual
.rgbMode
) {
654 /* Special case: directly write 32-bit depth values */
658 span
.end
= drawWidth
;
659 for (row
= 0; row
< height
; row
++, span
.y
++) {
660 const GLuint
*zptr
= (const GLuint
*)
661 _mesa_image_address(&ctx
->Unpack
, pixels
, width
, height
,
662 GL_DEPTH_COMPONENT
, type
, 0, row
, 0);
663 MEMCPY(span
.array
->z
, zptr
, drawWidth
* sizeof(GLdepth
));
664 _mesa_write_rgba_span(ctx
, &span
);
672 span
.end
= drawWidth
;
673 for (row
= 0; row
< height
; row
++, span
.y
++) {
674 GLfloat fspan
[MAX_WIDTH
];
675 const GLvoid
*src
= _mesa_image_address(&ctx
->Unpack
,
676 pixels
, width
, height
, GL_DEPTH_COMPONENT
, type
, 0, row
, 0);
677 _mesa_unpack_depth_span( ctx
, drawWidth
, fspan
, type
, src
,
679 /* clamp depth values to [0,1] and convert from floats to integers */
681 const GLfloat zs
= ctx
->DepthMaxF
;
683 for (i
= 0; i
< drawWidth
; i
++) {
684 span
.array
->z
[i
] = (GLdepth
) (fspan
[i
] * zs
+ 0.5F
);
687 if (ctx
->Visual
.rgbMode
) {
690 _mesa_write_zoomed_rgba_span(ctx
, &span
,
691 (const GLchan (*)[4]) span
.array
->rgba
, desty
);
694 _mesa_write_rgba_span(ctx
, &span
);
699 _mesa_write_zoomed_index_span(ctx
, &span
, desty
);
701 _mesa_write_index_span(ctx
, &span
);
709 * Do glDrawPixels of RGBA pixels.
712 draw_rgba_pixels( GLcontext
*ctx
, GLint x
, GLint y
,
713 GLsizei width
, GLsizei height
,
714 GLenum format
, GLenum type
, const GLvoid
*pixels
)
716 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
717 const struct gl_pixelstore_attrib
*unpack
= &ctx
->Unpack
;
718 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!=1.0 || ctx
->Pixel
.ZoomY
!=1.0;
719 const GLint desty
= y
;
721 GLfloat
*convImage
= NULL
;
722 GLuint transferOps
= ctx
->_ImageTransferState
;
725 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_RGBA
);
727 if (!_mesa_is_legal_format_and_type(format
, type
)) {
728 _mesa_error(ctx
, GL_INVALID_ENUM
, "glDrawPixels(format or type)");
732 /* Try an optimized glDrawPixels first */
733 if (fast_draw_pixels(ctx
, x
, y
, width
, height
, format
, type
, pixels
))
737 _mesa_span_default_z(ctx
, &span
);
738 if (ctx
->Fog
.Enabled
)
739 _mesa_span_default_fog(ctx
, &span
);
740 if (ctx
->Texture
._EnabledUnits
)
741 _mesa_span_default_texcoords(ctx
, &span
);
743 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0 && !zoom
&& x
>= 0 && y
>= 0
744 && x
+ width
<= (GLint
) ctx
->DrawBuffer
->Width
745 && y
+ height
<= (GLint
) ctx
->DrawBuffer
->Height
) {
749 quickDraw
= GL_FALSE
;
752 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
753 /* Convolution has to be handled specially. We'll create an
754 * intermediate image, applying all pixel transfer operations
755 * up to convolution. Then we'll convolve the image. Then
756 * we'll proceed with the rest of the transfer operations and
757 * rasterize the image.
760 GLfloat
*dest
, *tmpImage
;
762 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
764 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
767 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
770 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glDrawPixels");
774 /* Unpack the image and apply transfer ops up to convolution */
776 for (row
= 0; row
< height
; row
++) {
777 const GLvoid
*source
= _mesa_image_address(unpack
,
778 pixels
, width
, height
, format
, type
, 0, row
, 0);
779 _mesa_unpack_float_color_span(ctx
, width
, GL_RGBA
, (GLfloat
*) dest
,
780 format
, type
, source
, unpack
,
781 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
,
787 if (ctx
->Pixel
.Convolution2DEnabled
) {
788 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
791 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
792 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
796 /* continue transfer ops and draw the convolved image */
797 unpack
= &_mesa_native_packing
;
801 transferOps
&= IMAGE_POST_CONVOLUTION_BITS
;
809 if (width
> MAX_WIDTH
)
812 for (row
= 0; row
< height
; row
++, y
++) {
813 const GLvoid
*source
= _mesa_image_address(unpack
,
814 pixels
, width
, height
, format
, type
, 0, row
, 0);
816 _mesa_unpack_chan_color_span(ctx
, width
, GL_RGBA
,
817 (GLchan
*) span
.array
->rgba
,
818 format
, type
, source
, unpack
,
821 if ((ctx
->Pixel
.MinMaxEnabled
&& ctx
->MinMax
.Sink
) ||
822 (ctx
->Pixel
.HistogramEnabled
&& ctx
->Histogram
.Sink
))
825 if (ctx
->Pixel
.PixelTextureEnabled
&& ctx
->Texture
._EnabledUnits
) {
827 _swrast_pixel_texture(ctx
, &span
);
831 (*swrast
->Driver
.WriteRGBASpan
)(ctx
, width
, x
, y
,
832 (CONST
GLchan (*)[4]) span
.array
->rgba
, NULL
);
838 _mesa_write_zoomed_rgba_span(ctx
, &span
,
839 (CONST
GLchan (*)[4]) span
.array
->rgba
, desty
);
845 _mesa_write_rgba_span(ctx
, &span
);
858 * Execute glDrawPixels
861 _swrast_DrawPixels( GLcontext
*ctx
,
863 GLsizei width
, GLsizei height
,
864 GLenum format
, GLenum type
,
865 const struct gl_pixelstore_attrib
*unpack
,
866 const GLvoid
*pixels
)
868 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
871 if (swrast
->NewState
)
872 _swrast_validate_derived( ctx
);
874 RENDER_START(swrast
,ctx
);
877 case GL_STENCIL_INDEX
:
878 draw_stencil_pixels( ctx
, x
, y
, width
, height
, type
, pixels
);
880 case GL_DEPTH_COMPONENT
:
881 draw_depth_pixels( ctx
, x
, y
, width
, height
, type
, pixels
);
884 if (ctx
->Visual
.rgbMode
)
885 draw_rgba_pixels(ctx
, x
,y
, width
, height
, format
, type
, pixels
);
887 draw_index_pixels(ctx
, x
, y
, width
, height
, type
, pixels
);
894 case GL_LUMINANCE_ALPHA
:
900 draw_rgba_pixels(ctx
, x
, y
, width
, height
, format
, type
, pixels
);
903 _mesa_error( ctx
, GL_INVALID_ENUM
, "glDrawPixels(format)" );
906 RENDER_FINISH(swrast
,ctx
);
911 #if 0 /* experimental */
913 * Execute glDrawDepthPixelsMESA().
916 _swrast_DrawDepthPixelsMESA( GLcontext
*ctx
,
918 GLsizei width
, GLsizei height
,
919 GLenum colorFormat
, GLenum colorType
,
920 const GLvoid
*colors
,
921 GLenum depthType
, const GLvoid
*depths
,
922 const struct gl_pixelstore_attrib
*unpack
)
924 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
927 if (swrast
->NewState
)
928 _swrast_validate_derived( ctx
);
930 RENDER_START(swrast
,ctx
);
932 switch (colorFormat
) {
934 if (ctx
->Visual
.rgbMode
)
935 draw_rgba_pixels(ctx
, x
,y
, width
, height
, colorFormat
, colorType
, colors
);
937 draw_index_pixels(ctx
, x
, y
, width
, height
, colorType
, colors
);
944 case GL_LUMINANCE_ALPHA
:
950 draw_rgba_pixels(ctx
, x
, y
, width
, height
, colorFormat
, colorType
, colors
);
953 _mesa_error( ctx
, GL_INVALID_ENUM
,
954 "glDrawDepthPixelsMESA(colorFormat)" );
957 RENDER_FINISH(swrast
,ctx
);