Image filter code problem

Started by
4 comments, last by manhaeve 15 years, 9 months ago
Hi, I tried to make some filter applying code, it compiles with no error but if I use a filter it does not look like the example from the internet, except for blurring.

SDL_Surface *applyfilter(double *filter,int w, int h, SDL_Surface *image)
{
 SDL_Surface *ret=SDL_CreateRGBSurface(SDL_SWSURFACE,image->w,image->h,24,0,0,0,0);
 SDL_LockSurface(ret);
 for(int y=0;y<image->h;y++)
 {
  for(int x=0;x<image->w;x++)
  {
   int used=0,c=0,d=0;
   double sumr=0,sumg=0,sumb=0;
   for(int b=y-((h-1)/2);;b++)
   {
    for(int a=x-((w-1)/2);;a++)
    {
     if((a>0 && a<image->w)&&(b>0 && b<image->h))
     {
      used++;
      Uint8 red,green,blue;
      SDL_GetRGB(GetPixel(image,a,b),image->format,&red,&green,&blue);
      sumr+=(int)(red)*filter[c+(d*w)];
      sumg+=(int)(green)*filter[c+(d*w)];
      sumb+=(int)(blue)*filter[c+(d*w)];
     }
     c++;
     if(c==w)
      break;
    }
    c=0;
    d++;
    if(d==h)
      break;
   }
   sumr/=used/(w*h); 
   sumg/=used/(w*h);
   sumb/=used/(w*h);
   SetPixel(ret,x,y,(int)(sumr+0.5),(int)(sumg+0.5),(int)(sumb+0.5));
  }
 }
 SDL_UnlockSurface(ret);
 return ret;
}


Microsoft: “You’ve got questions. We’ve got dancing paperclips.” – unknown
Advertisement
What example from the internet?
What filter isn't working?
Do you set w and h for the filter correctly?
Any screen shots of the errors?
Width and height are set propperly. The emboss filter doesn't work:
[-0.5 0 0]
[ 0 1 0]
[ 0 0 0]

And there isn't any error, it's just not an emboss effect, just the image with a really tine hint of emboss and some random colour pixels in it, not like this:
Microsoft: “You’ve got questions. We’ve got dancing paperclips.” – unknown
That filter kernel you posted doesn't look like an emboss filter. Try using something like this:

[0 -1 -2]
[1 1 -1]
[2 1 0]
For some reason everyone's filters look different, but that one doesn't give me an emboss efect either, I get this:

Microsoft: “You’ve got questions. We’ve got dancing paperclips.” – unknown
Fixed it (mostly). The RGB values sometimes exceeded 255 or went below zero, I placed some limitation to it and now it gives some sweet effects, ty anyways.
Microsoft: “You’ve got questions. We’ve got dancing paperclips.” – unknown

This topic is closed to new replies.

Advertisement