7bae7ed99e0808cd7d7161e26c6ad1ea59f396a3
[mesa.git] / src / mesa / swrast / s_copypix.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.3
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 struct gl_renderbuffer *drawRb = NULL;
134 GLboolean quick_draw;
135 GLint row;
136 GLboolean changeBuffer;
137 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
138 const GLuint transferOps = ctx->_ImageTransferState;
139 GLfloat *dest, *tmpImage, *convImage;
140 struct sw_span span;
141
142 INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_RGBA);
143
144 if (ctx->Depth.Test)
145 _swrast_span_default_z(ctx, &span);
146 if (ctx->Fog.Enabled)
147 _swrast_span_default_fog(ctx, &span);
148
149
150 if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
151 && !zoom
152 && destx >= 0
153 && destx + width <= (GLint) ctx->DrawBuffer->Width) {
154 quick_draw = GL_TRUE;
155 drawRb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];
156 }
157 else {
158 quick_draw = GL_FALSE;
159 }
160
161 /* If read and draw buffer are different we must do buffer switching */
162 changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer[0]
163 || ctx->DrawBuffer != ctx->ReadBuffer;
164
165
166 /* allocate space for GLfloat image */
167 tmpImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
168 if (!tmpImage) {
169 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
170 return;
171 }
172 convImage = (GLfloat *) MALLOC(width * height * 4 * sizeof(GLfloat));
173 if (!convImage) {
174 FREE(tmpImage);
175 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyPixels");
176 return;
177 }
178
179 if (changeBuffer) {
180 /* choose the read buffer */
181 _swrast_use_read_buffer(ctx);
182 }
183
184 /* read source image */
185 dest = tmpImage;
186 for (row = 0; row < height; row++) {
187 GLchan rgba[MAX_WIDTH][4];
188 /* Read GLchan and convert to GLfloat */
189 _swrast_read_rgba_span(ctx, ctx->ReadBuffer->_ColorReadBuffer,
190 width, srcx, srcy + row, rgba);
191 chan_span_to_float(width, (CONST GLchan (*)[4]) rgba,
192 (GLfloat (*)[4]) dest);
193 dest += 4 * width;
194 }
195
196 if (changeBuffer) {
197 /* restore default src/dst buffer */
198 _swrast_use_draw_buffer(ctx);
199 }
200
201 /* do the image transfer ops which preceed convolution */
202 for (row = 0; row < height; row++) {
203 GLfloat (*rgba)[4] = (GLfloat (*)[4]) (tmpImage + row * width * 4);
204 _mesa_apply_rgba_transfer_ops(ctx,
205 transferOps & IMAGE_PRE_CONVOLUTION_BITS,
206 width, rgba);
207 }
208
209 /* do convolution */
210 if (ctx->Pixel.Convolution2DEnabled) {
211 _mesa_convolve_2d_image(ctx, &width, &height, tmpImage, convImage);
212 }
213 else {
214 ASSERT(ctx->Pixel.Separable2DEnabled);
215 _mesa_convolve_sep_image(ctx, &width, &height, tmpImage, convImage);
216 }
217 FREE(tmpImage);
218
219 /* do remaining post-convolution image transfer ops */
220 for (row = 0; row < height; row++) {
221 GLfloat (*rgba)[4] = (GLfloat (*)[4]) (convImage + row * width * 4);
222 _mesa_apply_rgba_transfer_ops(ctx,
223 transferOps & IMAGE_POST_CONVOLUTION_BITS,
224 width, rgba);
225 }
226
227 /* write the new image */
228 for (row = 0; row < height; row++) {
229 const GLfloat *src = convImage + row * width * 4;
230 GLint dy;
231
232 /* convert floats back to chan */
233 float_span_to_chan(width, (const GLfloat (*)[4]) src, span.array->rgba);
234
235 if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._EnabledUnits) {
236 span.end = width;
237 _swrast_pixel_texture(ctx, &span);
238 }
239
240 /* write row to framebuffer */
241
242 dy = desty + row;
243 if (quick_draw && dy >= 0 && dy < (GLint) ctx->DrawBuffer->Height) {
244 drawRb->PutRow(ctx, drawRb, width, destx, dy, span.array->rgba, NULL);
245 }
246 else if (zoom) {
247 span.x = destx;
248 span.y = dy;
249 span.end = width;
250 _swrast_write_zoomed_rgba_span(ctx, &span,
251 (CONST GLchan (*)[4])span.array->rgba,
252 desty, 0);
253 }
254 else {
255 span.x = destx;
256 span.y = dy;
257 span.end = width;
258 _swrast_write_rgba_span(ctx, &span);
259 }
260 }
261
262 FREE(convImage);
263 }
264
265
266 /*
267 * RGBA copypixels
268 */
269 static void
270 copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
271 GLint width, GLint height, GLint destx, GLint desty)
272 {
273 struct gl_renderbuffer *drawRb;
274 GLchan *tmpImage,*p;
275 GLboolean quick_draw;
276 GLint sy, dy, stepy, j;
277 GLboolean changeBuffer;
278 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
279 GLint overlapping;
280 const GLuint transferOps = ctx->_ImageTransferState;
281 struct sw_span span;
282
283 if (!ctx->ReadBuffer->_ColorReadBuffer) {
284 /* no readbuffer - OK */
285 return;
286 }
287
288 INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_RGBA);
289
290 if (ctx->Pixel.Convolution2DEnabled || ctx->Pixel.Separable2DEnabled) {
291 copy_conv_rgba_pixels(ctx, srcx, srcy, width, height, destx, desty);
292 return;
293 }
294
295 /* Determine if copy should be done bottom-to-top or top-to-bottom */
296 if (srcy < desty) {
297 /* top-down max-to-min */
298 sy = srcy + height - 1;
299 dy = desty + height - 1;
300 stepy = -1;
301 }
302 else {
303 /* bottom-up min-to-max */
304 sy = srcy;
305 dy = desty;
306 stepy = 1;
307 }
308
309 if (ctx->DrawBuffer == ctx->ReadBuffer) {
310 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
311 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
312 }
313 else {
314 overlapping = GL_FALSE;
315 }
316
317 if (ctx->Depth.Test)
318 _swrast_span_default_z(ctx, &span);
319 if (ctx->Fog.Enabled)
320 _swrast_span_default_fog(ctx, &span);
321
322 if (SWRAST_CONTEXT(ctx)->_RasterMask == 0
323 && !zoom
324 && destx >= 0
325 && destx + width <= (GLint) ctx->DrawBuffer->Width) {
326 quick_draw = GL_TRUE;
327 drawRb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];
328 }
329 else {
330 quick_draw = GL_FALSE;
331 drawRb = NULL;
332 }
333
334 /* If read and draw buffer are different we must do buffer switching */
335 changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer[0]
336 || ctx->DrawBuffer != ctx->ReadBuffer;
337
338 if (overlapping) {
339 GLint ssy = sy;
340 tmpImage = (GLchan *) MALLOC(width * height * sizeof(GLchan) * 4);
341 if (!tmpImage) {
342 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
343 return;
344 }
345 /* setup source */
346 if (changeBuffer)
347 _swrast_use_read_buffer(ctx);
348 /* read the source image */
349 p = tmpImage;
350 for (j = 0; j < height; j++, ssy += stepy) {
351 _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
352 width, srcx, ssy, (GLchan (*)[4]) p );
353 p += width * 4;
354 }
355 p = tmpImage;
356 /* restore dest */
357 if (changeBuffer) {
358 _swrast_use_draw_buffer(ctx);
359 changeBuffer = GL_FALSE;
360 }
361 }
362 else {
363 tmpImage = NULL; /* silence compiler warnings */
364 p = NULL;
365 }
366
367 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
368 /* Get source pixels */
369 if (overlapping) {
370 /* get from buffered image */
371 ASSERT(width < MAX_WIDTH);
372 MEMCPY(span.array->rgba, p, width * sizeof(GLchan) * 4);
373 p += width * 4;
374 }
375 else {
376 /* get from framebuffer */
377 if (changeBuffer)
378 _swrast_use_read_buffer(ctx);
379 ASSERT(width < MAX_WIDTH);
380 _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
381 width, srcx, sy, span.array->rgba );
382 if (changeBuffer)
383 _swrast_use_draw_buffer(ctx);
384 }
385
386 if (transferOps) {
387 DEFMARRAY(GLfloat, rgbaFloat, MAX_WIDTH, 4); /* mac 32k limitation */
388 CHECKARRAY(rgbaFloat, return);
389
390 /* convert to float, transfer, convert back to chan */
391 chan_span_to_float(width, (CONST GLchan (*)[4]) span.array->rgba,
392 rgbaFloat);
393 _mesa_apply_rgba_transfer_ops(ctx, transferOps, width, rgbaFloat);
394 float_span_to_chan(width, (CONST GLfloat (*)[4]) rgbaFloat,
395 span.array->rgba);
396
397 UNDEFARRAY(rgbaFloat); /* mac 32k limitation */
398 }
399
400 if (ctx->Pixel.PixelTextureEnabled && ctx->Texture._EnabledUnits) {
401 span.end = width;
402 _swrast_pixel_texture(ctx, &span);
403 }
404
405 /* Write color span */
406 if (quick_draw && dy >= 0 && dy < (GLint) ctx->DrawBuffer->Height) {
407 drawRb->PutRow(ctx, drawRb, width, destx, dy, span.array->rgba, NULL);
408 }
409 else if (zoom) {
410 span.x = destx;
411 span.y = dy;
412 span.end = width;
413 _swrast_write_zoomed_rgba_span(ctx, &span,
414 (CONST GLchan (*)[4]) span.array->rgba,
415 desty, 0);
416 }
417 else {
418 span.x = destx;
419 span.y = dy;
420 span.end = width;
421 _swrast_write_rgba_span(ctx, &span);
422 }
423 }
424
425 if (overlapping)
426 FREE(tmpImage);
427 }
428
429
430 static void
431 copy_ci_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
432 GLint width, GLint height,
433 GLint destx, GLint desty )
434 {
435 GLuint *tmpImage,*p;
436 GLint sy, dy, stepy;
437 GLint j;
438 GLboolean changeBuffer;
439 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
440 const GLboolean shift_or_offset = ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset;
441 GLint overlapping;
442 struct sw_span span;
443
444 if (!ctx->ReadBuffer->_ColorReadBuffer) {
445 /* no readbuffer - OK */
446 return;
447 }
448
449 INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_INDEX);
450
451 /* Determine if copy should be bottom-to-top or top-to-bottom */
452 if (srcy<desty) {
453 /* top-down max-to-min */
454 sy = srcy + height - 1;
455 dy = desty + height - 1;
456 stepy = -1;
457 }
458 else {
459 /* bottom-up min-to-max */
460 sy = srcy;
461 dy = desty;
462 stepy = 1;
463 }
464
465 if (ctx->DrawBuffer == ctx->ReadBuffer) {
466 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
467 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
468 }
469 else {
470 overlapping = GL_FALSE;
471 }
472
473 if (ctx->Depth.Test)
474 _swrast_span_default_z(ctx, &span);
475 if (ctx->Fog.Enabled)
476 _swrast_span_default_fog(ctx, &span);
477
478 /* If read and draw buffer are different we must do buffer switching */
479 changeBuffer = ctx->Pixel.ReadBuffer != ctx->Color.DrawBuffer[0]
480 || ctx->DrawBuffer != ctx->ReadBuffer;
481
482 if (overlapping) {
483 GLint ssy = sy;
484 tmpImage = (GLuint *) MALLOC(width * height * sizeof(GLuint));
485 if (!tmpImage) {
486 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
487 return;
488 }
489 /* setup source */
490 if (changeBuffer)
491 _swrast_use_read_buffer(ctx);
492 /* read the image */
493 p = tmpImage;
494 for (j = 0; j < height; j++, ssy += stepy) {
495 _swrast_read_index_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
496 width, srcx, ssy, p );
497 p += width;
498 }
499 p = tmpImage;
500 /* restore to draw buffer */
501 if (changeBuffer) {
502 _swrast_use_draw_buffer(ctx);
503 changeBuffer = GL_FALSE;
504 }
505 }
506 else {
507 tmpImage = NULL; /* silence compiler warning */
508 p = NULL;
509 }
510
511 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
512 /* Get color indexes */
513 if (overlapping) {
514 MEMCPY(span.array->index, p, width * sizeof(GLuint));
515 p += width;
516 }
517 else {
518 if (changeBuffer)
519 _swrast_use_read_buffer(ctx);
520 _swrast_read_index_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
521 width, srcx, sy, span.array->index );
522 if (changeBuffer)
523 _swrast_use_draw_buffer(ctx);
524 }
525
526 /* Apply shift, offset, look-up table */
527 if (shift_or_offset) {
528 _mesa_shift_and_offset_ci( ctx, width, span.array->index );
529 }
530 if (ctx->Pixel.MapColorFlag) {
531 _mesa_map_ci( ctx, width, span.array->index );
532 }
533
534 /* write color indexes */
535 span.x = destx;
536 span.y = dy;
537 span.end = width;
538 if (zoom)
539 _swrast_write_zoomed_index_span(ctx, &span, desty, 0);
540 else
541 _swrast_write_index_span(ctx, &span);
542 }
543
544 if (overlapping)
545 FREE(tmpImage);
546 }
547
548
549
550 /*
551 * TODO: Optimize!!!!
552 */
553 static void
554 copy_depth_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
555 GLint width, GLint height,
556 GLint destx, GLint desty )
557 {
558 const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
559 struct gl_renderbuffer *readRb
560 = ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
561 GLfloat *p, *tmpImage;
562 GLint sy, dy, stepy;
563 GLint i, j;
564 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
565 GLint overlapping;
566 struct sw_span span;
567
568 if (!readRb) {
569 /* no readbuffer - OK */
570 return;
571 }
572
573 INIT_SPAN(span, GL_BITMAP, 0, 0, SPAN_Z);
574
575 if (!ctx->Visual.depthBits) {
576 _mesa_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
577 return;
578 }
579
580 /* Determine if copy should be bottom-to-top or top-to-bottom */
581 if (srcy<desty) {
582 /* top-down max-to-min */
583 sy = srcy + height - 1;
584 dy = desty + height - 1;
585 stepy = -1;
586 }
587 else {
588 /* bottom-up min-to-max */
589 sy = srcy;
590 dy = desty;
591 stepy = 1;
592 }
593
594 if (ctx->DrawBuffer == ctx->ReadBuffer) {
595 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
596 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
597 }
598 else {
599 overlapping = GL_FALSE;
600 }
601
602 _swrast_span_default_color(ctx, &span);
603 if (ctx->Fog.Enabled)
604 _swrast_span_default_fog(ctx, &span);
605
606 if (overlapping) {
607 GLint ssy = sy;
608 tmpImage = (GLfloat *) MALLOC(width * height * sizeof(GLfloat));
609 if (!tmpImage) {
610 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
611 return;
612 }
613 p = tmpImage;
614 for (j = 0; j < height; j++, ssy += stepy) {
615 _swrast_read_depth_span_float(ctx, readRb, width, srcx, ssy, p);
616 p += width;
617 }
618 p = tmpImage;
619 }
620 else {
621 tmpImage = NULL; /* silence compiler warning */
622 p = NULL;
623 }
624
625 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
626 GLfloat depth[MAX_WIDTH];
627 float sum = 0;
628 /* get depth values */
629 if (overlapping) {
630 MEMCPY(depth, p, width * sizeof(GLfloat));
631 p += width;
632 }
633 else {
634 _swrast_read_depth_span_float(ctx, readRb, width, srcx, sy, depth);
635 }
636
637 /* apply scale and bias */
638 for (i = 0; i < width; i++) {
639 GLfloat d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
640 sum += d;
641 span.array->z[i] = (GLdepth) (CLAMP(d, 0.0F, 1.0F) * depthMax);
642 }
643
644 /* write depth values */
645 span.x = destx;
646 span.y = dy;
647 span.end = width;
648 if (ctx->Visual.rgbMode) {
649 if (zoom)
650 _swrast_write_zoomed_rgba_span( ctx, &span,
651 (const GLchan (*)[4])span.array->rgba, desty, 0 );
652 else
653 _swrast_write_rgba_span(ctx, &span);
654 }
655 else {
656 if (zoom)
657 _swrast_write_zoomed_index_span( ctx, &span, desty, 0 );
658 else
659 _swrast_write_index_span(ctx, &span);
660 }
661 }
662
663 if (overlapping)
664 FREE(tmpImage);
665 }
666
667
668
669 static void
670 copy_stencil_pixels( GLcontext *ctx, GLint srcx, GLint srcy,
671 GLint width, GLint height,
672 GLint destx, GLint desty )
673 {
674 struct gl_renderbuffer *rb
675 = ctx->ReadBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;
676 GLint sy, dy, stepy;
677 GLint j;
678 GLstencil *p, *tmpImage;
679 const GLboolean zoom = ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F;
680 const GLboolean shift_or_offset = ctx->Pixel.IndexShift || ctx->Pixel.IndexOffset;
681 GLint overlapping;
682
683 if (!ctx->Visual.stencilBits) {
684 _mesa_error( ctx, GL_INVALID_OPERATION, "glCopyPixels" );
685 return;
686 }
687
688 if (!rb) {
689 /* no readbuffer - OK */
690 return;
691 }
692
693 /* Determine if copy should be bottom-to-top or top-to-bottom */
694 if (srcy < desty) {
695 /* top-down max-to-min */
696 sy = srcy + height - 1;
697 dy = desty + height - 1;
698 stepy = -1;
699 }
700 else {
701 /* bottom-up min-to-max */
702 sy = srcy;
703 dy = desty;
704 stepy = 1;
705 }
706
707 if (ctx->DrawBuffer == ctx->ReadBuffer) {
708 overlapping = regions_overlap(srcx, srcy, destx, desty, width, height,
709 ctx->Pixel.ZoomX, ctx->Pixel.ZoomY);
710 }
711 else {
712 overlapping = GL_FALSE;
713 }
714
715 if (overlapping) {
716 GLint ssy = sy;
717 tmpImage = (GLstencil *) MALLOC(width * height * sizeof(GLstencil));
718 if (!tmpImage) {
719 _mesa_error( ctx, GL_OUT_OF_MEMORY, "glCopyPixels" );
720 return;
721 }
722 p = tmpImage;
723 for (j = 0; j < height; j++, ssy += stepy) {
724 _swrast_read_stencil_span( ctx, rb, width, srcx, ssy, p );
725 p += width;
726 }
727 p = tmpImage;
728 }
729 else {
730 tmpImage = NULL; /* silence compiler warning */
731 p = NULL;
732 }
733
734 for (j = 0; j < height; j++, sy += stepy, dy += stepy) {
735 GLstencil stencil[MAX_WIDTH];
736
737 /* Get stencil values */
738 if (overlapping) {
739 MEMCPY(stencil, p, width * sizeof(GLstencil));
740 p += width;
741 }
742 else {
743 _swrast_read_stencil_span( ctx, rb, width, srcx, sy, stencil );
744 }
745
746 /* Apply shift, offset, look-up table */
747 if (shift_or_offset) {
748 _mesa_shift_and_offset_stencil( ctx, width, stencil );
749 }
750 if (ctx->Pixel.MapStencilFlag) {
751 _mesa_map_stencil( ctx, width, stencil );
752 }
753
754 /* Write stencil values */
755 if (zoom) {
756 _swrast_write_zoomed_stencil_span( ctx, width, destx, dy,
757 stencil, desty, 0 );
758 }
759 else {
760 _swrast_write_stencil_span( ctx, width, destx, dy, stencil );
761 }
762 }
763
764 if (overlapping)
765 FREE(tmpImage);
766 }
767
768
769
770 void
771 _swrast_CopyPixels( GLcontext *ctx,
772 GLint srcx, GLint srcy, GLsizei width, GLsizei height,
773 GLint destx, GLint desty,
774 GLenum type )
775 {
776 SWcontext *swrast = SWRAST_CONTEXT(ctx);
777 RENDER_START(swrast,ctx);
778
779 if (swrast->NewState)
780 _swrast_validate_derived( ctx );
781
782 if (type == GL_COLOR && ctx->Visual.rgbMode) {
783 copy_rgba_pixels( ctx, srcx, srcy, width, height, destx, desty );
784 }
785 else if (type == GL_COLOR && !ctx->Visual.rgbMode) {
786 copy_ci_pixels( ctx, srcx, srcy, width, height, destx, desty );
787 }
788 else if (type == GL_DEPTH) {
789 copy_depth_pixels( ctx, srcx, srcy, width, height, destx, desty );
790 }
791 else if (type == GL_STENCIL) {
792 copy_stencil_pixels( ctx, srcx, srcy, width, height, destx, desty );
793 }
794 else {
795 _mesa_error( ctx, GL_INVALID_ENUM, "glCopyPixels" );
796 }
797
798 RENDER_FINISH(swrast,ctx);
799 }