1 /* 2 * Copyright 2021 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.ux.material.libmonet.quantize; 18 19 import java.util.LinkedHashMap; 20 import java.util.Map; 21 22 /** Creates a dictionary with keys of colors, and values of count of the color */ 23 public final class QuantizerMap implements Quantizer { 24 Map<Integer, Integer> colorToCount; 25 26 @Override quantize(int[] pixels, int colorCount)27 public QuantizerResult quantize(int[] pixels, int colorCount) { 28 final Map<Integer, Integer> pixelByCount = new LinkedHashMap<>(); 29 for (int pixel : pixels) { 30 final Integer currentPixelCount = pixelByCount.get(pixel); 31 final int newPixelCount = currentPixelCount == null ? 1 : currentPixelCount + 1; 32 pixelByCount.put(pixel, newPixelCount); 33 } 34 colorToCount = pixelByCount; 35 return new QuantizerResult(pixelByCount); 36 } 37 getColorToCount()38 public Map<Integer, Integer> getColorToCount() { 39 return colorToCount; 40 } 41 } 42