| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
package com.buckosoft.PicMan.business.mosaic.engine; |
| 9 | |
|
| 10 | |
import java.awt.Graphics2D; |
| 11 | |
import java.awt.image.BufferedImage; |
| 12 | |
import java.util.Arrays; |
| 13 | |
import java.util.List; |
| 14 | |
|
| 15 | |
import org.apache.commons.logging.Log; |
| 16 | |
import org.apache.commons.logging.LogFactory; |
| 17 | |
|
| 18 | |
import com.buckosoft.PicMan.business.mosaic.MosaicEngine; |
| 19 | |
import com.buckosoft.PicMan.domain.MosaicTile; |
| 20 | |
import com.buckosoft.PicMan.domain.Pic; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | 0 | public class SampleMaker extends MosaicEngine { |
| 29 | |
private static final boolean DEBUG = true; |
| 30 | 0 | private final Log logger = LogFactory.getLog(getClass()); |
| 31 | |
|
| 32 | 0 | private int mosaicId = 6; |
| 33 | 0 | private int curRow = 0; |
| 34 | |
|
| 35 | |
private int curCol; |
| 36 | |
private int maxRow; |
| 37 | |
private int maxCol; |
| 38 | |
private int p; |
| 39 | |
private List<Pic> picList; |
| 40 | 0 | private String info = "N/A"; |
| 41 | |
|
| 42 | |
private int tileRows; |
| 43 | 0 | private String buildStep = "Idle"; |
| 44 | 0 | private int pass = -1; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public String getStatus() { |
| 50 | 0 | StringBuilder sb = new StringBuilder(); |
| 51 | 0 | sb.append(buildStep); |
| 52 | 0 | sb.append(": pass="); |
| 53 | 0 | sb.append(pass); |
| 54 | 0 | if (!buildStep.equals("Calc")) { |
| 55 | 0 | sb.append(" curRow = "); |
| 56 | 0 | sb.append(curRow); |
| 57 | 0 | sb.append(" curCol = "); |
| 58 | 0 | sb.append(curCol); |
| 59 | |
} |
| 60 | 0 | sb.append(" p="); |
| 61 | 0 | sb.append(p); |
| 62 | 0 | return(sb.toString()); |
| 63 | |
} |
| 64 | |
|
| 65 | |
public String getInfo() { |
| 66 | 0 | return(info); |
| 67 | |
} |
| 68 | |
|
| 69 | 0 | private class PicWeight implements Comparable<PicWeight> { |
| 70 | |
public int index; |
| 71 | |
public long value; |
| 72 | |
|
| 73 | |
public int compareTo(PicWeight arg0) { |
| 74 | 0 | PicWeight pw = (PicWeight)arg0; |
| 75 | 0 | return((int)(this.value - pw.value)); |
| 76 | |
} |
| 77 | |
} |
| 78 | |
|
| 79 | |
protected boolean _build() { |
| 80 | 0 | Graphics2D gd = bi.createGraphics(); |
| 81 | |
|
| 82 | |
|
| 83 | 0 | picList = pmf.getDB().getPics(this.mosaicSet, 75); |
| 84 | |
|
| 85 | 0 | PicWeight[] pwArray = new PicWeight[picList.size()]; |
| 86 | |
|
| 87 | |
if (DEBUG) |
| 88 | 0 | logger.info("Working from " + picList.size() + " pics"); |
| 89 | 0 | maxRow = this.masterPic.getHeight(); |
| 90 | 0 | maxCol = this.masterPic.getWidth(); |
| 91 | 0 | tileRows = maxRow / tileHeight; |
| 92 | 0 | if (tileRows * tileHeight < maxRow) |
| 93 | 0 | tileRows++; |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | 0 | info = "tileRows=" + tileRows + " maxCol=" + maxCol + " pics=" + picList.size(); |
| 99 | |
if (DEBUG) |
| 100 | 0 | logger.info("tileRows=" + tileRows + " rowHeight=" + tileHeight); |
| 101 | 0 | boolean workDone = true; |
| 102 | 0 | while (workDone) { |
| 103 | 0 | pass++; |
| 104 | 0 | workDone = false; |
| 105 | |
BufferedImage mbi; |
| 106 | |
int x,y; |
| 107 | 0 | buildStep = "Calc"; |
| 108 | 0 | for (p=0; p<picList.size(); p++) { |
| 109 | 0 | pwArray[p] = new PicWeight(); |
| 110 | 0 | pwArray[p].index = p; |
| 111 | |
|
| 112 | 0 | mbi = pmf.getThumbNail(picList.get(p), tileHeight).getImage(); |
| 113 | 0 | for (x=0; x<mbi.getWidth(); x++) { |
| 114 | 0 | for (y=0; y<mbi.getHeight(); y++) { |
| 115 | 0 | int mpx = mbi.getRGB(x, y); |
| 116 | 0 | int ppx = 0; |
| 117 | 0 | pwArray[p].value += Math.abs(((mpx)&0xFF) - ((ppx)&0xFF)) * 30; |
| 118 | 0 | pwArray[p].value += Math.abs(((mpx>>8)&0xFF) - ((ppx>>8)&0xFF)) * 59; |
| 119 | 0 | pwArray[p].value += Math.abs(((mpx>>16)&0xFF) - ((ppx>>16)&0xFF)) * 11; |
| 120 | |
} |
| 121 | |
} |
| 122 | |
} |
| 123 | 0 | Arrays.sort(pwArray); |
| 124 | 0 | buildStep = "Draw"; |
| 125 | 0 | p = 0; |
| 126 | 0 | for (curRow=0; curRow<tileRows; curRow++) { |
| 127 | 0 | for (curCol=0; curCol<maxCol; ) { |
| 128 | 0 | int cr = curRow * tileHeight; |
| 129 | 0 | int curPic = pwArray[p].index; |
| 130 | 0 | mbi = pmf.getThumbNail(picList.get(curPic), tileHeight).getImage(); |
| 131 | 0 | if (!mosaic.isBatch()) { |
| 132 | 0 | MosaicTile tile = new MosaicTile(mosaicId, picList.get(curPic).getPid(), curCol, cr, mbi.getWidth(), mbi.getHeight()); |
| 133 | 0 | pmf.getDB().storeMosaicTile(tile); |
| 134 | |
} |
| 135 | 0 | gd.drawImage(mbi, null, curCol, cr); |
| 136 | 0 | curCol += mbi.getWidth(); |
| 137 | 0 | setLastMosaicUpdate(); |
| 138 | 0 | if (++p >= picList.size()) |
| 139 | 0 | p = 0; |
| 140 | 0 | } |
| 141 | |
} |
| 142 | |
} |
| 143 | 0 | buildStep = "Done"; |
| 144 | 0 | return(false); |
| 145 | |
} |
| 146 | |
|
| 147 | |
} |