Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/drawpix.c

raw
1 /* $Id: drawpix.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.2
6 * Copyright (C) 1995-1997 Brian Paul
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include "context.h"
26 #include "drawpix.h"
27 #include "feedback.h"
28 #include "dlist.h"
29 #include "macros.h"
30 #include "pixel.h"
31 #include "span.h"
32 #include "stencil.h"
33 #include "types.h"
34
35
36
37
38 /* TODO: apply texture mapping to fragments */
39
40
41
42 static void draw_index_pixels( GLcontext* ctx, GLsizei width, GLsizei height,
43 GLenum type, const GLvoid *pixels )
44 {
45 GLint x, y, desty;
46 GLuint i, j;
47 GLdepth zspan[MAX_WIDTH];
48 GLboolean zoom;
49
50 zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
51
52 /* Position, depth of pixels */
53 x = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
54 y = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
55 desty = y;
56 if (ctx->Depth.Test) {
57 GLdepth zval = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
58 for (i=0;i<width;i++) {
59 zspan[i] = zval;
60 }
61 }
62
63 /* process the image row by row */
64 for (i=0;i<height;i++,y++) {
65 GLuint ispan[MAX_WIDTH];
66
67 /* convert to uints */
68 switch (type) {
69 case GL_UNSIGNED_BYTE:
70 {
71 GLubyte *src = (GLubyte *) pixels + i * width;
72 for (j=0;j<width;j++) {
73 ispan[j] = (GLuint) *src++;
74 }
75 }
76 break;
77 case GL_BYTE:
78 {
79 GLbyte *src = (GLbyte *) pixels + i * width;
80 for (j=0;j<width;j++) {
81 ispan[j] = (GLuint) *src++;
82 }
83 }
84 break;
85 case GL_UNSIGNED_SHORT:
86 {
87 GLushort *src = (GLushort *) pixels + i * width;
88 for (j=0;j<width;j++) {
89 ispan[j] = (GLuint) *src++;
90 }
91 }
92 break;
93 case GL_SHORT:
94 {
95 GLshort *src = (GLshort *) pixels + i * width;
96 for (j=0;j<width;j++) {
97 ispan[j] = (GLuint) *src++;
98 }
99 }
100 break;
101 case GL_UNSIGNED_INT:
102 {
103 GLuint *src = (GLuint *) pixels + i * width;
104 for (j=0;j<width;j++) {
105 ispan[j] = *src++;
106 }
107 }
108 break;
109 case GL_INT:
110 {
111 GLint *src = (GLint *) pixels + i * width;
112 for (j=0;j<width;j++) {
113 ispan[j] = (GLuint) *src++;
114 }
115 }
116 break;
117 case GL_BITMAP:
118 /* TODO */
119 break;
120 case GL_FLOAT:
121 {
122 GLfloat *src = (GLfloat *) pixels + i * width;
123 for (j=0;j<width;j++) {
124 ispan[j] = (GLuint) (GLint) *src++;
125 }
126 }
127 break;
128 default:
129 gl_error( ctx, GL_INVALID_ENUM, "Internal: draw_index_pixels" );
130 }
131
132 /* apply shift and offset */
133 if (ctx->Pixel.IndexOffset || ctx->Pixel.IndexShift) {
134 if (ctx->Pixel.IndexShift>=0) {
135 for (j=0;j<width;j++) {
136 ispan[j] = (ispan[j] << ctx->Pixel.IndexShift)
137 + ctx->Pixel.IndexOffset;
138 }
139 }
140 else {
141 for (j=0;j<width;j++) {
142 ispan[j] = (ispan[j] >> -ctx->Pixel.IndexShift)
143 + ctx->Pixel.IndexOffset;
144 }
145 }
146 }
147
148 if (ctx->Visual->RGBAflag) {
149 /* Convert index to RGBA and write to frame buffer */
150 GLubyte red[MAX_WIDTH], green[MAX_WIDTH];
151 GLubyte blue[MAX_WIDTH], alpha[MAX_WIDTH];
152 for (j=0;j<width;j++) {
153 red[j] = (GLint) (ctx->Pixel.MapItoR[ispan[j]] * ctx->Visual->RedScale);
154 green[j] = (GLint) (ctx->Pixel.MapItoG[ispan[j]] * ctx->Visual->GreenScale);
155 blue[j] = (GLint) (ctx->Pixel.MapItoB[ispan[j]] * ctx->Visual->BlueScale);
156 alpha[j] = (GLint) (ctx->Pixel.MapItoA[ispan[j]] * ctx->Visual->AlphaScale);
157 }
158 if (zoom) {
159 gl_write_zoomed_color_span( ctx, width, x, y, zspan,
160 red, green, blue, alpha, desty );
161 }
162 else {
163 gl_write_color_span( ctx, width, x, y, zspan,
164 red, green, blue, alpha, GL_BITMAP );
165 }
166 }
167 else {
168 /* optionally apply index map then write to frame buffer */
169 if (ctx->Pixel.MapColorFlag) {
170 for (j=0;j<width;j++) {
171 ispan[j] = ctx->Pixel.MapItoI[ispan[j]];
172 }
173 }
174 if (zoom) {
175 gl_write_zoomed_index_span( ctx, width, x, y, zspan, ispan, desty );
176 }
177 else {
178 gl_write_index_span( ctx, width, x, y, zspan, ispan, GL_BITMAP );
179 }
180 }
181 }
182
183 }
184
185
186
187 static void draw_stencil_pixels( GLcontext* ctx, GLsizei width, GLsizei height,
188 GLenum type, const GLvoid *pixels )
189 {
190 GLint x, y, desty;
191 GLuint i, j;
192 GLboolean zoom;
193
194 zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
195
196 /* Position, depth of pixels */
197 x = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
198 y = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
199 desty = y;
200
201 /* process the image row by row */
202 for (i=0;i<height;i++,y++) {
203 GLubyte stencil[MAX_WIDTH];
204
205 /* convert to ubytes */
206 switch (type) {
207 case GL_UNSIGNED_BYTE:
208 {
209 GLubyte *src = (GLubyte *) pixels + i * width;
210 MEMCPY( stencil, src, width );
211 }
212 break;
213 case GL_BYTE:
214 {
215 GLbyte *src = (GLbyte *) pixels + i * width;
216 MEMCPY( stencil, src, width );
217 }
218 break;
219 case GL_UNSIGNED_SHORT:
220 {
221 GLushort *src = (GLushort *) pixels + i * width;
222 for (j=0;j<width;j++) {
223 stencil[j] = (GLubyte) ((*src++) & 0xff);
224 }
225 }
226 break;
227 case GL_SHORT:
228 {
229 GLshort *src = (GLshort *) pixels + i * width;
230 for (j=0;j<width;j++) {
231 stencil[j] = (GLubyte) ((*src++) & 0xff);
232 }
233 }
234 break;
235 case GL_UNSIGNED_INT:
236 {
237 GLuint *src = (GLuint *) pixels + i * width;
238 for (j=0;j<width;j++) {
239 stencil[j] = (GLubyte) ((*src++) & 0xff);
240 }
241 }
242 break;
243 case GL_INT:
244 {
245 GLint *src = (GLint *) pixels + i * width;
246 for (j=0;j<width;j++) {
247 stencil[j] = (GLubyte) ((*src++) & 0xff);
248 }
249 }
250 break;
251 case GL_BITMAP:
252 /* TODO */
253 break;
254 case GL_FLOAT:
255 {
256 GLfloat *src = (GLfloat *) pixels + i * width;
257 for (j=0;j<width;j++) {
258 stencil[j] = (GLubyte) (((GLint) *src++) & 0xff);
259 }
260 }
261 break;
262 default:
263 gl_error( ctx, GL_INVALID_ENUM, "Internal: draw_stencil_pixels" );
264 }
265
266 /* apply shift and offset */
267 if (ctx->Pixel.IndexOffset || ctx->Pixel.IndexShift) {
268 if (ctx->Pixel.IndexShift>=0) {
269 for (j=0;j<width;j++) {
270 stencil[j] = (stencil[j] << ctx->Pixel.IndexShift)
271 + ctx->Pixel.IndexOffset;
272 }
273 }
274 else {
275 for (j=0;j<width;j++) {
276 stencil[j] = (stencil[j] >> -ctx->Pixel.IndexShift)
277 + ctx->Pixel.IndexOffset;
278 }
279 }
280 }
281
282 /* mapping */
283 if (ctx->Pixel.MapStencilFlag) {
284 for (j=0;j<width;j++) {
285 stencil[j] = ctx->Pixel.MapStoS[ stencil[j] ];
286 }
287 }
288
289 /* write stencil values to stencil buffer */
290 if (zoom) {
291 gl_write_zoomed_stencil_span( ctx, (GLuint) width, x, y, stencil, desty );
292 }
293 else {
294 gl_write_stencil_span( ctx, (GLuint) width, x, y, stencil );
295 }
296 }
297 }
298
299
300
301 static void draw_depth_pixels( GLcontext* ctx, GLsizei width, GLsizei height,
302 GLenum type, const GLvoid *pixels )
303 {
304 GLint x, y, desty;
305 GLubyte red[MAX_WIDTH], green[MAX_WIDTH], blue[MAX_WIDTH], alpha[MAX_WIDTH];
306 GLuint ispan[MAX_WIDTH];
307 GLboolean bias_or_scale;
308 GLboolean zoom;
309
310 bias_or_scale = ctx->Pixel.DepthBias!=0.0 || ctx->Pixel.DepthScale!=1.0;
311 zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
312
313 /* Position, depth of pixels */
314 x = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
315 y = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
316 desty = y;
317
318 /* Color or index */
319 if (ctx->Visual->RGBAflag) {
320 GLint r, g, b, a;
321 r = (GLint) (ctx->Current.RasterColor[0] * ctx->Visual->RedScale);
322 g = (GLint) (ctx->Current.RasterColor[1] * ctx->Visual->GreenScale);
323 b = (GLint) (ctx->Current.RasterColor[2] * ctx->Visual->BlueScale);
324 a = (GLint) (ctx->Current.RasterColor[3] * ctx->Visual->AlphaScale);
325 MEMSET( red, r, width );
326 MEMSET( green, g, width );
327 MEMSET( blue, b, width );
328 MEMSET( alpha, a, width );
329 }
330 else {
331 GLuint i;
332 for (i=0;i<width;i++) {
333 ispan[i] = ctx->Current.RasterIndex;
334 }
335 }
336
337 if (type==GL_UNSIGNED_SHORT && sizeof(GLdepth)==sizeof(GLushort)
338 && !bias_or_scale && !zoom && ctx->Visual->RGBAflag) {
339 /* Special case: directly write 16-bit depth values */
340 GLuint j;
341 for (j=0;j<height;j++,y++) {
342 GLdepth *zptr = (GLdepth *) pixels + j * width;
343 gl_write_color_span( ctx, width, x, y, zptr,
344 red, green, blue, alpha, GL_BITMAP );
345 }
346 }
347 else if (type==GL_UNSIGNED_INT && sizeof(GLdepth)==sizeof(GLuint)
348 && !bias_or_scale && !zoom && ctx->Visual->RGBAflag) {
349 /* Special case: directly write 32-bit depth values */
350 GLuint i, j;
351 /* Compute shift value to scale 32-bit uints down to depth values. */
352 GLuint shift = 0;
353 GLuint max = MAX_DEPTH;
354 while ((max&0x80000000)==0) {
355 max = max << 1;
356 shift++;
357 }
358 for (j=0;j<height;j++,y++) {
359 GLdepth zspan[MAX_WIDTH];
360 GLuint *zptr = (GLuint *) pixels + j * width;
361 for (i=0;i<width;i++) {
362 zspan[i] = zptr[i] >> shift;
363 }
364 gl_write_color_span( ctx, width, x, y, zspan,
365 red, green, blue, alpha, GL_BITMAP );
366 }
367 }
368 else {
369 /* General case (slower) */
370 GLuint i, j;
371
372 /* process image row by row */
373 for (i=0;i<height;i++,y++) {
374 GLfloat depth[MAX_WIDTH];
375 GLdepth zspan[MAX_WIDTH];
376
377 switch (type) {
378 case GL_UNSIGNED_BYTE:
379 {
380 GLubyte *src = (GLubyte *) pixels + i * width;
381 for (j=0;j<width;j++) {
382 depth[j] = UBYTE_TO_FLOAT( *src++ );
383 }
384 }
385 break;
386 case GL_BYTE:
387 {
388 GLbyte *src = (GLbyte *) pixels + i * width;
389 for (j=0;j<width;j++) {
390 depth[j] = BYTE_TO_FLOAT( *src++ );
391 }
392 }
393 break;
394 case GL_UNSIGNED_SHORT:
395 {
396 GLushort *src = (GLushort *) pixels + i * width;
397 for (j=0;j<width;j++) {
398 depth[j] = USHORT_TO_FLOAT( *src++ );
399 }
400 }
401 break;
402 case GL_SHORT:
403 {
404 GLshort *src = (GLshort *) pixels + i * width;
405 for (j=0;j<width;j++) {
406 depth[j] = SHORT_TO_FLOAT( *src++ );
407 }
408 }
409 break;
410 case GL_UNSIGNED_INT:
411 {
412 GLuint *src = (GLuint *) pixels + i * width;
413 for (j=0;j<width;j++) {
414 depth[j] = UINT_TO_FLOAT( *src++ );
415 }
416 }
417 break;
418 case GL_INT:
419 {
420 GLint *src = (GLint *) pixels + i * width;
421 for (j=0;j<width;j++) {
422 depth[j] = INT_TO_FLOAT( *src++ );
423 }
424 }
425 break;
426 case GL_FLOAT:
427 {
428 GLfloat *src = (GLfloat *) pixels + i * width;
429 for (j=0;j<width;j++) {
430 depth[j] = *src++;
431 }
432 }
433 break;
434 default:
435 abort();
436 }
437
438 /* apply depth scale and bias */
439 if (ctx->Pixel.DepthScale!=1.0 || ctx->Pixel.DepthBias!=0.0) {
440 for (j=0;j<width;j++) {
441 depth[j] = depth[j] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
442 }
443 }
444
445 /* clamp depth values to [0,1] and convert from floats to integers */
446 for (j=0;j<width;j++) {
447 zspan[j] = (GLdepth) (CLAMP( depth[j], 0.0F, 1.0F ) * DEPTH_SCALE);
448 }
449
450 if (ctx->Visual->RGBAflag) {
451 if (zoom) {
452 gl_write_zoomed_color_span( ctx, width, x, y, zspan,
453 red, green, blue, alpha, desty );
454 }
455 else {
456 gl_write_color_span( ctx, width, x, y, zspan,
457 red, green, blue, alpha, GL_BITMAP );
458 }
459 }
460 else {
461 if (zoom) {
462 gl_write_zoomed_index_span( ctx, width, x, y, zspan,
463 ispan, GL_BITMAP );
464 }
465 else {
466 gl_write_index_span( ctx, width, x, y, zspan, ispan, GL_BITMAP );
467 }
468 }
469
470 }
471 }
472 }
473
474
475
476 static void draw_color_pixels( GLcontext* ctx,
477 GLsizei width, GLsizei height, GLenum format,
478 GLenum type, const GLvoid *pixels )
479 {
480 GLuint i, j;
481 GLint x, y, desty;
482 GLdepth zspan[MAX_WIDTH];
483 GLboolean scale_or_bias, quick_draw;
484 GLboolean zoom;
485
486 zoom = ctx->Pixel.ZoomX!=1.0 || ctx->Pixel.ZoomY!=1.0;
487
488 /* Position, depth of pixels */
489 x = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
490 y = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
491 desty = y;
492 if (ctx->Depth.Test) {
493 /* fill in array of z values */
494 GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
495 for (i=0;i<width;i++) {
496 zspan[i] = z;
497 }
498 }
499
500 /* Determine if scaling and/or biasing is needed */
501 if (ctx->Pixel.RedScale!=1.0F || ctx->Pixel.RedBias!=0.0F ||
502 ctx->Pixel.GreenScale!=1.0F || ctx->Pixel.GreenBias!=0.0F ||
503 ctx->Pixel.BlueScale!=1.0F || ctx->Pixel.BlueBias!=0.0F ||
504 ctx->Pixel.AlphaScale!=1.0F || ctx->Pixel.AlphaBias!=0.0F) {
505 scale_or_bias = GL_TRUE;
506 }
507 else {
508 scale_or_bias = GL_FALSE;
509 }
510
511 /* Determine if we can directly call the device driver function */
512 if (ctx->RasterMask==0 && !zoom && x>=0 && y>=0
513 && x+width<=ctx->Buffer->Width && y+height<=ctx->Buffer->Height) {
514 quick_draw = GL_TRUE;
515 }
516 else {
517 quick_draw = GL_FALSE;
518 }
519
520 /* First check for common cases */
521 if (type==GL_UNSIGNED_BYTE && (format==GL_RGB || format == GL_LUMINANCE)
522 && !ctx->Pixel.MapColorFlag && !scale_or_bias
523 && ctx->Visual->EightBitColor) {
524 DEFARRAY( GLubyte, alpha, MAX_WIDTH );
525 GLubyte *src = (GLubyte *) pixels;
526 /* constant alpha */
527 MEMSET( alpha, (GLint) ctx->Visual->AlphaScale, width );
528 if (format == GL_RGB) {
529 /* 8-bit RGB pixels */
530 DEFARRAY( GLubyte, red, MAX_WIDTH );
531 DEFARRAY( GLubyte, green, MAX_WIDTH );
532 DEFARRAY( GLubyte, blue, MAX_WIDTH );
533 for (i=0;i<height;i++,y++) {
534 for (j=0;j<width;j++) {
535 red[j] = *src++;
536 green[j] = *src++;
537 blue[j] = *src++;
538 }
539 if (quick_draw) {
540 (*ctx->Driver.WriteColorSpan)( ctx, width, x, y,
541 red, green, blue, alpha, NULL);
542 }
543 else if (zoom) {
544 gl_write_zoomed_color_span( ctx, (GLuint) width, x, y, zspan,
545 red, green, blue, alpha, desty );
546 }
547 else {
548 gl_write_color_span( ctx, (GLuint) width, x, y, zspan,
549 red, green, blue, alpha, GL_BITMAP );
550 }
551 }
552 UNDEFARRAY( red );
553 UNDEFARRAY( green );
554 UNDEFARRAY( blue );
555 }
556 else {
557 /* 8-bit Luminance pixels */
558 GLubyte *lum = (GLubyte *) pixels;
559 for (i=0;i<height;i++,y++,lum+=width) {
560 if (quick_draw) {
561 (*ctx->Driver.WriteColorSpan)( ctx, width, x,y,
562 lum, lum, lum, alpha, NULL);
563 }
564 else if (zoom) {
565 gl_write_zoomed_color_span( ctx, (GLuint) width, x, y, zspan,
566 lum, lum, lum, alpha, desty );
567 }
568 else {
569 gl_write_color_span( ctx, (GLuint) width, x, y, zspan,
570 lum, lum, lum, alpha, GL_BITMAP );
571 }
572 }
573 }
574 UNDEFARRAY( alpha );
575 }
576 else {
577 /* General solution */
578 GLboolean r_flag, g_flag, b_flag, a_flag, l_flag;
579 GLuint components;
580
581 r_flag = g_flag = b_flag = a_flag = l_flag = GL_FALSE;
582 switch (format) {
583 case GL_RED:
584 r_flag = GL_TRUE;
585 components = 1;
586 break;
587 case GL_GREEN:
588 g_flag = GL_TRUE;
589 components = 1;
590 break;
591 case GL_BLUE:
592 b_flag = GL_TRUE;
593 components = 1;
594 break;
595 case GL_ALPHA:
596 a_flag = GL_TRUE;
597 components = 1;
598 break;
599 case GL_RGB:
600 r_flag = g_flag = b_flag = GL_TRUE;
601 components = 3;
602 break;
603 case GL_LUMINANCE:
604 l_flag = GL_TRUE;
605 components = 1;
606 break;
607 case GL_LUMINANCE_ALPHA:
608 l_flag = a_flag = GL_TRUE;
609 components = 2;
610 break;
611 case GL_RGBA:
612 r_flag = g_flag = b_flag = a_flag = GL_TRUE;
613 components = 4;
614 break;
615 default:
616 abort();
617 }
618
619 /* process the image row by row */
620 for (i=0;i<height;i++,y++) {
621 DEFARRAY(GLfloat, rf, MAX_WIDTH);
622 DEFARRAY(GLfloat, gf, MAX_WIDTH);
623 DEFARRAY(GLfloat, bf, MAX_WIDTH);
624 DEFARRAY(GLfloat, af, MAX_WIDTH);
625 DEFARRAY(GLubyte, red, MAX_WIDTH);
626 DEFARRAY(GLubyte, green, MAX_WIDTH);
627 DEFARRAY(GLubyte, blue, MAX_WIDTH);
628 DEFARRAY(GLubyte, alpha, MAX_WIDTH);
629
630 /* convert to floats */
631 switch (type) {
632 case GL_UNSIGNED_BYTE:
633 {
634 GLubyte *src = (GLubyte *) pixels + i * width * components;
635 for (j=0;j<width;j++) {
636 if (l_flag) {
637 rf[j] = gf[j] = bf[j] = UBYTE_TO_FLOAT(*src++);
638 }
639 else {
640 rf[j] = r_flag ? UBYTE_TO_FLOAT(*src++) : 0.0;
641 gf[j] = g_flag ? UBYTE_TO_FLOAT(*src++) : 0.0;
642 bf[j] = b_flag ? UBYTE_TO_FLOAT(*src++) : 0.0;
643 }
644 af[j] = a_flag ? UBYTE_TO_FLOAT(*src++) : 1.0;
645 }
646 }
647 break;
648 case GL_BYTE:
649 {
650 GLbyte *src = (GLbyte *) pixels + i * width * components;
651 for (j=0;j<width;j++) {
652 if (l_flag) {
653 rf[j] = gf[j] = bf[j] = BYTE_TO_FLOAT(*src++);
654 }
655 else {
656 rf[j] = r_flag ? BYTE_TO_FLOAT(*src++) : 0.0;
657 gf[j] = g_flag ? BYTE_TO_FLOAT(*src++) : 0.0;
658 bf[j] = b_flag ? BYTE_TO_FLOAT(*src++) : 0.0;
659 }
660 af[j] = a_flag ? BYTE_TO_FLOAT(*src++) : 1.0;
661 }
662 }
663 break;
664 case GL_BITMAP:
665 /* special case */
666 break;
667 case GL_UNSIGNED_SHORT:
668 {
669 GLushort *src = (GLushort *) pixels + i * width * components;
670 for (j=0;j<width;j++) {
671 if (l_flag) {
672 rf[j] = gf[j] = bf[j] = USHORT_TO_FLOAT(*src++);
673 }
674 else {
675 rf[j] = r_flag ? USHORT_TO_FLOAT(*src++) : 0.0;
676 gf[j] = g_flag ? USHORT_TO_FLOAT(*src++) : 0.0;
677 bf[j] = b_flag ? USHORT_TO_FLOAT(*src++) : 0.0;
678 }
679 af[j] = a_flag ? USHORT_TO_FLOAT(*src++) : 1.0;
680 }
681 }
682 break;
683 case GL_SHORT:
684 {
685 GLshort *src = (GLshort *) pixels + i * width * components;
686 for (j=0;j<width;j++) {
687 if (l_flag) {
688 rf[j] = gf[j] = bf[j] = SHORT_TO_FLOAT(*src++);
689 }
690 else {
691 rf[j] = r_flag ? SHORT_TO_FLOAT(*src++) : 0.0;
692 gf[j] = g_flag ? SHORT_TO_FLOAT(*src++) : 0.0;
693 bf[j] = b_flag ? SHORT_TO_FLOAT(*src++) : 0.0;
694 }
695 af[j] = a_flag ? SHORT_TO_FLOAT(*src++) : 1.0;
696 }
697 }
698 break;
699 case GL_UNSIGNED_INT:
700 {
701 GLuint *src = (GLuint *) pixels + i * width * components;
702 for (j=0;j<width;j++) {
703 if (l_flag) {
704 rf[j] = gf[j] = bf[j] = UINT_TO_FLOAT(*src++);
705 }
706 else {
707 rf[j] = r_flag ? UINT_TO_FLOAT(*src++) : 0.0;
708 gf[j] = g_flag ? UINT_TO_FLOAT(*src++) : 0.0;
709 bf[j] = b_flag ? UINT_TO_FLOAT(*src++) : 0.0;
710 }
711 af[j] = a_flag ? af[j] = UINT_TO_FLOAT(*src++) : 1.0;
712 }
713 }
714 break;
715 case GL_INT:
716 {
717 GLint *src = (GLint *) pixels + i * width * components;
718 for (j=0;j<width;j++) {
719 if (l_flag) {
720 rf[j] = gf[j] = bf[j] = INT_TO_FLOAT(*src++);
721 }
722 else {
723 rf[j] = r_flag ? INT_TO_FLOAT(*src++) : 0.0;
724 gf[j] = g_flag ? INT_TO_FLOAT(*src++) : 0.0;
725 bf[j] = b_flag ? INT_TO_FLOAT(*src++) : 0.0;
726 }
727 af[j] = a_flag ? INT_TO_FLOAT(*src++) : 1.0;
728 }
729 }
730 break;
731 case GL_FLOAT:
732 {
733 GLfloat *src = (GLfloat *) pixels + i * width * components;
734 for (j=0;j<width;j++) {
735 if (l_flag) {
736 rf[j] = gf[j] = bf[j] = *src++;
737 }
738 else {
739 rf[j] = r_flag ? *src++ : 0.0;
740 gf[j] = g_flag ? *src++ : 0.0;
741 bf[j] = b_flag ? *src++ : 0.0;
742 }
743 af[j] = a_flag ? *src++ : 1.0;
744 }
745 }
746 break;
747 default:
748 gl_error( ctx, GL_INVALID_ENUM, "glDrawPixels" );
749 return;
750 }
751
752 /* apply scale and bias */
753 if (scale_or_bias) {
754 for (j=0;j<width;j++) {
755 GLfloat r, g, b, a;
756 r = rf[j] * ctx->Pixel.RedScale + ctx->Pixel.RedBias;
757 g = gf[j] * ctx->Pixel.GreenScale + ctx->Pixel.GreenBias;
758 b = bf[j] * ctx->Pixel.BlueScale + ctx->Pixel.BlueBias;
759 a = af[j] * ctx->Pixel.AlphaScale + ctx->Pixel.AlphaBias;
760 rf[j] = CLAMP( r, 0.0, 1.0 );
761 gf[j] = CLAMP( g, 0.0, 1.0 );
762 bf[j] = CLAMP( b, 0.0, 1.0 );
763 af[j] = CLAMP( a, 0.0, 1.0 );
764 }
765 }
766
767 /* apply pixel mappings */
768 if (ctx->Pixel.MapColorFlag) {
769 GLfloat rscale = ctx->Pixel.MapRtoRsize-1;
770 GLfloat gscale = ctx->Pixel.MapGtoGsize-1;
771 GLfloat bscale = ctx->Pixel.MapBtoBsize-1;
772 GLfloat ascale = ctx->Pixel.MapAtoAsize-1;
773 for (j=0;j<width;j++) {
774 rf[j] = ctx->Pixel.MapRtoR[ (GLint) (rf[j] * rscale) ];
775 gf[j] = ctx->Pixel.MapGtoG[ (GLint) (gf[j] * gscale) ];
776 bf[j] = ctx->Pixel.MapBtoB[ (GLint) (bf[j] * bscale) ];
777 af[j] = ctx->Pixel.MapAtoA[ (GLint) (af[j] * ascale) ];
778 }
779 }
780
781 /* convert to integers */
782 for (j=0;j<width;j++) {
783 red[j] = (GLint) (rf[j] * ctx->Visual->RedScale);
784 green[j] = (GLint) (gf[j] * ctx->Visual->GreenScale);
785 blue[j] = (GLint) (bf[j] * ctx->Visual->BlueScale);
786 alpha[j] = (GLint) (af[j] * ctx->Visual->AlphaScale);
787 }
788
789 /* write to frame buffer */
790 if (quick_draw) {
791 (*ctx->Driver.WriteColorSpan)( ctx, width, x, y,
792 red, green, blue, alpha, NULL);
793 }
794 else if (zoom) {
795 gl_write_zoomed_color_span( ctx, width, x, y, zspan,
796 red, green, blue, alpha, desty );
797 }
798 else {
799 gl_write_color_span( ctx, (GLuint) width, x, y, zspan,
800 red, green, blue, alpha, GL_BITMAP );
801 }
802
803 UNDEFARRAY(red);
804 UNDEFARRAY(green);
805 UNDEFARRAY(blue);
806 UNDEFARRAY(alpha);
807 }
808 }
809
810 }
811
812
813
814 /*
815 * Do a glDrawPixels( w, h, GL_RGB, GL_UNSIGNED_BYTE, pixels ) optimized
816 * for the case of no pixel mapping, no scale, no bias, no zoom, default
817 * storage mode, no raster ops, and no pixel clipping.
818 * Return: GL_TRUE if success
819 * GL_FALSE if conditions weren't met for optimized drawing
820 */
821 static GLboolean quickdraw_rgb( GLcontext* ctx, GLsizei width, GLsizei height,
822 const void *pixels )
823 {
824 DEFARRAY( GLubyte, red, MAX_WIDTH );
825 DEFARRAY( GLubyte, green, MAX_WIDTH );
826 DEFARRAY( GLubyte, blue, MAX_WIDTH );
827 DEFARRAY( GLubyte, alpha, MAX_WIDTH );
828 GLint i, j;
829 GLint x, y;
830 GLint bytes_per_row;
831 GLboolean result;
832
833 bytes_per_row = width * 3 + (width % ctx->Unpack.Alignment);
834
835 if (!ctx->Current.RasterPosValid) {
836 /* This is success, actually. */
837 result = GL_TRUE;
838 }
839 else {
840 x = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
841 y = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
842
843 if (x<0 || y<0
844 || x+width>ctx->Buffer->Width || y+height>ctx->Buffer->Height) {
845 result = GL_FALSE; /* can't handle this situation */
846 }
847 else {
848 /* constant alpha */
849 for (j=0;j<width;j++) {
850 alpha[j] = (GLint) ctx->Visual->AlphaScale;
851 }
852
853 /* write directly to device driver */
854 for (i=0;i<height;i++) {
855 /* each row of pixel data starts at 4-byte boundary */
856 GLubyte *src = (GLubyte *) pixels + i * bytes_per_row;
857 for (j=0;j<width;j++) {
858 red[j] = *src++;
859 green[j] = *src++;
860 blue[j] = *src++;
861 }
862 (*ctx->Driver.WriteColorSpan)( ctx, width, x, y+i,
863 red, green, blue, alpha, NULL);
864 }
865 result = GL_TRUE;
866 }
867 }
868
869 UNDEFARRAY( red );
870 UNDEFARRAY( green );
871 UNDEFARRAY( blue );
872 UNDEFARRAY( alpha );
873
874 return result;
875 }
876
877
878
879 /*
880 * Implements general glDrawPixels operation.
881 */
882 static void drawpixels( GLcontext* ctx, GLsizei width, GLsizei height,
883 GLenum format, GLenum type, const GLvoid *pixels )
884 {
885 if (INSIDE_BEGIN_END(ctx)) {
886 gl_error( ctx, GL_INVALID_OPERATION, "glDrawPixels" );
887 return;
888 }
889
890 if (ctx->NewState) {
891 gl_update_state(ctx);
892 }
893
894 if (ctx->RenderMode==GL_RENDER) {
895 if (!ctx->Current.RasterPosValid) {
896 return;
897 }
898 switch (format) {
899 case GL_COLOR_INDEX:
900 draw_index_pixels( ctx, width, height, type, pixels );
901 break;
902 case GL_STENCIL_INDEX:
903 draw_stencil_pixels( ctx, width, height, type, pixels );
904 break;
905 case GL_DEPTH_COMPONENT:
906 draw_depth_pixels( ctx, width, height, type, pixels );
907 break;
908 case GL_RED:
909 case GL_GREEN:
910 case GL_BLUE:
911 case GL_ALPHA:
912 case GL_RGB:
913 case GL_LUMINANCE:
914 case GL_LUMINANCE_ALPHA:
915 case GL_RGBA:
916 draw_color_pixels( ctx, width, height, format, type, pixels );
917 break;
918 default:
919 gl_error( ctx, GL_INVALID_ENUM, "glDrawPixels" );
920 }
921 }
922 else if (ctx->RenderMode==GL_FEEDBACK) {
923 GLfloat color[4], texcoord[4], invq;
924 color[0] = ctx->Current.IntColor[0] * ctx->Visual->InvRedScale;
925 color[1] = ctx->Current.IntColor[1] * ctx->Visual->InvGreenScale;
926 color[2] = ctx->Current.IntColor[2] * ctx->Visual->InvBlueScale;
927 color[3] = ctx->Current.IntColor[3] * ctx->Visual->InvAlphaScale;
928 invq = 1.0F / ctx->Current.TexCoord[3];
929 texcoord[0] = ctx->Current.TexCoord[0] * invq;
930 texcoord[1] = ctx->Current.TexCoord[1] * invq;
931 texcoord[2] = ctx->Current.TexCoord[2] * invq;
932 texcoord[3] = ctx->Current.TexCoord[3];
933 FEEDBACK_TOKEN( ctx, (GLfloat) GL_DRAW_PIXEL_TOKEN );
934 gl_feedback_vertex( ctx, ctx->Current.RasterPos[0],
935 ctx->Current.RasterPos[1],
936 ctx->Current.RasterPos[2],
937 ctx->Current.RasterPos[3],
938 color, ctx->Current.Index, texcoord );
939 }
940 else if (ctx->RenderMode==GL_SELECT) {
941 gl_update_hitflag( ctx, ctx->Current.RasterPos[2] );
942 }
943 }
944
945
946
947 /*
948 * Compile OR Execute a glDrawPixels!
949 */
950 void gl_DrawPixels( GLcontext* ctx, GLsizei width, GLsizei height,
951 GLenum format, GLenum type, const GLvoid *pixels )
952 {
953 GLvoid *image;
954
955 if (width<0 || height<0) {
956 gl_error( ctx, GL_INVALID_VALUE, "glDrawPixels" );
957 return;
958 }
959
960 /* Let the device driver take a crack at glDrawPixels */
961 if (!ctx->CompileFlag && ctx->Driver.DrawPixels) {
962 GLint x = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
963 GLint y = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
964 if ((*ctx->Driver.DrawPixels)( ctx, x, y, width, height,
965 format, type, GL_FALSE, pixels )) {
966 /* Device driver did the job */
967 return;
968 }
969 }
970
971 if (format==GL_RGB && type==GL_UNSIGNED_BYTE && ctx->FastDrawPixels
972 && !ctx->CompileFlag && ctx->RenderMode==GL_RENDER
973 && ctx->RasterMask==0) {
974 /* optimized path */
975 if (quickdraw_rgb( ctx, width, height, pixels )) {
976 /* success */
977 return;
978 }
979 }
980
981 /* take the general path */
982 image = gl_unpack_pixels( ctx, width, height, format, type, pixels );
983 if (!image) {
984 gl_error( ctx, GL_OUT_OF_MEMORY, "glDrawPixels" );
985 return;
986 }
987
988 if (ctx->CompileFlag) {
989 gl_save_DrawPixels( ctx, width, height, format, type, image );
990 }
991 if (ctx->ExecuteFlag) {
992 drawpixels( ctx, width, height, format, type, image );
993 if (!ctx->CompileFlag) {
994 /* may discard unpacked image now */
995 free( image );
996 }
997 }
998 }
999
1000