## 3 Proposed Method
### Dynamic Bit-Level Encoding Algorithm (DEA)
The number of entries in Group1 is referred to as the \"palette\", as it contains the most frequently used characters. Since different segments may have varying pixel values and distributions, the palette size also varies for each segment or image. Equation (1) presents the mathematical model designed to calculate the palette size by analyzing the frequency of characters that follow each character. This approach facilitates the efficient representation of frequently used characters, contributing to improved compression performance. By utilizing the palette size, the algorithm identifies the most frequently used characters and assigns them shorter bit lengths, enhancing compression efficiency. Given that \(a(Group\ 1)+b(Group2)\leq 255\),  
\[(\log_{2}a)\times(N+M\times\log_{2}b) \tag{1}\]  
In this context, N represents the total frequency of characters within the palette (Group 1), and M represents the total frequency of characters outside the palette (Group 2); the palette size is represented by a and the b value is determined as (255 \(-\) a). At this step, the calculated palette size (a) represents the most frequently used characters. This value is dynamically computed based on the frequency distribution within the image or segment. The pseudocode for determining the palette size and classifying characters into Group 1 and Group 2 is presented in Algorithm 1. **Step 3:** Based on the palette size determined in the previous step, the first and most frequently occurring group, referred to as Group 1, is processed using a dedicated HCA. Similarly, another HCA is applied to Group 2, which comprises the characters outside the palette (non-palette characters). The frequency value of the last element in Group 1 is set as the cumulative frequency of the characters in Group 2. During the encoding process, Group 1 is searched first. If the character is not found, the flag code (prefix code) value, the last element, is appended. **Step 4:** After defining Group 1, the remaining characters are also stored in Group 2. This group consists of characters with lower frequency values, signifying that they appear less often following the specified character. ```
function elementsInPalette \(\leftarrow\) specifyElementsInPalette(alphabet, tempElementsInPalette) { minLogResult = Int.MaxValue for i \(\gets\) 1 to 255 do //\"frequentlyRepeated\" method (in step 3) divides the alphabet into two according to the given value. currentElementsInPalette \(\leftarrow\) frequentlyRepeated(alphabet, i) logResult \(\leftarrow\) calculateLog(i) if(logResult \(<\) minLogResult) then minLogResult = i elementsInPalette = minLogResult } function calculateLog(paletteNumber){ for (currentKey in alphabet.keyset(0)) do for i \(\gets\) 0, alphabet.currentKey[].Length-1 do totalFirstFrequency += currentKey[i].valuetotalSecondFrequency += currentKey[Length-1].value \(\log_{2}a*\mathit{-Math.Log}(\mathit{\,paletteNumber})\)\(/\)\(\mathit{Math.log}\) \(2\)\(\log_{2}b*\mathit{-Math.Log}(\mathit{\,255-paletteNumber})\)\(/\)\(\mathit{Math.log}\) \(2\) return : \(\log_{2}a*(totalFirstFrequency\)\(+\)\(totalSecondFrequency*\log_{2}b)\) }
```  
**Algorithm 1** Pseudocode of the algorithm that calculates the number of palettes  
**Step 5:** After dividing the characters that follow each character into Group 1 and Group 2, two different Huffman trees are generated for each character. **Step 6:** At this stage, the encoding process begins by sequentially processing the entire alphabet from the beginning. * Since no structure exists initially and there is no preceding character, the first character is directly encoded using its ASCII value and stored as an 8-bit representation. * For the next character, its bit value is searched in the Group 1 Huffman tree. If the character is found, it is encoded using the shortened code word. * If the character is not found, a prefix code (flag code), defined as a distinguishing feature between Group 1 and Group 2, is added. The character is then encoded using its code-word value from Group 2.