[go: up one dir, main page]

Next Article in Journal
Explicit Expressions for Most Common Entropies
Next Article in Special Issue
On the Asymptotic Capacity of Information-Theoretic Privacy-Preserving Epidemiological Data Collection
Previous Article in Journal
Entropy and Cities: A Bibliographic Analysis towards More Circular and Sustainable Urban Environments
Previous Article in Special Issue
Skew Constacyclic Codes over a Non-Chain Ring
 
 
Font Type:
Arial Georgia Verdana
Font Size:
Aa Aa Aa
Line Spacing:
Column Width:
Background:
Article

FLoCIC: A Few Lines of Code for Raster Image Compression

1
Faculty of Electrical Engineering and Computer Science, University of Maribor, Koroška cesta 46, SI-2000 Maribor, Slovenia
2
Department of Computer Science and Engineering, University of West Bohemia, Technická 8, 306 14 Plzeň, Czech Republic
*
Author to whom correspondence should be addressed.
Entropy 2023, 25(3), 533; https://doi.org/10.3390/e25030533
Submission received: 24 February 2023 / Revised: 15 March 2023 / Accepted: 18 March 2023 / Published: 20 March 2023
(This article belongs to the Special Issue Advances in Information and Coding Theory)

Abstract

:
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.

1. 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:
  • An evaluation of a new local pixel prediction model;
  • A simplification of interpolative coding;
  • Testing the suitability of interpolative coding for continuous-tone image compression;
  • Comparison of the proposed data compression approach with PNG, JPEG LS, and JPEG 2000 in lossless mode;
  • A compact programming code.
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.

2. 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:
ϵ x , y = p x , y f ( L x , y ) .
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).
f x , y = min ( p x 1 , y , p x , y 1 ) ; when p x 1 , y 1 max ( p x 1 , y , p x , y 1 ) max ( p x 1 , y , p x , y 1 ) ; when p x 1 , y 1 min ( p x 1 , y , p x , y 1 ) p x 1 , y + p x , y 1 p x 1 , y 1 ; otherwise .
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):
Δ 1 = p x + 1 , y 1 p x , y 1 Δ 2 = p x , y 1 p x 1 , y 1 Δ 3 = p x 1 , y 1 p x 1 , y
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:
  • None:    L x , y = ;
  • Sub:    L x , y = { p x 1 , y } ;
  • Up:           L x , y = { p x , y 1 };
  • Average:     L x , y = { p x , y 1 , p x 1 , y } ;
  • Paeth:    L x , y = { p x , y 1 , p x 1 , y , p x 1 , y 1 } .
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].

3. 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.

3.1. 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 F x , y = { f i ( L x , y ) } , 0 i < I , where I is the number of functions f i and L x , y is a set of some already-seen neighboring pixels. Function M i n F , given by Equation (4), returns the index i of f i ( L x , y ) , which achieves the minimal prediction error:
M i n F ( F x , y ) = a r g m i n i { | p x , y f i ( L x , y ) | }
We supposed that if the i t h 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 .

3.2. 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 E = ϵ i , 0 i < n , ϵ i { 2 t + 1 , 2 t 1 } , 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 + , ϵ i + { 0 , 2 t + 1 1 } , 0 i < n by Equation (5), which interleaves the input positive and negative values:
ϵ i + = ϵ i ; when i = 0 , 2 ϵ i ; when i > 0 and ϵ i 0 , 2 | ϵ i | 1 ; when i > 0 and ϵ i < 0 .
N is then used to obtain a strictly increasing cumulative sequence C = c i , 0 i < n with Equation (6):
c i = ϵ 0 + ; when i = 0 , 1 + ϵ i + + c i 1 ; when 0 < i < n .
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 :
m = L + H 2
Then, c m is encoded in three steps:
  • 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:
    g L = c L + ( m L ) , g H = c H ( H m ) .
  • The number of bits g needed to encode all possible values from G is then calculated with Equation (9):
    g = log 2 ( g H g L + 1 ) .
  • 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]. If H L = D · ( c H c L ), 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):
    c i = ϵ 0 + ; when i = 0 , ϵ i + + c i 1 ; otherwise .
  • Calculation of guards g L and g H is not needed, as the range containing the value c m is simply G = [ c L , c H ] .
  • 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.

