gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.fmap.dal.file.jimi / src / main / java / org / gvsig / fmap / dal / file / jimi / MemoryImage.java @ 6298
History | View | Annotate | Download (4.63 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2016 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.dal.file.jimi; |
24 |
|
25 |
import java.awt.image.ColorModel; |
26 |
import java.awt.image.DirectColorModel; |
27 |
import java.awt.image.IndexColorModel; |
28 |
import java.nio.ByteBuffer; |
29 |
|
30 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
31 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
32 |
|
33 |
import com.sun.jimi.core.ImageAccessException; |
34 |
import com.sun.jimi.core.raster.ByteRasterImage; |
35 |
import com.sun.jimi.core.raster.IntRasterImage; |
36 |
import com.sun.jimi.core.raster.JimiRasterImage; |
37 |
|
38 |
/**
|
39 |
* Represents an image loaded in memory
|
40 |
* @author dmartinezizquierdo
|
41 |
*
|
42 |
*/
|
43 |
public class MemoryImage{ |
44 |
public int rows; |
45 |
public int columns; |
46 |
public int bands; |
47 |
public byte buffer[][]; |
48 |
public int dataType; |
49 |
private JimiRasterImage jimiRasterImage;
|
50 |
public boolean loaded; |
51 |
public ColorModel colorModel; |
52 |
|
53 |
public MemoryImage(JimiRasterImage jimiRasterImage) throws ImageAccessException { |
54 |
this.jimiRasterImage=jimiRasterImage;
|
55 |
rows=jimiRasterImage.getHeight(); |
56 |
columns=jimiRasterImage.getWidth(); |
57 |
|
58 |
colorModel = jimiRasterImage.getColorModel(); |
59 |
|
60 |
|
61 |
if (colorModel instanceof DirectColorModel){ |
62 |
// if( jimiRasterImage instanceof IntRasterImage ) {
|
63 |
// dataType = BufferManager.TYPE_INT;
|
64 |
// } else if( jimiRasterImage instanceof ByteRasterImage ) {
|
65 |
dataType = BufferManager.TYPE_BYTE; |
66 |
// } else {
|
67 |
// dataType = BufferManager.TYPE_INT;
|
68 |
// }
|
69 |
|
70 |
bands=3;
|
71 |
if ( colorModel.hasAlpha() ){
|
72 |
bands=4;
|
73 |
} |
74 |
buffer = new byte[bands][columns*rows]; |
75 |
|
76 |
for( int band=0; band<bands; band++) { |
77 |
switch (band) {
|
78 |
case 0: |
79 |
jimiRasterImage.getChannelRectangle(JimiRasterImage.CHANNEL_RED, 0, 0, columns, rows, buffer[band], 0, columns); |
80 |
break;
|
81 |
case 1: |
82 |
jimiRasterImage.getChannelRectangle(JimiRasterImage.CHANNEL_GREEN, 0, 0, columns, rows, buffer[band], 0, columns); |
83 |
break;
|
84 |
case 2: |
85 |
jimiRasterImage.getChannelRectangle(JimiRasterImage.CHANNEL_BLUE, 0, 0, columns, rows, buffer[band], 0, columns); |
86 |
break;
|
87 |
case 3: |
88 |
jimiRasterImage.getChannelRectangle(JimiRasterImage.CHANNEL_ALPHA, 0, 0, columns, rows, buffer[band], 0, columns); |
89 |
break;
|
90 |
default:
|
91 |
break;
|
92 |
} |
93 |
} |
94 |
}else if (colorModel instanceof IndexColorModel){ |
95 |
bands=1;
|
96 |
|
97 |
BufferManager bufferManager = BufferLocator.getBufferManager(); |
98 |
int dataTypeSize = bufferManager.getDataTypeSize(dataType);
|
99 |
buffer = new byte[bands][columns*rows* dataTypeSize]; |
100 |
|
101 |
if( jimiRasterImage instanceof ByteRasterImage ) { |
102 |
dataType = BufferManager.TYPE_BYTE; |
103 |
ByteRasterImage byteRasterImage = ( ByteRasterImage )jimiRasterImage; |
104 |
byteRasterImage.getRectangle(0, 0, columns, rows, buffer[0], 0, columns); |
105 |
} else {
|
106 |
dataType = BufferManager.TYPE_INT; |
107 |
IntRasterImage intRasterImage = ( IntRasterImage )jimiRasterImage; |
108 |
int[] intAuxArray=new int[columns*rows]; |
109 |
intRasterImage.getRectangle(0, 0, columns, rows, intAuxArray, 0, columns); |
110 |
|
111 |
ByteBuffer byteBuffer=ByteBuffer.wrap(buffer[0]); |
112 |
for(int i=0;i<=intAuxArray.length;i=i){ |
113 |
byteBuffer.putInt(intAuxArray[i]); |
114 |
} |
115 |
} |
116 |
|
117 |
} |
118 |
|
119 |
|
120 |
|
121 |
|
122 |
} |
123 |
|
124 |
public byte getValue(int row, int column, int band) { |
125 |
return buffer[band][row*columns + column];
|
126 |
} |
127 |
|
128 |
} |
129 |
|