Debian Codegraph @

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

raw
1 /* $Id: copypix.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.0
6 * Copyright (C) 1995-1996 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 <string.h>
24 #include "context.h"
25 #include "copypix.h"
26 #include "depth.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 static void copy_rgb_pixels( GLcontext* ctx,
39 GLint srcx, GLint srcy, GLint width, GLint height,
40 GLint destx, GLint desty )
41 {
42 DEFARRAY( GLdepth, zspan, MAX_WIDTH );
43 DEFARRAY( GLubyte, red, MAX_WIDTH );
44 DEFARRAY( GLubyte, green, MAX_WIDTH );
45 DEFARRAY( GLubyte, blue, MAX_WIDTH );
46 DEFARRAY( GLubyte, alpha, MAX_WIDTH );
47 GLboolean scale_or_bias, quick_draw, zoom;
48 GLint sy, dy, stepy;
49 GLint i, j;
50 GLboolean setbuffer;
51
52 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
53 zoom = GL_FALSE;
54 }
55 else {
56 zoom = GL_TRUE;
57 }
58
59 /* Determine if copy should be done bottom-to-top or top-to-bottom */
60 if (srcy<desty) {
61 /* top-down max-to-min */
62 sy = srcy + height - 1;
63 dy = desty + height - 1;
64 stepy = -1;
65 }
66 else {
67 /* bottom-up min-to-max */
68 sy = srcy;
69 dy = desty;
70 stepy = 1;
71 }
72
73 scale_or_bias = ctx->Pixel.RedScale!=1.0 || ctx->Pixel.RedBias!=0.0
74 || ctx->Pixel.GreenScale!=1.0 || ctx->Pixel.GreenBias!=0.0
75 || ctx->Pixel.BlueScale!=1.0 || ctx->Pixel.BlueBias!=0.0
76 || ctx->Pixel.AlphaScale!=1.0 || ctx->Pixel.AlphaBias!=0.0;
77
78 if (ctx->Depth.Test) {
79 /* fill in array of z values */
80 GLint z = (GLint) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
81 for (i=0;i<width;i++) {
82 zspan[i] = z;
83 }
84 }
85
86 if (ctx->RasterMask==0 && !zoom
87 && destx>=0 && destx+width<=ctx->Buffer->Width) {
88 quick_draw = GL_TRUE;
89 }
90 else {
91 quick_draw = GL_FALSE;
92 }
93
94 /* If read and draw buffer are different we must do buffer switching */
95 setbuffer = ctx->Pixel.ReadBuffer!=ctx->Color.DrawBuffer;
96
97 for (j=0; j<height; j++, sy+=stepy, dy+=stepy) {
98 /* read */
99
100 if (setbuffer) {
101 (*ctx->Driver.SetBuffer)( ctx, ctx->Pixel.ReadBuffer );
102 }
103 gl_read_color_span( ctx, width, srcx, sy, red, green, blue, alpha );
104
105 if (scale_or_bias) {
106 GLfloat rbias = ctx->Pixel.RedBias * ctx->Visual->RedScale;
107 GLfloat gbias = ctx->Pixel.GreenBias * ctx->Visual->GreenScale;
108 GLfloat bbias = ctx->Pixel.BlueBias * ctx->Visual->BlueScale;
109 GLfloat abias = ctx->Pixel.AlphaBias * ctx->Visual->AlphaScale;
110 GLint rmax = (GLint) ctx->Visual->RedScale;
111 GLint gmax = (GLint) ctx->Visual->GreenScale;
112 GLint bmax = (GLint) ctx->Visual->BlueScale;
113 GLint amax = (GLint) ctx->Visual->AlphaScale;
114 for (i=0;i<width;i++) {
115 GLint r = red[i] * ctx->Pixel.RedScale + rbias;
116 GLint g = green[i] * ctx->Pixel.GreenScale + gbias;
117 GLint b = blue[i] * ctx->Pixel.BlueScale + bbias;
118 GLint a = alpha[i] * ctx->Pixel.AlphaScale + abias;
119 red[i] = CLAMP( r, 0, rmax );
120 green[i] = CLAMP( g, 0, gmax );
121 blue[i] = CLAMP( b, 0, bmax );
122 alpha[i] = CLAMP( a, 0, amax );
123 }
124 }
125
126 if (ctx->Pixel.MapColorFlag) {
127 GLfloat r = (ctx->Pixel.MapRtoRsize-1) * ctx->Visual->InvRedScale;
128 GLfloat g = (ctx->Pixel.MapGtoGsize-1) * ctx->Visual->InvGreenScale;
129 GLfloat b = (ctx->Pixel.MapBtoBsize-1) * ctx->Visual->InvBlueScale;
130 GLfloat a = (ctx->Pixel.MapAtoAsize-1) * ctx->Visual->InvAlphaScale;
131 for (i=0;i<width;i++) {
132 GLint ir = red[i] * r;
133 GLint ig = green[i] * g;
134 GLint ib = blue[i] * b;
135 GLint ia = alpha[i] * a;
136 red[i] = (GLint) (ctx->Pixel.MapRtoR[ir]*ctx->Visual->RedScale);
137 green[i] = (GLint) (ctx->Pixel.MapGtoG[ig]*ctx->Visual->GreenScale);
138 blue[i] = (GLint) (ctx->Pixel.MapBtoB[ib]*ctx->Visual->BlueScale);
139 alpha[i] = (GLint) (ctx->Pixel.MapAtoA[ia]*ctx->Visual->AlphaScale);
140 }
141 }
142
143 /* write */
144 if (setbuffer) {
145 (*ctx->Driver.SetBuffer)( ctx, ctx->Color.DrawBuffer );
146 }
147 if (quick_draw && dy>=0 && dy<ctx->Buffer->Height) {
148 (*ctx->Driver.WriteColorSpan)( ctx, width, destx, dy,
149 red, green, blue, alpha, NULL);
150 }
151 else if (zoom) {
152 gl_write_zoomed_color_span( ctx, width, destx, dy, zspan,
153 red, green, blue, alpha, desty );
154 }
155 else {
156 gl_write_color_span( ctx, width, destx, dy, zspan,
157 red, green, blue, alpha, GL_BITMAP );
158 }
159 }
160 UNDEFARRAY( zspan );
161 UNDEFARRAY( red );
162 UNDEFARRAY( green );
163 UNDEFARRAY( blue );
164 UNDEFARRAY( alpha );
165 }
166
167
168
169 static void copy_ci_pixels( GLcontext* ctx,
170 GLint srcx, GLint srcy, GLint width, GLint height,
171 GLint destx, GLint desty )
172 {
173 GLdepth zspan[MAX_WIDTH];
174 GLuint indx[MAX_WIDTH];
175 GLint sy, dy, stepy;
176 GLint i, j;
177 GLboolean setbuffer, zoom;
178
179 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
180 zoom = GL_FALSE;
181 }
182 else {
183 zoom = GL_TRUE;
184 }
185
186 /* Determine if copy should be bottom-to-top or top-to-bottom */
187 if (srcy<desty) {
188 /* top-down max-to-min */
189 sy = srcy + height - 1;
190 dy = desty + height - 1;
191 stepy = -1;
192 }
193 else {
194 /* bottom-up min-to-max */
195 sy = srcy;
196 dy = desty;
197 stepy = 1;
198 }
199
200 if (ctx->Depth.Test) {
201 /* fill in array of z values */
202 GLint z = (GLint) (ctx->Current.RasterPos[2] * DEPTH_SCALE);
203 for (i=0;i<width;i++) {
204 zspan[i] = z;
205 }
206 }
207
208 /* If read and draw buffer are different we must do buffer switching */
209 setbuffer = ctx->Pixel.ReadBuffer!=ctx->Color.DrawBuffer;
210
211 for (j=0; j<height; j++, sy+=stepy, dy+=stepy) {
212 /* read */
213 if (setbuffer) {
214 (*ctx->Driver.SetBuffer)( ctx, ctx->Pixel.ReadBuffer );
215 }
216 gl_read_index_span( ctx, width, srcx, sy, indx );
217
218 /* shift, offset */
219 if (ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset) {
220 if (ctx->Pixel.IndexShift<0) {
221 for (i=0;i<width;i++) {
222 indx[i] = (indx[i] >> -ctx->Pixel.IndexShift)
223 + ctx->Pixel.IndexOffset;
224 }
225 }
226 else {
227 for (i=0;i<width;i++) {
228 indx[i] = (indx[i] << ctx->Pixel.IndexShift)
229 + ctx->Pixel.IndexOffset;
230 }
231 }
232 }
233
234 /* mapping */
235 if (ctx->Pixel.MapColorFlag) {
236 for (i=0;i<width;i++) {
237 if (indx[i] < ctx->Pixel.MapItoIsize) {
238 indx[i] = ctx->Pixel.MapItoI[ indx[i] ];
239 }
240 }
241 }
242
243 /* write */
244 if (setbuffer) {
245 (*ctx->Driver.SetBuffer)( ctx, ctx->Color.DrawBuffer );
246 }
247 if (zoom) {
248 gl_write_zoomed_index_span( ctx, width, destx, dy, zspan, indx, desty );
249 }
250 else {
251 gl_write_index_span( ctx, width, destx, dy, zspan, indx, GL_BITMAP );
252 }
253 }
254 }
255
256
257
258 /*
259 * TODO: Optimize!!!!
260 */
261 static void copy_depth_pixels( GLcontext* ctx, GLint srcx, GLint srcy,
262 GLint width, GLint height,
263 GLint destx, GLint desty )
264 {
265 GLfloat depth[MAX_WIDTH];
266 GLdepth zspan[MAX_WIDTH];
267 GLuint indx[MAX_WIDTH];
268 GLubyte red[MAX_WIDTH], green[MAX_WIDTH];
269 GLubyte blue[MAX_WIDTH], alpha[MAX_WIDTH];
270 GLint sy, dy, stepy;
271 GLint i, j;
272 GLboolean zoom;
273
274 if (!ctx->Buffer->Depth) {
275 gl_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
276 return;
277 }
278
279 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
280 zoom = GL_FALSE;
281 }
282 else {
283 zoom = GL_TRUE;
284 }
285
286 /* Determine if copy should be bottom-to-top or top-to-bottom */
287 if (srcy<desty) {
288 /* top-down max-to-min */
289 sy = srcy + height - 1;
290 dy = desty + height - 1;
291 stepy = -1;
292 }
293 else {
294 /* bottom-up min-to-max */
295 sy = srcy;
296 dy = desty;
297 stepy = 1;
298 }
299
300 /* setup colors or indexes */
301 if (ctx->Visual->RGBAflag) {
302 GLubyte r, g, b, a;
303 r = ctx->Current.IntColor[0];
304 g = ctx->Current.IntColor[1];
305 b = ctx->Current.IntColor[2];
306 a = ctx->Current.IntColor[3];
307 MEMSET( red, (int) r, width );
308 MEMSET( green, (int) g, width );
309 MEMSET( blue, (int) b, width );
310 MEMSET( alpha, (int) a, width );
311 }
312 else {
313 for (i=0;i<width;i++) {
314 indx[i] = ctx->Current.Index;
315 }
316 }
317
318 for (j=0; j<height; j++, sy+=stepy, dy+=stepy) {
319 /* read */
320 (*ctx->Driver.ReadDepthSpanFloat)( ctx, width, srcx, sy, depth );
321 /* scale, bias, clamp */
322 for (i=0;i<width;i++) {
323 GLfloat d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
324 zspan[i] = (GLint) (CLAMP( d, 0.0, 1.0 ) * DEPTH_SCALE);
325 }
326 /* write */
327 if (ctx->Visual->RGBAflag) {
328 if (zoom) {
329 gl_write_zoomed_color_span( ctx, width, destx, dy, zspan,
330 red, green, blue, alpha, desty );
331 }
332 else {
333 gl_write_color_span( ctx, width, destx, dy, zspan,
334 red, green, blue, alpha, GL_BITMAP );
335 }
336 }
337 else {
338 if (zoom) {
339 gl_write_zoomed_index_span( ctx, width, destx, dy,
340 zspan, indx, desty);
341 }
342 else {
343 gl_write_index_span( ctx, width, destx, dy,
344 zspan, indx, GL_BITMAP );
345 }
346 }
347 }
348 }
349
350
351
352 static void copy_stencil_pixels( GLcontext* ctx, GLint srcx, GLint srcy,
353 GLint width, GLint height,
354 GLint destx, GLint desty )
355 {
356 GLubyte stencil[MAX_WIDTH];
357 GLint sy, dy, stepy;
358 GLint i, j;
359 GLboolean zoom;
360
361 if (!ctx->Buffer->Stencil) {
362 gl_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
363 return;
364 }
365
366 if (ctx->Pixel.ZoomX==1.0F && ctx->Pixel.ZoomY==1.0F) {
367 zoom = GL_FALSE;
368 }
369 else {
370 zoom = GL_TRUE;
371 }
372
373 /* Determine if copy should be bottom-to-top or top-to-bottom */
374 if (srcy<desty) {
375 /* top-down max-to-min */
376 sy = srcy + height - 1;
377 dy = desty + height - 1;
378 stepy = -1;
379 }
380 else {
381 /* bottom-up min-to-max */
382 sy = srcy;
383 dy = desty;
384 stepy = 1;
385 }
386
387 for (j=0; j<height; j++, sy+=stepy, dy+=stepy) {
388 /* read */
389 gl_read_stencil_span( ctx, width, srcx, sy, stencil );
390 /* shift, offset */
391 if (ctx->Pixel.IndexShift<0) {
392 for (i=0;i<width;i++) {
393 stencil[i] = (stencil[i] >> -ctx->Pixel.IndexShift)
394 + ctx->Pixel.IndexOffset;
395 }
396 }
397 else {
398 for (i=0;i<width;i++) {
399 stencil[i] = (stencil[i] << ctx->Pixel.IndexShift)
400 + ctx->Pixel.IndexOffset;
401 }
402 }
403 /* mapping */
404 if (ctx->Pixel.MapStencilFlag) {
405 for (i=0;i<width;i++) {
406 if ((GLint) stencil[i] < ctx->Pixel.MapStoSsize) {
407 stencil[i] = ctx->Pixel.MapStoS[ stencil[i] ];
408 }
409 }
410 }
411 /* write */
412 if (zoom) {
413 gl_write_zoomed_stencil_span( ctx, width, destx, dy, stencil, desty );
414 }
415 else {
416 gl_write_stencil_span( ctx, width, destx, dy, stencil );
417 }
418 }
419 }
420
421
422
423
424 void gl_CopyPixels( GLcontext* ctx, GLint srcx, GLint srcy, GLsizei width, GLsizei height,
425 GLenum type )
426 {
427 GLint destx, desty;
428
429 if (INSIDE_BEGIN_END(ctx)) {
430 gl_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
431 return;
432 }
433
434 if (width<0 || height<0) {
435 gl_error( ctx, GL_INVALID_VALUE, "glCopyPixels" );
436 return;
437 }
438
439 if (ctx->NewState) {
440 gl_update_state(ctx);
441 }
442
443 if (ctx->RenderMode==GL_RENDER) {
444 /* Destination of copy: */
445 if (!ctx->Current.RasterPosValid) {
446 return;
447 }
448 destx = (GLint) (ctx->Current.RasterPos[0] + 0.5F);
449 desty = (GLint) (ctx->Current.RasterPos[1] + 0.5F);
450
451 if (type==GL_COLOR && ctx->Visual->RGBAflag) {
452 copy_rgb_pixels( ctx, srcx, srcy, width, height, destx, desty );
453 }
454 else if (type==GL_COLOR && !ctx->Visual->RGBAflag) {
455 copy_ci_pixels( ctx, srcx, srcy, width, height, destx, desty );
456 }
457 else if (type==GL_DEPTH) {
458 copy_depth_pixels( ctx, srcx, srcy, width, height, destx, desty );
459 }
460 else if (type==GL_STENCIL) {
461 copy_stencil_pixels( ctx, srcx, srcy, width, height, destx, desty );
462 }
463 else {
464 gl_error( ctx, GL_INVALID_ENUM, "glCopyPixels" );
465 }
466 }
467 else if (ctx->RenderMode==GL_FEEDBACK) {
468 GLfloat color[4];
469 color[0] = ctx->Current.IntColor[0] * ctx->Visual->InvRedScale;
470 color[1] = ctx->Current.IntColor[1] * ctx->Visual->InvGreenScale;
471 color[2] = ctx->Current.IntColor[2] * ctx->Visual->InvBlueScale;
472 color[3] = ctx->Current.IntColor[3] * ctx->Visual->InvAlphaScale;
473 FEEDBACK_TOKEN( ctx, (GLfloat) GL_COPY_PIXEL_TOKEN );
474 gl_feedback_vertex( ctx, ctx->Current.RasterPos[0],
475 ctx->Current.RasterPos[1],
476 ctx->Current.RasterPos[2],
477 ctx->Current.RasterPos[3],
478 color, ctx->Current.Index,
479 ctx->Current.TexCoord );
480 }
481 else if (ctx->RenderMode==GL_SELECT) {
482 gl_update_hitflag( ctx, ctx->Current.RasterPos[2] );
483 }
484
485 }
486
487