summaryrefslogtreecommitdiffstats
path: root/awt/java/awt/image/PixelGrabber.java
blob: cecd5c853229095af893f23695022cf28b23f894 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * @author Igor V. Stolyarov
 * @version $Revision$
 */
package java.awt.image;

import java.awt.Image;
import java.util.Hashtable;

import org.apache.harmony.awt.internal.nls.Messages;

public class PixelGrabber implements ImageConsumer {

    int width;
    int height;
    int X;
    int Y;
    int offset;
    int scanline;
    ImageProducer producer;

    byte bData[];
    int iData[];
    ColorModel cm;

    private int grabberStatus;
    private int dataType;
    private boolean isGrabbing;
    private boolean isRGB;


    private static final int DATA_TYPE_BYTE = 0;
    private static final int DATA_TYPE_INT = 1;
    private static final int DATA_TYPE_UNDEFINED = 2;

    private static final int ALL_BITS = (ImageObserver.FRAMEBITS |
            ImageObserver.ALLBITS);

    private static final int GRABBING_STOP = ALL_BITS | ImageObserver.ERROR;



    public PixelGrabber(ImageProducer ip, int x, int y, int w, int h, int[] pix,
            int off, int scansize) {
        initialize(ip, x, y, w, h, pix, off, scansize, true);
    }

    public PixelGrabber(Image img, int x, int y, int w, int h, int[] pix,
            int off, int scansize) {
        initialize(img.getSource(), x, y, w, h, pix, off, scansize, true);
    }

    public PixelGrabber(Image img, int x, int y, int w, int h, boolean forceRGB) {
        initialize(img.getSource(), x, y, w, h, null, 0, 0, forceRGB);
    }

    public void setProperties(Hashtable<?, ?> props) {
        return;
    }

    public synchronized Object getPixels() {
        switch(dataType){
        case DATA_TYPE_BYTE:
            return bData;
        case DATA_TYPE_INT:
            return iData;
        default:
            return null;
        }
    }

    public void setColorModel(ColorModel model) {
        return;
    }

    public void setPixels(int srcX, int srcY, int srcW, int srcH,
            ColorModel model, byte[] pixels, int srcOff, int srcScan) {
        if(srcY < Y){
            int delta = Y - srcY;
            if(delta >= height) {
                return;
            }
            srcY += delta;
            srcH -= delta;
            srcOff += srcScan * delta;
        }

        if(srcY + srcH > Y + height){
            srcH = Y + height - srcY;
            if(srcH <= 0) {
                return;
            }
        }

        if(srcX < X){
            int delta = X - srcX;
            if(delta >= width) {
                return;
            }
            srcW -= delta;
            srcX += delta;
            srcOff += delta;
        }

        if(srcX + srcW > X + width){
            srcW = X + width - srcX;
            if(srcW <= 0) {
                return;
            }
        }
        if(scanline == 0) {
            scanline = width;
        }
        int realOff = offset + (srcY - Y) * scanline + (srcX - X);
        switch(dataType){
        case DATA_TYPE_UNDEFINED:
            cm = model;
            if(model != ColorModel.getRGBdefault()){
                bData = new byte[width * height];
                isRGB = false;
                dataType = DATA_TYPE_BYTE;
            }else{
                iData = new int[width * height];
                isRGB = true;
                dataType = DATA_TYPE_INT;
            }
        case DATA_TYPE_BYTE:
            if(!isRGB && cm == model){
                for(int y = 0; y < srcH; y++){
                    System.arraycopy(pixels, srcOff, bData, realOff, srcW);
                    srcOff += srcScan;
                    realOff += scanline;
                }
                break;
            }
            forceToRGB();
        case DATA_TYPE_INT:
            for(int y = 0; y < srcH; y++){
                for(int x = 0; x < srcW; x++){
                    iData[realOff + x] = cm.getRGB(pixels[srcOff + x] & 0xff);                    
                }
                srcOff += srcScan;
                realOff += scanline;
            }
        }

        return;
    }

    public void setPixels(int srcX, int srcY, int srcW, int srcH,
            ColorModel model, int[] pixels, int srcOff, int srcScan) {

        if(srcY < Y){
            int delta = Y - srcY;
            if(delta >= height) {
                return;
            }
            srcY += delta;
            srcH -= delta;
            srcOff += srcScan * delta;
        }

        if(srcY + srcH > Y + height){
            srcH = Y + height - srcY;
            if(srcH <= 0) {
                return;
            }
        }

        if(srcX < X){
            int delta = X - srcX;
            if(delta >= width) {
                return;
            }
            srcW -= delta;
            srcX += delta;
            srcOff += delta;
        }

        if(srcX + srcW > X + width){
            srcW = X + width - srcX;
            if(srcW <= 0) {
                return;
            }
        }
        if(scanline == 0) {
            scanline = width;
        }
        int realOff = offset + (srcY - Y) * scanline + (srcX - X);

        int mask = 0xFF;

        switch(dataType){
        case DATA_TYPE_UNDEFINED:
            cm = model;
            iData = new int[width * height];
            dataType = DATA_TYPE_INT;
            isRGB = (cm == ColorModel.getRGBdefault());

        case DATA_TYPE_INT:
            if(cm == model){
                for(int y = 0; y < srcH; y++){
                    System.arraycopy(pixels, srcOff, iData, realOff, srcW);
                    srcOff += srcScan;
                    realOff += scanline;
                }
                break;
            }
            mask = 0xFFFFFFFF;

        case DATA_TYPE_BYTE:
            forceToRGB();
            for(int y = 0; y < srcH; y++){
                for(int x = 0; x < srcW; x++){
                    iData[realOff+x] = cm.getRGB(pixels[srcOff+x] & mask);
                }
                srcOff += srcScan;
                realOff += scanline;
            }
        }
    }

