View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.palette;
18  
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.apache.commons.imaging.ImagingException;
23  import org.apache.commons.imaging.common.Allocator;
24  
25  public class QuantizedPalette implements Palette {
26  
27      private final int precision;
28      private final List<ColorSpaceSubset> subsets;
29      private final ColorSpaceSubset[] straight;
30  
31      public QuantizedPalette(final List<ColorSpaceSubset> subsets, final int precision) {
32          this.subsets = subsets == null ? Collections.emptyList() : Collections.unmodifiableList(subsets);
33          this.precision = precision;
34  
35          straight = Allocator.array(1 << precision * 3, ColorSpaceSubset[]::new, ColorSpaceSubset.SHALLOW_SIZE);
36  
37          for (int i = 0; i < this.subsets.size(); i++) {
38              final ColorSpaceSubset subset = this.subsets.get(i);
39              subset.setIndex(i);
40  
41              for (int u = subset.mins[0]; u <= subset.maxs[0]; u++) {
42                  for (int j = subset.mins[1]; j <= subset.maxs[1]; j++) {
43                      for (int k = subset.mins[2]; k <= subset.maxs[2]; k++) {
44                          final int index = u << precision * 2 | j << precision * 1 | k << precision * 0;
45                          straight[index] = subset;
46                      }
47                  }
48              }
49          }
50      }
51  
52      @Override
53      public int getEntry(final int index) {
54          final ColorSpaceSubset subset = subsets.get(index);
55          return subset.rgb;
56      }
57  
58      @Override
59      public int getPaletteIndex(final int rgb) throws ImagingException {
60          final int precisionMask = (1 << precision) - 1;
61  
62          final int index = rgb >> 24 - 3 * precision & precisionMask << (precision << 1) | rgb >> 16 - 2 * precision & precisionMask << precision
63                  | rgb >> 8 - precision & precisionMask;
64  
65          return straight[index].getIndex();
66      }
67  
68      @Override
69      public int length() {
70          return subsets.size();
71      }
72  }