Coverage Report - com.buckosoft.PicMan.web.MosaicAnalysePicController
 
Classes in this File Line Coverage Branch Coverage Complexity
MosaicAnalysePicController
0%
0/44
0%
0/12
3.667
 
 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  0
 public class MosaicAnalysePicController extends BSAccountPageController {
 38  
 //        private final static boolean DEBUG = true;
 39  0
         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  0
         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  0
         public void setDatabase(DatabaseFacade dbf) { this.dbf = dbf; }
 54  
 //        public DatabaseFacade getDatabase() { return(dbf); }
 55  
 
 56  0
         private        Pic                                masterPic = null;
 57  0
         private        int                                masterMid = 0;
 58  0
         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  0
                 String x_ = request.getParameter("x");
 72  0
                 String y_ = request.getParameter("y");
 73  0
                 String w_ = request.getParameter("w");
 74  0
                 String h_ = request.getParameter("h");
 75  0
                 String mid_ = request.getParameter("mid");
 76  
 
 77  0
                 int        x = Integer.parseInt(x_);
 78  0
                 int        y = Integer.parseInt(y_);
 79  0
                 int        w = Integer.parseInt(w_);
 80  0
                 int        h = Integer.parseInt(h_);
 81  0
                 int        mid = Integer.parseInt(mid_);
 82  
 
 83  0
                 if (mid != masterMid || bi == null) {
 84  0
                         Mosaic mosaic = dbf.getMosaic(mid);
 85  0
                         masterMid = mid;
 86  0
                         if (masterPic == null || !mosaic.getMasterPic().equals(masterPic.getName())) {
 87  0
                                 masterPic = dbf.getPic(mosaic.getMasterPic());
 88  0
                                 bi = pmf.getPicReader().readPic(masterPic);
 89  
                         }
 90  
                 }
 91  0
                 if (x + w > bi.getWidth())
 92  0
                         w = bi.getWidth() - x;
 93  0
                 if (y + h > bi.getHeight())
 94  0
                         h = bi.getHeight() - y;
 95  0
                 BufferedImage tile = null;
 96  
                 try {
 97  0
                         tile = bi.getSubimage(x, y, w, h);
 98  0
                 } catch (Exception e) {
 99  0
                         String s = "Failed to create subimage: x/y = " + x + "/" + y + " w/h = " + w + "/" + h;
 100  0
                         Exception e1 = new Exception(s, e);
 101  0
                         logger.error(e1);
 102  0
                         pmf.addError(e1);
 103  0
                         return(null);
 104  0
                 }
 105  0
                 response.setContentType("image/jpeg");
 106  0
                 ServletOutputStream sos = response.getOutputStream();
 107  0
                 ImageOutputStream ios = ImageIO.createImageOutputStream(sos);
 108  0
                 Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
 109  0
                 ImageWriter writer = (ImageWriter)writers.next();
 110  0
                 writer.setOutput(ios);
 111  0
                 writer.write(tile);
 112  0
                 return(null);
 113  
         }
 114  
 
 115  
 }