[NUI] Introduce NUI Palette APIs
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Utility / ColorHistogram.cs
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
18 /*
19  * Copyright (C) 2017 The Android Open Source Project
20  *
21  * Modified by Woochan Lee(wc0917.lee@samsung.com)
22  */
23
24 using System;
25
26 namespace Tizen.NUI
27 {
28     internal sealed class ColorHistogram
29     {
30         private int numberColors;
31         private int[] colors;
32         private int[] colorCounts;
33
34         /// <summary>
35         /// A new ColorHistogram instance.
36         /// </summary>
37         public ColorHistogram(int[] pixels)
38         {
39             // Sort the pixels to enable counting below
40             Array.Sort(pixels);
41
42             // Count number of distinct colors
43             numberColors = CountDistinctColors(pixels);
44             Tizen.Log.Info("Palette", "DistinctColor Num = " + numberColors + "\n");
45
46             // Create arrays
47             colors = new int[numberColors];
48             colorCounts = new int[numberColors];
49
50             // Finally count the frequency of each color
51             CountFrequencies(pixels);
52         }
53
54         /// <summary>
55         /// return number of distinct colors in the image.
56         /// </summary>
57         public int GetNumberOfColors()
58         {
59             return numberColors;
60         }
61
62         /// <summary>
63         /// return an array containing all of the distinct colors in the image.
64         /// </summary>
65         public int[] GetColors()
66         {
67             return colors;
68         }
69
70         /// <summary>
71         /// return an array containing the frequency of a distinct colors within the image.
72         /// </summary>
73         public int[] GetColorCounts()
74         {
75             return colorCounts;
76         }
77
78         private static int CountDistinctColors(int[] pixels)
79         {
80             if (pixels.Length < 2)
81             {
82                 // If we have less than 2 pixels we can stop here
83                 return pixels.Length;
84             }
85             // If we have at least 2 pixels, we have a minimum of 1 color...
86             int colorCount = 1;
87             int currentColor = pixels[0];
88
89             // Now iterate from the second pixel to the end, counting distinct colors
90             for (int i = 1; i < pixels.Length; i++)
91             {
92                 // If we encounter a new color, increase the population
93                 if (pixels[i] != currentColor)
94                 {
95                     currentColor = pixels[i];
96                     colorCount++;
97                 }
98             }
99
100             return colorCount;
101         }
102
103         private void CountFrequencies(int[] pixels)
104         {
105             if (pixels.Length == 0)
106             {
107                 return;
108             }
109
110             int currentColorIndex = 0;
111             int currentColor = pixels[0];
112
113             colors[currentColorIndex] = currentColor;
114             colorCounts[currentColorIndex] = 1;
115
116             if (pixels.Length == 1)
117             {
118                 // If we only have one pixel, we can stop here
119                 return;
120             }
121
122             // Now iterate from the second pixel to the end, population distinct colors
123             for (int i = 1; i < pixels.Length; i++)
124             {
125                 if (pixels[i] == currentColor)
126                 {
127                     // We've hit the same color as before, increase population
128                     colorCounts[currentColorIndex]++;
129                 }
130                 else
131                 {
132                     // We've hit a new color, increase index
133                     currentColor = pixels[i];
134                     currentColorIndex++;
135                     colors[currentColorIndex] = currentColor;
136                     colorCounts[currentColorIndex] = 1;
137                 }
138             }
139         }
140     }
141 }