3.2.1. 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.
The simplified interpolative coding initializes L = 0 and H = 19 . The interval G = [ c L = 23 , c H = 63 ] is set, and m = 9 is calculated using Equation (7). The number of bits g = log 2 ( 63 23 + 1 ) = 6 for encoding all possible values from G. The value v is then calculated as v = c m c L = 35 23 = 12 and binary encoded with g = 6 bits. The algorithm now proceeds recursively as demonstrated in 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 .
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.
Algorithm 1 Lossless image compression with FLoCIC
1:
function  CompressWithFLoCIC( P , X, Y)      ▹ returns binary sequence B
2:
                   ▹ P : raw image; X , Y : image resolution
3:
   E Predict( P , X, Y)
4:
   n X × Y
5:
   N 0 E 0
6:
  for i← 1, n 1 do           ▹ turns ϵ i E to non-negative values
7:
   if ϵ i 0 then
8:
     N i 2 × ϵ i
9:
   else
10:
     N i 2 × abs ( ϵ i ) 1
11:
   end if
12:
  end for
13:
   C 0 N 0
14:
  for i← 1, n 1 do             ▹ forms the cumulative sequence
15:
    C i C i 1 + N i
16:
  end for
17:
   B SetHeader ( X , c 0 , c n 1 , n )
18:
   B IC ( B , C , 0 , n 1 )       ▹ call simplified interpolative coding
19:
  return B
20:
end function
Algorithm 2 JPEG LS prediction
1:
function  Predict( P , X, Y)            ▹ return a sequence of predicted values
2:
   for x 0 , X 1 do             ▹ P : raw image; X , Y : image resolution
3:
   for y 0 , Y 1 do
4:
     if x = 0 and y = 0 then            ▹ first element is not predicted
5:
       E y * X + x p 0 , 0
6:
     else if y = 0 then                        ▹ first row
7:
       E y * X + x p x 1 , 0 p x , 0
8:
     else if x = 0 then                       ▹ left column
9:
       E y * X + x p 0 , y 1 p 0 , y
10:
     else if p x 1 , y 1 max ( p x 1 , y , p x , y 1 ) then     ▹ for all remaining rows
11:
       E y * X + x min ( p x 1 , y , p x , y 1 ) p x , y
12:
     else if p x 1 , y 1 max ( p x 1 , y , p x , y 1 ) p x , y then
13:
       E y * X + x max ( p x 1 , y , p x , y 1 ) p x , y
14:
     else
15:
       E y * X + x p x 1 , y + p x , y 1 p x 1 , y 1 p x , y
16:
     end if
17:
    end for
18:
  end for
19:
  return E
20:
end function
Algorithm 3 Simplified interpolative coding
1:
function  IC( B , C , L , H )   ▹ B : sequence of bits; C : cumulative sequence; L , H : guards
2:
  if H L > 1 then
3:
   if c H c L then
4:
     m 0.5 × ( H + L )          ▹ position of the coded element
5:
     g log 2 ( c H c L + 1 )          ▹ number of needed bits
6:
     B Encode ( B , g , c m c L )   ▹ insert ordinary, truncated, or FELICS codes
7:
    if L < m then
8:
     IC ( B , C , L , m )
9:
    end if
10:
    if m < R then
11:
     IC ( B , C , m , R )
12:
    end if
13:
   end if
14:
  end if
15:
end function

3.2.2. 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 Section 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].
Algorithm 4 Image decompression with FLoCIC
1:
function  DecompressWithFLoCIC( B )            ▹ B : sequence of bits
2:
                ▹ Function returns reconstructed raw image P
3:
DecodeHeader ( B , X , n , c 0 , c n 1 )
4:
Y n / X
5:
C InitialiseC ( n , c 0 , c n 1 )  ▹ Create C with n elements and set C 0 and C n 1
6:
C DeIC ( B , C , 0 , n 1 )     ▹ Reconstruct remaining elements of C
7:
N 0 C 0
8:
for i 1 , n 1 do        ▹ Calculate non-cumulative sequence N
9:
   N i C i C i 1
10:
end for
11:
E 0 N 0
12:
for i 1 , n 1 do   ▹ 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:
    E i ( N i + 1 ) / 2
17:
  end if
