Lossless compression method of RGB / RGBA image

CN116828171BActive Publication Date: 2026-09-25ZHEJIANG UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310830672.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-07-06
Publication Date
2026-09-25
Estimated Expiration
2043-07-06

AI Technical Summary

Technical Problem

现有的图像无损压缩方法,要么就压缩率不高或只能针对特定的图像数据进行压缩,如哈夫曼编码和游程编码;要么就编码过程复杂计算量大,如jpeg_l s算法和png算法

Benefits of technology

[0043]本发明的有益效果是:本发明根据不同像素点的数据特征,采用游程编码、索引编码和差分编码等多种编码方式,以对单个像素点只进行一次遍历的编码操作,在保证高压缩比的同时,通过简单的编码过程来有效的消除图像中的冗余信息,实现图像无损压缩。与目前传统的PNG无损压缩算法相比,得到了更好的压缩率,同时编码规则简单,压缩速度快,易于实现。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116828171B_ABST
    Figure CN116828171B_ABST
Patent Text Reader

Abstract

The present application relates to a kind of RGB / RGBA image lossless compression method, the method comprises: loading image data;Hash array is built, and difference value and index value are calculated;In turn judge whether the pixel value to be encoded meets the condition of run block, index block, small difference block, medium difference block, big difference block, super big difference block, transparency block and rewrite block, to determine the best encoding mode and encode;According to the order from left to right, from top to bottom, traverse the pixel point of image until encoding is completed.The unique encoding mode of the present application combines the advantages of run encoding, index encoding and difference encoding and other multiple encodings, can effectively reduce the redundancy information of data, improve compression ratio.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of information technology, and in particular to a lossless compression method for RGB / RGBA images. Background Technology

[0002] In the 21st century, the continuous development of human society has led to an ever-increasing demand for images and videos. Simultaneously, with advancements in science and technology, the storage space required for images and videos has also grown significantly. This massive storage demand necessitates maximizing storage capacity while minimizing the space occupied by images and videos. The latter relies heavily on image and video compression technologies, which are typically implemented through encoding and decoding. Through image and video encoding and decoding, we can drastically reduce the massive and redundant original data volume. For example, the original data volume of a 4K film might be 10TB, but after encoding, its actual storage space can reach the 10GB level, achieving a compression ratio of 1000 times. Therefore, image and video encoding and decoding technologies are of paramount importance.

[0003] The most commonly used method for representing color images is the RGB image, which uses three color channels: red, green, and blue. RGBA images add a transparency channel to the RGB representation to indicate the image's transparency.

[0004] Image compression techniques are divided into lossy compression and lossless compression. Lossy compression, in pursuit of higher compression ratios and greater space savings, often results in the loss or damage of some original image information. In contrast, lossless compression methods can reduce data size while maintaining image quality and can completely restore the original image after decompression. Lossless compression is suitable for fields with high image detail requirements, such as medical imaging and satellite imagery. Existing lossless image compression methods either have low compression ratios or are only applicable to specific image data, such as Huffman coding and run-length coding; or they involve complex coding processes and high computational costs, such as the JPEG_LS algorithm and the PNG algorithm. How to achieve lossless image compression while maintaining a high compression ratio and using the simplest possible coding process remains a pressing problem for researchers. Summary of the Invention

[0005] (a) Technical problems to be solved

[0006] The technical problem to be solved by the present invention is to provide a method for lossless compression of RGB / RGBA images that has simple encoding rules, fast encoding speed, and high compression ratio, in order to address the above-mentioned defects in the prior art.

[0007] (II) Technical Solution

[0008] To achieve the above-mentioned technical objectives, the main technical solutions adopted by the present invention include:

[0009] In a first aspect, the present invention provides a method for lossless compression of RGB / RGBA images, comprising:

[0010] S1, update the previous pixel data and load the pixel data to be encoded; if the image data has been read completely at this time, execute S13, otherwise execute S2;

[0011] S2, construct a hash array, calculate the data difference and index position between the pixel to be encoded and the previous pixel;

[0012] S3, compare the data of the pixel to be encoded with the data of the previous pixel: if they are the same, increment the run length by 1 and execute S4; if they are different, execute S5.

[0013] S4: Determine if the run length has reached 32 or if the pixel value to be encoded is the last pixel; if so, output the run block, set the run length to 0, and return to S1; otherwise, return directly to S1.

[0014] S5, determine if the run length is greater than 0; if yes, output using run-length block encoding, set the run length to 0, and execute S6; otherwise, execute S6.

