summaryrefslogtreecommitdiffstats
path: root/awt/java/awt/image/WritableRaster.java
blob: 51366ee64c13c63a68f72860ef528ea486090203 (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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
 *  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.Point;
import java.awt.Rectangle;

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

/**
 * The WritableRaster class provides functionality for writing samples and pixel
 * capabilities to the Raster.
 * 
 * @since Android 1.0
 */
public class WritableRaster extends Raster {

    /**
     * Instantiates a new WritableRaster object with the specified SampleModel,
     * DataBuffer, rectangular region and parent WritableRaster.
     * 
     * @param sampleModel
     *            the specified SampleModel.
     * @param dataBuffer
     *            the specified DataBuffer.
     * @param aRegion
     *            the rectangular region which defines the new image bounds.
     * @param sampleModelTranslate
     *            this point defines the translation point from the SampleModel
     *            to the new WritableRaster coordinates.
     * @param parent
     *            the parent of this WritableRaster.
     */
    protected WritableRaster(SampleModel sampleModel, DataBuffer dataBuffer, Rectangle aRegion,
            Point sampleModelTranslate, WritableRaster parent) {
        super(sampleModel, dataBuffer, aRegion, sampleModelTranslate, parent);
    }

    /**
     * Instantiates a new WritableRaster object with the specified SampleModel
     * which defines a layout of this WritableRaster and DataBuffer objects
     * which defines the image data.
     * 
     * @param sampleModel
     *            the specified SampleModel.
     * @param dataBuffer
     *            the specified DataBuffer.
     * @param origin
     *            the point of origin.
     */
    protected WritableRaster(SampleModel sampleModel, DataBuffer dataBuffer, Point origin) {
        this(sampleModel, dataBuffer, new Rectangle(origin.x, origin.y, sampleModel.width,
                sampleModel.height), origin, null);
    }

    /**
     * Instantiates a new WritableRaster with the specified SampleModel.
     * 
     * @param sampleModel
     *            the specified SampleModel.
     * @param origin
     *            the origin.
     */
    protected WritableRaster(SampleModel sampleModel, Point origin) {
        this(sampleModel, sampleModel.createDataBuffer(), new Rectangle(origin.x, origin.y,
                sampleModel.width, sampleModel.height), origin, null);
    }

    /**
     * Sets the data for a single pixel from an input Object which represents an
     * array of primitive types: DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT,
     * DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT, or
     * DataBuffer.TYPE_DOUBLE.
     * 
     * @param x
     *            the X coordinate of the pixel.
     * @param y
     *            the Y coordinate of the pixel.
     * @param inData
     *            the input data.
     */
    public void setDataElements(int x, int y, Object inData) {
        sampleModel.setDataElements(x - sampleModelTranslateX, y - sampleModelTranslateY, inData,
                dataBuffer);
    }

    /**
     * Sets the data elements which represent pixel data to the specified
     * rectangle area as a primitive array. The following image data types are
     * supported: DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT,
     * DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT, or
     * DataBuffer.TYPE_DOUBLE.
     * 
     * @param x
     *            the X coordinate of the rectangle of pixels.
     * @param y
     *            the Y coordinate of the rectangle of pixels.
     * @param w
     *            the width of the rectangle of pixels.
     * @param h
     *            the height of the rectangle of pixels.
     * @param inData
     *            the array of primitive type data to be set to the specified
     *            area.
     */
    public void setDataElements(int x, int y, int w, int h, Object inData) {
        sampleModel.setDataElements(x - sampleModelTranslateX, y - sampleModelTranslateY, w, h,
                inData, dataBuffer);
    }

    /**
     * Creates the child of this WritableRaster by sharing the specified
     * rectangular area in this WritableRaster. The parentX, parentY, width and
     * height parameters specify rectangular area to be shared.
     * 
     * @param parentX
     *            the X coordinate of the upper left corner of the shared
     *            rectangle with respect to this WritableRaster' coordinates.
     * @param parentY
     *            the Y coordinate of the upper left corner of the shared
     *            rectangle with respect to this WritableRaster' coordinates.
     * @param w
     *            the width of the child area.
     * @param h
     *            the height of the child area.
     * @param childMinX
     *            the X coordinate of child area mapped to the parentX
     *            coordinate.
     * @param childMinY
     *            the Y coordinate of child area mapped to the parentY
     *            coordinate.
     * @param bandList
     *            the array of band indices.
     * @return the child WritableRaster.
     */
    public WritableRaster createWritableChild(int parentX, int parentY, int w, int h,
            int childMinX, int childMinY, int bandList[]) {
        if (w <= 0 || h <= 0) {
            // awt.244=Width or Height of child Raster is less than or equal to
            // zero
            throw new RasterFormatException(Messages.getString("awt.244")); //$NON-NLS-1$
        }

        if (parentX < this.minX || parentX + w > this.minX + this.width) {
            // awt.245=parentX disposes outside Raster
            throw new RasterFormatException(Messages.getString("awt.245")); //$NON-NLS-1$
        }

        if (parentY < this.minY || parentY + h > this.minY + this.height) {
            // awt.246=parentY disposes outside Raster
            throw new RasterFormatException(Messages.getString("awt.246")); //$NON-NLS-1$
        }

        if ((long)parentX + w > Integer.MAX_VALUE) {
            // awt.247=parentX + w results in integer overflow
            throw new RasterFormatException(Messages.getString("awt.247")); //$NON-NLS-1$
        }

        if ((long)parentY + h > Integer.MAX_VALUE) {
            // awt.248=parentY + h results in integer overflow
            throw new RasterFormatException(Messages.getString("awt.248")); //$NON-NLS-1$
        }

        if ((long)childMinX + w > Integer.MAX_VALUE) {
            // awt.249=childMinX + w results in integer overflow
            throw new RasterFormatException(Messages.getString("awt.249")); //$NON-NLS-1$
        }

        if ((long)childMinY + h > Integer.MAX_VALUE) {
            // awt.24A=childMinY + h results in integer overflow
            throw new RasterFormatException(Messages.getString("awt.24A")); //$NON-NLS-1$
        }

        SampleModel childModel;

        if (bandList == null) {
            childModel = sampleModel;
        } else {
            childModel = sampleModel.createSubsetSampleModel(bandList);
        }

        int childTranslateX = childMinX - parentX;
        int childTranslateY = childMinY - parentY;

        return new WritableRaster(childModel, dataBuffer,
                new Rectangle(childMinX, childMinY, w, h), new Point(childTranslateX
                        + sampleModelTranslateX, childTranslateY + sampleModelTranslateY), this);
    }

    /**
     * Creates the translated child of this WritableRaster. New WritableRaster
     * object is a reference to the this WritableRaster and with different
     * location.
     * 
     * @param childMinX
     *            the X coordinate of the new WritableRaster.
     * @param childMinY
     *            the Y coordinate of the new WritableRaster.
     * @return the WritableRaster.
     */
    public WritableRaster createWritableTranslatedChild(int childMinX, int childMinY) {
        return createWritableChild(minX, minY, width, height, childMinX, childMinY, null);
    }

    /**
     * Gets the parent WritableRaster for this WritableRaster object.
     * 
     * @return the parent WritableRaster for this WritableRaster object.
     */
    public WritableRaster getWritableParent() {
        return (WritableRaster)parent;
    }

    /**
     * Sets pixels from the specified source Raster srcRaster to this
     * WritableRaster.
     * 
     * @param srcRaster
     *            the source Raster.
     */
    public void setRect(Raster srcRaster) {
        setRect(0, 0, srcRaster);
    }

    /**
     * Sets pixels from the specified source Raster srcRaster to this
     * WritableRaster. Each pixel with (x, y) coordinates from the source Raster
     * is copied to pixel with (x+dx, y+dy) coordinates in this WritableRaster.
     * The pixels with (x+dx, y+dy) coordinates which are out the bounds of this
     * raster are ignored.
     * 
     * @param dx
     *            the distance the pixel's X coordinate in the source Raster is
     *            translated when writtien to this WritableRaster.
     * @param dy
     *            the distance the pixel's Y coordinate in the source Raster is
     *            translated when writtien to this WritableRaster.
     * @param srcRaster
     *            the source Raster.
     */
    public void setRect(int dx, int dy, Raster srcRaster) {
        int w = srcRaster.getWidth();
        int h = srcRaster.getHeight();

        int srcX = srcRaster.getMinX();
        int srcY = srcRaster.getMinY();

        int dstX = srcX + dx;
        int dstY = srcY + dy;

        if (dstX < this.minX) {
            int minOffX = this.minX - dstX;
            w -= minOffX;
            dstX = this.minX;
            srcX += minOffX;
        }

        if (dstY < this.minY) {
            int minOffY = this.minY - dstY;
            h -= minOffY;
            dstY = this.minY;
            srcY += minOffY;
        }

        if (dstX + w > this.minX + this.width) {
            int maxOffX = (dstX + w) - (this.minX + this.width);
            w -= maxOffX;
        }

        if (dstY + h > this.minY + this.height) {
            int maxOffY = (dstY + h) - (this.minY + this.height);
            h -= maxOffY;
        }

        if (w <= 0 || h <= 0) {
            return;
        }

        switch (sampleModel.getDataType()) {
            case DataBuffer.TYPE_BYTE:
            case DataBuffer.TYPE_SHORT:
            case DataBuffer.TYPE_USHORT:
            case DataBuffer.TYPE_INT:
                int iPixelsLine[] = null;
                for (int i = 0; i < h; i++) {
                    iPixelsLine = srcRaster.getPixels(srcX, srcY + i, w, 1, iPixelsLine);
                    setPixels(dstX, dstY + i, w, 1, iPixelsLine);
                }
                break;

            case DataBuffer.TYPE_FLOAT:
                float fPixelsLine[] = null;
                for (int i = 0; i < h; i++) {
                    fPixelsLine = srcRaster.getPixels(srcX, srcY + i, w, 1, fPixelsLine);
                    setPixels(dstX, dstY + i, w, 1, fPixelsLine);
                }
                break;

            case DataBuffer.TYPE_DOUBLE:
                double dPixelsLine[] = null;
                for (int i = 0; i < h; i++) {
                    dPixelsLine = srcRaster.getPixels(srcX, srcY + i, w, 1, dPixelsLine);
                    setPixels(dstX, dstY + i, w, 1, dPixelsLine);
                }
                break;
        }
    }

    /**
     * Sets the data for a rectangle of pixels from an input Raster to this
     * WritableRaster.
     * 
     * @param x
     *            the X coordinate of the point where the data of the input
     *            Raster is to be written.
     * @param y
     *            the Y coordinate of the point where the data of the input
     *            Raster is to be written.
     * @param inRaster
     *            the input Raster.
     */
    public void setDataElements(int x, int y, Raster inRaster) {
        int dstX = x + inRaster.getMinX();
        int dstY = y + inRaster.getMinY();

        int w = inRaster.getWidth();
        int h = inRaster.getHeight();

        if (dstX < this.minX || dstX + w > this.minX + this.width || dstY < this.minY
                || dstY + h > this.minY + this.height) {
            // awt.63=Coordinates are not in bounds
            throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$
        }

        int srcX = inRaster.getMinX();
        int srcY = inRaster.getMinY();
        Object line = null;

        for (int i = 0; i < h; i++) {
            line = inRaster.getDataElements(srcX, srcY + i, w, 1, line);
            setDataElements(dstX, dstY + i, w, 1, line);
        }
    }

    /**
     * Sets an integer array of samples for the specified pixel in this
     * WritableRaster.
     * 
     * @param x
     *            the pixel's X coordinate.
     * @param y
     *            the pixel's Y coordinate.
     * @param iArray
     *            the integer array of samples.
     */
    public void setPixel(int x, int y, int iArray[]) {
        sampleModel.setPixel(x - sampleModelTranslateX, y - sampleModelTranslateY, iArray,
                dataBuffer);
    }

    /**
     * Sets a float array of samples for the specified pixel in this
     * WritableRaster.
     * 
     * @param x
     *            the pixel's X coordinate.
     * @param y
     *            the pixel's Y coordinate.
     * @param fArray
     *            the float array of samples.
     */
    public void setPixel(int x, int y, float fArray[]) {
        sampleModel.setPixel(x - sampleModelTranslateX, y - sampleModelTranslateY, fArray,
                dataBuffer);
    }

    /**
     * Sets a double array of samples for the specified pixel in this
     * WritableRaster.
     * 
     * @param x
     *            the pixel's X coordinate.
     * @param y
     *            the pixel's Y coordinate.
     * @param dArray
     *            the double array of samples.
     */
    public void setPixel(int x, int y, double dArray[]) {
        sampleModel.setPixel(x - sampleModelTranslateX, y - sampleModelTranslateY, dArray,
                dataBuffer);
    }

    /**
     * Sets a integer array of samples for the specified rectangular area of
     * pixels in this WritableRaster.
     * 
     * @param x
     *            the X coordinate of rectangular area.
     * @param y
     *            the Y coordinate of rectangular area.
     * @param w
     *            the width of rectangular area.
     * @param h
     *            the height of rectangular area.
     * @param iArray
     *            the integer array of samples.
     */
    public void setPixels(int x, int y, int w, int h, int iArray[]) {
        sampleModel.setPixels(x - sampleModelTranslateX, y - sampleModelTranslateY, w, h, iArray,
                dataBuffer);
    }

    /**
     * Sets a float array of samples for the specified rectangular area of
     * pixels in this WritableRaster.
     * 
     * @param x
     *            the X coordinate of rectangular area.
     * @param y
     *            the Y coordinate of rectangular area.
     * @param w
     *            the width of rectangular area.
     * @param h
     *            the height of rectangular area.
     * @param fArray
     *            the float array of samples.
     */
    public void setPixels(int x, int y, int w, int h, float fArray[]) {
        sampleModel.setPixels(x - sampleModelTranslateX, y - sampleModelTranslateY, w, h, fArray,
                dataBuffer);
    }

    /**
     * Sets a double array of samples for the specified rectangular area of
     * pixels in this WritableRaster.
     * 
     * @param x
     *            the X coordinate of rectangular area.
     * @param y
     *            the Y coordinate of rectangular area.
     * @param w
     *            the width of rectangular area.
     * @param h
     *            the height of rectangular area.
     * @param dArray
     *            the double array of samples.
     */
    public void setPixels(int x, int y, int w, int h, double dArray[]) {
        sampleModel.setPixels(x - sampleModelTranslateX, y - sampleModelTranslateY, w, h, dArray,
                dataBuffer);
    }

    /**
     * Sets the samples for the specified band and the specified rectangular
     * area of pixels with an integer array of samples.
     * 
     * @param x
     *            the X coordinate of the area of pixels.
     * @param y
     *            the Y coordinate of the area of pixels.
     * @param w
     *            the width of the area of pixels.
     * @param h
     *            the height of the area of pixels.
     * @param b
     *            the specified band.
     * @param iArray
     *            the integer array of samples.
     */
    public void setSamples(int x, int y, int w, int h, int b, int iArray[]) {
        sampleModel.setSamples(x - sampleModelTranslateX, y - sampleModelTranslateY, w, h, b,
                iArray, dataBuffer);
    }

    /**
     * Sets the samples for the specified band and the specified rectangular
     * area of pixels with a float array of samples.
     * 
     * @param x
     *            the X coordinate of the area of pixels.
     * @param y
     *            the Y coordinate of the area of pixels.
     * @param w
     *            the width of the area of pixels.
     * @param h
     *            the height of the area of pixels.
     * @param b
     *            the specified band.
     * @param fArray
     *            the float array of samples.
     */
    public void setSamples(int x, int y, int w, int h, int b, float fArray[]) {
        sampleModel.setSamples(x - sampleModelTranslateX, y - sampleModelTranslateY, w, h, b,
                fArray, dataBuffer);
    }

    /**
     * Sets the samples for the specified band and the specified rectangular
     * area of pixels with a double array of samples.
     * 
     * @param x
     *            the X coordinate of the area of pixels.
     * @param y
     *            the Y coordinate of the area of pixels.
     * @param w
     *            the width of the area of pixels.
     * @param h
     *            the height of the area of pixels.
     * @param b
     *            the specified band.
     * @param dArray
     *            the double array of samples.
     */
    public void setSamples(int x, int y, int w, int h, int b, double dArray[]) {
        sampleModel.setSamples(x - sampleModelTranslateX, y - sampleModelTranslateY, w, h, b,
                dArray, dataBuffer);
    }

    /**
     * Sets the sample for the specified band and the specified pixel with an
     * integer sample.
     * 
     * @param x
     *            the X coordinate of the pixel.
     * @param y
     *            the Y coordinate of the pixel.
     * @param b
     *            the specified band.
     * @param s
     *            the sample to be set.
     */
    public void setSample(int x, int y, int b, int s) {
        sampleModel.setSample(x - sampleModelTranslateX, y - sampleModelTranslateY, b, s,
                dataBuffer);
    }

    /**
     * Sets the sample for the specified band and the specified pixel with a
     * float sample.
     * 
     * @param x
     *            the X coordinate of the pixel.
     * @param y
     *            the Y coordinate of the pixel.
     * @param b
     *            the specified band.
     * @param s
     *            the sample to be set.
     */
    public void setSample(int x, int y, int b, float s) {
        sampleModel.setSample(x - sampleModelTranslateX, y - sampleModelTranslateY, b, s,
                dataBuffer);
    }

    /**
     * Sets the sample for the specified band and the specified pixel with an
     * integer sample.
     * 
     * @param x
     *            the X coordinate of the pixel.
     * @param y
     *            the Y coordinate of the pixel.
     * @param b
     *            the specified band.
     * @param s
     *            the sample to be set.
     */
    public void setSample(int x, int y, int b, double s) {
        sampleModel.setSample(x - sampleModelTranslateX, y - sampleModelTranslateY, b, s,
                dataBuffer);
    }

}