Statistics
| Revision:

svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / io / Rasterizer.java @ 2312

History | View | Annotate | Download (5.29 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 * 
4
 * Copyright (C) 2004-5. 
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 * 
22
 * cresques@gmail.com
23
 */
24
package org.cresques.io;
25

    
26
import java.awt.Dimension;
27
import java.awt.Graphics2D;
28
import java.awt.image.BufferedImage;
29
import java.awt.image.WritableRaster;
30

    
31
import org.cresques.geo.ViewPortData;
32
import org.cresques.px.Extent;
33
import org.cresques.px.PxLayerList;
34

    
35

    
36
/**
37
 * Sirve datos solicitados por los drivers que salvan a raster. Implementa
38
 * IDataWriter que obliga al m?todo readData que es el que ser? llamado
39
 * desde el driver cada vez que vacie el buffer y necesite m?s datos.
40
 * @author Nacho Brodin (brodin_ign@gva.es)
41
 */
42
public class Rasterizer implements IDataWriter {
43

    
44
        private ViewPortData                 viewPort = null;
45
        private ViewPortData                 viewPortBlock = null;
46
        protected BufferedImage                image;
47
        private PxLayerList                 layerList;
48
        protected int                                 sizeBlock = 0;
49
        protected int                                lastBlock = 0;
50
        protected WritableRaster        raster = null;
51
        protected int                                 contBlocks = 1;
52
        protected double                        wcIntervalo = 0;
53
        protected int[]                         rasterData = null;
54
        protected Dimension                 dimension = null;
55
        protected int[]                         buffer; 
56
        protected double                         wcAlto = 0;
57
        protected Graphics2D                 g;
58
                
59
        /**
60
         * Calculo del viewPort de cada bloque que vamos salvando a raster
61
         * @param vp        viewPort
62
         */
63
        private void calcViewPort(ViewPortData vp){
64
                Extent ext = null;
65

    
66
                if(viewPortBlock == null)//Primer bloque
67
                        ext = new Extent(        Math.round(vp.getExtent().minX()),
68
                                                                Math.round(vp.getExtent().maxY()),
69
                                                                Math.round(vp.getExtent().maxX()),
70
                                                                Math.round(vp.getExtent().maxY()-wcIntervalo));
71
                else        //Bloques siguientes
72
                        ext = new Extent(        Math.round(vp.getExtent().minX()),
73
                                                                Math.round(vp.getExtent().minY()),
74
                                                                Math.round(vp.getExtent().maxX()),
75
                                                                Math.round(vp.getExtent().minY()-wcIntervalo));
76
                
77
                viewPortBlock = new ViewPortData(        vp.getProjection(),
78
                                                                                        ext,
79
                                                                                        dimension);
80
                viewPortBlock.zoom(ext);
81
                
82
        }
83
        
84
        /**
85
         * Constructor para las clases que hereden de esta
86
         * y redefinan la funcionalidad que sirve los datos. Estas 
87
         * tampoco usaran el viewPort ya que se supone que disponen
88
         * de su propia estructura para la seleccion de la vista
89
         */
90
        public Rasterizer(int sizeBlock){
91
                this.sizeBlock = sizeBlock;
92
        }
93
        
94
        /**
95
         * Constructor
96
         * @param layerList
97
         * @param vp
98
         * @param sizeBlock
99
         */
100
        
101
        public Rasterizer(PxLayerList layerList, ViewPortData vp, int sizeBlock){
102
                
103
                viewPort = (ViewPortData) vp.clone();
104
                
105
                //Calculo del viewPort del primer bloque
106
                
107
                wcAlto = viewPort.getExtent().maxY() - viewPort.getExtent().minY();
108
                wcIntervalo = (sizeBlock*wcAlto)/viewPort.getHeight();
109
                dimension = new Dimension((int)viewPort.getWidth(), sizeBlock);
110

    
111
                calcViewPort(viewPort);
112
                
113
                //Tama?o de ?ltimo bloque
114
                int nBlocks = (int)(vp.getHeight()/sizeBlock); 
115
                lastBlock = (int)(vp.getHeight()-(nBlocks*sizeBlock));
116
                
117
                this.layerList = layerList;
118
                this.sizeBlock = sizeBlock;
119
                //System.out.println("==>Iniciando servidor datos raster "+viewPort.getHeight()+" "+viewPort.getWidth()+" "+wcIntervalo +"..."+viewPortBlock.getExtent().maxX()+" - "+viewPortBlock.getExtent().maxY()+" - "+viewPortBlock.getExtent().minX()+" - "+viewPortBlock.getExtent().minY());
120
                
121
        }
122
        
123
        /* (non-Javadoc)
124
         * @see org.cresques.io.IDataWriter#readData(int, int, int)
125
         */
126
        public int[] readData(int sX, int sY, int nBand){
127
                        
128
                if(nBand == 0){ //Con nBand==0 se devuelven las 3 bandas
129
                        //image = new BufferedImage((int)this.viewPortBlock.getWidth(), (int)this.viewPortBlock.getHeight(), BufferedImage.TYPE_INT_RGB);
130
                        image = new BufferedImage(sX, sY, BufferedImage.TYPE_INT_RGB);
131
                        g = (Graphics2D) image.getGraphics();
132
                        layerList.draw(g, viewPortBlock);        
133
                        
134
                        rasterData = image.getRGB(0, 0, sX, sY, rasterData, 0, sX);                                
135
                        
136
                        //System.out.println("==>Cargando bloque ..."+(int)this.viewPortBlock.getWidth()+" * "+(int)this.viewPortBlock.getHeight()+" * "+viewPortBlock.getExtent().maxX()+" - "+viewPortBlock.getExtent().maxY()+" - "+viewPortBlock.getExtent().minX()+" - "+viewPortBlock.getExtent().minY());
137
                        //System.out.println("==>Escribiendo linea ...0 - 0 - "+sX+" - "+sY+" - "+contBlocks);
138
                        
139
                        //Calculamos el viewPort del sgte bloque
140
                        
141
                        if(((contBlocks+1)*sizeBlock)<=viewPort.getHeight()){
142
                                dimension = new Dimension(sX, sY);
143
                        }
144
                        else{ //Calculo de la altura del ?ltimo bloque
145
                                dimension = new Dimension(sX, (int)(viewPort.getHeight()-(contBlocks*sizeBlock)));
146
                                wcIntervalo = (lastBlock*wcAlto)/viewPort.getHeight();
147
                        }
148
                        
149
                        calcViewPort(viewPortBlock);                        
150
                
151
                        contBlocks++;
152
                        return rasterData;
153
                }
154
                
155
                return null;
156
        }
157
}