1
2
3
4
5
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
27
28
29
30
31
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
42
43
44 public void setPicMan(PicManFacade pmf) {
45 this.pmf = pmf;
46 }
47
48
49
50
51
52 public int getPicProcessing() {
53 return picProcessing;
54 }
55
56
57
58
59
60
61
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
75
76 for (int y=ym*height/depth; y<(ym+1)*height/depth; y++) {
77
78
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
95
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;
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
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
140
141 for (int y=ym*height/depth; y<(ym+1)*height/depth; y++) {
142
143
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
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 }