    public synchronized ColorModel getColorModel() {
        return cm;
    }

    public synchronized boolean grabPixels(long ms) 
    throws InterruptedException {
        if((grabberStatus & GRABBING_STOP) != 0){
            return ((grabberStatus & ALL_BITS) != 0);
        }

        long start = System.currentTimeMillis();

        if(!isGrabbing){
            isGrabbing = true;
            grabberStatus &= ~ImageObserver.ABORT;
            producer.startProduction(this);
        }
        while((grabberStatus & GRABBING_STOP) == 0){
            if(ms != 0){
                ms = start + ms - System.currentTimeMillis();
                if(ms <= 0) {
                    break;
                }
            }
            wait(ms);
        }

        return ((grabberStatus & ALL_BITS) != 0);
    }

    public void setDimensions(int w, int h) {
        if(width < 0) {
            width = w - X;
        }
        if(height < 0) {
            height = h - Y;
        }

        grabberStatus |= ImageObserver.WIDTH | ImageObserver.HEIGHT;

        if(width <=0 || height <=0){
            imageComplete(STATICIMAGEDONE);
            return;
        }

        if(isRGB && dataType == DATA_TYPE_UNDEFINED){
            iData = new int[width * height];
            dataType = DATA_TYPE_INT;
            scanline = width;
        }
    }

    public void setHints(int hints) {
        return;
    }

    public synchronized void imageComplete(int status) {
        switch(status){
        case IMAGEABORTED:
            grabberStatus |= ImageObserver.ABORT;
            break;
        case IMAGEERROR:
            grabberStatus |= ImageObserver.ERROR | ImageObserver.ABORT;
            break;
        case SINGLEFRAMEDONE:
            grabberStatus |= ImageObserver.FRAMEBITS;
            break;
        case STATICIMAGEDONE:
            grabberStatus |= ImageObserver.ALLBITS;
            break;
        default:
            // awt.26A=Incorrect ImageConsumer completion status
            throw new IllegalArgumentException(Messages.getString("awt.26A")); //$NON-NLS-1$
        }
        isGrabbing = false;
        producer.removeConsumer(this);
        notifyAll();
    }

    public boolean grabPixels() throws InterruptedException {
        return grabPixels(0);
    }

    public synchronized void startGrabbing() {
        if((grabberStatus & GRABBING_STOP) != 0){
            return;
        }
        if(!isGrabbing){
            isGrabbing = true;
            grabberStatus &= ~ImageObserver.ABORT;
            producer.startProduction(this);
        }
    }

    public synchronized void abortGrabbing() {
        imageComplete(IMAGEABORTED);
    }

    public synchronized int status() {
        return grabberStatus;
    }

    public synchronized int getWidth() {
        if(width < 0) {
            return -1;
        }
        return width;
    }

    public synchronized int getStatus() {
        return grabberStatus;
    }

    public synchronized int getHeight() {
        if(height < 0) {
            return -1;
        }
        return height;
    }

    private void initialize(ImageProducer ip, int x, int y, int w, int h,
            int pixels[], int off, int scansize, boolean forceRGB){

        producer = ip;
        X = x;
        Y = y;
        width = w;
        height = h;
        iData = pixels;
        dataType = (pixels == null) ? DATA_TYPE_UNDEFINED : DATA_TYPE_INT;
        offset = off;
        scanline = scansize;
        if(forceRGB){
            cm = ColorModel.getRGBdefault();
            isRGB = true;
        }
    }

    /**
     * Force pixels to INT RGB mode
     */
    private void forceToRGB(){
        if (isRGB)
            return;
    
        switch(dataType){
        case DATA_TYPE_BYTE:
            iData = new int[width * height];
            for(int i = 0; i < iData.length; i++){
                iData[i] = cm.getRGB(bData[i] & 0xff);
            }
            dataType = DATA_TYPE_INT;
            bData = null;
            break;

        case DATA_TYPE_INT:
            int buff[] = new int[width * height];
            for(int i = 0; i < iData.length; i++){
                buff[i] = cm.getRGB(iData[i]);
            }
            iData = buff;
            break;
        }
        offset = 0;
        scanline = width;
        cm = ColorModel.getRGBdefault();
        isRGB = true;
    }

}