View Javadoc
1   /******************************************************************************
2    * MosaicManDevelopment.java - Development methods to work out mosaic building algorithms
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2007 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.PicMan.business.mosaic;
9   
10  import java.awt.Color;
11  import java.awt.Graphics2D;
12  import java.awt.image.BufferedImage;
13  import java.util.Date;
14  import java.util.Iterator;
15  import java.util.List;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  
20  import com.buckosoft.PicMan.business.PicManFacade;
21  import com.buckosoft.PicMan.domain.JobLogEntry;
22  import com.buckosoft.PicMan.domain.Pic;
23  import com.buckosoft.PicMan.domain.Thumbnail;
24  import com.buckosoft.PicMan.domain.mosaic.MosaicVector;
25  
26  /** Mosaic test/development methods to work out mosaic building algorithms. <br>
27   * These are obsolete and could probably be deleted, but who knows... <br>
28   * These were hacked out of MosaicMan to clean him up so that he just does building managing.
29   * @author Dick Balaska
30   * @since 2008/07/01
31   * @see <a href="http://cvs.buckosoft.com/Projects/PicMan/PicMan/src/main/java/com/buckosoft/PicMan/business/mosaic/MosaicManDevelopment.java">MosaicManDevelopment.java</a>
32   */
33  public class MosaicManDevelopment {
34  	private static final boolean DEBUG = true;
35  	protected final Log logger = LogFactory.getLog(getClass());
36  	
37  	private	PicManFacade	pmf;
38  
39  	private	int picProcessing = 0;
40  
41  	/** Set the reference to the PicMan API.
42  	 * @param pmf The PicManFacade
43  	 */
44  	public	void setPicMan(PicManFacade pmf) {
45  		this.pmf = pmf;
46  	}
47  
48  	/** Which ordinal picture are we working on?
49  	 * This is just a status indicator
50  	 * @return the picProcessing
51  	 */
52  	public int getPicProcessing() {
53  		return picProcessing;
54  	}
55  
56  
57  	/** A Test/Development method that returns a thumbnail that is a mosaic of the pic.
58  	 * @param pic The Pic to make a mosaic thumb of
59  	 * @param height  The height of the resulting Thumbnail
60  	 * @param depth The number of divisions in the resulting Thumbnail. '3' will make a 3x3 mosaic.
61  	 * @return The mosaic'd thumbnail
62  	 */
63  	public Thumbnail	getMosaicThumbNail(Pic pic, int height, int depth) {
64  		Thumbnail tn = pmf.getThumbNail(pic, height);
65  		BufferedImage bi = tn.getImage();
66  		BufferedImage nbi = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_BGR);
67  		Graphics2D g = nbi.createGraphics();
68  		int width = bi.getWidth();
69  		for (int xm=0; xm<depth; xm++) {
70  			for (int ym=0; ym<depth; ym++) {
71  				long sumr = 0, sumg=0, sumb=0;
72  				int	count = 0;
73  				for (int x=xm*width/depth; x<(xm+1)*width/depth; x++) {
74  					//if (DEBUG)
75  					//	logger.info("x=" + x);
76  					for (int y=ym*height/depth; y<(ym+1)*height/depth; y++) {
77  					//	if (DEBUG)
78  					//		logger.info("  y=" + y);
79  						int rgb = bi.getRGB(x, y);
80  						sumb += rgb & 0xff;
81  						sumg += rgb >> 8 & 0xff;
82  						sumr += rgb >> 16 & 0xff;
83  						count++;
84  					}
85  				}
86  				int _r = (int)((double)sumr/(double)count);
87  				int _g = (int)((double)sumg/(double)count);
88  				int _b = (int)((double)sumb/(double)count);
89  				g.setColor(new Color(_r, _g, _b));
90  				int _x = xm*width/depth;
91  				int _y = ym*height/depth;
92  				int _w = (xm+1)*width/depth-_x;
93  				int _h = (ym+1)*height/depth-_y;
94  //				if (DEBUG)
95  //					logger.info(" d = " + depth + " x/y = " + _x + "/" + _y + "  w/h = " + _w + "/" + _h);
96  				g.fillRect(_x, _y, _w, _h);
97  			}
98  		}
99  		tn.setImage(nbi);
100 		return(tn);
101 	}
102 
103 	public MosaicVector	getMosaicVector(Pic pic) {
104 		final int height = 300;		// let's deal with the thumbs as 300 high for now
105 		MosaicVector mv = new MosaicVector();
106 		mv.pid = pic.getPid();
107 		Thumbnail tn;
108 		BufferedImage bi;
109 		try {
110 			tn = pmf.getThumbNail(pic, height);
111 			bi = tn.getImage();
112 		} catch (Exception e) {
113 			return(null);
114 		}
115 		//int width = bi.getWidth();
116 		int rgb = calculateVector(bi, 1, 0, 0);
117 		mv.rgb = rgb;
118 		int x;
119 		int	y;
120 		for (x=0; x<2; x++) {
121 			for (y=0; y<2; y++) {
122 				mv.rgb2[x][y] = calculateVector(bi, 2, x, y);
123 			}
124 		}
125 		for (x=0; x<3; x++) {
126 			for (y=0; y<3; y++) {
127 				mv.rgb3[x][y] = calculateVector(bi, 3, x, y);
128 			}
129 		}
130 		return(mv);
131 	}
132 	
133 	private	int	calculateVector(BufferedImage bi, int depth, int xm, int ym) {
134 		long sumr = 0, sumg=0, sumb=0;
135 		int	count = 0;
136 		int width = bi.getWidth();
137 		int height = bi.getHeight();
138 		for (int x=xm*width/depth; x<(xm+1)*width/depth; x++) {
139 			//if (DEBUG)
140 			//	logger.info("x=" + x);
141 			for (int y=ym*height/depth; y<(ym+1)*height/depth; y++) {
142 			//	if (DEBUG)
143 			//		logger.info("  y=" + y);
144 				int rgb = bi.getRGB(x, y);
145 				sumb += rgb & 0xff;
146 				sumg += rgb >> 8 & 0xff;
147 				sumr += rgb >> 16 & 0xff;
148 				count++;
149 			}
150 		}
151 		int _r = (int)((double)sumr/(double)count);
152 		int _g = (int)((double)sumg/(double)count);
153 		int _b = (int)((double)sumb/(double)count);
154 		return(_r<<16 | _g<<8 | _b);
155 	}
156 
157 	/** Read every Pic and calculate the mosaic vectors for it
158 	 */
159 	public void runVectors() {
160 		if (DEBUG)
161 			logger.info("runVectors()");
162 		picProcessing = 0;
163 		List<Pic> pics = pmf.getDB().getPics();
164 		JobLogEntry jle = new JobLogEntry();
165 		jle.setType(JobLogEntry.MOSAICV);
166 		pmf.addJobToLog(jle);
167 		Iterator<Pic> iter = pics.iterator();
168 		while (iter.hasNext()) {
169 			Pic pic = iter.next();
170 			MosaicVector mv = getMosaicVector(pic);
171 			if (mv != null)
172 				pmf.getDB().updateMosaicVectors(mv);
173 			picProcessing++;
174 		}
175 		jle.setEndTime(new Date());
176 		
177 	}
178 
179 }