[0015] S6. Based on the index position calculated in S2, compare the data to be encoded with the pixel value data stored in the hash array: if they are the same, use index block encoding to output and return to S1; if they are different, update the hash data according to the current data to be encoded and execute S7.

[0016] S7. Based on the difference calculated in S2, compare it with the encoding range of the small differential block: if it conforms to the encoding range of the small differential block, use the small differential block encoding for output and return to S1; otherwise, execute S8.

[0017] S8. Based on the difference calculated in S2, compare it with the encoding range of the intermediate differential block: if it conforms to the encoding range of the intermediate differential block, use the intermediate differential block encoding for output and return to S1; otherwise, execute S9.

[0018] S9. Based on the difference calculated in S2, compare it with the encoding range of the large differential block: if it conforms to the encoding range of the large differential block, use the large differential block encoding for output and return to S1; otherwise, execute S10.

[0019] S10: Based on the difference calculated in S2, compare it with the encoding range of the large differential block: if it meets the encoding range of the super-large differential block, use the super-large differential block encoding for output and return to S1; otherwise, execute S11.

[0020] S11: Based on the difference calculated in S2, compare the transparency value of the pixel to be encoded with that of the previous pixel: if they are equal, use transparent block encoding to output and return to S1; otherwise, execute S12.

[0021] S12, output using rewritten block encoding, and return to S1;

[0022] S13, output the data and name it in co i data format.

[0023] This invention proposes a lossless compression method for RGB / RGBA images. Based on the data characteristics of the pixels to be encoded, the RGBA values ​​of the pixels to be encoded are treated as a whole. By sequentially judging whether the pixel values ​​to be encoded satisfy the conditions of run-length block, index block, small difference block, medium difference block, large difference block, super-large difference block, transparency block, and rewrite block, the optimal encoding method is determined and encoded. While ensuring a high compression ratio, redundant information is effectively removed, achieving lossless image compression.

[0024] Further, in S2, the difference includes red difference, green difference, blue difference, transparency difference, red-green difference, and blue-green difference, wherein...

[0025] The red difference is represented as dr = px.r - px_pre.r;

[0026] The green difference is represented as dg = px.g - px_pre.g;

[0027] The blue difference is represented as db = px.b - px_pre.b;

[0028] The difference in transparency is expressed as dr = px.a - px_pre.a;

[0029] The red-green difference is represented as dr-dg;

[0030] The blue-green difference is represented as db-dg.

[0031] Furthermore, the run block includes 8 bits, of which the first 3 bits are 000, which are the run block flag bits, indicating that the pixel to be encoded uses the run block encoding method; the last 5 bits are the run length data bits, and one run block can represent up to 32 repeating pixels.

[0032] Furthermore, the index block includes 8 bits, of which the first 3 bits are 001, which are the flag bits of the index block, indicating that the pixel to be encoded adopts the index block encoding method; the last 5 bits are the index position data bits, and an index block represents the pixel information stored in the hash array at the index.

[0033] The index blocks employ dictionary encoding, storing previously encountered values ​​in a dictionary, with each value associated with a unique index. Each value in the dataset is then replaced with its corresponding index, thus achieving data compression.

[0034] Furthermore, the small differential block includes 11 bits, of which the first 3 bits are 010, which are the flag bits of the index block, indicating that the pixel to be encoded adopts the small differential block encoding method; the last 8 bits are differential data bits, including 2 bits each of dr, dg, db, and da.

[0035] Furthermore, the intermediate differential block includes 16 bits, of which the first 3 bits are 011, which are the flag bits of the index block, indicating that the pixel to be encoded adopts the intermediate differential block encoding method; the last 13 bits are differential data bits, including 4 bits of dr and 3 bits each of dg, db, and da.

[0036] Furthermore, the large differential block includes 21 bits, of which the first 3 bits are 100, which are the flag bits of the index block, indicating that the pixel to be encoded adopts the large differential block encoding method; the last 18 bits are differential data bits, including 6 bits of dr and dr-dg, db-db, and da, each of which is 4 bits.

[0037] Furthermore, the super-large differential block includes 25 bits, of which the first 3 bits are 101, which are the flag bits of the index block, indicating that the pixel to be encoded adopts the super-large differential block encoding method; the last 22 bits are differential data bits, including 7 bits of dr and dr-dg, db-db, and da, each 5 bits.

