Incorrect pixels bug


Hello SDL community , I am a very beginner with SDL and programming in general, trying to create a image processing software for my university assignment.
The reason for that I made this post is that I do get some weird results trying to create an Unsharp Mask effect. I will attach my code here and I will explain what it does and where the problems are (or at least , where I think they are).

So, as you can see, my function gets 2 arguments: the image source(from which pixel data will be extracted, and a target, where the image will be projected). I blur the original image, then i substract the RGB value of the blurred image from the source image to get “the mask” and then , i add the mask to the original image and that’s it. I added some clamping to make sure everything stays in the correct range and then I draw every pixel resulted on the output surface. All these surfaces have been converted in an SDL_PIXELFORMAT_ARGB8888 . The output surface is loaded into a texture (also SDL_PIXELFORMAT_ARGB8888) and rendered on the screen.

The results are pretty good in 90% of the image, I get the effect I want, however , there are some pixels that look weird in some places. I will also attach some results here(Original/Result):

I tried to fix this in any possible way I knew. I thought is a format problem and played with the pixel bit depth , but I couldn’t get to a good result. What i found is that all the values > 255 are negative values and I tried to make them completely white. And it works for the skies ,for example, but if you can see on my examples, the dark values, on the grass are also affected, which makes this not a good solution.

I also get this kind of wrong pixels when I want to add a contrast, and the values are really bright/dark.
In my opinion there may be a problem with the pixel format, but I’m not sure if that’s true.

Is there anyone that had this kind of problem before or knows a potential solution?
Thank you for your time and support !


I am not familiar with the algorithm but when you use unsigned integers to subtract, you can end with with large values. For example consider 50 - 100 = -50. Maybe it would be better to calculate mask using signed integer and clamp the result between [0-255], if that is how algo should work.

Also checking (value < 0) is unnecessary with unsigned integers.

I would also replace i with y and j with x, for readability :slight_smile:


@capehill, Thank you so much for your answer. I discovered earlier today that this was the problem. Being new to programming I didn’t really understand how this overflow works, but now I really understand it. Using your method the algorithm works perfectly! Moreover , thank you for the little tip in the end. I guess it would be easier to read if change the name of the variable to a x,y format.