Statistics
| Revision:

root / trunk / libraries / libCq_CMS_praster / src / org / cresques / filter / enhancement / TransparencyImageFilter.java @ 8026

History | View | Annotate | Download (5.73 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.filter.enhancement;
25

    
26
import java.awt.Image;
27
import java.awt.image.BufferedImage;
28

    
29
import org.cresques.io.data.RasterBuf;
30

    
31

    
32
/**
33
 * Filtro de transparencia aplicada a un rango de valores de un pixel sobre un objeto image
34
 * @author Nacho Brodin (brodin_ign@gva.es)
35
 *
36
 */
37
public class TransparencyImageFilter extends TransparencyFilter {
38
    /**
39
     * Constructor
40
     *
41
     */
42
    public TransparencyImageFilter() {
43
        super();
44
    }
45

    
46
    /* (non-Javadoc)
47
     * @see org.cresques.io.raster.IRasterFilter#pre()
48
     */
49
    public void pre() {
50
        //Obtenci?n de par?metros
51
        this.image = (Image) params.get("raster");
52
        height = image.getHeight(null);
53
        width = image.getWidth(null);
54
        super.pre();
55
    }
56

    
57
    /**
58
     * Asigna al pixel pasado como par?metro como transparente con 
59
     * el color de transparencia que est? seleccionado
60
     * @param px Pixel a asignarle transparencia
61
     */
62
    private void setPixelTransparent(int[] px){
63
                px[0] = this.alpha;
64
        px[1] = this.transparencyColorRed;
65
        px[2] = this.transparencyColorGreen;
66
        px[3] = this.transparencyColorBlue;        
67
    }
68
    
69
    /**     
70
     * @param range rango
71
     * @param banda banda
72
     * @param px    pixel
73
     */
74
    private void processRange(int[] px) {
75
            for (int i = 0; i < rangesList.size(); i++) {
76
                    TransparencyRange tr = ((TransparencyRange)rangesList.get(i));
77

    
78
                    if(tr.isAnd()){
79
                            if(tr.getRed() != null)
80
                                    if (px[1] < tr.getRed()[0] || px[1] > tr.getRed()[1])
81
                                            continue;
82
                            if(tr.getGreen() != null)
83
                                    if (px[2] < tr.getGreen()[0] || px[2] > tr.getGreen()[1])
84
                                            continue;
85
                            if(tr.getBlue() != null)
86
                                    if (px[3] < tr.getBlue()[0] || px[3] > tr.getBlue()[1])
87
                                            continue;
88
                            if(tr.getRed() != null || tr.getGreen() != null || tr.getBlue() != null)
89
                                    setPixelTransparent(px);
90
                            
91
                    }else{
92
                            if(tr.getRed() != null){
93
                                    if (px[1] >= tr.getRed()[0] && px[1] <= tr.getRed()[1]){
94
                                            setPixelTransparent(px);
95
                                            continue;
96
                                    }
97
                            }
98
                            if(tr.getGreen() != null){
99
                                    if (px[2] >= tr.getGreen()[0] && px[2] <= tr.getGreen()[1]){
100
                                            setPixelTransparent(px);
101
                                            continue;
102
                                    }
103
                            }
104
                            if(tr.getBlue() != null){
105
                                    if (px[3] >= tr.getBlue()[0] && px[3] <= tr.getBlue()[1]){
106
                                            setPixelTransparent(px);
107
                                            continue;
108
                                    }
109
                            }
110
                    }
111
            }
112
            
113
    }
114
    
115
    /* (non-Javadoc)
116
     * @see org.cresques.io.raster.IRasterFilter#process(int, int)
117
     */
118
    public void process(int col, int line) {
119
        int pt = ((BufferedImage) image).getRGB(col, line);
120
        int[] px4 = {
121
                        (pt & 0xff000000) >> 24, (pt & 0xff0000) >> 16,
122
                        (pt & 0x00ff00) >> 8, (pt & 0x0000ff)
123
                    };
124

    
125
        processRange(px4);
126
       
127
        ((BufferedImage) image).setRGB(col, line,
128
                                       (((px4[0] << 24) & 0xff000000) |
129
                                       ((px4[1] << 16) & 0x00ff0000) |
130
                                       ((px4[2] << 8) & 0x0000ff00) |
131
                                       (px4[3] & 0x0000ff)));
132
    }
133

    
134
    /* (non-Javadoc)
135
     * @see org.cresques.io.raster.IRasterFilter#processSuperSampling(int, int)
136
     */
137
    public void processSuperSampling(int col, int line) {
138
            int pt = ((BufferedImage) image).getRGB(col, line);
139
        int[] px4 = {
140
                        (pt & 0xff000000) >> 24, (pt & 0xff0000) >> 16,
141
                        (pt & 0x00ff00) >> 8, (pt & 0x0000ff)
142
                    };
143

    
144
        processRange(px4);
145
    
146
        for(int j = col; j < image.getWidth(null) && j < (col + stepX[contX + 1]); j++)
147
                        for(int i = line; i < image.getHeight(null) && i < (line + stepY[contY + 1]); i++)
148
                             ((BufferedImage) image).setRGB(j, i,         (((px4[0] << 24) & 0xff000000) |
149
                                                                                                             ((px4[1] << 16) & 0x00ff0000) |
150
                                                                                                             ((px4[2] << 8) & 0x0000ff00) |
151
                                                                                                             (px4[3] & 0x0000ff)));
152
        }
153
    
154
    /* (non-Javadoc)
155
     * @see org.cresques.io.raster.IRasterFilter#getInRasterDataType()
156
     */
157
    public int getInRasterDataType() {
158
        return RasterBuf.TYPE_IMAGE;
159
    }
160

    
161
    /* (non-Javadoc)
162
     * @see org.cresques.io.raster.IRasterFilter#getOutRasterDataType()
163
     */
164
    public int getOutRasterDataType() {
165
        return RasterBuf.TYPE_IMAGE;
166
    }
167

    
168
    /* (non-Javadoc)
169
     * @see org.cresques.io.raster.IRasterFilter#getResult(java.lang.String)
170
     */
171
    public Object getResult(String name) {
172
        if (name.equals("raster")) {
173
            return (Object) this.image;
174
        } else {
175
            return null;
176
        }
177
    }
178

    
179
    /* (non-Javadoc)
180
     * @see org.cresques.io.raster.RasterFilter#processLine(int)
181
     */
182
    public void processLine(int y) {
183
    }
184

    
185
    /* (non-Javadoc)
186
     * @see org.cresques.io.raster.IRasterFilter#post()
187
     */
188
    public void post() {
189
    }
190
}