[0038] Specifically, if the pixel to be encoded differs from the array information corresponding to its index position and does not meet the conditions for a run-length block, then the differential block is considered. The differential block employs the concept of residual coding. By calculating the difference between each data point and the previous data point, the data is encoded as a residual sequence. Its advantage lies in its ability to more effectively represent trends and patterns in the data. Since residual coding focuses only on the differences between data points, rather than the absolute values ​​themselves, it can reduce data redundancy to some extent. By layering the differential blocks and using shorter code lengths to represent smaller residuals, the patterns between pixels are fully explored, improving the compression ratio.

[0039] Furthermore, the transparency block comprises 27 bits, wherein the first 3 bits are 110, and the last 24 bits are pixel data bits, including 8 bits each for px.r, px.g, and px.b.

[0040] Furthermore, the rewrite block includes 35 bits, wherein the first 3 bits are 111, and the last 32 bits are pixel data bits, including 8 bits each for px.r, px.g, px.b, and px.a.

[0041] In this process, most pixels can be encoded by judging the conditions of the four residual blocks. However, some edge pixels may not be able to be represented by the given residual blocks due to the large residual values. Therefore, rewritten encoding blocks are used to save them.

[0042] (III) Beneficial Effects

[0043] The beneficial effects of this invention are as follows: Based on the data characteristics of different pixels, this invention employs multiple encoding methods such as run-length encoding, indexed encoding, and differential encoding to perform an encoding operation on a single pixel only once. While ensuring a high compression ratio, it effectively eliminates redundant information in the image through a simple encoding process, achieving lossless image compression. Compared with current traditional PNG lossless compression algorithms, it achieves a better compression ratio, while also having simple encoding rules, fast compression speed, and ease of implementation. Attached Figure Description

[0044] Figure 1 A flowchart illustrating a lossless compression method for RGB / RGBA images according to the present invention is shown schematically.

[0045] Figure 2 Eight types of coded block diagrams;

[0046] Figure 3 This is a schematic diagram of the 4x4 RGBA compression process. Detailed Implementation

[0047] To better explain and facilitate understanding of the present invention, the present invention will be described in detail below with reference to the accompanying drawings and specific embodiments.

[0048] To better understand the above technical solutions, exemplary embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although exemplary embodiments of the present invention are shown in the drawings, it should be understood that the present invention can be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that the present invention can be understood more clearly and thoroughly, and that the scope of the present invention can be fully conveyed to those skilled in the art.

[0049] Example 1

[0050] Reference Figure 1 This invention provides a lossless compression method for RGB / RGBA images, comprising:

[0051] S1: Pixels are loaded in the source image from left to right and top to bottom. Depending on the number of channels in the source image, the corresponding byte is read from the image data each time, and the color information of a single pixel is stored in the pixel structure px. Before each acquisition of data to be encoded, the previous pixel structure px_prev is updated (initialized to {0,0,0,255}). If the image data has been completely read at this point, proceed to S13; otherwise, proceed to S2.

[0052] S2, construct a hash array, large enough to store the data of a 32-pixel structure (px). Calculate the differences between each channel of the data of the pixel to be encoded and the previous pixel (including red, green, blue, opacity, red-green, and blue-green differences). Use the hash function pos = (3*px.r + 5*px.g + 7*px.b + 11*px.a) % 32 to map the pixel structure to an unsigned index position less than 32.

[0053] S3: Determine whether the data of the pixel to be encoded is the same as the data of the previous pixel. If so, increment the run length by 1 and execute S4; otherwise, execute S5.

[0054] S4 determines whether the run length has reached 32 or whether the pixel value to be encoded is the last pixel. If so, the run block is output. The format of the run block is 3 bits of flag 000 + 5 bits (run length - 1) 11111, and the run length is set to 0. Then return to S1. Otherwise, return directly to S1.

[0055] S5, determine if the run length is greater than 0. If so, use run-length block encoding for output and set the run length to 0, then execute S6; otherwise, execute S6.

[0056] S6: Based on the index position calculated in S2, compare whether the data to be encoded is consistent with the pixel value data stored in the hash array. If yes, use index block encoding for output. The format of the index block is 3-bit flag 001 + 5-bit index position pos, and return to S1; otherwise, update the hash data according to the current data to be encoded, and execute S7.

[0057] S7: Based on the difference calculated in S2, compare whether the difference to be encoded conforms to the encoding range of the small differential block. The encoding range of the small differential block is dr, dg, db, da: [-2, 1]. If yes, map dr, dg, db, da to the unsigned number range [0, 3]. Output using small differential block encoding, the format of which is 3 bits 010 + 2 bits (dr + 2) + 2 bits (dg + 2) + 2 bits (db + 2) + 2 bits (da + 2). Return to S1; otherwise, execute S8.

