package htm.input.image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import javax.imageio.ImageIO;

/**
 *
 * @author david.charubini
 */
public class ImageUtil {

    public static class FastConvexHull {

        public ArrayList<PixelInput> execute(ArrayList<PixelInput> points) {
            ArrayList<PixelInput> xSorted = (ArrayList<PixelInput>) points.clone();
            Collections.sort(xSorted, new XCompare());

            int n = xSorted.size();

            PixelInput[] lUpper = new PixelInput[n];

            lUpper[0] = xSorted.get(0);
            lUpper[1] = xSorted.get(1);

            int lUpperSize = 2;

            for (int i = 2; i < n; i++) {
                lUpper[lUpperSize] = xSorted.get(i);
                lUpperSize++;

                while (lUpperSize > 2 && !rightTurn(lUpper[lUpperSize - 3], lUpper[lUpperSize - 2], lUpper[lUpperSize - 1])) {
                    // Remove the middle point of the three last
                    lUpper[lUpperSize - 2] = lUpper[lUpperSize - 1];
                    lUpperSize--;
                }
            }

            PixelInput[] lLower = new PixelInput[n];

            lLower[0] = xSorted.get(n - 1);
            lLower[1] = xSorted.get(n - 2);

            int lLowerSize = 2;

            for (int i = n - 3; i >= 0; i--) {
                lLower[lLowerSize] = xSorted.get(i);
                lLowerSize++;

                while (lLowerSize > 2 && !rightTurn(lLower[lLowerSize - 3], lLower[lLowerSize - 2], lLower[lLowerSize - 1])) {
                    // Remove the middle point of the three last
                    lLower[lLowerSize - 2] = lLower[lLowerSize - 1];
                    lLowerSize--;
                }
            }

            ArrayList<PixelInput> result = new ArrayList<PixelInput>();

            for (int i = 0; i < lUpperSize; i++) {
                result.add(lUpper[i]);
            }

            for (int i = 1; i < lLowerSize - 1; i++) {
                result.add(lLower[i]);
            }

            return result;
        }

        private boolean rightTurn(PixelInput a, PixelInput b, PixelInput c) {
            return (b.getX() - a.getX()) * (c.getY() - a.getY()) - (b.getY() - a.getY()) * (c.getX() - a.getX()) > 0;
        }

        private class XCompare implements Comparator<PixelInput> {

            @Override
            public int compare(PixelInput o1, PixelInput o2) {
                return (new Integer(o1.getX())).compareTo(new Integer(o2.getX()));
            }
        }
    }

    public static void writeImage(int width, int height, Collection<PixelInput> pixels, String filepath) throws IOException {

        BufferedImage img = new BufferedImage(width + 1, height + 1, BufferedImage.TYPE_INT_BGR);

        for (int y = 0; y < img.getHeight(); y++) {
            for (int x = 0; x < img.getWidth(); x++) {
                img.setRGB(x, y, 0xFFFFFFFF);
            }
        }
        
//        ArrayList<PixelInput> hulled = new FastConvexHull().execute(new ArrayList<PixelInput>(pixels));
        
        for (PixelInput pixel : pixels) {
            int[] value = pixel.getValue();
            int rgb = ImageInputSet.getValue(value);
            try {
                img.setRGB(pixel.getX(), pixel.getY(), rgb);
            } catch (RuntimeException re) {
                re.printStackTrace();
            }
        }
        
        File outputfile = new File(filepath);
        ImageIO.write(img, "png", outputfile);
    }
}

@@ -126,2552 +126,113 @@
 til.
-ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.Collections;%0Aimport java.util.Comparator;%0Aimport javax.imageio.ImageIO;%0A%0A/**%0A *%0A * @author david.charubini%0A */%0Apublic class ImageUtil %7B%0A%0A    public static class FastConvexHull %7B%0A%0A        public ArrayList%3CPixelInput%3E execute(ArrayList%3CPixelInput%3E points) %7B%0A            ArrayList%3CPixelInput%3E xSorted = (ArrayList%3CPixelInput%3E) points.clone();%0A            Collections.sort(xSorted, new XCompare());%0A%0A            int n = xSorted.size();%0A%0A            PixelInput%5B%5D lUpper = new PixelInput%5Bn%5D;%0A%0A            lUpper%5B0%5D = xSorted.get(0);%0A            lUpper%5B1%5D = xSorted.get(1);%0A%0A            int lUpperSize = 2;%0A%0A            for (int i = 2; i %3C n; i++) %7B%0A                lUpper%5BlUpperSize%5D = xSorted.get(i);%0A                lUpperSize++;%0A%0A                while (lUpperSize %3E 2 && !rightTurn(lUpper%5BlUpperSize - 3%5D, lUpper%5BlUpperSize - 2%5D, lUpper%5BlUpperSize - 1%5D)) %7B%0A                    // Remove the middle point of the three last%0A                    lUpper%5BlUpperSize - 2%5D = lUpper%5BlUpperSize - 1%5D;%0A                    lUpperSize--;%0A                %7D%0A            %7D%0A%0A            PixelInput%5B%5D lLower = new PixelInput%5Bn%5D;%0A%0A            lLower%5B0%5D = xSorted.get(n - 1);%0A            lLower%5B1%5D = xSorted.get(n - 2);%0A%0A            int lLowerSize = 2;%0A%0A            for (int i = n - 3; i %3E= 0; i--) %7B%0A                lLower%5BlLowerSize%5D = xSorted.get(i);%0A                lLowerSize++;%0A%0A                while (lLowerSize %3E 2 && !rightTurn(lLower%5BlLowerSize - 3%5D, lLower%5BlLowerSize - 2%5D, lLower%5BlLowerSize - 1%5D)) %7B%0A                    // Remove the middle point of the three last%0A                    lLower%5BlLowerSize - 2%5D = lLower%5BlLowerSize - 1%5D;%0A                    lLowerSize--;%0A                %7D%0A            %7D%0A%0A            ArrayList%3CPixelInput%3E result = new ArrayList%3CPixelInput%3E();%0A%0A            for (int i = 0; i %3C lUpperSize; i++) %7B%0A                result.add(lUpper%5Bi%5D);%0A            %7D%0A%0A            for (int i = 1; i %3C lLowerSize - 1; i++) %7B%0A                result.add(lLower%5Bi%5D);%0A            %7D%0A%0A            return result;%0A        %7D%0A%0A        private boolean rightTurn(PixelInput a, PixelInput b, PixelInput c) %7B%0A            return (b.getX() - a.getX()) * (c.getY() - a.getY()) - (b.getY() - a.getY()) * (c.getX() - a.getX()) %3E 0;%0A        %7D%0A%0A        private class XCompare implements Comparator%3CPixelInput%3E %7B%0A%0A            @Override%0A            public int compare(PixelInput o1, PixelInput o2) %7B%0A                return (new Integer(o1.getX())).compareTo(new Integer(o2.getX()));%0A            %7D%0A        %7D%0A    %7D
+Collection;%0Aimport javax.imageio.ImageIO;%0A%0A/**%0A *%0A * @author david.charubini%0A */%0Apublic class ImageUtil %7B
 %0A%0A  

remove some random convex hull code - where'd that come from?