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 / MemoryBandDouble.java @ 43803
History | View | Annotate | Download (4.25 KB)
1 |
package org.gvsig.raster.lib.buffer.impl; |
---|---|
2 |
|
3 |
import java.util.Arrays; |
4 |
|
5 |
import org.gvsig.raster.lib.buffer.api.Band; |
6 |
import org.gvsig.raster.lib.buffer.api.Band.BandDouble; |
7 |
import org.gvsig.raster.lib.buffer.api.BandNotification; |
8 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
9 |
import org.gvsig.raster.lib.buffer.api.NoData; |
10 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
11 |
import org.gvsig.raster.lib.buffer.impl.exceptions.CopyFromBandException; |
12 |
|
13 |
/**
|
14 |
* @author fdiaz
|
15 |
*
|
16 |
*/
|
17 |
public class MemoryBandDouble extends AbstractBand implements BandDouble { |
18 |
|
19 |
double[] data; |
20 |
|
21 |
/**
|
22 |
* @param rows
|
23 |
* @param columns
|
24 |
*/
|
25 |
public MemoryBandDouble(int rows, int columns) { |
26 |
this(rows, columns, BufferLocator.getBufferManager().createNoData(null, null)); |
27 |
} |
28 |
|
29 |
/**
|
30 |
* @param rows
|
31 |
* @param columns
|
32 |
* @param noData
|
33 |
*/
|
34 |
public MemoryBandDouble(int rows, int columns, NoData noData) { |
35 |
this.rows = rows;
|
36 |
this.columns = columns;
|
37 |
data = new double[rows * columns]; |
38 |
if (noData == null) { |
39 |
this.noData = BufferLocator.getBufferManager().createNoData(null, null); |
40 |
} else {
|
41 |
this.noData = noData;
|
42 |
} |
43 |
} |
44 |
|
45 |
@Override
|
46 |
public int getDataType() { |
47 |
return BufferManager.TYPE_DOUBLE;
|
48 |
} |
49 |
|
50 |
@Override
|
51 |
public Object get(int row, int column) { |
52 |
return getValue(row, column);
|
53 |
} |
54 |
|
55 |
@Override
|
56 |
protected void doSet(int row, int column, Object value) { |
57 |
setValue(row, column, (double) value);
|
58 |
} |
59 |
|
60 |
@Override
|
61 |
protected void doFill(Object value) { |
62 |
value = nullValueToNoData(value); |
63 |
if(value == null){ |
64 |
value = 0;
|
65 |
} |
66 |
Arrays.fill(data, (double) value); |
67 |
} |
68 |
|
69 |
double[] getData() { |
70 |
return data;
|
71 |
} |
72 |
|
73 |
@Override
|
74 |
protected void doCopyFrom(Band source) throws CopyFromBandException { |
75 |
if (this.getColumns() == source.getColumns() && this.getRows() == source.getRows()){ |
76 |
if(source instanceof MemoryBandDouble) { |
77 |
System.arraycopy(((MemoryBandDouble) source).getData(), 0, this.data, 0, this.getRows() |
78 |
* this.getColumns());
|
79 |
} else {
|
80 |
Object rowBuffer = source.createRowBuffer();
|
81 |
for(int row=0; row<source.getRows(); row++){ |
82 |
source.fetchRow(row, rowBuffer);; |
83 |
try {
|
84 |
System.arraycopy(rowBuffer, 0, this.data, row*this.getColumns(), this.getColumns()); |
85 |
} catch (Exception e){ |
86 |
throw new CopyFromBandException(source, this, e); |
87 |
} |
88 |
} |
89 |
} |
90 |
} else {
|
91 |
throw new CopyFromBandException(source, this); |
92 |
} |
93 |
} |
94 |
|
95 |
@Override
|
96 |
protected void doCopyFrom(Band source, int row, int column) throws CopyFromBandException { |
97 |
if (this.getColumns() >= source.getColumns() + column && this.getRows() >= source.getRows() + row |
98 |
&& source instanceof MemoryBandDouble) {
|
99 |
for(int r=0; r<Math.min(this.getRows()-row, source.getRows()); r++){ |
100 |
System.arraycopy(((MemoryBandDouble) source).getData(), r*source.getColumns(), this.data, (r+row)*this.getColumns()+column, Math.min(this.getColumns()-column, source.getColumns())); |
101 |
} |
102 |
} else {
|
103 |
throw new CopyFromBandException(source, this); |
104 |
} |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public double getValue(int row, int column) { |
109 |
return data[row * getColumns() + column];
|
110 |
} |
111 |
|
112 |
@Override
|
113 |
public void setValue(int row, int column, double value) { |
114 |
doSetValue(row, column, value); |
115 |
notifyObservers(new DefaultBandNotification(BandNotification.SET, new Object[]{row, column, value})); |
116 |
} |
117 |
|
118 |
protected void doSetValue(int row, int column, double value) { |
119 |
data[row * getColumns() + column] = value; |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public double[] createRowBuffer() { |
124 |
return new double[getColumns()]; |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public void fetchRow(int row, Object rowBuffer) { |
129 |
System.arraycopy(data, row * getColumns(), (double[]) rowBuffer, 0, getColumns()); |
130 |
|
131 |
} |
132 |
|
133 |
@Override
|
134 |
protected void doPutRow(int row, Object rowBuffer) { |
135 |
System.arraycopy((double[]) rowBuffer, 0, data, row * getColumns(), getColumns()); |
136 |
|
137 |
} |
138 |
|
139 |
} |