Debian Codegraph @

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

raw
1 /* $Id: pixel.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 /*
24 * glPixelStore, glPixelTransfer, glPixelMap, glPixelZoom, etc.
25 */
26
27
28
29 #include <assert.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "context.h"
34 #include "dlist.h"
35 #include "macros.h"
36 #include "pixel.h"
37 #include "image.h"
38 #include "span.h"
39 #include "stencil.h"
40 #include "types.h"
41
42
43
44 /*
45 * Determine if we can use the optimized glDrawPixels function.
46 */
47 static void update_drawpixels_state( GLcontext *ctx )
48 {
49 if (ctx->Visual->RGBAflag==GL_TRUE &&
50 ctx->Visual->EightBitColor &&
51 ctx->Pixel.RedBias==0.0 && ctx->Pixel.RedScale==1.0 &&
52 ctx->Pixel.GreenBias==0.0 && ctx->Pixel.GreenScale==1.0 &&
53 ctx->Pixel.BlueBias==0.0 && ctx->Pixel.BlueScale==1.0 &&
54 ctx->Pixel.AlphaBias==0.0 && ctx->Pixel.AlphaScale==1.0 &&
55 ctx->Pixel.MapColorFlag==GL_FALSE &&
56 ctx->Pixel.ZoomX==1.0 && ctx->Pixel.ZoomY==1.0 &&
57 /* ctx->Unpack.Alignment==4 &&*/
58 ctx->Unpack.RowLength==0 &&
59 ctx->Unpack.SkipPixels==0 &&
60 ctx->Unpack.SkipRows==0 &&
61 ctx->Unpack.SwapBytes==0 &&
62 ctx->Unpack.LsbFirst==0) {
63 ctx->FastDrawPixels = GL_TRUE;
64 }
65 else {
66 ctx->FastDrawPixels = GL_FALSE;
67 }
68 }
69
70
71
72
73 /**********************************************************************/
74 /***** glPixelZoom *****/
75 /**********************************************************************/
76
77
78
79 void gl_PixelZoom( GLcontext *ctx, GLfloat xfactor, GLfloat yfactor )
80 {
81 if (INSIDE_BEGIN_END(ctx)) {
82 gl_error( ctx, GL_INVALID_OPERATION, "glPixelZoom" );
83 return;
84 }
85 ctx->Pixel.ZoomX = xfactor;
86 ctx->Pixel.ZoomY = yfactor;
87 update_drawpixels_state( ctx );
88 }
89
90
91
92
93 /*
94 * Write a span of pixels to the frame buffer while applying a pixel zoom.
95 * This is only used by glDrawPixels and glCopyPixels.
96 * Input: n - number of pixels in input row
97 * x, y - destination of the span
98 * z - depth values for the span
99 * red, green, blue, alpha - array of colors
100 * y0 - location of first row in the image we're drawing.
101 */
102 void
103 gl_write_zoomed_color_span( GLcontext *ctx,
104 GLuint n, GLint x, GLint y, const GLdepth z[],
105 const GLubyte red[], const GLubyte green[],
106 const GLubyte blue[], const GLubyte alpha[],
107 GLint y0 )
108 {
109 GLint m;
110 GLint r0, r1, row, r;
111 GLint i, j, skipcol;
112 GLubyte zred[MAX_WIDTH], zgreen[MAX_WIDTH]; /* zoomed pixel colors */
113 GLubyte zblue[MAX_WIDTH], zalpha[MAX_WIDTH];
114 GLdepth zdepth[MAX_WIDTH]; /* zoomed depth values */
115 GLint maxwidth = MIN2( ctx->Buffer->Width, MAX_WIDTH );
116
117 /* compute width of output row */
118 m = (GLint) ABSF( n * ctx->Pixel.ZoomX );
119 if (m==0) {
120 return;
121 }
122 if (ctx->Pixel.ZoomX<0.0) {
123 /* adjust x coordinate for left/right mirroring */
124 x = x - m;
125 }
126
127 /* compute which rows to draw */
128 row = y-y0;
129 r0 = y0 + (GLint) (row * ctx->Pixel.ZoomY);
130 r1 = y0 + (GLint) ((row+1) * ctx->Pixel.ZoomY);
131 if (r0==r1) {
132 return;
133 }
134 else if (r1<r0) {
135 GLint rtmp = r1;
136 r1 = r0;
137 r0 = rtmp;
138 }
139
140 /* return early if r0...r1 is above or below window */
141 if (r0<0 && r1<0) {
142 /* below window */
143 return;
144 }
145 if (r0>=ctx->Buffer->Height && r1>=ctx->Buffer->Height) {
146 /* above window */
147 return;
148 }
149
150 /* check if left edge is outside window */
151 skipcol = 0;
152 if (x<0) {
153 skipcol = -x;
154 m += x;
155 }
156 /* make sure span isn't too long or short */
157 if (m>maxwidth) {
158 m = maxwidth;
159 }
160 else if (m<=0) {
161 return;
162 }
163
164 assert( m <= MAX_WIDTH );
165
166 /* zoom the span horizontally */
167 if (ctx->Pixel.ZoomX==-1.0F) {
168 /* n==m */
169 for (j=0;j<m;j++) {
170 i = n - (j+skipcol) - 1;
171 zred[j] = red[i];
172 zgreen[j] = green[i];
173 zblue[j] = blue[i];
174 zalpha[j] = alpha[i];
175 zdepth[j] = z[i];
176 }
177 }
178 else {
179 GLfloat xscale = 1.0F / ctx->Pixel.ZoomX;
180 for (j=0;j<m;j++) {
181 i = (j+skipcol) * xscale;
182 if (i<0) i = n + i - 1;
183 zred[j] = red[i];
184 zgreen[j] = green[i];
185 zblue[j] = blue[i];
186 zalpha[j] = alpha[i];
187 zdepth[j] = z[i];
188 }
189 }
190
191 /* write the span */
192 for (r=r0; r<r1; r++) {
193 gl_write_color_span( ctx, m, x+skipcol, r, zdepth,
194 zred, zgreen, zblue, zalpha, GL_BITMAP );
195 }
196 }
197
198
199
200 /*
201 * As above, but write CI pixels.
202 */
203 void
204 gl_write_zoomed_index_span( GLcontext *ctx,
205 GLuint n, GLint x, GLint y, const GLdepth z[],
206 const GLuint indexes[], GLint y0 )
207 {
208 GLint m;
209 GLint r0, r1, row, r;
210 GLint i, j, skipcol;
211 GLuint zindexes[MAX_WIDTH]; /* zoomed color indexes */
212 GLdepth zdepth[MAX_WIDTH]; /* zoomed depth values */
213 GLint maxwidth = MIN2( ctx->Buffer->Width, MAX_WIDTH );
214
215 /* compute width of output row */
216 m = (GLint) ABSF( n * ctx->Pixel.ZoomX );
217 if (m==0) {
218 return;
219 }
220 if (ctx->Pixel.ZoomX<0.0) {
221 /* adjust x coordinate for left/right mirroring */
222 x = x - m;
223 }
224
225 /* compute which rows to draw */
226 row = y-y0;
227 r0 = y0 + (GLint) (row * ctx->Pixel.ZoomY);
228 r1 = y0 + (GLint) ((row+1) * ctx->Pixel.ZoomY);
229 if (r0==r1) {
230 return;
231 }
232 else if (r1<r0) {
233 GLint rtmp = r1;
234 r1 = r0;
235 r0 = rtmp;
236 }
237
238 /* return early if r0...r1 is above or below window */
239 if (r0<0 && r1<0) {
240 /* below window */
241 return;
242 }
243 if (r0>=ctx->Buffer->Height && r1>=ctx->Buffer->Height) {
244 /* above window */
245 return;
246 }
247
248 /* check if left edge is outside window */
249 skipcol = 0;
250 if (x<0) {
251 skipcol = -x;
252 m += x;
253 }
254 /* make sure span isn't too long or short */
255 if (m>maxwidth) {
256 m = maxwidth;
257 }
258 else if (m<=0) {
259 return;
260 }
261
262 assert( m <= MAX_WIDTH );
263
264 /* zoom the span horizontally */
265 if (ctx->Pixel.ZoomX==-1.0F) {
266 /* n==m */
267 for (j=0;j<m;j++) {
268 i = n - (j+skipcol) - 1;
269 zindexes[j] = indexes[i];
270 zdepth[j] = z[i];
271 }
272 }
273 else {
274 GLfloat xscale = 1.0F / ctx->Pixel.ZoomX;
275 for (j=0;j<m;j++) {
276 i = (j+skipcol) * xscale;
277 if (i<0) i = n + i - 1;
278 zindexes[j] = indexes[i];
279 zdepth[j] = z[i];
280 }
281 }
282
283 /* write the span */
284 for (r=r0; r<r1; r++) {
285 gl_write_index_span( ctx, m, x+skipcol, r, zdepth, zindexes, GL_BITMAP );
286 }
287 }
288
289
290
291 /*
292 * As above, but write stencil values.
293 */
294 void
295 gl_write_zoomed_stencil_span( GLcontext *ctx,
296 GLuint n, GLint x, GLint y,
297 const GLubyte stencil[], GLint y0 )
298 {
299 GLint m;
300 GLint r0, r1, row, r;
301 GLint i, j, skipcol;
302 GLubyte zstencil[MAX_WIDTH]; /* zoomed stencil values */
303 GLint maxwidth = MIN2( ctx->Buffer->Width, MAX_WIDTH );
304
305 /* compute width of output row */
306 m = (GLint) ABSF( n * ctx->Pixel.ZoomX );
307 if (m==0) {
308 return;
309 }
310 if (ctx->Pixel.ZoomX<0.0) {
311 /* adjust x coordinate for left/right mirroring */
312 x = x - m;
313 }
314
315 /* compute which rows to draw */
316 row = y-y0;
317 r0 = y0 + (GLint) (row * ctx->Pixel.ZoomY);
318 r1 = y0 + (GLint) ((row+1) * ctx->Pixel.ZoomY);
319 if (r0==r1) {
320 return;
321 }
322 else if (r1<r0) {
323 GLint rtmp = r1;
324 r1 = r0;
325 r0 = rtmp;
326 }
327
328 /* return early if r0...r1 is above or below window */
329 if (r0<0 && r1<0) {
330 /* below window */
331 return;
332 }
333 if (r0>=ctx->Buffer->Height && r1>=ctx->Buffer->Height) {
334 /* above window */
335 return;
336 }
337
338 /* check if left edge is outside window */
339 skipcol = 0;
340 if (x<0) {
341 skipcol = -x;
342 m += x;
343 }
344 /* make sure span isn't too long or short */
345 if (m>maxwidth) {
346 m = maxwidth;
347 }
348 else if (m<=0) {
349 return;
350 }
351
352 assert( m <= MAX_WIDTH );
353
354 /* zoom the span horizontally */
355 if (ctx->Pixel.ZoomX==-1.0F) {
356 /* n==m */
357 for (j=0;j<m;j++) {
358 i = n - (j+skipcol) - 1;
359 zstencil[j] = stencil[i];
360 }
361 }
362 else {
363 GLfloat xscale = 1.0F / ctx->Pixel.ZoomX;
364 for (j=0;j<m;j++) {
365 i = (j+skipcol) * xscale;
366 if (i<0) i = n + i - 1;
367 zstencil[j] = stencil[i];
368 }
369 }
370
371 /* write the span */
372 for (r=r0; r<r1; r++) {
373 gl_write_stencil_span( ctx, m, x+skipcol, r, zstencil );
374 }
375 }
376
377
378
379
380 /**********************************************************************/
381 /***** glPixelStore *****/
382 /**********************************************************************/
383
384
385 void gl_PixelStorei( GLcontext *ctx, GLenum pname, GLint param )
386 {
387 /* NOTE: this call can't be compiled into the display list */
388
389 if (INSIDE_BEGIN_END(ctx)) {
390 gl_error( ctx, GL_INVALID_OPERATION, "glPixelStore" );
391 return;
392 }
393
394 switch (pname) {
395 case GL_PACK_SWAP_BYTES:
396 ctx->Pack.SwapBytes = param ? GL_TRUE : GL_FALSE;
397 break;
398 case GL_PACK_LSB_FIRST:
399 ctx->Pack.LsbFirst = param ? GL_TRUE : GL_FALSE;
400 break;
401 case GL_PACK_ROW_LENGTH:
402 if (param<0) {
403 gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
404 }
405 else {
406 ctx->Pack.RowLength = param;
407 }
408 break;
409 case GL_PACK_SKIP_PIXELS:
410 if (param<0) {
411 gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
412 }
413 else {
414 ctx->Pack.SkipPixels = param;
415 }
416 break;
417 case GL_PACK_SKIP_ROWS:
418 if (param<0) {
419 gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
420 }
421 else {
422 ctx->Pack.SkipRows = param;
423 }
424 break;
425 case GL_PACK_ALIGNMENT:
426 if (param==1 || param==2 || param==4 || param==8) {
427 ctx->Pack.Alignment = param;
428 }
429 else {
430 gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
431 }
432 break;
433 case GL_UNPACK_SWAP_BYTES:
434 ctx->Unpack.SwapBytes = param ? GL_TRUE : GL_FALSE;
435 break;
436 case GL_UNPACK_LSB_FIRST:
437 ctx->Unpack.LsbFirst = param ? GL_TRUE : GL_FALSE;
438 break;
439 case GL_UNPACK_ROW_LENGTH:
440 if (param<0) {
441 gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
442 }
443 else {
444 ctx->Unpack.RowLength = param;
445 }
446 break;
447 case GL_UNPACK_SKIP_PIXELS:
448 if (param<0) {
449 gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
450 }
451 else {
452 ctx->Unpack.SkipPixels = param;
453 }
454 break;
455 case GL_UNPACK_SKIP_ROWS:
456 if (param<0) {
457 gl_error( ctx, GL_INVALID_VALUE, "glPixelStore(param)" );
458 }
459 else {
460 ctx->Unpack.SkipRows = param;
461 }
462 break;
463 case GL_UNPACK_ALIGNMENT:
464 if (param==1 || param==2 || param==4 || param==8) {
465 ctx->Unpack.Alignment = param;
466 }
467 else {
468 gl_error( ctx, GL_INVALID_VALUE, "glPixelStore" );
469 }
470 break;
471 default:
472 gl_error( ctx, GL_INVALID_ENUM, "glPixelStore" );
473 }
474 update_drawpixels_state( ctx );
475 }
476
477
478
479
480
481 /**********************************************************************/
482 /***** glPixelMap *****/
483 /**********************************************************************/
484
485
486
487 void gl_PixelMapfv( GLcontext *ctx,
488 GLenum map, GLint mapsize, const GLfloat *values )
489 {
490 GLuint i;
491
492 if (INSIDE_BEGIN_END(ctx)) {
493 gl_error( ctx, GL_INVALID_OPERATION, "glPixelMapfv" );
494 return;
495 }
496
497 if (mapsize<0 || mapsize>MAX_PIXEL_MAP_TABLE) {
498 gl_error( ctx, GL_INVALID_VALUE, "glPixelMapfv(mapsize)" );
499 return;
500 }
501
502 if (map>=GL_PIXEL_MAP_S_TO_S && map<=GL_PIXEL_MAP_I_TO_A) {
503 /* test that mapsize is a power of two */
504 GLuint p;
505 GLboolean ok = GL_FALSE;
506 for (p=1; p<=MAX_PIXEL_MAP_TABLE; p=p<<1) {
507 if ( (p&mapsize) == p ) {
508 ok = GL_TRUE;
509 break;
510 }
511 }
512 if (!ok) {
513 gl_error( ctx, GL_INVALID_VALUE, "glPixelMapfv(mapsize)" );
514 return;
515 }
516 }
517
518 switch (map) {
519 case GL_PIXEL_MAP_S_TO_S:
520 ctx->Pixel.MapStoSsize = mapsize;
521 for (i=0;i<mapsize;i++) {
522 ctx->Pixel.MapStoS[i] = (GLint) values[i];
523 }
524 break;
525 case GL_PIXEL_MAP_I_TO_I:
526 ctx->Pixel.MapItoIsize = mapsize;
527 for (i=0;i<mapsize;i++) {
528 ctx->Pixel.MapItoI[i] = (GLint) values[i];
529 }
530 break;
531 case GL_PIXEL_MAP_I_TO_R:
532 ctx->Pixel.MapItoRsize = mapsize;
533 for (i=0;i<mapsize;i++) {
534 ctx->Pixel.MapItoR[i] = CLAMP( values[i], 0.0, 1.0 );
535 }
536 break;
537 case GL_PIXEL_MAP_I_TO_G:
538 ctx->Pixel.MapItoGsize = mapsize;
539 for (i=0;i<mapsize;i++) {
540 ctx->Pixel.MapItoG[i] = CLAMP( values[i], 0.0, 1.0 );
541 }
542 break;
543 case GL_PIXEL_MAP_I_TO_B:
544 ctx->Pixel.MapItoBsize = mapsize;
545 for (i=0;i<mapsize;i++) {
546 ctx->Pixel.MapItoB[i] = CLAMP( values[i], 0.0, 1.0 );
547 }
548 break;
549 case GL_PIXEL_MAP_I_TO_A:
550 ctx->Pixel.MapItoAsize = mapsize;
551 for (i=0;i<mapsize;i++) {
552 ctx->Pixel.MapItoA[i] = CLAMP( values[i], 0.0, 1.0 );
553 }
554 break;
555 case GL_PIXEL_MAP_R_TO_R:
556 ctx->Pixel.MapRtoRsize = mapsize;
557 for (i=0;i<mapsize;i++) {
558 ctx->Pixel.MapRtoR[i] = CLAMP( values[i], 0.0, 1.0 );
559 }
560 break;
561 case GL_PIXEL_MAP_G_TO_G:
562 ctx->Pixel.MapGtoGsize = mapsize;
563 for (i=0;i<mapsize;i++) {
564 ctx->Pixel.MapGtoG[i] = CLAMP( values[i], 0.0, 1.0 );
565 }
566 break;
567 case GL_PIXEL_MAP_B_TO_B:
568 ctx->Pixel.MapBtoBsize = mapsize;
569 for (i=0;i<mapsize;i++) {
570 ctx->Pixel.MapBtoB[i] = CLAMP( values[i], 0.0, 1.0 );
571 }
572 break;
573 case GL_PIXEL_MAP_A_TO_A:
574 ctx->Pixel.MapAtoAsize = mapsize;
575 for (i=0;i<mapsize;i++) {
576 ctx->Pixel.MapAtoA[i] = CLAMP( values[i], 0.0, 1.0 );
577 }
578 break;
579 default:
580 gl_error( ctx, GL_INVALID_ENUM, "glPixelMapfv(map)" );
581 }
582 }
583
584
585
586
587
588 void gl_GetPixelMapfv( GLcontext *ctx, GLenum map, GLfloat *values )
589 {
590 GLuint i;
591
592 if (INSIDE_BEGIN_END(ctx)) {
593 gl_error( ctx, GL_INVALID_OPERATION, "glGetPixelMapfv" );
594 return;
595 }
596 switch (map) {
597 case GL_PIXEL_MAP_I_TO_I:
598 for (i=0;i<ctx->Pixel.MapItoIsize;i++) {
599 values[i] = (GLfloat) ctx->Pixel.MapItoI[i];
600 }
601 break;
602 case GL_PIXEL_MAP_S_TO_S:
603 for (i=0;i<ctx->Pixel.MapStoSsize;i++) {
604 values[i] = (GLfloat) ctx->Pixel.MapStoS[i];
605 }
606 break;
607 case GL_PIXEL_MAP_I_TO_R:
608 MEMCPY(values,ctx->Pixel.MapItoR,ctx->Pixel.MapItoRsize*sizeof(GLfloat));
609 break;
610 case GL_PIXEL_MAP_I_TO_G:
611 MEMCPY(values,ctx->Pixel.MapItoG,ctx->Pixel.MapItoGsize*sizeof(GLfloat));
612 break;
613 case GL_PIXEL_MAP_I_TO_B:
614 MEMCPY(values,ctx->Pixel.MapItoB,ctx->Pixel.MapItoBsize*sizeof(GLfloat));
615 break;
616 case GL_PIXEL_MAP_I_TO_A:
617 MEMCPY(values,ctx->Pixel.MapItoA,ctx->Pixel.MapItoAsize*sizeof(GLfloat));
618 break;
619 case GL_PIXEL_MAP_R_TO_R:
620 MEMCPY(values,ctx->Pixel.MapRtoR,ctx->Pixel.MapRtoRsize*sizeof(GLfloat));
621 break;
622 case GL_PIXEL_MAP_G_TO_G:
623 MEMCPY(values,ctx->Pixel.MapGtoG,ctx->Pixel.MapGtoGsize*sizeof(GLfloat));
624 break;
625 case GL_PIXEL_MAP_B_TO_B:
626 MEMCPY(values,ctx->Pixel.MapBtoB,ctx->Pixel.MapBtoBsize*sizeof(GLfloat));
627 break;
628 case GL_PIXEL_MAP_A_TO_A:
629 MEMCPY(values,ctx->Pixel.MapAtoA,ctx->Pixel.MapAtoAsize*sizeof(GLfloat));
630 break;
631 default:
632 gl_error( ctx, GL_INVALID_ENUM, "glGetPixelMapfv" );
633 }
634 }
635
636
637 void gl_GetPixelMapuiv( GLcontext *ctx, GLenum map, GLuint *values )
638 {
639 GLuint i;
640
641 if (INSIDE_BEGIN_END(ctx)) {
642 gl_error( ctx, GL_INVALID_OPERATION, "glGetPixelMapfv" );
643 return;
644 }
645 switch (map) {
646 case GL_PIXEL_MAP_I_TO_I:
647 MEMCPY(values, ctx->Pixel.MapItoI, ctx->Pixel.MapItoIsize*sizeof(GLint));
648 break;
649 case GL_PIXEL_MAP_S_TO_S:
650 MEMCPY(values, ctx->Pixel.MapStoS, ctx->Pixel.MapStoSsize*sizeof(GLint));
651 break;
652 case GL_PIXEL_MAP_I_TO_R:
653 for (i=0;i<ctx->Pixel.MapItoRsize;i++) {
654 values[i] = FLOAT_TO_UINT( ctx->Pixel.MapItoR[i] );
655 }
656 break;
657 case GL_PIXEL_MAP_I_TO_G:
658 for (i=0;i<ctx->Pixel.MapItoGsize;i++) {
659 values[i] = FLOAT_TO_UINT( ctx->Pixel.MapItoG[i] );
660 }
661 break;
662 case GL_PIXEL_MAP_I_TO_B:
663 for (i=0;i<ctx->Pixel.MapItoBsize;i++) {
664 values[i] = FLOAT_TO_UINT( ctx->Pixel.MapItoB[i] );
665 }
666 break;
667 case GL_PIXEL_MAP_I_TO_A:
668 for (i=0;i<ctx->Pixel.MapItoAsize;i++) {
669 values[i] = FLOAT_TO_UINT( ctx->Pixel.MapItoA[i] );
670 }
671 break;
672 case GL_PIXEL_MAP_R_TO_R:
673 for (i=0;i<ctx->Pixel.MapRtoRsize;i++) {
674 values[i] = FLOAT_TO_UINT( ctx->Pixel.MapRtoR[i] );
675 }
676 break;
677 case GL_PIXEL_MAP_G_TO_G:
678 for (i=0;i<ctx->Pixel.MapGtoGsize;i++) {
679 values[i] = FLOAT_TO_UINT( ctx->Pixel.MapGtoG[i] );
680 }
681 break;
682 case GL_PIXEL_MAP_B_TO_B:
683 for (i=0;i<ctx->Pixel.MapBtoBsize;i++) {
684 values[i] = FLOAT_TO_UINT( ctx->Pixel.MapBtoB[i] );
685 }
686 break;
687 case GL_PIXEL_MAP_A_TO_A:
688 for (i=0;i<ctx->Pixel.MapAtoAsize;i++) {
689 values[i] = FLOAT_TO_UINT( ctx->Pixel.MapAtoA[i] );
690 }
691 break;
692 default:
693 gl_error( ctx, GL_INVALID_ENUM, "glGetPixelMapfv" );
694 }
695 }
696
697
698 void gl_GetPixelMapusv( GLcontext *ctx, GLenum map, GLushort *values )
699 {
700 GLuint i;
701
702 if (INSIDE_BEGIN_END(ctx)) {
703 gl_error( ctx, GL_INVALID_OPERATION, "glGetPixelMapfv" );
704 return;
705 }
706 switch (map) {
707 case GL_PIXEL_MAP_I_TO_I:
708 for (i=0;i<ctx->Pixel.MapItoIsize;i++) {
709 values[i] = (GLushort) ctx->Pixel.MapItoI[i];
710 }
711 break;
712 case GL_PIXEL_MAP_S_TO_S:
713 for (i=0;i<ctx->Pixel.MapStoSsize;i++) {
714 values[i] = (GLushort) ctx->Pixel.MapStoS[i];
715 }
716 break;
717 case GL_PIXEL_MAP_I_TO_R:
718 for (i=0;i<ctx->Pixel.MapItoRsize;i++) {
719 values[i] = FLOAT_TO_USHORT( ctx->Pixel.MapItoR[i] );
720 }
721 break;
722 case GL_PIXEL_MAP_I_TO_G:
723 for (i=0;i<ctx->Pixel.MapItoGsize;i++) {
724 values[i] = FLOAT_TO_USHORT( ctx->Pixel.MapItoG[i] );
725 }
726 break;
727 case GL_PIXEL_MAP_I_TO_B:
728 for (i=0;i<ctx->Pixel.MapItoBsize;i++) {
729 values[i] = FLOAT_TO_USHORT( ctx->Pixel.MapItoB[i] );
730 }
731 break;
732 case GL_PIXEL_MAP_I_TO_A:
733 for (i=0;i<ctx->Pixel.MapItoAsize;i++) {
734 values[i] = FLOAT_TO_USHORT( ctx->Pixel.MapItoA[i] );
735 }
736 break;
737 case GL_PIXEL_MAP_R_TO_R:
738 for (i=0;i<ctx->Pixel.MapRtoRsize;i++) {
739 values[i] = FLOAT_TO_USHORT( ctx->Pixel.MapRtoR[i] );
740 }
741 break;
742 case GL_PIXEL_MAP_G_TO_G:
743 for (i=0;i<ctx->Pixel.MapGtoGsize;i++) {
744 values[i] = FLOAT_TO_USHORT( ctx->Pixel.MapGtoG[i] );
745 }
746 break;
747 case GL_PIXEL_MAP_B_TO_B:
748 for (i=0;i<ctx->Pixel.MapBtoBsize;i++) {
749 values[i] = FLOAT_TO_USHORT( ctx->Pixel.MapBtoB[i] );
750 }
751 break;
752 case GL_PIXEL_MAP_A_TO_A:
753 for (i=0;i<ctx->Pixel.MapAtoAsize;i++) {
754 values[i] = FLOAT_TO_USHORT( ctx->Pixel.MapAtoA[i] );
755 }
756 break;
757 default:
758 gl_error( ctx, GL_INVALID_ENUM, "glGetPixelMapfv" );
759 }
760 }
761
762
763
764 /**********************************************************************/
765 /***** glPixelTransfer *****/
766 /**********************************************************************/
767
768
769 /*
770 * Implements glPixelTransfer[fi] whether called immediately or from a
771 * display list.
772 */
773 void gl_PixelTransferf( GLcontext *ctx, GLenum pname, GLfloat param )
774 {
775 if (INSIDE_BEGIN_END(ctx)) {
776 gl_error( ctx, GL_INVALID_OPERATION, "glPixelTransfer" );
777 return;
778 }
779
780 switch (pname) {
781 case GL_MAP_COLOR:
782 ctx->Pixel.MapColorFlag = param ? GL_TRUE : GL_FALSE;
783 break;
784 case GL_MAP_STENCIL:
785 ctx->Pixel.MapStencilFlag = param ? GL_TRUE : GL_FALSE;
786 break;
787 case GL_INDEX_SHIFT:
788 ctx->Pixel.IndexShift = (GLint) param;
789 break;
790 case GL_INDEX_OFFSET:
791 ctx->Pixel.IndexOffset = (GLint) param;
792 break;
793 case GL_RED_SCALE:
794 ctx->Pixel.RedScale = param;
795 break;
796 case GL_RED_BIAS:
797 ctx->Pixel.RedBias = param;
798 break;
799 case GL_GREEN_SCALE:
800 ctx->Pixel.GreenScale = param;
801 break;
802 case GL_GREEN_BIAS:
803 ctx->Pixel.GreenBias = param;
804 break;
805 case GL_BLUE_SCALE:
806 ctx->Pixel.BlueScale = param;
807 break;
808 case GL_BLUE_BIAS:
809 ctx->Pixel.BlueBias = param;
810 break;
811 case GL_ALPHA_SCALE:
812 ctx->Pixel.AlphaScale = param;
813 break;
814 case GL_ALPHA_BIAS:
815 ctx->Pixel.AlphaBias = param;
816 break;
817 case GL_DEPTH_SCALE:
818 ctx->Pixel.DepthScale = param;
819 break;
820 case GL_DEPTH_BIAS:
821 ctx->Pixel.DepthBias = param;
822 break;
823 default:
824 gl_error( ctx, GL_INVALID_ENUM, "glPixelTransfer(pname)" );
825 return;
826 }
827 update_drawpixels_state( ctx );
828 }
829
830
831
832
833
834 /**********************************************************************/
835 /***** Pixel packing/unpacking *****/
836 /**********************************************************************/
837
838
839
840 /*
841 * Unpack a 2-D pixel array/image. The unpacked format will be con-
842 * tiguous (no "empty" bytes) with byte/bit swapping applied as needed.
843 * Input: same as glDrawPixels
844 * Output: pointer to block of pixel data in same format and type as input
845 * or NULL if error.
846 */
847 GLvoid *gl_unpack_pixels( GLcontext *ctx,
848 GLsizei width, GLsizei height,\
849 GLenum format, GLenum type,
850 const GLvoid *pixels )
851 {
852 GLint s, n;
853
854 s = gl_sizeof_type( type );
855 if (s<0) {
856 gl_error( ctx, GL_INVALID_ENUM, "internal error in gl_unpack(type)" );
857 return NULL;
858 }
859
860 n = gl_components_in_format( format );
861 if (n<0) {
862 gl_error( ctx, GL_INVALID_ENUM, "gl_unpack_pixels(format)" );
863 return NULL;
864 }
865
866 if (type==GL_BITMAP) {
867 /* BITMAP data */
868 GLint bytes, i, width_in_bytes;
869 GLubyte *buffer, *dst;
870 GLvoid *src;
871
872 /* Alloc dest storage */
873 bytes = CEILING( width * height , 8 );
874 buffer = (GLubyte *) malloc( bytes );
875 if (!buffer) {
876 return NULL;
877 }
878
879 /* Copy/unpack pixel data to buffer */
880 width_in_bytes = CEILING( width, 8 );
881 dst = buffer;
882 for (i=0;i<height;i++) {
883 src = gl_pixel_addr_in_image( &ctx->Unpack, pixels, width, height,
884 format, type, 0, i, 0 );
885 if (!src) {
886 free(buffer);
887 return NULL;
888 }
889 MEMCPY( dst, src, width_in_bytes );
890 dst += width_in_bytes;
891 }
892
893 /* Bit flipping */
894 if (ctx->Unpack.LsbFirst) {
895 gl_flip_bytes( buffer, bytes );
896 }
897 return (GLvoid *) buffer;
898 }
899 else {
900 /* Non-BITMAP data */
901 GLint width_in_bytes, bytes, i;
902 GLubyte *buffer, *dst;
903 GLvoid *src;
904
905 width_in_bytes = width * n * s;
906
907 /* Alloc dest storage */
908 bytes = height * width_in_bytes;
909 buffer = (GLubyte *) malloc( bytes );
910 if (!buffer) {
911 return NULL;
912 }
913
914 /* Copy/unpack pixel data to buffer */
915 dst = buffer;
916 for (i=0;i<height;i++) {
917 src = gl_pixel_addr_in_image( &ctx->Unpack, pixels, width, height,
918 format, type, 0, i, 0 );
919 if (!src) {
920 free(buffer);
921 return NULL;
922 }
923 MEMCPY( dst, src, width_in_bytes );
924 dst += width_in_bytes;
925 }
926
927 /* Byte swapping */
928 if (ctx->Unpack.SwapBytes && s>1) {
929 if (s==2) {
930 gl_swap2( (GLushort *) buffer, bytes/2 );
931 }
932 else if (s==4) {
933 gl_swap4( (GLuint *) buffer, bytes/4 );
934 }
935 }
936 return (GLvoid *) buffer;
937 }
938 }
939
940
941
942
943 /*
944 if (s>=a) {
945 k = n * l;
946 }
947 else { *s<a*
948 k = (a/s) * ceil( s*n*l / a );
949 }
950
951 s = size in bytes of a single component
952 a = alignment
953 n = number of components in a pixel
954 l = number of pixels in a row
955
956 k = number of components or indices between first pixel in each row in mem.
957 */
958