View Javadoc
1   /******************************************************************************
2    * MosaicAnalysePicController.java - A Spring controller for Mosaic Analysis, dumps Pic bits
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2008 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.PicMan.web;
9   
10  import java.awt.image.BufferedImage;
11  import java.io.IOException;
12  import java.util.Iterator;
13  
14  import javax.imageio.ImageIO;
15  import javax.imageio.ImageWriter;
16  import javax.imageio.stream.ImageOutputStream;
17  import javax.servlet.ServletException;
18  import javax.servlet.ServletOutputStream;
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.springframework.web.servlet.ModelAndView;
25  
26  import com.buckosoft.BSAccount.web.BSAccountPageController;
27  import com.buckosoft.PicMan.business.PicManFacade;
28  import com.buckosoft.PicMan.db.DatabaseFacade;
29  import com.buckosoft.PicMan.domain.Mosaic;
30  import com.buckosoft.PicMan.domain.Pic;
31  
32  /** The Spring controller to display some analysis of a Mosaic
33   * @author Dick Balaska
34   * @since 2008/06/02
35   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/web/MosaicAnalysePicController.java">MosaicAnalysePicController.java</a>
36   */
37  public class MosaicAnalysePicController extends BSAccountPageController {
38  //	private final static boolean DEBUG = true;
39  	protected final Log logger = LogFactory.getLog(getClass());
40  	
41  	private PicManFacade pmf;
42  	private DatabaseFacade dbf;
43  	
44  	/** Set the reference to the PicMan API.
45  	 * @param pmf The PicManFacade
46  	 */
47  	public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
48  //	public PicManFacade getPicMan() { return(pmf); }
49  
50  	/** Set the reference to the PicMan Database
51  	 * @param dbf The DatabaseFacade
52  	 */
53  	public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
54  //	public DatabaseFacade getDatabase() { return(dbf); }
55  
56  	private	Pic				masterPic = null;
57  	private	int				masterMid = 0;
58  	private	BufferedImage	bi = null;
59  
60  	/** Output a fragment of the Mosaic Master as a jpg. <br>
61  	 * url parameters: <br>
62  	 * x= the x coordinate of the master <br>
63  	 * y= the y coordinate of the master <br>
64  	 * w= the width to output 
65  	 * h= the height to output
66  	 * mid= the Mosaic ID.  Tells us which Master to use. 
67  	 */
68  	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
69  			throws ServletException, IOException {
70  
71  		String x_ = request.getParameter("x");
72  		String y_ = request.getParameter("y");
73  		String w_ = request.getParameter("w");
74  		String h_ = request.getParameter("h");
75  		String mid_ = request.getParameter("mid");
76  
77  		int	x = Integer.parseInt(x_);
78  		int	y = Integer.parseInt(y_);
79  		int	w = Integer.parseInt(w_);
80  		int	h = Integer.parseInt(h_);
81  		int	mid = Integer.parseInt(mid_);
82  
83  		if (mid != masterMid || bi == null) {
84  			Mosaic mosaic = dbf.getMosaic(mid);
85  			masterMid = mid;
86  			if (masterPic == null || !mosaic.getMasterPic().equals(masterPic.getName())) {
87  				masterPic = dbf.getPic(mosaic.getMasterPic());
88  				bi = pmf.getPicReader().readPic(masterPic);
89  			}
90  		}
91  		if (x + w > bi.getWidth())
92  			w = bi.getWidth() - x;
93  		if (y + h > bi.getHeight())
94  			h = bi.getHeight() - y;
95  		BufferedImage tile = null;
96  		try {
97  			tile = bi.getSubimage(x, y, w, h);
98  		} catch (Exception e) {
99  			String s = "Failed to create subimage: x/y = " + x + "/" + y + " w/h = " + w + "/" + h;
100 			Exception e1 = new Exception(s, e);
101 			logger.error(e1);
102 			pmf.addError(e1);
103 			return(null);
104 		}
105 		response.setContentType("image/jpeg");
106 		ServletOutputStream sos = response.getOutputStream();
107 		ImageOutputStream ios = ImageIO.createImageOutputStream(sos);
108 		Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
109 		ImageWriter writer = (ImageWriter)writers.next();
110 		writer.setOutput(ios);
111 		writer.write(tile);
112 		return(null);
113 	}
114 
115 }