Wednesday, 18 November 2015

Messy code which was fun to write

Some bits this week

cmd quser says when the user logged on, handy for when the boss asks why I am packing up so early, because I got here early :) 

Found what looks like a good book, The Icarus Deception: How High Will You Fly?

rainforestQA awesome to have that many testers on standby. 

Good pod cast, software engineering daily.

At the moment I like the idea of getting a laptop with a Linux on to play with even if it is dual boot.

So here is a mess but it works, trying to write a program that turns an image into lego blocks. 

 package com.company;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;

public class LegoPictureMaker {
    public String filePath;
    public BufferedImage img;
    public int legoSquareSideLength;

    public LegoPictureMaker(String filePath) {
        this.legoSquareSideLength = 16;
        this.filePath = filePath;
    }

    public void makeImage() {
        System.out.println("Grabbing file...");
        img = grabFile();
        System.out.println("Processing lego blocks");
        processFile();
        System.out.println("Done!");
    }

    //process file
    private void processFile() {

        //work out of squares needed in width and height
        int xTiles = img.getRaster().getWidth() / legoSquareSideLength;
        int yTiles = img.getRaster().getHeight() / legoSquareSideLength;

        //build image I can write to
        BufferedImage buildImage;
        buildImage = new BufferedImage(
                (xTiles * legoSquareSideLength),
                (yTiles * legoSquareSideLength),
                 BufferedImage.TYPE_INT_RGB);

        int totalTiles = xTiles * yTiles;
        int red= 0, green = 0, blue = 0;
        int[] testArray = new int[3];
        img.getRaster().getPixel(0,0, testArray);

        //make something usable
        WritableRaster writableRaster = (WritableRaster)buildImage.getData();
        int[][][] averageColour = new int[xTiles][yTiles][3];
        for(int x = 0; x < xTiles; x++) {
            for (int y = 0; y < yTiles; y++) {
                averageColour[x][y] = getAverageColour(x * legoSquareSideLength, y * legoSquareSideLength);
                writeTileColour(writableRaster, x * legoSquareSideLength, y * legoSquareSideLength, averageColour[x][y]);
            }
        }
        saveImage(buildImage, writableRaster);
    }

    private void writeTileColour(WritableRaster writableRaster, int xPoint, int yPoint, int[] rgbValues) {
        int endX = xPoint + legoSquareSideLength;
        int endY = yPoint + legoSquareSideLength;
        for (int x = xPoint; x < endX; x++) {
            for (int y = yPoint; y < endY; y++) {
                writableRaster.setPixel(x, y, rgbValues);
            }
        }
    }

    private int[] getAverageColour(int xPoint, int yPoint) {
        int red= 0, green = 0, blue = 0;
        int endX = xPoint + legoSquareSideLength;
        int endY = yPoint + legoSquareSideLength;
        int[] testArray = new int[3];;

        for (int i = xPoint; i < endX; i++) {
            for (int j = yPoint; j < endY; j++) {
                img.getRaster().getPixel(i,j, testArray);
                red += testArray[0];
                green += testArray[1];
                blue += testArray[2];
            }
        }
        int amountOfPixels = legoSquareSideLength * legoSquareSideLength;
        return new int[] {  red / amountOfPixels,
                            green / amountOfPixels,
                            blue / amountOfPixels};
    }

    //save image
    public void saveImage(BufferedImage buildImage, WritableRaster writableRaster) {
        //save and close file
        buildImage.setData(writableRaster);
        File outputFile = new File("testest.png");
        try {
            ImageIO.write(buildImage, "png", outputFile);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    //get the file
    public BufferedImage grabFile() {
        BufferedImage img = null;

        if (filePath != null && !filePath.isEmpty()) {
            try {
                img = ImageIO.read(new File(this.filePath));
            } catch (IOException ex) {
                System.out.println("broke here");
                ex.printStackTrace();
            }
        }
        return img;
    }
}

No comments:

Post a Comment