Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1006 / libraries / libCq CMS for java.old / src / org / cresques / io / Rasterizer.java @ 12458

History | View | Annotate | Download (6.03 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 org.cresques.geo.ViewPortData;
27

    
28
import org.cresques.px.Extent;
29
import org.cresques.px.PxLayerList;
30

    
31
import java.awt.Dimension;
32
import java.awt.Graphics2D;
33
import java.awt.image.BufferedImage;
34
import java.awt.image.WritableRaster;
35

    
36

    
37
/**
38
 * Sirve datos solicitados por los drivers que salvan a raster. Implementa
39
 * IDataWriter que obliga al m?todo readData que es el que ser? llamado
40
 * desde el driver cada vez que vacie el buffer y necesite m?s datos.
41
 * @author Nacho Brodin (brodin_ign@gva.es)
42
 */
43
public class Rasterizer implements IDataWriter {
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
     * Constructor para las clases que hereden de esta
61
     * y redefinan la funcionalidad que sirve los datos. Estas
62
     * tampoco usaran el viewPort ya que se supone que disponen
63
     * de su propia estructura para la seleccion de la vista
64
     */
65
    public Rasterizer(int sizeBlock) {
66
        this.sizeBlock = sizeBlock;
67
    }
68

    
69
    /**
70
     * Constructor
71
     * @param layerList
72
     * @param vp
73
     * @param sizeBlock
74
     */
75
    public Rasterizer(PxLayerList layerList, ViewPortData vp, int sizeBlock) {
76
        viewPort = (ViewPortData) vp.clone();
77

    
78
        //Calculo del viewPort del primer bloque
79
        wcAlto = viewPort.getExtent().maxY() - viewPort.getExtent().minY();
80
        wcIntervalo = (sizeBlock * wcAlto) / viewPort.getHeight();
81
        dimension = new Dimension((int) viewPort.getWidth(), sizeBlock);
82

    
83
        calcViewPort(viewPort);
84

    
85
        //Tama?o de ?ltimo bloque
86
        int nBlocks = (int) (vp.getHeight() / sizeBlock);
87
        lastBlock = (int) (vp.getHeight() - (nBlocks * sizeBlock));
88

    
89
        this.layerList = layerList;
90
        this.sizeBlock = sizeBlock;
91

    
92
        //System.out.println("==>Iniciando servidor datos raster "+viewPort.getHeight()+" "+viewPort.getWidth()+" "+wcIntervalo +"..."+viewPortBlock.getExtent().maxX()+" - "+viewPortBlock.getExtent().maxY()+" - "+viewPortBlock.getExtent().minX()+" - "+viewPortBlock.getExtent().minY());
93
    }
94

    
95
    /**
96
     * Calculo del viewPort de cada bloque que vamos salvando a raster
97
     * @param vp        viewPort
98
     */
99
    private void calcViewPort(ViewPortData vp) {
100
        Extent ext = null;
101

    
102
        if (viewPortBlock == null) { //Primer bloque
103
            ext = new Extent(Math.round(vp.getExtent().minX()),
104
                             Math.round(vp.getExtent().maxY()),
105
                             Math.round(vp.getExtent().maxX()),
106
                             Math.round(vp.getExtent().maxY() - wcIntervalo));
107
        } else { //Bloques siguientes
108
            ext = new Extent(Math.round(vp.getExtent().minX()),
109
                             Math.round(vp.getExtent().minY()),
110
                             Math.round(vp.getExtent().maxX()),
111
                             Math.round(vp.getExtent().minY() - wcIntervalo));
112
        }
113

    
114
        viewPortBlock = new ViewPortData(vp.getProjection(), ext, dimension);
115
        viewPortBlock.zoom(ext);
116
    }
117

    
118
    /* (non-Javadoc)
119
     * @see org.cresques.io.IDataWriter#readData(int, int, int)
120
     */
121
    public int[] readData(int sX, int sY, int nBand) {
122
        if (nBand == 0) { //Con nBand==0 se devuelven las 3 bandas
123

    
124
            //image = new BufferedImage((int)this.viewPortBlock.getWidth(), (int)this.viewPortBlock.getHeight(), BufferedImage.TYPE_INT_RGB);
125
            image = new BufferedImage(sX, sY, BufferedImage.TYPE_INT_RGB);
126
            g = (Graphics2D) image.getGraphics();
127
            layerList.draw(g, viewPortBlock);
128

    
129
            rasterData = image.getRGB(0, 0, sX, sY, rasterData, 0, sX);
130

    
131
            //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());
132
            //System.out.println("==>Escribiendo linea ...0 - 0 - "+sX+" - "+sY+" - "+contBlocks);
133
            //Calculamos el viewPort del sgte bloque
134
            if (((contBlocks + 1) * sizeBlock) <= viewPort.getHeight()) {
135
                dimension = new Dimension(sX, sY);
136
            } else { //Calculo de la altura del ?ltimo bloque
137
                dimension = new Dimension(sX,
138
                                          (int) (viewPort.getHeight() -
139
                                          (contBlocks * sizeBlock)));
140
                wcIntervalo = (lastBlock * wcAlto) / viewPort.getHeight();
141
            }
142

    
143
            calcViewPort(viewPortBlock);
144

    
145
            contBlocks++;
146

    
147
            return rasterData;
148
        }
149

    
150
        return null;
151
    }
152

    
153
    /**
154
     * Asigna el ancho del bloque
155
     * @param sizeBlock Ancho del bloque en pixeles
156
     */
157
        public void setSizeBlock(int sizeBlock) {
158
                this.sizeBlock = sizeBlock;
159
        }
160
}