A kind of process the method for alpha channel data in PNG picture format
Technical field
The invention belongs to data compression, computer graphic image process field, relate to a kind of processing the method for alpha channel data in PNG picture format.
Background technology
PNG, a kind of image file storage format, it is characterized in supporting transparent effect.PNG can be that original image defines 256 transparent levels so that the edge of coloured image smoothly can merge with any background, thus eliminates jagged edges up hill and dale.This function is that a lot of picture format is unexistent, the most common BMP picture format, JPEG picture form, does not the most support transparent effect.
Actually used PNG picture format data when, be often required to store data, network transmission etc. processes, and this relates to the compression of data.In most cases, the space of storage is the smaller the better, and the data volume of network transmission is the smaller the better.
The most frequently used PNG picture format data are the PNG data forms of 32, wherein contain the color channel data of 24 and the alpha channel data of 8, and alpha channel data is exactly transparency data.Compression facing to 32 PNG data processes, and the method that presently, there are the most does not carries out special handling to 8 alpha channel datas, is substantially with universal compressed engine compression once.
Summary of the invention
It is an object of the invention to for the deficiencies in the prior art, it is provided that a kind of process the method for alpha channel data in PNG picture format.
The technical solution adopted for the present invention to solve the technical problems comprises the steps:
Step (1), the header data of 32 PNG image datas of parsing, obtain the height h and width w of PNG picture, and this PNG image data contains n pixel number evidence, n=h × w;Each pixel number evidence has 32, wherein color channel data 24, alpha channel data 8;Memory space sp=8 × n that now alpha channel data needs, in units of bit;
Step (2), set up alpha channel data set DSA;
Step (3), travel through each pixel number according to Pi, 1≤i≤n, wherein pixel number is according to PiAlpha channel data be Ai, by AiPut into set DSA;Finally give set DSA={A1, A2..., An-1,An}
Step (4), the alpha channel data analyzed in set DSA;If meeting condition: A1=A2=...=An-1=An=KZ, say, that each data in set DSA are identical, jump to step (5);Otherwise, step (6) is jumped to;
Step (5), need not preserve the value of the alpha channel data of each pixel number evidence, 0 alpha channel data is used to express, because the alpha channel data values of all pixel number evidences is all the constant value KZ of 8, preserve a KZ value;The memory space sp=8 that now alpha channel data needs, in units of bit, alpha channel data is disposed;
Step (6) is if meeting condition: Ax1=Ax2=...=Axi=K1 and Ay1=Ay2=...=Ayi=K2,1≤xi≤n, 1≤yi≤n, K1 and K2 is 8 constants, K1 ≠ K2, say, that the alpha channel data of all pixel number evidences only has two values, K1 or K2, jumps to step (7);Otherwise, step (8) is jumped to;
Step (7), 1 alpha channel data is used to express, for each pixel number according to preserving the value of 1 alpha channel data, respectively 0 and 1, wherein 0 represent K1 value, 1 represents K2 value;Memory space sp=1 × n that now alpha channel data needs, in units of bit, alpha channel data is disposed;
Step (8), alpha channel data is not done special handling, for each pixel number according to the value preserving 8 alpha channel datas;Memory space sp=8 × n that now alpha channel data needs, in units of bit;Alpha channel data is disposed.
The present invention has the beneficial effect that:
The present invention is mainly characterized by 8 alpha channel datas in 32 the most frequently used PNG image datas have been carried out special handling disaggregatedly.Wherein 0 alpha passage, it is not necessary to preserve alpha channel value for each pixel, only needs 8 constants;1 alpha passage, it is only necessary to preserve the value of 1 alpha passage for each pixel.In practical business, the PNG picture of above-mentioned two situations is the most, effectively saves the resource such as memory space, network traffic flow.
Accompanying drawing explanation
Fig. 1 is the flow chart of the present invention;
Fig. 2 is 0 alpha access diagram, the alpha all 255 of each pixel of PNG picture;
Fig. 3 is 0 alpha access diagram, the alpha all 128 of each pixel of PNG picture;
Fig. 4 is 1 alpha passage figure schematic diagram, and the alpha value of the one part of pixel point of PNG picture is 0, and the alpha value of another part pixel is 255;
Fig. 5 is 8 alpha passage figures;
Detailed description of the invention
The invention will be further described below in conjunction with the accompanying drawings.
As described in Figure 1, a kind of process the method for alpha channel data in PNG picture format, specifically include following steps:
Step (1), the header data of 32 PNG data of parsing, obtain the height h and width w of picture, and this PNG data contains n pixel number evidence, n=h × w;Each pixel number evidence has 32, wherein color channel data 24, alpha channel data 8;The memory space that now alpha channel data needs is sp=8 × n, in units of bit
Step (2), set up data acquisition system DSA;
Step (3), travel through each pixel number according to Pi, 1≤i≤n, wherein PiAlpha channel data be Ai, by AiPut into set DSA;Finally give set DSA={A1, A2..., An-1,An}
Step (4), the data of analysis set DSA;If meeting condition: A1=A2=...=An-1=An=KZ, say, that each data in set DSA are identical, go to step (5);Otherwise, (6) are gone to step;
Step (5), as shown in Figures 2 and 3, this situation can use 0 alpha channel data to express, it is not necessary for each pixel and preserves the value of alpha passage, because the alpha value of all pixels is all the constant value KZ of 8, preserve a KZ value;The memory space that now alpha channel data needs is only sp=8, in units of bit;Go to step (9);
Step (6) is if meeting condition: Ax1=Ax2=...=Axi=K1 and Ay1=Ay2=...=Ayi=K2,1≤xi≤n, 1≤yi≤n, K1 and K2 is 8 constants, K1 ≠ K2, say, that the alpha passage of all pixels only has two values, K1 or K2, goes to step (7);Otherwise, (8) are gone to step;
Step (7), as shown in Figure 4, this situation can use 1 alpha channel data to express, it is only necessary to preserving the value of 1 alpha passage, respectively 0 and 1 for each pixel, wherein 0 represent K1 value, 1 represents K2 value;The memory space that now alpha channel data needs is sp=1 × n, in units of bit;Go to step (9);
Step (8), it is not belonging to above-mentioned two situations, alpha data is not done special handling, preserve the value of 8 alpha passages for each pixel;The memory space that now alpha channel data needs is sp=8 × n, in units of bit;Go to step (9);As shown in Figure 5.
Step (9), alpha channel data are disposed.