blob: 1f4eaf439b8790f18309166980de0e2b9fa67aae [file] [log] [blame]
Brian Paulcc8e37f2000-07-12 13:00:09 +00001/*
2 * Mesa 3-D graphics library
Brian Paulbd3b40a2004-10-31 17:36:23 +00003 * Version: 6.3
Brian Paulcc8e37f2000-07-12 13:00:09 +00004 *
Brian Paul4923e192004-02-28 22:30:58 +00005 * Copyright (C) 1999-2004 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 Paulbd3b40a2004-10-31 17:36:23 +0000147 if (ctx->Unpack.BufferObj->Name) {
148 /* unpack filter from PBO */
149 GLubyte *buf;
150 if (!_mesa_validate_pbo_access(&ctx->Unpack, width, 1, 1,
151 format, type, image)) {
152 _mesa_error(ctx, GL_INVALID_OPERATION,
153 "glConvolutionFilter1D(invalid PBO access)");
154 return;
155 }
156 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
157 GL_READ_ONLY_ARB,
158 ctx->Unpack.BufferObj);
159 if (!buf) {
160 /* buffer is already mapped - that's an error */
161 _mesa_error(ctx, GL_INVALID_OPERATION,
162 "glConvolutionFilter1D(PBO is mapped)");
163 return;
164 }
165 image = ADD_POINTERS(buf, image);
166 }
167 else if (!image) {
168 return;
169 }
170
Brian Paul8cfd08b2004-02-28 20:35:57 +0000171 _mesa_unpack_color_span_float(ctx, width, GL_RGBA,
Brian Paul147b0832000-08-23 14:31:25 +0000172 ctx->Convolution1D.Filter,
173 format, type, image, &ctx->Unpack,
Brian Paul4923e192004-02-28 22:30:58 +0000174 0); /* transferOps */
Brian Paul147b0832000-08-23 14:31:25 +0000175
Brian Paulbd3b40a2004-10-31 17:36:23 +0000176 if (ctx->Unpack.BufferObj->Name) {
177 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
178 ctx->Unpack.BufferObj);
179 }
180
Brian Paul450e9172004-10-31 18:40:55 +0000181 _mesa_scale_and_bias_rgba(width,
182 (GLfloat (*)[4]) ctx->Convolution1D.Filter,
183 ctx->Pixel.ConvolutionFilterScale[0][0],
184 ctx->Pixel.ConvolutionFilterScale[0][1],
185 ctx->Pixel.ConvolutionFilterScale[0][2],
186 ctx->Pixel.ConvolutionFilterScale[0][3],
187 ctx->Pixel.ConvolutionFilterBias[0][0],
188 ctx->Pixel.ConvolutionFilterBias[0][1],
189 ctx->Pixel.ConvolutionFilterBias[0][2],
190 ctx->Pixel.ConvolutionFilterBias[0][3]);
Keith Whitwella96308c2000-10-30 13:31:59 +0000191
Brian Paulb5012e12000-11-10 18:31:04 +0000192 ctx->NewState |= _NEW_PIXEL;
Brian Paul147b0832000-08-23 14:31:25 +0000193}
194
195
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000196void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000197_mesa_ConvolutionFilter2D(GLenum target, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *image)
198{
Brian Pauld0570642002-03-19 15:22:50 +0000199 GLint baseFormat;
Brian Paulb3050282003-12-04 03:19:46 +0000200 GLint i;
Brian Paul147b0832000-08-23 14:31:25 +0000201 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000202 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000203
204 if (target != GL_CONVOLUTION_2D) {
Brian Paul08836342001-03-03 20:33:27 +0000205 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000206 return;
207 }
208
209 baseFormat = base_filter_format(internalFormat);
210 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
Brian Paul08836342001-03-03 20:33:27 +0000211 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(internalFormat)");
Brian Paul147b0832000-08-23 14:31:25 +0000212 return;
213 }
214
215 if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
Brian Paul08836342001-03-03 20:33:27 +0000216 _mesa_error(ctx, GL_INVALID_VALUE, "glConvolutionFilter2D(width)");
Brian Paul147b0832000-08-23 14:31:25 +0000217 return;
218 }
219 if (height < 0 || height > MAX_CONVOLUTION_HEIGHT) {
Brian Paul08836342001-03-03 20:33:27 +0000220 _mesa_error(ctx, GL_INVALID_VALUE, "glConvolutionFilter2D(height)");
Brian Paul147b0832000-08-23 14:31:25 +0000221 return;
222 }
223
Brian Paulf959f6e2004-04-22 00:27:31 +0000224 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
Brian Paul08836342001-03-03 20:33:27 +0000225 _mesa_error(ctx, GL_INVALID_OPERATION, "glConvolutionFilter2D(format or type)");
Brian Paul90f042a2000-12-10 19:23:19 +0000226 return;
227 }
228 if (format == GL_COLOR_INDEX ||
Brian Paul147b0832000-08-23 14:31:25 +0000229 format == GL_STENCIL_INDEX ||
230 format == GL_DEPTH_COMPONENT ||
231 format == GL_INTENSITY ||
232 type == GL_BITMAP) {
Brian Paul08836342001-03-03 20:33:27 +0000233 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(format or type)");
Brian Paul147b0832000-08-23 14:31:25 +0000234 return;
235 }
236
Brian Paulb3050282003-12-04 03:19:46 +0000237 /* this should have been caught earlier */
238 assert(_mesa_components_in_format(format));
Brian Paul147b0832000-08-23 14:31:25 +0000239
240 ctx->Convolution2D.Format = format;
241 ctx->Convolution2D.InternalFormat = internalFormat;
242 ctx->Convolution2D.Width = width;
243 ctx->Convolution2D.Height = height;
244
Brian Paulbd3b40a2004-10-31 17:36:23 +0000245 if (ctx->Unpack.BufferObj->Name) {
246 /* unpack filter from PBO */
247 GLubyte *buf;
248 if (!_mesa_validate_pbo_access(&ctx->Unpack, width, height, 1,
249 format, type, image)) {
250 _mesa_error(ctx, GL_INVALID_OPERATION,
251 "glConvolutionFilter2D(invalid PBO access)");
252 return;
253 }
254 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
255 GL_READ_ONLY_ARB,
256 ctx->Unpack.BufferObj);
257 if (!buf) {
258 /* buffer is already mapped - that's an error */
259 _mesa_error(ctx, GL_INVALID_OPERATION,
260 "glConvolutionFilter2D(PBO is mapped)");
261 return;
262 }
263 image = ADD_POINTERS(buf, image);
264 }
265 else if (!image) {
266 return;
267 }
268
Brian Paul147b0832000-08-23 14:31:25 +0000269 /* Unpack filter image. We always store filters in RGBA format. */
270 for (i = 0; i < height; i++) {
271 const GLvoid *src = _mesa_image_address(&ctx->Unpack, image, width,
272 height, format, type, 0, i, 0);
273 GLfloat *dst = ctx->Convolution2D.Filter + i * width * 4;
Brian Paul8cfd08b2004-02-28 20:35:57 +0000274 _mesa_unpack_color_span_float(ctx, width, GL_RGBA, dst,
Brian Paul147b0832000-08-23 14:31:25 +0000275 format, type, src, &ctx->Unpack,
Brian Paul4923e192004-02-28 22:30:58 +0000276 0); /* transferOps */
Brian Paul147b0832000-08-23 14:31:25 +0000277 }
278
Brian Paulbd3b40a2004-10-31 17:36:23 +0000279 if (ctx->Unpack.BufferObj->Name) {
280 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
281 ctx->Unpack.BufferObj);
282 }
283
Brian Paul450e9172004-10-31 18:40:55 +0000284 _mesa_scale_and_bias_rgba(width * height,
285 (GLfloat (*)[4]) ctx->Convolution2D.Filter,
286 ctx->Pixel.ConvolutionFilterScale[1][0],
287 ctx->Pixel.ConvolutionFilterScale[1][1],
288 ctx->Pixel.ConvolutionFilterScale[1][2],
289 ctx->Pixel.ConvolutionFilterScale[1][3],
290 ctx->Pixel.ConvolutionFilterBias[1][0],
291 ctx->Pixel.ConvolutionFilterBias[1][1],
292 ctx->Pixel.ConvolutionFilterBias[1][2],
293 ctx->Pixel.ConvolutionFilterBias[1][3]);
Keith Whitwella96308c2000-10-30 13:31:59 +0000294
Brian Paulb5012e12000-11-10 18:31:04 +0000295 ctx->NewState |= _NEW_PIXEL;
Brian Paul147b0832000-08-23 14:31:25 +0000296}
297
298
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000299void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000300_mesa_ConvolutionParameterf(GLenum target, GLenum pname, GLfloat param)
301{
302 GET_CURRENT_CONTEXT(ctx);
303 GLuint c;
Keith Whitwell58e99172001-01-05 02:26:48 +0000304 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000305
306 switch (target) {
307 case GL_CONVOLUTION_1D:
308 c = 0;
309 break;
310 case GL_CONVOLUTION_2D:
311 c = 1;
312 break;
313 case GL_SEPARABLE_2D:
314 c = 2;
315 break;
316 default:
Brian Paul08836342001-03-03 20:33:27 +0000317 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000318 return;
319 }
320
321 switch (pname) {
322 case GL_CONVOLUTION_BORDER_MODE:
323 if (param == (GLfloat) GL_REDUCE ||
324 param == (GLfloat) GL_CONSTANT_BORDER ||
325 param == (GLfloat) GL_REPLICATE_BORDER) {
326 ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) param;
327 }
328 else {
Brian Paul08836342001-03-03 20:33:27 +0000329 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(params)");
Brian Paul147b0832000-08-23 14:31:25 +0000330 return;
331 }
332 break;
333 default:
Brian Paul08836342001-03-03 20:33:27 +0000334 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(pname)");
Brian Paul147b0832000-08-23 14:31:25 +0000335 return;
336 }
Keith Whitwella96308c2000-10-30 13:31:59 +0000337
338 ctx->NewState |= _NEW_PIXEL;
Brian Paul147b0832000-08-23 14:31:25 +0000339}
340
341
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000342void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000343_mesa_ConvolutionParameterfv(GLenum target, GLenum pname, const GLfloat *params)
344{
345 GET_CURRENT_CONTEXT(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000346 GLuint c;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000347 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000348
349 switch (target) {
350 case GL_CONVOLUTION_1D:
351 c = 0;
Brian Paul147b0832000-08-23 14:31:25 +0000352 break;
353 case GL_CONVOLUTION_2D:
354 c = 1;
Brian Paul147b0832000-08-23 14:31:25 +0000355 break;
356 case GL_SEPARABLE_2D:
357 c = 2;
Brian Paul147b0832000-08-23 14:31:25 +0000358 break;
359 default:
Brian Paul08836342001-03-03 20:33:27 +0000360 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000361 return;
362 }
363
364 switch (pname) {
365 case GL_CONVOLUTION_BORDER_COLOR:
366 COPY_4V(ctx->Pixel.ConvolutionBorderColor[c], params);
367 break;
368 case GL_CONVOLUTION_BORDER_MODE:
369 if (params[0] == (GLfloat) GL_REDUCE ||
370 params[0] == (GLfloat) GL_CONSTANT_BORDER ||
371 params[0] == (GLfloat) GL_REPLICATE_BORDER) {
372 ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) params[0];
373 }
374 else {
Brian Paul08836342001-03-03 20:33:27 +0000375 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(params)");
Brian Paul147b0832000-08-23 14:31:25 +0000376 return;
377 }
378 break;
379 case GL_CONVOLUTION_FILTER_SCALE:
380 COPY_4V(ctx->Pixel.ConvolutionFilterScale[c], params);
381 break;
382 case GL_CONVOLUTION_FILTER_BIAS:
383 COPY_4V(ctx->Pixel.ConvolutionFilterBias[c], params);
384 break;
385 default:
Brian Paul08836342001-03-03 20:33:27 +0000386 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(pname)");
Brian Paul147b0832000-08-23 14:31:25 +0000387 return;
388 }
Keith Whitwella96308c2000-10-30 13:31:59 +0000389
390 ctx->NewState |= _NEW_PIXEL;
Brian Paul147b0832000-08-23 14:31:25 +0000391}
392
393
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000394void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000395_mesa_ConvolutionParameteri(GLenum target, GLenum pname, GLint param)
396{
397 GET_CURRENT_CONTEXT(ctx);
398 GLuint c;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000399 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000400
401 switch (target) {
402 case GL_CONVOLUTION_1D:
403 c = 0;
404 break;
405 case GL_CONVOLUTION_2D:
406 c = 1;
407 break;
408 case GL_SEPARABLE_2D:
409 c = 2;
410 break;
411 default:
Brian Paul08836342001-03-03 20:33:27 +0000412 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000413 return;
414 }
415
416 switch (pname) {
417 case GL_CONVOLUTION_BORDER_MODE:
418 if (param == (GLint) GL_REDUCE ||
419 param == (GLint) GL_CONSTANT_BORDER ||
420 param == (GLint) GL_REPLICATE_BORDER) {
421 ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) param;
422 }
423 else {
Brian Paul08836342001-03-03 20:33:27 +0000424 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(params)");
Brian Paul147b0832000-08-23 14:31:25 +0000425 return;
426 }
427 break;
428 default:
Brian Paul08836342001-03-03 20:33:27 +0000429 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(pname)");
Brian Paul147b0832000-08-23 14:31:25 +0000430 return;
431 }
Keith Whitwella96308c2000-10-30 13:31:59 +0000432
433 ctx->NewState |= _NEW_PIXEL;
Brian Paul147b0832000-08-23 14:31:25 +0000434}
435
436
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000437void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000438_mesa_ConvolutionParameteriv(GLenum target, GLenum pname, const GLint *params)
439{
440 GET_CURRENT_CONTEXT(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000441 GLuint c;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000442 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000443
444 switch (target) {
445 case GL_CONVOLUTION_1D:
446 c = 0;
Brian Paul147b0832000-08-23 14:31:25 +0000447 break;
448 case GL_CONVOLUTION_2D:
449 c = 1;
Brian Paul147b0832000-08-23 14:31:25 +0000450 break;
451 case GL_SEPARABLE_2D:
452 c = 2;
Brian Paul147b0832000-08-23 14:31:25 +0000453 break;
454 default:
Brian Paul08836342001-03-03 20:33:27 +0000455 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000456 return;
457 }
458
459 switch (pname) {
460 case GL_CONVOLUTION_BORDER_COLOR:
461 ctx->Pixel.ConvolutionBorderColor[c][0] = INT_TO_FLOAT(params[0]);
462 ctx->Pixel.ConvolutionBorderColor[c][1] = INT_TO_FLOAT(params[1]);
463 ctx->Pixel.ConvolutionBorderColor[c][2] = INT_TO_FLOAT(params[2]);
464 ctx->Pixel.ConvolutionBorderColor[c][3] = INT_TO_FLOAT(params[3]);
465 break;
466 case GL_CONVOLUTION_BORDER_MODE:
467 if (params[0] == (GLint) GL_REDUCE ||
468 params[0] == (GLint) GL_CONSTANT_BORDER ||
469 params[0] == (GLint) GL_REPLICATE_BORDER) {
470 ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) params[0];
471 }
472 else {
Brian Paul08836342001-03-03 20:33:27 +0000473 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(params)");
Brian Paul147b0832000-08-23 14:31:25 +0000474 return;
475 }
476 break;
477 case GL_CONVOLUTION_FILTER_SCALE:
Karl Schultz7c426812001-09-19 20:30:44 +0000478 /* COPY_4V(ctx->Pixel.ConvolutionFilterScale[c], params); */
479 /* need cast to prevent compiler warnings */
480 ctx->Pixel.ConvolutionFilterScale[c][0] = (GLfloat) params[0];
481 ctx->Pixel.ConvolutionFilterScale[c][1] = (GLfloat) params[1];
482 ctx->Pixel.ConvolutionFilterScale[c][2] = (GLfloat) params[2];
483 ctx->Pixel.ConvolutionFilterScale[c][3] = (GLfloat) params[3];
Brian Paul147b0832000-08-23 14:31:25 +0000484 break;
485 case GL_CONVOLUTION_FILTER_BIAS:
Karl Schultz7c426812001-09-19 20:30:44 +0000486 /* COPY_4V(ctx->Pixel.ConvolutionFilterBias[c], params); */
487 /* need cast to prevent compiler warnings */
488 ctx->Pixel.ConvolutionFilterBias[c][0] = (GLfloat) params[0];
489 ctx->Pixel.ConvolutionFilterBias[c][1] = (GLfloat) params[1];
490 ctx->Pixel.ConvolutionFilterBias[c][2] = (GLfloat) params[2];
491 ctx->Pixel.ConvolutionFilterBias[c][3] = (GLfloat) params[3];
Brian Paul147b0832000-08-23 14:31:25 +0000492 break;
493 default:
Brian Paul08836342001-03-03 20:33:27 +0000494 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(pname)");
Brian Paul147b0832000-08-23 14:31:25 +0000495 return;
496 }
Keith Whitwella96308c2000-10-30 13:31:59 +0000497
498 ctx->NewState |= _NEW_PIXEL;
Brian Paul147b0832000-08-23 14:31:25 +0000499}
500
501
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000502void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000503_mesa_CopyConvolutionFilter1D(GLenum target, GLenum internalFormat, GLint x, GLint y, GLsizei width)
504{
Brian Pauld0570642002-03-19 15:22:50 +0000505 GLint baseFormat;
Brian Paul147b0832000-08-23 14:31:25 +0000506 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000507 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000508
509 if (target != GL_CONVOLUTION_1D) {
Brian Paul08836342001-03-03 20:33:27 +0000510 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter1D(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000511 return;
512 }
513
514 baseFormat = base_filter_format(internalFormat);
515 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
Brian Paul08836342001-03-03 20:33:27 +0000516 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter1D(internalFormat)");
Brian Paul147b0832000-08-23 14:31:25 +0000517 return;
518 }
519
520 if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
Brian Paul08836342001-03-03 20:33:27 +0000521 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter1D(width)");
Brian Paul147b0832000-08-23 14:31:25 +0000522 return;
523 }
524
Keith Whitwell70989242001-03-19 02:25:35 +0000525 ctx->Driver.CopyConvolutionFilter1D( ctx, target,
526 internalFormat, x, y, width);
Brian Paul147b0832000-08-23 14:31:25 +0000527}
528
529
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000530void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000531_mesa_CopyConvolutionFilter2D(GLenum target, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height)
532{
Brian Pauld0570642002-03-19 15:22:50 +0000533 GLint baseFormat;
Brian Paul147b0832000-08-23 14:31:25 +0000534 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000535 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000536
537 if (target != GL_CONVOLUTION_2D) {
Brian Paul08836342001-03-03 20:33:27 +0000538 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter2D(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000539 return;
540 }
541
542 baseFormat = base_filter_format(internalFormat);
543 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
Brian Paul08836342001-03-03 20:33:27 +0000544 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter2D(internalFormat)");
Brian Paul147b0832000-08-23 14:31:25 +0000545 return;
546 }
547
548 if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
Brian Paul08836342001-03-03 20:33:27 +0000549 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter2D(width)");
Brian Paul147b0832000-08-23 14:31:25 +0000550 return;
551 }
552 if (height < 0 || height > MAX_CONVOLUTION_HEIGHT) {
Brian Paul08836342001-03-03 20:33:27 +0000553 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter2D(height)");
Brian Paul147b0832000-08-23 14:31:25 +0000554 return;
555 }
556
Keith Whitwell70989242001-03-19 02:25:35 +0000557 ctx->Driver.CopyConvolutionFilter2D( ctx, target, internalFormat, x, y,
558 width, height );
Brian Paul147b0832000-08-23 14:31:25 +0000559}
560
561
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000562void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000563_mesa_GetConvolutionFilter(GLenum target, GLenum format, GLenum type, GLvoid *image)
564{
Brian Paulf75d6972000-09-05 20:28:56 +0000565 const struct gl_convolution_attrib *filter;
Brian Paulb51b0a82001-03-07 05:06:11 +0000566 GLuint row;
Brian Paul147b0832000-08-23 14:31:25 +0000567 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000568 ASSERT_OUTSIDE_BEGIN_END(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000569
Brian Paul0c000ec2000-11-21 23:26:13 +0000570 if (ctx->NewState) {
Brian Paul08836342001-03-03 20:33:27 +0000571 _mesa_update_state(ctx);
Brian Paul0c000ec2000-11-21 23:26:13 +0000572 }
573
Brian Paulf959f6e2004-04-22 00:27:31 +0000574 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
Brian Paul08836342001-03-03 20:33:27 +0000575 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetConvolutionFilter(format or type)");
Brian Paul90f042a2000-12-10 19:23:19 +0000576 return;
577 }
578
579 if (format == GL_COLOR_INDEX ||
Brian Paul147b0832000-08-23 14:31:25 +0000580 format == GL_STENCIL_INDEX ||
581 format == GL_DEPTH_COMPONENT ||
582 format == GL_INTENSITY ||
583 type == GL_BITMAP) {
Brian Paul08836342001-03-03 20:33:27 +0000584 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(format or type)");
Brian Paul147b0832000-08-23 14:31:25 +0000585 return;
586 }
587
Brian Paulf75d6972000-09-05 20:28:56 +0000588 switch (target) {
589 case GL_CONVOLUTION_1D:
590 filter = &(ctx->Convolution1D);
591 break;
592 case GL_CONVOLUTION_2D:
593 filter = &(ctx->Convolution2D);
594 break;
595 default:
Brian Paul08836342001-03-03 20:33:27 +0000596 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(target)");
Brian Paulf75d6972000-09-05 20:28:56 +0000597 return;
598 }
599
Brian Paulbd3b40a2004-10-31 17:36:23 +0000600 if (ctx->Pack.BufferObj->Name) {
601 /* Pack the filter into a PBO */
602 GLubyte *buf;
603 if (!_mesa_validate_pbo_access(&ctx->Pack, filter->Width, filter->Height,
604 1, format, type, image)) {
605 _mesa_error(ctx, GL_INVALID_OPERATION,
606 "glGetConvolutionFilter(invalid PBO access)");
607 return;
608 }
609 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
610 GL_WRITE_ONLY_ARB,
611 ctx->Pack.BufferObj);
612 if (!buf) {
613 /* buffer is already mapped - that's an error */
614 _mesa_error(ctx, GL_INVALID_OPERATION,
615 "glGetConvolutionFilter(PBO is mapped)");
616 return;
617 }
618 image = ADD_POINTERS(image, buf);
619 }
620
Brian Paulf75d6972000-09-05 20:28:56 +0000621 for (row = 0; row < filter->Height; row++) {
622 GLvoid *dst = _mesa_image_address( &ctx->Pack, image, filter->Width,
623 filter->Height, format, type,
624 0, row, 0);
625 const GLfloat *src = filter->Filter + row * filter->Width * 4;
Brian Paul8cfd08b2004-02-28 20:35:57 +0000626 _mesa_pack_rgba_span_float(ctx, filter->Width,
Brian Paulf75d6972000-09-05 20:28:56 +0000627 (const GLfloat (*)[4]) src,
628 format, type, dst, &ctx->Pack, 0);
629 }
Brian Paulbd3b40a2004-10-31 17:36:23 +0000630
631 if (ctx->Pack.BufferObj->Name) {
632 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
633 ctx->Pack.BufferObj);
634 }
Brian Paul147b0832000-08-23 14:31:25 +0000635}
636
637
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000638void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000639_mesa_GetConvolutionParameterfv(GLenum target, GLenum pname, GLfloat *params)
640{
641 GET_CURRENT_CONTEXT(ctx);
642 const struct gl_convolution_attrib *conv;
643 GLuint c;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000644 ASSERT_OUTSIDE_BEGIN_END(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000645
646 switch (target) {
647 case GL_CONVOLUTION_1D:
648 c = 0;
649 conv = &ctx->Convolution1D;
650 break;
651 case GL_CONVOLUTION_2D:
652 c = 1;
653 conv = &ctx->Convolution2D;
654 break;
655 case GL_SEPARABLE_2D:
656 c = 2;
657 conv = &ctx->Separable2D;
658 break;
659 default:
Brian Paul08836342001-03-03 20:33:27 +0000660 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameterfv(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000661 return;
662 }
663
664 switch (pname) {
665 case GL_CONVOLUTION_BORDER_COLOR:
666 COPY_4V(params, ctx->Pixel.ConvolutionBorderColor[c]);
667 break;
668 case GL_CONVOLUTION_BORDER_MODE:
669 *params = (GLfloat) ctx->Pixel.ConvolutionBorderMode[c];
670 break;
671 case GL_CONVOLUTION_FILTER_SCALE:
672 COPY_4V(params, ctx->Pixel.ConvolutionFilterScale[c]);
673 break;
674 case GL_CONVOLUTION_FILTER_BIAS:
675 COPY_4V(params, ctx->Pixel.ConvolutionFilterBias[c]);
676 break;
677 case GL_CONVOLUTION_FORMAT:
678 *params = (GLfloat) conv->Format;
679 break;
680 case GL_CONVOLUTION_WIDTH:
681 *params = (GLfloat) conv->Width;
682 break;
683 case GL_CONVOLUTION_HEIGHT:
684 *params = (GLfloat) conv->Height;
685 break;
686 case GL_MAX_CONVOLUTION_WIDTH:
687 *params = (GLfloat) ctx->Const.MaxConvolutionWidth;
688 break;
689 case GL_MAX_CONVOLUTION_HEIGHT:
690 *params = (GLfloat) ctx->Const.MaxConvolutionHeight;
691 break;
692 default:
Brian Paul08836342001-03-03 20:33:27 +0000693 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameterfv(pname)");
Brian Paul147b0832000-08-23 14:31:25 +0000694 return;
695 }
696}
697
698
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000699void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000700_mesa_GetConvolutionParameteriv(GLenum target, GLenum pname, GLint *params)
701{
702 GET_CURRENT_CONTEXT(ctx);
703 const struct gl_convolution_attrib *conv;
704 GLuint c;
Keith Whitwellcab974c2000-12-26 05:09:27 +0000705 ASSERT_OUTSIDE_BEGIN_END(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000706
707 switch (target) {
708 case GL_CONVOLUTION_1D:
709 c = 0;
710 conv = &ctx->Convolution1D;
711 break;
712 case GL_CONVOLUTION_2D:
713 c = 1;
714 conv = &ctx->Convolution2D;
715 break;
716 case GL_SEPARABLE_2D:
717 c = 2;
718 conv = &ctx->Separable2D;
719 break;
720 default:
Brian Paul08836342001-03-03 20:33:27 +0000721 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameteriv(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000722 return;
723 }
724
725 switch (pname) {
726 case GL_CONVOLUTION_BORDER_COLOR:
727 params[0] = FLOAT_TO_INT(ctx->Pixel.ConvolutionBorderColor[c][0]);
728 params[1] = FLOAT_TO_INT(ctx->Pixel.ConvolutionBorderColor[c][1]);
729 params[2] = FLOAT_TO_INT(ctx->Pixel.ConvolutionBorderColor[c][2]);
730 params[3] = FLOAT_TO_INT(ctx->Pixel.ConvolutionBorderColor[c][3]);
731 break;
732 case GL_CONVOLUTION_BORDER_MODE:
733 *params = (GLint) ctx->Pixel.ConvolutionBorderMode[c];
734 break;
735 case GL_CONVOLUTION_FILTER_SCALE:
736 params[0] = (GLint) ctx->Pixel.ConvolutionFilterScale[c][0];
737 params[1] = (GLint) ctx->Pixel.ConvolutionFilterScale[c][1];
738 params[2] = (GLint) ctx->Pixel.ConvolutionFilterScale[c][2];
739 params[3] = (GLint) ctx->Pixel.ConvolutionFilterScale[c][3];
740 break;
741 case GL_CONVOLUTION_FILTER_BIAS:
742 params[0] = (GLint) ctx->Pixel.ConvolutionFilterBias[c][0];
743 params[1] = (GLint) ctx->Pixel.ConvolutionFilterBias[c][1];
744 params[2] = (GLint) ctx->Pixel.ConvolutionFilterBias[c][2];
745 params[3] = (GLint) ctx->Pixel.ConvolutionFilterBias[c][3];
746 break;
747 case GL_CONVOLUTION_FORMAT:
748 *params = (GLint) conv->Format;
749 break;
750 case GL_CONVOLUTION_WIDTH:
751 *params = (GLint) conv->Width;
752 break;
753 case GL_CONVOLUTION_HEIGHT:
754 *params = (GLint) conv->Height;
755 break;
756 case GL_MAX_CONVOLUTION_WIDTH:
757 *params = (GLint) ctx->Const.MaxConvolutionWidth;
758 break;
759 case GL_MAX_CONVOLUTION_HEIGHT:
760 *params = (GLint) ctx->Const.MaxConvolutionHeight;
761 break;
762 default:
Brian Paul08836342001-03-03 20:33:27 +0000763 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameteriv(pname)");
Brian Paul147b0832000-08-23 14:31:25 +0000764 return;
765 }
766}
767
768
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000769void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000770_mesa_GetSeparableFilter(GLenum target, GLenum format, GLenum type, GLvoid *row, GLvoid *column, GLvoid *span)
771{
Brian Paulf75d6972000-09-05 20:28:56 +0000772 const GLint colStart = MAX_CONVOLUTION_WIDTH * 4;
773 const struct gl_convolution_attrib *filter;
Brian Paul147b0832000-08-23 14:31:25 +0000774 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000775 ASSERT_OUTSIDE_BEGIN_END(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000776
Brian Paul0c000ec2000-11-21 23:26:13 +0000777 if (ctx->NewState) {
Brian Paul08836342001-03-03 20:33:27 +0000778 _mesa_update_state(ctx);
Brian Paul0c000ec2000-11-21 23:26:13 +0000779 }
780
Brian Paul147b0832000-08-23 14:31:25 +0000781 if (target != GL_SEPARABLE_2D) {
Brian Paul08836342001-03-03 20:33:27 +0000782 _mesa_error(ctx, GL_INVALID_ENUM, "glGetSeparableFilter(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000783 return;
784 }
785
Brian Paulf959f6e2004-04-22 00:27:31 +0000786 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
Brian Paul08836342001-03-03 20:33:27 +0000787 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetConvolutionFilter(format or type)");
Brian Paul90f042a2000-12-10 19:23:19 +0000788 return;
789 }
790
791 if (format == GL_COLOR_INDEX ||
Brian Paul147b0832000-08-23 14:31:25 +0000792 format == GL_STENCIL_INDEX ||
793 format == GL_DEPTH_COMPONENT ||
794 format == GL_INTENSITY ||
795 type == GL_BITMAP) {
Brian Paul08836342001-03-03 20:33:27 +0000796 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(format or type)");
Brian Paul147b0832000-08-23 14:31:25 +0000797 return;
798 }
799
Brian Paulf75d6972000-09-05 20:28:56 +0000800 filter = &ctx->Separable2D;
801
Brian Paulbd3b40a2004-10-31 17:36:23 +0000802 if (ctx->Pack.BufferObj->Name) {
803 /* Pack filter into PBO */
804 GLubyte *buf;
805 if (!_mesa_validate_pbo_access(&ctx->Pack, filter->Width, 1, 1,
806 format, type, row)) {
807 _mesa_error(ctx, GL_INVALID_OPERATION,
808 "glGetSeparableFilter(invalid PBO access, width)");
809 return;
810 }
811 if (!_mesa_validate_pbo_access(&ctx->Pack, filter->Height, 1, 1,
812 format, type, column)) {
813 _mesa_error(ctx, GL_INVALID_OPERATION,
814 "glGetSeparableFilter(invalid PBO access, height)");
815 return;
816 }
817 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
818 GL_WRITE_ONLY_ARB,
819 ctx->Pack.BufferObj);
820 if (!buf) {
821 /* buffer is already mapped - that's an error */
822 _mesa_error(ctx, GL_INVALID_OPERATION,
823 "glGetSeparableFilter(PBO is mapped)");
824 return;
825 }
826 row = ADD_POINTERS(buf, row);
827 column = ADD_POINTERS(buf, column);
828 }
829
Brian Paulf75d6972000-09-05 20:28:56 +0000830 /* Row filter */
Brian Paulbd3b40a2004-10-31 17:36:23 +0000831 if (row) {
Brian Paulf75d6972000-09-05 20:28:56 +0000832 GLvoid *dst = _mesa_image_address( &ctx->Pack, row, filter->Width,
833 filter->Height, format, type,
834 0, 0, 0);
Brian Paul8cfd08b2004-02-28 20:35:57 +0000835 _mesa_pack_rgba_span_float(ctx, filter->Width,
Brian Paulf75d6972000-09-05 20:28:56 +0000836 (const GLfloat (*)[4]) filter->Filter,
837 format, type, dst, &ctx->Pack, 0);
838 }
839
840 /* Column filter */
Brian Paulbd3b40a2004-10-31 17:36:23 +0000841 if (column) {
Brian Paulf75d6972000-09-05 20:28:56 +0000842 GLvoid *dst = _mesa_image_address( &ctx->Pack, column, filter->Width,
843 1, format, type,
844 0, 0, 0);
845 const GLfloat *src = filter->Filter + colStart;
Brian Paul8cfd08b2004-02-28 20:35:57 +0000846 _mesa_pack_rgba_span_float(ctx, filter->Height,
Brian Paulf75d6972000-09-05 20:28:56 +0000847 (const GLfloat (*)[4]) src,
848 format, type, dst, &ctx->Pack, 0);
849 }
850
851 (void) span; /* unused at this time */
Brian Paulbd3b40a2004-10-31 17:36:23 +0000852
853 if (ctx->Pack.BufferObj->Name) {
854 /* Pack filter into PBO */
855 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
856 ctx->Unpack.BufferObj);
857 }
Brian Paul147b0832000-08-23 14:31:25 +0000858}
859
860
Kendall Bennettc40d1dd2003-10-21 22:22:17 +0000861void GLAPIENTRY
Brian Paul147b0832000-08-23 14:31:25 +0000862_mesa_SeparableFilter2D(GLenum target, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *row, const GLvoid *column)
863{
864 const GLint colStart = MAX_CONVOLUTION_WIDTH * 4;
Brian Pauld0570642002-03-19 15:22:50 +0000865 GLint baseFormat;
Brian Paul147b0832000-08-23 14:31:25 +0000866 GET_CURRENT_CONTEXT(ctx);
Keith Whitwellcab974c2000-12-26 05:09:27 +0000867 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
Brian Paul147b0832000-08-23 14:31:25 +0000868
869 if (target != GL_SEPARABLE_2D) {
Brian Paul08836342001-03-03 20:33:27 +0000870 _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(target)");
Brian Paul147b0832000-08-23 14:31:25 +0000871 return;
872 }
873
874 baseFormat = base_filter_format(internalFormat);
875 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
Brian Paul08836342001-03-03 20:33:27 +0000876 _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(internalFormat)");
Brian Paul147b0832000-08-23 14:31:25 +0000877 return;
878 }
879
880 if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
Brian Paul08836342001-03-03 20:33:27 +0000881 _mesa_error(ctx, GL_INVALID_VALUE, "glSeparableFilter2D(width)");
Brian Paul147b0832000-08-23 14:31:25 +0000882 return;
883 }
884 if (height < 0 || height > MAX_CONVOLUTION_HEIGHT) {
Brian Paul08836342001-03-03 20:33:27 +0000885 _mesa_error(ctx, GL_INVALID_VALUE, "glSeparableFilter2D(height)");
Brian Paul147b0832000-08-23 14:31:25 +0000886 return;
887 }
888
Brian Paulf959f6e2004-04-22 00:27:31 +0000889 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
Brian Paul08836342001-03-03 20:33:27 +0000890 _mesa_error(ctx, GL_INVALID_OPERATION, "glSeparableFilter2D(format or type)");
Brian Paul90f042a2000-12-10 19:23:19 +0000891 return;
892 }
893
894 if (format == GL_COLOR_INDEX ||
Brian Paul147b0832000-08-23 14:31:25 +0000895 format == GL_STENCIL_INDEX ||
896 format == GL_DEPTH_COMPONENT ||
897 format == GL_INTENSITY ||
898 type == GL_BITMAP) {
Brian Paul08836342001-03-03 20:33:27 +0000899 _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(format or type)");
Brian Paul147b0832000-08-23 14:31:25 +0000900 return;
901 }
902
903 ctx->Separable2D.Format = format;
904 ctx->Separable2D.InternalFormat = internalFormat;
905 ctx->Separable2D.Width = width;
906 ctx->Separable2D.Height = height;
907
Brian Paulbd3b40a2004-10-31 17:36:23 +0000908 if (ctx->Unpack.BufferObj->Name) {
909 /* unpack filter from PBO */
910 GLubyte *buf;
911 if (!_mesa_validate_pbo_access(&ctx->Unpack, width, 1, 1,
912 format, type, row)) {
913 _mesa_error(ctx, GL_INVALID_OPERATION,
914 "glSeparableFilter2D(invalid PBO access, width)");
915 return;
916 }
917 if (!_mesa_validate_pbo_access(&ctx->Unpack, height, 1, 1,
918 format, type, column)) {
919 _mesa_error(ctx, GL_INVALID_OPERATION,
920 "glSeparableFilter2D(invalid PBO access, height)");
921 return;
922 }
923 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
924 GL_READ_ONLY_ARB,
925 ctx->Unpack.BufferObj);
926 if (!buf) {
927 /* buffer is already mapped - that's an error */
928 _mesa_error(ctx, GL_INVALID_OPERATION,
929 "glSeparableFilter2D(PBO is mapped)");
930 return;
931 }
932 row = ADD_POINTERS(buf, row);
933 column = ADD_POINTERS(buf, column);
934 }
Brian Paul147b0832000-08-23 14:31:25 +0000935
Brian Paulbd3b40a2004-10-31 17:36:23 +0000936 /* unpack row filter */
937 if (row) {
938 _mesa_unpack_color_span_float(ctx, width, GL_RGBA,
939 ctx->Separable2D.Filter,
940 format, type, row, &ctx->Unpack,
941 0); /* transferOps */
942
Brian Paul450e9172004-10-31 18:40:55 +0000943 _mesa_scale_and_bias_rgba(width,
944 (GLfloat (*)[4]) ctx->Separable2D.Filter,
945 ctx->Pixel.ConvolutionFilterScale[2][0],
946 ctx->Pixel.ConvolutionFilterScale[2][1],
947 ctx->Pixel.ConvolutionFilterScale[2][2],
948 ctx->Pixel.ConvolutionFilterScale[2][3],
949 ctx->Pixel.ConvolutionFilterBias[2][0],
950 ctx->Pixel.ConvolutionFilterBias[2][1],
951 ctx->Pixel.ConvolutionFilterBias[2][2],
952 ctx->Pixel.ConvolutionFilterBias[2][3]);
Brian Paul147b0832000-08-23 14:31:25 +0000953 }
954
955 /* unpack column filter */
Brian Paulbd3b40a2004-10-31 17:36:23 +0000956 if (column) {
957 _mesa_unpack_color_span_float(ctx, height, GL_RGBA,
958 &ctx->Separable2D.Filter[colStart],
959 format, type, column, &ctx->Unpack,
960 0); /* transferOps */
Brian Paul147b0832000-08-23 14:31:25 +0000961
Brian Paul450e9172004-10-31 18:40:55 +0000962 _mesa_scale_and_bias_rgba(height,
963 (GLfloat (*)[4]) (ctx->Separable2D.Filter + colStart),
964 ctx->Pixel.ConvolutionFilterScale[2][0],
965 ctx->Pixel.ConvolutionFilterScale[2][1],
966 ctx->Pixel.ConvolutionFilterScale[2][2],
967 ctx->Pixel.ConvolutionFilterScale[2][3],
968 ctx->Pixel.ConvolutionFilterBias[2][0],
969 ctx->Pixel.ConvolutionFilterBias[2][1],
970 ctx->Pixel.ConvolutionFilterBias[2][2],
971 ctx->Pixel.ConvolutionFilterBias[2][3]);
Brian Paulbd3b40a2004-10-31 17:36:23 +0000972 }
973
974 if (ctx->Unpack.BufferObj->Name) {
975 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
976 ctx->Unpack.BufferObj);
Brian Paul147b0832000-08-23 14:31:25 +0000977 }
Jouk Jansen5e3bc0c2000-11-22 07:32:16 +0000978
Brian Paulb5012e12000-11-10 18:31:04 +0000979 ctx->NewState |= _NEW_PIXEL;
Brian Paul147b0832000-08-23 14:31:25 +0000980}
981
982
983/**********************************************************************/
984/*** image convolution functions ***/
985/**********************************************************************/
986
Brian Pauld4b799b2000-08-21 14:24:30 +0000987static void
988convolve_1d_reduce(GLint srcWidth, const GLfloat src[][4],
989 GLint filterWidth, const GLfloat filter[][4],
990 GLfloat dest[][4])
Brian Paulcc8e37f2000-07-12 13:00:09 +0000991{
Brian Paul7e708742000-08-22 18:54:25 +0000992 GLint dstWidth;
Brian Paulcc8e37f2000-07-12 13:00:09 +0000993 GLint i, n;
994
Brian Paul7e708742000-08-22 18:54:25 +0000995 if (filterWidth >= 1)
996 dstWidth = srcWidth - (filterWidth - 1);
997 else
998 dstWidth = srcWidth;
999
Brian Paulcc8e37f2000-07-12 13:00:09 +00001000 if (dstWidth <= 0)
1001 return; /* null result */
1002
1003 for (i = 0; i < dstWidth; i++) {
1004 GLfloat sumR = 0.0;
1005 GLfloat sumG = 0.0;
1006 GLfloat sumB = 0.0;
1007 GLfloat sumA = 0.0;
1008 for (n = 0; n < filterWidth; n++) {
1009 sumR += src[i + n][RCOMP] * filter[n][RCOMP];
1010 sumG += src[i + n][GCOMP] * filter[n][GCOMP];
1011 sumB += src[i + n][BCOMP] * filter[n][BCOMP];
1012 sumA += src[i + n][ACOMP] * filter[n][ACOMP];
1013 }
1014 dest[i][RCOMP] = sumR;
1015 dest[i][GCOMP] = sumG;
1016 dest[i][BCOMP] = sumB;
1017 dest[i][ACOMP] = sumA;
1018 }
1019}
1020
1021
Brian Pauld4b799b2000-08-21 14:24:30 +00001022static void
1023convolve_1d_constant(GLint srcWidth, const GLfloat src[][4],
1024 GLint filterWidth, const GLfloat filter[][4],
1025 GLfloat dest[][4],
1026 const GLfloat borderColor[4])
Brian Paulcc8e37f2000-07-12 13:00:09 +00001027{
1028 const GLint halfFilterWidth = filterWidth / 2;
1029 GLint i, n;
1030
1031 for (i = 0; i < srcWidth; i++) {
1032 GLfloat sumR = 0.0;
1033 GLfloat sumG = 0.0;
1034 GLfloat sumB = 0.0;
1035 GLfloat sumA = 0.0;
1036 for (n = 0; n < filterWidth; n++) {
1037 if (i + n < halfFilterWidth || i + n - halfFilterWidth >= srcWidth) {
1038 sumR += borderColor[RCOMP] * filter[n][RCOMP];
1039 sumG += borderColor[GCOMP] * filter[n][GCOMP];
1040 sumB += borderColor[BCOMP] * filter[n][BCOMP];
1041 sumA += borderColor[ACOMP] * filter[n][ACOMP];
1042 }
1043 else {
1044 sumR += src[i + n - halfFilterWidth][RCOMP] * filter[n][RCOMP];
1045 sumG += src[i + n - halfFilterWidth][GCOMP] * filter[n][GCOMP];
1046 sumB += src[i + n - halfFilterWidth][BCOMP] * filter[n][BCOMP];
1047 sumA += src[i + n - halfFilterWidth][ACOMP] * filter[n][ACOMP];
1048 }
1049 }
1050 dest[i][RCOMP] = sumR;
1051 dest[i][GCOMP] = sumG;
1052 dest[i][BCOMP] = sumB;
1053 dest[i][ACOMP] = sumA;
1054 }
1055}
1056
1057
Brian Pauld4b799b2000-08-21 14:24:30 +00001058static void
1059convolve_1d_replicate(GLint srcWidth, const GLfloat src[][4],
1060 GLint filterWidth, const GLfloat filter[][4],
1061 GLfloat dest[][4])
Brian Paulcc8e37f2000-07-12 13:00:09 +00001062{
1063 const GLint halfFilterWidth = filterWidth / 2;
1064 GLint i, n;
1065
1066 for (i = 0; i < srcWidth; i++) {
1067 GLfloat sumR = 0.0;
1068 GLfloat sumG = 0.0;
1069 GLfloat sumB = 0.0;
1070 GLfloat sumA = 0.0;
1071 for (n = 0; n < filterWidth; n++) {
1072 if (i + n < halfFilterWidth) {
1073 sumR += src[0][RCOMP] * filter[n][RCOMP];
1074 sumG += src[0][GCOMP] * filter[n][GCOMP];
1075 sumB += src[0][BCOMP] * filter[n][BCOMP];
1076 sumA += src[0][ACOMP] * filter[n][ACOMP];
1077 }
1078 else if (i + n - halfFilterWidth >= srcWidth) {
1079 sumR += src[srcWidth - 1][RCOMP] * filter[n][RCOMP];
1080 sumG += src[srcWidth - 1][GCOMP] * filter[n][GCOMP];
1081 sumB += src[srcWidth - 1][BCOMP] * filter[n][BCOMP];
1082 sumA += src[srcWidth - 1][ACOMP] * filter[n][ACOMP];
1083 }
1084 else {
1085 sumR += src[i + n - halfFilterWidth][RCOMP] * filter[n][RCOMP];
1086 sumG += src[i + n - halfFilterWidth][GCOMP] * filter[n][GCOMP];
1087 sumB += src[i + n - halfFilterWidth][BCOMP] * filter[n][BCOMP];
1088 sumA += src[i + n - halfFilterWidth][ACOMP] * filter[n][ACOMP];
1089 }
1090 }
1091 dest[i][RCOMP] = sumR;
1092 dest[i][GCOMP] = sumG;
1093 dest[i][BCOMP] = sumB;
1094 dest[i][ACOMP] = sumA;
1095 }
1096}
1097
1098
Brian Pauld4b799b2000-08-21 14:24:30 +00001099static void
1100convolve_2d_reduce(GLint srcWidth, GLint srcHeight,
1101 const GLfloat src[][4],
1102 GLint filterWidth, GLint filterHeight,
1103 const GLfloat filter[][4],
1104 GLfloat dest[][4])
Brian Paulcc8e37f2000-07-12 13:00:09 +00001105{
Brian Paul7e708742000-08-22 18:54:25 +00001106 GLint dstWidth, dstHeight;
Brian Pauld4b799b2000-08-21 14:24:30 +00001107 GLint i, j, n, m;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001108
Brian Paul7e708742000-08-22 18:54:25 +00001109 if (filterWidth >= 1)
1110 dstWidth = srcWidth - (filterWidth - 1);
1111 else
1112 dstWidth = srcWidth;
1113
1114 if (filterHeight >= 1)
1115 dstHeight = srcHeight - (filterHeight - 1);
1116 else
1117 dstHeight = srcHeight;
1118
Brian Pauld4b799b2000-08-21 14:24:30 +00001119 if (dstWidth <= 0 || dstHeight <= 0)
1120 return;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001121
Brian Pauld4b799b2000-08-21 14:24:30 +00001122 for (j = 0; j < dstHeight; j++) {
1123 for (i = 0; i < dstWidth; i++) {
1124 GLfloat sumR = 0.0;
1125 GLfloat sumG = 0.0;
1126 GLfloat sumB = 0.0;
1127 GLfloat sumA = 0.0;
1128 for (m = 0; m < filterHeight; m++) {
1129 for (n = 0; n < filterWidth; n++) {
1130 const GLint k = (j + m) * srcWidth + i + n;
1131 const GLint f = m * filterWidth + n;
1132 sumR += src[k][RCOMP] * filter[f][RCOMP];
1133 sumG += src[k][GCOMP] * filter[f][GCOMP];
1134 sumB += src[k][BCOMP] * filter[f][BCOMP];
1135 sumA += src[k][ACOMP] * filter[f][ACOMP];
1136 }
Brian Paulcc8e37f2000-07-12 13:00:09 +00001137 }
Brian Pauld4b799b2000-08-21 14:24:30 +00001138 dest[j * dstWidth + i][RCOMP] = sumR;
1139 dest[j * dstWidth + i][GCOMP] = sumG;
1140 dest[j * dstWidth + i][BCOMP] = sumB;
1141 dest[j * dstWidth + i][ACOMP] = sumA;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001142 }
Brian Paulcc8e37f2000-07-12 13:00:09 +00001143 }
1144}
1145
1146
Brian Pauld4b799b2000-08-21 14:24:30 +00001147static void
1148convolve_2d_constant(GLint srcWidth, GLint srcHeight,
1149 const GLfloat src[][4],
1150 GLint filterWidth, GLint filterHeight,
1151 const GLfloat filter[][4],
1152 GLfloat dest[][4],
1153 const GLfloat borderColor[4])
Brian Paulcc8e37f2000-07-12 13:00:09 +00001154{
1155 const GLint halfFilterWidth = filterWidth / 2;
Brian Pauld4b799b2000-08-21 14:24:30 +00001156 const GLint halfFilterHeight = filterHeight / 2;
1157 GLint i, j, n, m;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001158
Brian Pauld4b799b2000-08-21 14:24:30 +00001159 for (j = 0; j < srcHeight; j++) {
1160 for (i = 0; i < srcWidth; i++) {
1161 GLfloat sumR = 0.0;
1162 GLfloat sumG = 0.0;
1163 GLfloat sumB = 0.0;
1164 GLfloat sumA = 0.0;
1165 for (m = 0; m < filterHeight; m++) {
1166 for (n = 0; n < filterWidth; n++) {
1167 const GLint f = m * filterWidth + n;
1168 const GLint is = i + n - halfFilterWidth;
1169 const GLint js = j + m - halfFilterHeight;
1170 if (is < 0 || is >= srcWidth ||
1171 js < 0 || js >= srcHeight) {
1172 sumR += borderColor[RCOMP] * filter[f][RCOMP];
1173 sumG += borderColor[GCOMP] * filter[f][GCOMP];
1174 sumB += borderColor[BCOMP] * filter[f][BCOMP];
1175 sumA += borderColor[ACOMP] * filter[f][ACOMP];
1176 }
1177 else {
1178 const GLint k = js * srcWidth + is;
1179 sumR += src[k][RCOMP] * filter[f][RCOMP];
1180 sumG += src[k][GCOMP] * filter[f][GCOMP];
1181 sumB += src[k][BCOMP] * filter[f][BCOMP];
1182 sumA += src[k][ACOMP] * filter[f][ACOMP];
1183 }
Brian Paulcc8e37f2000-07-12 13:00:09 +00001184 }
1185 }
Brian Pauld4b799b2000-08-21 14:24:30 +00001186 dest[j * srcWidth + i][RCOMP] = sumR;
1187 dest[j * srcWidth + i][GCOMP] = sumG;
1188 dest[j * srcWidth + i][BCOMP] = sumB;
1189 dest[j * srcWidth + i][ACOMP] = sumA;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001190 }
Brian Paulcc8e37f2000-07-12 13:00:09 +00001191 }
1192}
1193
1194
Brian Pauld4b799b2000-08-21 14:24:30 +00001195static void
1196convolve_2d_replicate(GLint srcWidth, GLint srcHeight,
1197 const GLfloat src[][4],
1198 GLint filterWidth, GLint filterHeight,
1199 const GLfloat filter[][4],
1200 GLfloat dest[][4])
Brian Paulcc8e37f2000-07-12 13:00:09 +00001201{
1202 const GLint halfFilterWidth = filterWidth / 2;
Brian Pauld4b799b2000-08-21 14:24:30 +00001203 const GLint halfFilterHeight = filterHeight / 2;
1204 GLint i, j, n, m;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001205
Brian Pauld4b799b2000-08-21 14:24:30 +00001206 for (j = 0; j < srcHeight; j++) {
1207 for (i = 0; i < srcWidth; i++) {
1208 GLfloat sumR = 0.0;
1209 GLfloat sumG = 0.0;
1210 GLfloat sumB = 0.0;
1211 GLfloat sumA = 0.0;
1212 for (m = 0; m < filterHeight; m++) {
1213 for (n = 0; n < filterWidth; n++) {
1214 const GLint f = m * filterWidth + n;
1215 GLint is = i + n - halfFilterWidth;
1216 GLint js = j + m - halfFilterHeight;
1217 GLint k;
1218 if (is < 0)
1219 is = 0;
1220 else if (is >= srcWidth)
1221 is = srcWidth - 1;
1222 if (js < 0)
1223 js = 0;
1224 else if (js >= srcHeight)
1225 js = srcHeight - 1;
1226 k = js * srcWidth + is;
1227 sumR += src[k][RCOMP] * filter[f][RCOMP];
1228 sumG += src[k][GCOMP] * filter[f][GCOMP];
1229 sumB += src[k][BCOMP] * filter[f][BCOMP];
1230 sumA += src[k][ACOMP] * filter[f][ACOMP];
Brian Paulcc8e37f2000-07-12 13:00:09 +00001231 }
1232 }
Brian Pauld4b799b2000-08-21 14:24:30 +00001233 dest[j * srcWidth + i][RCOMP] = sumR;
1234 dest[j * srcWidth + i][GCOMP] = sumG;
1235 dest[j * srcWidth + i][BCOMP] = sumB;
1236 dest[j * srcWidth + i][ACOMP] = sumA;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001237 }
Brian Paulcc8e37f2000-07-12 13:00:09 +00001238 }
1239}
1240
1241
Brian Pauld4b799b2000-08-21 14:24:30 +00001242static void
1243convolve_sep_reduce(GLint srcWidth, GLint srcHeight,
1244 const GLfloat src[][4],
1245 GLint filterWidth, GLint filterHeight,
1246 const GLfloat rowFilt[][4],
1247 const GLfloat colFilt[][4],
1248 GLfloat dest[][4])
Brian Paulcc8e37f2000-07-12 13:00:09 +00001249{
Brian Paul7e708742000-08-22 18:54:25 +00001250 GLint dstWidth, dstHeight;
Brian Pauld4b799b2000-08-21 14:24:30 +00001251 GLint i, j, n, m;
Brian Paul7e708742000-08-22 18:54:25 +00001252
1253 if (filterWidth >= 1)
1254 dstWidth = srcWidth - (filterWidth - 1);
1255 else
1256 dstWidth = srcWidth;
1257
1258 if (filterHeight >= 1)
1259 dstHeight = srcHeight - (filterHeight - 1);
1260 else
1261 dstHeight = srcHeight;
1262
1263 if (dstWidth <= 0 || dstHeight <= 0)
1264 return;
1265
1266 for (j = 0; j < dstHeight; j++) {
1267 for (i = 0; i < dstWidth; i++) {
Brian Pauld4b799b2000-08-21 14:24:30 +00001268 GLfloat sumR = 0.0;
1269 GLfloat sumG = 0.0;
1270 GLfloat sumB = 0.0;
1271 GLfloat sumA = 0.0;
1272 for (m = 0; m < filterHeight; m++) {
1273 for (n = 0; n < filterWidth; n++) {
Brian Paul7e708742000-08-22 18:54:25 +00001274 GLint k = (j + m) * srcWidth + i + n;
1275 sumR += src[k][RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
1276 sumG += src[k][GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
1277 sumB += src[k][BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
1278 sumA += src[k][ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
Brian Paulcc8e37f2000-07-12 13:00:09 +00001279 }
1280 }
Brian Paul7e708742000-08-22 18:54:25 +00001281 dest[j * dstWidth + i][RCOMP] = sumR;
1282 dest[j * dstWidth + i][GCOMP] = sumG;
1283 dest[j * dstWidth + i][BCOMP] = sumB;
1284 dest[j * dstWidth + i][ACOMP] = sumA;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001285 }
Brian Paulcc8e37f2000-07-12 13:00:09 +00001286 }
Brian Paulcc8e37f2000-07-12 13:00:09 +00001287}
1288
1289
Brian Pauld4b799b2000-08-21 14:24:30 +00001290static void
1291convolve_sep_constant(GLint srcWidth, GLint srcHeight,
1292 const GLfloat src[][4],
1293 GLint filterWidth, GLint filterHeight,
1294 const GLfloat rowFilt[][4],
1295 const GLfloat colFilt[][4],
1296 GLfloat dest[][4],
1297 const GLfloat borderColor[4])
Brian Paulcc8e37f2000-07-12 13:00:09 +00001298{
1299 const GLint halfFilterWidth = filterWidth / 2;
Brian Pauld4b799b2000-08-21 14:24:30 +00001300 const GLint halfFilterHeight = filterHeight / 2;
1301 GLint i, j, n, m;
Brian Paul7e708742000-08-22 18:54:25 +00001302
Brian Pauld4b799b2000-08-21 14:24:30 +00001303 for (j = 0; j < srcHeight; j++) {
1304 for (i = 0; i < srcWidth; i++) {
1305 GLfloat sumR = 0.0;
1306 GLfloat sumG = 0.0;
1307 GLfloat sumB = 0.0;
1308 GLfloat sumA = 0.0;
1309 for (m = 0; m < filterHeight; m++) {
1310 for (n = 0; n < filterWidth; n++) {
Brian Paul7e708742000-08-22 18:54:25 +00001311 const GLint is = i + n - halfFilterWidth;
1312 const GLint js = j + m - halfFilterHeight;
1313 if (is < 0 || is >= srcWidth ||
1314 js < 0 || js >= srcHeight) {
Brian Pauld4b799b2000-08-21 14:24:30 +00001315 sumR += borderColor[RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
1316 sumG += borderColor[GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
1317 sumB += borderColor[BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
1318 sumA += borderColor[ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
1319 }
1320 else {
Brian Paul7e708742000-08-22 18:54:25 +00001321 GLint k = js * srcWidth + is;
Brian Pauld4b799b2000-08-21 14:24:30 +00001322 sumR += src[k][RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
1323 sumG += src[k][GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
1324 sumB += src[k][BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
1325 sumA += src[k][ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
1326 }
Brian Paul7e708742000-08-22 18:54:25 +00001327
Brian Paulcc8e37f2000-07-12 13:00:09 +00001328 }
1329 }
Brian Paul7e708742000-08-22 18:54:25 +00001330 dest[j * srcWidth + i][RCOMP] = sumR;
1331 dest[j * srcWidth + i][GCOMP] = sumG;
1332 dest[j * srcWidth + i][BCOMP] = sumB;
1333 dest[j * srcWidth + i][ACOMP] = sumA;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001334 }
Brian Pauld4b799b2000-08-21 14:24:30 +00001335 }
Brian Pauld4b799b2000-08-21 14:24:30 +00001336}
1337
1338
1339static void
1340convolve_sep_replicate(GLint srcWidth, GLint srcHeight,
1341 const GLfloat src[][4],
1342 GLint filterWidth, GLint filterHeight,
1343 const GLfloat rowFilt[][4],
1344 const GLfloat colFilt[][4],
1345 GLfloat dest[][4])
1346{
1347 const GLint halfFilterWidth = filterWidth / 2;
1348 const GLint halfFilterHeight = filterHeight / 2;
1349 GLint i, j, n, m;
1350
1351 for (j = 0; j < srcHeight; j++) {
1352 for (i = 0; i < srcWidth; i++) {
1353 GLfloat sumR = 0.0;
1354 GLfloat sumG = 0.0;
1355 GLfloat sumB = 0.0;
1356 GLfloat sumA = 0.0;
1357 for (m = 0; m < filterHeight; m++) {
1358 for (n = 0; n < filterWidth; n++) {
Brian Paul7e708742000-08-22 18:54:25 +00001359 GLint is = i + n - halfFilterWidth;
1360 GLint js = j + m - halfFilterHeight;
1361 GLint k;
1362 if (is < 0)
1363 is = 0;
1364 else if (is >= srcWidth)
1365 is = srcWidth - 1;
1366 if (js < 0)
1367 js = 0;
1368 else if (js >= srcHeight)
1369 js = srcHeight - 1;
1370 k = js * srcWidth + is;
1371 sumR += src[k][RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
1372 sumG += src[k][GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
1373 sumB += src[k][BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
1374 sumA += src[k][ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
Brian Pauld4b799b2000-08-21 14:24:30 +00001375 }
1376 }
Brian Paul7e708742000-08-22 18:54:25 +00001377 dest[j * srcWidth + i][RCOMP] = sumR;
1378 dest[j * srcWidth + i][GCOMP] = sumG;
1379 dest[j * srcWidth + i][BCOMP] = sumB;
1380 dest[j * srcWidth + i][ACOMP] = sumA;
Brian Pauld4b799b2000-08-21 14:24:30 +00001381 }
1382 }
1383}
1384
1385
1386
1387void
1388_mesa_convolve_1d_image(const GLcontext *ctx, GLsizei *width,
1389 const GLfloat *srcImage, GLfloat *dstImage)
1390{
1391 switch (ctx->Pixel.ConvolutionBorderMode[0]) {
1392 case GL_REDUCE:
1393 convolve_1d_reduce(*width, (const GLfloat (*)[4]) srcImage,
1394 ctx->Convolution1D.Width,
1395 (const GLfloat (*)[4]) ctx->Convolution1D.Filter,
1396 (GLfloat (*)[4]) dstImage);
Brian Paul7e708742000-08-22 18:54:25 +00001397 *width = *width - (MAX2(ctx->Convolution1D.Width, 1) - 1);
Brian Pauld4b799b2000-08-21 14:24:30 +00001398 break;
1399 case GL_CONSTANT_BORDER:
1400 convolve_1d_constant(*width, (const GLfloat (*)[4]) srcImage,
1401 ctx->Convolution1D.Width,
1402 (const GLfloat (*)[4]) ctx->Convolution1D.Filter,
1403 (GLfloat (*)[4]) dstImage,
1404 ctx->Pixel.ConvolutionBorderColor[0]);
1405 break;
1406 case GL_REPLICATE_BORDER:
1407 convolve_1d_replicate(*width, (const GLfloat (*)[4]) srcImage,
1408 ctx->Convolution1D.Width,
1409 (const GLfloat (*)[4]) ctx->Convolution1D.Filter,
1410 (GLfloat (*)[4]) dstImage);
1411 break;
1412 default:
1413 ;
1414 }
1415}
1416
1417
1418void
1419_mesa_convolve_2d_image(const GLcontext *ctx, GLsizei *width, GLsizei *height,
1420 const GLfloat *srcImage, GLfloat *dstImage)
1421{
1422 switch (ctx->Pixel.ConvolutionBorderMode[1]) {
1423 case GL_REDUCE:
1424 convolve_2d_reduce(*width, *height,
1425 (const GLfloat (*)[4]) srcImage,
1426 ctx->Convolution2D.Width,
1427 ctx->Convolution2D.Height,
1428 (const GLfloat (*)[4]) ctx->Convolution2D.Filter,
1429 (GLfloat (*)[4]) dstImage);
Brian Paul7e708742000-08-22 18:54:25 +00001430 *width = *width - (MAX2(ctx->Convolution2D.Width, 1) - 1);
1431 *height = *height - (MAX2(ctx->Convolution2D.Height, 1) - 1);
Brian Pauld4b799b2000-08-21 14:24:30 +00001432 break;
1433 case GL_CONSTANT_BORDER:
1434 convolve_2d_constant(*width, *height,
1435 (const GLfloat (*)[4]) srcImage,
1436 ctx->Convolution2D.Width,
1437 ctx->Convolution2D.Height,
1438 (const GLfloat (*)[4]) ctx->Convolution2D.Filter,
1439 (GLfloat (*)[4]) dstImage,
1440 ctx->Pixel.ConvolutionBorderColor[1]);
1441 break;
1442 case GL_REPLICATE_BORDER:
1443 convolve_2d_replicate(*width, *height,
1444 (const GLfloat (*)[4]) srcImage,
1445 ctx->Convolution2D.Width,
1446 ctx->Convolution2D.Height,
1447 (const GLfloat (*)[4])ctx->Convolution2D.Filter,
1448 (GLfloat (*)[4]) dstImage);
1449 break;
1450 default:
1451 ;
1452 }
1453}
1454
1455
1456void
1457_mesa_convolve_sep_image(const GLcontext *ctx,
1458 GLsizei *width, GLsizei *height,
1459 const GLfloat *srcImage, GLfloat *dstImage)
1460{
1461 const GLfloat *rowFilter = ctx->Separable2D.Filter;
1462 const GLfloat *colFilter = rowFilter + 4 * MAX_CONVOLUTION_WIDTH;
1463
1464 switch (ctx->Pixel.ConvolutionBorderMode[2]) {
1465 case GL_REDUCE:
1466 convolve_sep_reduce(*width, *height,
1467 (const GLfloat (*)[4]) srcImage,
Brian Paul7e708742000-08-22 18:54:25 +00001468 ctx->Separable2D.Width,
1469 ctx->Separable2D.Height,
Brian Pauld4b799b2000-08-21 14:24:30 +00001470 (const GLfloat (*)[4]) rowFilter,
1471 (const GLfloat (*)[4]) colFilter,
1472 (GLfloat (*)[4]) dstImage);
Brian Paul7e708742000-08-22 18:54:25 +00001473 *width = *width - (MAX2(ctx->Separable2D.Width, 1) - 1);
1474 *height = *height - (MAX2(ctx->Separable2D.Height, 1) - 1);
Brian Pauld4b799b2000-08-21 14:24:30 +00001475 break;
1476 case GL_CONSTANT_BORDER:
1477 convolve_sep_constant(*width, *height,
1478 (const GLfloat (*)[4]) srcImage,
Brian Paul7e708742000-08-22 18:54:25 +00001479 ctx->Separable2D.Width,
1480 ctx->Separable2D.Height,
Brian Pauld4b799b2000-08-21 14:24:30 +00001481 (const GLfloat (*)[4]) rowFilter,
1482 (const GLfloat (*)[4]) colFilter,
1483 (GLfloat (*)[4]) dstImage,
1484 ctx->Pixel.ConvolutionBorderColor[2]);
1485 break;
1486 case GL_REPLICATE_BORDER:
1487 convolve_sep_replicate(*width, *height,
1488 (const GLfloat (*)[4]) srcImage,
Brian Paul7e708742000-08-22 18:54:25 +00001489 ctx->Separable2D.Width,
1490 ctx->Separable2D.Height,
Brian Pauld4b799b2000-08-21 14:24:30 +00001491 (const GLfloat (*)[4]) rowFilter,
1492 (const GLfloat (*)[4]) colFilter,
1493 (GLfloat (*)[4]) dstImage);
1494 break;
1495 default:
1496 ;
Brian Paulcc8e37f2000-07-12 13:00:09 +00001497 }
1498}
Brian Paul16461f72001-02-06 17:22:16 +00001499
1500
1501
1502/*
1503 * This function computes an image's size after convolution.
1504 * If the convolution border mode is GL_REDUCE, the post-convolution
1505 * image will be smaller than the original.
1506 */
1507void
1508_mesa_adjust_image_for_convolution(const GLcontext *ctx, GLuint dimensions,
1509 GLsizei *width, GLsizei *height)
1510{
1511 if (ctx->Pixel.Convolution1DEnabled
1512 && dimensions == 1
1513 && ctx->Pixel.ConvolutionBorderMode[0] == GL_REDUCE) {
1514 *width = *width - (MAX2(ctx->Convolution1D.Width, 1) - 1);
1515 }
1516 else if (ctx->Pixel.Convolution2DEnabled
1517 && dimensions > 1
1518 && ctx->Pixel.ConvolutionBorderMode[1] == GL_REDUCE) {
1519 *width = *width - (MAX2(ctx->Convolution2D.Width, 1) - 1);
1520 *height = *height - (MAX2(ctx->Convolution2D.Height, 1) - 1);
1521 }
1522 else if (ctx->Pixel.Separable2DEnabled
1523 && dimensions > 1
1524 && ctx->Pixel.ConvolutionBorderMode[2] == GL_REDUCE) {
1525 *width = *width - (MAX2(ctx->Separable2D.Width, 1) - 1);
1526 *height = *height - (MAX2(ctx->Separable2D.Height, 1) - 1);
1527 }
1528}