jdk/src/java.desktop/share/classes/javax/imageio/plugins/jpeg/JPEGQTable.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
child 35667 ed476aba94de
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, 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 javax.imageio.plugins.jpeg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A class encapsulating a single JPEG quantization table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * The elements appear in natural order (as opposed to zig-zag order).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Static variables are provided for the "standard" tables taken from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *  Annex K of the JPEG specification, as well as the default tables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * conventionally used for visually lossless encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * For more information about the operation of the standard JPEG plug-in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * see the <A HREF="../../metadata/doc-files/jpeg_metadata.html">JPEG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * metadata format specification and usage notes</A>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class JPEGQTable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final int[] k1 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        16,  11,  10,  16,  24,  40,  51,  61,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        12,  12,  14,  19,  26,  58,  60,  55,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        14,  13,  16,  24,  40,  57,  69,  56,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        14,  17,  22,  29,  51,  87,  80,  62,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        18,  22,  37,  56,  68,  109, 103, 77,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        24,  35,  55,  64,  81,  104, 113, 92,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        49,  64,  78,  87,  103, 121, 120, 101,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        72,  92,  95,  98,  112, 100, 103, 99,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final int[] k1div2 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        8,   6,   5,   8,   12,  20,  26,  31,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        6,   6,   7,   10,  13,  29,  30,  28,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        7,   7,   8,   12,  20,  29,  35,  28,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        7,   9,   11,  15,  26,  44,  40,  31,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        9,   11,  19,  28,  34,  55,  52,  39,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        12,  18,  28,  32,  41,  52,  57,  46,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        25,  32,  39,  44,  52,  61,  60,  51,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        36,  46,  48,  49,  56,  50,  52,  50,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final int[] k2 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        17,  18,  24,  47,  99,  99,  99,  99,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        18,  21,  26,  66,  99,  99,  99,  99,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        24,  26,  56,  99,  99,  99,  99,  99,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        47,  66,  99,  99,  99,  99,  99,  99,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        99,  99,  99,  99,  99,  99,  99,  99,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        99,  99,  99,  99,  99,  99,  99,  99,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        99,  99,  99,  99,  99,  99,  99,  99,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        99,  99,  99,  99,  99,  99,  99,  99,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final int[] k2div2 = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        9,   9,   12,  24,  50,  50,  50,  50,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        9,   11,  13,  33,  50,  50,  50,  50,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        12,  13,  28,  50,  50,  50,  50,  50,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        24,  33,  50,  50,  50,  50,  50,  50,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        50,  50,  50,  50,  50,  50,  50,  50,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        50,  50,  50,  50,  50,  50,  50,  50,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        50,  50,  50,  50,  50,  50,  50,  50,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        50,  50,  50,  50,  50,  50,  50,  50,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * The sample luminance quantization table given in the JPEG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * specification, table K.1. According to the specification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * these values produce "good" quality output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @see #K1Div2Luminance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static final JPEGQTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        K1Luminance = new JPEGQTable(k1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * The sample luminance quantization table given in the JPEG
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * specification, table K.1, with all elements divided by 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * According to the specification, these values produce "very good"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * quality output. This is the table usually used for "visually lossless"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * encoding, and is the default luminance table used if the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * tables and quality settings are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @see #K1Luminance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public static final JPEGQTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        K1Div2Luminance = new JPEGQTable(k1div2, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * The sample chrominance quantization table given in the JPEG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * specification, table K.2. According to the specification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * these values produce "good" quality output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @see #K2Div2Chrominance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public static final JPEGQTable K2Chrominance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        new JPEGQTable(k2, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * The sample chrominance quantization table given in the JPEG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * specification, table K.1, with all elements divided by 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * According to the specification, these values produce "very good"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * quality output. This is the table usually used for "visually lossless"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * encoding, and is the default chrominance table used if the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * tables and quality settings are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see #K2Chrominance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public static final JPEGQTable K2Div2Chrominance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        new JPEGQTable(k2div2, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private int[] qTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private JPEGQTable(int[] table, boolean copy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        qTable = (copy) ? Arrays.copyOf(table, table.length) : table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Constructs a quantization table from the argument, which must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * contain 64 elements in natural order (not zig-zag order).
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   139
     * A copy is made of the input array.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param table the quantization table, as an <code>int</code> array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @throws IllegalArgumentException if <code>table</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <code>null</code> or <code>table.length</code> is not equal to 64.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public JPEGQTable(int[] table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (table == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new IllegalArgumentException("table must not be null.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (table.length != 64) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new IllegalArgumentException("table.length != 64");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        qTable = Arrays.copyOf(table, table.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Returns a copy of the current quantization table as an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * of {@code int}s in natural (not zig-zag) order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return A copy of the current quantization table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public int[] getTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return Arrays.copyOf(qTable, qTable.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Returns a new quantization table where the values are multiplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * by <code>scaleFactor</code> and then clamped to the range 1..32767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * (or to 1..255 if <code>forceBaseline</code> is true).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Values of <code>scaleFactor</code> less than 1 tend to improve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * the quality level of the table, and values greater than 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * degrade the quality level of the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param scaleFactor multiplication factor for the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param forceBaseline if <code>true</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * the values will be clamped to the range 1..255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return a new quantization table that is a linear multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * of the current table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public JPEGQTable getScaledInstance(float scaleFactor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                        boolean forceBaseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        int max = (forceBaseline) ? 255 : 32767;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        int[] scaledTable = new int[qTable.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        for (int i=0; i<qTable.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            int sv = (int)((qTable[i] * scaleFactor)+0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (sv < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                sv = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (sv > max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                sv = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            scaledTable[i] = sv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return new JPEGQTable(scaledTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Returns a {@code String} representing this quantization table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @return a {@code String} representing this quantization table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        String ls = System.getProperty("line.separator", "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        StringBuilder sb = new StringBuilder("JPEGQTable:"+ls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        for (int i=0; i < qTable.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (i % 8 == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                sb.append('\t');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            sb.append(qTable[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            sb.append(((i % 8) == 7) ? ls : ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
}