Merge branch 'master' into gallium-0.2
[mesa.git] / src / mesa / swrast / s_imaging.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
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 /* KW: Moved these here to remove knowledge of swrast from core mesa.
26 * Should probably pull the entire software implementation of these
27 * extensions into either swrast or a sister module.
28 */
29
30 #include "main/glheader.h"
31 #include "main/colortab.h"
32 #include "main/convolve.h"
33 #include "s_context.h"
34 #include "s_span.h"
35
36
37 void
38 _swrast_CopyColorTable( GLcontext *ctx,
39 GLenum target, GLenum internalformat,
40 GLint x, GLint y, GLsizei width)
41 {
42 SWcontext *swrast = SWRAST_CONTEXT(ctx);
43 GLchan data[MAX_WIDTH][4];
44 struct gl_buffer_object *bufferSave;
45
46 if (!ctx->ReadBuffer->_ColorReadBuffer) {
47 /* no readbuffer - OK */
48 return;
49 }
50
51 if (width > MAX_WIDTH)
52 width = MAX_WIDTH;
53
54 RENDER_START( swrast, ctx );
55
56 /* read the data from framebuffer */
57 _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
58 width, x, y, CHAN_TYPE, data );
59
60 RENDER_FINISH(swrast,ctx);
61
62 /* save PBO binding */
63 bufferSave = ctx->Unpack.BufferObj;
64 ctx->Unpack.BufferObj = ctx->Array.NullBufferObj;
65
66 _mesa_ColorTable(target, internalformat, width, GL_RGBA, CHAN_TYPE, data);
67
68 /* restore PBO binding */
69 ctx->Unpack.BufferObj = bufferSave;
70 }
71
72
73 void
74 _swrast_CopyColorSubTable( GLcontext *ctx,GLenum target, GLsizei start,
75 GLint x, GLint y, GLsizei width)
76 {
77 SWcontext *swrast = SWRAST_CONTEXT(ctx);
78 GLchan data[MAX_WIDTH][4];
79 struct gl_buffer_object *bufferSave;
80
81 if (!ctx->ReadBuffer->_ColorReadBuffer) {
82 /* no readbuffer - OK */
83 return;
84 }
85
86 if (width > MAX_WIDTH)
87 width = MAX_WIDTH;
88
89 RENDER_START( swrast, ctx );
90
91 /* read the data from framebuffer */
92 _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
93 width, x, y, CHAN_TYPE, data );
94
95 RENDER_FINISH(swrast,ctx);
96
97 /* save PBO binding */
98 bufferSave = ctx->Unpack.BufferObj;
99 ctx->Unpack.BufferObj = ctx->Array.NullBufferObj;
100
101 _mesa_ColorSubTable(target, start, width, GL_RGBA, CHAN_TYPE, data);
102
103 /* restore PBO binding */
104 ctx->Unpack.BufferObj = bufferSave;
105 }
106
107
108 void
109 _swrast_CopyConvolutionFilter1D(GLcontext *ctx, GLenum target,
110 GLenum internalFormat,
111 GLint x, GLint y, GLsizei width)
112 {
113 SWcontext *swrast = SWRAST_CONTEXT(ctx);
114 GLchan rgba[MAX_CONVOLUTION_WIDTH][4];
115 struct gl_buffer_object *bufferSave;
116
117 if (!ctx->ReadBuffer->_ColorReadBuffer) {
118 /* no readbuffer - OK */
119 return;
120 }
121
122 RENDER_START( swrast, ctx );
123
124 /* read the data from framebuffer */
125 _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
126 width, x, y, CHAN_TYPE, rgba );
127
128 RENDER_FINISH( swrast, ctx );
129
130 /* save PBO binding */
131 bufferSave = ctx->Unpack.BufferObj;
132 ctx->Unpack.BufferObj = ctx->Array.NullBufferObj;
133
134 /* store as convolution filter */
135 _mesa_ConvolutionFilter1D(target, internalFormat, width,
136 GL_RGBA, CHAN_TYPE, rgba);
137
138 /* restore PBO binding */
139 ctx->Unpack.BufferObj = bufferSave;
140 }
141
142
143 void
144 _swrast_CopyConvolutionFilter2D(GLcontext *ctx, GLenum target,
145 GLenum internalFormat,
146 GLint x, GLint y, GLsizei width, GLsizei height)
147 {
148 SWcontext *swrast = SWRAST_CONTEXT(ctx);
149 struct gl_pixelstore_attrib packSave;
150 GLchan rgba[MAX_CONVOLUTION_HEIGHT][MAX_CONVOLUTION_WIDTH][4];
151 GLint i;
152 struct gl_buffer_object *bufferSave;
153
154 if (!ctx->ReadBuffer->_ColorReadBuffer) {
155 /* no readbuffer - OK */
156 return;
157 }
158
159 RENDER_START(swrast,ctx);
160
161 /* read pixels from framebuffer */
162 for (i = 0; i < height; i++) {
163 _swrast_read_rgba_span( ctx, ctx->ReadBuffer->_ColorReadBuffer,
164 width, x, y + i, CHAN_TYPE, rgba[i] );
165 }
166
167 RENDER_FINISH(swrast,ctx);
168
169 /*
170 * HACK: save & restore context state so we can store this as a
171 * convolution filter via the GL api. Doesn't call any callbacks
172 * hanging off ctx->Unpack statechanges.
173 */
174
175 packSave = ctx->Unpack; /* save pixel packing params */
176
177 ctx->Unpack.Alignment = 1;
178 ctx->Unpack.RowLength = MAX_CONVOLUTION_WIDTH;
179 ctx->Unpack.SkipPixels = 0;
180 ctx->Unpack.SkipRows = 0;
181 ctx->Unpack.ImageHeight = 0;
182 ctx->Unpack.SkipImages = 0;
183 ctx->Unpack.SwapBytes = GL_FALSE;
184 ctx->Unpack.LsbFirst = GL_FALSE;
185 ctx->Unpack.BufferObj = ctx->Array.NullBufferObj;
186 ctx->NewState |= _NEW_PACKUNPACK;
187
188 /* save PBO binding */
189 bufferSave = ctx->Unpack.BufferObj;
190 ctx->Unpack.BufferObj = ctx->Array.NullBufferObj;
191
192 _mesa_ConvolutionFilter2D(target, internalFormat, width, height,
193 GL_RGBA, CHAN_TYPE, rgba);
194
195 /* restore PBO binding */
196 ctx->Unpack.BufferObj = bufferSave;
197
198 ctx->Unpack = packSave; /* restore pixel packing params */
199 ctx->NewState |= _NEW_PACKUNPACK;
200 }