2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
29 #include "histogram.h"
34 #include "s_context.h"
36 #include "s_pixeltex.h"
38 #include "s_stencil.h"
39 #include "s_texture.h"
45 * Determine if there's overlap in an image copy.
46 * This test also compensates for the fact that copies are done from
47 * bottom to top and overlaps can sometimes be handled correctly
48 * without making a temporary image copy.
51 regions_overlap(GLint srcx
, GLint srcy
,
52 GLint dstx
, GLint dsty
,
53 GLint width
, GLint height
,
54 GLfloat zoomX
, GLfloat zoomY
)
56 if (zoomX
== 1.0 && zoomY
== 1.0) {
58 if (srcx
>= dstx
+ width
|| (srcx
+ width
<= dstx
)) {
61 else if (srcy
< dsty
) { /* this is OK */
64 else if (srcy
> dsty
+ height
) {
72 /* add one pixel of slop when zooming, just to be safe */
73 if ((srcx
> dstx
+ (width
* zoomX
) + 1) || (srcx
+ width
+ 1 < dstx
)) {
76 else if ((srcy
< dsty
) && (srcy
+ height
< dsty
+ (height
* zoomY
))) {
79 else if ((srcy
> dsty
) && (srcy
+ height
> dsty
+ (height
* zoomY
))) {
91 * RGBA copypixels with convolution.
94 copy_conv_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
95 GLint width
, GLint height
, GLint destx
, GLint desty
)
97 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
100 GLboolean changeBuffer
;
101 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
102 const GLuint transferOps
= ctx
->_ImageTransferState
;
103 GLfloat
*dest
, *tmpImage
, *convImage
;
106 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_RGBA
);
109 _swrast_span_default_z(ctx
, &span
);
110 if (ctx
->Fog
.Enabled
)
111 _swrast_span_default_fog(ctx
, &span
);
114 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
117 && destx
+ width
<= (GLint
) ctx
->DrawBuffer
->Width
) {
118 quick_draw
= GL_TRUE
;
121 quick_draw
= GL_FALSE
;
124 /* If read and draw buffer are different we must do buffer switching */
125 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
126 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
129 /* allocate space for GLfloat image */
130 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
132 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
135 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
138 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
145 /* choose the read buffer */
146 _swrast_use_read_buffer(ctx
);
149 /* read source image */
151 for (row
= 0; row
< height
; row
++) {
152 GLchan rgba
[MAX_WIDTH
][4];
154 _swrast_read_rgba_span(ctx
, ctx
->ReadBuffer
, width
, srcx
, srcy
+ row
, rgba
);
155 /* convert GLchan to GLfloat */
156 for (i
= 0; i
< width
; i
++) {
157 *dest
++ = (GLfloat
) rgba
[i
][RCOMP
] * (1.0F
/ CHAN_MAXF
);
158 *dest
++ = (GLfloat
) rgba
[i
][GCOMP
] * (1.0F
/ CHAN_MAXF
);
159 *dest
++ = (GLfloat
) rgba
[i
][BCOMP
] * (1.0F
/ CHAN_MAXF
);
160 *dest
++ = (GLfloat
) rgba
[i
][ACOMP
] * (1.0F
/ CHAN_MAXF
);
165 /* restore default src/dst buffer */
166 _swrast_use_draw_buffer(ctx
);
169 /* do the image transfer ops which preceed convolution */
170 for (row
= 0; row
< height
; row
++) {
171 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) (tmpImage
+ row
* width
* 4);
172 _mesa_apply_rgba_transfer_ops(ctx
,
173 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
,
178 if (ctx
->Pixel
.Convolution2DEnabled
) {
179 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
182 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
183 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
187 /* do remaining post-convolution image transfer ops */
188 for (row
= 0; row
< height
; row
++) {
189 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) (convImage
+ row
* width
* 4);
190 _mesa_apply_rgba_transfer_ops(ctx
,
191 transferOps
& IMAGE_POST_CONVOLUTION_BITS
,
195 /* write the new image */
196 for (row
= 0; row
< height
; row
++) {
197 const GLfloat
*src
= convImage
+ row
* width
* 4;
200 /* clamp to [0,1] and convert float back to chan */
201 for (i
= 0; i
< width
; i
++) {
202 GLint r
= (GLint
) (src
[i
* 4 + RCOMP
] * CHAN_MAXF
);
203 GLint g
= (GLint
) (src
[i
* 4 + GCOMP
] * CHAN_MAXF
);
204 GLint b
= (GLint
) (src
[i
* 4 + BCOMP
] * CHAN_MAXF
);
205 GLint a
= (GLint
) (src
[i
* 4 + ACOMP
] * CHAN_MAXF
);
206 span
.array
->rgba
[i
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
207 span
.array
->rgba
[i
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
208 span
.array
->rgba
[i
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
209 span
.array
->rgba
[i
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
212 if (ctx
->Pixel
.PixelTextureEnabled
&& ctx
->Texture
._EnabledUnits
) {
214 _swrast_pixel_texture(ctx
, &span
);
217 /* write row to framebuffer */
220 if (quick_draw
&& dy
>= 0 && dy
< (GLint
) ctx
->DrawBuffer
->Height
) {
221 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
222 (const GLchan (*)[4])span
.array
->rgba
, NULL
);
228 _swrast_write_zoomed_rgba_span(ctx
, &span
,
229 (CONST
GLchan (*)[4])span
.array
->rgba
,
236 _swrast_write_rgba_span(ctx
, &span
);
248 copy_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
249 GLint width
, GLint height
, GLint destx
, GLint desty
)
251 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
253 GLboolean quick_draw
;
254 GLint sy
, dy
, stepy
, j
;
255 GLboolean changeBuffer
;
256 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
258 const GLuint transferOps
= ctx
->_ImageTransferState
;
261 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_RGBA
);
263 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
264 copy_conv_rgba_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
268 /* Determine if copy should be done bottom-to-top or top-to-bottom */
270 /* top-down max-to-min */
271 sy
= srcy
+ height
- 1;
272 dy
= desty
+ height
- 1;
276 /* bottom-up min-to-max */
282 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
283 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
284 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
287 overlapping
= GL_FALSE
;
291 _swrast_span_default_z(ctx
, &span
);
292 if (ctx
->Fog
.Enabled
)
293 _swrast_span_default_fog(ctx
, &span
);
295 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
298 && destx
+ width
<= (GLint
) ctx
->DrawBuffer
->Width
) {
299 quick_draw
= GL_TRUE
;
302 quick_draw
= GL_FALSE
;
305 /* If read and draw buffer are different we must do buffer switching */
306 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
307 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
311 tmpImage
= (GLchan
*) MALLOC(width
* height
* sizeof(GLchan
) * 4);
313 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
318 _swrast_use_read_buffer(ctx
);
319 /* read the source image */
321 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
322 _swrast_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
,
329 _swrast_use_draw_buffer(ctx
);
330 changeBuffer
= GL_FALSE
;
334 tmpImage
= NULL
; /* silence compiler warnings */
338 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
339 /* Get source pixels */
341 /* get from buffered image */
342 ASSERT(width
< MAX_WIDTH
);
343 MEMCPY(span
.array
->rgba
, p
, width
* sizeof(GLchan
) * 4);
347 /* get from framebuffer */
349 _swrast_use_read_buffer(ctx
);
350 ASSERT(width
< MAX_WIDTH
);
351 _swrast_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
,
354 _swrast_use_draw_buffer(ctx
);
358 const GLfloat scale
= (1.0F
/ CHAN_MAXF
);
360 DEFMARRAY(GLfloat
, rgbaFloat
, MAX_WIDTH
, 4); /* mac 32k limitation */
361 CHECKARRAY(rgbaFloat
, return);
363 /* convert chan to float */
364 for (k
= 0; k
< width
; k
++) {
365 rgbaFloat
[k
][RCOMP
] = (GLfloat
) span
.array
->rgba
[k
][RCOMP
] * scale
;
366 rgbaFloat
[k
][GCOMP
] = (GLfloat
) span
.array
->rgba
[k
][GCOMP
] * scale
;
367 rgbaFloat
[k
][BCOMP
] = (GLfloat
) span
.array
->rgba
[k
][BCOMP
] * scale
;
368 rgbaFloat
[k
][ACOMP
] = (GLfloat
) span
.array
->rgba
[k
][ACOMP
] * scale
;
371 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, width
, rgbaFloat
);
373 /* clamp to [0,1] and convert float back to chan */
374 for (k
= 0; k
< width
; k
++) {
375 GLint r
= (GLint
) (rgbaFloat
[k
][RCOMP
] * CHAN_MAXF
);
376 GLint g
= (GLint
) (rgbaFloat
[k
][GCOMP
] * CHAN_MAXF
);
377 GLint b
= (GLint
) (rgbaFloat
[k
][BCOMP
] * CHAN_MAXF
);
378 GLint a
= (GLint
) (rgbaFloat
[k
][ACOMP
] * CHAN_MAXF
);
379 span
.array
->rgba
[k
][RCOMP
] = (GLchan
) CLAMP(r
, 0, CHAN_MAX
);
380 span
.array
->rgba
[k
][GCOMP
] = (GLchan
) CLAMP(g
, 0, CHAN_MAX
);
381 span
.array
->rgba
[k
][BCOMP
] = (GLchan
) CLAMP(b
, 0, CHAN_MAX
);
382 span
.array
->rgba
[k
][ACOMP
] = (GLchan
) CLAMP(a
, 0, CHAN_MAX
);
384 UNDEFARRAY(rgbaFloat
); /* mac 32k limitation */
387 if (ctx
->Pixel
.PixelTextureEnabled
&& ctx
->Texture
._EnabledUnits
) {
389 _swrast_pixel_texture(ctx
, &span
);
392 /* Write color span */
393 if (quick_draw
&& dy
>= 0 && dy
< (GLint
) ctx
->DrawBuffer
->Height
) {
394 (*swrast
->Driver
.WriteRGBASpan
)( ctx
, width
, destx
, dy
,
395 (const GLchan (*)[4])span
.array
->rgba
, NULL
);
401 _swrast_write_zoomed_rgba_span(ctx
, &span
,
402 (CONST
GLchan (*)[4]) span
.array
->rgba
,
409 _swrast_write_rgba_span(ctx
, &span
);
419 copy_ci_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
420 GLint width
, GLint height
,
421 GLint destx
, GLint desty
)
426 GLboolean changeBuffer
;
427 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
428 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
432 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_INDEX
);
434 /* Determine if copy should be bottom-to-top or top-to-bottom */
436 /* top-down max-to-min */
437 sy
= srcy
+ height
- 1;
438 dy
= desty
+ height
- 1;
442 /* bottom-up min-to-max */
448 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
449 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
450 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
453 overlapping
= GL_FALSE
;
457 _swrast_span_default_z(ctx
, &span
);
458 if (ctx
->Fog
.Enabled
)
459 _swrast_span_default_fog(ctx
, &span
);
461 /* If read and draw buffer are different we must do buffer switching */
462 changeBuffer
= ctx
->Pixel
.ReadBuffer
!= ctx
->Color
.DrawBuffer
463 || ctx
->DrawBuffer
!= ctx
->ReadBuffer
;
467 tmpImage
= (GLuint
*) MALLOC(width
* height
* sizeof(GLuint
));
469 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
474 _swrast_use_read_buffer(ctx
);
477 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
478 _swrast_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
, p
);
482 /* restore to draw buffer */
484 _swrast_use_draw_buffer(ctx
);
485 changeBuffer
= GL_FALSE
;
489 tmpImage
= NULL
; /* silence compiler warning */
493 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
494 /* Get color indexes */
496 MEMCPY(span
.array
->index
, p
, width
* sizeof(GLuint
));
501 _swrast_use_read_buffer(ctx
);
502 _swrast_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
,
505 _swrast_use_draw_buffer(ctx
);
508 /* Apply shift, offset, look-up table */
509 if (shift_or_offset
) {
510 _mesa_shift_and_offset_ci( ctx
, width
, span
.array
->index
);
512 if (ctx
->Pixel
.MapColorFlag
) {
513 _mesa_map_ci( ctx
, width
, span
.array
->index
);
516 /* write color indexes */
521 _swrast_write_zoomed_index_span(ctx
, &span
, desty
, 0);
523 _swrast_write_index_span(ctx
, &span
);
536 copy_depth_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
537 GLint width
, GLint height
,
538 GLint destx
, GLint desty
)
540 GLfloat
*p
, *tmpImage
;
543 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
547 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_Z
);
549 if (!ctx
->Visual
.depthBits
) {
550 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
554 /* Determine if copy should be bottom-to-top or top-to-bottom */
556 /* top-down max-to-min */
557 sy
= srcy
+ height
- 1;
558 dy
= desty
+ height
- 1;
562 /* bottom-up min-to-max */
568 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
569 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
570 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
573 overlapping
= GL_FALSE
;
576 _swrast_span_default_color(ctx
, &span
);
577 if (ctx
->Fog
.Enabled
)
578 _swrast_span_default_fog(ctx
, &span
);
582 tmpImage
= (GLfloat
*) MALLOC(width
* height
* sizeof(GLfloat
));
584 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
588 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
589 _swrast_read_depth_span_float(ctx
, width
, srcx
, ssy
, p
);
595 tmpImage
= NULL
; /* silence compiler warning */
599 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
600 GLfloat depth
[MAX_WIDTH
];
602 /* get depth values */
604 MEMCPY(depth
, p
, width
* sizeof(GLfloat
));
608 _swrast_read_depth_span_float(ctx
, width
, srcx
, sy
, depth
);
611 /* apply scale and bias */
612 for (i
= 0; i
< width
; i
++) {
613 GLfloat d
= depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
614 span
.array
->z
[i
] = (GLdepth
) (CLAMP(d
, 0.0F
, 1.0F
) * ctx
->DepthMax
);
617 /* write depth values */
621 if (ctx
->Visual
.rgbMode
) {
623 _swrast_write_zoomed_rgba_span( ctx
, &span
,
624 (const GLchan (*)[4])span
.array
->rgba
, desty
, 0 );
626 _swrast_write_rgba_span(ctx
, &span
);
630 _swrast_write_zoomed_index_span( ctx
, &span
, desty
, 0 );
632 _swrast_write_index_span(ctx
, &span
);
643 copy_stencil_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
644 GLint width
, GLint height
,
645 GLint destx
, GLint desty
)
649 GLstencil
*p
, *tmpImage
;
650 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
651 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
654 if (!ctx
->Visual
.stencilBits
) {
655 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
659 /* Determine if copy should be bottom-to-top or top-to-bottom */
661 /* top-down max-to-min */
662 sy
= srcy
+ height
- 1;
663 dy
= desty
+ height
- 1;
667 /* bottom-up min-to-max */
673 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
674 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
675 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
678 overlapping
= GL_FALSE
;
683 tmpImage
= (GLstencil
*) MALLOC(width
* height
* sizeof(GLstencil
));
685 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
689 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
690 _swrast_read_stencil_span( ctx
, width
, srcx
, ssy
, p
);
696 tmpImage
= NULL
; /* silence compiler warning */
700 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
701 GLstencil stencil
[MAX_WIDTH
];
703 /* Get stencil values */
705 MEMCPY(stencil
, p
, width
* sizeof(GLstencil
));
709 _swrast_read_stencil_span( ctx
, width
, srcx
, sy
, stencil
);
712 /* Apply shift, offset, look-up table */
713 if (shift_or_offset
) {
714 _mesa_shift_and_offset_stencil( ctx
, width
, stencil
);
716 if (ctx
->Pixel
.MapStencilFlag
) {
717 _mesa_map_stencil( ctx
, width
, stencil
);
720 /* Write stencil values */
722 _swrast_write_zoomed_stencil_span( ctx
, width
, destx
, dy
,
726 _swrast_write_stencil_span( ctx
, width
, destx
, dy
, stencil
);
737 _swrast_CopyPixels( GLcontext
*ctx
,
738 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
739 GLint destx
, GLint desty
,
742 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
743 RENDER_START(swrast
,ctx
);
745 if (swrast
->NewState
)
746 _swrast_validate_derived( ctx
);
748 if (type
== GL_COLOR
&& ctx
->Visual
.rgbMode
) {
749 copy_rgba_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
751 else if (type
== GL_COLOR
&& !ctx
->Visual
.rgbMode
) {
752 copy_ci_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
754 else if (type
== GL_DEPTH
) {
755 copy_depth_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
757 else if (type
== GL_STENCIL
) {
758 copy_stencil_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
761 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyPixels" );
764 RENDER_FINISH(swrast
,ctx
);