blob: edf524409a201766315c57f827abcaff921135fd [file] [log] [blame]
Keith Whitwell1bf9dfa1999-09-18 20:41:22 +00001/* $Id: drawpix.c,v 1.2 1999/09/18 20:41:22 keithw Exp $ */
jtgafb833d1999-08-19 00:55:39 +00002
3/*
4 * Mesa 3-D graphics library
5 * Version: 3.1
6 *
7 * Copyright (C) 1999 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
29
30
31#ifdef PC_HEADER
32#include "all.h"
33#else
34#include <assert.h>
35#include <stdlib.h>
36#include <string.h>
37#include "context.h"
38#include "drawpix.h"
39#include "feedback.h"
40#include "image.h"
41#include "macros.h"
42#include "mmath.h"
43#include "pixel.h"
44#include "span.h"
45#include "stencil.h"
46#include "types.h"
47#include "zoom.h"
48#ifdef XFree86Server
49#include "GL/xf86glx.h"
50#endif
51#endif
52
53
54
55/* TODO: apply texture mapping to fragments */
56
57
58/*
59 * Try to do a fast glDrawPixels. Conditions include:
60 * not using a display list
61 * simple pixel unpacking
62 * no raster ops
63 * etc....
64 * Return: GL_TRUE if success, GL_FALSE if slow path must be used instead
65 */
66GLboolean gl_direct_DrawPixels( GLcontext *ctx,
67 const struct gl_pixelstore_attrib *unpack,
68 GLsizei width, GLsizei height,
69 GLenum format, GLenum type,
70 const GLvoid *pixels )
71{
72 GLubyte rgb[MAX_WIDTH][3];
73 GLubyte rgba[MAX_WIDTH][4];
74
75 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glDrawPixels",
76 GL_FALSE);
77
78
79 if (!ctx->Current.RasterPosValid) {
80 /* no-op */
81 return GL_TRUE;
82 }
83
84 if (ctx->NewState) {
85 gl_update_state(ctx);
86 }
87
88 /* see if device driver can do the drawpix */
89 if (ctx->Driver.DrawPixels) {
90 GLint x = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
91 GLint y = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
92 if ((*ctx->Driver.DrawPixels)(ctx, x, y, width, height, format, type,
93 unpack, pixels))
94 return GL_TRUE;
95 }
96
97 if ((ctx->RasterMask&(~(SCISSOR_BIT|WINCLIP_BIT)))==0
98 && ctx->Pixel.RedBias==0.0 && ctx->Pixel.RedScale==1.0
99 && ctx->Pixel.GreenBias==0.0 && ctx->Pixel.GreenScale==1.0
100 && ctx->Pixel.BlueBias==0.0 && ctx->Pixel.BlueScale==1.0
101 && ctx->Pixel.AlphaBias==0.0 && ctx->Pixel.AlphaScale==1.0
102 && ctx->Pixel.IndexShift==0 && ctx->Pixel.IndexOffset==0
103 && ctx->Pixel.MapColorFlag==0
104 && unpack->Alignment==1
105 && !unpack->SwapBytes
106 && !unpack->LsbFirst) {
107
108 GLint destX = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
109 GLint destY = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
110 GLint drawWidth = width; /* actual width drawn */
111 GLint drawHeight = height; /* actual height drawn */
112 GLint skipPixels = unpack->SkipPixels;
113 GLint skipRows = unpack->SkipRows;
114 GLint rowLength;
115 GLdepth zSpan[MAX_WIDTH]; /* only used when zooming */
116 GLint zoomY0;
117
118 if (unpack->RowLength > 0)
119 rowLength = unpack->RowLength;
120 else
121 rowLength = width;
122
123 /* If we're not using pixel zoom then do all clipping calculations
124 * now. Otherwise, we'll let the gl_write_zoomed_*_span() functions
125 * handle the clipping.
126 */
127 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
128 /* horizontal clipping */
129 if (destX < ctx->Buffer->Xmin) {
130 skipPixels += (ctx->Buffer->Xmin - destX);
131 drawWidth -= (ctx->Buffer->Xmin - destX);
132 destX = ctx->Buffer->Xmin;
133 }
134 if (destX + drawWidth > ctx->Buffer->Xmax)
135 drawWidth -= (destX + drawWidth - ctx->Buffer->Xmax - 1);
136 if (drawWidth <= 0)
137 return GL_TRUE;
138
139 /* vertical clipping */
140 if (destY < ctx->Buffer->Ymin) {
141 skipRows += (ctx->Buffer->Ymin - destY);
142 drawHeight -= (ctx->Buffer->Ymin - destY);
143 destY = ctx->Buffer->Ymin;
144 }
145 if (destY + drawHeight > ctx->Buffer->Ymax)
146 drawHeight -= (destY + drawHeight - ctx->Buffer->Ymax - 1);
147 if (drawHeight <= 0)
148 return GL_TRUE;
149 }
150 else {
151 /* setup array of fragment Z value to pass to zoom function */
152 GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
153 GLint i;
154 assert(drawWidth < MAX_WIDTH);
155 for (i=0; i<drawWidth; i++)
156 zSpan[i] = z;
157
158 /* save Y value of first row */
159 zoomY0 = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
160 }
161
162
163 /*
164 * Ready to draw!
165 * The window region at (destX, destY) of size (drawWidth, drawHeight)
166 * will be written to.
167 * We'll take pixel data from buffer pointed to by "pixels" but we'll
168 * skip "skipRows" rows and skip "skipPixels" pixels/row.
169 */
170
171 if (format==GL_RGBA && type==GL_UNSIGNED_BYTE) {
172 if (ctx->Visual->RGBAflag) {
173 GLubyte *src = (GLubyte *) pixels
174 + (skipRows * rowLength + skipPixels) * 4;
175 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
176 /* no zooming */
177 GLint row;
178 for (row=0; row<drawHeight; row++) {
179 (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY,
180 (void *) src, NULL);
181 src += rowLength * 4;
182 destY++;
183 }
184 }
185 else {
186 /* with zooming */
187 GLint row;
188 for (row=0; row<drawHeight; row++) {
189 gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY,
190 zSpan, (void *) src, zoomY0);
191 src += rowLength * 4;
192 destY++;
193 }
194 }
195 }
196 return GL_TRUE;
197 }
198 else if (format==GL_RGB && type==GL_UNSIGNED_BYTE) {
199 if (ctx->Visual->RGBAflag) {
200 GLubyte *src = (GLubyte *) pixels
201 + (skipRows * rowLength + skipPixels) * 3;
202 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
203 GLint row;
204 for (row=0; row<drawHeight; row++) {
205 (*ctx->Driver.WriteRGBSpan)(ctx, drawWidth, destX, destY,
206 (void *) src, NULL);
207 src += rowLength * 3;
208 destY++;
209 }
210 }
211 else {
212 /* with zooming */
213 GLint row;
214 for (row=0; row<drawHeight; row++) {
215 gl_write_zoomed_rgb_span(ctx, drawWidth, destX, destY,
216 zSpan, (void *) src, zoomY0);
217 src += rowLength * 3;
218 destY++;
219 }
220 }
221 }
222 return GL_TRUE;
223 }
224 else if (format==GL_LUMINANCE && type==GL_UNSIGNED_BYTE) {
225 if (ctx->Visual->RGBAflag) {
226 GLubyte *src = (GLubyte *) pixels
227 + (skipRows * rowLength + skipPixels);
228 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
229 /* no zooming */
230 GLint row;
231 assert(drawWidth < MAX_WIDTH);
232 for (row=0; row<drawHeight; row++) {
233 GLint i;
234 for (i=0;i<drawWidth;i++) {
235 rgb[i][0] = src[i];
236 rgb[i][1] = src[i];
237 rgb[i][2] = src[i];
238 }
239 (*ctx->Driver.WriteRGBSpan)(ctx, drawWidth, destX, destY,
240 (void *) rgb, NULL);
241 src += rowLength;
242 destY++;
243 }
244 }
245 else {
246 /* with zooming */
247 GLint row;
248 assert(drawWidth < MAX_WIDTH);
249 for (row=0; row<drawHeight; row++) {
250 GLint i;
251 for (i=0;i<drawWidth;i++) {
252 rgb[i][0] = src[i];
253 rgb[i][1] = src[i];
254 rgb[i][2] = src[i];
255 }
256 gl_write_zoomed_rgb_span(ctx, drawWidth, destX, destY,
257 zSpan, (void *) rgb, zoomY0);
258 src += rowLength;
259 destY++;
260 }
261 }
262 }
263 return GL_TRUE;
264 }
265 else if (format==GL_LUMINANCE_ALPHA && type==GL_UNSIGNED_BYTE) {
266 if (ctx->Visual->RGBAflag) {
267 GLubyte *src = (GLubyte *) pixels
268 + (skipRows * rowLength + skipPixels)*2;
269 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
270 /* no zooming */
271 GLint row;
272 assert(drawWidth < MAX_WIDTH);
273 for (row=0; row<drawHeight; row++) {
274 GLint i;
275 GLubyte *ptr = src;
276 for (i=0;i<drawWidth;i++) {
277 rgba[i][0] = *ptr;
278 rgba[i][1] = *ptr;
279 rgba[i][2] = *ptr++;
280 rgba[i][3] = *ptr++;
281 }
282 (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY,
283 (void *) rgba, NULL);
284 src += rowLength*2;
285 destY++;
286 }
287 }
288 else {
289 /* with zooming */
290 GLint row;
291 assert(drawWidth < MAX_WIDTH);
292 for (row=0; row<drawHeight; row++) {
293 GLubyte *ptr = src;
294 GLint i;
295 for (i=0;i<drawWidth;i++) {
296 rgba[i][0] = *ptr;
297 rgba[i][1] = *ptr;
298 rgba[i][2] = *ptr++;
299 rgba[i][3] = *ptr++;
300 }
301 gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY,
302 zSpan, (void *) rgba, zoomY0);
303 src += rowLength*2;
304 destY++;
305 }
306 }
307 }
308 return GL_TRUE;
309 }
310 else if (format==GL_COLOR_INDEX && type==GL_UNSIGNED_BYTE) {
311 GLubyte *src = (GLubyte *) pixels + skipRows * rowLength + skipPixels;
312 if (ctx->Visual->RGBAflag) {
313 /* convert CI data to RGBA */
314 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
315 /* no zooming */
316 GLint row;
317 for (row=0; row<drawHeight; row++) {
318 assert(drawWidth < MAX_WIDTH);
319 gl_map_ci8_to_rgba(ctx, drawWidth, src, rgba);
320 (*ctx->Driver.WriteRGBASpan)(ctx, drawWidth, destX, destY,
321 (const GLubyte (*)[4])rgba,
322 NULL);
323 src += rowLength;
324 destY++;
325 }
326 return GL_TRUE;
327 }
328 else {
329 /* with zooming */
330 GLint row;
331 for (row=0; row<drawHeight; row++) {
332 assert(drawWidth < MAX_WIDTH);
333 gl_map_ci8_to_rgba(ctx, drawWidth, src, rgba);
334 gl_write_zoomed_rgba_span(ctx, drawWidth, destX, destY,
335 zSpan, (void *) rgba, zoomY0);
336 src += rowLength;
337 destY++;
338 }
339 return GL_TRUE;
340 }
341 }
342 else {
343 /* write CI data to CI frame buffer */
344 GLint row;
345 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
346 /* no zooming */
347 for (row=0; row<drawHeight; row++) {
348 (*ctx->Driver.WriteCI8Span)(ctx, drawWidth, destX, destY,
349 src, NULL);
350 src += rowLength;
351 destY++;
352 }
353 return GL_TRUE;
354 }
355 else {
356 /* with zooming */
357 return GL_FALSE;
358 }
359 }
360 }
361 else {
362 /* can't handle this pixel format and/or data type here */
363 return GL_FALSE;
364 }
365 }
366 else {
367 /* can't do direct render, have to use slow path */
368 return GL_FALSE;
369 }
370}
371
372
373
374/*
375 * Do glDrawPixels of index pixels.
376 */
377static void draw_index_pixels( GLcontext *ctx, GLint x, GLint y,
378 const struct gl_image *image )
379{
380 GLint width, height, widthInBytes;
381 const GLint desty = y;
382 GLint i, j;
383 GLdepth zspan[MAX_WIDTH];
384 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
385
386 assert(image);
387 assert(image->Format == GL_COLOR_INDEX);
388
389 width = image->Width;
390 height = image->Height;
391 if (image->Type == GL_BITMAP)
392 widthInBytes = (width + 7) / 8;
393 else
394 widthInBytes = width;
395
396 /* Fragment depth values */
397 if (ctx->Depth.Test) {
398 GLdepth zval = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
399 for (i=0;i<width;i++) {
400 zspan[i] = zval;
401 }
402 }
403
404 /* process the image row by row */
405 for (i=0;i<height;i++,y++) {
406 GLuint ispan[MAX_WIDTH];
407
408 /* convert to uints */
409 switch (image->Type) {
410 case GL_UNSIGNED_BYTE:
411 {
412 GLubyte *src = (GLubyte *) image->Data + i * width;
413 for (j=0;j<width;j++) {
414 ispan[j] = (GLuint) *src++;
415 }
416 }
417 break;
418 case GL_FLOAT:
419 {
420 GLfloat *src = (GLfloat *) image->Data + i * width;
421 for (j=0;j<width;j++) {
422 ispan[j] = (GLuint) (GLint) *src++;
423 }
424 }
425 break;
426 case GL_BITMAP:
427 {
428 GLubyte *src = (GLubyte *) image->Data + i * widthInBytes;
429 for (j=0;j<width;j++) {
430 ispan[j] = ( src[j >> 3] >> (7 - (j & 0x7)) ) & 1;
431 }
432 }
433 break;
434 default:
435 gl_problem( ctx, "draw_index_pixels type" );
436 return;
437 }
438
439 /* apply shift and offset */
440 if (ctx->Pixel.IndexOffset || ctx->Pixel.IndexShift) {
441 gl_shift_and_offset_ci( ctx, width, ispan );
442 }
443
444 if (ctx->Visual->RGBAflag) {
445 /* Convert index to RGBA and write to frame buffer */
446 GLubyte rgba[MAX_WIDTH][4];
447 gl_map_ci_to_rgba( ctx, width, ispan, rgba );
448 if (zoom) {
449 gl_write_zoomed_rgba_span( ctx, width, x, y, zspan,
450 (const GLubyte (*)[4])rgba, desty );
451 }
452 else {
453 gl_write_rgba_span( ctx, width, x, y, zspan, rgba, GL_BITMAP );
454 }
455 }
456 else {
457 /* optionally apply index map then write to frame buffer */
458 if (ctx->Pixel.MapColorFlag) {
459 gl_map_ci(ctx, width, ispan);
460 }
461 if (zoom) {
462 gl_write_zoomed_index_span( ctx, width, x, y, zspan, ispan, desty );
463 }
464 else {
465 gl_write_index_span( ctx, width, x, y, zspan, ispan, GL_BITMAP );
466 }
467 }
468 }
469
470}
471
472
473
474/*
475 * Do glDrawPixels of stencil image. The image datatype may either
476 * be GLubyte or GLbitmap.
477 */
478static void draw_stencil_pixels( GLcontext *ctx, GLint x, GLint y,
479 const struct gl_image *image )
480{
481 GLint widthInBytes, width, height;
482 const GLint desty = y;
483 GLint i;
484 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
485
486 assert(image);
487 assert(image->Format == GL_STENCIL_INDEX);
488 assert(image->Type == GL_UNSIGNED_BYTE || image->Type == GL_BITMAP);
489
490 if (image->Type == GL_UNSIGNED_BYTE)
491 widthInBytes = image->Width;
492 else
493 widthInBytes = (image->Width + 7) / 8;
494 width = image->Width;
495 height = image->Height;
496
497 /* process the image row by row */
498 for (i=0;i<height;i++,y++) {
499 GLstencil *src = (GLstencil*)image->Data + i * widthInBytes;
500 GLstencil *stencilValues;
501 GLstencil stencilCopy[MAX_WIDTH];
502
503 if (image->Type == GL_BITMAP) {
504 /* convert bitmap data to GLubyte (0 or 1) data */
505 GLint j;
506 for (j = 0; j < width; j++) {
507 stencilCopy[j] = ( src[j >> 3] >> (7 - (j & 0x7)) ) & 1;
508 }
509 src = stencilCopy;
510 }
511
512 if (ctx->Pixel.IndexOffset || ctx->Pixel.IndexShift
513 || ctx->Pixel.MapStencilFlag) {
514
515 /* make copy of stencil values */
516 if (src != stencilCopy)
517 MEMCPY( stencilCopy, src, width * sizeof(GLstencil));
518
519 /* apply shift and offset */
520 if (ctx->Pixel.IndexOffset || ctx->Pixel.IndexShift) {
521 gl_shift_and_offset_stencil( ctx, width, stencilCopy );
522 }
523
524 /* mapping */
525 if (ctx->Pixel.MapStencilFlag) {
526 gl_map_stencil( ctx, width, stencilCopy );
527 }
528
529 stencilValues = stencilCopy;
530 }
531 else {
532 /* use stencil values in-place */
533 stencilValues = src;
534 }
535
536 /* write stencil values to stencil buffer */
537 if (zoom) {
538 gl_write_zoomed_stencil_span( ctx, (GLuint) width, x, y,
539 stencilValues, desty );
540 }
541 else {
542 gl_write_stencil_span( ctx, (GLuint) width, x, y, stencilValues );
543 }
544 }
545}
546
547
548
549/*
550 * Do a glDrawPixels of depth values.
551 */
552static void draw_depth_pixels( GLcontext *ctx, GLint x, GLint y,
553 const struct gl_image *image )
554{
555 GLint width, height;
556 const GLint desty = y;
557 GLubyte rgba[MAX_WIDTH][4];
558 GLuint ispan[MAX_WIDTH];
559 const GLboolean bias_or_scale = ctx->Pixel.DepthBias!=0.0 || ctx->Pixel.DepthScale!=1.0;
560 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
561
562 assert(image);
563 assert(image->Format == GL_DEPTH_COMPONENT);
564
565 width = image->Width;
566 height = image->Height;
567
568 /* Color or index */
569 if (ctx->Visual->RGBAflag) {
570 GLint r = (GLint) (ctx->Current.RasterColor[0] * 255.0F);
571 GLint g = (GLint) (ctx->Current.RasterColor[1] * 255.0F);
572 GLint b = (GLint) (ctx->Current.RasterColor[2] * 255.0F);
573 GLint a = (GLint) (ctx->Current.RasterColor[3] * 255.0F);
574 GLint i;
575 for (i=0; i<width; i++) {
576 rgba[i][RCOMP] = r;
577 rgba[i][GCOMP] = g;
578 rgba[i][BCOMP] = b;
579 rgba[i][ACOMP] = a;
580 }
581 }
582 else {
583 GLint i;
584 for (i=0;i<width;i++) {
585 ispan[i] = ctx->Current.RasterIndex;
586 }
587 }
588
589 if (image->Type==GL_UNSIGNED_SHORT && sizeof(GLdepth)==sizeof(GLushort)
590 && !bias_or_scale && !zoom && ctx->Visual->RGBAflag) {
591 /* Special case: directly write 16-bit depth values */
592 GLint j;
593 for (j=0;j<height;j++,y++) {
594 GLdepth *zptr = (GLdepth *) image->Data + j * width;
595 gl_write_rgba_span( ctx, width, x, y, zptr, rgba, GL_BITMAP );
596 }
597 }
598 else if (image->Type==GL_UNSIGNED_INT && sizeof(GLdepth)==sizeof(GLuint)
599 && !bias_or_scale && !zoom && ctx->Visual->RGBAflag) {
600 /* Special case: directly write 32-bit depth values */
601 GLint i, j;
602 /* Compute shift value to scale 32-bit uints down to depth values. */
603 GLuint shift = 0;
604 GLuint max = MAX_DEPTH;
605 while ((max&0x80000000)==0) {
606 max = max << 1;
607 shift++;
608 }
609 for (j=0;j<height;j++,y++) {
610 GLdepth zspan[MAX_WIDTH];
611 GLuint *zptr = (GLuint *) image->Data + j * width;
612 for (i=0;i<width;i++) {
613 zspan[i] = zptr[i] >> shift;
614 }
615 gl_write_rgba_span( ctx, width, x, y, zspan, rgba, GL_BITMAP );
616 }
617 }
618 else {
619 /* General case (slower) */
620 GLint i, j;
621
622 /* process image row by row */
623 for (i=0;i<height;i++,y++) {
624 GLfloat depth[MAX_WIDTH];
625 GLdepth zspan[MAX_WIDTH];
626
627 switch (image->Type) {
628 case GL_UNSIGNED_SHORT:
629 {
630 GLushort *src = (GLushort *) image->Data + i * width;
631 for (j=0;j<width;j++) {
632 depth[j] = USHORT_TO_FLOAT( *src++ );
633 }
634 }
635 break;
636 case GL_UNSIGNED_INT:
637 {
638 GLuint *src = (GLuint *) image->Data + i * width;
639 for (j=0;j<width;j++) {
640 depth[j] = UINT_TO_FLOAT( *src++ );
641 }
642 }
643 break;
644 case GL_FLOAT:
645 {
646 GLfloat *src = (GLfloat *) image->Data + i * width;
647 for (j=0;j<width;j++) {
648 depth[j] = *src++;
649 }
650 }
651 break;
652 default:
653 gl_problem(ctx, "Bad type in draw_depth_pixels");
654 return;
655 }
656
657 /* apply depth scale and bias */
658 if (ctx->Pixel.DepthScale!=1.0 || ctx->Pixel.DepthBias!=0.0) {
659 for (j=0;j<width;j++) {
660 depth[j] = depth[j] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
661 }
662 }
663
664 /* clamp depth values to [0,1] and convert from floats to integers */
665 for (j=0;j<width;j++) {
666 zspan[j] = (GLdepth) (CLAMP( depth[j], 0.0F, 1.0F ) * DEPTH_SCALE);
667 }
668
669 if (ctx->Visual->RGBAflag) {
670 if (zoom) {
671 gl_write_zoomed_rgba_span( ctx, width, x, y, zspan,
672 (const GLubyte (*)[4])rgba, desty );
673 }
674 else {
675 gl_write_rgba_span( ctx, width, x, y, zspan, rgba, GL_BITMAP );
676 }
677 }
678 else {
679 if (zoom) {
680 gl_write_zoomed_index_span( ctx, width, x, y, zspan,
681 ispan, GL_BITMAP );
682 }
683 else {
684 gl_write_index_span( ctx, width, x, y, zspan, ispan, GL_BITMAP );
685 }
686 }
687
688 }
689 }
690}
691
692
693
694/* Simple unpacking parameters: */
695static struct gl_pixelstore_attrib NoUnpack = {
696 1, /* Alignment */
697 0, /* RowLength */
698 0, /* SkipPixels */
699 0, /* SkipRows */
700 0, /* ImageHeight */
701 0, /* SkipImages */
702 GL_FALSE, /* SwapBytes */
703 GL_FALSE /* LsbFirst */
704};
705
706
707/*
708 * Do glDrawPixels of RGBA pixels.
709 */
710static void draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y,
711 const struct gl_image *image )
712{
713 GLint width, height;
714 GLint i, j;
715 const GLint desty = y;
716 GLdepth zspan[MAX_WIDTH];
717 GLboolean quickDraw;
718 const GLboolean zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
719
720 assert(image);
721
722 /* Try an optimized glDrawPixels first */
723 if (gl_direct_DrawPixels(ctx, &NoUnpack, image->Width, image->Height,
724 image->Format, image->Type, image->Data ))
725 return;
726
727 width = image->Width;
728 height = image->Height;
729
730 /* Fragment depth values */
731 if (ctx->Depth.Test) {
732 /* fill in array of z values */
733 GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
734 for (i=0;i<width;i++) {
735 zspan[i] = z;
736 }
737 }
738
739 if (ctx->RasterMask==0 && !zoom && x>=0 && y>=0
740 && x+width<=ctx->Buffer->Width && y+height<=ctx->Buffer->Height) {
741 quickDraw = GL_TRUE;
742 }
743 else {
744 quickDraw = GL_FALSE;
745 }
746
747 {
748 /* General solution */
749 GLboolean r_flag, g_flag, b_flag, a_flag, l_flag;
750 GLuint components;
751 GLubyte rgba[MAX_WIDTH][4];
752 GLfloat rf[MAX_WIDTH];
753 GLfloat gf[MAX_WIDTH];
754 GLfloat bf[MAX_WIDTH];
755 DEFARRAY(GLfloat,af,MAX_WIDTH);
756 CHECKARRAY(af,return);
757
758 r_flag = g_flag = b_flag = a_flag = l_flag = GL_FALSE;
759 switch (image->Format) {
760 case GL_RED:
761 r_flag = GL_TRUE;
762 components = 1;
763 break;
764 case GL_GREEN:
765 g_flag = GL_TRUE;
766 components = 1;
767 break;
768 case GL_BLUE:
769 b_flag = GL_TRUE;
770 components = 1;
771 break;
772 case GL_ALPHA:
773 a_flag = GL_TRUE;
774 components = 1;
775 break;
776 case GL_RGB:
777 r_flag = g_flag = b_flag = GL_TRUE;
778 components = 3;
779 break;
780 case GL_LUMINANCE:
781 l_flag = GL_TRUE;
782 components = 1;
783 break;
784 case GL_LUMINANCE_ALPHA:
785 l_flag = a_flag = GL_TRUE;
786 components = 2;
787 break;
788 case GL_RGBA:
789 r_flag = g_flag = b_flag = a_flag = GL_TRUE;
790 components = 4;
791 break;
792 default:
793 gl_problem(ctx, "Bad type in draw_rgba_pixels");
794 goto cleanup;
795 }
796
797 /* process the image row by row */
798 for (i=0;i<height;i++,y++) {
799 /* convert to floats */
800 switch (image->Type) {
801 case GL_UNSIGNED_BYTE:
802 {
803 GLubyte *src = (GLubyte *) image->Data + i * width * components;
804 for (j=0;j<width;j++) {
805 if (l_flag) {
806 rf[j] = gf[j] = bf[j] = UBYTE_TO_FLOAT(*src++);
807 }
808 else {
809 rf[j] = r_flag ? UBYTE_TO_FLOAT(*src++) : 0.0;
810 gf[j] = g_flag ? UBYTE_TO_FLOAT(*src++) : 0.0;
811 bf[j] = b_flag ? UBYTE_TO_FLOAT(*src++) : 0.0;
812 }
813 af[j] = a_flag ? UBYTE_TO_FLOAT(*src++) : 1.0;
814 }
815 }
816 break;
817 case GL_FLOAT:
818 {
819 GLfloat *src = (GLfloat *) image->Data + i * width * components;
820 for (j=0;j<width;j++) {
821 if (l_flag) {
822 rf[j] = gf[j] = bf[j] = *src++;
823 }
824 else {
825 rf[j] = r_flag ? *src++ : 0.0;
826 gf[j] = g_flag ? *src++ : 0.0;
827 bf[j] = b_flag ? *src++ : 0.0;
828 }
829 af[j] = a_flag ? *src++ : 1.0;
830 }
831 }
832 break;
833 default:
834 gl_problem( ctx, "draw_rgba_pixels type" );
835 goto cleanup;
836 }
837
838 /* apply scale and bias */
839 if (ctx->Pixel.ScaleOrBiasRGBA) {
840 gl_scale_and_bias_color(ctx, width, rf, gf, bf, af);
841 }
842
843 /* apply pixel mappings */
844 if (ctx->Pixel.MapColorFlag) {
845 gl_map_color(ctx, width, rf, gf, bf, af);
846 }
847
848 /* convert to integers */
849 for (j=0;j<width;j++) {
850 rgba[j][RCOMP] = (GLint) (rf[j] * 255.0F);
851 rgba[j][GCOMP] = (GLint) (gf[j] * 255.0F);
852 rgba[j][BCOMP] = (GLint) (bf[j] * 255.0F);
853 rgba[j][ACOMP] = (GLint) (af[j] * 255.0F);
854 }
855
856 /* write to frame buffer */
857 if (quickDraw) {
858 (*ctx->Driver.WriteRGBASpan)( ctx, width, x, y,
859 (const GLubyte (*)[4])rgba, NULL);
860 }
861 else if (zoom) {
862 gl_write_zoomed_rgba_span( ctx, width, x, y, zspan,
863 (const GLubyte (*)[4])rgba, desty );
864 }
865 else {
866 gl_write_rgba_span( ctx, (GLuint) width, x, y, zspan, rgba, GL_BITMAP);
867 }
868 }
869cleanup:
870 UNDEFARRAY(af);
871 }
872}
873
874
875
876/*
877 * Execute glDrawPixels
878 */
879void gl_DrawPixels( GLcontext* ctx, struct gl_image *image )
880{
881 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDrawPixels");
882
883
884 if (gl_image_error_test( ctx, image, "glDrawPixels" ))
885 return;
886
887 if (ctx->RenderMode==GL_RENDER) {
888 GLint x, y;
889 if (!ctx->Current.RasterPosValid) {
890 return;
891 }
892
893 x = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
894 y = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
895
896 switch (image->Format) {
897 case GL_COLOR_INDEX:
898 draw_index_pixels( ctx, x, y, image );
899 break;
900 case GL_STENCIL_INDEX:
901 draw_stencil_pixels( ctx, x, y, image );
902 break;
903 case GL_DEPTH_COMPONENT:
904 draw_depth_pixels( ctx, x, y, image );
905 break;
906 case GL_RED:
907 case GL_GREEN:
908 case GL_BLUE:
909 case GL_ALPHA:
910 case GL_RGB:
911 case GL_LUMINANCE:
912 case GL_LUMINANCE_ALPHA:
913 case GL_RGBA:
914 draw_rgba_pixels( ctx, x, y, image );
915 break;
916 default:
917 gl_error( ctx, GL_INVALID_ENUM, "glDrawPixels" );
918 return;
919 }
920 }
921 else if (ctx->RenderMode==GL_FEEDBACK) {
922 if (ctx->Current.RasterPosValid) {
923 GLfloat color[4];
924 GLfloat texcoord[4], invq;
925 UBYTE_RGBA_TO_FLOAT_RGBA(color, ctx->Current.ByteColor);
926 invq = 1.0F / ctx->Current.Texcoord[0][3];
927 texcoord[0] = ctx->Current.Texcoord[0][0] * invq;
928 texcoord[1] = ctx->Current.Texcoord[0][1] * invq;
929 texcoord[2] = ctx->Current.Texcoord[0][2] * invq;
930 texcoord[3] = ctx->Current.Texcoord[0][3];
931 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_DRAW_PIXEL_TOKEN );
932 gl_feedback_vertex( ctx,
Keith Whitwell1bf9dfa1999-09-18 20:41:22 +0000933 ctx->Current.RasterPos,
jtgafb833d1999-08-19 00:55:39 +0000934 color, ctx->Current.Index, texcoord );
935 }
936 }
937 else if (ctx->RenderMode==GL_SELECT) {
938 if (ctx->Current.RasterPosValid) {
939 gl_update_hitflag( ctx, ctx->Current.RasterPos[2] );
940 }
941 }
942}
943