    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.Collections;
    020import java.util.List;
    021
    022import org.apache.commons.imaging.ImagingException;
    023import org.apache.commons.imaging.common.Allocator;
    024
    025public class QuantizedPalette implements Palette {
    026
    027    private final int precision;
    028    private final List<ColorSpaceSubset> subsets;
    029    private final ColorSpaceSubset[] straight;
    030
    031    public QuantizedPalette(final List<ColorSpaceSubset> subsets, final int precision) {
    032        this.subsets = subsets == null ? Collections.emptyList() : Collections.unmodifiableList(subsets);
    033        this.precision = precision;
    034
    035        straight = Allocator.array(1 << precision * 3, ColorSpaceSubset[]::new, ColorSpaceSubset.SHALLOW_SIZE);
    036
    037        for (int i = 0; i < this.subsets.size(); i++) {
    038            final ColorSpaceSubset subset = this.subsets.get(i);
    039            subset.setIndex(i);
    040
    041            for (int u = subset.mins[0]; u <= subset.maxs[0]; u++) {
    042                for (int j = subset.mins[1]; j <= subset.maxs[1]; j++) {
    043                    for (int k = subset.mins[2]; k <= subset.maxs[2]; k++) {
    044                        final int index = u << precision * 2 | j << precision * 1 | k << precision * 0;
    045                        straight[index] = subset;
    046                    }
    047                }
    048            }
    049        }
    050    }
    051
    052    @Override
    053    public int getEntry(final int index) {
    054        final ColorSpaceSubset subset = subsets.get(index);
    055        return subset.rgb;
    056    }
    057
    058    @Override
    059    public int getPaletteIndex(final int rgb) throws ImagingException {
    060        final int precisionMask = (1 << precision) - 1;
    061
    062        final int index = rgb >> 24 - 3 * precision & precisionMask << (precision << 1) | rgb >> 16 - 2 * precision & precisionMask << precision
    063                | rgb >> 8 - precision & precisionMask;
    064
    065        return straight[index].getIndex();
    066    }
    067
    068    @Override
    069    public int length() {
    070        return subsets.size();
    071    }
    072}