blob: bf6f6619d4fdb8526d2a3b5b8831e3be86e66399 [file] [log] [blame]
Brian Paulcc8e37f2000-07-12 13:00:09 +00001/*
2 * Mesa 3-D graphics library
Brian Paul176501d2006-10-13 16:34:25 +00003 * Version: 6.5.2
Brian Paulcc8e37f2000-07-12 13:00:09 +00004 *
Brian Paul176501d2006-10-13 16:34:25 +00005 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
Brian Paulcc8e37f2000-07-12 13:00:09 +00006 *
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
26/*
27 * Image convolution functions.
28 *
29 * Notes: filter kernel elements are indexed by <n> and <m> as in
30 * the GL spec.
31 */
32
33
Brian Paulcc8e37f2000-07-12 13:00:09 +000034#include "glheader.h"
Brian Paulbd3b40a2004-10-31 17:36:23 +000035#include "bufferobj.h"
Brian Paulc893a012000-10-28 20:41:13 +000036#include "colormac.h"
Brian Pauld4b799b2000-08-21 14:24:30 +000037#include "convolve.h"
38#include "context.h"
Brian Paul147b0832000-08-23 14:31:25 +000039#include "image.h"
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +000040#include "mtypes.h"
Brian Paul450e9172004-10-31 18:40:55 +000041#include "pixel.h"
Jon Taylorcdfba5d2000-11-23 02:50:56 +000042#include "state.h"
Brian Paulcc8e37f2000-07-12 13:00:09 +000043
44
Brian Paul147b0832000-08-23 14:31:25 +000045/*
46 * Given an internalFormat token passed to glConvolutionFilter
47 * or glSeparableFilter, return the corresponding base format.
48 * Return -1 if invalid token.
49 */
50static GLint
51base_filter_format( GLenum format )
52{
53 switch (format) {
54 case GL_ALPHA:
55 case GL_ALPHA4:
56 case GL_ALPHA8:
57 case GL_ALPHA12:
58 case GL_ALPHA16:
59 return GL_ALPHA;
60 case GL_LUMINANCE:
61 case GL_LUMINANCE4:
62 case GL_LUMINANCE8:
63 case GL_LUMINANCE12:
64 case GL_LUMINANCE16:
65 return GL_LUMINANCE;
66 case GL_LUMINANCE_ALPHA:
67 case GL_LUMINANCE4_ALPHA4:
68 case GL_LUMINANCE6_ALPHA2:
69 case GL_LUMINANCE8_ALPHA8:
70 case GL_LUMINANCE12_ALPHA4:
71 case GL_LUMINANCE12_ALPHA12:
72 case GL_LUMINANCE16_ALPHA16:
73 return GL_LUMINANCE_ALPHA;
74 case GL_INTENSITY:
75 case GL_INTENSITY4:
76 case GL_INTENSITY8:
77 case GL_INTENSITY12:
78 case GL_INTENSITY16:
79 return GL_INTENSITY;
80 case GL_RGB:
81 case GL_R3_G3_B2:
82 case GL_RGB4:
83 case GL_RGB5:
84 case GL_RGB8:
85 case GL_RGB10:
86 case GL_RGB12:
87 case GL_RGB16:
88 return GL_RGB;
89 case 4:
90 case GL_RGBA:
91 case GL_RGBA2:
92 case GL_RGBA4:
93 case GL_RGB5_A1:
94 case GL_RGBA8:
95 case GL_RGB10_A2:
96 case GL_RGBA12:
97 case GL_RGBA16:
98 return GL_RGBA;
99 default:
100 return -1; /* error */
101 }
102}
103
104
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000105void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000106_mesa_ConvolutionFilter1D(GLenum target, GLenum internalFormat, GLsizei width, GLenum format, GLenum type, const GLvoid *image)
107{
Brian Pauld0570642002-03-19 15:22:50 +0000108 GLint baseFormat;
Brian Paul147b0832000-08-23 14:31:25 +0000109 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000110 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000111
112 if (target != GL_CONVOLUTION_1D) {
Brian Paul08836342001-03-03 20:33:27 +0000113 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter1D(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000114 return;
115 }
116
117 baseFormat = base_filter_format(internalFormat);
118 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
Brian Paul08836342001-03-03 20:33:27 +0000119 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter1D(internalFormat)");
Brian Paul147b0832000-08-23 14:31:25 +0000120 return;
121 }
122
123 if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
Brian Paul08836342001-03-03 20:33:27 +0000124 _mesa_error(ctx, GL_INVALID_VALUE, "glConvolutionFilter1D(width)");
Brian Paul147b0832000-08-23 14:31:25 +0000125 return;
126 }
127
Brian Paulf959f6e2004-04-22 00:27:31 +0000128 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
Brian Paul08836342001-03-03 20:33:27 +0000129 _mesa_error(ctx, GL_INVALID_OPERATION, "glConvolutionFilter1D(format or type)");
Brian Paul90f042a2000-12-10 19:23:19 +0000130 return;
131 }
132
133 if (format == GL_COLOR_INDEX ||
Brian Paul147b0832000-08-23 14:31:25 +0000134 format == GL_STENCIL_INDEX ||
135 format == GL_DEPTH_COMPONENT ||
136 format == GL_INTENSITY ||
137 type == GL_BITMAP) {
Brian Paul08836342001-03-03 20:33:27 +0000138 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter1D(format or type)");
Brian Paul147b0832000-08-23 14:31:25 +0000139 return;
140 }
141
142 ctx->Convolution1D.Format = format;
143 ctx->Convolution1D.InternalFormat = internalFormat;
144 ctx->Convolution1D.Width = width;
145 ctx->Convolution1D.Height = 1;
146
Brian Paul95027a02009-09-03 11:23:05 -0600147 image = _mesa_map_validate_pbo_source(ctx,
148 1, &ctx->Unpack, width, 1, 1,
149 format, type, image,
150 "glConvolutionFilter1D");
151 if (!image)
Brian Paul203f3952009-09-03 10:41:14 -0600152 return;
Brian Paulbd3b40a2004-10-31 17:36:23 +0000153
Brian Paul8cfd08b2004-02-28 20:35:57 +0000154 _mesa_unpack_color_span_float(ctx, width, GL_RGBA,
Brian Paul147b0832000-08-23 14:31:25 +0000155 ctx->Convolution1D.Filter,
156 format, type, image, &ctx->Unpack,
Brian Paul4923e192004-02-28 22:30:58 +0000157 0); /* transferOps */
Brian Paul147b0832000-08-23 14:31:25 +0000158
Brian Paul203f3952009-09-03 10:41:14 -0600159 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
Brian Paulbd3b40a2004-10-31 17:36:23 +0000160
Brian Paul450e9172004-10-31 18:40:55 +0000161 _mesa_scale_and_bias_rgba(width,
162 (GLfloat (*)[4]) ctx->Convolution1D.Filter,
163 ctx->Pixel.ConvolutionFilterScale[0][0],
164 ctx->Pixel.ConvolutionFilterScale[0][1],
165 ctx->Pixel.ConvolutionFilterScale[0][2],
166 ctx->Pixel.ConvolutionFilterScale[0][3],
167 ctx->Pixel.ConvolutionFilterBias[0][0],
168 ctx->Pixel.ConvolutionFilterBias[0][1],
169 ctx->Pixel.ConvolutionFilterBias[0][2],
170 ctx->Pixel.ConvolutionFilterBias[0][3]);
Keith Whitwella96308c2000-10-30 13:31:59 +0000171
Brian Paulb5012e12000-11-10 18:31:04 +0000172 ctx->NewState |= _NEW_PIXEL;
Brian Paul147b0832000-08-23 14:31:25 +0000173}
174
175
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000176void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000177_mesa_ConvolutionFilter2D(GLenum target, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *image)
178{
Brian Pauld0570642002-03-19 15:22:50 +0000179 GLint baseFormat;
Brian Paulb3050282003-12-04 03:19:46 +0000180 GLint i;
Brian Paul147b0832000-08-23 14:31:25 +0000181 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000182 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000183
184 if (target != GL_CONVOLUTION_2D) {
Brian Paul08836342001-03-03 20:33:27 +0000185 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000186 return;
187 }
188
189 baseFormat = base_filter_format(internalFormat);
190 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
Brian Paul08836342001-03-03 20:33:27 +0000191 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(internalFormat)");
Brian Paul147b0832000-08-23 14:31:25 +0000192 return;
193 }
194
195 if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
Brian Paul08836342001-03-03 20:33:27 +0000196 _mesa_error(ctx, GL_INVALID_VALUE, "glConvolutionFilter2D(width)");
Brian Paul147b0832000-08-23 14:31:25 +0000197 return;
198 }
199 if (height < 0 || height > MAX_CONVOLUTION_HEIGHT) {
Brian Paul08836342001-03-03 20:33:27 +0000200 _mesa_error(ctx, GL_INVALID_VALUE, "glConvolutionFilter2D(height)");
Brian Paul147b0832000-08-23 14:31:25 +0000201 return;
202 }
203
Brian Paulf959f6e2004-04-22 00:27:31 +0000204 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
Brian Paul08836342001-03-03 20:33:27 +0000205 _mesa_error(ctx, GL_INVALID_OPERATION, "glConvolutionFilter2D(format or type)");
Brian Paul90f042a2000-12-10 19:23:19 +0000206 return;
207 }
208 if (format == GL_COLOR_INDEX ||
Brian Paul147b0832000-08-23 14:31:25 +0000209 format == GL_STENCIL_INDEX ||
210 format == GL_DEPTH_COMPONENT ||
211 format == GL_INTENSITY ||
212 type == GL_BITMAP) {
Brian Paul08836342001-03-03 20:33:27 +0000213 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(format or type)");
Brian Paul147b0832000-08-23 14:31:25 +0000214 return;
215 }
216
Brian Paulb3050282003-12-04 03:19:46 +0000217 /* this should have been caught earlier */
218 assert(_mesa_components_in_format(format));
Brian Paul147b0832000-08-23 14:31:25 +0000219
220 ctx->Convolution2D.Format = format;
221 ctx->Convolution2D.InternalFormat = internalFormat;
222 ctx->Convolution2D.Width = width;
223 ctx->Convolution2D.Height = height;
224
Brian Paul95027a02009-09-03 11:23:05 -0600225 image = _mesa_map_validate_pbo_source(ctx,
226 2, &ctx->Unpack, width, height, 1,
227 format, type, image,
228 "glConvolutionFilter2D");
229 if (!image)
Brian Paul203f3952009-09-03 10:41:14 -0600230 return;
Brian Paulbd3b40a2004-10-31 17:36:23 +0000231
Brian Paul147b0832000-08-23 14:31:25 +0000232 /* Unpack filter image. We always store filters in RGBA format. */
233 for (i = 0; i < height; i++) {
Brian Paul60909382004-11-10 15:46:52 +0000234 const GLvoid *src = _mesa_image_address2d(&ctx->Unpack, image, width,
235 height, format, type, i, 0);
Brian Paul147b0832000-08-23 14:31:25 +0000236 GLfloat *dst = ctx->Convolution2D.Filter + i * width * 4;
Brian Paul8cfd08b2004-02-28 20:35:57 +0000237 _mesa_unpack_color_span_float(ctx, width, GL_RGBA, dst,
Brian Paul147b0832000-08-23 14:31:25 +0000238 format, type, src, &ctx->Unpack,
Brian Paul4923e192004-02-28 22:30:58 +0000239 0); /* transferOps */
Brian Paul147b0832000-08-23 14:31:25 +0000240 }
241
Brian Paul203f3952009-09-03 10:41:14 -0600242 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
Brian Paulbd3b40a2004-10-31 17:36:23 +0000243
Brian Paul450e9172004-10-31 18:40:55 +0000244 _mesa_scale_and_bias_rgba(width * height,
245 (GLfloat (*)[4]) ctx->Convolution2D.Filter,
246 ctx->Pixel.ConvolutionFilterScale[1][0],
247 ctx->Pixel.ConvolutionFilterScale[1][1],
248 ctx->Pixel.ConvolutionFilterScale[1][2],
249 ctx->Pixel.ConvolutionFilterScale[1][3],
250 ctx->Pixel.ConvolutionFilterBias[1][0],
251 ctx->Pixel.ConvolutionFilterBias[1][1],
252 ctx->Pixel.ConvolutionFilterBias[1][2],
253 ctx->Pixel.ConvolutionFilterBias[1][3]);
Keith Whitwella96308c2000-10-30 13:31:59 +0000254
Brian Paulb5012e12000-11-10 18:31:04 +0000255 ctx->NewState |= _NEW_PIXEL;
Brian Paul147b0832000-08-23 14:31:25 +0000256}
257
258
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000259void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000260_mesa_ConvolutionParameterf(GLenum target, GLenum pname, GLfloat param)
261{
262 GET_CURRENT_CONTEXT(ctx);
263 GLuint c;
Keith Whitwell58e99172001-01-05 02:26:48 +0000264 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000265
266 switch (target) {
267 case GL_CONVOLUTION_1D:
268 c = 0;
269 break;
270 case GL_CONVOLUTION_2D:
271 c = 1;
272 break;
273 case GL_SEPARABLE_2D:
274 c = 2;
275 break;
276 default:
Brian Paul08836342001-03-03 20:33:27 +0000277 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000278 return;
279 }
280
281 switch (pname) {
282 case GL_CONVOLUTION_BORDER_MODE:
283 if (param == (GLfloat) GL_REDUCE ||
284 param == (GLfloat) GL_CONSTANT_BORDER ||
285 param == (GLfloat) GL_REPLICATE_BORDER) {
286 ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) param;
287 }
288 else {
Brian Paul08836342001-03-03 20:33:27 +0000289 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(params)");
Brian Paul147b0832000-08-23 14:31:25 +0000290 return;
291 }
292 break;
293 default:
Brian Paul08836342001-03-03 20:33:27 +0000294 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(pname)");
Brian Paul147b0832000-08-23 14:31:25 +0000295 return;
296 }
Keith Whitwella96308c2000-10-30 13:31:59 +0000297
298 ctx->NewState |= _NEW_PIXEL;
Brian Paul147b0832000-08-23 14:31:25 +0000299}
300
301
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000302void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000303_mesa_ConvolutionParameterfv(GLenum target, GLenum pname, const GLfloat *params)
304{
305 GET_CURRENT_CONTEXT(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000306 GLuint c;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000307 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000308
309 switch (target) {
310 case GL_CONVOLUTION_1D:
311 c = 0;
Brian Paul147b0832000-08-23 14:31:25 +0000312 break;
313 case GL_CONVOLUTION_2D:
314 c = 1;
Brian Paul147b0832000-08-23 14:31:25 +0000315 break;
316 case GL_SEPARABLE_2D:
317 c = 2;
Brian Paul147b0832000-08-23 14:31:25 +0000318 break;
319 default:
Brian Paul08836342001-03-03 20:33:27 +0000320 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000321 return;
322 }
323
324 switch (pname) {
325 case GL_CONVOLUTION_BORDER_COLOR:
326 COPY_4V(ctx->Pixel.ConvolutionBorderColor[c], params);
327 break;
328 case GL_CONVOLUTION_BORDER_MODE:
329 if (params[0] == (GLfloat) GL_REDUCE ||
330 params[0] == (GLfloat) GL_CONSTANT_BORDER ||
331 params[0] == (GLfloat) GL_REPLICATE_BORDER) {
332 ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) params[0];
333 }
334 else {
Brian Paul08836342001-03-03 20:33:27 +0000335 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(params)");
Brian Paul147b0832000-08-23 14:31:25 +0000336 return;
337 }
338 break;
339 case GL_CONVOLUTION_FILTER_SCALE:
340 COPY_4V(ctx->Pixel.ConvolutionFilterScale[c], params);
341 break;
342 case GL_CONVOLUTION_FILTER_BIAS:
343 COPY_4V(ctx->Pixel.ConvolutionFilterBias[c], params);
344 break;
345 default:
Brian Paul08836342001-03-03 20:33:27 +0000346 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(pname)");
Brian Paul147b0832000-08-23 14:31:25 +0000347 return;
348 }
Keith Whitwella96308c2000-10-30 13:31:59 +0000349
350 ctx->NewState |= _NEW_PIXEL;
Brian Paul147b0832000-08-23 14:31:25 +0000351}
352
353
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000354void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000355_mesa_ConvolutionParameteri(GLenum target, GLenum pname, GLint param)
356{
357 GET_CURRENT_CONTEXT(ctx);
358 GLuint c;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000359 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000360
361 switch (target) {
362 case GL_CONVOLUTION_1D:
363 c = 0;
364 break;
365 case GL_CONVOLUTION_2D:
366 c = 1;
367 break;
368 case GL_SEPARABLE_2D:
369 c = 2;
370 break;
371 default:
Brian Paul08836342001-03-03 20:33:27 +0000372 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000373 return;
374 }
375
376 switch (pname) {
377 case GL_CONVOLUTION_BORDER_MODE:
378 if (param == (GLint) GL_REDUCE ||
379 param == (GLint) GL_CONSTANT_BORDER ||
380 param == (GLint) GL_REPLICATE_BORDER) {
381 ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) param;
382 }
383 else {
Brian Paul08836342001-03-03 20:33:27 +0000384 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(params)");
Brian Paul147b0832000-08-23 14:31:25 +0000385 return;
386 }
387 break;
388 default:
Brian Paul08836342001-03-03 20:33:27 +0000389 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(pname)");
Brian Paul147b0832000-08-23 14:31:25 +0000390 return;
391 }
Keith Whitwella96308c2000-10-30 13:31:59 +0000392
393 ctx->NewState |= _NEW_PIXEL;
Brian Paul147b0832000-08-23 14:31:25 +0000394}
395
396
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000397void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000398_mesa_ConvolutionParameteriv(GLenum target, GLenum pname, const GLint *params)
399{
400 GET_CURRENT_CONTEXT(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000401 GLuint c;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000402 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000403
404 switch (target) {
405 case GL_CONVOLUTION_1D:
406 c = 0;
Brian Paul147b0832000-08-23 14:31:25 +0000407 break;
408 case GL_CONVOLUTION_2D:
409 c = 1;
Brian Paul147b0832000-08-23 14:31:25 +0000410 break;
411 case GL_SEPARABLE_2D:
412 c = 2;
Brian Paul147b0832000-08-23 14:31:25 +0000413 break;
414 default:
Brian Paul08836342001-03-03 20:33:27 +0000415 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000416 return;
417 }
418
419 switch (pname) {
420 case GL_CONVOLUTION_BORDER_COLOR:
421 ctx->Pixel.ConvolutionBorderColor[c][0] = INT_TO_FLOAT(params[0]);
422 ctx->Pixel.ConvolutionBorderColor[c][1] = INT_TO_FLOAT(params[1]);
423 ctx->Pixel.ConvolutionBorderColor[c][2] = INT_TO_FLOAT(params[2]);
424 ctx->Pixel.ConvolutionBorderColor[c][3] = INT_TO_FLOAT(params[3]);
425 break;
426 case GL_CONVOLUTION_BORDER_MODE:
427 if (params[0] == (GLint) GL_REDUCE ||
428 params[0] == (GLint) GL_CONSTANT_BORDER ||
429 params[0] == (GLint) GL_REPLICATE_BORDER) {
430 ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) params[0];
431 }
432 else {
Brian Paul08836342001-03-03 20:33:27 +0000433 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(params)");
Brian Paul147b0832000-08-23 14:31:25 +0000434 return;
435 }
436 break;
437 case GL_CONVOLUTION_FILTER_SCALE:
Karl Schultz7c426812001-09-19 20:30:44 +0000438 /* COPY_4V(ctx->Pixel.ConvolutionFilterScale[c], params); */
439 /* need cast to prevent compiler warnings */
440 ctx->Pixel.ConvolutionFilterScale[c][0] = (GLfloat) params[0];
441 ctx->Pixel.ConvolutionFilterScale[c][1] = (GLfloat) params[1];
442 ctx->Pixel.ConvolutionFilterScale[c][2] = (GLfloat) params[2];
443 ctx->Pixel.ConvolutionFilterScale[c][3] = (GLfloat) params[3];
Brian Paul147b0832000-08-23 14:31:25 +0000444 break;
445 case GL_CONVOLUTION_FILTER_BIAS:
Karl Schultz7c426812001-09-19 20:30:44 +0000446 /* COPY_4V(ctx->Pixel.ConvolutionFilterBias[c], params); */
447 /* need cast to prevent compiler warnings */
448 ctx->Pixel.ConvolutionFilterBias[c][0] = (GLfloat) params[0];
449 ctx->Pixel.ConvolutionFilterBias[c][1] = (GLfloat) params[1];
450 ctx->Pixel.ConvolutionFilterBias[c][2] = (GLfloat) params[2];
451 ctx->Pixel.ConvolutionFilterBias[c][3] = (GLfloat) params[3];
Brian Paul147b0832000-08-23 14:31:25 +0000452 break;
453 default:
Brian Paul08836342001-03-03 20:33:27 +0000454 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(pname)");
Brian Paul147b0832000-08-23 14:31:25 +0000455 return;
456 }
Keith Whitwella96308c2000-10-30 13:31:59 +0000457
458 ctx->NewState |= _NEW_PIXEL;
Brian Paul147b0832000-08-23 14:31:25 +0000459}
460
461
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000462void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000463_mesa_CopyConvolutionFilter1D(GLenum target, GLenum internalFormat, GLint x, GLint y, GLsizei width)
464{
Brian Pauld0570642002-03-19 15:22:50 +0000465 GLint baseFormat;
Brian Paul147b0832000-08-23 14:31:25 +0000466 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000467 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000468
469 if (target != GL_CONVOLUTION_1D) {
Brian Paul08836342001-03-03 20:33:27 +0000470 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter1D(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000471 return;
472 }
473
474 baseFormat = base_filter_format(internalFormat);
475 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
Brian Paul08836342001-03-03 20:33:27 +0000476 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter1D(internalFormat)");
Brian Paul147b0832000-08-23 14:31:25 +0000477 return;
478 }
479
480 if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
Brian Paul08836342001-03-03 20:33:27 +0000481 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter1D(width)");
Brian Paul147b0832000-08-23 14:31:25 +0000482 return;
483 }
484
Brian Pauldc3839e2009-09-19 16:27:59 -0600485 if (!ctx->ReadBuffer->_ColorReadBuffer) {
486 return; /* no readbuffer - OK */
487 }
488
Keith Whitwell70989242001-03-19 02:25:35 +0000489 ctx->Driver.CopyConvolutionFilter1D( ctx, target,
490 internalFormat, x, y, width);
Brian Paul147b0832000-08-23 14:31:25 +0000491}
492
493
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000494void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000495_mesa_CopyConvolutionFilter2D(GLenum target, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height)
496{
Brian Pauld0570642002-03-19 15:22:50 +0000497 GLint baseFormat;
Brian Paul147b0832000-08-23 14:31:25 +0000498 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000499 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000500
501 if (target != GL_CONVOLUTION_2D) {
Brian Paul08836342001-03-03 20:33:27 +0000502 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter2D(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000503 return;
504 }
505
506 baseFormat = base_filter_format(internalFormat);
507 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
Brian Paul08836342001-03-03 20:33:27 +0000508 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter2D(internalFormat)");
Brian Paul147b0832000-08-23 14:31:25 +0000509 return;
510 }
511
512 if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
Brian Paul08836342001-03-03 20:33:27 +0000513 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter2D(width)");
Brian Paul147b0832000-08-23 14:31:25 +0000514 return;
515 }
516 if (height < 0 || height > MAX_CONVOLUTION_HEIGHT) {
Brian Paul08836342001-03-03 20:33:27 +0000517 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter2D(height)");
Brian Paul147b0832000-08-23 14:31:25 +0000518 return;
519 }
520
Brian Pauldc3839e2009-09-19 16:27:59 -0600521 if (!ctx->ReadBuffer->_ColorReadBuffer) {
522 return; /* no readbuffer - OK */
523 }
524
Keith Whitwell70989242001-03-19 02:25:35 +0000525 ctx->Driver.CopyConvolutionFilter2D( ctx, target, internalFormat, x, y,
526 width, height );
Brian Paul147b0832000-08-23 14:31:25 +0000527}
528
529
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000530void GLAPIENTRY
Brian Paul176501d2006-10-13 16:34:25 +0000531_mesa_GetConvolutionFilter(GLenum target, GLenum format, GLenum type,
532 GLvoid *image)
Brian Paul147b0832000-08-23 14:31:25 +0000533{
Brian Paul176501d2006-10-13 16:34:25 +0000534 struct gl_convolution_attrib *filter;
Brian Paulb51b0a82001-03-07 05:06:11 +0000535 GLuint row;
Brian Paul147b0832000-08-23 14:31:25 +0000536 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000537 ASSERT_OUTSIDE_BEGIN_END(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000538
Brian Paul0c000ec2000-11-21 23:26:13 +0000539 if (ctx->NewState) {
Brian Paul08836342001-03-03 20:33:27 +0000540 _mesa_update_state(ctx);
Brian Paul0c000ec2000-11-21 23:26:13 +0000541 }
542
Brian Paulf959f6e2004-04-22 00:27:31 +0000543 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
Brian Paul08836342001-03-03 20:33:27 +0000544 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetConvolutionFilter(format or type)");
Brian Paul90f042a2000-12-10 19:23:19 +0000545 return;
546 }
547
548 if (format == GL_COLOR_INDEX ||
Brian Paul147b0832000-08-23 14:31:25 +0000549 format == GL_STENCIL_INDEX ||
550 format == GL_DEPTH_COMPONENT ||
551 format == GL_INTENSITY ||
552 type == GL_BITMAP) {
Brian Paul08836342001-03-03 20:33:27 +0000553 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(format or type)");
Brian Paul147b0832000-08-23 14:31:25 +0000554 return;
555 }
556
Brian Paulf75d6972000-09-05 20:28:56 +0000557 switch (target) {
558 case GL_CONVOLUTION_1D:
559 filter = &(ctx->Convolution1D);
560 break;
561 case GL_CONVOLUTION_2D:
562 filter = &(ctx->Convolution2D);
563 break;
564 default:
Brian Paul08836342001-03-03 20:33:27 +0000565 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(target)");
Brian Paulf75d6972000-09-05 20:28:56 +0000566 return;
567 }
568
Brian Paul95027a02009-09-03 11:23:05 -0600569 image = _mesa_map_validate_pbo_dest(ctx, 2, &ctx->Pack,
570 filter->Width, filter->Height, 1,
571 format, type, image,
572 "glGetConvolutionFilter");
573 if (!image)
Brian Paul203f3952009-09-03 10:41:14 -0600574 return;
Brian Paulbd3b40a2004-10-31 17:36:23 +0000575
Brian Paulf75d6972000-09-05 20:28:56 +0000576 for (row = 0; row < filter->Height; row++) {
Brian Paul60909382004-11-10 15:46:52 +0000577 GLvoid *dst = _mesa_image_address2d(&ctx->Pack, image, filter->Width,
578 filter->Height, format, type,
579 row, 0);
Brian Paul176501d2006-10-13 16:34:25 +0000580 GLfloat (*src)[4] = (GLfloat (*)[4]) (filter->Filter + row * filter->Width * 4);
581 _mesa_pack_rgba_span_float(ctx, filter->Width, src,
582 format, type, dst, &ctx->Pack, 0x0);
Brian Paulf75d6972000-09-05 20:28:56 +0000583 }
Brian Paulbd3b40a2004-10-31 17:36:23 +0000584
Brian Paul203f3952009-09-03 10:41:14 -0600585 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
Brian Paul147b0832000-08-23 14:31:25 +0000586}
587
588
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000589void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000590_mesa_GetConvolutionParameterfv(GLenum target, GLenum pname, GLfloat *params)
591{
592 GET_CURRENT_CONTEXT(ctx);
593 const struct gl_convolution_attrib *conv;
594 GLuint c;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000595 ASSERT_OUTSIDE_BEGIN_END(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000596
597 switch (target) {
598 case GL_CONVOLUTION_1D:
599 c = 0;
600 conv = &ctx->Convolution1D;
601 break;
602 case GL_CONVOLUTION_2D:
603 c = 1;
604 conv = &ctx->Convolution2D;
605 break;
606 case GL_SEPARABLE_2D:
607 c = 2;
608 conv = &ctx->Separable2D;
609 break;
610 default:
Brian Paul08836342001-03-03 20:33:27 +0000611 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameterfv(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000612 return;
613 }
614
615 switch (pname) {
616 case GL_CONVOLUTION_BORDER_COLOR:
617 COPY_4V(params, ctx->Pixel.ConvolutionBorderColor[c]);
618 break;
619 case GL_CONVOLUTION_BORDER_MODE:
620 *params = (GLfloat) ctx->Pixel.ConvolutionBorderMode[c];
621 break;
622 case GL_CONVOLUTION_FILTER_SCALE:
623 COPY_4V(params, ctx->Pixel.ConvolutionFilterScale[c]);
624 break;
625 case GL_CONVOLUTION_FILTER_BIAS:
626 COPY_4V(params, ctx->Pixel.ConvolutionFilterBias[c]);
627 break;
628 case GL_CONVOLUTION_FORMAT:
629 *params = (GLfloat) conv->Format;
630 break;
631 case GL_CONVOLUTION_WIDTH:
632 *params = (GLfloat) conv->Width;
633 break;
634 case GL_CONVOLUTION_HEIGHT:
635 *params = (GLfloat) conv->Height;
636 break;
637 case GL_MAX_CONVOLUTION_WIDTH:
638 *params = (GLfloat) ctx->Const.MaxConvolutionWidth;
639 break;
640 case GL_MAX_CONVOLUTION_HEIGHT:
641 *params = (GLfloat) ctx->Const.MaxConvolutionHeight;
642 break;
643 default:
Brian Paul08836342001-03-03 20:33:27 +0000644 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameterfv(pname)");
Brian Paul147b0832000-08-23 14:31:25 +0000645 return;
646 }
647}
648
649
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000650void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000651_mesa_GetConvolutionParameteriv(GLenum target, GLenum pname, GLint *params)
652{
653 GET_CURRENT_CONTEXT(ctx);
654 const struct gl_convolution_attrib *conv;
655 GLuint c;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000656 ASSERT_OUTSIDE_BEGIN_END(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000657
658 switch (target) {
659 case GL_CONVOLUTION_1D:
660 c = 0;
661 conv = &ctx->Convolution1D;
662 break;
663 case GL_CONVOLUTION_2D:
664 c = 1;
665 conv = &ctx->Convolution2D;
666 break;
667 case GL_SEPARABLE_2D:
668 c = 2;
669 conv = &ctx->Separable2D;
670 break;
671 default:
Brian Paul08836342001-03-03 20:33:27 +0000672 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameteriv(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000673 return;
674 }
675
676 switch (pname) {
677 case GL_CONVOLUTION_BORDER_COLOR:
678 params[0] = FLOAT_TO_INT(ctx->Pixel.ConvolutionBorderColor[c][0]);
679 params[1] = FLOAT_TO_INT(ctx->Pixel.ConvolutionBorderColor[c][1]);
680 params[2] = FLOAT_TO_INT(ctx->Pixel.ConvolutionBorderColor[c][2]);
681 params[3] = FLOAT_TO_INT(ctx->Pixel.ConvolutionBorderColor[c][3]);
682 break;
683 case GL_CONVOLUTION_BORDER_MODE:
684 *params = (GLint) ctx->Pixel.ConvolutionBorderMode[c];
685 break;
686 case GL_CONVOLUTION_FILTER_SCALE:
687 params[0] = (GLint) ctx->Pixel.ConvolutionFilterScale[c][0];
688 params[1] = (GLint) ctx->Pixel.ConvolutionFilterScale[c][1];
689 params[2] = (GLint) ctx->Pixel.ConvolutionFilterScale[c][2];
690 params[3] = (GLint) ctx->Pixel.ConvolutionFilterScale[c][3];
691 break;
692 case GL_CONVOLUTION_FILTER_BIAS:
693 params[0] = (GLint) ctx->Pixel.ConvolutionFilterBias[c][0];
694 params[1] = (GLint) ctx->Pixel.ConvolutionFilterBias[c][1];
695 params[2] = (GLint) ctx->Pixel.ConvolutionFilterBias[c][2];
696 params[3] = (GLint) ctx->Pixel.ConvolutionFilterBias[c][3];
697 break;
698 case GL_CONVOLUTION_FORMAT:
699 *params = (GLint) conv->Format;
700 break;
701 case GL_CONVOLUTION_WIDTH:
702 *params = (GLint) conv->Width;
703 break;
704 case GL_CONVOLUTION_HEIGHT:
705 *params = (GLint) conv->Height;
706 break;
707 case GL_MAX_CONVOLUTION_WIDTH:
708 *params = (GLint) ctx->Const.MaxConvolutionWidth;
709 break;
710 case GL_MAX_CONVOLUTION_HEIGHT:
711 *params = (GLint) ctx->Const.MaxConvolutionHeight;
712 break;
713 default:
Brian Paul08836342001-03-03 20:33:27 +0000714 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameteriv(pname)");
Brian Paul147b0832000-08-23 14:31:25 +0000715 return;
716 }
717}
718
719
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000720void GLAPIENTRY
Brian Paul176501d2006-10-13 16:34:25 +0000721_mesa_GetSeparableFilter(GLenum target, GLenum format, GLenum type,
722 GLvoid *row, GLvoid *column, GLvoid *span)
Brian Paul147b0832000-08-23 14:31:25 +0000723{
Brian Paulf75d6972000-09-05 20:28:56 +0000724 const GLint colStart = MAX_CONVOLUTION_WIDTH * 4;
Brian Paul176501d2006-10-13 16:34:25 +0000725 struct gl_convolution_attrib *filter;
Brian Paul147b0832000-08-23 14:31:25 +0000726 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000727 ASSERT_OUTSIDE_BEGIN_END(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000728
Brian Paul0c000ec2000-11-21 23:26:13 +0000729 if (ctx->NewState) {
Brian Paul08836342001-03-03 20:33:27 +0000730 _mesa_update_state(ctx);
Brian Paul0c000ec2000-11-21 23:26:13 +0000731 }
732
Brian Paul147b0832000-08-23 14:31:25 +0000733 if (target != GL_SEPARABLE_2D) {
Brian Paul08836342001-03-03 20:33:27 +0000734 _mesa_error(ctx, GL_INVALID_ENUM, "glGetSeparableFilter(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000735 return;
736 }
737
Brian Paulf959f6e2004-04-22 00:27:31 +0000738 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
Brian Paul176501d2006-10-13 16:34:25 +0000739 _mesa_error(ctx, GL_INVALID_OPERATION,
740 "glGetConvolutionFilter(format or type)");
Brian Paul90f042a2000-12-10 19:23:19 +0000741 return;
742 }
743
744 if (format == GL_COLOR_INDEX ||
Brian Paul147b0832000-08-23 14:31:25 +0000745 format == GL_STENCIL_INDEX ||
746 format == GL_DEPTH_COMPONENT ||
747 format == GL_INTENSITY ||
748 type == GL_BITMAP) {
Brian Paul08836342001-03-03 20:33:27 +0000749 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(format or type)");
Brian Paul147b0832000-08-23 14:31:25 +0000750 return;
751 }
752
Brian Paulf75d6972000-09-05 20:28:56 +0000753 filter = &ctx->Separable2D;
754
Brian Paul2db37ef2009-09-03 11:41:29 -0600755 /* Get row filter */
756 row = _mesa_map_validate_pbo_dest(ctx, 1, &ctx->Pack,
757 filter->Width, 1, 1,
758 format, type, row,
759 "glGetConvolutionFilter");
Brian Paulbd3b40a2004-10-31 17:36:23 +0000760 if (row) {
Brian Paul60909382004-11-10 15:46:52 +0000761 GLvoid *dst = _mesa_image_address1d(&ctx->Pack, row, filter->Width,
762 format, type, 0);
Brian Paul8cfd08b2004-02-28 20:35:57 +0000763 _mesa_pack_rgba_span_float(ctx, filter->Width,
Brian Paul176501d2006-10-13 16:34:25 +0000764 (GLfloat (*)[4]) filter->Filter,
765 format, type, dst, &ctx->Pack, 0x0);
Brian Paul2db37ef2009-09-03 11:41:29 -0600766 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
Brian Paulf75d6972000-09-05 20:28:56 +0000767 }
768
Brian Paul2db37ef2009-09-03 11:41:29 -0600769 /* get column filter */
770 column = _mesa_map_validate_pbo_dest(ctx, 1, &ctx->Pack,
771 filter->Height, 1, 1,
Brian Pauld75a99e2009-09-03 12:10:53 -0600772 format, type, column,
Brian Paul2db37ef2009-09-03 11:41:29 -0600773 "glGetConvolutionFilter");
Brian Paulbd3b40a2004-10-31 17:36:23 +0000774 if (column) {
Brian Paul60909382004-11-10 15:46:52 +0000775 GLvoid *dst = _mesa_image_address1d(&ctx->Pack, column, filter->Height,
776 format, type, 0);
Brian Paul176501d2006-10-13 16:34:25 +0000777 GLfloat (*src)[4] = (GLfloat (*)[4]) (filter->Filter + colStart);
778 _mesa_pack_rgba_span_float(ctx, filter->Height, src,
779 format, type, dst, &ctx->Pack, 0x0);
Brian Paul2db37ef2009-09-03 11:41:29 -0600780 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
Brian Paulf75d6972000-09-05 20:28:56 +0000781 }
782
783 (void) span; /* unused at this time */
Brian Paul147b0832000-08-23 14:31:25 +0000784}
785
786
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000787void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000788_mesa_SeparableFilter2D(GLenum target, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *row, const GLvoid *column)
789{
790 const GLint colStart = MAX_CONVOLUTION_WIDTH * 4;
Brian Pauld0570642002-03-19 15:22:50 +0000791 GLint baseFormat;
Brian Paul147b0832000-08-23 14:31:25 +0000792 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000793 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000794
795 if (target != GL_SEPARABLE_2D) {
Brian Paul08836342001-03-03 20:33:27 +0000796 _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000797 return;
798 }
799
800 baseFormat = base_filter_format(internalFormat);
801 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
Brian Paul08836342001-03-03 20:33:27 +0000802 _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(internalFormat)");
Brian Paul147b0832000-08-23 14:31:25 +0000803 return;
804 }
805
806 if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
Brian Paul08836342001-03-03 20:33:27 +0000807 _mesa_error(ctx, GL_INVALID_VALUE, "glSeparableFilter2D(width)");
Brian Paul147b0832000-08-23 14:31:25 +0000808 return;
809 }
810 if (height < 0 || height > MAX_CONVOLUTION_HEIGHT) {
Brian Paul08836342001-03-03 20:33:27 +0000811 _mesa_error(ctx, GL_INVALID_VALUE, "glSeparableFilter2D(height)");
Brian Paul147b0832000-08-23 14:31:25 +0000812 return;
813 }
814
Brian Paulf959f6e2004-04-22 00:27:31 +0000815 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
Brian Paul08836342001-03-03 20:33:27 +0000816 _mesa_error(ctx, GL_INVALID_OPERATION, "glSeparableFilter2D(format or type)");
Brian Paul90f042a2000-12-10 19:23:19 +0000817 return;
818 }
819
820 if (format == GL_COLOR_INDEX ||
Brian Paul147b0832000-08-23 14:31:25 +0000821 format == GL_STENCIL_INDEX ||
822 format == GL_DEPTH_COMPONENT ||
823 format == GL_INTENSITY ||
824 type == GL_BITMAP) {
Brian Paul08836342001-03-03 20:33:27 +0000825 _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(format or type)");
Brian Paul147b0832000-08-23 14:31:25 +0000826 return;
827 }
828
829 ctx->Separable2D.Format = format;
830 ctx->Separable2D.InternalFormat = internalFormat;
831 ctx->Separable2D.Width = width;
832 ctx->Separable2D.Height = height;
833
Brian Paulbd3b40a2004-10-31 17:36:23 +0000834 /* unpack row filter */
Brian Paul2db37ef2009-09-03 11:41:29 -0600835 row = _mesa_map_validate_pbo_source(ctx, 1, &ctx->Unpack,
836 width, 1, 1,
837 format, type, row,
Brian Pauld75a99e2009-09-03 12:10:53 -0600838 "glSeparableFilter2D");
Brian Paulbd3b40a2004-10-31 17:36:23 +0000839 if (row) {
840 _mesa_unpack_color_span_float(ctx, width, GL_RGBA,
841 ctx->Separable2D.Filter,
842 format, type, row, &ctx->Unpack,
Brian Paul2db37ef2009-09-03 11:41:29 -0600843 0x0); /* transferOps */
Brian Paul450e9172004-10-31 18:40:55 +0000844 _mesa_scale_and_bias_rgba(width,
845 (GLfloat (*)[4]) ctx->Separable2D.Filter,
846 ctx->Pixel.ConvolutionFilterScale[2][0],
847 ctx->Pixel.ConvolutionFilterScale[2][1],
848 ctx->Pixel.ConvolutionFilterScale[2][2],
849 ctx->Pixel.ConvolutionFilterScale[2][3],
850 ctx->Pixel.ConvolutionFilterBias[2][0],
851 ctx->Pixel.ConvolutionFilterBias[2][1],
852 ctx->Pixel.ConvolutionFilterBias[2][2],
853 ctx->Pixel.ConvolutionFilterBias[2][3]);
Brian Paul2db37ef2009-09-03 11:41:29 -0600854 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
Brian Paul147b0832000-08-23 14:31:25 +0000855 }
856
857 /* unpack column filter */
Brian Paul2db37ef2009-09-03 11:41:29 -0600858 column = _mesa_map_validate_pbo_source(ctx, 1, &ctx->Unpack,
859 height, 1, 1,
Brian Pauld75a99e2009-09-03 12:10:53 -0600860 format, type, column,
861 "glSeparableFilter2D");
Brian Paulbd3b40a2004-10-31 17:36:23 +0000862 if (column) {
Brian Pauleffb7202004-10-31 18:41:38 +0000863 _mesa_unpack_color_span_float(ctx, height, GL_RGBA,
864 &ctx->Separable2D.Filter[colStart],
865 format, type, column, &ctx->Unpack,
866 0); /* transferOps */
Brian Paul147b0832000-08-23 14:31:25 +0000867
Brian Paul450e9172004-10-31 18:40:55 +0000868 _mesa_scale_and_bias_rgba(height,
869 (GLfloat (*)[4]) (ctx->Separable2D.Filter + colStart),
870 ctx->Pixel.ConvolutionFilterScale[2][0],
871 ctx->Pixel.ConvolutionFilterScale[2][1],
872 ctx->Pixel.ConvolutionFilterScale[2][2],
873 ctx->Pixel.ConvolutionFilterScale[2][3],
874 ctx->Pixel.ConvolutionFilterBias[2][0],
875 ctx->Pixel.ConvolutionFilterBias[2][1],
876 ctx->Pixel.ConvolutionFilterBias[2][2],
877 ctx->Pixel.ConvolutionFilterBias[2][3]);
Brian Paul2db37ef2009-09-03 11:41:29 -0600878 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
Brian Paulbd3b40a2004-10-31 17:36:23 +0000879 }
880
Brian Paul434ec3a2009-08-12 13:46:16 -0600881 if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
Brian Paulbd3b40a2004-10-31 17:36:23 +0000882 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
883 ctx->Unpack.BufferObj);
Brian Paul147b0832000-08-23 14:31:25 +0000884 }
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000885
Brian Paulb5012e12000-11-10 18:31:04 +0000886 ctx->NewState |= _NEW_PIXEL;
Brian Paul147b0832000-08-23 14:31:25 +0000887}
888
889
890/**********************************************************************/
891/*** image convolution functions ***/
892/**********************************************************************/
893
Brian Pauld4b799b2000-08-21 14:24:30 +0000894static void
895convolve_1d_reduce(GLint srcWidth, const GLfloat src[][4],
896 GLint filterWidth, const GLfloat filter[][4],
897 GLfloat dest[][4])
Brian Paulcc8e37f2000-07-12 13:00:09 +0000898{
Brian Paul7e708742000-08-22 18:54:25 +0000899 GLint dstWidth;
Brian Paulcc8e37f2000-07-12 13:00:09 +0000900 GLint i, n;
901
Brian Paul7e708742000-08-22 18:54:25 +0000902 if (filterWidth >= 1)
903 dstWidth = srcWidth - (filterWidth - 1);
904 else
905 dstWidth = srcWidth;
906
Brian Paulcc8e37f2000-07-12 13:00:09 +0000907 if (dstWidth <= 0)
908 return; /* null result */
909
910 for (i = 0; i < dstWidth; i++) {
911 GLfloat sumR = 0.0;
912 GLfloat sumG = 0.0;
913 GLfloat sumB = 0.0;
914 GLfloat sumA = 0.0;
915 for (n = 0; n < filterWidth; n++) {
916 sumR += src[i + n][RCOMP] * filter[n][RCOMP];
917 sumG += src[i + n][GCOMP] * filter[n][GCOMP];
918 sumB += src[i + n][BCOMP] * filter[n][BCOMP];
919 sumA += src[i + n][ACOMP] * filter[n][ACOMP];
920 }
921 dest[i][RCOMP] = sumR;
922 dest[i][GCOMP] = sumG;
923 dest[i][BCOMP] = sumB;
924 dest[i][ACOMP] = sumA;
925 }
926}
927
928
Brian Pauld4b799b2000-08-21 14:24:30 +0000929static void
930convolve_1d_constant(GLint srcWidth, const GLfloat src[][4],
931 GLint filterWidth, const GLfloat filter[][4],
932 GLfloat dest[][4],
933 const GLfloat borderColor[4])
Brian Paulcc8e37f2000-07-12 13:00:09 +0000934{
935 const GLint halfFilterWidth = filterWidth / 2;
936 GLint i, n;
937
938 for (i = 0; i < srcWidth; i++) {
939 GLfloat sumR = 0.0;
940 GLfloat sumG = 0.0;
941 GLfloat sumB = 0.0;
942 GLfloat sumA = 0.0;
943 for (n = 0; n < filterWidth; n++) {
944 if (i + n < halfFilterWidth || i + n - halfFilterWidth >= srcWidth) {
945 sumR += borderColor[RCOMP] * filter[n][RCOMP];
946 sumG += borderColor[GCOMP] * filter[n][GCOMP];
947 sumB += borderColor[BCOMP] * filter[n][BCOMP];
948 sumA += borderColor[ACOMP] * filter[n][ACOMP];
949 }
950 else {
951 sumR += src[i + n - halfFilterWidth][RCOMP] * filter[n][RCOMP];
952 sumG += src[i + n - halfFilterWidth][GCOMP] * filter[n][GCOMP];
953 sumB += src[i + n - halfFilterWidth][BCOMP] * filter[n][BCOMP];
954 sumA += src[i + n - halfFilterWidth][ACOMP] * filter[n][ACOMP];
955 }
956 }
957 dest[i][RCOMP] = sumR;
958 dest[i][GCOMP] = sumG;
959 dest[i][BCOMP] = sumB;
960 dest[i][ACOMP] = sumA;
961 }
962}
963
964
Brian Pauld4b799b2000-08-21 14:24:30 +0000965static void
966convolve_1d_replicate(GLint srcWidth, const GLfloat src[][4],
967 GLint filterWidth, const GLfloat filter[][4],
968 GLfloat dest[][4])
Brian Paulcc8e37f2000-07-12 13:00:09 +0000969{
970 const GLint halfFilterWidth = filterWidth / 2;
971 GLint i, n;
972
973 for (i = 0; i < srcWidth; i++) {
974 GLfloat sumR = 0.0;
975 GLfloat sumG = 0.0;
976 GLfloat sumB = 0.0;
977 GLfloat sumA = 0.0;
978 for (n = 0; n < filterWidth; n++) {
979 if (i + n < halfFilterWidth) {
980 sumR += src[0][RCOMP] * filter[n][RCOMP];
981 sumG += src[0][GCOMP] * filter[n][GCOMP];
982 sumB += src[0][BCOMP] * filter[n][BCOMP];
983 sumA += src[0][ACOMP] * filter[n][ACOMP];
984 }
985 else if (i + n - halfFilterWidth >= srcWidth) {
986 sumR += src[srcWidth - 1][RCOMP] * filter[n][RCOMP];
987 sumG += src[srcWidth - 1][GCOMP] * filter[n][GCOMP];
988 sumB += src[srcWidth - 1][BCOMP] * filter[n][BCOMP];
989 sumA += src[srcWidth - 1][ACOMP] * filter[n][ACOMP];
990 }
991 else {
992 sumR += src[i + n - halfFilterWidth][RCOMP] * filter[n][RCOMP];
993 sumG += src[i + n - halfFilterWidth][GCOMP] * filter[n][GCOMP];
994 sumB += src[i + n - halfFilterWidth][BCOMP] * filter[n][BCOMP];
995 sumA += src[i + n - halfFilterWidth][ACOMP] * filter[n][ACOMP];
996 }
997 }
998 dest[i][RCOMP] = sumR;
999 dest[i][GCOMP] = sumG;
1000 dest[i][BCOMP] = sumB;
1001 dest[i][ACOMP] = sumA;
1002 }
1003}
1004
1005
Brian Pauld4b799b2000-08-21 14:24:30 +00001006static void
1007convolve_2d_reduce(GLint srcWidth, GLint srcHeight,
1008 const GLfloat src[][4],
1009 GLint filterWidth, GLint filterHeight,
1010 const GLfloat filter[][4],
1011 GLfloat dest[][4])
Brian Paulcc8e37f2000-07-12 13:00:09 +00001012{
Brian Paul7e708742000-08-22 18:54:25 +00001013 GLint dstWidth, dstHeight;
Brian Pauld4b799b2000-08-21 14:24:30 +00001014 GLint i, j, n, m;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001015
Brian Paul7e708742000-08-22 18:54:25 +00001016 if (filterWidth >= 1)
1017 dstWidth = srcWidth - (filterWidth - 1);
1018 else
1019 dstWidth = srcWidth;
1020
1021 if (filterHeight >= 1)
1022 dstHeight = srcHeight - (filterHeight - 1);
1023 else
1024 dstHeight = srcHeight;
1025
Brian Pauld4b799b2000-08-21 14:24:30 +00001026 if (dstWidth <= 0 || dstHeight <= 0)
1027 return;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001028
Brian Pauld4b799b2000-08-21 14:24:30 +00001029 for (j = 0; j < dstHeight; j++) {
1030 for (i = 0; i < dstWidth; i++) {
1031 GLfloat sumR = 0.0;
1032 GLfloat sumG = 0.0;
1033 GLfloat sumB = 0.0;
1034 GLfloat sumA = 0.0;
1035 for (m = 0; m < filterHeight; m++) {
1036 for (n = 0; n < filterWidth; n++) {
1037 const GLint k = (j + m) * srcWidth + i + n;
1038 const GLint f = m * filterWidth + n;
1039 sumR += src[k][RCOMP] * filter[f][RCOMP];
1040 sumG += src[k][GCOMP] * filter[f][GCOMP];
1041 sumB += src[k][BCOMP] * filter[f][BCOMP];
1042 sumA += src[k][ACOMP] * filter[f][ACOMP];
1043 }
Brian Paulcc8e37f2000-07-12 13:00:09 +00001044 }
Brian Pauld4b799b2000-08-21 14:24:30 +00001045 dest[j * dstWidth + i][RCOMP] = sumR;
1046 dest[j * dstWidth + i][GCOMP] = sumG;
1047 dest[j * dstWidth + i][BCOMP] = sumB;
1048 dest[j * dstWidth + i][ACOMP] = sumA;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001049 }
Brian Paulcc8e37f2000-07-12 13:00:09 +00001050 }
1051}
1052
1053
Brian Pauld4b799b2000-08-21 14:24:30 +00001054static void
1055convolve_2d_constant(GLint srcWidth, GLint srcHeight,
1056 const GLfloat src[][4],
1057 GLint filterWidth, GLint filterHeight,
1058 const GLfloat filter[][4],
1059 GLfloat dest[][4],
1060 const GLfloat borderColor[4])
Brian Paulcc8e37f2000-07-12 13:00:09 +00001061{
1062 const GLint halfFilterWidth = filterWidth / 2;
Brian Pauld4b799b2000-08-21 14:24:30 +00001063 const GLint halfFilterHeight = filterHeight / 2;
1064 GLint i, j, n, m;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001065
Brian Pauld4b799b2000-08-21 14:24:30 +00001066 for (j = 0; j < srcHeight; j++) {
1067 for (i = 0; i < srcWidth; i++) {
1068 GLfloat sumR = 0.0;
1069 GLfloat sumG = 0.0;
1070 GLfloat sumB = 0.0;
1071 GLfloat sumA = 0.0;
1072 for (m = 0; m < filterHeight; m++) {
1073 for (n = 0; n < filterWidth; n++) {
1074 const GLint f = m * filterWidth + n;
1075 const GLint is = i + n - halfFilterWidth;
1076 const GLint js = j + m - halfFilterHeight;
1077 if (is < 0 || is >= srcWidth ||
1078 js < 0 || js >= srcHeight) {
1079 sumR += borderColor[RCOMP] * filter[f][RCOMP];
1080 sumG += borderColor[GCOMP] * filter[f][GCOMP];
1081 sumB += borderColor[BCOMP] * filter[f][BCOMP];
1082 sumA += borderColor[ACOMP] * filter[f][ACOMP];
1083 }
1084 else {
1085 const GLint k = js * srcWidth + is;
1086 sumR += src[k][RCOMP] * filter[f][RCOMP];
1087 sumG += src[k][GCOMP] * filter[f][GCOMP];
1088 sumB += src[k][BCOMP] * filter[f][BCOMP];
1089 sumA += src[k][ACOMP] * filter[f][ACOMP];
1090 }
Brian Paulcc8e37f2000-07-12 13:00:09 +00001091 }
1092 }
Brian Pauld4b799b2000-08-21 14:24:30 +00001093 dest[j * srcWidth + i][RCOMP] = sumR;
1094 dest[j * srcWidth + i][GCOMP] = sumG;
1095 dest[j * srcWidth + i][BCOMP] = sumB;
1096 dest[j * srcWidth + i][ACOMP] = sumA;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001097 }
Brian Paulcc8e37f2000-07-12 13:00:09 +00001098 }
1099}
1100
1101
Brian Pauld4b799b2000-08-21 14:24:30 +00001102static void
1103convolve_2d_replicate(GLint srcWidth, GLint srcHeight,
1104 const GLfloat src[][4],
1105 GLint filterWidth, GLint filterHeight,
1106 const GLfloat filter[][4],
1107 GLfloat dest[][4])
Brian Paulcc8e37f2000-07-12 13:00:09 +00001108{
1109 const GLint halfFilterWidth = filterWidth / 2;
Brian Pauld4b799b2000-08-21 14:24:30 +00001110 const GLint halfFilterHeight = filterHeight / 2;
1111 GLint i, j, n, m;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001112
Brian Pauld4b799b2000-08-21 14:24:30 +00001113 for (j = 0; j < srcHeight; j++) {
1114 for (i = 0; i < srcWidth; i++) {
1115 GLfloat sumR = 0.0;
1116 GLfloat sumG = 0.0;
1117 GLfloat sumB = 0.0;
1118 GLfloat sumA = 0.0;
1119 for (m = 0; m < filterHeight; m++) {
1120 for (n = 0; n < filterWidth; n++) {
1121 const GLint f = m * filterWidth + n;
1122 GLint is = i + n - halfFilterWidth;
1123 GLint js = j + m - halfFilterHeight;
1124 GLint k;
1125 if (is < 0)
1126 is = 0;
1127 else if (is >= srcWidth)
1128 is = srcWidth - 1;
1129 if (js < 0)
1130 js = 0;
1131 else if (js >= srcHeight)
1132 js = srcHeight - 1;
1133 k = js * srcWidth + is;
1134 sumR += src[k][RCOMP] * filter[f][RCOMP];
1135 sumG += src[k][GCOMP] * filter[f][GCOMP];
1136 sumB += src[k][BCOMP] * filter[f][BCOMP];
1137 sumA += src[k][ACOMP] * filter[f][ACOMP];
Brian Paulcc8e37f2000-07-12 13:00:09 +00001138 }
1139 }
Brian Pauld4b799b2000-08-21 14:24:30 +00001140 dest[j * srcWidth + i][RCOMP] = sumR;
1141 dest[j * srcWidth + i][GCOMP] = sumG;
1142 dest[j * srcWidth + i][BCOMP] = sumB;
1143 dest[j * srcWidth + i][ACOMP] = sumA;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001144 }
Brian Paulcc8e37f2000-07-12 13:00:09 +00001145 }
1146}
1147
1148
Brian Pauld4b799b2000-08-21 14:24:30 +00001149static void
1150convolve_sep_reduce(GLint srcWidth, GLint srcHeight,
1151 const GLfloat src[][4],
1152 GLint filterWidth, GLint filterHeight,
1153 const GLfloat rowFilt[][4],
1154 const GLfloat colFilt[][4],
1155 GLfloat dest[][4])
Brian Paulcc8e37f2000-07-12 13:00:09 +00001156{
Brian Paul7e708742000-08-22 18:54:25 +00001157 GLint dstWidth, dstHeight;
Brian Pauld4b799b2000-08-21 14:24:30 +00001158 GLint i, j, n, m;
Brian Paul7e708742000-08-22 18:54:25 +00001159
1160 if (filterWidth >= 1)
1161 dstWidth = srcWidth - (filterWidth - 1);
1162 else
1163 dstWidth = srcWidth;
1164
1165 if (filterHeight >= 1)
1166 dstHeight = srcHeight - (filterHeight - 1);
1167 else
1168 dstHeight = srcHeight;
1169
1170 if (dstWidth <= 0 || dstHeight <= 0)
1171 return;
1172
1173 for (j = 0; j < dstHeight; j++) {
1174 for (i = 0; i < dstWidth; i++) {
Brian Pauld4b799b2000-08-21 14:24:30 +00001175 GLfloat sumR = 0.0;
1176 GLfloat sumG = 0.0;
1177 GLfloat sumB = 0.0;
1178 GLfloat sumA = 0.0;
1179 for (m = 0; m < filterHeight; m++) {
1180 for (n = 0; n < filterWidth; n++) {
Brian Paul7e708742000-08-22 18:54:25 +00001181 GLint k = (j + m) * srcWidth + i + n;
1182 sumR += src[k][RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
1183 sumG += src[k][GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
1184 sumB += src[k][BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
1185 sumA += src[k][ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
Brian Paulcc8e37f2000-07-12 13:00:09 +00001186 }
1187 }
Brian Paul7e708742000-08-22 18:54:25 +00001188 dest[j * dstWidth + i][RCOMP] = sumR;
1189 dest[j * dstWidth + i][GCOMP] = sumG;
1190 dest[j * dstWidth + i][BCOMP] = sumB;
1191 dest[j * dstWidth + i][ACOMP] = sumA;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001192 }
Brian Paulcc8e37f2000-07-12 13:00:09 +00001193 }
Brian Paulcc8e37f2000-07-12 13:00:09 +00001194}
1195
1196
Brian Pauld4b799b2000-08-21 14:24:30 +00001197static void
1198convolve_sep_constant(GLint srcWidth, GLint srcHeight,
1199 const GLfloat src[][4],
1200 GLint filterWidth, GLint filterHeight,
1201 const GLfloat rowFilt[][4],
1202 const GLfloat colFilt[][4],
1203 GLfloat dest[][4],
1204 const GLfloat borderColor[4])
Brian Paulcc8e37f2000-07-12 13:00:09 +00001205{
1206 const GLint halfFilterWidth = filterWidth / 2;
Brian Pauld4b799b2000-08-21 14:24:30 +00001207 const GLint halfFilterHeight = filterHeight / 2;
1208 GLint i, j, n, m;
Brian Paul7e708742000-08-22 18:54:25 +00001209
Brian Pauld4b799b2000-08-21 14:24:30 +00001210 for (j = 0; j < srcHeight; j++) {
1211 for (i = 0; i < srcWidth; i++) {
1212 GLfloat sumR = 0.0;
1213 GLfloat sumG = 0.0;
1214 GLfloat sumB = 0.0;
1215 GLfloat sumA = 0.0;
1216 for (m = 0; m < filterHeight; m++) {
1217 for (n = 0; n < filterWidth; n++) {
Brian Paul7e708742000-08-22 18:54:25 +00001218 const GLint is = i + n - halfFilterWidth;
1219 const GLint js = j + m - halfFilterHeight;
1220 if (is < 0 || is >= srcWidth ||
1221 js < 0 || js >= srcHeight) {
Brian Pauld4b799b2000-08-21 14:24:30 +00001222 sumR += borderColor[RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
1223 sumG += borderColor[GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
1224 sumB += borderColor[BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
1225 sumA += borderColor[ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
1226 }
1227 else {
Brian Paul7e708742000-08-22 18:54:25 +00001228 GLint k = js * srcWidth + is;
Brian Pauld4b799b2000-08-21 14:24:30 +00001229 sumR += src[k][RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
1230 sumG += src[k][GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
1231 sumB += src[k][BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
1232 sumA += src[k][ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
1233 }
Brian Paul7e708742000-08-22 18:54:25 +00001234
Brian Paulcc8e37f2000-07-12 13:00:09 +00001235 }
1236 }
Brian Paul7e708742000-08-22 18:54:25 +00001237 dest[j * srcWidth + i][RCOMP] = sumR;
1238 dest[j * srcWidth + i][GCOMP] = sumG;
1239 dest[j * srcWidth + i][BCOMP] = sumB;
1240 dest[j * srcWidth + i][ACOMP] = sumA;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001241 }
Brian Pauld4b799b2000-08-21 14:24:30 +00001242 }
Brian Pauld4b799b2000-08-21 14:24:30 +00001243}
1244
1245
1246static void
1247convolve_sep_replicate(GLint srcWidth, GLint srcHeight,
1248 const GLfloat src[][4],
1249 GLint filterWidth, GLint filterHeight,
1250 const GLfloat rowFilt[][4],
1251 const GLfloat colFilt[][4],
1252 GLfloat dest[][4])
1253{
1254 const GLint halfFilterWidth = filterWidth / 2;
1255 const GLint halfFilterHeight = filterHeight / 2;
1256 GLint i, j, n, m;
1257
1258 for (j = 0; j < srcHeight; j++) {
1259 for (i = 0; i < srcWidth; i++) {
1260 GLfloat sumR = 0.0;
1261 GLfloat sumG = 0.0;
1262 GLfloat sumB = 0.0;
1263 GLfloat sumA = 0.0;
1264 for (m = 0; m < filterHeight; m++) {
1265 for (n = 0; n < filterWidth; n++) {
Brian Paul7e708742000-08-22 18:54:25 +00001266 GLint is = i + n - halfFilterWidth;
1267 GLint js = j + m - halfFilterHeight;
1268 GLint k;
1269 if (is < 0)
1270 is = 0;
1271 else if (is >= srcWidth)
1272 is = srcWidth - 1;
1273 if (js < 0)
1274 js = 0;
1275 else if (js >= srcHeight)
1276 js = srcHeight - 1;
1277 k = js * srcWidth + is;
1278 sumR += src[k][RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
1279 sumG += src[k][GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
1280 sumB += src[k][BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
1281 sumA += src[k][ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
Brian Pauld4b799b2000-08-21 14:24:30 +00001282 }
1283 }
Brian Paul7e708742000-08-22 18:54:25 +00001284 dest[j * srcWidth + i][RCOMP] = sumR;
1285 dest[j * srcWidth + i][GCOMP] = sumG;
1286 dest[j * srcWidth + i][BCOMP] = sumB;
1287 dest[j * srcWidth + i][ACOMP] = sumA;
Brian Pauld4b799b2000-08-21 14:24:30 +00001288 }
1289 }
1290}
1291
1292
1293
1294void
1295_mesa_convolve_1d_image(const GLcontext *ctx, GLsizei *width,
1296 const GLfloat *srcImage, GLfloat *dstImage)
1297{
1298 switch (ctx->Pixel.ConvolutionBorderMode[0]) {
1299 case GL_REDUCE:
1300 convolve_1d_reduce(*width, (const GLfloat (*)[4]) srcImage,
1301 ctx->Convolution1D.Width,
1302 (const GLfloat (*)[4]) ctx->Convolution1D.Filter,
1303 (GLfloat (*)[4]) dstImage);
Brian Paul7e708742000-08-22 18:54:25 +00001304 *width = *width - (MAX2(ctx->Convolution1D.Width, 1) - 1);
Brian Pauld4b799b2000-08-21 14:24:30 +00001305 break;
1306 case GL_CONSTANT_BORDER:
1307 convolve_1d_constant(*width, (const GLfloat (*)[4]) srcImage,
1308 ctx->Convolution1D.Width,
1309 (const GLfloat (*)[4]) ctx->Convolution1D.Filter,
1310 (GLfloat (*)[4]) dstImage,
1311 ctx->Pixel.ConvolutionBorderColor[0]);
1312 break;
1313 case GL_REPLICATE_BORDER:
1314 convolve_1d_replicate(*width, (const GLfloat (*)[4]) srcImage,
1315 ctx->Convolution1D.Width,
1316 (const GLfloat (*)[4]) ctx->Convolution1D.Filter,
1317 (GLfloat (*)[4]) dstImage);
1318 break;
1319 default:
1320 ;
1321 }
1322}
1323
1324
1325void
1326_mesa_convolve_2d_image(const GLcontext *ctx, GLsizei *width, GLsizei *height,
1327 const GLfloat *srcImage, GLfloat *dstImage)
1328{
1329 switch (ctx->Pixel.ConvolutionBorderMode[1]) {
1330 case GL_REDUCE:
1331 convolve_2d_reduce(*width, *height,
1332 (const GLfloat (*)[4]) srcImage,
1333 ctx->Convolution2D.Width,
1334 ctx->Convolution2D.Height,
1335 (const GLfloat (*)[4]) ctx->Convolution2D.Filter,
1336 (GLfloat (*)[4]) dstImage);
Brian Paul7e708742000-08-22 18:54:25 +00001337 *width = *width - (MAX2(ctx->Convolution2D.Width, 1) - 1);
1338 *height = *height - (MAX2(ctx->Convolution2D.Height, 1) - 1);
Brian Pauld4b799b2000-08-21 14:24:30 +00001339 break;
1340 case GL_CONSTANT_BORDER:
1341 convolve_2d_constant(*width, *height,
1342 (const GLfloat (*)[4]) srcImage,
1343 ctx->Convolution2D.Width,
1344 ctx->Convolution2D.Height,
1345 (const GLfloat (*)[4]) ctx->Convolution2D.Filter,
1346 (GLfloat (*)[4]) dstImage,
1347 ctx->Pixel.ConvolutionBorderColor[1]);
1348 break;
1349 case GL_REPLICATE_BORDER:
1350 convolve_2d_replicate(*width, *height,
1351 (const GLfloat (*)[4]) srcImage,
1352 ctx->Convolution2D.Width,
1353 ctx->Convolution2D.Height,
1354 (const GLfloat (*)[4])ctx->Convolution2D.Filter,
1355 (GLfloat (*)[4]) dstImage);
1356 break;
1357 default:
1358 ;
1359 }
1360}
1361
1362
1363void
1364_mesa_convolve_sep_image(const GLcontext *ctx,
1365 GLsizei *width, GLsizei *height,
1366 const GLfloat *srcImage, GLfloat *dstImage)
1367{
1368 const GLfloat *rowFilter = ctx->Separable2D.Filter;
1369 const GLfloat *colFilter = rowFilter + 4 * MAX_CONVOLUTION_WIDTH;
1370
1371 switch (ctx->Pixel.ConvolutionBorderMode[2]) {
1372 case GL_REDUCE:
1373 convolve_sep_reduce(*width, *height,
1374 (const GLfloat (*)[4]) srcImage,
Brian Paul7e708742000-08-22 18:54:25 +00001375 ctx->Separable2D.Width,
1376 ctx->Separable2D.Height,
Brian Pauld4b799b2000-08-21 14:24:30 +00001377 (const GLfloat (*)[4]) rowFilter,
1378 (const GLfloat (*)[4]) colFilter,
1379 (GLfloat (*)[4]) dstImage);
Brian Paul7e708742000-08-22 18:54:25 +00001380 *width = *width - (MAX2(ctx->Separable2D.Width, 1) - 1);
1381 *height = *height - (MAX2(ctx->Separable2D.Height, 1) - 1);
Brian Pauld4b799b2000-08-21 14:24:30 +00001382 break;
1383 case GL_CONSTANT_BORDER:
1384 convolve_sep_constant(*width, *height,
1385 (const GLfloat (*)[4]) srcImage,
Brian Paul7e708742000-08-22 18:54:25 +00001386 ctx->Separable2D.Width,
1387 ctx->Separable2D.Height,
Brian Pauld4b799b2000-08-21 14:24:30 +00001388 (const GLfloat (*)[4]) rowFilter,
1389 (const GLfloat (*)[4]) colFilter,
1390 (GLfloat (*)[4]) dstImage,
1391 ctx->Pixel.ConvolutionBorderColor[2]);
1392 break;
1393 case GL_REPLICATE_BORDER:
1394 convolve_sep_replicate(*width, *height,
1395 (const GLfloat (*)[4]) srcImage,
Brian Paul7e708742000-08-22 18:54:25 +00001396 ctx->Separable2D.Width,
1397 ctx->Separable2D.Height,
Brian Pauld4b799b2000-08-21 14:24:30 +00001398 (const GLfloat (*)[4]) rowFilter,
1399 (const GLfloat (*)[4]) colFilter,
1400 (GLfloat (*)[4]) dstImage);
1401 break;
1402 default:
1403 ;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001404 }
1405}
Brian Paul16461f72001-02-06 17:22:16 +00001406
1407
1408
1409/*
1410 * This function computes an image's size after convolution.
1411 * If the convolution border mode is GL_REDUCE, the post-convolution
1412 * image will be smaller than the original.
1413 */
1414void
1415_mesa_adjust_image_for_convolution(const GLcontext *ctx, GLuint dimensions,
1416 GLsizei *width, GLsizei *height)
1417{
1418 if (ctx->Pixel.Convolution1DEnabled
1419 && dimensions == 1
1420 && ctx->Pixel.ConvolutionBorderMode[0] == GL_REDUCE) {
1421 *width = *width - (MAX2(ctx->Convolution1D.Width, 1) - 1);
1422 }
1423 else if (ctx->Pixel.Convolution2DEnabled
1424 && dimensions > 1
1425 && ctx->Pixel.ConvolutionBorderMode[1] == GL_REDUCE) {
1426 *width = *width - (MAX2(ctx->Convolution2D.Width, 1) - 1);
1427 *height = *height - (MAX2(ctx->Convolution2D.Height, 1) - 1);
1428 }
1429 else if (ctx->Pixel.Separable2DEnabled
1430 && dimensions > 1
1431 && ctx->Pixel.ConvolutionBorderMode[2] == GL_REDUCE) {
1432 *width = *width - (MAX2(ctx->Separable2D.Width, 1) - 1);
1433 *height = *height - (MAX2(ctx->Separable2D.Height, 1) - 1);
1434 }
1435}