2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 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.
30 #include "histogram.h"
36 #include "s_context.h"
38 #include "s_pixeltex.h"
40 #include "s_stencil.h"
41 #include "s_texture.h"
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.
53 regions_overlap(GLint srcx
, GLint srcy
,
54 GLint dstx
, GLint dsty
,
55 GLint width
, GLint height
,
56 GLfloat zoomX
, GLfloat zoomY
)
58 if (zoomX
== 1.0 && zoomY
== 1.0) {
60 if (srcx
>= dstx
+ width
|| (srcx
+ width
<= dstx
)) {
63 else if (srcy
< dsty
) { /* this is OK */
66 else if (srcy
> dsty
+ height
) {
74 /* add one pixel of slop when zooming, just to be safe */
75 if ((srcx
> dstx
+ (width
* zoomX
) + 1) || (srcx
+ width
+ 1 < dstx
)) {
78 else if ((srcy
< dsty
) && (srcy
+ height
< dsty
+ (height
* zoomY
))) {
81 else if ((srcy
> dsty
) && (srcy
+ height
> dsty
+ (height
* zoomY
))) {
92 * Convert GLfloat[n][4] colors to GLchan[n][4].
93 * XXX maybe move into image.c
96 float_span_to_chan(GLuint n
, CONST GLfloat in
[][4], GLchan out
[][4])
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
]);
109 * Convert GLchan[n][4] colors to GLfloat[n][4].
110 * XXX maybe move into image.c
113 chan_span_to_float(GLuint n
, CONST GLchan in
[][4], GLfloat out
[][4])
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
]);
127 * RGBA copypixels with convolution.
130 copy_conv_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
131 GLint width
, GLint height
, GLint destx
, GLint desty
)
133 struct gl_renderbuffer
*drawRb
= NULL
;
134 GLboolean quick_draw
;
136 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
137 const GLuint transferOps
= ctx
->_ImageTransferState
;
138 GLfloat
*dest
, *tmpImage
, *convImage
;
141 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_RGBA
);
144 _swrast_span_default_z(ctx
, &span
);
145 if (ctx
->Fog
.Enabled
)
146 _swrast_span_default_fog(ctx
, &span
);
149 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
152 && destx
+ width
<= (GLint
) ctx
->DrawBuffer
->Width
) {
153 quick_draw
= GL_TRUE
;
154 drawRb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
157 quick_draw
= GL_FALSE
;
160 /* allocate space for GLfloat image */
161 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
163 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
166 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
169 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
173 /* read source image */
175 for (row
= 0; row
< height
; row
++) {
176 GLchan rgba
[MAX_WIDTH
][4];
177 /* Read GLchan and convert to GLfloat */
178 _swrast_read_rgba_span(ctx
, ctx
->ReadBuffer
->_ColorReadBuffer
,
179 width
, srcx
, srcy
+ row
, rgba
);
180 chan_span_to_float(width
, (CONST
GLchan (*)[4]) rgba
,
181 (GLfloat (*)[4]) dest
);
185 /* do the image transfer ops which preceed convolution */
186 for (row
= 0; row
< height
; row
++) {
187 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) (tmpImage
+ row
* width
* 4);
188 _mesa_apply_rgba_transfer_ops(ctx
,
189 transferOps
& IMAGE_PRE_CONVOLUTION_BITS
,
194 if (ctx
->Pixel
.Convolution2DEnabled
) {
195 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
198 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
199 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
203 /* do remaining post-convolution image transfer ops */
204 for (row
= 0; row
< height
; row
++) {
205 GLfloat (*rgba
)[4] = (GLfloat (*)[4]) (convImage
+ row
* width
* 4);
206 _mesa_apply_rgba_transfer_ops(ctx
,
207 transferOps
& IMAGE_POST_CONVOLUTION_BITS
,
211 /* write the new image */
212 for (row
= 0; row
< height
; row
++) {
213 const GLfloat
*src
= convImage
+ row
* width
* 4;
216 /* convert floats back to chan */
217 float_span_to_chan(width
, (const GLfloat (*)[4]) src
, span
.array
->rgba
);
219 if (ctx
->Pixel
.PixelTextureEnabled
&& ctx
->Texture
._EnabledUnits
) {
221 _swrast_pixel_texture(ctx
, &span
);
224 /* write row to framebuffer */
227 if (quick_draw
&& dy
>= 0 && dy
< (GLint
) ctx
->DrawBuffer
->Height
) {
228 drawRb
->PutRow(ctx
, drawRb
, width
, destx
, dy
, span
.array
->rgba
, NULL
);
234 _swrast_write_zoomed_rgba_span(ctx
, &span
,
235 (CONST
GLchan (*)[4])span
.array
->rgba
,
242 _swrast_write_rgba_span(ctx
, &span
);
254 copy_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
255 GLint width
, GLint height
, GLint destx
, GLint desty
)
257 struct gl_renderbuffer
*drawRb
;
259 GLboolean quick_draw
;
260 GLint sy
, dy
, stepy
, j
;
261 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
263 const GLuint transferOps
= ctx
->_ImageTransferState
;
266 if (!ctx
->ReadBuffer
->_ColorReadBuffer
) {
267 /* no readbuffer - OK */
271 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_RGBA
);
273 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
274 copy_conv_rgba_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
278 /* Determine if copy should be done bottom-to-top or top-to-bottom */
280 /* top-down max-to-min */
281 sy
= srcy
+ height
- 1;
282 dy
= desty
+ height
- 1;
286 /* bottom-up min-to-max */
292 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
293 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
294 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
297 overlapping
= GL_FALSE
;
301 _swrast_span_default_z(ctx
, &span
);
302 if (ctx
->Fog
.Enabled
)
303 _swrast_span_default_fog(ctx
, &span
);
305 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
308 && destx
+ width
<= (GLint
) ctx
->DrawBuffer
->Width
) {
309 quick_draw
= GL_TRUE
;
310 drawRb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
313 quick_draw
= GL_FALSE
;
319 tmpImage
= (GLchan
*) MALLOC(width
* height
* sizeof(GLchan
) * 4);
321 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
324 /* read the source image */
326 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
327 _swrast_read_rgba_span( ctx
, ctx
->ReadBuffer
->_ColorReadBuffer
,
328 width
, srcx
, ssy
, (GLchan (*)[4]) p
);
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 */
348 ASSERT(width
< MAX_WIDTH
);
349 _swrast_read_rgba_span( ctx
, ctx
->ReadBuffer
->_ColorReadBuffer
,
350 width
, srcx
, sy
, span
.array
->rgba
);
354 GLfloat rgbaFloat
[MAX_WIDTH
][4];
355 /* convert to float, transfer, convert back to chan */
356 chan_span_to_float(width
, (CONST
GLchan (*)[4]) span
.array
->rgba
,
358 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, width
, rgbaFloat
);
359 float_span_to_chan(width
, (CONST
GLfloat (*)[4]) rgbaFloat
,
363 if (ctx
->Pixel
.PixelTextureEnabled
&& ctx
->Texture
._EnabledUnits
) {
365 _swrast_pixel_texture(ctx
, &span
);
368 /* Write color span */
369 if (quick_draw
&& dy
>= 0 && dy
< (GLint
) ctx
->DrawBuffer
->Height
) {
370 drawRb
->PutRow(ctx
, drawRb
, width
, destx
, dy
, span
.array
->rgba
, NULL
);
376 _swrast_write_zoomed_rgba_span(ctx
, &span
,
377 (CONST
GLchan (*)[4]) span
.array
->rgba
,
384 _swrast_write_rgba_span(ctx
, &span
);
394 copy_ci_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
395 GLint width
, GLint height
,
396 GLint destx
, GLint desty
)
401 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
402 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
406 if (!ctx
->ReadBuffer
->_ColorReadBuffer
) {
407 /* no readbuffer - OK */
411 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_INDEX
);
413 /* Determine if copy should be bottom-to-top or top-to-bottom */
415 /* top-down max-to-min */
416 sy
= srcy
+ height
- 1;
417 dy
= desty
+ height
- 1;
421 /* bottom-up min-to-max */
427 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
428 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
429 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
432 overlapping
= GL_FALSE
;
436 _swrast_span_default_z(ctx
, &span
);
437 if (ctx
->Fog
.Enabled
)
438 _swrast_span_default_fog(ctx
, &span
);
442 tmpImage
= (GLuint
*) MALLOC(width
* height
* sizeof(GLuint
));
444 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
449 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
450 _swrast_read_index_span( ctx
, ctx
->ReadBuffer
->_ColorReadBuffer
,
451 width
, srcx
, ssy
, p
);
457 tmpImage
= NULL
; /* silence compiler warning */
461 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
462 /* Get color indexes */
464 MEMCPY(span
.array
->index
, p
, width
* sizeof(GLuint
));
468 _swrast_read_index_span( ctx
, ctx
->ReadBuffer
->_ColorReadBuffer
,
469 width
, srcx
, sy
, span
.array
->index
);
472 /* Apply shift, offset, look-up table */
473 if (shift_or_offset
) {
474 _mesa_shift_and_offset_ci( ctx
, width
, span
.array
->index
);
476 if (ctx
->Pixel
.MapColorFlag
) {
477 _mesa_map_ci( ctx
, width
, span
.array
->index
);
480 /* write color indexes */
485 _swrast_write_zoomed_index_span(ctx
, &span
, desty
, 0);
487 _swrast_write_index_span(ctx
, &span
);
500 copy_depth_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
501 GLint width
, GLint height
,
502 GLint destx
, GLint desty
)
504 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
505 struct gl_renderbuffer
*readRb
506 = ctx
->ReadBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
507 GLfloat
*p
, *tmpImage
;
510 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
515 /* no readbuffer - OK */
519 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_Z
);
521 if (!ctx
->Visual
.depthBits
) {
522 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
526 /* Determine if copy should be bottom-to-top or top-to-bottom */
528 /* top-down max-to-min */
529 sy
= srcy
+ height
- 1;
530 dy
= desty
+ height
- 1;
534 /* bottom-up min-to-max */
540 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
541 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
542 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
545 overlapping
= GL_FALSE
;
548 _swrast_span_default_color(ctx
, &span
);
549 if (ctx
->Fog
.Enabled
)
550 _swrast_span_default_fog(ctx
, &span
);
554 tmpImage
= (GLfloat
*) MALLOC(width
* height
* sizeof(GLfloat
));
556 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
560 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
561 _swrast_read_depth_span_float(ctx
, readRb
, width
, srcx
, ssy
, p
);
567 tmpImage
= NULL
; /* silence compiler warning */
571 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
572 GLfloat depth
[MAX_WIDTH
];
574 /* get depth values */
576 MEMCPY(depth
, p
, width
* sizeof(GLfloat
));
580 _swrast_read_depth_span_float(ctx
, readRb
, width
, srcx
, sy
, depth
);
583 /* apply scale and bias */
584 for (i
= 0; i
< width
; i
++) {
585 GLfloat d
= depth
[i
] * ctx
->Pixel
.DepthScale
+ ctx
->Pixel
.DepthBias
;
587 span
.array
->z
[i
] = (GLdepth
) (CLAMP(d
, 0.0F
, 1.0F
) * depthMax
);
590 /* write depth values */
594 if (ctx
->Visual
.rgbMode
) {
596 _swrast_write_zoomed_rgba_span( ctx
, &span
,
597 (const GLchan (*)[4])span
.array
->rgba
, desty
, 0 );
599 _swrast_write_rgba_span(ctx
, &span
);
603 _swrast_write_zoomed_index_span( ctx
, &span
, desty
, 0 );
605 _swrast_write_index_span(ctx
, &span
);
616 copy_stencil_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
617 GLint width
, GLint height
,
618 GLint destx
, GLint desty
)
620 struct gl_renderbuffer
*rb
621 = ctx
->ReadBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
;
624 GLstencil
*p
, *tmpImage
;
625 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
626 const GLboolean shift_or_offset
= ctx
->Pixel
.IndexShift
|| ctx
->Pixel
.IndexOffset
;
629 if (!ctx
->Visual
.stencilBits
) {
630 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
635 /* no readbuffer - OK */
639 /* Determine if copy should be bottom-to-top or top-to-bottom */
641 /* top-down max-to-min */
642 sy
= srcy
+ height
- 1;
643 dy
= desty
+ height
- 1;
647 /* bottom-up min-to-max */
653 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
654 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
655 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
658 overlapping
= GL_FALSE
;
663 tmpImage
= (GLstencil
*) MALLOC(width
* height
* sizeof(GLstencil
));
665 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
669 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
670 _swrast_read_stencil_span( ctx
, rb
, width
, srcx
, ssy
, p
);
676 tmpImage
= NULL
; /* silence compiler warning */
680 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
681 GLstencil stencil
[MAX_WIDTH
];
683 /* Get stencil values */
685 MEMCPY(stencil
, p
, width
* sizeof(GLstencil
));
689 _swrast_read_stencil_span( ctx
, rb
, width
, srcx
, sy
, stencil
);
692 /* Apply shift, offset, look-up table */
693 if (shift_or_offset
) {
694 _mesa_shift_and_offset_stencil( ctx
, width
, stencil
);
696 if (ctx
->Pixel
.MapStencilFlag
) {
697 _mesa_map_stencil( ctx
, width
, stencil
);
700 /* Write stencil values */
702 _swrast_write_zoomed_stencil_span( ctx
, width
, destx
, dy
,
706 _swrast_write_stencil_span( ctx
, width
, destx
, dy
, stencil
);
717 _swrast_CopyPixels( GLcontext
*ctx
,
718 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
719 GLint destx
, GLint desty
,
722 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
723 RENDER_START(swrast
,ctx
);
725 if (swrast
->NewState
)
726 _swrast_validate_derived( ctx
);
728 if (type
== GL_COLOR
&& ctx
->Visual
.rgbMode
) {
729 copy_rgba_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
731 else if (type
== GL_COLOR
&& !ctx
->Visual
.rgbMode
) {
732 copy_ci_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
734 else if (type
== GL_DEPTH
) {
735 copy_depth_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
737 else if (type
== GL_STENCIL
) {
738 copy_stencil_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
741 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyPixels" );
744 RENDER_FINISH(swrast
,ctx
);