summaryrefslogtreecommitdiffstats
path: root/awt/java/awt/image/PixelInterleavedSampleModel.java
blob: 8e646f80b9d769916f979bd6c1f90ca1d02f23ca (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
/*
 *  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 org.apache.harmony.awt.internal.nls.Messages;

/**
 * The PixelInterleavedSampleModel class represents image data as represented as
 * interleaved pixels and for which each sample of a pixel takes one data
 * element of the DataBuffer.
 * 
 * @since Android 1.0
 */
public class PixelInterleavedSampleModel extends ComponentSampleModel {

    /**
     * Instantiates a new PixelInterleavedSampleModel with the specified
     * parameters.
     * 
     * @param dataType
     *            the data type of the samples.
     * @param w
     *            the width of the image data.
     * @param h
     *            the height of the image data.
     * @param pixelStride
     *            the pixel stride of the image data.
     * @param scanlineStride
     *            the scanline stride of the of the image data.
     * @param bandOffsets
     *            the array of the band offsets.
     */
    public PixelInterleavedSampleModel(int dataType, int w, int h, int pixelStride,
            int scanlineStride, int bandOffsets[]) {

        super(dataType, w, h, pixelStride, scanlineStride, bandOffsets);

        int maxOffset = bandOffsets[0];
        int minOffset = bandOffsets[0];
        for (int i = 1; i < bandOffsets.length; i++) {
            if (bandOffsets[i] > maxOffset) {
                maxOffset = bandOffsets[i];
            }
            if (bandOffsets[i] < minOffset) {
                minOffset = bandOffsets[i];
            }
        }

        maxOffset -= minOffset;

        if (maxOffset > scanlineStride) {
            // awt.241=Any offset between bands is greater than the Scanline
            // stride
            throw new IllegalArgumentException(Messages.getString("awt.241")); //$NON-NLS-1$
        }

        if (maxOffset > pixelStride) {
            // awt.242=Pixel stride is less than any offset between bands
            throw new IllegalArgumentException(Messages.getString("awt.242")); //$NON-NLS-1$
        }

        if (pixelStride * w > scanlineStride) {
            // awt.243=Product of Pixel stride and w is greater than Scanline
            // stride
            throw new IllegalArgumentException(Messages.getString("awt.243")); //$NON-NLS-1$
        }

    }

    @Override
    public SampleModel createSubsetSampleModel(int bands[]) {
        int newOffsets[] = new int[bands.length];
        for (int i = 0; i < bands.length; i++) {
            newOffsets[i] = bandOffsets[bands[i]];
        }

        return new PixelInterleavedSampleModel(dataType, width, height, pixelStride,
                scanlineStride, newOffsets);
    }

    @Override
    public SampleModel createCompatibleSampleModel(int w, int h) {
        int newOffsets[];
        int minOffset = bandOffsets[0];

        for (int i = 1; i < numBands; i++) {
            if (bandOffsets[i] < minOffset) {
                minOffset = bandOffsets[i];
            }
        }

        if (minOffset > 0) {
            newOffsets = new int[numBands];
            for (int i = 0; i < numBands; i++) {
                newOffsets[i] = bandOffsets[i] - minOffset;
            }
        } else {
            newOffsets = bandOffsets;
        }

        return new PixelInterleavedSampleModel(dataType, w, h, pixelStride, pixelStride * w,
                newOffsets);
    }

    @Override
    public int hashCode() {
        int hash = super.hashCode();
        int tmp = hash >>> 8;
        hash <<= 8;
        hash |= tmp;

        return hash ^ 0x66;
    }

}