[0058] S8: Based on the difference calculated in S2, compare whether the difference to be encoded conforms to the encoding range of the middle differential block. The encoding range of the middle differential block is dg:[-8,7], dr-dg, db-dg, da=[-4,3]. If yes, map dg to [0,15], dr-dg, db-dg, da to [0,7]. Output using middle differential block encoding, the format of which is 3 bits 011 + 4 bits (dg+8) + 3 bits (dr-dg+4) + 3 bits (db-dg+4) + 3 bits (da+4), and return to S1. Otherwise, execute S9.

[0059] S9: Based on the difference calculated in S2, compare whether the difference to be encoded conforms to the encoding range of the large differential block. The encoding range of the large differential block is dg:[-32,31], dr-dg, db-dg, da = [-8,7]. If so, map dg to [0,63], dr-dg, db-dg, and da to [0,15]. Output using large differential block encoding. The format of the medium differential block is 3 bits 100 + 6 bits (dg+32) + 4 bits (dr-dg+8) + 4 bits (db-dg+8) + 4 bits (da+8), and return to S1. Otherwise, execute S10.

[0060] S10: Based on the difference calculated in S2, compare whether the difference to be encoded conforms to the encoding range of the super-large differential block. The encoding range of the super-large differential block is dg: [-64, 63], dr-dg, db-dg, da = [-16, 15]. If yes, map dg to [0, 127] and dr-dg, db-dg, da to [0, 31]. Output using super-large differential block encoding. The format of the super-large differential block is 3bit101 + 7bit(dg+64) + 5bit(dr-dg+16) + 5bit(db-dg+16) + 5bit(da+8), and return to S1. Otherwise, execute S11.

[0061] S11: Based on the difference calculated in S2, compare the transparency value of the pixel to be encoded with that of the previous pixel. If they are equal, output using transparency block encoding and return to S1. Otherwise, execute S12.

[0062] S12 uses rewritten block encoding for output and returns to S1.

[0063] S13, the original RGB / RGBA image is compressed into a combination of 8 coded blocks, and this new format is named the coi data format.

[0064] Reference Figure 2 , Figure 2The first image represents a run-length block. Specifically, an 8-bit block is used to represent a run-length block. The first 3 bits (000) are the run-length block flag, indicating that the pixel to be encoded uses the run-length block encoding method. The last 5 bits are the run-length data bits. A run-length block can represent a maximum of 32 repeating pixels. Since 5 bits can only represent the unsigned number range [0,..,31], while the run-length range is [1,..,32], the run-length must be subtracted by one before encoding.

[0065] For example, suppose there are 40 consecutive pixels with RGBA values ​​of {0,0,0,255}, and the 41st pixel is {0,0,0,254}. The initial value of Run is 0. Step 1: Compare the first RGBA value with the previous pixel's RGBA value (initialized px_prev to {0,0,0,255}). The result is that they are equal, and runlength equals runlength + 1. At this point, Run is 1, which is less than 32, so the next pixel is loaded. Repeat step 1. When the 32nd pixel is loaded, runlength equals 32, reaching the maximum encoding range of the run-length block; therefore, output 000 (run-length block flag) + 11111 (runlength - 1). Then, set Run to 0. Continue repeating step 1. When the 41st pixel is loaded, the RGBA pixel value is not equal to the previous RGBA pixel value. At this point, the run length Run is 8, which is greater than 0, indicating that run-length encoding has been performed previously. The output should be 000 (flag bit) 00111 (run length Run-1), then Run should be set to 0. At this point, the first 40 identical pixel values ​​have been encoded using only two run blocks. The 41st pixel has not yet been encoded; the index condition check is now performed.

[0066] Reference Figure 2 , Figure 2 The second diagram represents an index block. During operation, if a pixel's RGBA value exactly matches the RGBA value at a certain index position in the index array, then that index position will be used for encoding. If the current pixel's RGBA value cannot be found in the dictionary, the RGBA value corresponding to the current index position is updated. It is ensured that the array always contains information on the 32 most recently occurring pixels. Specifically, an index block is represented using 8 bits. The first 3 bits (001) are the index block flag, indicating that the pixel to be encoded uses the index block encoding method. The last 5 bits are the index position data bits; an index block can represent the pixel information stored in the hash array at that index.

