Statistics
| Revision:

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 @ 6288

History | View | Annotate | Download (4.68 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.ComponentColorModel;
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
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

    
52
    public MemoryImage(JimiRasterImage jimiRasterImage) throws ImageAccessException {
53
        this.jimiRasterImage=jimiRasterImage;
54
        rows=jimiRasterImage.getHeight();
55
        columns=jimiRasterImage.getWidth();
56

    
57
        ColorModel colorModel = jimiRasterImage.getColorModel();
58

    
59
        if (colorModel instanceof ComponentColorModel){
60
            if( jimiRasterImage instanceof IntRasterImage ) {
61
                dataType = BufferManager.TYPE_INT;
62
            } else if( jimiRasterImage instanceof ByteRasterImage ) {
63
                dataType = BufferManager.TYPE_BYTE;
64
            } else {
65
                dataType = BufferManager.TYPE_INT;
66
            }
67

    
68
            bands=3;
69
            if ( jimiRasterImage.getColorModel().hasAlpha() ){
70
                bands=4;
71
            }
72
            buffer = new byte[bands][columns*rows];
73

    
74
            for( int band=0; band<bands; band++) {
75
                switch (band) {
76
                case 0:
77
                    jimiRasterImage.getChannelRectangle(JimiRasterImage.CHANNEL_RED, 0, 0, columns, rows, buffer[band], 0, columns);
78
                    break;
79
                case 1:
80
                    jimiRasterImage.getChannelRectangle(JimiRasterImage.CHANNEL_GREEN, 0, 0, columns, rows, buffer[band], 0, columns);
81
                    break;
82
                case 2:
83
                    jimiRasterImage.getChannelRectangle(JimiRasterImage.CHANNEL_BLUE, 0, 0, columns, rows, buffer[band], 0, columns);
84
                    break;
85
                case 3:
86
                    jimiRasterImage.getChannelRectangle(JimiRasterImage.CHANNEL_ALPHA, 0, 0, columns, rows, buffer[band], 0, columns);
87
                    break;
88
                default:
89
                    break;
90
                }
91
            }
92
        }else if (colorModel instanceof IndexColorModel){
93
            bands=1;
94
            IndexColorModel indexColorModel = (IndexColorModel)colorModel;
95
            BufferManager bufferManager = BufferLocator.getBufferManager();
96
            int dataTypeSize = bufferManager.getDataTypeSize(dataType);
97
            buffer = new byte[bands][columns*rows* dataTypeSize];
98

    
99
            if( jimiRasterImage instanceof ByteRasterImage ) {
100
                dataType = BufferManager.TYPE_BYTE;
101
                ByteRasterImage byteRasterImage = ( ByteRasterImage )jimiRasterImage;
102
                byteRasterImage.getRectangle(0, 0, columns, rows, buffer[0], 0, columns);
103
            } else {
104
                dataType = BufferManager.TYPE_INT;
105
                IntRasterImage intRasterImage = ( IntRasterImage )jimiRasterImage;
106
                int[] intAuxArray=new int[columns*rows];
107
                intRasterImage.getRectangle(0, 0, columns, rows, intAuxArray, 0, columns);
108

    
109
                ByteBuffer byteBuffer=ByteBuffer.wrap(buffer[0]);
110
                for(int i=0;i<=intAuxArray.length;i=i){
111
                    byteBuffer.putInt(intAuxArray[i]);
112
                }
113
            }
114

    
115
        }
116

    
117

    
118

    
119

    
120
    }
121

    
122
    public byte getValue(int row, int column, int band) {
123
        return buffer[band][row*columns + column];
124
    }
125

    
126
}
127