src/java.desktop/share/classes/java/awt/image/ByteLookupTable.java
author tvaleev
Thu, 04 Oct 2018 12:40:55 -0700
changeset 52248 2e330da7cbf4
parent 47216 71c04702a3d5
permissions -rw-r--r--
8211300: Convert C-style array declarations in JDK client code Reviewed-by: prr, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * This class defines a lookup table object.  The output of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * lookup operation using an object of this class is interpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * as an unsigned byte quantity.  The lookup table contains byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * data arrays for one or more bands (or components) of an image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * and it contains an offset which will be subtracted from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * input values before indexing the arrays.  This allows an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * smaller than the native data size to be provided for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * constrained input.  If there is only one array in the lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * table, it will be applied to all bands.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see ShortLookupTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see LookupOp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class ByteLookupTable extends LookupTable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
    49
    byte[][] data;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Constructs a ByteLookupTable object from an array of byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * arrays representing a lookup table for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * band.  The offset will be subtracted from input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * values before indexing into the arrays.  The number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * bands is the length of the data argument.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * data array for each band is stored as a reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param offset the value subtracted from the input values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *        before indexing into the arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param data an array of byte arrays representing a lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *        table for each band
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    62
     * @throws IllegalArgumentException if {@code offset} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    63
     *         is less than 0 or if the length of {@code data}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *         is less than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
    66
    public ByteLookupTable(int offset, byte[][] data) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        super(offset,data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        numComponents = data.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        numEntries    = data[0].length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.data = new byte[numComponents][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        // Allocate the array and copy the data reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        for (int i=0; i < numComponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            this.data[i] = data[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Constructs a ByteLookupTable object from an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * of bytes representing a lookup table to be applied to all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * bands.  The offset will be subtracted from input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * values before indexing into the array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The data array is stored as a reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param offset the value subtracted from the input values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *        before indexing into the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param data an array of bytes
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    86
     * @throws IllegalArgumentException if {@code offset} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    87
     *         is less than 0 or if the length of {@code data}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *         is less than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 47216
diff changeset
    90
    public ByteLookupTable(int offset, byte[] data) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        super(offset,data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        numComponents = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        numEntries    = data.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.data = new byte[1][];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.data[0] = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Returns the lookup table data by reference.  If this ByteLookupTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * was constructed using a single byte array, the length of the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * array is one.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   102
     * @return the data array of this {@code ByteLookupTable}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public final byte[][] getTable(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Returns an array of samples of a pixel, translated with the lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * table. The source and destination array can be the same array.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   111
     * Array {@code dst} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param src the source array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param dst the destination array. This array must be at least as
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   115
     *         long as {@code src}.  If {@code dst} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   116
     *         {@code null}, a new array will be allocated having the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   117
     *         same length as {@code src}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   118
     * @return the array {@code dst}, an {@code int} array of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *         samples.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   120
     * @exception ArrayIndexOutOfBoundsException if {@code src} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   121
     *            longer than {@code dst} or if for any element
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   122
     *            {@code i} of {@code src},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   123
     *            {@code src[i]-offset} is either less than zero or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *            greater than or equal to the length of the lookup table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *            for any band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public int[] lookupPixel(int[] src, int[] dst){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (dst == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // Need to alloc a new destination array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            dst = new int[src.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (numComponents == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // Apply one LUT to all bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            for (int i=0; i < src.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                int s = src[i] - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                if (s < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    throw new ArrayIndexOutOfBoundsException("src["+i+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                                             "]-offset is "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                                             "less than zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                dst[i] = (int) data[0][s];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            for (int i=0; i < src.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                int s = src[i] - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                if (s < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    throw new ArrayIndexOutOfBoundsException("src["+i+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                                             "]-offset is "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                                             "less than zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                dst[i] = (int) data[i][s];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Returns an array of samples of a pixel, translated with the lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * table. The source and destination array can be the same array.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   162
     * Array {@code dst} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param src the source array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param dst the destination array. This array must be at least as
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   166
     *         long as {@code src}.  If {@code dst} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   167
     *         {@code null}, a new array will be allocated having the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   168
     *         same length as {@code src}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   169
     * @return the array {@code dst}, an {@code int} array of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *         samples.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   171
     * @exception ArrayIndexOutOfBoundsException if {@code src} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   172
     *            longer than {@code dst} or if for any element
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   173
     *            {@code i} of {@code src},
19169
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 5506
diff changeset
   174
     *            {@code (src[i]&0xff)-offset} is either less than
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *            zero or greater than or equal to the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *            lookup table for any band.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public byte[] lookupPixel(byte[] src, byte[] dst){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (dst == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            // Need to alloc a new destination array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            dst = new byte[src.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (numComponents == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            // Apply one LUT to all bands
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            for (int i=0; i < src.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                int s = (src[i]&0xff) - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                if (s < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    throw new ArrayIndexOutOfBoundsException("src["+i+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                                             "]-offset is "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                                             "less than zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                dst[i] = data[0][s];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            for (int i=0; i < src.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                int s = (src[i]&0xff) - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                if (s < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    throw new ArrayIndexOutOfBoundsException("src["+i+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                                             "]-offset is "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                                             "less than zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                dst[i] = data[i][s];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
}