package de.jeha.photo.mosaic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * @author jenshadlich@googlemail.com
 */
public class PhotoMosaic {

    private static final Logger LOG = LoggerFactory.getLogger(PhotoMosaic.class);

    private static final int TILE_WIDTH = 40;
    private static final int TILE_HEIGHT = 30;

    // TODO: collect cmd line arguments with args4j
    private String targetFile;
    private String inputDirectory;
    private boolean recursive = false;

    public static void main(String... args) throws IOException {
        System.setProperty("java.awt.headless", "true");

        final int tileWidth = TILE_WIDTH;
        final int tileHeight = TILE_HEIGHT;

        //BufferedImage image = ImageIO.read(new File("src/test/resources/IMG_20150320_142153.jpg"));
        BufferedImage image = ImageIO.read(new File("src/test/resources/sprd_logo_small.jpg"));
        LOG.info("h={}, w={}", image.getHeight(), image.getWidth());

        for (int x = 0; x < image.getWidth() - tileWidth; x += tileWidth) {
            for (int y = 0; y < image.getHeight() - tileHeight; y += tileHeight) {
                LOG.debug("tile x={}, y={}", x, y);
                BufferedImage tile = image.getSubimage(x, y, tileWidth, tileHeight);
                ColorCalculator.RGBA averageColor = ColorCalculator.averageColor(tile);
                LOG.debug("tile average color r={}, g={}, b={}",
                        (int) averageColor.getR(), (int) averageColor.getG(), (int) averageColor.getB());
            }
        }
    }

}

@@ -149,16 +149,88 @@
 dImage;%0A
+import java.awt.image.ColorModel;%0Aimport java.awt.image.WritableRaster;%0A
 import j
@@ -898,18 +898,16 @@
         
-//
 Buffered
@@ -904,36 +904,37 @@
   BufferedImage 
-imag
+sourc
 e = ImageIO.read
@@ -991,32 +991,34 @@
 jpg%22));%0A        
+//
 BufferedImage im
@@ -1015,20 +1015,21 @@
 edImage 
-imag
+sourc
 e = Imag
@@ -1112,56 +1112,525 @@
 fo(%22
-h=%7B%7D, w=%7B%7D%22, image.getHeight(), image.getWidth()
+source image h=%7B%7D, w=%7B%7D%22, source.getHeight(), source.getWidth());%0A%0A        // TODO: process the input directory to collect tiles and their average colors for the target image%0A%0A        final int correctedWidth = (source.getWidth() / tileWidth) * tileWidth;%0A        final int correctedHeight = (source.getHeight() / tileHeight) * tileHeight;%0A        LOG.info(%22target image h=%7B%7D, w=%7B%7D%22, correctedHeight, correctedWidth);%0A%0A        BufferedImage target = copyImage(source).getSubimage(0, 0, correctedWidth, correctedHeight
 );%0A%0A
@@ -1657,20 +1657,21 @@
  0; x %3C 
-imag
+sourc
 e.getWid
@@ -1738,20 +1738,21 @@
  0; y %3C 
-imag
+sourc
 e.getHei
@@ -1806,29 +1806,29 @@
         LOG.
-debug
+trace
 (%22tile x=%7B%7D,
@@ -1879,20 +1879,21 @@
  tile = 
-imag
+sourc
 e.getSub
@@ -2040,13 +2040,13 @@
 LOG.
-debug
+trace
 (%22ti
@@ -2203,19 +2203,1098 @@
     
-%7D%0A        %7D
+    WritableRaster writableRaster = target.getWritableTile(x, y);%0A%0A                // TODO: find an image that matches the average color best%0A%0A                // just write the average color (= rasterization) for debug%0A                double array%5B%5D = new double%5B4%5D;%0A                array%5B0%5D = averageColor.getR();%0A                array%5B1%5D = averageColor.getG();%0A                array%5B2%5D = averageColor.getB();%0A                array%5B3%5D = 0.0;%0A%0A                for (int i = x; i %3C x + tileWidth; i++) %7B%0A                    for (int j = y; j %3C y + tileHeight; j++) %7B%0A                        writableRaster.setPixel(i, j, array);%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A%0A        LOG.debug(%22Writing rasterized target image%22);%0A        ImageIO.write(target, %22png%22, new File(%22target/target_debug_rasterized.png%22));%0A    %7D%0A%0A    private static BufferedImage copyImage(BufferedImage image) %7B%0A        final ColorModel cm = image.getColorModel();%0A        WritableRaster raster = image.copyData(null);%0A        return new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
 %0A   

write raster for debugging