120 * specification, table K.1, with all elements divided by 2. 121 * According to the specification, these values produce "very good" 122 * quality output. This is the table usually used for "visually lossless" 123 * encoding, and is the default chrominance table used if the default 124 * tables and quality settings are used. 125 * @see #K2Chrominance 126 */ 127 public static final JPEGQTable K2Div2Chrominance = 128 new JPEGQTable(k2div2, false); 129 130 private int[] qTable; 131 132 private JPEGQTable(int[] table, boolean copy) { 133 qTable = (copy) ? Arrays.copyOf(table, table.length) : table; 134 } 135 136 /** 137 * Constructs a quantization table from the argument, which must 138 * contain 64 elements in natural order (not zig-zag order). 139 * A copy is made of the input array. 140 * @param table the quantization table, as an <code>int</code> array. 141 * @throws IllegalArgumentException if <code>table</code> is 142 * <code>null</code> or <code>table.length</code> is not equal to 64. 143 */ 144 public JPEGQTable(int[] table) { 145 if (table == null) { 146 throw new IllegalArgumentException("table must not be null."); 147 } 148 if (table.length != 64) { 149 throw new IllegalArgumentException("table.length != 64"); 150 } 151 qTable = Arrays.copyOf(table, table.length); 152 } 153 154 /** 155 * Returns a copy of the current quantization table as an array 156 * of {@code int}s in natural (not zig-zag) order. 157 * @return A copy of the current quantization table. 158 */ 159 public int[] getTable() { 160 return Arrays.copyOf(qTable, qTable.length); 161 } 162 163 /** 164 * Returns a new quantization table where the values are multiplied 165 * by <code>scaleFactor</code> and then clamped to the range 1..32767 166 * (or to 1..255 if <code>forceBaseline</code> is true). 167 * <p> 168 * Values of <code>scaleFactor</code> less than 1 tend to improve 169 * the quality level of the table, and values greater than 1.0 170 * degrade the quality level of the table. 171 * @param scaleFactor multiplication factor for the table. 172 * @param forceBaseline if <code>true</code>, 173 * the values will be clamped to the range 1..255 174 * @return a new quantization table that is a linear multiple 175 * of the current table. 176 */ 177 public JPEGQTable getScaledInstance(float scaleFactor, 178 boolean forceBaseline) { 179 int max = (forceBaseline) ? 255 : 32767; 180 int[] scaledTable = new int[qTable.length]; 181 for (int i=0; i<qTable.length; i++) { 182 int sv = (int)((qTable[i] * scaleFactor)+0.5f); 183 if (sv < 1) { 184 sv = 1; 185 } 186 if (sv > max) { 187 sv = max; 188 } 189 scaledTable[i] = sv; 190 } 191 return new JPEGQTable(scaledTable); 192 } | 120 * specification, table K.1, with all elements divided by 2. 121 * According to the specification, these values produce "very good" 122 * quality output. This is the table usually used for "visually lossless" 123 * encoding, and is the default chrominance table used if the default 124 * tables and quality settings are used. 125 * @see #K2Chrominance 126 */ 127 public static final JPEGQTable K2Div2Chrominance = 128 new JPEGQTable(k2div2, false); 129 130 private int[] qTable; 131 132 private JPEGQTable(int[] table, boolean copy) { 133 qTable = (copy) ? Arrays.copyOf(table, table.length) : table; 134 } 135 136 /** 137 * Constructs a quantization table from the argument, which must 138 * contain 64 elements in natural order (not zig-zag order). 139 * A copy is made of the input array. 140 * @param table the quantization table, as an {@code int} array. 141 * @throws IllegalArgumentException if {@code table} is 142 * {@code null} or {@code table.length} is not equal to 64. 143 */ 144 public JPEGQTable(int[] table) { 145 if (table == null) { 146 throw new IllegalArgumentException("table must not be null."); 147 } 148 if (table.length != 64) { 149 throw new IllegalArgumentException("table.length != 64"); 150 } 151 qTable = Arrays.copyOf(table, table.length); 152 } 153 154 /** 155 * Returns a copy of the current quantization table as an array 156 * of {@code int}s in natural (not zig-zag) order. 157 * @return A copy of the current quantization table. 158 */ 159 public int[] getTable() { 160 return Arrays.copyOf(qTable, qTable.length); 161 } 162 163 /** 164 * Returns a new quantization table where the values are multiplied 165 * by {@code scaleFactor} and then clamped to the range 1..32767 166 * (or to 1..255 if {@code forceBaseline} is true). 167 * <p> 168 * Values of {@code scaleFactor} less than 1 tend to improve 169 * the quality level of the table, and values greater than 1.0 170 * degrade the quality level of the table. 171 * @param scaleFactor multiplication factor for the table. 172 * @param forceBaseline if {@code true}, 173 * the values will be clamped to the range 1..255 174 * @return a new quantization table that is a linear multiple 175 * of the current table. 176 */ 177 public JPEGQTable getScaledInstance(float scaleFactor, 178 boolean forceBaseline) { 179 int max = (forceBaseline) ? 255 : 32767; 180 int[] scaledTable = new int[qTable.length]; 181 for (int i=0; i<qTable.length; i++) { 182 int sv = (int)((qTable[i] * scaleFactor)+0.5f); 183 if (sv < 1) { 184 sv = 1; 185 } 186 if (sv > max) { 187 sv = max; 188 } 189 scaledTable[i] = sv; 190 } 191 return new JPEGQTable(scaledTable); 192 } |