2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
134 GLboolean quick_draw
;
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
;
142 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_RGBA
);
145 _swrast_span_default_z(ctx
, &span
);
146 if (ctx
->Fog
.Enabled
)
147 _swrast_span_default_fog(ctx
, &span
);
150 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
153 && destx
+ width
<= (GLint
) ctx
->DrawBuffer
->Width
) {
154 quick_draw
= GL_TRUE
;
157 quick_draw
= GL_FALSE
;
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
;
165 /* allocate space for GLfloat image */
166 tmpImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
168 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
171 convImage
= (GLfloat
*) MALLOC(width
* height
* 4 * sizeof(GLfloat
));
174 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels");
179 /* choose the read buffer */
180 _swrast_use_read_buffer(ctx
);
183 /* read source image */
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
,
190 chan_span_to_float(width
, (CONST
GLchan (*)[4]) rgba
,
191 (GLfloat (*)[4]) dest
);
196 /* restore default src/dst buffer */
197 _swrast_use_draw_buffer(ctx
);
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
,
209 if (ctx
->Pixel
.Convolution2DEnabled
) {
210 _mesa_convolve_2d_image(ctx
, &width
, &height
, tmpImage
, convImage
);
213 ASSERT(ctx
->Pixel
.Separable2DEnabled
);
214 _mesa_convolve_sep_image(ctx
, &width
, &height
, tmpImage
, convImage
);
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
,
226 /* write the new image */
227 for (row
= 0; row
< height
; row
++) {
228 const GLfloat
*src
= convImage
+ row
* width
* 4;
231 /* convert floats back to chan */
232 float_span_to_chan(width
, (const GLfloat (*)[4]) src
, span
.array
->rgba
);
234 if (ctx
->Pixel
.PixelTextureEnabled
&& ctx
->Texture
._EnabledUnits
) {
236 _swrast_pixel_texture(ctx
, &span
);
239 /* write row to framebuffer */
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
);
250 _swrast_write_zoomed_rgba_span(ctx
, &span
,
251 (CONST
GLchan (*)[4])span
.array
->rgba
,
258 _swrast_write_rgba_span(ctx
, &span
);
270 copy_rgba_pixels(GLcontext
*ctx
, GLint srcx
, GLint srcy
,
271 GLint width
, GLint height
, GLint destx
, GLint desty
)
273 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
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
;
280 const GLuint transferOps
= ctx
->_ImageTransferState
;
283 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_RGBA
);
285 if (ctx
->Pixel
.Convolution2DEnabled
|| ctx
->Pixel
.Separable2DEnabled
) {
286 copy_conv_rgba_pixels(ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
290 /* Determine if copy should be done bottom-to-top or top-to-bottom */
292 /* top-down max-to-min */
293 sy
= srcy
+ height
- 1;
294 dy
= desty
+ height
- 1;
298 /* bottom-up min-to-max */
304 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
305 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
306 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
309 overlapping
= GL_FALSE
;
313 _swrast_span_default_z(ctx
, &span
);
314 if (ctx
->Fog
.Enabled
)
315 _swrast_span_default_fog(ctx
, &span
);
317 if (SWRAST_CONTEXT(ctx
)->_RasterMask
== 0
320 && destx
+ width
<= (GLint
) ctx
->DrawBuffer
->Width
) {
321 quick_draw
= GL_TRUE
;
324 quick_draw
= GL_FALSE
;
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
;
333 tmpImage
= (GLchan
*) MALLOC(width
* height
* sizeof(GLchan
) * 4);
335 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
340 _swrast_use_read_buffer(ctx
);
341 /* read the source image */
343 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
344 _swrast_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
,
351 _swrast_use_draw_buffer(ctx
);
352 changeBuffer
= GL_FALSE
;
356 tmpImage
= NULL
; /* silence compiler warnings */
360 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
361 /* Get source pixels */
363 /* get from buffered image */
364 ASSERT(width
< MAX_WIDTH
);
365 MEMCPY(span
.array
->rgba
, p
, width
* sizeof(GLchan
) * 4);
369 /* get from framebuffer */
371 _swrast_use_read_buffer(ctx
);
372 ASSERT(width
< MAX_WIDTH
);
373 _swrast_read_rgba_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
,
376 _swrast_use_draw_buffer(ctx
);
380 DEFMARRAY(GLfloat
, rgbaFloat
, MAX_WIDTH
, 4); /* mac 32k limitation */
381 CHECKARRAY(rgbaFloat
, return);
383 /* convert to float, transfer, convert back to chan */
384 chan_span_to_float(width
, (CONST
GLchan (*)[4]) span
.array
->rgba
,
386 _mesa_apply_rgba_transfer_ops(ctx
, transferOps
, width
, rgbaFloat
);
387 float_span_to_chan(width
, (CONST
GLfloat (*)[4]) rgbaFloat
,
390 UNDEFARRAY(rgbaFloat
); /* mac 32k limitation */
393 if (ctx
->Pixel
.PixelTextureEnabled
&& ctx
->Texture
._EnabledUnits
) {
395 _swrast_pixel_texture(ctx
, &span
);
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
);
407 _swrast_write_zoomed_rgba_span(ctx
, &span
,
408 (CONST
GLchan (*)[4]) span
.array
->rgba
,
415 _swrast_write_rgba_span(ctx
, &span
);
425 copy_ci_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
426 GLint width
, GLint height
,
427 GLint destx
, GLint desty
)
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
;
438 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_INDEX
);
440 /* Determine if copy should be bottom-to-top or top-to-bottom */
442 /* top-down max-to-min */
443 sy
= srcy
+ height
- 1;
444 dy
= desty
+ height
- 1;
448 /* bottom-up min-to-max */
454 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
455 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
456 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
459 overlapping
= GL_FALSE
;
463 _swrast_span_default_z(ctx
, &span
);
464 if (ctx
->Fog
.Enabled
)
465 _swrast_span_default_fog(ctx
, &span
);
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
;
473 tmpImage
= (GLuint
*) MALLOC(width
* height
* sizeof(GLuint
));
475 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
480 _swrast_use_read_buffer(ctx
);
483 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
484 _swrast_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, ssy
, p
);
488 /* restore to draw buffer */
490 _swrast_use_draw_buffer(ctx
);
491 changeBuffer
= GL_FALSE
;
495 tmpImage
= NULL
; /* silence compiler warning */
499 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
500 /* Get color indexes */
502 MEMCPY(span
.array
->index
, p
, width
* sizeof(GLuint
));
507 _swrast_use_read_buffer(ctx
);
508 _swrast_read_index_span( ctx
, ctx
->ReadBuffer
, width
, srcx
, sy
,
511 _swrast_use_draw_buffer(ctx
);
514 /* Apply shift, offset, look-up table */
515 if (shift_or_offset
) {
516 _mesa_shift_and_offset_ci( ctx
, width
, span
.array
->index
);
518 if (ctx
->Pixel
.MapColorFlag
) {
519 _mesa_map_ci( ctx
, width
, span
.array
->index
);
522 /* write color indexes */
527 _swrast_write_zoomed_index_span(ctx
, &span
, desty
, 0);
529 _swrast_write_index_span(ctx
, &span
);
542 copy_depth_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
543 GLint width
, GLint height
,
544 GLint destx
, GLint desty
)
546 GLfloat
*p
, *tmpImage
;
549 const GLboolean zoom
= ctx
->Pixel
.ZoomX
!= 1.0F
|| ctx
->Pixel
.ZoomY
!= 1.0F
;
553 INIT_SPAN(span
, GL_BITMAP
, 0, 0, SPAN_Z
);
555 if (!ctx
->Visual
.depthBits
) {
556 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
560 /* Determine if copy should be bottom-to-top or top-to-bottom */
562 /* top-down max-to-min */
563 sy
= srcy
+ height
- 1;
564 dy
= desty
+ height
- 1;
568 /* bottom-up min-to-max */
574 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
575 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
576 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
579 overlapping
= GL_FALSE
;
582 _swrast_span_default_color(ctx
, &span
);
583 if (ctx
->Fog
.Enabled
)
584 _swrast_span_default_fog(ctx
, &span
);
588 tmpImage
= (GLfloat
*) MALLOC(width
* height
* sizeof(GLfloat
));
590 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
594 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
595 _swrast_read_depth_span_float(ctx
, width
, srcx
, ssy
, p
);
601 tmpImage
= NULL
; /* silence compiler warning */
605 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
606 GLfloat depth
[MAX_WIDTH
];
608 /* get depth values */
610 MEMCPY(depth
, p
, width
* sizeof(GLfloat
));
614 _swrast_read_depth_span_float(ctx
, width
, srcx
, sy
, depth
);
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
);
623 /* write depth values */
627 if (ctx
->Visual
.rgbMode
) {
629 _swrast_write_zoomed_rgba_span( ctx
, &span
,
630 (const GLchan (*)[4])span
.array
->rgba
, desty
, 0 );
632 _swrast_write_rgba_span(ctx
, &span
);
636 _swrast_write_zoomed_index_span( ctx
, &span
, desty
, 0 );
638 _swrast_write_index_span(ctx
, &span
);
649 copy_stencil_pixels( GLcontext
*ctx
, GLint srcx
, GLint srcy
,
650 GLint width
, GLint height
,
651 GLint destx
, GLint desty
)
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
;
660 if (!ctx
->Visual
.stencilBits
) {
661 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glCopyPixels" );
665 /* Determine if copy should be bottom-to-top or top-to-bottom */
667 /* top-down max-to-min */
668 sy
= srcy
+ height
- 1;
669 dy
= desty
+ height
- 1;
673 /* bottom-up min-to-max */
679 if (ctx
->DrawBuffer
== ctx
->ReadBuffer
) {
680 overlapping
= regions_overlap(srcx
, srcy
, destx
, desty
, width
, height
,
681 ctx
->Pixel
.ZoomX
, ctx
->Pixel
.ZoomY
);
684 overlapping
= GL_FALSE
;
689 tmpImage
= (GLstencil
*) MALLOC(width
* height
* sizeof(GLstencil
));
691 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "glCopyPixels" );
695 for (j
= 0; j
< height
; j
++, ssy
+= stepy
) {
696 _swrast_read_stencil_span( ctx
, width
, srcx
, ssy
, p
);
702 tmpImage
= NULL
; /* silence compiler warning */
706 for (j
= 0; j
< height
; j
++, sy
+= stepy
, dy
+= stepy
) {
707 GLstencil stencil
[MAX_WIDTH
];
709 /* Get stencil values */
711 MEMCPY(stencil
, p
, width
* sizeof(GLstencil
));
715 _swrast_read_stencil_span( ctx
, width
, srcx
, sy
, stencil
);
718 /* Apply shift, offset, look-up table */
719 if (shift_or_offset
) {
720 _mesa_shift_and_offset_stencil( ctx
, width
, stencil
);
722 if (ctx
->Pixel
.MapStencilFlag
) {
723 _mesa_map_stencil( ctx
, width
, stencil
);
726 /* Write stencil values */
728 _swrast_write_zoomed_stencil_span( ctx
, width
, destx
, dy
,
732 _swrast_write_stencil_span( ctx
, width
, destx
, dy
, stencil
);
743 _swrast_CopyPixels( GLcontext
*ctx
,
744 GLint srcx
, GLint srcy
, GLsizei width
, GLsizei height
,
745 GLint destx
, GLint desty
,
748 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
749 RENDER_START(swrast
,ctx
);
751 if (swrast
->NewState
)
752 _swrast_validate_derived( ctx
);
754 if (type
== GL_COLOR
&& ctx
->Visual
.rgbMode
) {
755 copy_rgba_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
757 else if (type
== GL_COLOR
&& !ctx
->Visual
.rgbMode
) {
758 copy_ci_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
760 else if (type
== GL_DEPTH
) {
761 copy_depth_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
763 else if (type
== GL_STENCIL
) {
764 copy_stencil_pixels( ctx
, srcx
, srcy
, width
, height
, destx
, desty
);
767 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCopyPixels" );
770 RENDER_FINISH(swrast
,ctx
);