FLoCIC: A Few Lines of Code for Raster Image Compression

A new approach is proposed for lossless raster image compression employing interpolative coding. A new multifunction prediction scheme is presented first. Then, interpolative coding, which has not been applied frequently for image compression, is explained briefly. Its simplification is introduced in regard to the original approach. It is determined that the JPEG LS predictor reduces the information entropy slightly better than the multi-functional approach. Furthermore, the interpolative coding was moderately more efficient than the most frequently used arithmetic coding. Finally, our compression pipeline is compared against JPEG LS, JPEG 2000 in the lossless mode, and PNG using 24 standard grayscale benchmark images. JPEG LS turned out to be the most efficient, followed by JPEG 2000, while our approach using simplified interpolative coding was moderately better than PNG. The implementation of the proposed encoder is extremely simple and can be performed in less than 60 lines of programming code for the coder and 60 lines for the decoder, which is demonstrated in the given pseudocodes.


Introduction
Data compression is one of the oldest disciplines in computer science [1]. It is present in many computer applications in a wide variety of domains, where it reduces traffic on information channels and supports data archiving. Many data compression approaches have been developed, and reviews of the most important ones can be found in several books [2][3][4][5][6].
Data compression algorithms can be classified as lossless, near-lossless, or lossy. The latter are domain-specific and consider the characteristics of humans' senses for vision and hearing [7]. Typically, transformations in the frequency domain [8][9][10][11] are used to identify high-frequency components, which are quantized and eliminated permanently. Complete reconstruction is impossible because of this. Other techniques include, domain-specific triangulation [12] or color reductions [13,14]. Lossy methods cannot guarantee a distortion rate below the chosen limit at the level of an individual element (e.g., a pixel), which is why the near-lossless methods have been developed [15]. They enable users to specify exactly to what extent the errors in the reconstructed data are acceptable. The lossless methods [16] reconstruct the original data exactly. In some domains, they are indispensable, such as compressing text, medical images, or high-quality sound, especially for editing purposes, to prevent accumulation of compression errors through repetitive compression and decompression. This paper introduces a new approach for lossless compression of continuous-tone raster images (photos). The suitability of interpolative coding for this type of image is examined after experiments with prediction functions. The main contribution of this paper can be summarized as follows: This paper consists of five sections. Section 2 explains briefly the backgrounds of JPEG LS, PNG, and JPEG 2000 in lossless mode. Section 3 introduces the proposed Few Lines of Code raster Image Compressor (FLoCIC) method. The corresponding pseudocodes are given in this Section. An evaluation of the method is given in Section 4. Section 5 concludes the paper.

