Debian Codegraph @

nx-X11/extras/Mesa/src/mesa/swrast/s_copypix.c

raw
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.4
4 *
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "glheader.h"
27 #include "context.h"
28 #include "colormac.h"
29 #include "convolve.h"
30 #include "histogram.h"
31 #include "image.h"
32 #include "macros.h"
33 #include "imports.h"
34 #include "pixel.h"
35
36 #include "s_context.h"
37 #include "s_depth.h"
38 #include "s_pixeltex.h"
39 #include "s_span.h"
40 #include "s_stencil.h"
41 #include "s_texture.h"
42 #include "s_zoom.h"
43
44
45
46 /*
47 * Determine if there's overlap in an image copy.
48 * This test also compensates for the fact that copies are done from
49 * bottom to top and overlaps can sometimes be handled correctly
50 * without making a temporary image copy.
51 */
52 static GLboolean
53 regions_overlap(GLint srcx, GLint srcy,
54 GLint dstx, GLint dsty,
55 GLint width, GLint height,
56 GLfloat zoomX, GLfloat zoomY)
57 {
58 if (zoomX == 1.0 && zoomY == 1.0) {
59 /* no zoom */
60 if (srcx >= dstx + width || (srcx + width <= dstx)) {
61 return GL_FALSE;
62 }
63 else if (srcy < dsty) { /* this is OK */
64 return GL_FALSE;
65 }
66 else if (srcy > dsty + height) {
67 return GL_FALSE;
68 }
69 else {
70 return GL_TRUE;
71 }
72 }
73 else {
74 /* add one pixel of slop when zooming, just to be safe */
75 if ((srcx > dstx + (width * zoomX) + 1) || (srcx + width + 1 < dstx)) {
76 return GL_FALSE;
77 }
78 else if ((srcy < dsty) && (srcy + height < dsty + (height * zoomY))) {
79 return GL_FALSE;
80 }
81 else if ((srcy > dsty) && (srcy + height > dsty + (height * zoomY))) {
82 return GL_FALSE;
83 }
84 else {
85 return GL_TRUE;
86 }
87 }
88 }
89
90
91 /**
92 * Convert GLfloat[n][4] colors to GLchan[n][4].
93 * XXX maybe move into image.c
94 */
95 static void
96 float_span_to_chan(GLuint n, CONST GLfloat in[][4], GLchan out[][4])
97 {
98 GLuint i;
99 for (i = 0; i < n; i++) {
100 UNCLAMPED_FLOAT_TO_CHAN(out[i][RCOMP], in[i][RCOMP]);
101 UNCLAMPED_FLOAT_TO_CHAN(out[i][GCOMP], in[i][GCOMP]);
102 UNCLAMPED_FLOAT_TO_CHAN(out[i][BCOMP], in[i][BCOMP]);
103 UNCLAMPED_FLOAT_TO_CHAN(out[i][ACOMP], in[i][ACOMP]);
104 }
105 }
106
107
108 /**
109 * Convert GLchan[n][4] colors to GLfloat[n][4].
110 * XXX maybe move into image.c
111 */
112 static void
113 chan_span_to_float(GLuint n, CONST GLchan in[][4], GLfloat out[][4])
114 {
115 GLuint i;
116 for (i = 0; i < n; i++) {
117 out[i][RCOMP] = CHAN_TO_FLOAT(in[i][RCOMP]);
118 out[i][GCOMP] = CHAN_TO_FLOAT(in[i][GCOMP]);
119 out[i][BCOMP] = CHAN_TO_FLOAT(in[i][BCOMP]);
120 out[i][ACOMP] = CHAN_TO_FLOAT(in[i][ACOMP]);
121 }
122 }
123
124
125
126 /*
127 * RGBA copypixels with convolution.
128 */
129 static void
130 copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
131 GLint width, GLint height, GLint destx, GLint desty)
132 {
133 SWcontext *swrast = SWRAST_CONTEXT(ctx);
134 struct gl_renderbuffer *drawRb = NULL;
135 GLboolean quick_draw;
136 GLint row;
137 GLboolean changeBuffer;
138 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
139 const GLuint transferOps = ctx->_ImageTransferState;
140 GLfloat *dest, *tmpImage, *convImage;
141 struct sw_span span;
142
143 INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_RGBA);
144
145 if (ctx->Depth.Test)
146 _swrast_span_default_z(ctx, &span);
147 if (swrast->_FogEnabled)
148 _swrast_span_default_fog(ctx, &span);
149
150
151 if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
152 && !zoom
153 && destx >= 0
154 && destx + width <= (GLint) ctx->DrawBuffer->Width) {
155 quick_draw = GL_TRUE;
156 drawRb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];
157 }
158 else {
159 quick_draw = GL_FALSE;
160 }
161
162 /* If read and draw buffer are different we must do buffer switching */
163 changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer[0]
164 || ctx->DrawBuffer != ctx->ReadBuffer;
165
166
167 /* allocate space for GLfloat image */
168 tmpImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
169 if (!tmpImage) {
170 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
171 return;
172 }
173 convImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
174 if (!convImage) {
175 FREE(tmpImage);
176 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
177 return;
178 }
179
180 if (changeBuffer) {
181 /* choose the read buffer */
182 _swrast_use_read_buffer(ctx);
183 }
184
185 /* read source image */
186 dest = tmpImage;
187 for (row = 0; row < height; row++) {
188 GLchan rgba[MAX_WIDTH][4];
189 /* Read GLchan and convert to GLfloat */
190 _swrast_read_rgba_span(ctx, ctx->ReadBuffer->_ColorReadBuffer,
191 width, srcx, srcy + row, rgba);
192 chan_span_to_float(width, (CONST GLchan (*)[4]) rgba,
193 (GLfloat (*)[4]) dest);
194 dest += 4 * width;
195 }
196
197 if (changeBuffer) {
198 /* restore default src/dst buffer */
199 _swrast_use_draw_buffer(ctx);
200 }
201
202 /* do the image transfer ops which preceed convolution */
203 for (row = 0; row < height; row++) {
204 GLfloat (*rgba)[4] = (GLfloat (*)[4]) (tmpImage + row * width * 4);
205 _mesa_apply_rgba_transfer_ops(ctx,
206 transferOps & IMAGE_PRE_CONVOLUTION_BITS,
207 width, rgba);
208 }
209
210 /* do convolution */
211 if (ctx->Pixel.Convolution2DEnabled) {
212 _mesa_convolve_2d_image(ctx, &width, &height, tmpImage, convImage);
213 }
214 else {
215 ASSERT(ctx->Pixel.Separable2DEnabled);
216 _mesa_convolve_sep_image(ctx, &width, &height, tmpImage, convImage);
217 }
218 FREE(tmpImage);
219
220 /* do remaining post-convolution image transfer ops */
221 for (row = 0; row < height; row++) {
222 GLfloat (*rgba)[4] = (GLfloat (*)[4]) (convImage + row * width * 4);
223 _mesa_apply_rgba_transfer_ops(ctx,
224 transferOps & IMAGE_POST_CONVOLUTION_BITS,
225 width, rgba);
226 }
227
228 /* write the new image */
229 for (row = 0; row < height; row++) {
230 const GLfloat *src = convImage + row * width * 4;
231 GLint dy;
232
233 /* convert floats back to chan */
234 float_span_to_chan(width, (const GLfloat (*)[4]) src, span.array->rgba);
235
236 if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._EnabledUnits) {
237 span.end = width;
238 _swrast_pixel_texture(ctx, &span);
239 }
240
241 /* write row to framebuffer */
242
243 dy = desty + row;
244 if (quick_draw && dy >= 0 && dy < (GLint) ctx->DrawBuffer->Height) {
245 drawRb->PutRow(ctx, drawRb, width, destx, dy, span.array->rgba, NULL);
246 }
247 else if (zoom) {
248 span.x = destx;
249 span.y = dy;
250 span.end = width;
251 _swrast_write_zoomed_rgba_span(ctx, &span,
252 (CONST GLchan (*)[4])span.array->rgba,
253 desty, 0);
254 }
255 else {
256 span.x = destx;
257 span.y = dy;
258 span.end = width;
259 _swrast_write_rgba_span(ctx, &span);
260 }
261 }
262
263 FREE(convImage);
264 }
265
266
267 /*
268 * RGBA copypixels
269 */
270 static void
271 copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
272 GLint width, GLint height, GLint destx, GLint desty)
273 {
274 SWcontext *swrast = SWRAST_CONTEXT(ctx);
275 struct gl_renderbuffer *drawRb;
276 GLchan *tmpImage,*p;
277 GLboolean quick_draw;
278 GLint sy, dy, stepy, j;
279 GLboolean changeBuffer;
280 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
281 GLint overlapping;
282 const GLuint transferOps = ctx->_ImageTransferState;
283 struct sw_span span;
284
285 if (!ctx->ReadBuffer->_ColorReadBuffer) {
286 /* no readbuffer - OK */
287 return;
288 }
289
290 INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_RGBA);
291
292 if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
293 copy_conv_rgba_pixels(ctx, srcx, srcy, width, height, destx, desty);
294 return;
295 }
296
297 /* Determine if copy should be done bottom-to-top or top-to-bottom */
298 if (srcy < desty) {
299 /* top-down max-to-min */
300 sy = srcy + height - 1;
301 dy = desty + height - 1;
302 stepy = -1;
303 }
304 else {
305 /* bottom-up min-to-max */
306 sy = srcy;
307 dy = desty;
308 stepy = 1;
309 }
310
311 if (ctx->DrawBuffer == ctx->ReadBuffer) {
312 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
313 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
314 }
315 else {
316 overlapping = GL_FALSE;
317 }
318
319 if (ctx->Depth.Test)
320 _swrast_span_default_z(ctx, &span);
321 if (swrast->_FogEnabled)
322 _swrast_span_default_fog(ctx, &span);
323
324 if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
325 && !zoom
326 && destx >= 0
327 && destx + width <= (GLint) ctx->DrawBuffer->Width) {
328 quick_draw = GL_TRUE;
329 drawRb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];
330 }
331 else {
332 quick_draw = GL_FALSE;
333 drawRb = NULL;
334 }
335
336 /* If read and draw buffer are different we must do buffer switching */
337 changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer[0]
338 || ctx->DrawBuffer != ctx->ReadBuffer;
339
340 if (overlapping) {
341 GLint ssy = sy;
342 tmpImage = (GLchan *) MALLOC(width * height * sizeof(GLchan) * 4);
343 if (!tmpImage) {
344 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
345 return;
346 }
347 /* setup source */
348 if (changeBuffer)
349 _swrast_use_read_buffer(ctx);
350 /* read the source image */
351 p = tmpImage;
352 for (j = 0; j < height; j++, ssy += stepy) {
353 _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
354 width, srcx, ssy, (GLchan (*)[4]) p );
355 p += width * 4;
356 }
357 p = tmpImage;
358 /* restore dest */
359 if (changeBuffer) {
360 _swrast_use_draw_buffer(ctx);
361 changeBuffer = GL_FALSE;
362 }
363 }
364 else {
365 tmpImage = NULL; /* silence compiler warnings */
366 p = NULL;
367 }
368
369 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
370 /* Get source pixels */
371 if (overlapping) {
372 /* get from buffered image */
373 ASSERT(width < MAX_WIDTH);
374 MEMCPY(span.array->rgba, p, width * sizeof(GLchan) * 4);
375 p += width * 4;
376 }
377 else {
378 /* get from framebuffer */
379 if (changeBuffer)
380 _swrast_use_read_buffer(ctx);
381 ASSERT(width < MAX_WIDTH);
382 _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
383 width, srcx, sy, span.array->rgba );
384 if (changeBuffer)
385 _swrast_use_draw_buffer(ctx);
386 }
387
388 if (transferOps) {
389 DEFMARRAY(GLfloat, rgbaFloat, MAX_WIDTH, 4); /* mac 32k limitation */
390 CHECKARRAY(rgbaFloat, return);
391
392 /* convert to float, transfer, convert back to chan */
393 chan_span_to_float(width, (CONST GLchan (*)[4]) span.array->rgba,
394 rgbaFloat);
395 _mesa_apply_rgba_transfer_ops(ctx, transferOps, width, rgbaFloat);
396 float_span_to_chan(width, (CONST GLfloat (*)[4]) rgbaFloat,
397 span.array->rgba);
398
399 UNDEFARRAY(rgbaFloat); /* mac 32k limitation */
400 }
401
402 if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._EnabledUnits) {
403 span.end = width;
404 _swrast_pixel_texture(ctx, &span);
405 }
406
407 /* Write color span */
408 if (quick_draw && dy >= 0 && dy < (GLint) ctx->DrawBuffer->Height) {
409 drawRb->PutRow(ctx, drawRb, width, destx, dy, span.array->rgba, NULL);
410 }
411 else if (zoom) {
412 span.x = destx;
413 span.y = dy;
414 span.end = width;
415 _swrast_write_zoomed_rgba_span(ctx, &span,
416 (CONST GLchan (*)[4]) span.array->rgba,
417 desty, 0);
418 }
419 else {
420 span.x = destx;
421 span.y = dy;
422 span.end = width;
423 _swrast_write_rgba_span(ctx, &span);
424 }
425 }
426
427 if (overlapping)
428 FREE(tmpImage);
429 }
430
431
432 static void
433 copy_ci_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
434 GLint width, GLint height,
435 GLint destx, GLint desty )
436 {
437 SWcontext *swrast = SWRAST_CONTEXT(ctx);
438 GLuint *tmpImage,*p;
439 GLint sy, dy, stepy;
440 GLint j;
441 GLboolean changeBuffer;
442 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
443 const GLboolean shift_or_offset = ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset;
444 GLint overlapping;
445 struct sw_span span;
446
447 if (!ctx->ReadBuffer->_ColorReadBuffer) {
448 /* no readbuffer - OK */
449 return;
450 }
451
452 INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_INDEX);
453
454 /* Determine if copy should be bottom-to-top or top-to-bottom */
455 if (srcy<desty) {
456 /* top-down max-to-min */
457 sy = srcy + height - 1;
458 dy = desty + height - 1;
459 stepy = -1;
460 }
461 else {
462 /* bottom-up min-to-max */
463 sy = srcy;
464 dy = desty;
465 stepy = 1;
466 }
467
468 if (ctx->DrawBuffer == ctx->ReadBuffer) {
469 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
470 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
471 }
472 else {
473 overlapping = GL_FALSE;
474 }
475
476 if (ctx->Depth.Test)
477 _swrast_span_default_z(ctx, &span);
478 if (swrast->_FogEnabled)
479 _swrast_span_default_fog(ctx, &span);
480
481 /* If read and draw buffer are different we must do buffer switching */
482 changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer[0]
483 || ctx->DrawBuffer != ctx->ReadBuffer;
484
485 if (overlapping) {
486 GLint ssy = sy;
487 tmpImage = (GLuint *) MALLOC(width * height * sizeof(GLuint));
488 if (!tmpImage) {
489 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
490 return;
491 }
492 /* setup source */
493 if (changeBuffer)
494 _swrast_use_read_buffer(ctx);
495 /* read the image */
496 p = tmpImage;
497 for (j = 0; j < height; j++, ssy += stepy) {
498 _swrast_read_index_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
499 width, srcx, ssy, p );
500 p += width;
501 }
502 p = tmpImage;
503 /* restore to draw buffer */
504 if (changeBuffer) {
505 _swrast_use_draw_buffer(ctx);
506 changeBuffer = GL_FALSE;
507 }
508 }
509 else {
510 tmpImage = NULL; /* silence compiler warning */
511 p = NULL;
512 }
513
514 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
515 /* Get color indexes */
516 if (overlapping) {
517 MEMCPY(span.array->index, p, width * sizeof(GLuint));
518 p += width;
519 }
520 else {
521 if (changeBuffer)
522 _swrast_use_read_buffer(ctx);
523 _swrast_read_index_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
524 width, srcx, sy, span.array->index );
525 if (changeBuffer)
526 _swrast_use_draw_buffer(ctx);
527 }
528
529 /* Apply shift, offset, look-up table */
530 if (shift_or_offset) {
531 _mesa_shift_and_offset_ci( ctx, width, span.array->index );
532 }
533 if (ctx->Pixel.MapColorFlag) {
534 _mesa_map_ci( ctx, width, span.array->index );
535 }
536
537 /* write color indexes */
538 span.x = destx;
539 span.y = dy;
540 span.end = width;
541 if (zoom)
542 _swrast_write_zoomed_index_span(ctx, &span, desty, 0);
543 else
544 _swrast_write_index_span(ctx, &span);
545 }
546
547 if (overlapping)
548 FREE(tmpImage);
549 }
550
551
552
553 /*
554 * TODO: Optimize!!!!
555 */
556 static void
557 copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
558 GLint width, GLint height,
559 GLint destx, GLint desty )
560 {
561 SWcontext *swrast = SWRAST_CONTEXT(ctx);
562 const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
563 struct gl_renderbuffer *readRb
564 = ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
565 GLfloat *p, *tmpImage;
566 GLint sy, dy, stepy;
567 GLint i, j;
568 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
569 GLint overlapping;
570 struct sw_span span;
571
572 if (!readRb) {
573 /* no readbuffer - OK */
574 return;
575 }
576
577 INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_Z);
578
579 if (!ctx->Visual.depthBits) {
580 _mesa_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
581 return;
582 }
583
584 /* Determine if copy should be bottom-to-top or top-to-bottom */
585 if (srcy<desty) {
586 /* top-down max-to-min */
587 sy = srcy + height - 1;
588 dy = desty + height - 1;
589 stepy = -1;
590 }
591 else {
592 /* bottom-up min-to-max */
593 sy = srcy;
594 dy = desty;
595 stepy = 1;
596 }
597
598 if (ctx->DrawBuffer == ctx->ReadBuffer) {
599 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
600 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
601 }
602 else {
603 overlapping = GL_FALSE;
604 }
605
606 _swrast_span_default_color(ctx, &span);
607 if (swrast->_FogEnabled)
608 _swrast_span_default_fog(ctx, &span);
609
610 if (overlapping) {
611 GLint ssy = sy;
612 tmpImage = (GLfloat *) MALLOC(width * height * sizeof(GLfloat));
613 if (!tmpImage) {
614 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
615 return;
616 }
617 p = tmpImage;
618 for (j = 0; j < height; j++, ssy += stepy) {
619 _swrast_read_depth_span_float(ctx, readRb, width, srcx, ssy, p);
620 p += width;
621 }
622 p = tmpImage;
623 }
624 else {
625 tmpImage = NULL; /* silence compiler warning */
626 p = NULL;
627 }
628
629 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
630 GLfloat depth[MAX_WIDTH];
631 /* get depth values */
632 if (overlapping) {
633 MEMCPY(depth, p, width * sizeof(GLfloat));
634 p += width;
635 }
636 else {
637 _swrast_read_depth_span_float(ctx, readRb, width, srcx, sy, depth);
638 }
639
640 /* apply scale and bias */
641 for (i = 0; i < width; i++) {
642 GLfloat d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
643 span.array->z[i] = (GLdepth) (CLAMP(d, 0.0F, 1.0F) * depthMax);
644 }
645
646 /* write depth values */
647 span.x = destx;
648 span.y = dy;
649 span.end = width;
650 if (ctx->Visual.rgbMode) {
651 if (zoom)
652 _swrast_write_zoomed_rgba_span( ctx, &span,
653 (const GLchan (*)[4])span.array->rgba, desty, 0 );
654 else
655 _swrast_write_rgba_span(ctx, &span);
656 }
657 else {
658 if (zoom)
659 _swrast_write_zoomed_index_span( ctx, &span, desty, 0 );
660 else
661 _swrast_write_index_span(ctx, &span);
662 }
663 }
664
665 if (overlapping)
666 FREE(tmpImage);
667 }
668
669
670
671 static void
672 copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
673 GLint width, GLint height,
674 GLint destx, GLint desty )
675 {
676 struct gl_renderbuffer *rb
677 = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
678 GLint sy, dy, stepy;
679 GLint j;
680 GLstencil *p, *tmpImage;
681 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
682 const GLboolean shift_or_offset = ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset;
683 GLint overlapping;
684
685 if (!ctx->Visual.stencilBits) {
686 _mesa_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
687 return;
688 }
689
690 if (!rb) {
691 /* no readbuffer - OK */
692 return;
693 }
694
695 /* Determine if copy should be bottom-to-top or top-to-bottom */
696 if (srcy < desty) {
697 /* top-down max-to-min */
698 sy = srcy + height - 1;
699 dy = desty + height - 1;
700 stepy = -1;
701 }
702 else {
703 /* bottom-up min-to-max */
704 sy = srcy;
705 dy = desty;
706 stepy = 1;
707 }
708
709 if (ctx->DrawBuffer == ctx->ReadBuffer) {
710 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
711 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
712 }
713 else {
714 overlapping = GL_FALSE;
715 }
716
717 if (overlapping) {
718 GLint ssy = sy;
719 tmpImage = (GLstencil *) MALLOC(width * height * sizeof(GLstencil));
720 if (!tmpImage) {
721 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
722 return;
723 }
724 p = tmpImage;
725 for (j = 0; j < height; j++, ssy += stepy) {
726 _swrast_read_stencil_span( ctx, rb, width, srcx, ssy, p );
727 p += width;
728 }
729 p = tmpImage;
730 }
731 else {
732 tmpImage = NULL; /* silence compiler warning */
733 p = NULL;
734 }
735
736 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
737 GLstencil stencil[MAX_WIDTH];
738
739 /* Get stencil values */
740 if (overlapping) {
741 MEMCPY(stencil, p, width * sizeof(GLstencil));
742 p += width;
743 }
744 else {
745 _swrast_read_stencil_span( ctx, rb, width, srcx, sy, stencil );
746 }
747
748 /* Apply shift, offset, look-up table */
749 if (shift_or_offset) {
750 _mesa_shift_and_offset_stencil( ctx, width, stencil );
751 }
752 if (ctx->Pixel.MapStencilFlag) {
753 _mesa_map_stencil( ctx, width, stencil );
754 }
755
756 /* Write stencil values */
757 if (zoom) {
758 _swrast_write_zoomed_stencil_span( ctx, width, destx, dy,
759 stencil, desty, 0 );
760 }
761 else {
762 _swrast_write_stencil_span( ctx, width, destx, dy, stencil );
763 }
764 }
765
766 if (overlapping)
767 FREE(tmpImage);
768 }
769
770
771
772 void
773 _swrast_CopyPixels( GLcontext *ctx,
774 GLint srcx, GLint srcy, GLsizei width, GLsizei height,
775 GLint destx, GLint desty,
776 GLenum type )
777 {
778 SWcontext *swrast = SWRAST_CONTEXT(ctx);
779 RENDER_START(swrast,ctx);
780
781 if (swrast->NewState)
782 _swrast_validate_derived( ctx );
783
784 if (type == GL_COLOR && ctx->Visual.rgbMode) {
785 copy_rgba_pixels( ctx, srcx, srcy, width, height, destx, desty );
786 }
787 else if (type == GL_COLOR && !ctx->Visual.rgbMode) {
788 copy_ci_pixels( ctx, srcx, srcy, width, height, destx, desty );
789 }
790 else if (type == GL_DEPTH) {
791 copy_depth_pixels( ctx, srcx, srcy, width, height, destx, desty );
792 }
793 else if (type == GL_STENCIL) {
794 copy_stencil_pixels( ctx, srcx, srcy, width, height, destx, desty );
795 }
796 else {
797 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyPixels" );
798 }
799
800 RENDER_FINISH(swrast,ctx);
801 }