18:
end for
19:
P = PredictInverse ( E , X , Y )
20:
return P
21:
end function
Algorithm 5 Simplified interpolative decoding
1:
function  DeIC( B , C , L , H )           ▹ B : sequence of bits to be decoded
2:
       ▹ 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
6:
     C i c L
7:
   end for
8:
  else
9:
    m 0.5 × ( H + L )       ▹ position of the element to be decoded
10:
    g log 2 ( c H c L + 1 )             ▹ get number of bits
11:
    B GetBits ( B , g )                  ▹ read g bits from B
12:
    C m Decode ( B )  ▹ Decode ordirani, trunacated, or FELICS binary code
13:
   if L < m then      ▹ proceed recursively with the reconstruction of C
14:
     DeIC ( B , C , L , m )
15:
   end if
16:
   if m < H then
17:
     DeIC ( B , C , m , H )
18:
   end if
19:
   end if
20:
end function
Algorithm 6 Inverted JPEG LS predictor
1:
function  PredictInverse( E , X, Y)    ▹ return reconstructed raw image data in P
2:
for x 0 , X 1 do  ▹ E : sequence of prediction errors; X , Y : image resolution
3:
  for y 0 , Y 1 do
4:
   if x = 0 and y = 0 then           ▹ first element is not predicted
5:
     p 0 , 0 E 0
6:
    else if y = 0 then                      ▹ first row
7:
     p x , 0 p x 1 , 0 + E y * X + x
8:
   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
12:
   else if p x 1 , y 1 max ( p x 1 , y , p x , y 1 ) then
13:
     p x , y max ( p x 1 , y , p x , y 1 ) + E y * X + x
14:
   else
15:
     p x , y p x 1 , y + p x , y 1 p x 1 , y 1 + E y * X + x
16:
   end if
17:
  end for
18:
end for
19:
return P
20:
end function

4. 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 r a w ) [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 N R is the information entropy when the next right pixel is predicted, and H N B stands for the next bottom pixel.) while H J P E G L S 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.

5. 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].

Author Contributions

Conceptualization, B.Ž.; methodology, D.S. and N.L.; software, B.Ž. and M.Ž.; validation, I.K. and A.N.; formal analysis, D.P. and I.K.; investigation, B.Ž., D.P., I.K. and A.N.; resources, I.K.; data curation, Š.K.; writing—original draft preparation, B.Ž.; writing—review and editing, D.S., I.K., Š.K., N.L., B.L. and M.Ž.; visualization, A.N. and B.L.; supervision, I.K. and B.Ž.; project administration, I.K. and D.P.; funding acquisition, I.K. and B.Ž. All authors have read and agreed to the published version of the manuscript.

Funding

This research was funded by the Slovene Research Agency under Research Project J2-4458 and Research Programme P2-0041 and the Czech Science Foundation under Research Project 23-04622L.

Institutional Review Board Statement

Not applicable.

Informed Consent Statement

Not applicable.

Data Availability Statement

Not applicable.

Conflicts of Interest

The authors declare no conflict of interest.

