svn-gvsig-desktop / trunk / org.gvsig.desktop / 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 / MemoryBandInt.java @ 44831
History | View | Annotate | Download (4.19 KB)
1 | 44831 | jjdelcerro | 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.BandNotification; |
||
7 | import org.gvsig.raster.lib.buffer.api.BufferManager; |
||
8 | import org.gvsig.raster.lib.buffer.api.NoData; |
||
9 | import org.gvsig.raster.lib.buffer.api.BufferLocator; |
||
10 | import org.gvsig.raster.lib.buffer.api.Band.BandInt; |
||
11 | import org.gvsig.raster.lib.buffer.impl.exceptions.CopyFromBandException; |
||
12 | |||
13 | /**
|
||
14 | * @author fdiaz
|
||
15 | *
|
||
16 | */
|
||
17 | public class MemoryBandInt extends AbstractBand implements BandInt { |
||
18 | |||
19 | int[] data; |
||
20 | |||
21 | /**
|
||
22 | * @param rows
|
||
23 | * @param columns
|
||
24 | */
|
||
25 | public MemoryBandInt(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 MemoryBandInt(int rows, int columns, NoData noData) { |
||
35 | this.rows = rows;
|
||
36 | this.columns = columns;
|
||
37 | data = new int[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_INT;
|
||
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, (int) 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, (int) value); |
||
67 | } |
||
68 | |||
69 | int[] 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 MemoryBandInt) { |
||
77 | System.arraycopy(((MemoryBandInt) 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 MemoryBandInt) {
|
||
99 | for(int r=0; r<Math.min(this.getRows()-row, source.getRows()); r++){ |
||
100 | System.arraycopy(((MemoryBandInt) 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 int getValue(int row, int column) { |
||
109 | return data[row * getColumns() + column];
|
||
110 | } |
||
111 | |||
112 | @Override
|
||
113 | public void setValue(int row, int column, int 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, int value) { |
||
119 | data[row * getColumns() + column] = value; |
||
120 | } |
||
121 | |||
122 | @Override
|
||
123 | public int[] createRowBuffer() { |
||
124 | return new int[getColumns()]; |
||
125 | } |
||
126 | |||
127 | @Override
|
||
128 | public void fetchRow(int row, Object rowBuffer) { |
||
129 | System.arraycopy(data, row * getColumns(), (int[]) rowBuffer, 0, getColumns()); |
||
130 | |||
131 | } |
||
132 | |||
133 | @Override
|
||
134 | protected void doPutRow(int row, Object rowBuffer) { |
||
135 | System.arraycopy((int[]) rowBuffer, 0, data, row * getColumns(), getColumns()); |
||
136 | |||
137 | } |
||
138 | } |