[0067] For example, if there are three pixels to be encoded, rgba = {0,0,1,254}, {5,5,5,254}, and {0,0,1,254}, and these three pixels are all different, then run-length encoding cannot be used, and the condition of index encoding block needs to be checked.

[0068] Step 1: Initialize the hash index array. The 32 positions store the pixel information {0,0,0,255}. Step 2: Load the first pixel and calculate pos = (0*3 + 0*5 + 1*7 + 254*11) % 32 = 17. Since index

[17] {0,0,0,255} is not equal to the pixel value of the first pixel {0,0,1,254}, it means that the same pixel has not appeared before. Update the pixel information corresponding to index

[17] to {0,0,1,254}. Step 3: Load the second pixel and calculate pos = (5*3 + 5*5 + 5*7 + 254*11) % 32 = 21. Similarly, it is not equal to index

[21] . Update index

[21] to {5,5,5,254}. Load the third pixel, pos = (0*3 + 0*5 + 1*7 + 254*11) % 32 = 17. The data already exists in the index array index

[17] (this RGBA data is the same as the first RGBA data). The best encoding method is the index block index. The bitstream output is 001 010001, where 001 is the flag bit of the index block, and the following 010001 is the unsigned number representation of index position 17.

[0069] Reference Figure 2 , Figure 2 The third image represents a small differential block, the fourth a medium differential block, the fifth a large differential block, and the sixth a very large differential block. In this invention, the residual is calculated by treating the RGBA value of the pixel to be encoded as a whole and calculating the difference between it and the RGBA value of the previous pixel. The difference is categorized into six types: red difference dr = px.r - px_pre.r, green difference dg = px.g - px_pre.g, blue difference db = px.b - px_pre.b, transparency difference dr = px.a - px_pre.a, red-green difference dr - dg, and blue-green difference db - dg. Based on these six residuals, the differential blocks are divided into small, medium, large, and very large differential blocks. The code length of these differential blocks increases sequentially, and the range of residuals they can represent also increases sequentially. Since the principles of these four types of differential blocks are basically similar, only the code length and the range they can include are different, so only the small differential block will be explained here.

[0070] Specifically, 11 bits are used to represent a small differential block. The first 3 bits (010) are the index block flags, indicating that the pixel to be encoded uses the small differential block encoding method. The last 8 bits are the differential data bits, representing 2 bits of dr, dg, db, and da, respectively.

[0071] For example, suppose the pixel to be encoded is rgba = {0,0,1,254}, the previous pixel is {0,0,0,255}, and the index array index

[17] = {0,0,0,255}. First, {0,0,1,254} is different from the previous pixel and index

[17] is not equal to {0,0,1,254}, so it enters the condition judgment of the small residual block. By calculating the difference, we know that dr = 0, dg = 0, db = 1, da = -1, and the encoding range of the small residual block is dr, dg, db, da = {-2,..1}. Therefore, the best encoding method is the small residual block diff0. To map the above residuals into unsigned numbers that can be represented by 2 bits, i.e. {0,..,3}, we only need to add 2 to dr, dg, db, da. The bitstream output is 010 10 10 11 01. 010 is the flag bit of the small differential block, and 10 10 11 01 are the binary representations of dr+2, dg+2, db+2, and da+2, respectively.

[0072] Reference Figure 2 , Figure 2 The seventh image represents the transparency block, and the eighth image represents the rewrite block. A rewrite block uses 35 bits to represent a single block. The first 3 bits (111) are the index block flag, indicating that the pixel to be encoded uses the rewrite block encoding method. The last 32 bits are the pixel data bits, 8 bits each for px.r, px.g, px.b, and px.a. Considering that in real images, a significant number of pixels share the same transparency channel as the previous pixel, saving the transparency information and rewriting only the RGB channels improves the compression ratio. The transparency compression block uses 27 bits to represent a single block. The first 3 bits (110) are the index block flag, indicating that the pixel to be encoded uses the transparency block encoding method. The last 24 bits are the pixel data bits, 8 bits each for px.r, px.g, and px.b.

