Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Image Blurring (Bitwise) problem  (Read 613 times)
0 Members and 1 Guest are viewing this topic.
Offline Saiqal

Senior Newbie





« Posted 2013-06-14 13:21:57 »

Hi everybody  Grin
I'm not really new to the forum but that is the first time I'm posting something.
I have been working on a little project inspired by "Zen'a photo garden",and I need to blur the image in that project. So I decided to learn how to do blurring. I've searched for the algorithm and I think I understand it. However I have a problem with the color channels. Here is my code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
public void blur(BufferedImage image) {
      //Note: the BufferedImage is of the type "INT_RGB"
     
      // set pixels
     int[] pixelData = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
      //Second array so pixels don't get overwritten while still needing them
     int[] pixels = pixelData.clone();
     
      //main loop over pixels
     for (int n = 0; n < pixelData.length; n++) {
         //Separate channels
        int r = 0, g = 0, b = 0;

         //loops to get neighbor pixels in the x direction
        for (int i = -1; i < 1; i++) {
            //discarding exceptions for the first and last pixels
           try {
               r += (pixelData[n + i] >> 16) & 0xff0000;
               g += (pixelData[n + i] >> 8) & 0x00ff00;
               b += (pixelData[n + i] >> 0) & 0x0000ff;
            } catch (ArrayIndexOutOfBoundsException e) {
               
            }
         }
         
         //averaging the colors of the pixels
        r /= (3);
         g /= (3);
         b /= (3);
         
         //final pixel
        pixels[n] = r | g | b;
      }
     
      //writing pixel data to the image
     image.setRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
   }


This is the method that I call to blur the image. As you can see (and I can't  Wink ) there is a problem with it. It outputs different colors even when I'm using black and white. I believe the mistake is in the bitwise operations. I've tried various methids to get it to work but with no avail  Sad .

If someone could explain to me what is wrong with my code and how can I solve it that would be great  Grin

PS: I know that this method only takes neighboring pixels in the x direction, and I'm planning to make it take circular shaped neighboring. But that is just for demonstration.
Offline davedes
« Reply #1 - Posted 2013-06-14 14:04:54 »

For CPU blurring I would recommend Romain Guy's box blur algorithm. It's simple and fast enough, and also adjustable for different strengths.

Example:
http://www.java-gaming.org/?action=pastebin&id=612

For GPU blurring look here:
https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson5

Offline EgonOlsen
« Reply #2 - Posted 2013-06-14 15:01:01 »

Your extraction of the color components as well as the composition of the final pixel are both wrong. They should look like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
for (int i = -1; i < 1; i++) {
            //discarding exceptions for the first and last pixels
           try {
               int pix=pixelData[n + i];
               r += (pix >> 16) & 255;
               g += (pix >> 8) & 255;
               b += pix & 255;
            } catch (ArrayIndexOutOfBoundsException e) {
               
            }
         }
         
         //averaging the colors of the pixels
        r /= (3);
         g /= (3);
         b /= (3);
         
         //final pixel
        pixels[n] = (r<<16) | (g<<8) | b;

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Saiqal

Senior Newbie





« Reply #3 - Posted 2013-06-14 15:14:35 »

My god ! It works  Cheesy
Thank you so much. However, what did we really do ? Why did we shift them first to the right and then to the left ? Why isn't it the same ? I'm guessing that the shifting was just for the masking "&" to work correctly (?). Also, why did we mask with "0xff = 255" on all the channels ? I mean why didn't we mask green with "0x00ff00" etc. ? Moreover the image kept getting darker when I blurred it, but that is something I think I can fix by myself. I know that's alot of question but bear with me  Roll Eyes
Offline EgonOlsen
« Reply #4 - Posted 2013-06-14 15:28:41 »

You could as well mask them without shifting, but that makes clamping and working with the different color components much more complicated, because you would have to shift your other values instead (for example, if you want to add 128 to red, you would have to add 128<<16 instead). I prefer to shift them down to 8bit, so that all have the same range of values. That makes mixing them much easier too and it allows you to use the same algorithms on all components.

In your case, you had a mix of both methods, which doesn't work. Shifting red to the right by 16 and then applying the mask leaves you with a black color.

Offline Saiqal

Senior Newbie





« Reply #5 - Posted 2013-06-14 15:35:34 »

Oh I finally understand  Shocked
Thank you mate . That was very helpful  Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (44 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (32 views)
2014-09-21 01:30:30

moogie (40 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!