Background
Let P = p x,y , 0 ≤ x < X, 0 ≤ y < Y be a t bit plane, continuous-tone grayscale raster image (t > 1) with a resolution of X × Y pixels, where p x,y ∈ [0, 1, · · · , 2 t − 1]. The lossless image compression methods follow the idea shown schematically in Figure 1. P should be processed in a predefined order, commonly in the raster-scan way. The value of the processed pixel p x,y ∈ P is estimated first by the prediction function f (L x,y ), which uses the values of some already-processed pixels, where L x,y = {p i,j }, j < y or j = y and i < x. Prediction models where L consists of just the neighboring pixels in the close proximity of p x,y will be considered local predictors.
The predicted value is then subtracted from the value of the processed pixel (see Equation (1)), and a prediction error x,y is obtained: Although the domain of x,y ∈ [−2 t + 1, 2 t − 1] is larger than the domain of p x,y ∈ P, its information entropy is expected to be smaller. Namely, the conventional distribution of the x,y values follows the geometric distribution [17], which offers a good opportunity for information entropy reduction [4].
The prediction values can be corrected further in the second step of the compression pipeline (see Figure 1) using context-based models [17][18][19]. Many methods, however, omit this step and proceed directly with the encoding, where RLE, Huffman, arithmetic, or dictionary-based encoding is used (or a combination of them) [16].
A very brief overview of JPEG LS and JPEG 2000 in lossless mode and PNG (the formats used for the comparison in Section 4) is given in the continuation.
Joint Photographic Experts Group-Lossless (JPEG LS): After the success of the JPEG standard, the same group of experts continued the work on lossless (and the near-lossless) image compression. JPEG LS was published in 1999 (ISO/IEC 14495-1), and the extensions followed four years later (ISO/IEC 14495-2) [20]. JPEG LS consists of a regular and RLE mode. Only the regular mode is considered briefly for the purposes of this paper.
JPEG LS follows the ideas developed in LOCO-I [17] and includes all steps from Figure 1. L x,y contains three neighboring pixels as shown in Figure 2a (i.e., L x,y = {p x−1,y , p x−1,y−1 , p x,y−1 }). The prediction function f (L x,y ) is shown in Equation (2). otherwise. (2) A mechanism for the prediction correction is used after x,y is determined. For this, three gradients for ∆ i , where i ∈ {1, 2, 3}, are calculated using Equation (3) (see Figure 2b): As the number of all possible combinations of the three gradient values for an image with t = 8 is a huge 511 3 , it is brought down by a reduction function to a manageable 355 values, which represent the entry points into the context models. These models improve adaptively during the image compression process and serve for correcting x,y . Details can be found in [5,17].
The corrected values x,y are encoded with Golomb codes [21]. Golomb's parameter is also obtained from the context model. However, as this coding lacked efficiency, the arithmetic coding was added to the standard in 2003. In this way, JPEG LS became the best lossless compression standard which uses only the local predictors. Unfortunately, its usage was limited due to patents. This is why the PNG standard has become the most popular format for lossless raster image compression.
Portable Network Graphics (PNG): This was designed as a replacement for the GIF format, which contained the patent-protected LZW [22] compression algorithm. The development started as an open project of many individuals [23]. PNG was soon accepted by the W3C consortium, which boosted its popularity. In 2004, it became an international standard (ISO/IEC 15948).
PNG performs the prediction on the level of a raster scan line. It applies five predictors (named filters), where L x,y is defined as follows: The filter average calculates the average values of two pixels in L x,y , while the Paeth filter is determined by the algorithm given in [24]. The best predictor is then applied on the whole line. PNG does not use any context-based corrections for x,y . The open-source algorithm Deflate [5] is used in the final step. It is based on the LZ77 algorithm [25], whose tokens are then compressed by Huffman coding [26]. PNG is still the most popular lossless image compression format. JPEG 2000 in lossless mode: JPEG 2000 is another standard from the JPEG consortium whose primary goal was to achieve excellent lossy compression with support for scalability [27]. It is based on the wavelet transform. The Le Gall-Tabatabai wavelet [28] was used for lossless compression as it operates with integer coefficients only. JPEG 2000 does not perform any prediction nor any correction of the predicted error. Instead, it explores the properties of the hierarchical wavelet transform to compress the obtained coefficients efficiently with the specially designed arithmetic encoder, namely with MQ-coder [29].
There are, however, other prediction models. An overview of them can be found in a very recent paper by Ulacha and Łazoryszczak [30].

Materials and Methods
The new prediction model, used later in experiments, is introduced first. An explanation of interpolative encoding and its simplifications is given after that.

Multifunction Local Predictions
A new prediction mechanism was tried, although the prediction suggested in JPEG LS (Equation (2)) has been proven to work well. Let us have a set of predictors where I is the number of functions f i and L x,y is a set of some already-seen neighboring pixels. Function MinF, given by Equation (4), returns the index i of f i (L x,y ), which achieves the minimal prediction error: We supposed that if the i th predictor achieved the smallest | x,y | for p x,y , then most of the time, the same predictor was also the best one for the neighboring pixel, (i.e., for the next right p x+1,y or for the next bottom pixel p x,y+1 ). Table 1 shows a set of the predictors used in our case, when L x,y = {p x−1,y , p x−1,y−1 , p x,y−1 , p x+1,y−1 } and I = 12. The first pixel p 0,0 cannot be predicted, while the function f 0 is applied only for the remaining pixels p x,0 (i.e., for the pixels in the first row of P). Similarly, the function f 1 is used for pixels p 0,y .

Interpolative Coding
The idea of interpolative coding (IC), proposed by Moffat and Stuiver in 2000 [31], differs drastically from other compression methods. For example, the statistically based approaches, such as Huffman or arithmetic coding, assign a unique prefix code to each symbol of the message [5]. Dictionary-based approaches (i.e., LZ family compression algorithms) construct phrases from the messages and assign them unique tokens [6]. The symbols from the input message are processed in the given sequence in both cases. On the other hand, IC processes the input message in an arbitrary yet predefined way, where the code of a particular symbol depends more on its position than on its value.
The message was, in our case, obtained from the prediction step of the compression pipeline (see Figure 1); in other words, it is sequence is where t is the bit plane depth (see Section 2). The raster scan traversal transforms (x, y) → i = y · X + x, and therefore n = X · Y.
IC works in two steps: initialization and encoding.
Initialization: E is transformed first into a sequence of non-negative integers N = + i , (5), which interleaves the input positive and negative values: N is then used to obtain a strictly increasing cumulative sequence C = c i , 0 ≤ i < n with Equation (6): Encoding: The original IC mechanism, as described in [31], is given first, and our modification, which simplifies the encoding process, is explained after that. IC works through a recursive dividing of C in half according to Equation (7), where L denotes the low guard and H is the high guard of the considered part of C: Then, c m is encoded in three steps:

1.
A range G = [g L , g H ] of all possible values is determined first (see Equation (8)) by taking into account that C is strictly monotone: 2.
The number of bits g needed to encode all possible values from G is then calculated with Equation (9): 3. Finally, the value v = c m − g L is encoded in binary with g bits and sent to the output B = b i , where b i ∈ {0, 1} and 0 ≤ i < |B| are bits and |B| is the total number of bits.
IC also has a special (i.e., the best) case, which may increase its efficiency drastically. When H − L = c H − c L , IC does not need to send any bits at all to B. In particular, this case is trivially detectable by a decoder. The interval between L and H is filled simply by incrementing the value g L . A similar case was recognized in [32,33].
where D is the maximal value of the domain, then the encoder also does not emit any bits. However, this case is extremely rare in image compression after applying a prediction. Therefore, it is not worth using it in this application.
The encoding in step 3 can be completed in different ways: classical binary codes, truncated binary code [5], FELICS codes [34], and Ψ codes (in the case of a small alphabet), as suggested in [32].
Simplifying the interpolative encoding process: IC, as described above and proposed in [31], can be simplified further. Specifically, if the requirement of a strictly increasing cumulative sequence of integers is released, then the whole procedure becomes simpler as follows: • C is obtained from N with Equation (10): • Calculation of guards g L and g H is not needed, as the range containing the value c m is Detection of the optimal case is simplified to check whether c H = c L . Finally, it should be noted that the simplified version does not shorten B. Indeed, the original and simplified versions of IC generate the same stream of bits.
In [35] it was reported that IC can be a good alternative to arithmetic coding for bi-level image compression. IC was also successful at chain code compression [32,33,36]. The characteristic of both domains is a small alphabet. However, to the best of our knowledge, IC has not been used for compression of continuous-tone images, as is the case in this application.

An Example
A short example is given to clarify the encoding process. Let us suppose that a prediction model has a generated matrix A containing error values x,y (see Figure 3). The first element in the matrix represents the absolute pixel value which, of course, cannot be predicted. A is then used to obtain E , shown in Figure 4a, with the raster scan traversal. Applying Equation (5) yields N (see Figure 4b), from which C is obtained with Equation (10) (see Figure 4c). The indices of all the sequences are shown at the top of Figure 4.    Table 2, while the resulting sequence B is given in Figure 5. Binary encoding was used in this example for v due to clarity. Applying truncated binary codes or FELICS codes would yield a shorter B.  (8) is not needed in the simplified version. 2 Remember that v = c m − c L . 3 As c L = c H , the coder does not output any bits. B = 001100 1100 0011 01 10011 01000 0000 0011 0110 0000 0111 00 Finally, the pseudocodes are given for FLoCIC: Algorithm 1 performs the initialization, Algorithm 2 implements the JPEG LS prediction, and Algorithm 3 presents the interpolative coder.

Decoding
The decoder needs the following data to restore C: • The values of the first c 0 and the last element c n−1 ; • The length n; • The sequence of bits B. The first three items form the header, while B is stored after it. In our case, 8 bits were reserved for c 0 , while for c n−1 and n, 32 bits were allocated (i.e., the header occupied 72 bits in total). The content of the header for the example from Ssection 3.2.1 is in Figure 6 and given in decimals. When coding raster images, its resolution in the X direction should be added to the header, as Y can be obtained by Y = n/X. Decoding starts with reading the header, allocating the n = 20 memory units for sequence C, and initializing c 0 = 23 and c n−1 = 63 (see Figure 7a). The decoder sets L = 0 and H = n − 1 = 19. As c 0 = c 19 , m = 9 is calculated with Equation (7). The number of bits g, which were used for encoding c 9 , is then calculated as g = log 2 (63 − 23 + 1) = 6. Therefore, the first 6 bits are read from B (i.e., bits 001100, corresponding to v = 12). Finally, c L + v reconstructs 35, which is written at c 9 (see Figure 7b). The decoder now operates recursively, mirroring the coding process. If c L = c H , then the decoder sets c i = c L , L < i < H and does not read any bit from B. Algorithm 4 shows the FLoCIC decoder, while Algorithms 5 and 6 contain the inverse simplified interpolative decoder and the inverse JPEG LS predictor, respectively. From all the pseudocodes, it is evident that the FLoCIC's coder and decoder were completely symmetrical. FLoCIC's programming code is accessible in [37].
Unwrap the values to get the errors in the prediction 13: if Even(N i ) then 14: E i ← N i /2 15: else 16: C: sequence to be reconstructed after all recursive calls are executed 3: L, H : guards 4: if c L = c H then cheching for the special case 5: for i ← L + 1, H − 1 do else if x = 0 then left column 9: p 0,y ← p 0,y−1 + E y * X+x 10: else if p x−1,y−1 ≥ max(p x−1,y , p x,y−1 ) then for all remaining rows 11: p x,y ← min(p x−1,y , p x,y−1 ) + E y * X+x