[0073] For example, suppose two pixels to be encoded have rgba = {32, 64, 128, 128} and {0, 196, 0, 128}, and the previous pixel is {94, 94, 95, 254}, and index[pos] is not equal to px. First, the first given pixel is encoded. Since {32, 64, 128, 128} is different from the previous pixel and index[pos] is not equal to px, the conditional check of the residual block is entered. Calculating the difference, da = -126, meaning that even a very large residual block with an encoding range of da = {-16, ..., 15} cannot represent this pixel. The alpha channel difference is not equal to 0, so only a rewrite block can be used to record this pixel. The bitstream output is 111 00100000100000 1000000010000000. Here, 111 is the flag bit for the rewrite block, and each subsequent byte represents the px four-channel pixel value: 32, 64, 128, 128. Next, the previous pixel is updated to {32, 64, 128, 128}, and then the second pixel {0, 196, 0, 128} is loaded. According to the previous rules, the second pixel clearly does not meet the conditions for run-length blocks and index blocks, and by calculating dg = 132, even the encoding range of the large residual block, d = {-64, ..., 63}, cannot represent this pixel. Since the alpha channel difference is equal to 0, this pixel can be recorded as an alpha block. The bitstream output is 1100000000 1100000 00000000. Here, 110 is the flag bit for the alpha block, and each subsequent byte represents the px three-channel pixel value: 0, 196, 0.

[0074] Example 2

[0075] Reference Figure 3 The encoding method of this invention is illustrated using a 4x4 RGBA block example. First, the previous pixel structure px_prev (initialized to {0,0,0,255}) is loaded. Pixels are loaded in the order of left to right and top to bottom of the source image. The first pixel is different from the previous pixel, and its data is not stored in the hash array. By calculating dr=0, dg=0, db=1, da=-1, it is determined that the optimal encoding method is the small differential block diff0. The difference is mapped to the range [0,3], that is, the difference of each channel is increased by 2. The bitstream output is 01010 10 11 01.

[0076] Update the previous pixel, px_prev = {0,0,1,254}. Then load the second pixel, find it's the same as the previous pixel, and increment the run length by one. Load the third pixel, find it's the same as the previous pixel, and increment the run length by one.

[0077] The fourth pixel is found to be the same as the previous pixel, so the run length is incremented by one. The fifth pixel is then loaded; it is different from the previous pixel and has a run length greater than 0. The optimal encoding method is a run-length block (run). The bitstream output is 000 00010. Here, 00010 represents the run length minus one, since the run length range is [1, 32], while a 5-bit unsigned number represents the range [0, 31]. Calculations show dr = 5, dg = 5, db = 5, da = 0, dr-dg = 0, db-dg = 0. The optimal encoding method is a middle differential block (diff1). Mapping dg to the range [0, 15], i.e., adding 8 to the difference, results in 13. Mapping dr-dg, db-dg, and da to the range [0, 7], i.e., adding 4 to each difference, results in 4, 4, 4 respectively. The bitstream output is 011 1101 100100100.

[0078] Load the 6th pixel and calculate pos = (0*3 + 0*5 + 1*7 + 254*11) % 32 = 17. It is found that the data already exists in the index array index

[17] (this RGBA data is the same as the first RGBA data). The optimal encoding method is the index block. The bitstream output is 001 010001.

[0079] Loading the 7th pixel, calculations show dr = 31, dg = 31, db = 31, da = 0, dr-dg = 0, db-dg = 0. The optimal encoding method is a large difference block luma0. Mapping dg to the range [0, 64], i.e., adding 32 to the difference, results in 63. Mapping dr-dg, db-dg, and da to the range [0, 15], i.e., adding 8 to each difference, results in 8, 8, and 8 respectively. The bitstream output is 1001111111000 1000 1000.

[0080] Loading the 8th pixel, calculations show dr = 63, dg = 63, db = 63, da = 0, dr-dg = 0, db-dg = 0. The optimal encoding method is the large differential block luma1. Mapping dg to the range [0, 128], i.e., adding 64 to the difference, yields 127. Mapping dr-dg, db-dg, and da to the range [0, 32], i.e., adding 16 to each difference, yields 16, 16, and 16 respectively. The bitstream output is 101 1111111 10000 10000 10000.

[0081] Loading the 9th pixel, we find that da = -126, the number of channels 'a' is different, and there are no duplicate data in the index array. This does not meet the encoding conditions of the first 7 coding blocks, so we can only use the rewritten block RGBA to record the information of this pixel. The bitstream output is 1011111111 10000 10000 10000.

[0082] Loading the 10th pixel, dg = 196 and the pixel does not exist in the index array. Fortunately, this pixel has the same transparency as the previous pixel, so the optimal encoding method is RGB for transparency blocks. The bitstream output is 110 000000001100000000000000.

[0083] Load pixels 11-16, all with identical data. The run length is 6, but we've reached the end of the image, so a run block is needed. The output bitstream is 000 00101. At this point, the 4x4 RGBA image block compression is complete. Calculate the compressed file size by outputting the bitstream in compressed blocks:

[0084] diff0(11)+run(8)+diff1(16)+index(8)+luma0(21)+luma1(25)+rgb a(35)+rgb(27)+run(8)=159 bits.

[0085] In actual compression, zeros are added to the end of any segment less than one byte. In the above embodiment, only one zero is needed, resulting in 160 bits or 20 bytes. The original image size is (number of channels) 4 * (number of pixels) 16 = 64 bytes, achieving a compression rate of 31.25%.

[0086] This invention proposes a lossless compression method for RGB / RGBA images. This method uses a unique encoding scheme to encode the data characteristics of different pixels, thereby improving compression efficiency, increasing the compression ratio, and greatly reducing data redundancy.

[0087] Those skilled in the art will understand that various modifications and variations can be made to this invention without departing from its spirit and scope. Therefore, if these modifications and variations fall within the scope of the claims of this invention and their equivalents, then this invention should also include these modifications and variations.

[0088] In the description of this invention, it should be understood that the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of indicated technical features. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of this invention, "a plurality of" means two or more, unless otherwise explicitly specified.

[0089] In this invention, unless otherwise explicitly specified and limited, the terms "installation," "connection," "linking," and "fixing," etc., should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral part; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; they can refer to the internal communication of two components or the interaction between two components. Those skilled in the art can understand the specific meaning of the above terms in this invention according to the specific circumstances.

[0090] In this invention, unless otherwise explicitly specified and limited, "above" or "below" the second feature can mean that the first and second features are in direct contact, or that they are in indirect contact through an intermediate medium. Furthermore, "above," "over," or "on top" the second feature can mean that the first feature is directly above or diagonally above the second feature, or simply indicates that the first feature is at a higher horizontal level than the second feature. "Below," "below," or "beneath" the second feature can mean that the first feature is directly below or diagonally below the second feature, or simply indicates that the first feature is at a lower horizontal level than the second feature.

[0091] In the description of this specification, the terms "one embodiment," "some embodiments," "embodiment," "example," "specific example," or "some examples," etc., refer to specific features, structures, materials, or characteristics described in connection with that embodiment or example, which are included in at least one embodiment or example of the present invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples. Moreover, without contradiction, those skilled in the art can combine and integrate the different embodiments or examples described in this specification, as well as the features of different embodiments or examples.

[0092] Although embodiments of the present invention have been shown and described above, it is understood that the above embodiments are exemplary and should not be construed as limiting the present invention. Those skilled in the art can make modifications, alterations, substitutions and variations to the above embodiments within the scope of the present invention.

Claims

1. A lossless compression method for RGB / RGBA images, characterized in that: include S1, update the previous pixel data, load the pixel data to be encoded; If the image data has been loaded and encoded at this point, execute S13; otherwise, execute S2. S2, construct a hash array, calculate the data difference and index position between the pixel to be encoded and the previous pixel; S3, compare the data of the pixel to be encoded with the data of the previous pixel: if they are the same, increment the run length by 1 and execute S4; if they are different, execute S5. S4: Determine if the run length has reached 32 or if the pixel value to be encoded is the last pixel; if so, output the run block, set the run length to 0, and return to S1; Otherwise, return directly to S1; S5, determine if the run length is greater than 0; if yes, output using run-block encoding, set the run length to 0, and execute S6; otherwise, execute S6. S6, based on the index position calculated in S2, compare the data to be encoded with the pixel value data stored in the hash array: if they are the same, use index block encoding to output and return to S1; If they are different, update the hash data according to the current data to be encoded, and execute S7; S7. Based on the difference calculated in S2, compare whether the difference to be encoded conforms to the encoding range of the small differential block. The encoding range of the small differential block is dr,dg,db,da:[-2,1]. If yes, map dr, dg, db, da to the unsigned number range [0, 3], and output using small differential block encoding. The format of the small differential block is 3 bits 010 + 2 bits (dr + 2) + 2 bits (dg + 2) + 2 bits (db + 2) + 2 bits (da + 2), and return to S1; otherwise, execute S8. S8. Based on the difference calculated in S2, compare whether the difference to be encoded conforms to the encoding range of the middle differential block. The encoding range of the middle differential block is dg:[-8,7],dr-dg,,db-dg,da=[-4,3]; If so, map dg to [0,15], dr-dg, db-dg, and da to [0,7], and output using medium differential block encoding. The medium differential block format is 3 bits 011 + 4 bits (dg+8) + 3 bits (dr-dg+4) + 3 bits (db-dg+4) + 3 bits (da+4), and return to S1; Otherwise, execute S9; S9, based on the difference calculated in S2, compare whether the difference to be encoded conforms to the encoding range of the large difference block. The encoding range of the large difference block is dg:[-32,31], dr-dg, db-dg. da = [-8, 7]; If so, map dg to [0,63], dr-dg, db-dg, and da to [0,15], and output using large differential block encoding. The format of the large differential block is 3 bits 100 + 6 bits (dg+32) + 4 bits (dr-dg+8) + 4 bits (db-dg+8) + 4 bits (da+8), and return to S1; Otherwise, execute S10; S10: Based on the difference calculated in S2, compare it with the encoding range of the super-large differential block: if it meets the encoding range of the super-large differential block, use the super-large differential block encoding for output and return to S1; otherwise, execute S11. S11: Based on the difference calculated in S2, compare the transparency value of the pixel to be encoded with that of the previous pixel: if they are equal, use transparent block encoding to output and return to S1; otherwise, execute S12. S12, output using rewritten block encoding, and return to S1; S13, output the data and name it in coi data format.

