1*970e1046SAndroid Build Coastguard Worker /* 2*970e1046SAndroid Build Coastguard Worker * Copyright 2021 Google LLC 3*970e1046SAndroid Build Coastguard Worker * 4*970e1046SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*970e1046SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*970e1046SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*970e1046SAndroid Build Coastguard Worker * 8*970e1046SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*970e1046SAndroid Build Coastguard Worker * 10*970e1046SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*970e1046SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*970e1046SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*970e1046SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*970e1046SAndroid Build Coastguard Worker * limitations under the License. 15*970e1046SAndroid Build Coastguard Worker */ 16*970e1046SAndroid Build Coastguard Worker 17*970e1046SAndroid Build Coastguard Worker package com.google.ux.material.libmonet.quantize; 18*970e1046SAndroid Build Coastguard Worker 19*970e1046SAndroid Build Coastguard Worker import java.util.Map; 20*970e1046SAndroid Build Coastguard Worker import java.util.Set; 21*970e1046SAndroid Build Coastguard Worker 22*970e1046SAndroid Build Coastguard Worker /** 23*970e1046SAndroid Build Coastguard Worker * An image quantizer that improves on the quality of a standard K-Means algorithm by setting the 24*970e1046SAndroid Build Coastguard Worker * K-Means initial state to the output of a Wu quantizer, instead of random centroids. Improves on 25*970e1046SAndroid Build Coastguard Worker * speed by several optimizations, as implemented in Wsmeans, or Weighted Square Means, K-Means with 26*970e1046SAndroid Build Coastguard Worker * those optimizations. 27*970e1046SAndroid Build Coastguard Worker * 28*970e1046SAndroid Build Coastguard Worker * <p>This algorithm was designed by M. Emre Celebi, and was found in their 2011 paper, Improving 29*970e1046SAndroid Build Coastguard Worker * the Performance of K-Means for Color Quantization. https://arxiv.org/abs/1101.0395 30*970e1046SAndroid Build Coastguard Worker */ 31*970e1046SAndroid Build Coastguard Worker public final class QuantizerCelebi { QuantizerCelebi()32*970e1046SAndroid Build Coastguard Worker private QuantizerCelebi() {} 33*970e1046SAndroid Build Coastguard Worker 34*970e1046SAndroid Build Coastguard Worker /** 35*970e1046SAndroid Build Coastguard Worker * Reduce the number of colors needed to represented the input, minimizing the difference between 36*970e1046SAndroid Build Coastguard Worker * the original image and the recolored image. 37*970e1046SAndroid Build Coastguard Worker * 38*970e1046SAndroid Build Coastguard Worker * @param pixels Colors in ARGB format. 39*970e1046SAndroid Build Coastguard Worker * @param maxColors The number of colors to divide the image into. A lower number of colors may be 40*970e1046SAndroid Build Coastguard Worker * returned. 41*970e1046SAndroid Build Coastguard Worker * @return Map with keys of colors in ARGB format, and values of number of pixels in the original 42*970e1046SAndroid Build Coastguard Worker * image that correspond to the color in the quantized image. 43*970e1046SAndroid Build Coastguard Worker */ quantize(int[] pixels, int maxColors)44*970e1046SAndroid Build Coastguard Worker public static Map<Integer, Integer> quantize(int[] pixels, int maxColors) { 45*970e1046SAndroid Build Coastguard Worker QuantizerWu wu = new QuantizerWu(); 46*970e1046SAndroid Build Coastguard Worker QuantizerResult wuResult = wu.quantize(pixels, maxColors); 47*970e1046SAndroid Build Coastguard Worker 48*970e1046SAndroid Build Coastguard Worker Set<Integer> wuClustersAsObjects = wuResult.colorToCount.keySet(); 49*970e1046SAndroid Build Coastguard Worker int index = 0; 50*970e1046SAndroid Build Coastguard Worker int[] wuClusters = new int[wuClustersAsObjects.size()]; 51*970e1046SAndroid Build Coastguard Worker for (Integer argb : wuClustersAsObjects) { 52*970e1046SAndroid Build Coastguard Worker wuClusters[index++] = argb; 53*970e1046SAndroid Build Coastguard Worker } 54*970e1046SAndroid Build Coastguard Worker 55*970e1046SAndroid Build Coastguard Worker return QuantizerWsmeans.quantize(pixels, wuClusters, maxColors); 56*970e1046SAndroid Build Coastguard Worker } 57*970e1046SAndroid Build Coastguard Worker } 58