Coverage Report - com.buckosoft.PicMan.business.mosaic.MosaicManDevelopment
 
Classes in this File Line Coverage Branch Coverage Complexity
MosaicManDevelopment
0%
0/81
0%
0/24
3.5
 
 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  0
 public class MosaicManDevelopment {
 34  
         private static final boolean DEBUG = true;
 35  0
         protected final Log logger = LogFactory.getLog(getClass());
 36  
         
 37  
         private        PicManFacade        pmf;
 38  
 
 39  0
         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  0
                 this.pmf = pmf;
 46  0
         }
 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  0
                 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  0
                 Thumbnail tn = pmf.getThumbNail(pic, height);
 65  0
                 BufferedImage bi = tn.getImage();
 66  0
                 BufferedImage nbi = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_BGR);
 67  0
                 Graphics2D g = nbi.createGraphics();
 68  0
                 int width = bi.getWidth();
 69  0
                 for (int xm=0; xm<depth; xm++) {
 70  0
                         for (int ym=0; ym<depth; ym++) {
 71  0
                                 long sumr = 0, sumg=0, sumb=0;
 72  0
                                 int        count = 0;
 73  0
                                 for (int x=xm*width/depth; x<(xm+1)*width/depth; x++) {
 74  
                                         //if (DEBUG)
 75  
                                         //        logger.info("x=" + x);
 76  0
                                         for (int y=ym*height/depth; y<(ym+1)*height/depth; y++) {
 77  
                                         //        if (DEBUG)
 78  
                                         //                logger.info("  y=" + y);
 79  0
                                                 int rgb = bi.getRGB(x, y);
 80  0
                                                 sumb += rgb & 0xff;
 81  0
                                                 sumg += rgb >> 8 & 0xff;
 82  0
                                                 sumr += rgb >> 16 & 0xff;
 83  0
                                                 count++;
 84  
                                         }
 85  
                                 }
 86  0
                                 int _r = (int)((double)sumr/(double)count);
 87  0
                                 int _g = (int)((double)sumg/(double)count);
 88  0
                                 int _b = (int)((double)sumb/(double)count);
 89  0
                                 g.setColor(new Color(_r, _g, _b));
 90  0
                                 int _x = xm*width/depth;
 91  0
                                 int _y = ym*height/depth;
 92  0
                                 int _w = (xm+1)*width/depth-_x;
 93  0
                                 int _h = (ym+1)*height/depth-_y;
 94  
 //                                if (DEBUG)
 95  
 //                                        logger.info(" d = " + depth + " x/y = " + _x + "/" + _y + "  w/h = " + _w + "/" + _h);
 96  0
                                 g.fillRect(_x, _y, _w, _h);
 97  
                         }
 98  
                 }
 99  0
                 tn.setImage(nbi);
 100  0
                 return(tn);
 101  
         }
 102  
 
 103  
         public MosaicVector        getMosaicVector(Pic pic) {
 104  0
                 final int height = 300;                // let's deal with the thumbs as 300 high for now
 105  0
                 MosaicVector mv = new MosaicVector();
 106  0
                 mv.pid = pic.getPid();
 107  
                 Thumbnail tn;
 108  
                 BufferedImage bi;
 109  
                 try {
 110  0
                         tn = pmf.getThumbNail(pic, height);
 111  0
                         bi = tn.getImage();
 112  0
                 } catch (Exception e) {
 113  0
                         return(null);
 114  0
                 }
 115  
                 //int width = bi.getWidth();
 116  0
                 int rgb = calculateVector(bi, 1, 0, 0);
 117  0
                 mv.rgb = rgb;
 118  
                 int x;
 119  
                 int        y;
 120  0
                 for (x=0; x<2; x++) {
 121  0
                         for (y=0; y<2; y++) {
 122  0
                                 mv.rgb2[x][y] = calculateVector(bi, 2, x, y);
 123  
                         }
 124  
                 }
 125  0
                 for (x=0; x<3; x++) {
 126  0
                         for (y=0; y<3; y++) {
 127  0
                                 mv.rgb3[x][y] = calculateVector(bi, 3, x, y);
 128  
                         }
 129  
                 }
 130  0
                 return(mv);
 131  
         }
 132  
         
 133  
         private        int        calculateVector(BufferedImage bi, int depth, int xm, int ym) {
 134  0
                 long sumr = 0, sumg=0, sumb=0;
 135  0
                 int        count = 0;
 136  0
                 int width = bi.getWidth();
 137  0
                 int height = bi.getHeight();
 138  0
                 for (int x=xm*width/depth; x<(xm+1)*width/depth; x++) {
 139  
                         //if (DEBUG)
 140  
                         //        logger.info("x=" + x);
 141  0
                         for (int y=ym*height/depth; y<(ym+1)*height/depth; y++) {
 142  
                         //        if (DEBUG)
 143  
                         //                logger.info("  y=" + y);
 144  0
                                 int rgb = bi.getRGB(x, y);
 145  0
                                 sumb += rgb & 0xff;
 146  0
                                 sumg += rgb >> 8 & 0xff;
 147  0
                                 sumr += rgb >> 16 & 0xff;
 148  0
                                 count++;
 149  
                         }
 150  
                 }
 151  0
                 int _r = (int)((double)sumr/(double)count);
 152  0
                 int _g = (int)((double)sumg/(double)count);
 153  0
                 int _b = (int)((double)sumb/(double)count);
 154  0
                 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  0
                         logger.info("runVectors()");
 162  0
                 picProcessing = 0;
 163  0
                 List<Pic> pics = pmf.getDB().getPics();
 164  0
                 JobLogEntry jle = new JobLogEntry();
 165  0
                 jle.setType(JobLogEntry.MOSAICV);
 166  0
                 pmf.addJobToLog(jle);
 167  0
                 Iterator<Pic> iter = pics.iterator();
 168  0
                 while (iter.hasNext()) {
 169  0
                         Pic pic = iter.next();
 170  0
                         MosaicVector mv = getMosaicVector(pic);
 171  0
                         if (mv != null)
 172  0
                                 pmf.getDB().updateMosaicVectors(mv);
 173  0
                         picProcessing++;
 174  0
                 }
 175  0
                 jle.setEndTime(new Date());
 176  
                 
 177  0
         }
 178  
 
 179  
 }