Experiments
Twenty-four popular benchmark grayscale images with t = 8 were used in the experiments (see Figure 8). Table 3 introduces in the first three columns the information about these images, including their resolutions, raw sizes in bytes, and the values of the raw data information entropy (H raw ) [4]. The remaining three columns show the effect of the information entropy reduction after applying three different predictors: the first two are the multifunction predictors (see Section 3.1), (H NR is the information entropy when the next right pixel is predicted, and H NB stands for the next bottom pixel.) while H JPEGLS is the predictor used in JPEG LS (see Equation (2)). The last column contains the average absolute prediction error E obtained when the JPEG LS predictor was used. Although the differences between the obtained information entropies were small, it can be concluded that the JPEG LS predictor is better. Indeed, in all cases except for the Peppers image, it reduced the information entropy the best. The JPEG LS predictor was therefore used in the continuation.   FLoCIC was compared against JPEG LS, JPEG 2000 in lossless mode, and PNG. The results are given in Table 4. The JPEG LS images were generated by IrfanView's JPEG LS plug-in [38], while the JPEG 2000 in lossless mode and PNG images were obtained by ImageMagick [39]. FLoCIC was, of course, coded by ourselves. Our implementation of the arithmetic coding (AC) based on the E1, E2 and E3 transforms [40] was used to confront it with IC.
JPEG LS performed the best, and JPEG 2000 in lossless mode was second. FLoCIC outperformed PNG slightly, either when IC or AC was used in the final step. Surprisingly, IC combined with the FELICS codes [34] turned out to be moderately better than AC on average. However, it should be stressed that the most basic implementation of AC was used. For example, context-based adaptive binary arithmetic coding [41] would yield better results.
As can be seen, FLoCIC worked successfully with images of different resolutions. Just for the reader's information, the largest image, Sun, was compressed in 0.793 s, while the more than 64 times smaller image, Cameraman, was compressed in 0.018 s on a very modest computer: an Intel i5-2500K processor with 3.3 GHz with 16 GB of RAM running Windows 10. FLoCIC was implemented in C++ and compiled with Visual Studio 19. Decompression was approximately 15% faster, as decoding the FELICS codes was faster than encoding them. At this point, it should be stressed that none of these methods are competitive with the modern lossless image compression approaches, such as JPEG XL [42] or WebP [43] in lossless mode. They do not perform a local prediction but instead investigate larger areas of pixels.

Discussion
This paper introduces a new, very simple algorithm for lossless image compression named few lines of code raster image compression (FLoCIC). Indeed, as shown in the given pseudocode, less than 60 lines of programming code are needed for it. The code is, however, even shorter when coded in, for example, C++. The compression pipeline is classical, consisting of only two parts: the prediction (the JPEG LS predictor turned out to be the most successful) and the entropy encoder. Interpolative coding, a technique developed by Moffat and Stuiver [31], is less known and has not been used in image compression, except for bi-level images [32,33,35]. It turned out to be as good as the widely used arithmetic coding for images with continuous tones as well. In this paper, we simplified interpolative coding, leading to further shortening of the programming code.
Twenty-four classical benchmark 8 bit grayscale images were used to evaluate the effectiveness of FLoCIC. They had different resolutions, ranging from 256 × 256 up to 2100 × 2034 pixels. Concerning the compression ratio achieved, FLoCIC can cope with PNG, the most widely used lossless image compression standard. In the given set of testing images, FLoCIC turned to actually be slightly better and moderately worse than JPEG 2000. JPEG LS was, however, better by almost 10 %. It is the only one of the considered approaches that incorporates the correction of prediction errors. Despite being efficient, JPEG LS is rarely found in practice. It should be noted, however, that none of the mentioned approaches are competitive according to the compression ratio with the state-of-the-art JPEG XL or WebP. However, they do not use the simple and fast local prediction techniques and instead employ the wider pixel's surroundings.
FLoCIC is an interesting alternative to PNG. It is extremely easy to implement, and as such, it could be applied in an environment with modest computational power, such as in embedded systems [44]. It is also suitable for programming training for students of computer science, similar to, for example, Delaunay triangulation [45,46].