f63bddc44d50a623459e5593b855d1d6187cd593
[mesa.git] / src / mesa / main / convolve.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
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
34 #include "glheader.h"
35 #include "bufferobj.h"
36 #include "colormac.h"
37 #include "convolve.h"
38 #include "context.h"
39 #include "image.h"
40 #include "macros.h"
41 #include "mtypes.h"
42 #include "state.h"
43 #include "main/dispatch.h"
44
45
46 #if FEATURE_convolve
47
48
49 /*
50 * Given an internalFormat token passed to glConvolutionFilter
51 * or glSeparableFilter, return the corresponding base format.
52 * Return -1 if invalid token.
53 */
54 static GLint
55 base_filter_format( GLenum format )
56 {
57 switch (format) {
58 case GL_ALPHA:
59 case GL_ALPHA4:
60 case GL_ALPHA8:
61 case GL_ALPHA12:
62 case GL_ALPHA16:
63 return GL_ALPHA;
64 case GL_LUMINANCE:
65 case GL_LUMINANCE4:
66 case GL_LUMINANCE8:
67 case GL_LUMINANCE12:
68 case GL_LUMINANCE16:
69 return GL_LUMINANCE;
70 case GL_LUMINANCE_ALPHA:
71 case GL_LUMINANCE4_ALPHA4:
72 case GL_LUMINANCE6_ALPHA2:
73 case GL_LUMINANCE8_ALPHA8:
74 case GL_LUMINANCE12_ALPHA4:
75 case GL_LUMINANCE12_ALPHA12:
76 case GL_LUMINANCE16_ALPHA16:
77 return GL_LUMINANCE_ALPHA;
78 case GL_INTENSITY:
79 case GL_INTENSITY4:
80 case GL_INTENSITY8:
81 case GL_INTENSITY12:
82 case GL_INTENSITY16:
83 return GL_INTENSITY;
84 case GL_RGB:
85 case GL_R3_G3_B2:
86 case GL_RGB4:
87 case GL_RGB5:
88 case GL_RGB8:
89 case GL_RGB10:
90 case GL_RGB12:
91 case GL_RGB16:
92 return GL_RGB;
93 case 4:
94 case GL_RGBA:
95 case GL_RGBA2:
96 case GL_RGBA4:
97 case GL_RGB5_A1:
98 case GL_RGBA8:
99 case GL_RGB10_A2:
100 case GL_RGBA12:
101 case GL_RGBA16:
102 return GL_RGBA;
103 default:
104 return -1; /* error */
105 }
106 }
107
108
109 void GLAPIENTRY
110 _mesa_ConvolutionFilter1D(GLenum target, GLenum internalFormat, GLsizei width, GLenum format, GLenum type, const GLvoid *image)
111 {
112 GLint baseFormat;
113 GET_CURRENT_CONTEXT(ctx);
114 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
115
116 if (target != GL_CONVOLUTION_1D) {
117 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter1D(target)");
118 return;
119 }
120
121 baseFormat = base_filter_format(internalFormat);
122 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
123 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter1D(internalFormat)");
124 return;
125 }
126
127 if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
128 _mesa_error(ctx, GL_INVALID_VALUE, "glConvolutionFilter1D(width)");
129 return;
130 }
131
132 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
133 _mesa_error(ctx, GL_INVALID_OPERATION, "glConvolutionFilter1D(format or type)");
134 return;
135 }
136
137 if (format == GL_COLOR_INDEX ||
138 format == GL_STENCIL_INDEX ||
139 format == GL_DEPTH_COMPONENT ||
140 format == GL_INTENSITY ||
141 type == GL_BITMAP) {
142 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter1D(format or type)");
143 return;
144 }
145
146 ctx->Convolution1D.Format = format;
147 ctx->Convolution1D.InternalFormat = internalFormat;
148 ctx->Convolution1D.Width = width;
149 ctx->Convolution1D.Height = 1;
150
151 image = _mesa_map_validate_pbo_source(ctx,
152 1, &ctx->Unpack, width, 1, 1,
153 format, type, image,
154 "glConvolutionFilter1D");
155 if (!image)
156 return;
157
158 _mesa_unpack_color_span_float(ctx, width, GL_RGBA,
159 ctx->Convolution1D.Filter,
160 format, type, image, &ctx->Unpack,
161 0); /* transferOps */
162
163 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
164
165 _mesa_scale_and_bias_rgba(width,
166 (GLfloat (*)[4]) ctx->Convolution1D.Filter,
167 ctx->Pixel.ConvolutionFilterScale[0][0],
168 ctx->Pixel.ConvolutionFilterScale[0][1],
169 ctx->Pixel.ConvolutionFilterScale[0][2],
170 ctx->Pixel.ConvolutionFilterScale[0][3],
171 ctx->Pixel.ConvolutionFilterBias[0][0],
172 ctx->Pixel.ConvolutionFilterBias[0][1],
173 ctx->Pixel.ConvolutionFilterBias[0][2],
174 ctx->Pixel.ConvolutionFilterBias[0][3]);
175
176 ctx->NewState |= _NEW_PIXEL;
177 }
178
179
180 void GLAPIENTRY
181 _mesa_ConvolutionFilter2D(GLenum target, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *image)
182 {
183 GLint baseFormat;
184 GLint i;
185 GET_CURRENT_CONTEXT(ctx);
186 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
187
188 if (target != GL_CONVOLUTION_2D) {
189 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(target)");
190 return;
191 }
192
193 baseFormat = base_filter_format(internalFormat);
194 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
195 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(internalFormat)");
196 return;
197 }
198
199 if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
200 _mesa_error(ctx, GL_INVALID_VALUE, "glConvolutionFilter2D(width)");
201 return;
202 }
203 if (height < 0 || height > MAX_CONVOLUTION_HEIGHT) {
204 _mesa_error(ctx, GL_INVALID_VALUE, "glConvolutionFilter2D(height)");
205 return;
206 }
207
208 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
209 _mesa_error(ctx, GL_INVALID_OPERATION, "glConvolutionFilter2D(format or type)");
210 return;
211 }
212 if (format == GL_COLOR_INDEX ||
213 format == GL_STENCIL_INDEX ||
214 format == GL_DEPTH_COMPONENT ||
215 format == GL_INTENSITY ||
216 type == GL_BITMAP) {
217 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionFilter2D(format or type)");
218 return;
219 }
220
221 /* this should have been caught earlier */
222 assert(_mesa_components_in_format(format));
223
224 ctx->Convolution2D.Format = format;
225 ctx->Convolution2D.InternalFormat = internalFormat;
226 ctx->Convolution2D.Width = width;
227 ctx->Convolution2D.Height = height;
228
229 image = _mesa_map_validate_pbo_source(ctx,
230 2, &ctx->Unpack, width, height, 1,
231 format, type, image,
232 "glConvolutionFilter2D");
233 if (!image)
234 return;
235
236 /* Unpack filter image. We always store filters in RGBA format. */
237 for (i = 0; i < height; i++) {
238 const GLvoid *src = _mesa_image_address2d(&ctx->Unpack, image, width,
239 height, format, type, i, 0);
240 GLfloat *dst = ctx->Convolution2D.Filter + i * width * 4;
241 _mesa_unpack_color_span_float(ctx, width, GL_RGBA, dst,
242 format, type, src, &ctx->Unpack,
243 0); /* transferOps */
244 }
245
246 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
247
248 _mesa_scale_and_bias_rgba(width * height,
249 (GLfloat (*)[4]) ctx->Convolution2D.Filter,
250 ctx->Pixel.ConvolutionFilterScale[1][0],
251 ctx->Pixel.ConvolutionFilterScale[1][1],
252 ctx->Pixel.ConvolutionFilterScale[1][2],
253 ctx->Pixel.ConvolutionFilterScale[1][3],
254 ctx->Pixel.ConvolutionFilterBias[1][0],
255 ctx->Pixel.ConvolutionFilterBias[1][1],
256 ctx->Pixel.ConvolutionFilterBias[1][2],
257 ctx->Pixel.ConvolutionFilterBias[1][3]);
258
259 ctx->NewState |= _NEW_PIXEL;
260 }
261
262
263 static void GLAPIENTRY
264 _mesa_ConvolutionParameterf(GLenum target, GLenum pname, GLfloat param)
265 {
266 GET_CURRENT_CONTEXT(ctx);
267 GLuint c;
268 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
269
270 switch (target) {
271 case GL_CONVOLUTION_1D:
272 c = 0;
273 break;
274 case GL_CONVOLUTION_2D:
275 c = 1;
276 break;
277 case GL_SEPARABLE_2D:
278 c = 2;
279 break;
280 default:
281 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(target)");
282 return;
283 }
284
285 switch (pname) {
286 case GL_CONVOLUTION_BORDER_MODE:
287 if (param == (GLfloat) GL_REDUCE ||
288 param == (GLfloat) GL_CONSTANT_BORDER ||
289 param == (GLfloat) GL_REPLICATE_BORDER) {
290 ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) param;
291 }
292 else {
293 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(params)");
294 return;
295 }
296 break;
297 default:
298 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterf(pname)");
299 return;
300 }
301
302 ctx->NewState |= _NEW_PIXEL;
303 }
304
305
306 static void GLAPIENTRY
307 _mesa_ConvolutionParameterfv(GLenum target, GLenum pname, const GLfloat *params)
308 {
309 GET_CURRENT_CONTEXT(ctx);
310 GLuint c;
311 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
312
313 switch (target) {
314 case GL_CONVOLUTION_1D:
315 c = 0;
316 break;
317 case GL_CONVOLUTION_2D:
318 c = 1;
319 break;
320 case GL_SEPARABLE_2D:
321 c = 2;
322 break;
323 default:
324 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(target)");
325 return;
326 }
327
328 switch (pname) {
329 case GL_CONVOLUTION_BORDER_COLOR:
330 COPY_4V(ctx->Pixel.ConvolutionBorderColor[c], params);
331 break;
332 case GL_CONVOLUTION_BORDER_MODE:
333 if (params[0] == (GLfloat) GL_REDUCE ||
334 params[0] == (GLfloat) GL_CONSTANT_BORDER ||
335 params[0] == (GLfloat) GL_REPLICATE_BORDER) {
336 ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) params[0];
337 }
338 else {
339 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(params)");
340 return;
341 }
342 break;
343 case GL_CONVOLUTION_FILTER_SCALE:
344 COPY_4V(ctx->Pixel.ConvolutionFilterScale[c], params);
345 break;
346 case GL_CONVOLUTION_FILTER_BIAS:
347 COPY_4V(ctx->Pixel.ConvolutionFilterBias[c], params);
348 break;
349 default:
350 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameterfv(pname)");
351 return;
352 }
353
354 ctx->NewState |= _NEW_PIXEL;
355 }
356
357
358 static void GLAPIENTRY
359 _mesa_ConvolutionParameteri(GLenum target, GLenum pname, GLint param)
360 {
361 GET_CURRENT_CONTEXT(ctx);
362 GLuint c;
363 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
364
365 switch (target) {
366 case GL_CONVOLUTION_1D:
367 c = 0;
368 break;
369 case GL_CONVOLUTION_2D:
370 c = 1;
371 break;
372 case GL_SEPARABLE_2D:
373 c = 2;
374 break;
375 default:
376 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(target)");
377 return;
378 }
379
380 switch (pname) {
381 case GL_CONVOLUTION_BORDER_MODE:
382 if (param == (GLint) GL_REDUCE ||
383 param == (GLint) GL_CONSTANT_BORDER ||
384 param == (GLint) GL_REPLICATE_BORDER) {
385 ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) param;
386 }
387 else {
388 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(params)");
389 return;
390 }
391 break;
392 default:
393 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteri(pname)");
394 return;
395 }
396
397 ctx->NewState |= _NEW_PIXEL;
398 }
399
400
401 static void GLAPIENTRY
402 _mesa_ConvolutionParameteriv(GLenum target, GLenum pname, const GLint *params)
403 {
404 GET_CURRENT_CONTEXT(ctx);
405 GLuint c;
406 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
407
408 switch (target) {
409 case GL_CONVOLUTION_1D:
410 c = 0;
411 break;
412 case GL_CONVOLUTION_2D:
413 c = 1;
414 break;
415 case GL_SEPARABLE_2D:
416 c = 2;
417 break;
418 default:
419 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(target)");
420 return;
421 }
422
423 switch (pname) {
424 case GL_CONVOLUTION_BORDER_COLOR:
425 ctx->Pixel.ConvolutionBorderColor[c][0] = INT_TO_FLOAT(params[0]);
426 ctx->Pixel.ConvolutionBorderColor[c][1] = INT_TO_FLOAT(params[1]);
427 ctx->Pixel.ConvolutionBorderColor[c][2] = INT_TO_FLOAT(params[2]);
428 ctx->Pixel.ConvolutionBorderColor[c][3] = INT_TO_FLOAT(params[3]);
429 break;
430 case GL_CONVOLUTION_BORDER_MODE:
431 if (params[0] == (GLint) GL_REDUCE ||
432 params[0] == (GLint) GL_CONSTANT_BORDER ||
433 params[0] == (GLint) GL_REPLICATE_BORDER) {
434 ctx->Pixel.ConvolutionBorderMode[c] = (GLenum) params[0];
435 }
436 else {
437 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(params)");
438 return;
439 }
440 break;
441 case GL_CONVOLUTION_FILTER_SCALE:
442 /* COPY_4V(ctx->Pixel.ConvolutionFilterScale[c], params); */
443 /* need cast to prevent compiler warnings */
444 ctx->Pixel.ConvolutionFilterScale[c][0] = (GLfloat) params[0];
445 ctx->Pixel.ConvolutionFilterScale[c][1] = (GLfloat) params[1];
446 ctx->Pixel.ConvolutionFilterScale[c][2] = (GLfloat) params[2];
447 ctx->Pixel.ConvolutionFilterScale[c][3] = (GLfloat) params[3];
448 break;
449 case GL_CONVOLUTION_FILTER_BIAS:
450 /* COPY_4V(ctx->Pixel.ConvolutionFilterBias[c], params); */
451 /* need cast to prevent compiler warnings */
452 ctx->Pixel.ConvolutionFilterBias[c][0] = (GLfloat) params[0];
453 ctx->Pixel.ConvolutionFilterBias[c][1] = (GLfloat) params[1];
454 ctx->Pixel.ConvolutionFilterBias[c][2] = (GLfloat) params[2];
455 ctx->Pixel.ConvolutionFilterBias[c][3] = (GLfloat) params[3];
456 break;
457 default:
458 _mesa_error(ctx, GL_INVALID_ENUM, "glConvolutionParameteriv(pname)");
459 return;
460 }
461
462 ctx->NewState |= _NEW_PIXEL;
463 }
464
465
466 static void GLAPIENTRY
467 _mesa_CopyConvolutionFilter1D(GLenum target, GLenum internalFormat, GLint x, GLint y, GLsizei width)
468 {
469 GLint baseFormat;
470 GET_CURRENT_CONTEXT(ctx);
471 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
472
473 if (target != GL_CONVOLUTION_1D) {
474 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter1D(target)");
475 return;
476 }
477
478 baseFormat = base_filter_format(internalFormat);
479 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
480 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter1D(internalFormat)");
481 return;
482 }
483
484 if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
485 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter1D(width)");
486 return;
487 }
488
489 if (!ctx->ReadBuffer->_ColorReadBuffer) {
490 return; /* no readbuffer - OK */
491 }
492
493 ctx->Driver.CopyConvolutionFilter1D( ctx, target,
494 internalFormat, x, y, width);
495 }
496
497
498 static void GLAPIENTRY
499 _mesa_CopyConvolutionFilter2D(GLenum target, GLenum internalFormat, GLint x, GLint y, GLsizei width, GLsizei height)
500 {
501 GLint baseFormat;
502 GET_CURRENT_CONTEXT(ctx);
503 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
504
505 if (target != GL_CONVOLUTION_2D) {
506 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter2D(target)");
507 return;
508 }
509
510 baseFormat = base_filter_format(internalFormat);
511 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
512 _mesa_error(ctx, GL_INVALID_ENUM, "glCopyConvolutionFilter2D(internalFormat)");
513 return;
514 }
515
516 if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
517 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter2D(width)");
518 return;
519 }
520 if (height < 0 || height > MAX_CONVOLUTION_HEIGHT) {
521 _mesa_error(ctx, GL_INVALID_VALUE, "glCopyConvolutionFilter2D(height)");
522 return;
523 }
524
525 if (!ctx->ReadBuffer->_ColorReadBuffer) {
526 return; /* no readbuffer - OK */
527 }
528
529 ctx->Driver.CopyConvolutionFilter2D( ctx, target, internalFormat, x, y,
530 width, height );
531 }
532
533
534 static void GLAPIENTRY
535 _mesa_GetConvolutionFilter(GLenum target, GLenum format, GLenum type,
536 GLvoid *image)
537 {
538 struct gl_convolution_attrib *filter;
539 GLuint row;
540 GET_CURRENT_CONTEXT(ctx);
541 ASSERT_OUTSIDE_BEGIN_END(ctx);
542
543 if (ctx->NewState) {
544 _mesa_update_state(ctx);
545 }
546
547 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
548 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetConvolutionFilter(format or type)");
549 return;
550 }
551
552 if (format == GL_COLOR_INDEX ||
553 format == GL_STENCIL_INDEX ||
554 format == GL_DEPTH_COMPONENT ||
555 format == GL_INTENSITY ||
556 type == GL_BITMAP) {
557 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(format or type)");
558 return;
559 }
560
561 switch (target) {
562 case GL_CONVOLUTION_1D:
563 filter = &(ctx->Convolution1D);
564 break;
565 case GL_CONVOLUTION_2D:
566 filter = &(ctx->Convolution2D);
567 break;
568 default:
569 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(target)");
570 return;
571 }
572
573 image = _mesa_map_validate_pbo_dest(ctx, 2, &ctx->Pack,
574 filter->Width, filter->Height, 1,
575 format, type, image,
576 "glGetConvolutionFilter");
577 if (!image)
578 return;
579
580 for (row = 0; row < filter->Height; row++) {
581 GLvoid *dst = _mesa_image_address2d(&ctx->Pack, image, filter->Width,
582 filter->Height, format, type,
583 row, 0);
584 GLfloat (*src)[4] = (GLfloat (*)[4]) (filter->Filter + row * filter->Width * 4);
585 _mesa_pack_rgba_span_float(ctx, filter->Width, src,
586 format, type, dst, &ctx->Pack, 0x0);
587 }
588
589 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
590 }
591
592
593 static void GLAPIENTRY
594 _mesa_GetConvolutionParameterfv(GLenum target, GLenum pname, GLfloat *params)
595 {
596 GET_CURRENT_CONTEXT(ctx);
597 const struct gl_convolution_attrib *conv;
598 GLuint c;
599 ASSERT_OUTSIDE_BEGIN_END(ctx);
600
601 switch (target) {
602 case GL_CONVOLUTION_1D:
603 c = 0;
604 conv = &ctx->Convolution1D;
605 break;
606 case GL_CONVOLUTION_2D:
607 c = 1;
608 conv = &ctx->Convolution2D;
609 break;
610 case GL_SEPARABLE_2D:
611 c = 2;
612 conv = &ctx->Separable2D;
613 break;
614 default:
615 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameterfv(target)");
616 return;
617 }
618
619 switch (pname) {
620 case GL_CONVOLUTION_BORDER_COLOR:
621 COPY_4V(params, ctx->Pixel.ConvolutionBorderColor[c]);
622 break;
623 case GL_CONVOLUTION_BORDER_MODE:
624 *params = (GLfloat) ctx->Pixel.ConvolutionBorderMode[c];
625 break;
626 case GL_CONVOLUTION_FILTER_SCALE:
627 COPY_4V(params, ctx->Pixel.ConvolutionFilterScale[c]);
628 break;
629 case GL_CONVOLUTION_FILTER_BIAS:
630 COPY_4V(params, ctx->Pixel.ConvolutionFilterBias[c]);
631 break;
632 case GL_CONVOLUTION_FORMAT:
633 *params = (GLfloat) conv->Format;
634 break;
635 case GL_CONVOLUTION_WIDTH:
636 *params = (GLfloat) conv->Width;
637 break;
638 case GL_CONVOLUTION_HEIGHT:
639 *params = (GLfloat) conv->Height;
640 break;
641 case GL_MAX_CONVOLUTION_WIDTH:
642 *params = (GLfloat) ctx->Const.MaxConvolutionWidth;
643 break;
644 case GL_MAX_CONVOLUTION_HEIGHT:
645 *params = (GLfloat) ctx->Const.MaxConvolutionHeight;
646 break;
647 default:
648 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameterfv(pname)");
649 return;
650 }
651 }
652
653
654 static void GLAPIENTRY
655 _mesa_GetConvolutionParameteriv(GLenum target, GLenum pname, GLint *params)
656 {
657 GET_CURRENT_CONTEXT(ctx);
658 const struct gl_convolution_attrib *conv;
659 GLuint c;
660 ASSERT_OUTSIDE_BEGIN_END(ctx);
661
662 switch (target) {
663 case GL_CONVOLUTION_1D:
664 c = 0;
665 conv = &ctx->Convolution1D;
666 break;
667 case GL_CONVOLUTION_2D:
668 c = 1;
669 conv = &ctx->Convolution2D;
670 break;
671 case GL_SEPARABLE_2D:
672 c = 2;
673 conv = &ctx->Separable2D;
674 break;
675 default:
676 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameteriv(target)");
677 return;
678 }
679
680 switch (pname) {
681 case GL_CONVOLUTION_BORDER_COLOR:
682 params[0] = FLOAT_TO_INT(ctx->Pixel.ConvolutionBorderColor[c][0]);
683 params[1] = FLOAT_TO_INT(ctx->Pixel.ConvolutionBorderColor[c][1]);
684 params[2] = FLOAT_TO_INT(ctx->Pixel.ConvolutionBorderColor[c][2]);
685 params[3] = FLOAT_TO_INT(ctx->Pixel.ConvolutionBorderColor[c][3]);
686 break;
687 case GL_CONVOLUTION_BORDER_MODE:
688 *params = (GLint) ctx->Pixel.ConvolutionBorderMode[c];
689 break;
690 case GL_CONVOLUTION_FILTER_SCALE:
691 params[0] = (GLint) ctx->Pixel.ConvolutionFilterScale[c][0];
692 params[1] = (GLint) ctx->Pixel.ConvolutionFilterScale[c][1];
693 params[2] = (GLint) ctx->Pixel.ConvolutionFilterScale[c][2];
694 params[3] = (GLint) ctx->Pixel.ConvolutionFilterScale[c][3];
695 break;
696 case GL_CONVOLUTION_FILTER_BIAS:
697 params[0] = (GLint) ctx->Pixel.ConvolutionFilterBias[c][0];
698 params[1] = (GLint) ctx->Pixel.ConvolutionFilterBias[c][1];
699 params[2] = (GLint) ctx->Pixel.ConvolutionFilterBias[c][2];
700 params[3] = (GLint) ctx->Pixel.ConvolutionFilterBias[c][3];
701 break;
702 case GL_CONVOLUTION_FORMAT:
703 *params = (GLint) conv->Format;
704 break;
705 case GL_CONVOLUTION_WIDTH:
706 *params = (GLint) conv->Width;
707 break;
708 case GL_CONVOLUTION_HEIGHT:
709 *params = (GLint) conv->Height;
710 break;
711 case GL_MAX_CONVOLUTION_WIDTH:
712 *params = (GLint) ctx->Const.MaxConvolutionWidth;
713 break;
714 case GL_MAX_CONVOLUTION_HEIGHT:
715 *params = (GLint) ctx->Const.MaxConvolutionHeight;
716 break;
717 default:
718 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionParameteriv(pname)");
719 return;
720 }
721 }
722
723
724 static void GLAPIENTRY
725 _mesa_GetSeparableFilter(GLenum target, GLenum format, GLenum type,
726 GLvoid *row, GLvoid *column, GLvoid *span)
727 {
728 const GLint colStart = MAX_CONVOLUTION_WIDTH * 4;
729 struct gl_convolution_attrib *filter;
730 GET_CURRENT_CONTEXT(ctx);
731 ASSERT_OUTSIDE_BEGIN_END(ctx);
732
733 if (ctx->NewState) {
734 _mesa_update_state(ctx);
735 }
736
737 if (target != GL_SEPARABLE_2D) {
738 _mesa_error(ctx, GL_INVALID_ENUM, "glGetSeparableFilter(target)");
739 return;
740 }
741
742 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
743 _mesa_error(ctx, GL_INVALID_OPERATION,
744 "glGetConvolutionFilter(format or type)");
745 return;
746 }
747
748 if (format == GL_COLOR_INDEX ||
749 format == GL_STENCIL_INDEX ||
750 format == GL_DEPTH_COMPONENT ||
751 format == GL_INTENSITY ||
752 type == GL_BITMAP) {
753 _mesa_error(ctx, GL_INVALID_ENUM, "glGetConvolutionFilter(format or type)");
754 return;
755 }
756
757 filter = &ctx->Separable2D;
758
759 /* Get row filter */
760 row = _mesa_map_validate_pbo_dest(ctx, 1, &ctx->Pack,
761 filter->Width, 1, 1,
762 format, type, row,
763 "glGetConvolutionFilter");
764 if (row) {
765 GLvoid *dst = _mesa_image_address1d(&ctx->Pack, row, filter->Width,
766 format, type, 0);
767 _mesa_pack_rgba_span_float(ctx, filter->Width,
768 (GLfloat (*)[4]) filter->Filter,
769 format, type, dst, &ctx->Pack, 0x0);
770 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
771 }
772
773 /* get column filter */
774 column = _mesa_map_validate_pbo_dest(ctx, 1, &ctx->Pack,
775 filter->Height, 1, 1,
776 format, type, column,
777 "glGetConvolutionFilter");
778 if (column) {
779 GLvoid *dst = _mesa_image_address1d(&ctx->Pack, column, filter->Height,
780 format, type, 0);
781 GLfloat (*src)[4] = (GLfloat (*)[4]) (filter->Filter + colStart);
782 _mesa_pack_rgba_span_float(ctx, filter->Height, src,
783 format, type, dst, &ctx->Pack, 0x0);
784 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
785 }
786
787 (void) span; /* unused at this time */
788 }
789
790
791 static void GLAPIENTRY
792 _mesa_SeparableFilter2D(GLenum target, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *row, const GLvoid *column)
793 {
794 const GLint colStart = MAX_CONVOLUTION_WIDTH * 4;
795 GLint baseFormat;
796 GET_CURRENT_CONTEXT(ctx);
797 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
798
799 if (target != GL_SEPARABLE_2D) {
800 _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(target)");
801 return;
802 }
803
804 baseFormat = base_filter_format(internalFormat);
805 if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
806 _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(internalFormat)");
807 return;
808 }
809
810 if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
811 _mesa_error(ctx, GL_INVALID_VALUE, "glSeparableFilter2D(width)");
812 return;
813 }
814 if (height < 0 || height > MAX_CONVOLUTION_HEIGHT) {
815 _mesa_error(ctx, GL_INVALID_VALUE, "glSeparableFilter2D(height)");
816 return;
817 }
818
819 if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
820 _mesa_error(ctx, GL_INVALID_OPERATION, "glSeparableFilter2D(format or type)");
821 return;
822 }
823
824 if (format == GL_COLOR_INDEX ||
825 format == GL_STENCIL_INDEX ||
826 format == GL_DEPTH_COMPONENT ||
827 format == GL_INTENSITY ||
828 type == GL_BITMAP) {
829 _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(format or type)");
830 return;
831 }
832
833 ctx->Separable2D.Format = format;
834 ctx->Separable2D.InternalFormat = internalFormat;
835 ctx->Separable2D.Width = width;
836 ctx->Separable2D.Height = height;
837
838 /* unpack row filter */
839 row = _mesa_map_validate_pbo_source(ctx, 1, &ctx->Unpack,
840 width, 1, 1,
841 format, type, row,
842 "glSeparableFilter2D");
843 if (row) {
844 _mesa_unpack_color_span_float(ctx, width, GL_RGBA,
845 ctx->Separable2D.Filter,
846 format, type, row, &ctx->Unpack,
847 0x0); /* transferOps */
848 _mesa_scale_and_bias_rgba(width,
849 (GLfloat (*)[4]) ctx->Separable2D.Filter,
850 ctx->Pixel.ConvolutionFilterScale[2][0],
851 ctx->Pixel.ConvolutionFilterScale[2][1],
852 ctx->Pixel.ConvolutionFilterScale[2][2],
853 ctx->Pixel.ConvolutionFilterScale[2][3],
854 ctx->Pixel.ConvolutionFilterBias[2][0],
855 ctx->Pixel.ConvolutionFilterBias[2][1],
856 ctx->Pixel.ConvolutionFilterBias[2][2],
857 ctx->Pixel.ConvolutionFilterBias[2][3]);
858 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
859 }
860
861 /* unpack column filter */
862 column = _mesa_map_validate_pbo_source(ctx, 1, &ctx->Unpack,
863 height, 1, 1,
864 format, type, column,
865 "glSeparableFilter2D");
866 if (column) {
867 _mesa_unpack_color_span_float(ctx, height, GL_RGBA,
868 &ctx->Separable2D.Filter[colStart],
869 format, type, column, &ctx->Unpack,
870 0); /* transferOps */
871
872 _mesa_scale_and_bias_rgba(height,
873 (GLfloat (*)[4]) (ctx->Separable2D.Filter + colStart),
874 ctx->Pixel.ConvolutionFilterScale[2][0],
875 ctx->Pixel.ConvolutionFilterScale[2][1],
876 ctx->Pixel.ConvolutionFilterScale[2][2],
877 ctx->Pixel.ConvolutionFilterScale[2][3],
878 ctx->Pixel.ConvolutionFilterBias[2][0],
879 ctx->Pixel.ConvolutionFilterBias[2][1],
880 ctx->Pixel.ConvolutionFilterBias[2][2],
881 ctx->Pixel.ConvolutionFilterBias[2][3]);
882 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
883 }
884
885 if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
886 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
887 ctx->Unpack.BufferObj);
888 }
889
890 ctx->NewState |= _NEW_PIXEL;
891 }
892
893
894 /**********************************************************************/
895 /*** image convolution functions ***/
896 /**********************************************************************/
897
898 static void
899 convolve_1d_reduce(GLint srcWidth, const GLfloat src[][4],
900 GLint filterWidth, const GLfloat filter[][4],
901 GLfloat dest[][4])
902 {
903 GLint dstWidth;
904 GLint i, n;
905
906 if (filterWidth >= 1)
907 dstWidth = srcWidth - (filterWidth - 1);
908 else
909 dstWidth = srcWidth;
910
911 if (dstWidth <= 0)
912 return; /* null result */
913
914 for (i = 0; i < dstWidth; i++) {
915 GLfloat sumR = 0.0;
916 GLfloat sumG = 0.0;
917 GLfloat sumB = 0.0;
918 GLfloat sumA = 0.0;
919 for (n = 0; n < filterWidth; n++) {
920 sumR += src[i + n][RCOMP] * filter[n][RCOMP];
921 sumG += src[i + n][GCOMP] * filter[n][GCOMP];
922 sumB += src[i + n][BCOMP] * filter[n][BCOMP];
923 sumA += src[i + n][ACOMP] * filter[n][ACOMP];
924 }
925 dest[i][RCOMP] = sumR;
926 dest[i][GCOMP] = sumG;
927 dest[i][BCOMP] = sumB;
928 dest[i][ACOMP] = sumA;
929 }
930 }
931
932
933 static void
934 convolve_1d_constant(GLint srcWidth, const GLfloat src[][4],
935 GLint filterWidth, const GLfloat filter[][4],
936 GLfloat dest[][4],
937 const GLfloat borderColor[4])
938 {
939 const GLint halfFilterWidth = filterWidth / 2;
940 GLint i, n;
941
942 for (i = 0; i < srcWidth; i++) {
943 GLfloat sumR = 0.0;
944 GLfloat sumG = 0.0;
945 GLfloat sumB = 0.0;
946 GLfloat sumA = 0.0;
947 for (n = 0; n < filterWidth; n++) {
948 if (i + n < halfFilterWidth || i + n - halfFilterWidth >= srcWidth) {
949 sumR += borderColor[RCOMP] * filter[n][RCOMP];
950 sumG += borderColor[GCOMP] * filter[n][GCOMP];
951 sumB += borderColor[BCOMP] * filter[n][BCOMP];
952 sumA += borderColor[ACOMP] * filter[n][ACOMP];
953 }
954 else {
955 sumR += src[i + n - halfFilterWidth][RCOMP] * filter[n][RCOMP];
956 sumG += src[i + n - halfFilterWidth][GCOMP] * filter[n][GCOMP];
957 sumB += src[i + n - halfFilterWidth][BCOMP] * filter[n][BCOMP];
958 sumA += src[i + n - halfFilterWidth][ACOMP] * filter[n][ACOMP];
959 }
960 }
961 dest[i][RCOMP] = sumR;
962 dest[i][GCOMP] = sumG;
963 dest[i][BCOMP] = sumB;
964 dest[i][ACOMP] = sumA;
965 }
966 }
967
968
969 static void
970 convolve_1d_replicate(GLint srcWidth, const GLfloat src[][4],
971 GLint filterWidth, const GLfloat filter[][4],
972 GLfloat dest[][4])
973 {
974 const GLint halfFilterWidth = filterWidth / 2;
975 GLint i, n;
976
977 for (i = 0; i < srcWidth; i++) {
978 GLfloat sumR = 0.0;
979 GLfloat sumG = 0.0;
980 GLfloat sumB = 0.0;
981 GLfloat sumA = 0.0;
982 for (n = 0; n < filterWidth; n++) {
983 if (i + n < halfFilterWidth) {
984 sumR += src[0][RCOMP] * filter[n][RCOMP];
985 sumG += src[0][GCOMP] * filter[n][GCOMP];
986 sumB += src[0][BCOMP] * filter[n][BCOMP];
987 sumA += src[0][ACOMP] * filter[n][ACOMP];
988 }
989 else if (i + n - halfFilterWidth >= srcWidth) {
990 sumR += src[srcWidth - 1][RCOMP] * filter[n][RCOMP];
991 sumG += src[srcWidth - 1][GCOMP] * filter[n][GCOMP];
992 sumB += src[srcWidth - 1][BCOMP] * filter[n][BCOMP];
993 sumA += src[srcWidth - 1][ACOMP] * filter[n][ACOMP];
994 }
995 else {
996 sumR += src[i + n - halfFilterWidth][RCOMP] * filter[n][RCOMP];
997 sumG += src[i + n - halfFilterWidth][GCOMP] * filter[n][GCOMP];
998 sumB += src[i + n - halfFilterWidth][BCOMP] * filter[n][BCOMP];
999 sumA += src[i + n - halfFilterWidth][ACOMP] * filter[n][ACOMP];
1000 }
1001 }
1002 dest[i][RCOMP] = sumR;
1003 dest[i][GCOMP] = sumG;
1004 dest[i][BCOMP] = sumB;
1005 dest[i][ACOMP] = sumA;
1006 }
1007 }
1008
1009
1010 static void
1011 convolve_2d_reduce(GLint srcWidth, GLint srcHeight,
1012 const GLfloat src[][4],
1013 GLint filterWidth, GLint filterHeight,
1014 const GLfloat filter[][4],
1015 GLfloat dest[][4])
1016 {
1017 GLint dstWidth, dstHeight;
1018 GLint i, j, n, m;
1019
1020 if (filterWidth >= 1)
1021 dstWidth = srcWidth - (filterWidth - 1);
1022 else
1023 dstWidth = srcWidth;
1024
1025 if (filterHeight >= 1)
1026 dstHeight = srcHeight - (filterHeight - 1);
1027 else
1028 dstHeight = srcHeight;
1029
1030 if (dstWidth <= 0 || dstHeight <= 0)
1031 return;
1032
1033 for (j = 0; j < dstHeight; j++) {
1034 for (i = 0; i < dstWidth; i++) {
1035 GLfloat sumR = 0.0;
1036 GLfloat sumG = 0.0;
1037 GLfloat sumB = 0.0;
1038 GLfloat sumA = 0.0;
1039 for (m = 0; m < filterHeight; m++) {
1040 for (n = 0; n < filterWidth; n++) {
1041 const GLint k = (j + m) * srcWidth + i + n;
1042 const GLint f = m * filterWidth + n;
1043 sumR += src[k][RCOMP] * filter[f][RCOMP];
1044 sumG += src[k][GCOMP] * filter[f][GCOMP];
1045 sumB += src[k][BCOMP] * filter[f][BCOMP];
1046 sumA += src[k][ACOMP] * filter[f][ACOMP];
1047 }
1048 }
1049 dest[j * dstWidth + i][RCOMP] = sumR;
1050 dest[j * dstWidth + i][GCOMP] = sumG;
1051 dest[j * dstWidth + i][BCOMP] = sumB;
1052 dest[j * dstWidth + i][ACOMP] = sumA;
1053 }
1054 }
1055 }
1056
1057
1058 static void
1059 convolve_2d_constant(GLint srcWidth, GLint srcHeight,
1060 const GLfloat src[][4],
1061 GLint filterWidth, GLint filterHeight,
1062 const GLfloat filter[][4],
1063 GLfloat dest[][4],
1064 const GLfloat borderColor[4])
1065 {
1066 const GLint halfFilterWidth = filterWidth / 2;
1067 const GLint halfFilterHeight = filterHeight / 2;
1068 GLint i, j, n, m;
1069
1070 for (j = 0; j < srcHeight; j++) {
1071 for (i = 0; i < srcWidth; i++) {
1072 GLfloat sumR = 0.0;
1073 GLfloat sumG = 0.0;
1074 GLfloat sumB = 0.0;
1075 GLfloat sumA = 0.0;
1076 for (m = 0; m < filterHeight; m++) {
1077 for (n = 0; n < filterWidth; n++) {
1078 const GLint f = m * filterWidth + n;
1079 const GLint is = i + n - halfFilterWidth;
1080 const GLint js = j + m - halfFilterHeight;
1081 if (is < 0 || is >= srcWidth ||
1082 js < 0 || js >= srcHeight) {
1083 sumR += borderColor[RCOMP] * filter[f][RCOMP];
1084 sumG += borderColor[GCOMP] * filter[f][GCOMP];
1085 sumB += borderColor[BCOMP] * filter[f][BCOMP];
1086 sumA += borderColor[ACOMP] * filter[f][ACOMP];
1087 }
1088 else {
1089 const GLint k = js * srcWidth + is;
1090 sumR += src[k][RCOMP] * filter[f][RCOMP];
1091 sumG += src[k][GCOMP] * filter[f][GCOMP];
1092 sumB += src[k][BCOMP] * filter[f][BCOMP];
1093 sumA += src[k][ACOMP] * filter[f][ACOMP];
1094 }
1095 }
1096 }
1097 dest[j * srcWidth + i][RCOMP] = sumR;
1098 dest[j * srcWidth + i][GCOMP] = sumG;
1099 dest[j * srcWidth + i][BCOMP] = sumB;
1100 dest[j * srcWidth + i][ACOMP] = sumA;
1101 }
1102 }
1103 }
1104
1105
1106 static void
1107 convolve_2d_replicate(GLint srcWidth, GLint srcHeight,
1108 const GLfloat src[][4],
1109 GLint filterWidth, GLint filterHeight,
1110 const GLfloat filter[][4],
1111 GLfloat dest[][4])
1112 {
1113 const GLint halfFilterWidth = filterWidth / 2;
1114 const GLint halfFilterHeight = filterHeight / 2;
1115 GLint i, j, n, m;
1116
1117 for (j = 0; j < srcHeight; j++) {
1118 for (i = 0; i < srcWidth; i++) {
1119 GLfloat sumR = 0.0;
1120 GLfloat sumG = 0.0;
1121 GLfloat sumB = 0.0;
1122 GLfloat sumA = 0.0;
1123 for (m = 0; m < filterHeight; m++) {
1124 for (n = 0; n < filterWidth; n++) {
1125 const GLint f = m * filterWidth + n;
1126 GLint is = i + n - halfFilterWidth;
1127 GLint js = j + m - halfFilterHeight;
1128 GLint k;
1129 if (is < 0)
1130 is = 0;
1131 else if (is >= srcWidth)
1132 is = srcWidth - 1;
1133 if (js < 0)
1134 js = 0;
1135 else if (js >= srcHeight)
1136 js = srcHeight - 1;
1137 k = js * srcWidth + is;
1138 sumR += src[k][RCOMP] * filter[f][RCOMP];
1139 sumG += src[k][GCOMP] * filter[f][GCOMP];
1140 sumB += src[k][BCOMP] * filter[f][BCOMP];
1141 sumA += src[k][ACOMP] * filter[f][ACOMP];
1142 }
1143 }
1144 dest[j * srcWidth + i][RCOMP] = sumR;
1145 dest[j * srcWidth + i][GCOMP] = sumG;
1146 dest[j * srcWidth + i][BCOMP] = sumB;
1147 dest[j * srcWidth + i][ACOMP] = sumA;
1148 }
1149 }
1150 }
1151
1152
1153 static void
1154 convolve_sep_reduce(GLint srcWidth, GLint srcHeight,
1155 const GLfloat src[][4],
1156 GLint filterWidth, GLint filterHeight,
1157 const GLfloat rowFilt[][4],
1158 const GLfloat colFilt[][4],
1159 GLfloat dest[][4])
1160 {
1161 GLint dstWidth, dstHeight;
1162 GLint i, j, n, m;
1163
1164 if (filterWidth >= 1)
1165 dstWidth = srcWidth - (filterWidth - 1);
1166 else
1167 dstWidth = srcWidth;
1168
1169 if (filterHeight >= 1)
1170 dstHeight = srcHeight - (filterHeight - 1);
1171 else
1172 dstHeight = srcHeight;
1173
1174 if (dstWidth <= 0 || dstHeight <= 0)
1175 return;
1176
1177 for (j = 0; j < dstHeight; j++) {
1178 for (i = 0; i < dstWidth; i++) {
1179 GLfloat sumR = 0.0;
1180 GLfloat sumG = 0.0;
1181 GLfloat sumB = 0.0;
1182 GLfloat sumA = 0.0;
1183 for (m = 0; m < filterHeight; m++) {
1184 for (n = 0; n < filterWidth; n++) {
1185 GLint k = (j + m) * srcWidth + i + n;
1186 sumR += src[k][RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
1187 sumG += src[k][GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
1188 sumB += src[k][BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
1189 sumA += src[k][ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
1190 }
1191 }
1192 dest[j * dstWidth + i][RCOMP] = sumR;
1193 dest[j * dstWidth + i][GCOMP] = sumG;
1194 dest[j * dstWidth + i][BCOMP] = sumB;
1195 dest[j * dstWidth + i][ACOMP] = sumA;
1196 }
1197 }
1198 }
1199
1200
1201 static void
1202 convolve_sep_constant(GLint srcWidth, GLint srcHeight,
1203 const GLfloat src[][4],
1204 GLint filterWidth, GLint filterHeight,
1205 const GLfloat rowFilt[][4],
1206 const GLfloat colFilt[][4],
1207 GLfloat dest[][4],
1208 const GLfloat borderColor[4])
1209 {
1210 const GLint halfFilterWidth = filterWidth / 2;
1211 const GLint halfFilterHeight = filterHeight / 2;
1212 GLint i, j, n, m;
1213
1214 for (j = 0; j < srcHeight; j++) {
1215 for (i = 0; i < srcWidth; i++) {
1216 GLfloat sumR = 0.0;
1217 GLfloat sumG = 0.0;
1218 GLfloat sumB = 0.0;
1219 GLfloat sumA = 0.0;
1220 for (m = 0; m < filterHeight; m++) {
1221 for (n = 0; n < filterWidth; n++) {
1222 const GLint is = i + n - halfFilterWidth;
1223 const GLint js = j + m - halfFilterHeight;
1224 if (is < 0 || is >= srcWidth ||
1225 js < 0 || js >= srcHeight) {
1226 sumR += borderColor[RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
1227 sumG += borderColor[GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
1228 sumB += borderColor[BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
1229 sumA += borderColor[ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
1230 }
1231 else {
1232 GLint k = js * srcWidth + is;
1233 sumR += src[k][RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
1234 sumG += src[k][GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
1235 sumB += src[k][BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
1236 sumA += src[k][ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
1237 }
1238
1239 }
1240 }
1241 dest[j * srcWidth + i][RCOMP] = sumR;
1242 dest[j * srcWidth + i][GCOMP] = sumG;
1243 dest[j * srcWidth + i][BCOMP] = sumB;
1244 dest[j * srcWidth + i][ACOMP] = sumA;
1245 }
1246 }
1247 }
1248
1249
1250 static void
1251 convolve_sep_replicate(GLint srcWidth, GLint srcHeight,
1252 const GLfloat src[][4],
1253 GLint filterWidth, GLint filterHeight,
1254 const GLfloat rowFilt[][4],
1255 const GLfloat colFilt[][4],
1256 GLfloat dest[][4])
1257 {
1258 const GLint halfFilterWidth = filterWidth / 2;
1259 const GLint halfFilterHeight = filterHeight / 2;
1260 GLint i, j, n, m;
1261
1262 for (j = 0; j < srcHeight; j++) {
1263 for (i = 0; i < srcWidth; i++) {
1264 GLfloat sumR = 0.0;
1265 GLfloat sumG = 0.0;
1266 GLfloat sumB = 0.0;
1267 GLfloat sumA = 0.0;
1268 for (m = 0; m < filterHeight; m++) {
1269 for (n = 0; n < filterWidth; n++) {
1270 GLint is = i + n - halfFilterWidth;
1271 GLint js = j + m - halfFilterHeight;
1272 GLint k;
1273 if (is < 0)
1274 is = 0;
1275 else if (is >= srcWidth)
1276 is = srcWidth - 1;
1277 if (js < 0)
1278 js = 0;
1279 else if (js >= srcHeight)
1280 js = srcHeight - 1;
1281 k = js * srcWidth + is;
1282 sumR += src[k][RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
1283 sumG += src[k][GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
1284 sumB += src[k][BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
1285 sumA += src[k][ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
1286 }
1287 }
1288 dest[j * srcWidth + i][RCOMP] = sumR;
1289 dest[j * srcWidth + i][GCOMP] = sumG;
1290 dest[j * srcWidth + i][BCOMP] = sumB;
1291 dest[j * srcWidth + i][ACOMP] = sumA;
1292 }
1293 }
1294 }
1295
1296
1297
1298 void
1299 _mesa_convolve_1d_image(const GLcontext *ctx, GLsizei *width,
1300 const GLfloat *srcImage, GLfloat *dstImage)
1301 {
1302 switch (ctx->Pixel.ConvolutionBorderMode[0]) {
1303 case GL_REDUCE:
1304 convolve_1d_reduce(*width, (const GLfloat (*)[4]) srcImage,
1305 ctx->Convolution1D.Width,
1306 (const GLfloat (*)[4]) ctx->Convolution1D.Filter,
1307 (GLfloat (*)[4]) dstImage);
1308 *width = *width - (MAX2(ctx->Convolution1D.Width, 1) - 1);
1309 break;
1310 case GL_CONSTANT_BORDER:
1311 convolve_1d_constant(*width, (const GLfloat (*)[4]) srcImage,
1312 ctx->Convolution1D.Width,
1313 (const GLfloat (*)[4]) ctx->Convolution1D.Filter,
1314 (GLfloat (*)[4]) dstImage,
1315 ctx->Pixel.ConvolutionBorderColor[0]);
1316 break;
1317 case GL_REPLICATE_BORDER:
1318 convolve_1d_replicate(*width, (const GLfloat (*)[4]) srcImage,
1319 ctx->Convolution1D.Width,
1320 (const GLfloat (*)[4]) ctx->Convolution1D.Filter,
1321 (GLfloat (*)[4]) dstImage);
1322 break;
1323 default:
1324 ;
1325 }
1326 }
1327
1328
1329 void
1330 _mesa_convolve_2d_image(const GLcontext *ctx, GLsizei *width, GLsizei *height,
1331 const GLfloat *srcImage, GLfloat *dstImage)
1332 {
1333 switch (ctx->Pixel.ConvolutionBorderMode[1]) {
1334 case GL_REDUCE:
1335 convolve_2d_reduce(*width, *height,
1336 (const GLfloat (*)[4]) srcImage,
1337 ctx->Convolution2D.Width,
1338 ctx->Convolution2D.Height,
1339 (const GLfloat (*)[4]) ctx->Convolution2D.Filter,
1340 (GLfloat (*)[4]) dstImage);
1341 *width = *width - (MAX2(ctx->Convolution2D.Width, 1) - 1);
1342 *height = *height - (MAX2(ctx->Convolution2D.Height, 1) - 1);
1343 break;
1344 case GL_CONSTANT_BORDER:
1345 convolve_2d_constant(*width, *height,
1346 (const GLfloat (*)[4]) srcImage,
1347 ctx->Convolution2D.Width,
1348 ctx->Convolution2D.Height,
1349 (const GLfloat (*)[4]) ctx->Convolution2D.Filter,
1350 (GLfloat (*)[4]) dstImage,
1351 ctx->Pixel.ConvolutionBorderColor[1]);
1352 break;
1353 case GL_REPLICATE_BORDER:
1354 convolve_2d_replicate(*width, *height,
1355 (const GLfloat (*)[4]) srcImage,
1356 ctx->Convolution2D.Width,
1357 ctx->Convolution2D.Height,
1358 (const GLfloat (*)[4])ctx->Convolution2D.Filter,
1359 (GLfloat (*)[4]) dstImage);
1360 break;
1361 default:
1362 ;
1363 }
1364 }
1365
1366
1367 void
1368 _mesa_convolve_sep_image(const GLcontext *ctx,
1369 GLsizei *width, GLsizei *height,
1370 const GLfloat *srcImage, GLfloat *dstImage)
1371 {
1372 const GLfloat *rowFilter = ctx->Separable2D.Filter;
1373 const GLfloat *colFilter = rowFilter + 4 * MAX_CONVOLUTION_WIDTH;
1374
1375 switch (ctx->Pixel.ConvolutionBorderMode[2]) {
1376 case GL_REDUCE:
1377 convolve_sep_reduce(*width, *height,
1378 (const GLfloat (*)[4]) srcImage,
1379 ctx->Separable2D.Width,
1380 ctx->Separable2D.Height,
1381 (const GLfloat (*)[4]) rowFilter,
1382 (const GLfloat (*)[4]) colFilter,
1383 (GLfloat (*)[4]) dstImage);
1384 *width = *width - (MAX2(ctx->Separable2D.Width, 1) - 1);
1385 *height = *height - (MAX2(ctx->Separable2D.Height, 1) - 1);
1386 break;
1387 case GL_CONSTANT_BORDER:
1388 convolve_sep_constant(*width, *height,
1389 (const GLfloat (*)[4]) srcImage,
1390 ctx->Separable2D.Width,
1391 ctx->Separable2D.Height,
1392 (const GLfloat (*)[4]) rowFilter,
1393 (const GLfloat (*)[4]) colFilter,
1394 (GLfloat (*)[4]) dstImage,
1395 ctx->Pixel.ConvolutionBorderColor[2]);
1396 break;
1397 case GL_REPLICATE_BORDER:
1398 convolve_sep_replicate(*width, *height,
1399 (const GLfloat (*)[4]) srcImage,
1400 ctx->Separable2D.Width,
1401 ctx->Separable2D.Height,
1402 (const GLfloat (*)[4]) rowFilter,
1403 (const GLfloat (*)[4]) colFilter,
1404 (GLfloat (*)[4]) dstImage);
1405 break;
1406 default:
1407 ;
1408 }
1409 }
1410
1411
1412
1413 /*
1414 * This function computes an image's size after convolution.
1415 * If the convolution border mode is GL_REDUCE, the post-convolution
1416 * image will be smaller than the original.
1417 */
1418 void
1419 _mesa_adjust_image_for_convolution(const GLcontext *ctx, GLuint dimensions,
1420 GLsizei *width, GLsizei *height)
1421 {
1422 if (ctx->Pixel.Convolution1DEnabled
1423 && dimensions == 1
1424 && ctx->Pixel.ConvolutionBorderMode[0] == GL_REDUCE) {
1425 *width = *width - (MAX2(ctx->Convolution1D.Width, 1) - 1);
1426 }
1427 else if (ctx->Pixel.Convolution2DEnabled
1428 && dimensions > 1
1429 && ctx->Pixel.ConvolutionBorderMode[1] == GL_REDUCE) {
1430 *width = *width - (MAX2(ctx->Convolution2D.Width, 1) - 1);
1431 *height = *height - (MAX2(ctx->Convolution2D.Height, 1) - 1);
1432 }
1433 else if (ctx->Pixel.Separable2DEnabled
1434 && dimensions > 1
1435 && ctx->Pixel.ConvolutionBorderMode[2] == GL_REDUCE) {
1436 *width = *width - (MAX2(ctx->Separable2D.Width, 1) - 1);
1437 *height = *height - (MAX2(ctx->Separable2D.Height, 1) - 1);
1438 }
1439 }
1440
1441
1442 void
1443 _mesa_init_convolve_dispatch(struct _glapi_table *disp)
1444 {
1445 SET_ConvolutionFilter1D(disp, _mesa_ConvolutionFilter1D);
1446 SET_ConvolutionFilter2D(disp, _mesa_ConvolutionFilter2D);
1447 SET_ConvolutionParameterf(disp, _mesa_ConvolutionParameterf);
1448 SET_ConvolutionParameterfv(disp, _mesa_ConvolutionParameterfv);
1449 SET_ConvolutionParameteri(disp, _mesa_ConvolutionParameteri);
1450 SET_ConvolutionParameteriv(disp, _mesa_ConvolutionParameteriv);
1451 SET_CopyConvolutionFilter1D(disp, _mesa_CopyConvolutionFilter1D);
1452 SET_CopyConvolutionFilter2D(disp, _mesa_CopyConvolutionFilter2D);
1453 SET_GetConvolutionFilter(disp, _mesa_GetConvolutionFilter);
1454 SET_GetConvolutionParameterfv(disp, _mesa_GetConvolutionParameterfv);
1455 SET_GetConvolutionParameteriv(disp, _mesa_GetConvolutionParameteriv);
1456 SET_SeparableFilter2D(disp, _mesa_SeparableFilter2D);
1457 SET_GetSeparableFilter(disp, _mesa_GetSeparableFilter);
1458 }
1459
1460
1461 #endif /* FEATURE_convolve */