A kind of implementation method of combination picture lossy compression and record transparence information
Technical field
The invention belongs to data compression, computer graphic image process field, the height in conjunction with lossy compression is especially needed
The method that can be used when the requirement of compression ratio and record transparence information.
Background technique
With the rapid development of computer and networks, picture is one of highly important media, the compress mode point of picture
For two major classes, lossy compression and lossless compression, wherein lossy compression is the sensitive journey according to human eye to color, brightness, details etc.
Degree, selectively abandons some insensitive data, achievees the effect that reduce space hold, common are damage compressed file at present
Format is jpg format, but jpg format does not support storage transparence information.Transparence information in the practical application of picture be also
Very important, the image comprising transparence information can enable the edge of color image smoothly merge with any background, from
And thoroughly eliminate edge and the phenomenon that sawtooth occur, the format comprising transparence information common at present is png format, but png
Format is not supported to carry out lossy compression to image data.This method combines the advantages of jpg format and png format, can use
Lossy compression improves compression ratio, and can store transparence information, adapts to wider usage scenario.
Summary of the invention
The purpose of the present invention is being directed to the blank of the prior art, lossy compression and storage can be used by providing one kind
The method of transparence information.The present invention includes two parts, is turned from png format conversion cost method format and from this method format
Change png format into.
Specific steps of the method for the present invention from png format conversion cost method format are as follows:
Step 1: reading in png image file to be processed using open source image procossing class libraries CXImage, it is denoted as SRC, so
The Encode method of SRC is called afterwards, and type parameter is set as CXIMAGE_FORMAT_JPG, SRC is converted to the memory of jpg format
Byte stream is denoted as M1, calls the Encode2RGBA method of SRC, gets the memory byte stream of SRC transparence information, is denoted as
M2。
Step 2: file header is written in new file, file header includes the about definite value I for identifying file type, wherein
Using the length of byte stream M1, the length of byte stream M2 as the content of file header, content, the content of byte stream M2 of byte stream M1
As the content of new file, to complete the write-in of new file.
The present invention is converted into the specific steps of png format from this method format are as follows:
Step 3: file header is read from the file of this method format, about definite value II, picture including identifying file type
The length L1 of lossy compression data, picture transparence information length L2, if the about definite value II of the mark file type read
It is identical as about definite value I, then enter step four;If the about definite value II of the mark file type read is different from about definite value I, then it is assumed that
It is not the format of this method, convert failed.
Step 4: reading the byte stream that length is L1, the i.e. image data of jpg format after file header, used
CXImage decoding, obtains the RGB data of image, is denoted as DST, then proceedes to read the byte stream that length is L2, i.e. transparency is believed
The memory byte stream of breath calls the AlphaSet function of DST, and transparence information is arranged into the picture material of DST, is then adjusted
With the Encode function of DST, type parameter, which is set as CXIMAGE_FORMAT_PNG, can be obtained the result of final png format.
The present invention has the beneficial effect that:
The thought of this method is to combine jpg format and png format, has both obtained high good of jpg format compression rate
Place, png format of having got back support the benefit of transparence information, significantly shorten file size on the whole.
Specific embodiment
Below with reference to embodiment, the invention will be further described.
By taking img_answer_tc.png as an example, original document size is 15232 bytes, uses open source image procossing class libraries
CXImage reads in img_answer_tc.png to be processed, is denoted as SRC, then calls the Encode method of SRC, type parameter
It is set as CXIMAGE_FORMAT_JPG, is converted into the memory byte stream of jpg format, is denoted as M1, the length of M1 is 7431 words
Then section calls the Encode2RGBA method of SRC, gets the memory byte stream of transparence information, be denoted as M2, the length of M2
For 1425 bytes, in addition file header, the file total length ultimately produced is 8888 bytes, and the space occupied is original document
58%.
Read in file header from file section start to be decompressed, obtain the about definite value II of mark file type, jpg format it is interior
Deposit the memory byte stream length L2=1425 of byte stream length L1=7431, transparence information, will about definite value II and about definite value I into
Row compares, and confirmation is this method format;Then 7431 bytes are read in again, it is decoded using CXImage, obtains image
RGB data is denoted as DST;Then 1425 bytes are read in again, are the memory byte stream of transparence information, are called DST's
The memory byte stream of transparence information is arranged into the picture material of DST AlphaSet function, then calls DST's
Encode function, type parameter be set as CXIMAGE_FORMAT_PNG can be obtained final png format as a result, last recovery
File size be 15048 bytes, reason of different sizes is that this method contains lossy compression process with original document, through over-voltage
Data and initial data after contracting and decompression are not quite identical.