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