Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / io / JpegWriter.java @ 12471

History | View | Annotate | Download (6.76 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.gvsig.raster.dataset.io;
25

    
26
import java.io.File;
27
import java.io.IOException;
28

    
29
import org.cresques.cts.IProjection;
30
import org.gvsig.raster.dataset.GeoRasterWriter;
31
import org.gvsig.raster.dataset.IDataWriter;
32
import org.gvsig.raster.dataset.Params;
33
import org.gvsig.raster.dataset.io.features.JpegFeatures;
34
import org.gvsig.raster.dataset.io.features.WriteFileFormatFeatures;
35
import org.gvsig.raster.datastruct.Extent;
36
import org.gvsig.raster.util.RasterUtilities;
37
import org.gvsig.raster.util.extensionPoints.ExtensionPoints;
38
import org.gvsig.raster.util.extensionPoints.ExtensionPointsSingleton;
39

    
40
import es.gva.cit.jgdal.Gdal;
41
import es.gva.cit.jgdal.GdalDriver;
42
import es.gva.cit.jgdal.GdalException;
43

    
44

    
45
/**
46
 * Driver para la escritura de Jpeg.
47
 * Este driver utiliza GdalWriter para salvar Jpeg. 
48
 * La escritura de un jpeg no es posible utilizando un servidor de datos 
49
 * como el que usan los drivers comunes por lo que ser? necesario salvar antes
50
 * a Tif con el driver de Gdal para posteriormente convertir la imagen completa
51
 * a jpg.
52
 *  
53
 @author Nacho Brodin (nachobrodin@gmail.com)
54
 */
55
public class JpegWriter extends GeoRasterWriter {
56
    
57
        public final int                                 windowSizeX = 386;
58
    public final int                                 windowSizeY = 220;
59
    public final int                                 panelSizeX = 358;
60
    public final int                                 panelSizeY = 125;
61
    
62
        //Datos de registro de drivers
63
    public static void register() {
64
            ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
65

    
66
                extensionPoints.add("RasterWriter", "jpg", JpegWriter.class);
67
                fileFeature.put("jpg", new JpegFeatures());
68
    }
69

    
70
    private GdalWriter                                gdalWriter = null;
71
    private String                                        outTif = null;
72
    private String                                        outJpg = null;
73
    private String[]                                 params = null; //Par?metros de creaci?n del dataset.
74
        
75
        /**
76
         * Carga los par?metros de este driver.
77
         */
78
        public void loadParams(String ident) {
79
                WriteFileFormatFeatures wfff = (WriteFileFormatFeatures)fileFeature.get(ident);
80
                wfff.loadParams();
81
                driverParams = wfff.getParams();
82
        }
83
        
84
    /**
85
     * Constructor para la obtenci?n de par?metros del driver
86
     * @param drvType        Tipo de driver
87
     */
88
    public JpegWriter(String fileName) {
89
                ident = RasterUtilities.getExtensionFromFileName(fileName);
90
                driver = ((WriteFileFormatFeatures)fileFeature.get(ident)).getDriverName();
91
                  gdalWriter = new GdalWriter(fileName);
92
                  
93
                loadParams(ident);
94
    }
95

    
96
    /**
97
     * Constructor para salvar datos servidos por el cliente
98
     * @param dataWriter        Objeto servidor de datos para el driver de escritura
99
     * @param outSizeX        N?mero de pixels en X de la imagen de salida
100
     * @param outSizeY        N?mero de pixels en Y de la imagen de salida
101
     * @param outFilename        Fichero de salida
102
     * @param extentMaxX        Posici?n en X m?xima del extent
103
     * @param extentMinX        Posici?n en X m?nima del extent
104
     * @param extentMaxY        Posici?n en Y m?xima del extent
105
     * @param extentMinY        Posici?n en Y m?nima del extent
106
     * @param nBands        N?mero de bandas
107
     * @param drvType        Tipo de driver
108
     * @throws GdalException
109
     * @throws IOException
110
     */
111
    public JpegWriter(        IDataWriter dataWriter,
112
                                            String outFileName,
113
                                            Integer nBands,
114
                                            Extent ex,
115
                                            Integer outSizeX,
116
                                            Integer outSizeY,
117
                                            Integer dataType,
118
                                            Params params,
119
                                            IProjection proj)throws GdalException, IOException  {
120
            ident = RasterUtilities.getExtensionFromFileName(outFileName);
121
                driver = ((WriteFileFormatFeatures)fileFeature.get(ident)).getDriverName();
122
            outJpg = outFileName;
123
            outTif = outFileName.substring(0, outFileName.lastIndexOf("."));
124
            outTif += ".tif";
125
            
126
            gdalWriter = new GdalWriter(dataWriter, outTif, nBands, 
127
                                                                    ex, outSizeX, outSizeY, dataType, params, proj);
128
                if(params == null)
129
                        loadParams(ident);
130
                else
131
                        this.driverParams = params;
132
    }
133

    
134
   
135
    /**
136
     * Asigna el tipo de driver con el que se salvar? la imagen
137
     * @param drvType        Tipo de driver
138
     */
139
    public void setDriverType(String drvType) {
140
        gdalWriter.setDriverType(drvType);
141
    }
142
   
143
    /**
144
     * Realiza la funci?n de compresi?n a partir de un GeoRasterFile.
145
     * @throws IOException
146
     */
147
    public void fileWrite() throws IOException {
148
            gdalWriter.fileWrite();
149
    }
150

    
151
    /**
152
     * Realiza una copia en el formato especificado.
153
     * @throws IOException
154
     */
155
    public static void createCopy(GdalDriver driverDst, String dst, String src, 
156
                    boolean bstrict, String[] params, IProjection proj) throws IOException, GdalException {
157
            GdalWriter.createCopy(driverDst, dst, src, bstrict, params, proj);
158
    }
159
    
160
    /**
161
     * Realiza la escritura de datos con los datos que le pasa el cliente.
162
     * @throws IOException
163
     */
164
    public void dataWrite() throws IOException {
165
        gdalWriter.dataWrite();
166
        if(gdalWriter.isWrite()){
167
                gdalWriter.writeClose();
168
                if(outTif != null){
169
                        GdalDriver driver = null;
170
                        try{
171
                                    driver = Gdal.getDriverByName("JPEG");
172
                                    GdalWriter.createCopy(driver, outJpg, outTif, false, gdalWriter.gdalParamsFromRasterParams(driverParams), null);
173
                        }catch(GdalException exc){
174
                                throw new IOException("No se ha podido obtener el driver.");
175
                            }
176
                        File file = new File(outTif);
177
                        file.delete();        
178
                }
179
        }
180
    }
181

    
182
    /**
183
     * Cierra el compresor ecw.
184
     * @throws GdalException
185
     */
186
    public void writeClose() {
187
            //El close del tif se hizo en dataWrite
188
    }
189

    
190
    /**
191
     * Cancela el salvado de datos.
192
     */
193
    public void writeCancel() {
194
            gdalWriter.setWrite(false);
195
    }
196
    
197
    /*
198
     * (non-Javadoc)
199
     * @see org.gvsig.raster.dataset.GeoRasterWriter#setParams(org.gvsig.raster.dataset.Params)
200
     */
201
        public void setParams(Params params) {
202
                driverParams = params;
203
                if(gdalWriter != null)
204
                        gdalWriter.setParams(params);
205
        }
206
    
207
}