Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / provider / fusion / BufferListFusion.java @ 2438

History | View | Annotate | Download (7.38 KB)

1
package org.gvsig.raster.impl.provider.fusion;
2

    
3
import java.awt.geom.Rectangle2D;
4
import java.util.List;
5

    
6
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
7
import org.gvsig.fmap.dal.coverage.datastruct.Extent;
8
import org.gvsig.raster.impl.datastruct.ExtentImpl;
9

    
10
/** 
11
 * Buffer composed by a list of buffers and its extents. The buffers are not tiles and it can
12
 * be superposed among them. The number of pixel of each buffer can be different too. 
13
 * 
14
 * @author Nacho Brodin (nachobrodin@gmail.com)
15
 */
16
public class BufferListFusion {
17
        //private Logger           log          = LoggerFactory.getLogger(MemoryMatrixBuffer.class);
18
        public static int            FIRST                     = 0;
19
        public static int            LAST                      = 0;
20
        public static int            MIXED                     = 0;
21
        public static int            MINIMUM                   = 0;
22
        public static int            MAXIMUM                   = 0;
23
        public static int            AVERAGE                   = 0;
24
        
25
        public static int            COLOR_STATISTICS          = 0;
26
        public static int            COLOR_HISTOGRAM           = 0;
27
        public static int            COLOR_LINEAR_CORRELATION  = 0;
28
        
29
        private List<Buffer>         bufferList                = null;
30
        private List<Extent>         bboxList                  = null;
31
        private PixelSquareStructure pxSquare                  = null;
32
        private AbstractFusionMethod fusion                    = null;
33
        private int                  alphaBandNumber           = -1;
34
        
35
        public BufferListFusion(
36
                        List<Buffer> bufferList,
37
                        PixelSquareStructure pxSquare,
38
                        int fusionMethod) {
39
                this.bufferList = bufferList;
40
                if(pxSquare.bbox == null) {
41
                        Extent bbox = getBoundingBox();
42
                        pxSquare.bbox = bbox;
43
                }
44
                this.pxSquare = pxSquare;
45
                
46
                if(fusionMethod == FIRST) {
47
                        fusion = new FirstFusionMethod(bufferList, pxSquare);
48
                }
49
        }
50
        
51
        /**
52
         * Gets a entire window from buffers
53
         * @param ext
54
         * @param buf
55
         * @return
56
         */
57
        public Buffer getWindow() {
58
                if(pxSquare.dataType == Buffer.TYPE_BYTE) {
59
                        for (int row = 0; row < pxSquare.buffer.getHeight(); row++) {
60
                                for (int col = 0; col < pxSquare.buffer.getWidth(); col++) {
61
                                        boolean hasData = false;
62
                                        
63
                                        for (int band = 0; band < fusion.getBandCount(); band++) {
64
                                                Byte value = fusion.getByteValue(row, col, band);
65
                                                if(value != null) {
66
                                                        pxSquare.buffer.setElem(row, col, band, value.byteValue());
67
                                                        hasData = true;
68
                                                        value = null;
69
                                                }
70
                                        }
71
                                        
72
                                        for (int band = fusion.getBandCount(); band < pxSquare.buffer.getBandCount(); band++) {
73
                                                pxSquare.buffer.setElem(row, col, band, (byte)255);        
74
                                        }
75
                                        
76
                                        if(alphaBandNumber != -1 && alphaBandNumber < pxSquare.buffer.getBandCount())
77
                                                if(hasData) //No data has been written
78
                                                        pxSquare.buffer.setElem(row, col, alphaBandNumber, (byte)255);
79
                                                else
80
                                                        pxSquare.buffer.setElem(row, col, alphaBandNumber, (byte)0);
81
                                }        
82
                        }
83
                }
84

    
85
                if(pxSquare.dataType == Buffer.TYPE_FLOAT) {
86
                        for (int row = 0; row < pxSquare.buffer.getHeight(); row++) {
87
                                for (int col = 0; col < pxSquare.buffer.getWidth(); col++) {
88
                                        for (int band = 0; band < fusion.getBandCount(); band++) {
89
                                                pxSquare.buffer.setElem(row, col, band, fusion.getFloatValue(row, col, band));
90
                                        }
91
                                        for (int band = fusion.getBandCount(); band < pxSquare.buffer.getBandCount(); band++) {
92
                                                pxSquare.buffer.setElem(row, col, band, 0F);        
93
                                        }
94
                                }        
95
                        }
96
                }
97
                return pxSquare.buffer;
98
        }
99
        
100
        /**
101
         * Sets a band to NODATA value 
102
         * @param bufResult
103
         * @param tileExtent
104
         * @param buf
105
         * @param ex
106
         * @param pixelSize
107
         */
108
        protected void clearMaskBuffer(Buffer buf) {
109
                if(buf.getDataType() == Buffer.TYPE_BYTE) {
110
                        for (int i = 0; i < buf.getHeight(); i++) {
111
                                for (int j = 0; j < buf.getWidth(); j++) {
112
                                        for (int iBand = 0; iBand < buf.getBandCount(); iBand++) {
113
                                                buf.setElem(i, j, iBand, pxSquare.nodata.getValue().byteValue());                                                
114
                                        }
115
                                }
116
                        }
117
                        return;
118
                }
119
                if(buf.getDataType() == Buffer.TYPE_SHORT) {
120
                        for (int i = 0; i < buf.getHeight(); i++) {
121
                                for (int j = 0; j < buf.getWidth(); j++) {
122
                                        for (int iBand = 0; iBand < buf.getBandCount(); iBand++) {
123
                                                buf.setElem(i, j, iBand, pxSquare.nodata.getValue().shortValue());                                                
124
                                        }
125
                                }
126
                        }
127
                        return;
128
                }
129
                if(buf.getDataType() == Buffer.TYPE_INT) {
130
                        for (int i = 0; i < buf.getHeight(); i++) {
131
                                for (int j = 0; j < buf.getWidth(); j++) {
132
                                        for (int iBand = 0; iBand < buf.getBandCount(); iBand++) {
133
                                                buf.setElem(i, j, iBand, pxSquare.nodata.getValue().intValue());                                                
134
                                        }
135
                                }
136
                        }
137
                        return;
138
                }
139
                if(buf.getDataType() == Buffer.TYPE_FLOAT) {
140
                        for (int i = 0; i < buf.getHeight(); i++) {
141
                                for (int j = 0; j < buf.getWidth(); j++) {
142
                                        for (int iBand = 0; iBand < buf.getBandCount(); iBand++) {
143
                                                buf.setElem(i, j, iBand, pxSquare.nodata.getValue().floatValue());                                                
144
                                        }
145
                                }
146
                        }
147
                        return;
148
                }
149
                if(buf.getDataType() == Buffer.TYPE_DOUBLE) {
150
                        for (int i = 0; i < buf.getHeight(); i++) {
151
                                for (int j = 0; j < buf.getWidth(); j++) {
152
                                        for (int iBand = 0; iBand < buf.getBandCount(); iBand++) {
153
                                                buf.setElem(i, j, iBand, pxSquare.nodata.getValue().doubleValue());                                                
154
                                        }
155
                                }
156
                        }
157
                        return;
158
                }
159
        }
160
        
161
        private Extent getBoundingBox() {
162
                double minX = Double.MAX_VALUE;
163
                double minY = Double.MAX_VALUE;
164
                double maxX = 0;
165
                double maxY = 0;
166
                if(bboxList != null) {
167
                        for (int i = 0; i < bboxList.size(); i++) {
168
                                Extent e = bboxList.get(i);
169
                                if(e.getMin().getX() < minX)
170
                                        minX = e.getMin().getX();
171
                                if(e.getMin().getY() < minY)
172
                                        minY = e.getMin().getY();
173
                                if(e.getMax().getX() > maxX)
174
                                        maxX = e.getMax().getX();
175
                                if(e.getMax().getY() > maxY)
176
                                        maxY = e.getMax().getY();
177
                        }
178
                } else {
179
                        for (int i = 0; i < bufferList.size(); i++) {
180
                                Rectangle2D e = bufferList.get(i).getDataExtent();
181
                                if(e.getX() < minX)
182
                                        minX = e.getX();
183
                                if((e.getY() - e.getHeight()) < minY)
184
                                        minY = (e.getY() - e.getHeight());
185
                                if((e.getX() + e.getWidth()) > maxX)
186
                                        maxX = (e.getX() + e.getWidth());
187
                                if(e.getY() > maxY)
188
                                        maxY = e.getY();
189
                        }
190
                }
191
                return new ExtentImpl(minX, minY, maxX, maxY);
192
        }
193
        
194
        public void dispose() {
195
                for (int i = 0; i < bufferList.size(); i++) {
196
                        bufferList.get(i).dispose();
197
                }
198
                bufferList.clear();
199
        }
200

    
201
        public void setAlphaBand(int alphaBandNumber) {
202
                this.alphaBandNumber = alphaBandNumber;
203
        }
204
        
205
        /*public void setNoDataValue(int datatype) {
206
                NoData noData = RasterLocator.getManager().getDataStructFactory().createDefaultNoData(bandCount, datatype);
207
                switch (datatype) {
208
                case Buffer.TYPE_BYTE:
209
                        byteNoData = noData.getValue().byteValue();
210
                        break;
211
                case Buffer.TYPE_SHORT:
212
                        shortNoData = noData.getValue().shortValue();
213
                        break;
214
                case Buffer.TYPE_INT:
215
                        intNoData = noData.getValue().intValue();
216
                        break;
217
                case Buffer.TYPE_FLOAT:
218
                        floatNoData = noData.getValue().floatValue();
219
                        break;
220
                case Buffer.TYPE_DOUBLE:
221
                        doubleNoData = ((NoData)noData).getValue().doubleValue();
222
                        break;
223
                }
224
        }
225
        
226
        private Extent getIntersectionBoundingBox(Extent bbox) {
227
                Extent intersection = bbox;
228
                if(bboxList != null) {
229
                        for (int i = 0; i < bboxList.size(); i++) {
230
                                Extent e = bboxList.get(i);
231
                                intersection = intersection.intersection(e);
232
                        }
233
                } else {
234
                        for (int i = 0; i < bufferList.size(); i++) {
235
                                Rectangle2D r = bufferList.get(i).getDataExtent();
236
                                Extent e = new ExtentImpl(r.getMinX(), r.getMinY(), r.getMaxX(), r.getMaxY());
237
                                intersection = intersection.intersection(e);
238
                        }
239
                }
240
                return intersection;
241
        }*/
242
        
243
        
244
}