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 / MemoryBandByte.java @ 43803
History | View | Annotate | Download (4.67 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.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.Band.BandByte; |
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 MemoryBandByte extends AbstractBand implements BandByte { |
18 |
|
19 |
byte[] data; |
20 |
|
21 |
/**
|
22 |
* @param rows
|
23 |
* @param columns
|
24 |
*/
|
25 |
public MemoryBandByte(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 MemoryBandByte(int rows, int columns, NoData noData) { |
35 |
this.rows = rows;
|
36 |
this.columns = columns;
|
37 |
data = new byte[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_BYTE;
|
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 |
doSetValue(row, column, (byte) 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, (byte) value); |
67 |
} |
68 |
|
69 |
byte[] 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 MemoryBandByte) { |
77 |
System.arraycopy(((MemoryBandByte) 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 |
} |
91 |
} else {
|
92 |
throw new CopyFromBandException(source, this); |
93 |
} |
94 |
} |
95 |
|
96 |
@Override
|
97 |
protected void doCopyFrom(Band source, int row, int column) throws CopyFromBandException { |
98 |
if (this.getColumns() >= source.getColumns()+column && this.getRows() >= source.getRows()+row){ |
99 |
if(source instanceof MemoryBandByte) { |
100 |
for(int r=0; r<Math.min(this.getRows()-row, source.getRows()); r++){ |
101 |
System.arraycopy(((MemoryBandByte) source).getData(), r*source.getColumns(), this.data, (r+row)*this.getColumns()+column, Math.min(this.getColumns()-column, source.getColumns())); |
102 |
} |
103 |
} else {
|
104 |
Object rowBuffer = source.createRowBuffer();
|
105 |
for(int r=row; r<Math.min(this.getRows()-row, source.getRows()); r++){ |
106 |
source.fetchRow(r, rowBuffer);; |
107 |
System.arraycopy(rowBuffer, 0, this.data, (r+row)*this.getColumns(), Math.min(this.getColumns()-column, source.getColumns())); |
108 |
} |
109 |
} |
110 |
} else {
|
111 |
throw new CopyFromBandException(source, this); |
112 |
} |
113 |
} |
114 |
|
115 |
@Override
|
116 |
public byte getValue(int row, int column) { |
117 |
return data[row * getColumns() + column];
|
118 |
} |
119 |
|
120 |
@Override
|
121 |
public void setValue(int row, int column, byte value) { |
122 |
doSetValue(row, column, value); |
123 |
notifyObservers(new DefaultBandNotification(BandNotification.SET, new Object[]{row, column, value})); |
124 |
} |
125 |
|
126 |
protected void doSetValue(int row, int column, byte value) { |
127 |
data[row * getColumns() + column] = value; |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public byte[] createRowBuffer() { |
132 |
return new byte[getColumns()]; |
133 |
} |
134 |
|
135 |
@Override
|
136 |
public void fetchRow(int row, Object rowBuffer) { |
137 |
System.arraycopy(data, row * getColumns(), (byte[]) rowBuffer, 0, getColumns()); |
138 |
|
139 |
} |
140 |
|
141 |
protected void doPutRow(int row, Object rowBuffer) { |
142 |
System.arraycopy((byte[]) rowBuffer, 0, data, row * getColumns(), getColumns()); |
143 |
|
144 |
} |
145 |
|
146 |
@Override
|
147 |
public boolean isPaginated() { |
148 |
return false; |
149 |
} |
150 |
|
151 |
} |