    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.imaging.palette;
    018
    019import java.util.List;
    020import org.apache.commons.imaging.ImageWriteException;
    021
    022public class QuantizedPalette implements Palette {
    023    private final int precision;
    024    private final List<ColorSpaceSubset> subsets;
    025    private final ColorSpaceSubset[] straight;
    026
    027    public QuantizedPalette(final List<ColorSpaceSubset> subsets, final int precision) {
    028        this.subsets = subsets;
    029        this.precision = precision;
    030
    031        straight = new ColorSpaceSubset[1 << (precision * 3)];
    032
    033        for (int i = 0; i < subsets.size(); i++) {
    034            final ColorSpaceSubset subset = subsets.get(i);
    035            subset.setIndex(i);
    036
    037            for (int u = subset.mins[0]; u <= subset.maxs[0]; u++) {
    038                for (int j = subset.mins[1]; j <= subset.maxs[1]; j++) {
    039                    for (int k = subset.mins[2]; k <= subset.maxs[2]; k++) {
    040                        final int index = (u << (precision * 2))
    041                                | (j << (precision * 1))
    042                                | (k << (precision * 0));
    043                        straight[index] = subset;
    044                    }
    045                }
    046            }
    047        }
    048    }
    049
    050    @Override
    051    public int getPaletteIndex(final int rgb) throws ImageWriteException {
    052        final int precisionMask = (1 << precision) - 1;
    053
    054        final int index = ((rgb >> (24 - 3 * precision)) & (precisionMask << (precision << 1)))
    055                | ((rgb >> (16 - 2 * precision)) & (precisionMask << precision))
    056                | ((rgb >> (8 - precision)) & (precisionMask));
    057
    058        return straight[index].getIndex();
    059    }
    060
    061    @Override
    062    public int getEntry(final int index) {
    063        final ColorSpaceSubset subset = subsets.get(index);
    064        return subset.rgb;
    065    }
    066
    067    @Override
    068    public int length() {
    069        return subsets.size();
    070    }
    071}