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