A method for compressing and decompressing images
By extracting the resolution and color specifications of the picture, and using specific scanning and conversion methods to compress and decompress the picture, it solves the problems of large picture storage space and poor definition in the existing technology, achieves efficient picture data processing, and maintains the quality of the picture. quality.
Patent Information
- Application Number
- CN202110495432.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-05-07
- Publication Date
- 2024-09-13
- Estimated Expiration
- 2041-05-07
AI Technical Summary
When existing technology reduces image storage space, it is difficult to maintain image clarity, and the compression and decompression process will cause data loss, affecting user experience.
By obtaining the resolution and color specifications of the image, extracting the pixels and color table for compression, processing the pixel data by scanning from the upper left corner to the lower right corner, filling the color table and counting continuous colors, converting it to 8bp png format, combined with DMA Read and display populate data for efficient compression and decompression.
Achieving a 30% compression rate of image size while maintaining image clarity, avoiding data loss, and improving storage space utilization and user experience.
Smart Images

Figure CN113223100B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of picture processing, and in particular to a method for compressing and decompressing pictures. Background Art
[0002] With the advancement of technology, the performance of mobile phones has been greatly improved, and at the same time, there are more and more pictures in mobile phones. The sources of pictures may include multiple aspects, such as user photos, web page pictures, pictures generated by social software, etc. As the number of pictures increases, a large amount of storage space in the mobile phone will be occupied. How to reduce the storage space occupied by pictures while ensuring the quality of pictures is a technical problem that needs to be solved urgently.
[0003] Currently, the storage space occupied by images is reduced mainly by cropping or compressing the images.
[0004] However, if you crop the image with a uniform height such as 1024 pixels, the image clarity will be reduced when it is displayed on a mobile phone with a high pixel density. If the image height is less than the uniform cropping height, the image will be stretched and displayed with poor clarity. If you compress the image with a uniform size such as 200kb, data loss will occur and the image clarity will also be reduced.
[0005] In addition, when the image is decompressed after compression, data will be lost and the clarity of the image will also be reduced. Summary of the invention
[0006] The purpose of the present invention is to provide a method for compressing and decompressing an image to solve the technical problems of large image storage space, poor clarity after image compression, data loss and poor user experience proposed in the above background technology.
[0007] To achieve the above object, the present invention provides the following technical solution: a method for compressing an image, comprising the following steps:
[0008] Step S1, obtaining the resolution of the image to be compressed, where the resolution includes two parameters H and W;
[0009] Step S2, obtaining the color specification of the image, that is, the number of colors in the entire image;
[0010] Step S3, extracting pixels and color table of the image for compression;
[0011] Wherein, step S3 includes:
[0012] Step S301, first save the color table uint16 ColorTab
[256] ;
[0013] uint8 colorindex, Lastcolor, pixel data uint8 pixdata[H*W];
[0014] Step S302, before starting compression, the pixels prepared in step S301 need to be cleared to enter the compression preparation process;
[0015] Step S303, starting from the upper left corner of the pixel to the lower right corner, pixel retrieval and compression.
[0016] Preferably, the step S303 further includes the following steps:
[0017] Step S3031, reading pixel color;
[0018] Step S3032, check whether the color of the pixel exists in colortab, and if so, return the offset value colorindex of the color in the color table;
[0019] If it does not exist, add the color to the color table and return the offset value colorindex of the current color; this process completes the filling of the color table and pixel compression to provide counts.
[0020] Preferably, if the color in the result returned in step S3032 is the same as lastcolor, pixcount++ indicates the occurrence of continuous colors; then the number of pixels in pix is increased but not saved.
[0021] Preferably, if the color returned in step S3032 is different from lastcolor, it means that the color has changed and the previously counted color needs to be saved; the previous pixels have completed a compression, the data is saved, and the color saving of the next group of pixels begins.
[0022] Preferably, in step S2, it is also necessary to determine whether the image format is an 8bp png image. If so, the next step of compression is directly performed. If not, the image is compressed into an 8bp png image through an 8bp approximate conversion method.
[0023] Preferably, H in step 301 is the height of the image to be compressed, W is the width of the image to be compressed, and uint8pixcount is the statistical number of the same continuous pixels.
[0024] The method for decompressing an image based on one of the above is as follows:
[0025] Step S100, read the color table corresponding to the picture ID from Flash and save it in colTab;
[0026] Step S200, reading 512 bytes of image data from Flash;
[0027] Step S300, start DMA to read the subsequent 512 bytes of data from Flash;
[0028] Step S400, filling pixels onto the display screen according to colors and corresponding quantities;
[0029] Step S500, read the data from the buffer to complete drawing, if completed, wait for the DMA data reading to be completed, otherwise repeat step S400;
[0030] Step S600, switch DMA data reading to complete, if yes, complete decompression, otherwise continue DMA to obtain the remaining data, and return to step S400, repeat the corresponding steps until the image decompression is completed.
[0031] Compared with the prior art, the present invention has the following beneficial effects:
[0032] Through the above method, the present invention can make the size of the picture 32*32, the pixel color table size 256*2=512 bytes, the pixel content 1024 pixels, and the total size 1024+512 bytes; the compressed size is: pixel color table 512 bytes, pixel content is about 320 bytes, a total of 832 bytes; the compressed pixel content is about 1 / 3 of the original image; when the picture size is relatively large, the compression rate can be close to 30%, so the compressed picture has the beneficial effects of small storage space, clear picture, no data loss, or reduced storage space after compression and decompression, clear picture, and no data loss. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] Figure 1 This is a flow chart of a picture compression method of the present invention:
[0034] Figure 2 , Figure 3 This is a flow chart of extracting picture pixels and color table of the present invention;
[0035] Figure 4 This is a flow chart of the picture compression and decompression method of the present invention;
[0036] Figure 5 This is a picture compression effect diagram of the present invention. DETAILED DESCRIPTION
[0037] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0038] See also Figure 1-Figure 3 , an embodiment provided by the present invention:
[0039] A method for compressing an image comprises the following steps:
[0040] Step S1, obtaining the resolution of the image to be compressed, where the resolution includes two parameters H and W;
[0041] Step S2, obtaining the color specification of the image, that is, the number of colors in the entire image;
[0042] Generally, low-end embedded devices use RGB565-color images. If the image format is png-8, the entire image has 256 colors.
[0043] If the image is png-8, it will start the image compression process. If it is not png-8, it will first enter the image conversion process, and the color will enter the approximate compression process to keep the color of the entire image within 256 colors; png-8 images are generally saved through color table + pixel data;
[0044] Without compression, the storage space size of a 240*240 png8 image is 256*2 bytes for the color table + 57600 bytes for the pixel data. The total size is 58112 bytes.
[0045] Step S3, extracting pixels and color table of the image for compression;
[0046] Among them, pixel refers to each pixel of the image. For example, if the resolution of an image is 240*240, the pixels contained in the entire image are 57,600 pixels. The color table is an index table of the image color, and the colors of the entire image are saved in the color table.
[0047] like Figure 2 , Figure 3 As shown, in order to extract the picture pixels and the color table, step S3 includes the following steps:
[0048] Step S301, first save the color table uint16 ColorTab
[256] ;
[0049] uint8 colorindex, Lastcolor, pixel data uint8 pixdata[H*W];
[0050] Step S302, before starting compression, the pixels prepared in step S301 need to be cleared to enter the compression preparation process;
[0051] Step S303, starting from the upper left corner of the pixel to the lower right corner, pixel retrieval and compression.
[0052] The step S303 also includes the following steps:
[0053] Step S3031, reading pixel color;
[0054] Step S3032, check whether the color of the pixel exists in colortab, and if so, return the offset value colorindex of the color in the color table;
[0055] If it does not exist, add the color to the color table and return the offset value colorindex of the current color; this process completes the filling of the color table and pixel compression to provide counts.
[0056] If the color in the result returned in step S3032 is the same as lastcolor, pixcount++ indicates the occurrence of continuous colors; then the number of pixels in pix is increased but not saved.
[0057] If the color returned in step S3032 is different from lastcolor, it means that the color has changed and the previously counted color needs to be saved; it means that the previous pixels have completed a compression, the data is saved, and the color saving of the next group of pixels begins.
[0058] In order to compress the picture into 8 bp png, in step S2:
[0059] Determine whether the image format is an 8bp png image. If so, directly perform the next step of compression. If not, compress the image into an 8bp png image through the 8bp approximate conversion method.
[0060] In step 301, H is the height of the image to be compressed, W is the width of the image to be compressed, and uint8 pixcount is the statistical number of the same continuous pixels.
[0061] See also Figure 4 , an embodiment provided by the present invention:
[0062] In order to decompress the compressed image, a method for decompressing the image includes the following steps:
[0063] Step S100, read the color table corresponding to the picture ID from Flash and save it in colTab;
[0064] Step S200, reading 512 bytes of image data from Flash;
[0065] Step S300, start DMA to read the subsequent 512 bytes of data from Flash;
[0066] Step S400, filling pixels onto the display screen according to colors and corresponding quantities;
[0067] Step S500, read the data from the buffer to complete drawing, if completed, wait for the DMA data reading to be completed, otherwise repeat step S400;
[0068] Step S600, switch DMA data reading to complete, if yes, complete decompression, otherwise continue DMA to obtain the remaining data, and return to step S400, repeat the corresponding steps until the image decompression is completed.
[0069] like Figure 5 As shown, this is a compression case of the present invention:
[0070] The picture is a 32x32 pixel picture, and it is a detailed display after being magnified 20 times. In the entire picture, we start compressing from 1 pixel, extract the first color as 0x0000, save the first color to the position of colorTab[0], and assign colorindex to 0x0000. At this time, the execution flow goes to S3032, pixindex++.
[0071] Then, when the 12th pixel is repeatedly executed and the pixel color value is 0x10EC, the data is saved to pixdata[0] = 0x0, the color offset value; pixdata[1] = 11, the number of colors; in this way, all pixels are compressed into the color table and pixdata.
[0072] Combined with the above: Figure 5 The picture is for reference. The size of the uncompressed picture is 32*32, and the pixel color table size is 256*2=512 bytes. The pixel content is 1024 pixels, and the total size is 1024+512 bytes;
[0073] The compressed size is: 512 bytes for the pixel color table, 320 bytes for the pixel content, and 832 bytes in total. The compressed pixel content is about 1 / 3 of the original image. When the image size is relatively large, the compression rate can be close to 30%.
[0074] It will be apparent to those skilled in the art that the invention is not limited to the details of the exemplary embodiments described above and that the invention can be implemented in other specific forms without departing from the spirit or essential features of the invention. Therefore, the embodiments should be considered exemplary and non-limiting in all respects, and the scope of the invention is defined by the appended claims rather than the foregoing description, and it is intended that all variations falling within the meaning and scope of the equivalent elements of the claims be included in the invention. Any reference numeral in a claim should not be considered as limiting the claim to which it relates.
Claims
1. A method for compressing an image, characterized in that: The following steps are involved: Step S1, obtaining the resolution of the image to be compressed, where the resolution includes two parameters H and W; Step S2, obtaining the color specification of the image, that is, the number of colors in the entire image; Step S3, extracting pixels and color table of the image for compression; Wherein, step S3 includes: Step S301, first save the color table uint16 ColorTab[256]; uint8 colorindex, Lastcolor, pixel data uint8 pixdata[H*W]; Step S302, before starting compression, the pixels prepared in step S301 need to be cleared to enter the compression preparation process; Step S303, starting from the upper left corner of the pixel to the lower right corner, searching and compressing the pixel; The step S303 also includes the following steps: Step S3031, reading pixel color; Step S3032, check whether the color of the pixel exists in colortab, if it does, return the offset value colorindex of the color in the color table, if not, add the color to the color table, and return the offset value colorindex of the current color; this process completes the filling of the color table and pixel compression to provide counts; If the color in the result returned in step S3032 is the same as lastcolor, pixcount++ indicates that continuous colors appear; then the number of pixels in pix is increased, but not saved; If the color returned in step S3032 is different from lastcolor, it means that the color has changed and the previously counted color needs to be saved; it means that the previous pixels have completed a compression, the data is saved, and the color saving of the next group of pixels begins.
2. The method for compressing a picture according to claim 1, characterized in that: In step S2, it is also necessary to determine whether the image format is an 8bp png image. If so, the next step of compression is directly performed. If not, the image is compressed into an 8bp png image through the 8bp approximate conversion method.
3. The method for compressing a picture according to claim 1, characterized in that: In step 301, H is the height of the image to be compressed, W is the width of the image to be compressed, and uint8 pixcount is the statistical number of the same continuous pixels.
4. A method for decompressing a picture according to any one of claims 1 to 3, characterized in that: The following steps are involved: Step S100, read the color table corresponding to the picture ID from Flash and save it in colTab; Step S200, reading 512 bytes of image data from Flash; Step S300, start DMA to read the subsequent 512 bytes of data from Flash; Step S400, filling pixels onto the display screen according to colors and corresponding quantities; Step S500, read the data from the buffer to complete drawing, if completed, wait for the DMA data reading to be completed, otherwise repeat step S400; Step S600, switch DMA data reading to complete, if yes, complete decompression, otherwise continue DMA to obtain the remaining data, and return to step S400, repeat the corresponding steps until the image decompression is completed.
