svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.library / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer.impl / src / main / java / org / gvsig / raster / lib / buffer / impl / DefaultBuffer.java @ 43803
History | View | Annotate | Download (6.73 KB)
1 |
package org.gvsig.raster.lib.buffer.impl; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.image.BufferedImage; |
5 |
import java.util.ArrayList; |
6 |
import java.util.List; |
7 |
|
8 |
import org.cresques.cts.IProjection; |
9 |
|
10 |
import org.gvsig.fmap.geom.Geometry; |
11 |
import org.gvsig.fmap.geom.GeometryLocator; |
12 |
import org.gvsig.fmap.geom.GeometryManager; |
13 |
import org.gvsig.fmap.geom.primitive.Envelope; |
14 |
import org.gvsig.raster.lib.buffer.api.Band; |
15 |
import org.gvsig.raster.lib.buffer.api.Buffer; |
16 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
17 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
18 |
import org.gvsig.raster.lib.buffer.api.NoData; |
19 |
import org.gvsig.raster.lib.buffer.api.PageManager; |
20 |
import org.gvsig.raster.lib.buffer.api.exceptions.BandException; |
21 |
import org.gvsig.raster.lib.buffer.api.exceptions.BufferException; |
22 |
import org.gvsig.raster.lib.buffer.impl.exceptions.CreateBufferException; |
23 |
import org.gvsig.tools.locator.LocatorException; |
24 |
|
25 |
import org.slf4j.Logger; |
26 |
import org.slf4j.LoggerFactory; |
27 |
|
28 |
/**
|
29 |
* @author fdiaz
|
30 |
*
|
31 |
*/
|
32 |
public class DefaultBuffer extends AbstractBuffer { |
33 |
|
34 |
protected static final Logger logger = LoggerFactory.getLogger(DefaultBuffer.class); |
35 |
|
36 |
/**
|
37 |
* Default constructor of buffer.
|
38 |
*
|
39 |
* @param rows
|
40 |
* Rows of buffer and buffer bands.
|
41 |
* @param columns
|
42 |
* Columns of buffer bands.
|
43 |
* @param bandDataTypes
|
44 |
* Type of buffer bands. The band types also indicates the number
|
45 |
* of buffer bands.
|
46 |
* @param bandNoData
|
47 |
* Band noData. If there are bans without NoData, bands will have
|
48 |
* undefined NoData value.
|
49 |
* @param projection
|
50 |
* Projection of buffer
|
51 |
* @param envelope
|
52 |
* Envelope of buffer
|
53 |
* @param pageManagers
|
54 |
* Page manager to paginate this buffer. If page manager is null,
|
55 |
* buffer will be loaded in memory.
|
56 |
* @throws CreateBufferException
|
57 |
* If there are some problems creating buffer.
|
58 |
*/
|
59 |
public DefaultBuffer(int rows, int columns, int[] bandDataTypes, NoData[] bandNoData, |
60 |
IProjection projection, Envelope envelope, List<PageManager> pageManagers)
|
61 |
throws CreateBufferException {
|
62 |
|
63 |
if (rows < 1) { |
64 |
try {
|
65 |
throw new IllegalArgumentException("Can't create buffer with rows = " + rows); |
66 |
} catch (IllegalArgumentException e) { |
67 |
logger.warn(e.getMessage(), e); |
68 |
throw e;
|
69 |
} |
70 |
} |
71 |
if (columns < 1) { |
72 |
try {
|
73 |
throw new IllegalArgumentException("Can't create buffer with columns = " + columns); |
74 |
} catch (IllegalArgumentException e) { |
75 |
logger.warn(e.getMessage(), e); |
76 |
throw e;
|
77 |
} |
78 |
} |
79 |
|
80 |
this.bands = new ArrayList<Band>(); |
81 |
this.projection = projection;
|
82 |
try {
|
83 |
if (envelope == null) { |
84 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
85 |
envelope = |
86 |
geometryManager.createEnvelope(0, 0, columns, rows, |
87 |
Geometry.SUBTYPES.GEOM2D); |
88 |
} |
89 |
this.dimensions = BufferLocator.getBufferManager().createBufferDimensions(rows, columns, envelope);
|
90 |
if (bandDataTypes != null) { |
91 |
createBands(bandDataTypes, bandNoData, pageManagers); |
92 |
} |
93 |
} catch (Exception e) { |
94 |
throw new CreateBufferException(e); |
95 |
} |
96 |
} |
97 |
|
98 |
private void createBands(int[] bandDataTypes, NoData[] bandNoData, |
99 |
List<PageManager> pageManagers) throws BandException { |
100 |
|
101 |
BufferManager bufferManager = BufferLocator.getBufferManager(); |
102 |
|
103 |
for (int i = 0; i < bandDataTypes.length; i++) { |
104 |
|
105 |
NoData noDataBand = null;
|
106 |
if (bandNoData != null && i < bandNoData.length) { |
107 |
noDataBand = bandNoData[i]; |
108 |
} |
109 |
|
110 |
PageManager pageManager = null;
|
111 |
if (pageManagers != null && i < pageManagers.size()) { |
112 |
pageManager = pageManagers.get(i); |
113 |
} |
114 |
|
115 |
Band band = |
116 |
bufferManager.createBand(bandDataTypes[i], this.getDimensions().getRows(), this.getDimensions().getColumns(), noDataBand, |
117 |
pageManager); |
118 |
bands.add(band); |
119 |
band.addObserver(this);
|
120 |
} |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public Object clone() throws CloneNotSupportedException { |
125 |
Buffer cloned;
|
126 |
try {
|
127 |
cloned = |
128 |
BufferLocator.getBufferManager().createBuffer(this.getRows(), this.getColumns(), |
129 |
this.getBandTypes(), this.getBandNoData(), this.getProjection(), this.dimensions.getEnvelope()); |
130 |
} catch (LocatorException | BufferException e) {
|
131 |
throw new CloneNotSupportedException("Can't clone buffer."); |
132 |
} |
133 |
for (int i = 0; i < this.getBandCount(); i++) { |
134 |
cloned.addBand((Band) this.getBand(i).clone());
|
135 |
} |
136 |
return cloned;
|
137 |
} |
138 |
|
139 |
public BufferedImage getBufferedImage() { |
140 |
int imageType;
|
141 |
int[] bandTypes = getBandTypes(); |
142 |
if(bandTypes.length<3 ||bandTypes.length>4){ |
143 |
return null; |
144 |
} |
145 |
int type = bandTypes[0]; |
146 |
if(type == BufferManager.TYPE_BYTE){
|
147 |
if(bandTypes.length==3){ |
148 |
imageType = BufferedImage.TYPE_3BYTE_BGR;
|
149 |
} else {
|
150 |
imageType = BufferedImage.TYPE_4BYTE_ABGR;
|
151 |
} |
152 |
// } else if(type == BufferManager.TYPE_INT) {
|
153 |
// if(bandTypes.length==3){
|
154 |
// imageType = BufferedImage.TYPE_INT_RGB;
|
155 |
// } else {
|
156 |
// imageType = BufferedImage.TYPE_INT_ARGB;
|
157 |
// }
|
158 |
} else {
|
159 |
return null; |
160 |
} |
161 |
|
162 |
BufferedImage img = new BufferedImage(getColumns(), getRows(), imageType); |
163 |
for (int row=0; row<this.getRows(); row++){ |
164 |
for (int column=0; column<this.getColumns(); column++){ |
165 |
Color color;
|
166 |
if(getBandCount()==4){ |
167 |
color = new Color( |
168 |
((Number)getBand(0).get(row, column)).intValue(), |
169 |
((Number)getBand(1).get(row, column)).intValue(), |
170 |
((Number)getBand(2).get(row, column)).intValue(), |
171 |
((Number)getBand(3).get(row, column)).intValue() |
172 |
); |
173 |
|
174 |
} else {
|
175 |
|
176 |
color = new Color( |
177 |
((Number)getBand(0).get(row, column)).intValue(), |
178 |
((Number)getBand(1).get(row, column)).intValue(), |
179 |
((Number)getBand(2).get(row, column)).intValue() |
180 |
); |
181 |
} |
182 |
img.setRGB(row, column, color.getRGB()); |
183 |
|
184 |
} |
185 |
} |
186 |
|
187 |
return img;
|
188 |
|
189 |
} |
190 |
|
191 |
} |