References

  1. Shannon, C.E. A Mathematical Theory of Communication. AT&T Tech. J. 1948, 27, 379–423. [Google Scholar]
  2. Nelson, M.; Gailly, J.-L. The Data Compression Book, 2nd ed.; M&T Books: New York, NY, USA, 1991. [Google Scholar]
  3. Moffat, A.; Turpin, A. Compression and Coding Algorithms; Kluwer Academic: New York, NY, USA, 2002. [Google Scholar]
  4. Cover, T.M.; Thomas, J.A. Elements of Information Theory, 2nd ed.; Wiley: Hoboken, NJ, USA, 2006. [Google Scholar]
  5. Salomon, D.; Motta, G. Handbook of Data Compression, 5th ed.; Springer: London, UK, 2010. [Google Scholar]
  6. Sayood, K. Introduction to Data Compression, 4th ed.; Morgan Kaufman: Waltham, MA, USA; Elsevier: Waltham, MA, USA, 2012. [Google Scholar]
  7. Richardson, I.E.G. H.264 and MPEG-4 Video Compression: Video Coding for Next-Generation Multimedia; Wiley: Chichester, UK, 2003. [Google Scholar]
  8. Rao, K.R.; Yip, P. Discrete Cosine Transform; Academic Press: Boston, MA, USA, 1990. [Google Scholar]
  9. Sridhar, S.; Kumar, P.R.; Ramanalah, K.V. Wavelet Transform Techniques for Image Compression—An Evaluation. Int. J. Image Graph. Sig. Process. 2014, 6, 54–67. [Google Scholar] [CrossRef] [Green Version]
  10. Starosolski, R. Hybrid Adaptive Lossless Image Compression Based on Discrete Wavelet Transform. Entropy 2020, 22, 751. [Google Scholar] [CrossRef]
  11. Qin, Q.; Liang, Z.; Liu, S.; Wang, X.; Zhou, C. A Dual-Domain Image Encryption Algorithm Based on Hyperchaos and Dynamic Wavelet Decomposition. IEEE Access 2022, 10, 122726–122744. [Google Scholar] [CrossRef]
  12. Demaret, L.; Dyn, N.; Iske, A. Image compression by linear splines over adaptive triangulations. Signal Process 2020, 22, 1604–1616. [Google Scholar] [CrossRef]
  13. Papamarkos, N.; Atsalakis, A.E.; Strouthopoulos, C.P. Adaptive color reduction. IEEE Trans. Syst. Man Cybern. 2002, 32, 44–56. [Google Scholar] [CrossRef] [PubMed]
  14. Jeromel, A.; Žalik, B. An efficient lossy cartoon image compression method. Multimed. Tools Appl. 2020, 79, 433–451. [Google Scholar] [CrossRef] [Green Version]
  15. Ansari, R.; Momon, N.; Ceran, E. Near-lossless image compression techniques. J. Electron. Imaging 1998, 7, 486–494. [Google Scholar]
  16. Rahman, M.A.; Hamada, M. Lossless Image Compression Techniques: A State-of-the-Art Survey. Symmetry 2019, 11, 1274. [Google Scholar] [CrossRef] [Green Version]
  17. Weinberger, M.J.; Seroussi, G.; Sapiro, G. The LOCO-I Lossless Image Compression Algorithm: Principles and Standardization into JPEG-LS. IEEE T Image Process 2000, 9, 1309–1324. [Google Scholar] [CrossRef] [Green Version]
  18. Xiaolin, W.; Memon, N. Context-based lossless interband compression-extending CALIC. IEEE Trans. Image Process 2000, 9, 994–1001. [Google Scholar] [CrossRef] [PubMed] [Green Version]
  19. Žalik, B.; Mongus, D.; Lukač, N. Can burrows-Wheeler transform be replaced in chain code compression? Inf. Sci. 2020, 525, 109–118. [Google Scholar] [CrossRef]
  20. Overview of JPEG LS. Available online: https://jpeg.org/jpegls/index.html (accessed on 23 January 2023).
  21. Golomb, S.W. Run–length encodings. IEEE Trans. Inform. Theory 1966, 12, 399–401. [Google Scholar] [CrossRef] [Green Version]
  22. Welch, T. A Technique for High-Performance Data Compression. Computer 1984, 17, 8–19. [Google Scholar] [CrossRef]
  23. PortableNetwork Graphics. Available online: http://www.libpng.org/pub/png/ (accessed on 23 January 2023).
  24. Paeth, A.W. Image File Compression Made Easy; Graphics Gems, 2; James, A., Ed.; Academic Press: San Diego, CA, USA, 1991; pp. 93–100. [Google Scholar]
  25. Ziv, J.; Lempel, A. A universal algorithm for sequential data compression. IEEE Trans. Inf. Theory 1977, 23, 337–343. [Google Scholar] [CrossRef] [Green Version]
  26. Huffman, D.A. A Method for the Construction of Minimum-Redundancy Codes. Proc. IRE 1952, 40, 1098–1101. [Google Scholar] [CrossRef]
  27. Taubman, D.; Marcellin, M.W. JPEG2000: Image Compression Fundamentals Standards and Practice; Kluwer: Boston, MA, USA, 2002. [Google Scholar]
  28. Le Gall, D.; Tabatabai, A.J. Sub-band coding of digital images using symmetric short kernel filters and arithmetic coding techniques. In Proceedings of the ICASSP-88: International Conference on Acoustics, Speech, and Signal Processing, New York, NY, USA, 11–14 April 1988; IEEE Press: Piscataway, NJ, USA, 1988; pp. 761–764. [Google Scholar]
  29. Ko, H.-H. Enhanced Binary MQ Arithmetic Coder with Look-Up Table. Information 2021, 12, 143. [Google Scholar] [CrossRef]
  30. Ulacha, G.; Łazoryszczak, M. Lossless Image Coding Using Non-MMSE Algorithms to Calculate Linear Prediction Coefficients. Entropy 2023, 25, 156. [Google Scholar] [CrossRef]
  31. Moffat, A.; Stuiver, L. Binary interpolative coding for effective index compression. Inf. Retr. 2000, 3, 25–47. [Google Scholar] [CrossRef]
  32. Žalik, B.; Mongus, D.; Lukač, N.; Rizman Žalik, K. Efficient chain code compression with interpolative coding. Inf. Sci. 2018, 439, 39–49. [Google Scholar] [CrossRef]
  33. Žalik, B.; Rizman Žalik, K.; Zupančič, E.; Lukač, N.; Žalik, M.; Mongus, D. Chain code compression with modified interpolative coding. Comput. Electr. Eng. 2019, 77, 27–36. [Google Scholar] [CrossRef]
  34. Howard, P.G.; Vitter, J. Fast and efficient lossless image compression. In Proceedings of the DC’93: Data Compression Conference, Snowbird, UT, USA, 30 March–2 April 1993; IEEE Computer Society: New York, NY, USA, 1993; pp. 208–215. [Google Scholar]
  35. Niemi, A.; Teuhola, J. Interpolative coding as an alternative to arithmetic coding in bi-level image compression. In Proceedings of the SCC 2015—10th International ITG Conference on Systems, Communications and Coding, Hamburg, Germany, 2–5 May 2015; IEEE: New York, NY, USA, 2015; pp. 1–6. [Google Scholar]
  36. Strnad, D.; Kohek, Š.; Nerat, A.; Žalik, B. Efficient representation of geometric tree models with level-of-detail using compressed 3D chain code. IEEE Trans. Vis. Comput. Graph. 2020, 26, 3177–3188. [Google Scholar] [CrossRef] [PubMed]
  37. FLoCIC. Available online: https://github.com/mitzal/FLoCIC (accessed on 14 March 2023).
  38. IrfanView. Available online: https://www.irfanview.com/ (accessed on 23 January 2023).
  39. ImageMagick. Available online: https://imagemagick.org/ (accessed on 23 January 2023).
  40. Bodden, E.; Clasen, M.; Kneis, J. Arithmetic Coding Revealed; Sable Technical Report No. 2007-5; McGill University: Montreal, QC, Canada, 2007. [Google Scholar]
  41. Marpe, D.; Scwarz, H.; Wiegand, T. Context-Based Adaptive Binary Arithmetic Coding in the H.264/AVC Video Compression Standard. IEEE Trans. Circuits Syst. Video Technol. 2003, 13, 620–636. [Google Scholar] [CrossRef] [Green Version]
  42. Overview of JPEG XL. Available online: https://jpeg.org/jpegxl/ (accessed on 23 January 2023).
  43. An Image Format for Web. Available online: https://developers.google.com/speed/webp/ (accessed on 23 January 2023).
  44. Globačnik, T.; Žalik, B. An efficient raster font compression for embedded systems. Pattern Recogn. 2010, 43, 4137–4147. [Google Scholar] [CrossRef]
  45. Špelič, D.; Novak, F.; Žalik, B. Educational support for computational geometry course—The Delaunay triangulation tester. Int. J. Eng. Educ. 2009, 25, 93–101. [Google Scholar]
  46. Krivograd, S.; Žalik, B.; Novak, F. TriMeDeC tool for preparing visual teaching materials based on triangular networks. Comput. Appl. Eng. Educ. 2002, 10, 144–154. [Google Scholar] [CrossRef]
