Files

41 lines
1.4 KiB
Java
Raw Permalink Normal View History

2021-10-14 17:07:54 +00:00
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package quantize;
2022-11-18 08:47:19 -08:00
import java.util.LinkedHashMap;
2021-10-14 17:07:54 +00:00
import java.util.Map;
/** Creates a dictionary with keys of colors, and values of count of the color */
public final class QuantizerMap implements Quantizer {
Map<Integer, Integer> colorToCount;
@Override
public QuantizerResult quantize(int[] pixels, int colorCount) {
2022-11-18 08:47:19 -08:00
final Map<Integer, Integer> pixelByCount = new LinkedHashMap<>();
2021-10-14 17:07:54 +00:00
for (int pixel : pixels) {
final Integer currentPixelCount = pixelByCount.get(pixel);
final int newPixelCount = currentPixelCount == null ? 1 : currentPixelCount + 1;
pixelByCount.put(pixel, newPixelCount);
2021-10-14 17:07:54 +00:00
}
colorToCount = pixelByCount;
return new QuantizerResult(pixelByCount);
}
public Map<Integer, Integer> getColorToCount() {
return colorToCount;
}
}