Implementation method for combining picture lossy compression and recording transparency information
Technical Field
The invention belongs to the field of data compression and computer graphic image processing, and particularly relates to a method which can be used when high compression ratio of lossy compression is required to be combined with the requirement of recording transparency information.
Background
With the rapid development of computers and networks, pictures are one of very important media, and the compression modes of pictures are divided into two categories, namely lossy compression and lossless compression, wherein lossy compression refers to selectively discarding some insensitive data according to the sensitivity of human eyes to color, brightness, details and the like, so as to achieve the effect of reducing space occupation. Transparency information is also very important in practical application of pictures, and an image containing the transparency information can enable edges of a color image to be smoothly fused with any background, so that the phenomenon that the edges are jagged is thoroughly eliminated. The method integrates the advantages of the jpg format and the png format, can improve the compression rate by using lossy compression, can store transparency information, and is suitable for wider use scenes.
Disclosure of Invention
The invention aims to provide a method for storing transparency information by using lossy compression aiming at the blank of the prior art. The present invention comprises two parts, conversion from png format to the present method format and conversion from the present method format to png format.
The method comprises the following specific steps of converting the png format into the format of the method:
step one, reading a png image file to be processed by using an open source image processing class library CXImage, recording the png image file as SRC, calling an Encode method of the SRC, setting a type parameter as CXIMAGE _ FORMAT _ JPG, converting the SRC into a memory byte stream in a JPG FORMAT, recording the memory byte stream as M1, calling an Encode2RGBA method of the SRC, and acquiring a memory byte stream of SRC transparency information, recording the memory byte stream as M2.
And secondly, writing a file header in the new file, wherein the file header comprises a default value I for identifying the file type, the length of the byte stream M1 and the length of the byte stream M2 are used as the content of the file header, and the content of the byte stream M1 and the content of the byte stream M2 are used as the content of the new file, so that the writing of the new file is completed.
The method comprises the following specific steps of converting the format of the method into the png format:
step three, reading a file header from the file in the format of the method, wherein the file header comprises a default value II for identifying the file type, a length L1 of lossy compression data of the picture and a transparency information length L2 of the picture, and if the read default value II for identifying the file type is the same as the default value I, entering step four; if the default value II of the read identification file type is different from the default value I, the file is not in the format of the method, and the conversion fails.
Reading a byte stream with the length of L1, namely picture data in a jpg FORMAT, behind a file header, decoding the picture data by using CXImage to obtain RGB data of an image, recording the RGB data as DST, continuously reading the byte stream with the length of L2, namely the memory byte stream of transparency information, calling an AlphaSet function of the DST, setting the transparency information into the image content of the DST, calling an Encode function of the DST, and setting the type parameter as CXIMAGE _ FORMAT _ PNG to obtain a final PNG FORMAT result.
The invention has the following beneficial effects:
the method has the idea that the jpg format and the png format are combined, so that the advantage of high compression rate of the jpg format and the advantage of transparency information supported by the png format are obtained, and the file length is greatly shortened on the whole.
Detailed Description
The present invention will be further described with reference to the following examples.
Taking img _ answer _ tc.png as an example, the original file size is 15232 bytes, the open source image processing class library CXImage is used to read in the img _ answer _ tc.png to be processed, which is recorded as SRC, then the Encode method of SRC is invoked, the type parameter is set as CXImage _ FORMAT _ JPG, the memory byte stream is converted into a memory byte stream in JPG FORMAT, which is recorded as M1, the length of M1 is 7431 bytes, then the Encode2RGBA method of SRC is invoked, the memory byte stream for obtaining transparency information is recorded as M2, the length of M2 is 1425 bytes, the file header is added, the total length of the finally generated file is 8888 bytes, and the occupied space is 58% of the original file.
Reading a file header from the beginning of a file to be decompressed, obtaining a default value II for identifying the file type, a memory byte stream length L1 of a jpg format being 7431, a memory byte stream length L2 of transparency information being 1425, comparing the default value II with the default value I, and confirming that the file is in the format of the method; then 7431 bytes are read again, and CXImage is used for decoding to obtain RGB data of the image, and the RGB data is recorded as DST; then, 1425 bytes are read again to be a memory byte stream of transparency information, an AlphaSet function of the DST is called, the memory byte stream of transparency information is set in the image content of the DST, an Encode function of the DST is called, a type parameter is set as CXIMAGE _ FORMAT _ PNG, a final PNG FORMAT result can be obtained, the size of a finally recovered file is 15048 bytes, the reason that the size of the finally recovered file is different from that of the original file is that the method contains a lossy compression process, and data after compression and decompression is not completely consistent with the original data.