| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FirstRibbon |
|
| 6.2;6.2 |
| 1 | /****************************************************************************** | |
| 2 | * FirstRibbon.java - Make a Mosaic | |
| 3 | * | |
| 4 | * PicMan - The BuckoSoft Picture Manager in Java | |
| 5 | * Copyright(c) 2008 - Dick Balaska | |
| 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.Iterator; | |
| 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 | /** After 8 years, finally make a Mosaic. | |
| 23 | * This engine walks through each row of a Pic and places the best tile there. | |
| 24 | * "Best" is determined by subtracting the tile from the Pic and summing the difference in each pixel. | |
| 25 | * This is performed from each tile in the set, and the one with the least difference is chosen. | |
| 26 | * @author Dick Balaska | |
| 27 | * @since 2008/01/20 | |
| 28 | * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/main/java/com/buckosoft/PicMan/business/mosaic/engine/FirstRibbon.java">FirstRibbon.java</a> | |
| 29 | */ | |
| 30 | 0 | public class FirstRibbon extends MosaicEngine { |
| 31 | private static final boolean DEBUG = true; | |
| 32 | 0 | private final Log logger = LogFactory.getLog(getClass()); |
| 33 | ||
| 34 | 0 | private int curRow = 0; |
| 35 | 0 | private int curCol = 0; |
| 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 | private long[] used; | |
| 42 | ||
| 43 | /** Return our current build status | |
| 44 | * @return The status | |
| 45 | */ | |
| 46 | public String getStatus() { | |
| 47 | 0 | StringBuilder sb = new StringBuilder(); |
| 48 | 0 | sb.append("curRow = "); |
| 49 | 0 | sb.append(curRow); |
| 50 | 0 | sb.append(" curCol = "); |
| 51 | 0 | sb.append(curCol); |
| 52 | 0 | sb.append(" p="); |
| 53 | 0 | sb.append(p); |
| 54 | 0 | return(sb.toString()); |
| 55 | } | |
| 56 | ||
| 57 | public String getInfo() { | |
| 58 | 0 | return(info); |
| 59 | } | |
| 60 | ||
| 61 | protected boolean _build() { | |
| 62 | 0 | Graphics2D gd = bi.createGraphics(); |
| 63 | ||
| 64 | // 75, pull from this size set even though the size calc should be able to deal with, say, a 50 and figure it out. | |
| 65 | 0 | picList = pmf.getDB().getPics(this.mosaicSet, this.tileHeight); |
| 66 | if (DEBUG) | |
| 67 | 0 | logger.info("Working from " + picList.size() + " pics"); |
| 68 | 0 | maxRow = this.masterPic.getHeight(); |
| 69 | 0 | maxCol = this.masterPic.getWidth(); |
| 70 | //int rowHeight = maxRow / tileHeight; | |
| 71 | 0 | long[] rate = new long[picList.size()]; // rate each pic for this location |
| 72 | 0 | used = new long[picList.size()]; // number of times each pic is used |
| 73 | 0 | info = "maxRow=" + maxRow + " maxCol=" + maxCol + " pics=" + picList.size(); |
| 74 | if (DEBUG) | |
| 75 | 0 | logger.info("maxRow=" + maxRow + " rowHeight=" + tileHeight); |
| 76 | 0 | restoreBuild(gd); |
| 77 | 0 | for (; curRow<maxRow; curRow += tileHeight, curCol=0) { |
| 78 | 0 | for (; curCol<maxCol; ) { |
| 79 | int x,y; | |
| 80 | 0 | for (p=0; p<picList.size(); p++) { |
| 81 | 0 | BufferedImage mbi = pmf.getMosaicThumbNail(picList.get(p), tileHeight).getImage(); |
| 82 | //BufferedImage mbi = pmf.getThumbNail(null, tileHeight).getImage(); | |
| 83 | 0 | rate[p] = used[p]*1; |
| 84 | 0 | for (x=0; x<mbi.getWidth(); x++) { |
| 85 | 0 | for (y=0; y<mbi.getHeight(); y++) { |
| 86 | 0 | int mpx = mbi.getRGB(x, y); |
| 87 | int ppx; | |
| 88 | 0 | if (x+curCol >= bi.getWidth() || y+curRow >= bi.getHeight()) |
| 89 | 0 | ppx = mpx; |
| 90 | else | |
| 91 | 0 | ppx = bi.getRGB(x+curCol, y+ curRow); |
| 92 | // rate[p] += Math.abs(((mpx)&0xFF) - ((ppx)&0xFF)) * 30; | |
| 93 | // rate[p] += Math.abs(((mpx>>8)&0xFF) - ((ppx>>8)&0xFF)) * 59; | |
| 94 | // rate[p] += Math.abs(((mpx>>16)&0xFF) - ((ppx>>16)&0xFF)) * 11; | |
| 95 | 0 | rate[p] += Math.abs(((mpx)&0xFF) - ((ppx)&0xFF)) * 1; |
| 96 | 0 | rate[p] += Math.abs(((mpx>>8)&0xFF) - ((ppx>>8)&0xFF)) * 1; |
| 97 | 0 | rate[p] += Math.abs(((mpx>>16)&0xFF) - ((ppx>>16)&0xFF)) * 1; |
| 98 | } | |
| 99 | } | |
| 100 | // Portrait always wins over landscape because there is less pixels to go wrong. | |
| 101 | // Give equal weight per pixel column | |
| 102 | 0 | rate[p] /= mbi.getWidth(); |
| 103 | //logger.info("rate[" + p + "]=" + rate[p] + " ("+ picList.get(p).getName() + ")"); | |
| 104 | } | |
| 105 | 0 | long best = Long.MAX_VALUE; |
| 106 | 0 | int besti = 0; |
| 107 | 0 | int besti2 = 0; |
| 108 | 0 | for (x=0; x<rate.length; x++) { |
| 109 | 0 | if (rate[x] < best) { |
| 110 | 0 | besti2 = besti; |
| 111 | 0 | best = rate[x]; |
| 112 | 0 | besti = x; |
| 113 | } | |
| 114 | } | |
| 115 | if (DEBUG) { | |
| 116 | 0 | logger.info("besti = " + besti + " (" + picList.get(besti).getName() + ") = " + rate[besti]); |
| 117 | 0 | logger.info("besti2 = " + besti2 + " (" + picList.get(besti2).getName() + ") = " + rate[besti2]); |
| 118 | } | |
| 119 | 0 | used[besti]++; |
| 120 | 0 | BufferedImage mbi = pmf.getThumbNail(picList.get(besti), tileHeight).getImage(); |
| 121 | 0 | if (!mosaic.isBatch()) { |
| 122 | 0 | MosaicTile tile = new MosaicTile(this.mid, picList.get(besti).getPid(), curCol, curRow, mbi.getWidth(), mbi.getHeight()); |
| 123 | 0 | pmf.getDB().storeMosaicTile(tile); |
| 124 | } | |
| 125 | 0 | gd.drawImage(mbi, null, curCol, curRow); |
| 126 | 0 | curCol += mbi.getWidth(); |
| 127 | 0 | setLastMosaicUpdate(); |
| 128 | 0 | if (!pmf.getDB().getSystem().isEngineOn()) |
| 129 | 0 | return(true); |
| 130 | 0 | } |
| 131 | } | |
| 132 | 0 | return(false); |
| 133 | } | |
| 134 | ||
| 135 | private void restoreBuild(Graphics2D gd) { | |
| 136 | 0 | List<MosaicTile> lmt = pmf.getDB().getMosaicTiles(this.mid); |
| 137 | if (DEBUG) | |
| 138 | 0 | logger.info("Restoring " + lmt.size() + " tiles"); |
| 139 | 0 | for (; curRow<maxRow; curRow += tileHeight, curCol=0) { |
| 140 | 0 | for (; curCol<maxCol; ) { |
| 141 | 0 | boolean found = false; |
| 142 | 0 | Iterator<MosaicTile> iter = lmt.iterator(); |
| 143 | 0 | while (iter.hasNext()) { |
| 144 | 0 | MosaicTile mt = iter.next(); |
| 145 | 0 | if (mt.getX() == curCol && mt.getY() == curRow) { |
| 146 | 0 | int besti = getPicListIndex(mt.getPid()); |
| 147 | if (DEBUG) | |
| 148 | 0 | logger.info("restore: besti = " + besti + " (" + picList.get(besti).getName() + ")"); |
| 149 | 0 | used[besti]++; |
| 150 | 0 | BufferedImage mbi = pmf.getThumbNail(picList.get(besti), tileHeight).getImage(); |
| 151 | 0 | gd.drawImage(mbi, null, curCol, curRow); |
| 152 | 0 | curCol += mbi.getWidth(); |
| 153 | 0 | setLastMosaicUpdate(); |
| 154 | 0 | found = true; |
| 155 | 0 | break; |
| 156 | } | |
| 157 | 0 | } |
| 158 | 0 | if (!found) |
| 159 | 0 | return; |
| 160 | 0 | } |
| 161 | } | |
| 162 | 0 | } |
| 163 | ||
| 164 | private int getPicListIndex(int pid) { | |
| 165 | 0 | for (int i=0; i<picList.size(); i++) { |
| 166 | 0 | Pic p = picList.get(i); |
| 167 | 0 | if (p.getPid() == pid) |
| 168 | 0 | return(i); |
| 169 | } | |
| 170 | 0 | return(-1); |
| 171 | } | |
| 172 | } |