Figure 1. Typical components of the lossless image compression pipeline.
Figure 1. Typical components of the lossless image compression pipeline.
Entropy 25 00533 g001
Figure 2. Pixels used in (a) JPEG LS prediction and (b) for context modeling.
Figure 2. Pixels used in (a) JPEG LS prediction and (b) for context modeling.
Entropy 25 00533 g002
Figure 3. Example: The matrix contains the values of ϵ after the prediction process.
Figure 3. Example: The matrix contains the values of ϵ after the prediction process.
Entropy 25 00533 g003
Figure 4. Example: (a) A sequence of prediction errors. (b) A sequence of interleaved values. (c) The running sum sequence of cumulative integer values.
Figure 4. Example: (a) A sequence of prediction errors. (b) A sequence of interleaved values. (c) The running sum sequence of cumulative integer values.
Entropy 25 00533 g004
Figure 5. Example: Result of encoding.
Figure 5. Example: Result of encoding.
Entropy 25 00533 g005
Figure 6. Example: Storing the results of interpolative coding.
Figure 6. Example: Storing the results of interpolative coding.
Entropy 25 00533 g006
Figure 7. Example of decoding. (a) Situation after initialization. (b) Decoded element at m = 9 .
Figure 7. Example of decoding. (a) Situation after initialization. (b) Decoded element at m = 9 .
Entropy 25 00533 g007
Figure 8. Testing raster images.
Figure 8. Testing raster images.
Entropy 25 00533 g008
Table 1. Set of predictors F .
Table 1. Set of predictors F .
f 0 = p x 1 , y f 6 = 0.5 · ( p x 1 , y 1 + p x , y 1 )
f 1 = p x , y 1 f 7 = 0.5 · ( p x , y 1 + p x + 1 , y 1 )
f 2 = p x 1 , y 1 f 8 = 0.5 · ( p x 1 , y + p x + 1 , y 1 )
f 3 = p x + 1 , y 1 f 9 = 0.5 · ( p x 1 , y 1 + p x + 1 , y 1 )
f 4 = p x 1 , y + p x , y 1 p x 1 , y 1 f 10 = p x , y 1 + p x 1 , y 1 p x + 1 , y 1
f 5 = 0.5 · ( p x 1 , y + p x 1 , y 1 ) f 11 = p x , y 1 + p x 1 , y 1 p x 1 , y
Table 2. An example of simplified interpolative coding.
Table 2. An example of simplified interpolative coding.
LHm c m G = [ c L , c H ]   1gv 2Code
019935[23, 63]612001100
09435[23, 35]4121100
04226[23, 35]430011
02124[23, 26]2101
49//[35, 35]/// 3
9191454[35, 63]51910011
9141143[35, 54]5801000
9111035[35, 43]400000
11141246[43, 54]430011
12141352[46, 54]460110
14191654[54, 63]400000
1416//[54, 54]/// 3
16191761[54, 63]470111
17191861[61, 63]2000
1 The calculation of the interval’s guards according to Equation (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.
Table 3. Information about the images’ resolutions and raw sizes in bytes, entropy of the raw images, entropies for three prediction models, and average absolute prediction errors for JPEG LS predictor.
Table 3. Information about the images’ resolutions and raw sizes in bytes, entropy of the raw images, entropies for three prediction models, and average absolute prediction errors for JPEG LS predictor.
ImageResolutionRaw Size H raw   1 H NR   2 H NB   3 H JPEGLS   4 E ¯
Baboon 512 × 512 262,1447.3576.4996.4146.27514.342
Balloon 720 × 576 414,7207.3463.2823.2043.1201.608
Barbara 512 × 512 262,1447.3435.7945.8905.75812.031
Barb2 720 × 576 414,7207.4845.4905.2385.1816.895
Board 720 × 576 414,7206.8284.0734.0133.9472.927
Boats 720 × 576 414,7207.0884.5274.3944.3073.707
Cameraman 256 × 256 65,5366.9045.2005.2735.1508.214
Flower 512 × 480 245,7607.4103.8813.8893.8662.755
Fruits 512 × 480 245,7607.3664.1734.1704.0143.062
Girl 720 × 576 414,7207.2884.3544.1534.2073.391
Gold 720 × 576 414,7207.5304.9594.9514.7164.716
Hotel 720 × 576 414,7207.5464.8614.8544.7324.987
Lena 512 × 512 262,1447.3484.3744.4674.3423.849
Malamute 1616 × 1080 1,745,2807.7924.7834.7144.6204.711
Man 1024 × 1024 1,048,5767.5245.0585.0844.9365.711
Monarch 768 × 512 393,2167.184.1434.14424.0953.887
Mushrooms 321 × 481 154,4017.5855.1295.1615.0678.078
Parrots 768 × 512 393,2167.2563.9453.9883.8282.884
Pens 512 × 480 245,7607.4824.3684.2684.1883.393
Peppers 512 × 512 262,1447.5944.8284.8594.9425.747
Rainier 1920 × 1080 2,073,6007.0884.4994.4664.2987.699
Sun 2100 × 2034 4,271,4006.9503.2953.5772.7361.274
Yachts 512 × 480 245,7607.5604.3694.3024.1483.423
Zelda 720 × 576 414,7207.3344.2654.1274.1123.226
Average 4.6464.6664.516
1 Information entropy of the raw data. 2 Information entropy obtained by the multifunction local predictor (next-right). 3 Information entropy gained by the multifunction local predictor (next-bottom). 4 Information entropy achieved by the JPEG LS predictor.
Table 4. Compression achieved with different methods.
Table 4. Compression achieved with different methods.
ImageJPEG LSJPEG 2000PNGFLoCIC-ICFLoCIC-AC
Size 1bppSize 1bppSize 1bppSize 1bppSize 1bpp
Baboon196,3915.99200,2436.11203,8486.22206,4036.30206,5006.30
Balloon149,3222.88157,2963.03177,4263.42170,1413.28162,0943.13
Barbara165,5905.05168,0835.13186,0085.68178,7455.46189,9435.80
Barb2241,0274.65248,4004.79266,7565.15265,8775.13269,3525.20
Board188,8144.65195,6564.79208,7225.15213,5505.13205,1345.20
Boats202,3883.90210,8794.07224,2944.33226,5954.37223,7404.32
Cam.38,1374.6640,8504.9942,4035.1841,1425.0243,3105.29
Flower106,9463.48108,4593.53124,2914.05120,7873.93119,1303.88
Fruits113,0003.68114,4403.73129,5964.22123,8004.03123,7704.03
Girl201,9173.90210,6964.06224,7364.34226,6194.37218,5134.22
Gold230,5624.45238,7854.61243,0194.69249,5354.81245,0064.73
Hotel225,3164.35237,8614.59248,9164.80248,0764.79246,0134.75
Lena130,7044.00134,4174.10145,6344.44143,2594.37142,7894.36
Malamut2,234,6804.142,225,7574.062,394,8834.652,467,1924.432,295,1954.62
Man611,9094.67632,1634.82650,5994.96658,9215.03649,2984.95
Monarch180,1413.67187,5073.82208,8134.25202,5924.12202,1214.11
Mush.84,8154.4087,8184.5598,5815.1191,1614.7298,7165.12
Parrots170,1843.46172,9133.52193,3403.93190,0193.87188,9683.85
Pens119,1553.88121,3083.95133,3584.34130,9574.26129,1204.20
Peppers146,6304.48151,7394.63160,4654.90166,8225.01162,5624.96
Rainier878,7013.39891,8003.44933,2633.60890,0903.431,115,9314.30
Sun1,336,0352.501,727,1033.241,595,7902.991,499,3052.811,463,0572.74
Yachts115,1833.75120,2253.91132,1984.30126,7984.13127,9604.17
Zelda200,1423.86201,2733.88214,7994.14226,2164.36213,5694.12
Average 4.03 4.18 4.49 4.43 4.46
1 Size is given in bytes.
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content.

Share and Cite

MDPI and ACS Style

Žalik, B.; Strnad, D.; Kohek, Š.; Kolingerová, I.; Nerat, A.; Lukač, N.; Lipuš, B.; Žalik, M.; Podgorelec, D. FLoCIC: A Few Lines of Code for Raster Image Compression. Entropy 2023, 25, 533. https://doi.org/10.3390/e25030533

AMA Style

Žalik B, Strnad D, Kohek Š, Kolingerová I, Nerat A, Lukač N, Lipuš B, Žalik M, Podgorelec D. FLoCIC: A Few Lines of Code for Raster Image Compression. Entropy. 2023; 25(3):533. https://doi.org/10.3390/e25030533

Chicago/Turabian Style

Žalik, Borut, Damjan Strnad, Štefan Kohek, Ivana Kolingerová, Andrej Nerat, Niko Lukač, Bogdan Lipuš, Mitja Žalik, and David Podgorelec. 2023. "FLoCIC: A Few Lines of Code for Raster Image Compression" Entropy 25, no. 3: 533. https://doi.org/10.3390/e25030533

Note that from the first issue of 2016, this journal uses article numbers instead of page numbers. See further details here.

Article Metrics

Back to TopTop