View Javadoc
1   /******************************************************************************
2    * SampleMaker.java - Make a Mosaic that shows all of the pics available in the set
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.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  /** Build a mosaic that contains all of the pics in the set.  
23   * This ignores the masterPic, which is just used for sizing the final.
24   * @author Dick Balaska
25   * @since 2008/05/28
26   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/main/java/com/buckosoft/PicMan/business/mosaic/engine/SampleMaker.java">SampleMaker.java</a>
27   */
28  public class SampleMaker extends MosaicEngine {
29  	private static final boolean DEBUG = true;
30  	private final Log logger = LogFactory.getLog(getClass());
31  	
32  	private	int		mosaicId = 6;
33  	private	int		curRow = 0;
34  //	private	int[]	curCols;
35  	private	int		curCol;
36  	private	int		maxRow;
37  	private	int		maxCol;
38  	private	int		p;
39  	private	List<Pic>	picList;
40  	private	String	info = "N/A";
41  //	private	int[]	used;
42  	private	int		tileRows;
43  	private	String	buildStep = "Idle";
44  	private	int		pass = -1;
45  	
46  	/** Return our current build status
47  	 * @return The status
48  	 */
49  	public	String	getStatus() {
50  		StringBuilder sb = new StringBuilder();
51  		sb.append(buildStep);
52  		sb.append(": pass=");
53  		sb.append(pass);
54  		if (!buildStep.equals("Calc")) {
55  			sb.append(" curRow = ");
56  			sb.append(curRow);
57  			sb.append(" curCol = ");
58  			sb.append(curCol);
59  		}
60  		sb.append(" p=");
61  		sb.append(p);
62  		return(sb.toString());
63  	}
64  
65  	public String getInfo() {
66  		return(info);
67  	}
68  
69  	private class PicWeight implements Comparable<PicWeight> {
70  		public	int		index;
71  		public	long	value;
72  	
73  		public int compareTo(PicWeight arg0) {
74  			PicWeight pw = (PicWeight)arg0;
75  			return((int)(this.value - pw.value));
76  		}
77  	}
78  
79  	protected boolean _build() {
80  		Graphics2D	gd = bi.createGraphics();
81  		
82  		// 75, pull from this size set even though the size calc should be able to deal with, say, a 50 and figure it out.
83  		picList = pmf.getDB().getPics(this.mosaicSet, 75);
84  //		LinkedList<PicWeight> lpw = new LinkedList<PicWeight>();
85  		PicWeight[]	pwArray = new PicWeight[picList.size()];
86  		
87  		if (DEBUG)
88  			logger.info("Working from " + picList.size() + " pics");
89  		maxRow = this.masterPic.getHeight();
90  		maxCol = this.masterPic.getWidth();
91  		tileRows = maxRow / tileHeight;
92  		if (tileRows * tileHeight < maxRow)
93  			tileRows++;
94  //		curCols = new int[tileRows];
95  		
96  //		long[]	rate = new long[picList.size()];		// rate each pic for this location
97  //				used = new int[picList.size()];		// number of times each pic is used
98  		info = "tileRows=" + tileRows + " maxCol=" + maxCol + " pics=" + picList.size();
99  		if (DEBUG)
100 			logger.info("tileRows=" + tileRows + " rowHeight=" + tileHeight);
101 		boolean workDone = true;
102 		while (workDone) {
103 			pass++;
104 			workDone = false;
105 			BufferedImage mbi;
106 			int	x,y;
107 			buildStep = "Calc";
108 			for (p=0; p<picList.size(); p++) {
109 				pwArray[p] = new PicWeight();
110 				pwArray[p].index = p;
111 				
112 				mbi = pmf.getThumbNail(picList.get(p), tileHeight).getImage();
113 				for (x=0; x<mbi.getWidth(); x++) {
114 					for (y=0; y<mbi.getHeight(); y++) {
115 						int mpx = mbi.getRGB(x, y);
116 						int	ppx = 0;
117 						pwArray[p].value += Math.abs(((mpx)&0xFF)     - ((ppx)&0xFF))     * 30;
118 						pwArray[p].value += Math.abs(((mpx>>8)&0xFF)  - ((ppx>>8)&0xFF))  * 59;
119 						pwArray[p].value += Math.abs(((mpx>>16)&0xFF) - ((ppx>>16)&0xFF)) * 11;
120 					}
121 				}
122 			}
123 			Arrays.sort(pwArray);
124 			buildStep = "Draw";
125 			p = 0;
126 			for (curRow=0; curRow<tileRows; curRow++) {
127 				for (curCol=0; curCol<maxCol; ) {
128 					int	cr = curRow * tileHeight;
129 					int curPic = pwArray[p].index;
130 					mbi = pmf.getThumbNail(picList.get(curPic), tileHeight).getImage();
131 					if (!mosaic.isBatch()) {
132 						MosaicTile	tile = new MosaicTile(mosaicId, picList.get(curPic).getPid(), curCol, cr, mbi.getWidth(), mbi.getHeight());
133 						pmf.getDB().storeMosaicTile(tile);
134 					}
135 					gd.drawImage(mbi, null, curCol, cr);
136 					curCol += mbi.getWidth();
137 					setLastMosaicUpdate();
138 					if (++p >= picList.size())
139 						p = 0;
140 				}
141 			}
142 		}
143 		buildStep = "Done";
144 		return(false);
145 	}
146 
147 }