-static void GLAPIENTRY
-_mesa_SeparableFilter2D(GLenum target, GLenum internalFormat, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *row, const GLvoid *column)
-{
- const GLint colStart = MAX_CONVOLUTION_WIDTH * 4;
- GLint baseFormat;
- GET_CURRENT_CONTEXT(ctx);
- ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
-
- if (target != GL_SEPARABLE_2D) {
- _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(target)");
- return;
- }
-
- baseFormat = base_filter_format(internalFormat);
- if (baseFormat < 0 || baseFormat == GL_COLOR_INDEX) {
- _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(internalFormat)");
- return;
- }
-
- if (width < 0 || width > MAX_CONVOLUTION_WIDTH) {
- _mesa_error(ctx, GL_INVALID_VALUE, "glSeparableFilter2D(width)");
- return;
- }
- if (height < 0 || height > MAX_CONVOLUTION_HEIGHT) {
- _mesa_error(ctx, GL_INVALID_VALUE, "glSeparableFilter2D(height)");
- return;
- }
-
- if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
- _mesa_error(ctx, GL_INVALID_OPERATION, "glSeparableFilter2D(format or type)");
- return;
- }
-
- if (format == GL_COLOR_INDEX ||
- format == GL_STENCIL_INDEX ||
- format == GL_DEPTH_COMPONENT ||
- format == GL_INTENSITY ||
- type == GL_BITMAP) {
- _mesa_error(ctx, GL_INVALID_ENUM, "glSeparableFilter2D(format or type)");
- return;
- }
-
- ctx->Separable2D.Format = format;
- ctx->Separable2D.InternalFormat = internalFormat;
- ctx->Separable2D.Width = width;
- ctx->Separable2D.Height = height;
-
- /* unpack row filter */
- row = _mesa_map_validate_pbo_source(ctx, 1, &ctx->Unpack,
- width, 1, 1,
- format, type, row,
- "glSeparableFilter2D");
- if (row) {
- _mesa_unpack_color_span_float(ctx, width, GL_RGBA,
- ctx->Separable2D.Filter,
- format, type, row, &ctx->Unpack,
- 0x0); /* transferOps */
- _mesa_scale_and_bias_rgba(width,
- (GLfloat (*)[4]) ctx->Separable2D.Filter,
- ctx->Pixel.ConvolutionFilterScale[2][0],
- ctx->Pixel.ConvolutionFilterScale[2][1],
- ctx->Pixel.ConvolutionFilterScale[2][2],
- ctx->Pixel.ConvolutionFilterScale[2][3],
- ctx->Pixel.ConvolutionFilterBias[2][0],
- ctx->Pixel.ConvolutionFilterBias[2][1],
- ctx->Pixel.ConvolutionFilterBias[2][2],
- ctx->Pixel.ConvolutionFilterBias[2][3]);
- _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
- }
-
- /* unpack column filter */
- column = _mesa_map_validate_pbo_source(ctx, 1, &ctx->Unpack,
- height, 1, 1,
- format, type, column,
- "glSeparableFilter2D");
- if (column) {
- _mesa_unpack_color_span_float(ctx, height, GL_RGBA,
- &ctx->Separable2D.Filter[colStart],
- format, type, column, &ctx->Unpack,
- 0); /* transferOps */
-
- _mesa_scale_and_bias_rgba(height,
- (GLfloat (*)[4]) (ctx->Separable2D.Filter + colStart),
- ctx->Pixel.ConvolutionFilterScale[2][0],
- ctx->Pixel.ConvolutionFilterScale[2][1],
- ctx->Pixel.ConvolutionFilterScale[2][2],
- ctx->Pixel.ConvolutionFilterScale[2][3],
- ctx->Pixel.ConvolutionFilterBias[2][0],
- ctx->Pixel.ConvolutionFilterBias[2][1],
- ctx->Pixel.ConvolutionFilterBias[2][2],
- ctx->Pixel.ConvolutionFilterBias[2][3]);
- _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
- }
-
- if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
- ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
- ctx->Unpack.BufferObj);
- }
-
- ctx->NewState |= _NEW_PIXEL;
-}
-
-
-/**********************************************************************/
-/*** image convolution functions ***/
-/**********************************************************************/
-
-static void
-convolve_1d_reduce(GLint srcWidth, const GLfloat src[][4],
- GLint filterWidth, const GLfloat filter[][4],
- GLfloat dest[][4])
-{
- GLint dstWidth;
- GLint i, n;
-
- if (filterWidth >= 1)
- dstWidth = srcWidth - (filterWidth - 1);
- else
- dstWidth = srcWidth;
-
- if (dstWidth <= 0)
- return; /* null result */
-
- for (i = 0; i < dstWidth; i++) {
- GLfloat sumR = 0.0;
- GLfloat sumG = 0.0;
- GLfloat sumB = 0.0;
- GLfloat sumA = 0.0;
- for (n = 0; n < filterWidth; n++) {
- sumR += src[i + n][RCOMP] * filter[n][RCOMP];
- sumG += src[i + n][GCOMP] * filter[n][GCOMP];
- sumB += src[i + n][BCOMP] * filter[n][BCOMP];
- sumA += src[i + n][ACOMP] * filter[n][ACOMP];
- }
- dest[i][RCOMP] = sumR;
- dest[i][GCOMP] = sumG;
- dest[i][BCOMP] = sumB;
- dest[i][ACOMP] = sumA;
- }
-}
-
-
-static void
-convolve_1d_constant(GLint srcWidth, const GLfloat src[][4],
- GLint filterWidth, const GLfloat filter[][4],
- GLfloat dest[][4],
- const GLfloat borderColor[4])
-{
- const GLint halfFilterWidth = filterWidth / 2;
- GLint i, n;
-
- for (i = 0; i < srcWidth; i++) {
- GLfloat sumR = 0.0;
- GLfloat sumG = 0.0;
- GLfloat sumB = 0.0;
- GLfloat sumA = 0.0;
- for (n = 0; n < filterWidth; n++) {
- if (i + n < halfFilterWidth || i + n - halfFilterWidth >= srcWidth) {
- sumR += borderColor[RCOMP] * filter[n][RCOMP];
- sumG += borderColor[GCOMP] * filter[n][GCOMP];
- sumB += borderColor[BCOMP] * filter[n][BCOMP];
- sumA += borderColor[ACOMP] * filter[n][ACOMP];
- }
- else {
- sumR += src[i + n - halfFilterWidth][RCOMP] * filter[n][RCOMP];
- sumG += src[i + n - halfFilterWidth][GCOMP] * filter[n][GCOMP];
- sumB += src[i + n - halfFilterWidth][BCOMP] * filter[n][BCOMP];
- sumA += src[i + n - halfFilterWidth][ACOMP] * filter[n][ACOMP];
- }
- }
- dest[i][RCOMP] = sumR;
- dest[i][GCOMP] = sumG;
- dest[i][BCOMP] = sumB;
- dest[i][ACOMP] = sumA;
- }
-}
-
-
-static void
-convolve_1d_replicate(GLint srcWidth, const GLfloat src[][4],
- GLint filterWidth, const GLfloat filter[][4],
- GLfloat dest[][4])
-{
- const GLint halfFilterWidth = filterWidth / 2;
- GLint i, n;
-
- for (i = 0; i < srcWidth; i++) {
- GLfloat sumR = 0.0;
- GLfloat sumG = 0.0;
- GLfloat sumB = 0.0;
- GLfloat sumA = 0.0;
- for (n = 0; n < filterWidth; n++) {
- if (i + n < halfFilterWidth) {
- sumR += src[0][RCOMP] * filter[n][RCOMP];
- sumG += src[0][GCOMP] * filter[n][GCOMP];
- sumB += src[0][BCOMP] * filter[n][BCOMP];
- sumA += src[0][ACOMP] * filter[n][ACOMP];
- }
- else if (i + n - halfFilterWidth >= srcWidth) {
- sumR += src[srcWidth - 1][RCOMP] * filter[n][RCOMP];
- sumG += src[srcWidth - 1][GCOMP] * filter[n][GCOMP];
- sumB += src[srcWidth - 1][BCOMP] * filter[n][BCOMP];
- sumA += src[srcWidth - 1][ACOMP] * filter[n][ACOMP];
- }
- else {
- sumR += src[i + n - halfFilterWidth][RCOMP] * filter[n][RCOMP];
- sumG += src[i + n - halfFilterWidth][GCOMP] * filter[n][GCOMP];
- sumB += src[i + n - halfFilterWidth][BCOMP] * filter[n][BCOMP];
- sumA += src[i + n - halfFilterWidth][ACOMP] * filter[n][ACOMP];
- }
- }
- dest[i][RCOMP] = sumR;
- dest[i][GCOMP] = sumG;
- dest[i][BCOMP] = sumB;
- dest[i][ACOMP] = sumA;
- }
-}
-
-
-static void
-convolve_2d_reduce(GLint srcWidth, GLint srcHeight,
- const GLfloat src[][4],
- GLint filterWidth, GLint filterHeight,
- const GLfloat filter[][4],
- GLfloat dest[][4])
-{
- GLint dstWidth, dstHeight;
- GLint i, j, n, m;
-
- if (filterWidth >= 1)
- dstWidth = srcWidth - (filterWidth - 1);
- else
- dstWidth = srcWidth;
-
- if (filterHeight >= 1)
- dstHeight = srcHeight - (filterHeight - 1);
- else
- dstHeight = srcHeight;
-
- if (dstWidth <= 0 || dstHeight <= 0)
- return;
-
- for (j = 0; j < dstHeight; j++) {
- for (i = 0; i < dstWidth; i++) {
- GLfloat sumR = 0.0;
- GLfloat sumG = 0.0;
- GLfloat sumB = 0.0;
- GLfloat sumA = 0.0;
- for (m = 0; m < filterHeight; m++) {
- for (n = 0; n < filterWidth; n++) {
- const GLint k = (j + m) * srcWidth + i + n;
- const GLint f = m * filterWidth + n;
- sumR += src[k][RCOMP] * filter[f][RCOMP];
- sumG += src[k][GCOMP] * filter[f][GCOMP];
- sumB += src[k][BCOMP] * filter[f][BCOMP];
- sumA += src[k][ACOMP] * filter[f][ACOMP];
- }
- }
- dest[j * dstWidth + i][RCOMP] = sumR;
- dest[j * dstWidth + i][GCOMP] = sumG;
- dest[j * dstWidth + i][BCOMP] = sumB;
- dest[j * dstWidth + i][ACOMP] = sumA;
- }
- }
-}
-
-
-static void
-convolve_2d_constant(GLint srcWidth, GLint srcHeight,
- const GLfloat src[][4],
- GLint filterWidth, GLint filterHeight,
- const GLfloat filter[][4],
- GLfloat dest[][4],
- const GLfloat borderColor[4])
-{
- const GLint halfFilterWidth = filterWidth / 2;
- const GLint halfFilterHeight = filterHeight / 2;
- GLint i, j, n, m;
-
- for (j = 0; j < srcHeight; j++) {
- for (i = 0; i < srcWidth; i++) {
- GLfloat sumR = 0.0;
- GLfloat sumG = 0.0;
- GLfloat sumB = 0.0;
- GLfloat sumA = 0.0;
- for (m = 0; m < filterHeight; m++) {
- for (n = 0; n < filterWidth; n++) {
- const GLint f = m * filterWidth + n;
- const GLint is = i + n - halfFilterWidth;
- const GLint js = j + m - halfFilterHeight;
- if (is < 0 || is >= srcWidth ||
- js < 0 || js >= srcHeight) {
- sumR += borderColor[RCOMP] * filter[f][RCOMP];
- sumG += borderColor[GCOMP] * filter[f][GCOMP];
- sumB += borderColor[BCOMP] * filter[f][BCOMP];
- sumA += borderColor[ACOMP] * filter[f][ACOMP];
- }
- else {
- const GLint k = js * srcWidth + is;
- sumR += src[k][RCOMP] * filter[f][RCOMP];
- sumG += src[k][GCOMP] * filter[f][GCOMP];
- sumB += src[k][BCOMP] * filter[f][BCOMP];
- sumA += src[k][ACOMP] * filter[f][ACOMP];
- }
- }
- }
- dest[j * srcWidth + i][RCOMP] = sumR;
- dest[j * srcWidth + i][GCOMP] = sumG;
- dest[j * srcWidth + i][BCOMP] = sumB;
- dest[j * srcWidth + i][ACOMP] = sumA;
- }
- }
-}
-
-
-static void
-convolve_2d_replicate(GLint srcWidth, GLint srcHeight,
- const GLfloat src[][4],
- GLint filterWidth, GLint filterHeight,
- const GLfloat filter[][4],
- GLfloat dest[][4])
-{
- const GLint halfFilterWidth = filterWidth / 2;
- const GLint halfFilterHeight = filterHeight / 2;
- GLint i, j, n, m;
-
- for (j = 0; j < srcHeight; j++) {
- for (i = 0; i < srcWidth; i++) {
- GLfloat sumR = 0.0;
- GLfloat sumG = 0.0;
- GLfloat sumB = 0.0;
- GLfloat sumA = 0.0;
- for (m = 0; m < filterHeight; m++) {
- for (n = 0; n < filterWidth; n++) {
- const GLint f = m * filterWidth + n;
- GLint is = i + n - halfFilterWidth;
- GLint js = j + m - halfFilterHeight;
- GLint k;
- if (is < 0)
- is = 0;
- else if (is >= srcWidth)
- is = srcWidth - 1;
- if (js < 0)
- js = 0;
- else if (js >= srcHeight)
- js = srcHeight - 1;
- k = js * srcWidth + is;
- sumR += src[k][RCOMP] * filter[f][RCOMP];
- sumG += src[k][GCOMP] * filter[f][GCOMP];
- sumB += src[k][BCOMP] * filter[f][BCOMP];
- sumA += src[k][ACOMP] * filter[f][ACOMP];
- }
- }
- dest[j * srcWidth + i][RCOMP] = sumR;
- dest[j * srcWidth + i][GCOMP] = sumG;
- dest[j * srcWidth + i][BCOMP] = sumB;
- dest[j * srcWidth + i][ACOMP] = sumA;
- }
- }
-}
-
-
-static void
-convolve_sep_reduce(GLint srcWidth, GLint srcHeight,
- const GLfloat src[][4],
- GLint filterWidth, GLint filterHeight,
- const GLfloat rowFilt[][4],
- const GLfloat colFilt[][4],
- GLfloat dest[][4])
-{
- GLint dstWidth, dstHeight;
- GLint i, j, n, m;
-
- if (filterWidth >= 1)
- dstWidth = srcWidth - (filterWidth - 1);
- else
- dstWidth = srcWidth;
-
- if (filterHeight >= 1)
- dstHeight = srcHeight - (filterHeight - 1);
- else
- dstHeight = srcHeight;
-
- if (dstWidth <= 0 || dstHeight <= 0)
- return;
-
- for (j = 0; j < dstHeight; j++) {
- for (i = 0; i < dstWidth; i++) {
- GLfloat sumR = 0.0;
- GLfloat sumG = 0.0;
- GLfloat sumB = 0.0;
- GLfloat sumA = 0.0;
- for (m = 0; m < filterHeight; m++) {
- for (n = 0; n < filterWidth; n++) {
- GLint k = (j + m) * srcWidth + i + n;
- sumR += src[k][RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
- sumG += src[k][GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
- sumB += src[k][BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
- sumA += src[k][ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
- }
- }
- dest[j * dstWidth + i][RCOMP] = sumR;
- dest[j * dstWidth + i][GCOMP] = sumG;
- dest[j * dstWidth + i][BCOMP] = sumB;
- dest[j * dstWidth + i][ACOMP] = sumA;
- }
- }
-}
-
-
-static void
-convolve_sep_constant(GLint srcWidth, GLint srcHeight,
- const GLfloat src[][4],
- GLint filterWidth, GLint filterHeight,
- const GLfloat rowFilt[][4],
- const GLfloat colFilt[][4],
- GLfloat dest[][4],
- const GLfloat borderColor[4])
-{
- const GLint halfFilterWidth = filterWidth / 2;
- const GLint halfFilterHeight = filterHeight / 2;
- GLint i, j, n, m;
-
- for (j = 0; j < srcHeight; j++) {
- for (i = 0; i < srcWidth; i++) {
- GLfloat sumR = 0.0;
- GLfloat sumG = 0.0;
- GLfloat sumB = 0.0;
- GLfloat sumA = 0.0;
- for (m = 0; m < filterHeight; m++) {
- for (n = 0; n < filterWidth; n++) {
- const GLint is = i + n - halfFilterWidth;
- const GLint js = j + m - halfFilterHeight;
- if (is < 0 || is >= srcWidth ||
- js < 0 || js >= srcHeight) {
- sumR += borderColor[RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
- sumG += borderColor[GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
- sumB += borderColor[BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
- sumA += borderColor[ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
- }
- else {
- GLint k = js * srcWidth + is;
- sumR += src[k][RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
- sumG += src[k][GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
- sumB += src[k][BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
- sumA += src[k][ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
- }
-
- }
- }
- dest[j * srcWidth + i][RCOMP] = sumR;
- dest[j * srcWidth + i][GCOMP] = sumG;
- dest[j * srcWidth + i][BCOMP] = sumB;
- dest[j * srcWidth + i][ACOMP] = sumA;
- }
- }
-}
-
-
-static void
-convolve_sep_replicate(GLint srcWidth, GLint srcHeight,
- const GLfloat src[][4],
- GLint filterWidth, GLint filterHeight,
- const GLfloat rowFilt[][4],
- const GLfloat colFilt[][4],
- GLfloat dest[][4])
-{
- const GLint halfFilterWidth = filterWidth / 2;
- const GLint halfFilterHeight = filterHeight / 2;
- GLint i, j, n, m;
-
- for (j = 0; j < srcHeight; j++) {
- for (i = 0; i < srcWidth; i++) {
- GLfloat sumR = 0.0;
- GLfloat sumG = 0.0;
- GLfloat sumB = 0.0;
- GLfloat sumA = 0.0;
- for (m = 0; m < filterHeight; m++) {
- for (n = 0; n < filterWidth; n++) {
- GLint is = i + n - halfFilterWidth;
- GLint js = j + m - halfFilterHeight;
- GLint k;
- if (is < 0)
- is = 0;
- else if (is >= srcWidth)
- is = srcWidth - 1;
- if (js < 0)
- js = 0;
- else if (js >= srcHeight)
- js = srcHeight - 1;
- k = js * srcWidth + is;
- sumR += src[k][RCOMP] * rowFilt[n][RCOMP] * colFilt[m][RCOMP];
- sumG += src[k][GCOMP] * rowFilt[n][GCOMP] * colFilt[m][GCOMP];
- sumB += src[k][BCOMP] * rowFilt[n][BCOMP] * colFilt[m][BCOMP];
- sumA += src[k][ACOMP] * rowFilt[n][ACOMP] * colFilt[m][ACOMP];
- }
- }
- dest[j * srcWidth + i][RCOMP] = sumR;
- dest[j * srcWidth + i][GCOMP] = sumG;
- dest[j * srcWidth + i][BCOMP] = sumB;
- dest[j * srcWidth + i][ACOMP] = sumA;
- }
- }
-}
-
-
-
-void
-_mesa_convolve_1d_image(const GLcontext *ctx, GLsizei *width,
- const GLfloat *srcImage, GLfloat *dstImage)
-{
- switch (ctx->Pixel.ConvolutionBorderMode[0]) {
- case GL_REDUCE:
- convolve_1d_reduce(*width, (const GLfloat (*)[4]) srcImage,
- ctx->Convolution1D.Width,
- (const GLfloat (*)[4]) ctx->Convolution1D.Filter,
- (GLfloat (*)[4]) dstImage);
- *width = *width - (MAX2(ctx->Convolution1D.Width, 1) - 1);
- break;
- case GL_CONSTANT_BORDER:
- convolve_1d_constant(*width, (const GLfloat (*)[4]) srcImage,
- ctx->Convolution1D.Width,
- (const GLfloat (*)[4]) ctx->Convolution1D.Filter,
- (GLfloat (*)[4]) dstImage,
- ctx->Pixel.ConvolutionBorderColor[0]);
- break;
- case GL_REPLICATE_BORDER:
- convolve_1d_replicate(*width, (const GLfloat (*)[4]) srcImage,
- ctx->Convolution1D.Width,
- (const GLfloat (*)[4]) ctx->Convolution1D.Filter,
- (GLfloat (*)[4]) dstImage);
- break;
- default:
- ;
- }
-}
-
-
-void
-_mesa_convolve_2d_image(const GLcontext *ctx, GLsizei *width, GLsizei *height,
- const GLfloat *srcImage, GLfloat *dstImage)
-{
- switch (ctx->Pixel.ConvolutionBorderMode[1]) {
- case GL_REDUCE:
- convolve_2d_reduce(*width, *height,
- (const GLfloat (*)[4]) srcImage,
- ctx->Convolution2D.Width,
- ctx->Convolution2D.Height,
- (const GLfloat (*)[4]) ctx->Convolution2D.Filter,
- (GLfloat (*)[4]) dstImage);
- *width = *width - (MAX2(ctx->Convolution2D.Width, 1) - 1);
- *height = *height - (MAX2(ctx->Convolution2D.Height, 1) - 1);
- break;
- case GL_CONSTANT_BORDER:
- convolve_2d_constant(*width, *height,
- (const GLfloat (*)[4]) srcImage,
- ctx->Convolution2D.Width,
- ctx->Convolution2D.Height,
- (const GLfloat (*)[4]) ctx->Convolution2D.Filter,
- (GLfloat (*)[4]) dstImage,
- ctx->Pixel.ConvolutionBorderColor[1]);
- break;
- case GL_REPLICATE_BORDER:
- convolve_2d_replicate(*width, *height,
- (const GLfloat (*)[4]) srcImage,
- ctx->Convolution2D.Width,
- ctx->Convolution2D.Height,
- (const GLfloat (*)[4])ctx->Convolution2D.Filter,
- (GLfloat (*)[4]) dstImage);
- break;
- default:
- ;
- }
-}
-
-
-void
-_mesa_convolve_sep_image(const GLcontext *ctx,
- GLsizei *width, GLsizei *height,
- const GLfloat *srcImage, GLfloat *dstImage)