work on GL_SGI_color_table
[mesa.git] / src / mesa / main / drawpix.c
1 /* $Id: drawpix.c,v 1.20 2000/04/12 18:54:48 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.3
6 *
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
28 #ifdef PC_HEADER
29 #include "all.h"
30 #else
31 #include "glheader.h"
32 #include "context.h"
33 #include "drawpix.h"
34 #include "feedback.h"
35 #include "image.h"
36 #include "macros.h"
37 #include "mem.h"
38 #include "mmath.h"
39 #include "pixel.h"
40 #include "pixeltex.h"
41 #include "span.h"
42 #include "state.h"
43 #include "stencil.h"
44 #include "texture.h"
45 #include "types.h"
46 #include "zoom.h"
47 #endif
48
49
50
51 /*
52 * Given the dest position, size and skipPixels and skipRows values
53 * for a glDrawPixels command, perform clipping of the image bounds
54 * so the result lies withing the context's buffer bounds.
55 * Return: GL_TRUE if image is ready for drawing
56 * GL_FALSE if image was completely clipped away (draw nothing)
57 */
58 GLboolean
59 _mesa_clip_pixelrect(const GLcontext *ctx,
60 GLint *destX, GLint *destY,
61 GLsizei *width, GLsizei *height,
62 GLint *skipPixels, GLint *skipRows)
63 {
64 const GLframebuffer *buffer = ctx->DrawBuffer;
65
66 /* left clipping */
67 if (*destX < buffer->Xmin) {
68 *skipPixels += (buffer->Xmin - *destX);
69 *width -= (buffer->Xmin - *destX);
70 *destX = buffer->Xmin;
71 }
72 /* right clipping */
73 if (*destX + *width > buffer->Xmax)
74 *width -= (*destX + *width - buffer->Xmax - 1);
75
76 if (*width <= 0)
77 return GL_FALSE;
78
79 /* bottom clipping */
80 if (*destY < buffer->Ymin) {
81 *skipRows += (buffer->Ymin - *destY);
82 *height -= (buffer->Ymin - *destY);
83 *destY = buffer->Ymin;
84 }
85 /* top clipping */
86 if (*destY + *height > buffer->Ymax)
87 *height -= (*destY + *height - buffer->Ymax - 1);
88
89 if (*height <= 0)
90 return GL_TRUE;
91
92 return GL_TRUE;
93 }
94
95
96
97 /*
98 * Try to do a fast and simple RGB(a) glDrawPixels.
99 * Return: GL_TRUE if success, GL_FALSE if slow path must be used instead
100 */
101 static GLboolean
102 simple_DrawPixels( GLcontext *ctx, GLint x, GLint y,
103 GLsizei width, GLsizei height, GLenum format, GLenum type,
104 const GLvoid *pixels )
105 {
106 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
107 GLubyte rgb[MAX_WIDTH][3];
108 GLubyte rgba[MAX_WIDTH][4];
109
110 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glDrawPixels",
111 GL_FALSE);
112
113
114 if (!ctx->Current.RasterPosValid) {
115 /* no-op */
116 return GL_TRUE;
117 }
118
119 if ((ctx->RasterMask&(~(SCISSOR_BIT|WINCLIP_BIT)))==0
120 && !ctx->Pixel.ScaleOrBiasRGBA
121 && !ctx->Pixel.ScaleOrBiasRGBApcm
122 && ctx->ColorMatrix.type == MATRIX_IDENTITY
123 && !ctx->Pixel.ColorTableEnabled
124 && ctx->Pixel.IndexShift==0 && ctx->Pixel.IndexOffset==0
125 && ctx->Pixel.MapColorFlag==0
126 && ctx->Texture.ReallyEnabled == 0
127 && unpack->Alignment==1
128 && !unpack->SwapBytes
129 && !unpack->LsbFirst) {
130
131 GLint destX = x;
132 GLint destY = y;
133 GLint drawWidth = width; /* actual width drawn */
134 GLint drawHeight = height; /* actual height drawn */
135 GLint skipPixels = unpack->SkipPixels;
136 GLint skipRows = unpack->SkipRows;
137 GLint rowLength;
138 GLdepth zSpan[MAX_WIDTH]; /* only used when zooming */
139 GLint zoomY0;
140
141 if (unpack->RowLength > 0)
142 rowLength = unpack->RowLength;
143 else
144 rowLength = width;
145
146 /* If we're not using pixel zoom then do all clipping calculations
147 * now. Otherwise, we'll let the gl_write_zoomed_*_span() functions
148 * handle the clipping.
149 */
150 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
151 /* horizontal clipping */
152 if (destX < ctx->DrawBuffer->Xmin) {
153 skipPixels += (ctx->DrawBuffer->Xmin - destX);
154 drawWidth -= (ctx->DrawBuffer->Xmin - destX);
155 destX = ctx->DrawBuffer->Xmin;
156 }
157 if (destX + drawWidth > ctx->DrawBuffer->Xmax)
158 drawWidth -= (destX + drawWidth - ctx->DrawBuffer->Xmax - 1);
159 if (drawWidth <= 0)
160 return GL_TRUE;
161
162 /* vertical clipping */
163 if (destY < ctx->DrawBuffer->Ymin) {
164 skipRows += (ctx->DrawBuffer->Ymin - destY);
165 drawHeight -= (ctx->DrawBuffer->Ymin - destY);
166 destY = ctx->DrawBuffer->Ymin;
167 }
168 if (destY + drawHeight > ctx->DrawBuffer->Ymax)
169 drawHeight -= (destY + drawHeight - ctx->DrawBuffer->Ymax - 1);
170 if (drawHeight <= 0)
171 return GL_TRUE;
172
173 zoomY0 = 0; /* not used - silence compiler warning */
174 }
175 else {
176 /* setup array of fragment Z value to pass to zoom function */
177 GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->Visual->DepthMaxF);
178 GLint i;
179 assert(drawWidth < MAX_WIDTH);
180 for (i=0; i<drawWidth; i++)
181 zSpan[i] = z;
182
183 /* save Y value of first row */
184 zoomY0 = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
185 }
186
187
188 /*
189 * Ready to draw!
190 * The window region at (destX, destY) of size (drawWidth, drawHeight)
191 * will be written to.
192 * We'll take pixel data from buffer pointed to by "pixels" but we'll
193 * skip "skipRows" rows and skip "skipPixels" pixels/row.
194 */
195
196 if (format==GL_RGBA && type==GL_UNSIGNED_BYTE) {
197 if (ctx->Visual->RGBAflag) {
198 GLubyte *src = (GLubyte *) pixels
199 + (skipRows * rowLength + skipPixels) * 4;
200 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
201 /* no zooming */
202 GLint row;
203 for (row=0; row<drawHeight; row++) {
204 (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY,
205 (void *) src, NULL);
206 src += rowLength * 4;
207 destY++;
208 }
209 }
210 else {
211 /* with zooming */
212 GLint row;
213 for (row=0; row<drawHeight; row++) {
214 gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY,
215 zSpan, (void *) src, zoomY0);
216 src += rowLength * 4;
217 destY++;
218 }
219 }
220 }
221 return GL_TRUE;
222 }
223 else if (format==GL_RGB && type==GL_UNSIGNED_BYTE) {
224 if (ctx->Visual->RGBAflag) {
225 GLubyte *src = (GLubyte *) pixels
226 + (skipRows * rowLength + skipPixels) * 3;
227 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
228 GLint row;
229 for (row=0; row<drawHeight; row++) {
230 (*ctx->Driver.WriteRGBSpan)(ctx, drawWidth, destX, destY,
231 (void *) src, NULL);
232 src += rowLength * 3;
233 destY++;
234 }
235 }
236 else {
237 /* with zooming */
238 GLint row;
239 for (row=0; row<drawHeight; row++) {
240 gl_write_zoomed_rgb_span(ctx, drawWidth, destX, destY,
241 zSpan, (void *) src, zoomY0);
242 src += rowLength * 3;
243 destY++;
244 }
245 }
246 }
247 return GL_TRUE;
248 }
249 else if (format==GL_LUMINANCE && type==GL_UNSIGNED_BYTE) {
250 if (ctx->Visual->RGBAflag) {
251 GLubyte *src = (GLubyte *) pixels
252 + (skipRows * rowLength + skipPixels);
253 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
254 /* no zooming */
255 GLint row;
256 assert(drawWidth < MAX_WIDTH);
257 for (row=0; row<drawHeight; row++) {
258 GLint i;
259 for (i=0;i<drawWidth;i++) {
260 rgb[i][0] = src[i];
261 rgb[i][1] = src[i];
262 rgb[i][2] = src[i];
263 }
264 (*ctx->Driver.WriteRGBSpan)(ctx, drawWidth, destX, destY,
265 (void *) rgb, NULL);
266 src += rowLength;
267 destY++;
268 }
269 }
270 else {
271 /* with zooming */
272 GLint row;
273 assert(drawWidth < MAX_WIDTH);
274 for (row=0; row<drawHeight; row++) {
275 GLint i;
276 for (i=0;i<drawWidth;i++) {
277 rgb[i][0] = src[i];
278 rgb[i][1] = src[i];
279 rgb[i][2] = src[i];
280 }
281 gl_write_zoomed_rgb_span(ctx, drawWidth, destX, destY,
282 zSpan, (void *) rgb, zoomY0);
283 src += rowLength;
284 destY++;
285 }
286 }
287 }
288 return GL_TRUE;
289 }
290 else if (format==GL_LUMINANCE_ALPHA && type==GL_UNSIGNED_BYTE) {
291 if (ctx->Visual->RGBAflag) {
292 GLubyte *src = (GLubyte *) pixels
293 + (skipRows * rowLength + skipPixels)*2;
294 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
295 /* no zooming */
296 GLint row;
297 assert(drawWidth < MAX_WIDTH);
298 for (row=0; row<drawHeight; row++) {
299 GLint i;
300 GLubyte *ptr = src;
301 for (i=0;i<drawWidth;i++) {
302 rgba[i][0] = *ptr;
303 rgba[i][1] = *ptr;
304 rgba[i][2] = *ptr++;
305 rgba[i][3] = *ptr++;
306 }
307 (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY,
308 (void *) rgba, NULL);
309 src += rowLength*2;
310 destY++;
311 }
312 }
313 else {
314 /* with zooming */
315 GLint row;
316 assert(drawWidth < MAX_WIDTH);
317 for (row=0; row<drawHeight; row++) {
318 GLubyte *ptr = src;
319 GLint i;
320 for (i=0;i<drawWidth;i++) {
321 rgba[i][0] = *ptr;
322 rgba[i][1] = *ptr;
323 rgba[i][2] = *ptr++;
324 rgba[i][3] = *ptr++;
325 }
326 gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY,
327 zSpan, (void *) rgba, zoomY0);
328 src += rowLength*2;
329 destY++;
330 }
331 }
332 }
333 return GL_TRUE;
334 }
335 else if (format==GL_COLOR_INDEX && type==GL_UNSIGNED_BYTE) {
336 GLubyte *src = (GLubyte *) pixels + skipRows * rowLength + skipPixels;
337 if (ctx->Visual->RGBAflag) {
338 /* convert CI data to RGBA */
339 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
340 /* no zooming */
341 GLint row;
342 for (row=0; row<drawHeight; row++) {
343 assert(drawWidth < MAX_WIDTH);
344 _mesa_map_ci8_to_rgba(ctx, drawWidth, src, rgba);
345 (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY,
346 (const GLubyte (*)[4])rgba,
347 NULL);
348 src += rowLength;
349 destY++;
350 }
351 return GL_TRUE;
352 }
353 else {
354 /* with zooming */
355 GLint row;
356 for (row=0; row<drawHeight; row++) {
357 assert(drawWidth < MAX_WIDTH);
358 _mesa_map_ci8_to_rgba(ctx, drawWidth, src, rgba);
359 gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY,
360 zSpan, (void *) rgba, zoomY0);
361 src += rowLength;
362 destY++;
363 }
364 return GL_TRUE;
365 }
366 }
367 else {
368 /* write CI data to CI frame buffer */
369 GLint row;
370 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
371 /* no zooming */
372 for (row=0; row<drawHeight; row++) {
373 (*ctx->Driver.WriteCI8Span)(ctx, drawWidth, destX, destY,
374 src, NULL);
375 src += rowLength;
376 destY++;
377 }
378 return GL_TRUE;
379 }
380 else {
381 /* with zooming */
382 return GL_FALSE;
383 }
384 }
385 }
386 else {
387 /* can't handle this pixel format and/or data type here */
388 return GL_FALSE;
389 }
390 }
391
392 /* can't do a simple draw, have to use slow path */
393 return GL_FALSE;
394 }
395
396
397
398 /*
399 * Do glDrawPixels of index pixels.
400 */
401 static void
402 draw_index_pixels( GLcontext *ctx, GLint x, GLint y,
403 GLsizei width, GLsizei height,
404 GLenum type, const GLvoid *pixels )
405 {
406 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
407 const GLint desty = y;
408 GLint row, drawWidth;
409 GLdepth zspan[MAX_WIDTH];
410
411 drawWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
412
413 /* Fragment depth values */
414 if (ctx->Depth.Test || ctx->Fog.Enabled) {
415 GLdepth zval = (GLdepth) (ctx->Current.RasterPos[2] * ctx->Visual->DepthMaxF);
416 GLint i;
417 for (i = 0; i < drawWidth; i++) {
418 zspan[i] = zval;
419 }
420 }
421
422 /*
423 * General solution
424 */
425 for (row = 0; row < height; row++, y++) {
426 GLuint indexes[MAX_WIDTH];
427 const GLvoid *source = _mesa_image_address(&ctx->Unpack,
428 pixels, width, height, GL_COLOR_INDEX, type, 0, row, 0);
429 _mesa_unpack_index_span(ctx, drawWidth, GL_UNSIGNED_INT, indexes,
430 type, source, &ctx->Unpack, GL_TRUE);
431 if (zoom) {
432 gl_write_zoomed_index_span(ctx, drawWidth, x, y, zspan, indexes, desty);
433 }
434 else {
435 gl_write_index_span(ctx, drawWidth, x, y, zspan, indexes, GL_BITMAP);
436 }
437 }
438 }
439
440
441
442 /*
443 * Do glDrawPixels of stencil image. The image datatype may either
444 * be GLubyte or GLbitmap.
445 */
446 static void
447 draw_stencil_pixels( GLcontext *ctx, GLint x, GLint y,
448 GLsizei width, GLsizei height,
449 GLenum type, const GLvoid *pixels )
450 {
451 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
452 const GLboolean shift_or_offset = ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset;
453 const GLint desty = y;
454 GLint row, drawWidth;
455
456 if (type != GL_BYTE &&
457 type != GL_UNSIGNED_BYTE &&
458 type != GL_SHORT &&
459 type != GL_UNSIGNED_SHORT &&
460 type != GL_INT &&
461 type != GL_UNSIGNED_INT &&
462 type != GL_FLOAT &&
463 type != GL_BITMAP) {
464 gl_error( ctx, GL_INVALID_ENUM, "glDrawPixels(stencil type)");
465 return;
466 }
467
468 drawWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
469
470 for (row = 0; row < height; row++, y++) {
471 GLstencil values[MAX_WIDTH];
472 GLenum destType = (sizeof(GLstencil) == sizeof(GLubyte))
473 ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT;
474 const GLvoid *source = _mesa_image_address(&ctx->Unpack,
475 pixels, width, height, GL_COLOR_INDEX, type, 0, row, 0);
476 _mesa_unpack_index_span(ctx, drawWidth, destType, values,
477 type, source, &ctx->Unpack, GL_FALSE);
478 if (shift_or_offset) {
479 _mesa_shift_and_offset_stencil( ctx, drawWidth, values );
480 }
481 if (ctx->Pixel.MapStencilFlag) {
482 _mesa_map_stencil( ctx, drawWidth, values );
483 }
484
485 if (zoom) {
486 gl_write_zoomed_stencil_span( ctx, (GLuint) drawWidth, x, y,
487 values, desty );
488 }
489 else {
490 _mesa_write_stencil_span( ctx, (GLuint) drawWidth, x, y, values );
491 }
492 }
493 }
494
495
496
497 /*
498 * Do a glDrawPixels of depth values.
499 */
500 static void
501 draw_depth_pixels( GLcontext *ctx, GLint x, GLint y,
502 GLsizei width, GLsizei height,
503 GLenum type, const GLvoid *pixels )
504 {
505 const GLboolean bias_or_scale = ctx->Pixel.DepthBias!=0.0 || ctx->Pixel.DepthScale!=1.0;
506 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
507 const GLint desty = y;
508 GLubyte rgba[MAX_WIDTH][4];
509 GLuint ispan[MAX_WIDTH];
510 GLint drawWidth = (width > MAX_WIDTH) ? MAX_WIDTH : width;
511
512 if (type != GL_UNSIGNED_BYTE
513 && type != GL_UNSIGNED_BYTE
514 && type != GL_UNSIGNED_SHORT
515 && type != GL_UNSIGNED_SHORT
516 && type != GL_UNSIGNED_INT
517 && type != GL_UNSIGNED_INT
518 && type != GL_FLOAT) {
519 gl_error(ctx, GL_INVALID_ENUM, "glDrawPixels(type)");
520 return;
521 }
522
523 /* Colors or indexes */
524 if (ctx->Visual->RGBAflag) {
525 GLint r = (GLint) (ctx->Current.RasterColor[0] * 255.0F);
526 GLint g = (GLint) (ctx->Current.RasterColor[1] * 255.0F);
527 GLint b = (GLint) (ctx->Current.RasterColor[2] * 255.0F);
528 GLint a = (GLint) (ctx->Current.RasterColor[3] * 255.0F);
529 GLint i;
530 for (i = 0; i < drawWidth; i++) {
531 rgba[i][RCOMP] = r;
532 rgba[i][GCOMP] = g;
533 rgba[i][BCOMP] = b;
534 rgba[i][ACOMP] = a;
535 }
536 }
537 else {
538 GLint i;
539 for (i = 0; i < drawWidth; i++) {
540 ispan[i] = ctx->Current.RasterIndex;
541 }
542 }
543
544 if (type==GL_UNSIGNED_SHORT && sizeof(GLdepth)==sizeof(GLushort)
545 && !bias_or_scale && !zoom && ctx->Visual->RGBAflag) {
546 /* Special case: directly write 16-bit depth values */
547 GLint row;
548 for (row = 0; row < height; row++, y++) {
549 GLdepth zspan[MAX_WIDTH];
550 const GLushort *zptr = _mesa_image_address(&ctx->Unpack,
551 pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0);
552 GLint i;
553 for (i = 0; i < width; i++)
554 zspan[i] = zptr[i];
555 gl_write_rgba_span( ctx, width, x, y, zspan, rgba, GL_BITMAP );
556 }
557 }
558 else if (type==GL_UNSIGNED_INT && ctx->Visual->DepthBits == 32
559 && !bias_or_scale && !zoom && ctx->Visual->RGBAflag) {
560 /* Special case: directly write 32-bit depth values */
561 GLint row;
562 for (row = 0; row < height; row++, y++) {
563 const GLuint *zptr = _mesa_image_address(&ctx->Unpack,
564 pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0);
565 gl_write_rgba_span( ctx, width, x, y, zptr, rgba, GL_BITMAP );
566 }
567 }
568 else {
569 /* General case */
570 GLint row;
571 for (row = 0; row < height; row++, y++) {
572 GLdepth zspan[MAX_WIDTH];
573 const GLvoid *src = _mesa_image_address(&ctx->Unpack,
574 pixels, width, height, GL_DEPTH_COMPONENT, type, 0, row, 0);
575 _mesa_unpack_depth_span( ctx, drawWidth, zspan, type, src,
576 &ctx->Unpack, GL_TRUE );
577 if (ctx->Visual->RGBAflag) {
578 if (zoom) {
579 gl_write_zoomed_rgba_span(ctx, width, x, y, zspan,
580 (const GLubyte (*)[4])rgba, desty);
581 }
582 else {
583 gl_write_rgba_span(ctx, width, x, y, zspan, rgba, GL_BITMAP);
584 }
585 }
586 else {
587 if (zoom) {
588 gl_write_zoomed_index_span(ctx, width, x, y, zspan,
589 ispan, GL_BITMAP);
590 }
591 else {
592 gl_write_index_span(ctx, width, x, y, zspan, ispan, GL_BITMAP);
593 }
594 }
595
596 }
597 }
598 }
599
600
601 /*
602 * Do glDrawPixels of RGBA pixels.
603 */
604 static void
605 draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y,
606 GLsizei width, GLsizei height,
607 GLenum format, GLenum type, const GLvoid *pixels )
608 {
609 const struct gl_pixelstore_attrib *unpack = &ctx->Unpack;
610 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
611 const GLint desty = y;
612 GLdepth zspan[MAX_WIDTH];
613 GLboolean quickDraw;
614
615 /* Try an optimized glDrawPixels first */
616 if (simple_DrawPixels(ctx, x, y, width, height, format, type, pixels))
617 return;
618
619 /* Fragment depth values */
620 if (ctx->Depth.Test || ctx->Fog.Enabled) {
621 /* fill in array of z values */
622 GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->Visual->DepthMaxF);
623 GLint i;
624 for (i=0;i<width;i++) {
625 zspan[i] = z;
626 }
627 }
628
629
630 if (ctx->RasterMask == 0 && !zoom
631 && x >= 0 && y >= 0
632 && x + width <= ctx->DrawBuffer->Width
633 && y + height <= ctx->DrawBuffer->Height) {
634 quickDraw = GL_TRUE;
635 }
636 else {
637 quickDraw = GL_FALSE;
638 }
639
640 /*
641 * General solution
642 */
643 {
644 GLubyte rgba[MAX_WIDTH][4];
645 GLint row;
646 if (width > MAX_WIDTH)
647 width = MAX_WIDTH;
648 for (row = 0; row < height; row++, y++) {
649 const GLvoid *source = _mesa_image_address(unpack,
650 pixels, width, height, format, type, 0, row, 0);
651 _mesa_unpack_ubyte_color_span(ctx, width, GL_RGBA, (void*) rgba,
652 format, type, source, unpack, GL_TRUE);
653
654 if (ctx->Texture.ReallyEnabled && ctx->Pixel.PixelTextureEnabled) {
655 GLfloat s[MAX_WIDTH], t[MAX_WIDTH], r[MAX_WIDTH], q[MAX_WIDTH];
656 GLuint unit;
657 /* XXX not sure how multitexture is supposed to work here */
658 for (unit = 0; unit < MAX_TEXTURE_UNITS; unit++) {
659 _mesa_pixeltexgen(ctx, width, (const GLubyte (*)[4]) rgba,
660 s, t, r, q);
661 gl_texture_pixels(ctx, unit, width, s, t, r, NULL, rgba);
662 }
663 }
664
665 if (quickDraw) {
666 (*ctx->Driver.WriteRGBASpan)( ctx, width, x, y,
667 (CONST GLubyte (*)[]) rgba, NULL);
668 }
669 else if (zoom) {
670 gl_write_zoomed_rgba_span( ctx, width, x, y, zspan,
671 (CONST GLubyte (*)[]) rgba, desty );
672 }
673 else {
674 gl_write_rgba_span( ctx, (GLuint) width, x, y, zspan, rgba, GL_BITMAP);
675 }
676 }
677 }
678 }
679
680
681
682 /*
683 * Execute glDrawPixels
684 */
685 void
686 _mesa_DrawPixels( GLsizei width, GLsizei height,
687 GLenum format, GLenum type, const GLvoid *pixels )
688 {
689 GET_CURRENT_CONTEXT(ctx);
690 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDrawPixels");
691
692 if (ctx->RenderMode==GL_RENDER) {
693 GLint x, y;
694 if (!pixels || !ctx->Current.RasterPosValid) {
695 return;
696 }
697
698 if (ctx->NewState) {
699 gl_update_state(ctx);
700 }
701
702 x = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
703 y = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
704
705 ctx->OcclusionResult = GL_TRUE;
706
707 /* see if device driver can do the drawpix */
708 if (ctx->Driver.DrawPixels
709 && (*ctx->Driver.DrawPixels)(ctx, x, y, width, height, format, type,
710 &ctx->Unpack, pixels)) {
711 return;
712 }
713
714 switch (format) {
715 case GL_STENCIL_INDEX:
716 draw_stencil_pixels( ctx, x, y, width, height, type, pixels );
717 break;
718 case GL_DEPTH_COMPONENT:
719 draw_depth_pixels( ctx, x, y, width, height, type, pixels );
720 break;
721 case GL_COLOR_INDEX:
722 if (ctx->Visual->RGBAflag)
723 draw_rgba_pixels(ctx, x,y, width, height, format, type, pixels);
724 else
725 draw_index_pixels(ctx, x, y, width, height, type, pixels);
726 break;
727 case GL_RED:
728 case GL_GREEN:
729 case GL_BLUE:
730 case GL_ALPHA:
731 case GL_LUMINANCE:
732 case GL_LUMINANCE_ALPHA:
733 case GL_RGB:
734 case GL_BGR:
735 case GL_RGBA:
736 case GL_BGRA:
737 case GL_ABGR_EXT:
738 draw_rgba_pixels(ctx, x, y, width, height, format, type, pixels);
739 break;
740 default:
741 gl_error( ctx, GL_INVALID_ENUM, "glDrawPixels(format)" );
742 return;
743 }
744 }
745 else if (ctx->RenderMode==GL_FEEDBACK) {
746 if (ctx->Current.RasterPosValid) {
747 GLfloat color[4];
748 GLfloat texcoord[4], invq;
749 UBYTE_RGBA_TO_FLOAT_RGBA(color, ctx->Current.ByteColor);
750 invq = 1.0F / ctx->Current.Texcoord[0][3];
751 texcoord[0] = ctx->Current.Texcoord[0][0] * invq;
752 texcoord[1] = ctx->Current.Texcoord[0][1] * invq;
753 texcoord[2] = ctx->Current.Texcoord[0][2] * invq;
754 texcoord[3] = ctx->Current.Texcoord[0][3];
755 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
756 gl_feedback_vertex( ctx,
757 ctx->Current.RasterPos,
758 color, ctx->Current.Index, texcoord );
759 }
760 }
761 else if (ctx->RenderMode==GL_SELECT) {
762 if (ctx->Current.RasterPosValid) {
763 gl_update_hitflag( ctx, ctx->Current.RasterPos[2] );
764 }
765 }
766 }
767