Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Trying to pixelate a section of an image  (Read 1813 times)
0 Members and 1 Guest are viewing this topic.
Offline Porlus

Junior Member





« Posted 2013-04-18 18:38:12 »

Hi all, I've been looking around for a premade algorithm to pixelate an image or something similar that I could expand on although I've only been able to find descriptions of the algorithm so I've attempted to make it. What it does is loops through every pixel in a BufferedImage, loaded from a 16-bit PNG image file (byte for each component) and it divides the current X and Y components by the square size . From here it performs a further nested for loop to total the pixel colours in the block that this pixel is in.

It then divides by the block size, which is represented by the "pp" field, except it's squared because it samples pixels in a square along the X and Y axes. According to a few explanations I've read this should give me the average pixel colour for this block and every pixel in this block should be coloured in it. The algorithm I've written works somewhat in that the general shape of the image is preserved after the pixelation with far less detail, as is expected. The problem is that the colours are completely wrong and I'll show you how in the image below:

http://i.imgur.com/Ezfp4W8.png

Here there's just a picture of a hamster I found. The algorithm magnifies the area by a sort of scale factor.

Also here's the algorithm:

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

Can anyone spot why the colours aren't averaging out right?

Thanks

Paul
Offline SimonH
« Reply #1 - Posted 2013-04-18 19:14:15 »

You can't average colours like that, you have to break them into RGB;
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
int red;
int green;
int blue;

// loop over pp field of (numDots pixels)

  red+=(pixels[i]>>16)&0xff;
  green+=(pixels[i]>>8)&0xff;
  blue+=(pixels[i])&0xff;

// end loop

int averageColour=((red/numDots)<<16)|((green/numDots)<<8)|(blue/numDots);

// loop over pp field

  pixels[i]=averageColour;

// end loop


Edit: On reflection, it might be better just to use the first pixel of the pp field to colour the whole field - faster too!

People make games and games make people
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #2 - Posted 2013-04-18 19:14:50 »

You cant just add ints and then devide, it works for numbers, but it really messes up colors.
this way 0x000100 / 2 becomes  0x000080, so very black-green becomes blue.
i guess you should separate the colors, and count these up or something.
Im sure there is some kind of blending algorithm.

*dahm, ninjad Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Porlus

Junior Member





« Reply #3 - Posted 2013-04-18 19:24:31 »

Thanks for both your replies. Smiley I've substituted the single colour divides and used the components and it works great. I was just also wondering whether the magnification of the image is expected or whether I've glossed over another detail. :p Normally when I see pixelation the area of the image that's been pixelated is to scale with the rest of the image.
Offline Porlus

Junior Member





« Reply #4 - Posted 2013-04-18 21:04:04 »

I've sorted it now. Previously I'd made a magnification method and I think I borrowed too much logic from it. The working solution just loops through the outer for loops in steps the size of the block size. Then an inner set of nested for loops samples each of the pixels in that block, finds the average colour and then loops through them setting each to the colour of the average.
It's not the best solution but I'll post it here in case anyone needs a starting point.

http://www.java-gaming.org/?action=pastebin&id=545
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.

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

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

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

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

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (73 views)
2014-09-10 13:57:51

TehJavaDev (96 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29
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!