Pixel reordering is wrong when trying to process and display image copy with lower res - 8 bit style pixelation
I'm currently making an application using processing intended to take an image and apply 8bit style processing to it: that is to make it look pixelated. To do this it has a method that take a style and window size as parameters (style is the shape in which the window is to be displayed - rect, ellipse, cross etc, and window size is a number between 1-10 squared) - to produce results similar to the iphone app pxl (http://itunes.apple.com/us/app/pxl./id499620829?mt=8 ). This method then counts through the image's pixels, window by window averages the colour of the window and displays a rect(or which every shape/style chosen) at the equivalent space on the other side of the sketch window (the sketch when run is supposed to display the original image on the left mirror it with the processed version on the right).
The problem Im having is when drawing the averaged colour rects, the order in which they display becomes skewed..
Although the results are rather amusing, they are not what I want. This has been bugging me for a while now as I can't see where in my code im offsetting the coordinates so they display like this. I know its probably something very trivial but I can seem to work it out. If anyone can spot why this skewed reordering is happening i would be much obliged as i have quite a lot of other ideas i want to implement and this is holding me back (I have also asked this on stackoverflow but this is the processing forum so I realised i was more likely to get an answer here :)). The sRGB class is essentially color() so you can replace with that to get it working (although if youd like that and the other colour space classes XYZ and LAB i can post but they aren't really used within the code). Anyway PLEASE Help me its a rather annoying bug...
Thanks,
Here the code:
- //=========================================================\\
- //=========================================================\\
- // 8BITIMG \\
- //=========================================================\\
- // An Image Processing application for 8Bit Stylisation \\
- //=========================================================\\
- //=========================================================\\
- // Copyright (C) 2012 Pierre Chanquion \\
- // pierre.chanquion@gmail.com \\
- //=========================================================\\
- //=========================================================\\
- // This program is free software: you can \\
- // redistribute it and/or modify it under the terms \\
- // of the GNU General Public License as published by \\
- // the Free Software Foundation, either version 3 of \\
- // the License, or (at your option) any later \\
- // version. \\
- // \\
- // This program is distributed in the hope that it \\
- // will be useful, but WITHOUT ANY WARRANTY; without \\
- // even the implied warranty of MERCHANTABILITY or \\
- // FITNESS FOR A PARTICULAR PURPOSE. See the GNU \\
- // General Public License for more details. \\
- //=========================================================\\
- // You should have received a copy of the GNU General \\
- // Public License along with this program. If not, \\
- // see <http://www.gnu.org/licenses/>. \\
- //=========================================================\\
- //=========================================================\\
- // ControlP5 Library used for GUI objects
- import controlP5.*;
- //=========================================================
- // GLOBAL VARIABLES
- //=========================================================
- PImage img;
- public int avR, avG, avB;
- private final int BLOCKS = 0, DOTS = 1, VERTICAL_CROSSES = 2, HORIZONTAL_CROSSES = 3;
- public sRGB styleColour;
- //=========================================================
- // METHODS FOR AVERAGING WINDOW COLOURS, CREATING AN
- // 8 BIT REPRESENTATION OF THE IMAGE AND LOADING AN
- // IMAGE
- //=========================================================
- public sRGB averageWindowColour(color [] c){
- // RGB Variables
- float r = 0;
- float g = 0;
- float b = 0;
- // Iterator
- int i = 0;
- int sizeOfWindow = c.length;
- // Count through the window's pixels, store the
- // red, green and blue values in the RGB variables
- // and sum them into the average variables
- for(i = 0; i < c.length; i++){
- r = red (c[i]);
- g = green(c[i]);
- b = blue (c[i]);
- avR += r;
- avG += g;
- avB += b;
- }
- // Divide the sum of the red, green and blue
- // values by the number of pixels in the window
- // to obtain the average
- avR = avR / sizeOfWindow;
- avG = avG / sizeOfWindow;
- avB = avB / sizeOfWindow;
- // Return the colour
- return new sRGB(avR,avG,avB);
- }
- public void eightBitIT(int style, int windowSize){
- img.loadPixels();
- for(int wx = 0; wx < img.width; wx += (sqrt(windowSize))){
- for(int wy = 0; wy < img.height; wy += (sqrt(windowSize))){
- color [] tempCols = new color[windowSize];
- int i = 0;
- for(int x = 0; x < (sqrt(windowSize)); x ++){
- for(int y = 0; y < (sqrt(windowSize)); y ++){
- int loc = (wx+x) + (y+wy)*(img.width-windowSize);
- tempCols[i] = img.pixels[loc];
- // println("Window loc X: "+(wx+(img.width+5))+" Window loc Y: "+(wy+5)+" Window pix X: "+x+" Window Pix Y: "+y);
- i++;
- }
- }
- styleColour = new sRGB(averageWindowColour(tempCols));
- // println("R: "+ red(styleColour.returnColourScaled())+" G: "+green(styleColour.returnColourScaled())+" B: "+blue(styleColour.returnColourScaled()));
- ellipseMode(CORNER);
- noStroke();
- fill(styleColour.returnColourScaled());
- // println("Rect Loc X: "+(wx+(img.width+5))+" Y: "+(wy+5));
- ellipse(wx+(img.width+5),wy+5,sqrt(windowSize),sqrt(windowSize));
- }
- }
- }
- public PImage load(String s){
- PImage temp = loadImage(s);
- temp.resize(600,470);
- return temp;
- }
- void setup(){
- background(0);
- img = loadImage("nomadindia.png");
- size(img.width*2+15,(img.height+10));
- frameRate(1);
- image(img,5,5);
- strokeWeight(5);
- stroke(255);
- rectMode(CORNERS);
- noFill();
- rect(2.5,2.5,img.width+3,height-3);
- rect(img.width+2.5,2.5,width-3,height-3);
- stroke(255,0,0);
- strokeWeight(1);
- rect(5,5,9,9);
- eightBitIT(BLOCKS, 4);
- }
- void draw(){
- // eightBitIT(BLOCKS, 4);
- //println("X: "+mouseX+" Y: "+mouseY);
- }