Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by nazgul ( 16 years ago )
#include <malloc.h>
#include <memory.h>
#include "convolution.h"

int
convolution_apply (short *rgb, unsigned int width, unsigned int height,
                   double **kernel,
                   unsigned int kernelWidth, unsigned int kernelHeight)
{
  unsigned int x, y, i, j;
  short *outputRGB = malloc (sizeof (short) * width * height * 3);

#pragma omp parallel \
    private(x, y, i, j)

#pragma omp for
  for (x = 0; x < width; x++)
  {
      for (y = 0; y < height; y++)
      {
          double rSum = 0, gSum = 0, bSum = 0, kSum = 0;

          for (i = 0; i < kernelWidth; i++)
          {
              for (j = 0; j < kernelHeight; j++)
              {
                  int pixelPosX = x + (i - (kernelWidth / 2));
                  int pixelPosY = y + (j - (kernelHeight / 2));
                  if ((pixelPosX < 0) || 
                      (pixelPosX >= width) || 
                      (pixelPosY < 0) || 
                      (pixelPosY >= height)) continue;

                  short b = rgb[3 * (width * pixelPosY + pixelPosX) + 0];
                  short g = rgb[3 * (width * pixelPosY + pixelPosX) + 1];
                  short r = rgb[3 * (width * pixelPosY + pixelPosX) + 2];

                  double kernelVal = kernel[i][j];

                  rSum += r * kernelVal;
                  gSum += g * kernelVal;
                  bSum += b * kernelVal;

                  kSum += kernelVal;
              }
          }

          if (kSum <= 0) kSum = 1;

          rSum /= kSum;
          if (rSum < 0) rSum = 0;
          if (rSum > 255) rSum = 255;

          gSum /= kSum;
          if (gSum < 0) gSum = 0;
          if (gSum > 255) gSum = 255;

          bSum /= kSum;
          if (bSum < 0) bSum = 0;
          if (bSum > 255) bSum = 255;

          outputRGB[3 * (width * y + x) + 0] = (short)bSum;
          outputRGB[3 * (width * y + x) + 1] = (short)gSum;
          outputRGB[3 * (width * y + x) + 2] = (short)rSum;
      }
  }

  memcpy (rgb, outputRGB, sizeof(short) * 3 * width * height);
  free (outputRGB);

  return 0;
}

 

Revise this Paste

Your Name: Code Language: