xref: /aosp_15_r20/external/libmonet/quantize/QuantizerMap.java (revision 970e10460f970939fd510dd6ad3e0d65908272e3)
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.LinkedHashMap;
20*970e1046SAndroid Build Coastguard Worker import java.util.Map;
21*970e1046SAndroid Build Coastguard Worker 
22*970e1046SAndroid Build Coastguard Worker /** Creates a dictionary with keys of colors, and values of count of the color */
23*970e1046SAndroid Build Coastguard Worker public final class QuantizerMap implements Quantizer {
24*970e1046SAndroid Build Coastguard Worker   Map<Integer, Integer> colorToCount;
25*970e1046SAndroid Build Coastguard Worker 
26*970e1046SAndroid Build Coastguard Worker   @Override
quantize(int[] pixels, int colorCount)27*970e1046SAndroid Build Coastguard Worker   public QuantizerResult quantize(int[] pixels, int colorCount) {
28*970e1046SAndroid Build Coastguard Worker     final Map<Integer, Integer> pixelByCount = new LinkedHashMap<>();
29*970e1046SAndroid Build Coastguard Worker     for (int pixel : pixels) {
30*970e1046SAndroid Build Coastguard Worker       final Integer currentPixelCount = pixelByCount.get(pixel);
31*970e1046SAndroid Build Coastguard Worker       final int newPixelCount = currentPixelCount == null ? 1 : currentPixelCount + 1;
32*970e1046SAndroid Build Coastguard Worker       pixelByCount.put(pixel, newPixelCount);
33*970e1046SAndroid Build Coastguard Worker     }
34*970e1046SAndroid Build Coastguard Worker     colorToCount = pixelByCount;
35*970e1046SAndroid Build Coastguard Worker     return new QuantizerResult(pixelByCount);
36*970e1046SAndroid Build Coastguard Worker   }
37*970e1046SAndroid Build Coastguard Worker 
getColorToCount()38*970e1046SAndroid Build Coastguard Worker   public Map<Integer, Integer> getColorToCount() {
39*970e1046SAndroid Build Coastguard Worker     return colorToCount;
40*970e1046SAndroid Build Coastguard Worker   }
41*970e1046SAndroid Build Coastguard Worker }
42