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