2. The lossless compression method for RGB / RGBA images as described in claim 1, characterized in that: In S2, the differences include red difference, green difference, blue difference, transparency difference, red-green difference, and blue-green difference, wherein... The red difference is represented as dr = px.r - px_pre.r; The green difference is represented as dg = px.g - px_pre.g; The blue difference is represented as db = px.b - px_pre.b; The difference in transparency is represented as da = px.a - px_pre.a; The red-green difference is represented as dr-dg; The blue-green difference is represented as db-dg.

3. The lossless compression method for RGB / RGBA images as described in claim 1, characterized in that: The run block consists of 8 bits, of which the first 3 bits are 000, which are the run block flag bits, indicating that the pixel to be encoded uses the run block encoding method; the last 5 bits are the run length data bits, and one run block can represent a maximum of 32 repeating pixels.

4. The lossless compression method for RGB / RGBA images as described in claim 1, characterized in that: The index block consists of 8 bits, of which the first 3 bits are 001, which are the flag bits of the index block, indicating that the pixel to be encoded uses the index block encoding method; the last 5 bits are the index position data bits, and an index block represents the pixel information stored in the hash array at the index.

5. The lossless compression method for RGB / RGBA images as described in claim 1, characterized in that: The small differential block consists of 11 bits, of which the first 3 bits are 010, which are the index block flag bits, indicating that the pixel to be encoded uses the small differential block encoding method; the last 8 bits are differential data bits, including 2 bits each of dr, dg, db, and da.

6. The lossless compression method for RGB / RGBA images as described in claim 1, characterized in that: The intermediate differential block consists of 16 bits, of which the first 3 bits are 011, which are the index block flag bits, indicating that the pixel to be encoded uses the intermediate differential block encoding method; the last 13 bits are differential data bits, including 4 bits of dr and 3 bits each of dg, db, and da.

7. The lossless compression method for RGB / RGBA images as described in claim 1, characterized in that: The large differential block consists of 21 bits, of which the first 3 bits are 100, which are the index block flag bits, indicating that the pixel to be encoded uses the large differential block encoding method; the last 18 bits are differential data bits, including 6 bits of dr and dr-dg, db-db, and da, each of which is 4 bits.

8. The lossless compression method for RGB / RGBA images as described in claim 1, characterized in that: The super-large differential block consists of 25 bits, of which the first 3 bits are 101, which are the index block flag bits, indicating that the pixel to be encoded uses the super-large differential block encoding method; the last 22 bits are differential data bits, including 7 bits of dr and dr-dg, db-db, and da, each 5 bits.

9. The lossless compression method for RGB / RGBA images as described in claim 1, characterized in that: The transparency block consists of 27 bits, of which the first 3 bits are 110, which are the index block flag bits, indicating that the pixel to be encoded uses the transparency block encoding method; the last 24 bits are the pixel data bits, including 8 bits each for px.r, px.g, and px.b.

10. The lossless compression method for RGB / RGBA images as described in claim 1, characterized in that: The rewrite block consists of 35 bits, of which the first 3 bits are 111, which are the flag bits of the index block, indicating that the pixel to be encoded adopts the encoding method of the rewrite block; the last 32 bits are the pixel data bits, including 8 bits each of px.r, px.g, px.b, and px.a.

Citation Information

Patent Citations

  • Binary image compression coding and decoding method based on Base62 coding

    CN114286102A

  • Image encoding device and method, and encoded image decoding device and method

    CN1692626A