A PNG format encoding and decoding method and device
By extending true color images with inequality depth and true color images with inequality depth in PNG's IHDR file header data block, the problem that MCU is difficult to directly support RGB565 and RGB565A formats is solved, and more efficient image storage and decoding is achieved.
Patent Information
- Application Number
- CN202011111269.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-10-16
- Publication Date
- 2025-04-18
- Estimated Expiration
- 2040-10-16
AI Technical Summary
When using PNG format images, it is difficult for MCU to directly support RGB565 and RGB565A formats, resulting in increased data transmission and low decoding efficiency.
Direct support for formats such as RGB565 is achieved by extending the encoding and decoding of true color images with inequality depth and true color images with inequality depth in PNG's IHDR file header data block.
It effectively reduces the storage space of PNG images and speeds up decoding. It is especially suitable for MCU chips and products, and is compatible with the original PNG color type format.
Smart Images

Figure CN114387356B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of image processing, and in particular relates to a PNG format encoding and decoding method and device. Background Art
[0002] In recent years, MCU chips have been widely used in smart wearables, industrial control, Internet of Things control, medical electronics, etc. More and more customers hope that MCU can have certain graphics computing capabilities to make the human-machine interface (UI) more beautiful and gorgeous. The MCU itself is limited by the main frequency, image processing capabilities, memory size and interface rate. In particular, MCU widely uses SPI interface screens, which are limited by the SPI interface rate limit, which makes the amount of pixel data transmitted between MCU and screen higher. The requirements are put forward. When the color screen in the MCU is displayed, the RGB565 color mode can reduce the amount of data transmission between the MCU and the color screen by 1 / 3 compared with RGB888, so RGB565 is widely used in MCU products.
[0003] In MCU products, PNG format is usually used for image storage. Due to its high compression rate, statistics show that it can save at least half of the memory space, or even more. Of course, if PNG format is used for image storage, the MCU needs to decode the PNG image, which puts forward requirements on the processing power of the MCU. This bottleneck can generally be solved by designing a PNG hardware accelerator, which will not be discussed further here.
[0004] PNG is a bitmap format that uses a lossless compression algorithm. It is designed to replace GIF and TIFF file formats, while adding some features that GIF file formats do not have. PNG uses a lossless data compression algorithm derived from LZ77. Due to its high compression ratio and small file size, it is generally used in JAVA programs, web pages or S60 programs. The PNG format mainly has three forms: 8-bit, 24-bit, and 32-bit. Among them, 8-bit PNG supports two different forms of transparency (index transparency and alpha transparency), 24-bit PNG does not support transparency, and 32-bit PNG adds an 8-bit transparency channel on the basis of 24 bits, so it can show 256 levels of transparency.
[0005] The encoding process of PNG includes three processes: filter, deflate, and chunk encoding; the decoding process of PNG includes three processes: chunk decoding, inflate, and un-filter. The filtering is lossless and is performed in bytes. The filtering process itself does not reduce the size of the data, but the compression capability of deflate can be improved through filtering. The deflate process includes LZ77 compression and huffman encoding of the compressed data, which is also an operation on the data itself. When the PNG standard was defined, it only supported pixel images with equal color bit depths, such as color bit depths of 1, 2, 4, 8, and 16. Since the bit depths of the three colors in the two formats of RGB565 and RGB565A are inconsistent, the encoding process of PNG does not support these formats.
[0006] On the other hand, when PNG is used on the MCU, if RGB565 is used for screen display, there is a requirement for PNG to directly support RGB565 and RGB565A. In order to save the storage space of PNG images and avoid an extra mapping from RGB888 (or RGB888A) to RGB565 (or RGB565A) during decoding, it is necessary to study the PNG image format and compression method to implement a PNG encoding and decoding method that supports RGB formats with inconsistent color bit depths. Summary of the invention
[0007] Based on the problems existing in the prior art, the present invention proposes a PNG format encoding and decoding method and device; considering that the RGB565 color mode occupies 16 bits, the data transmission amount between the MCU and the color screen is reduced by 1 / 3 compared with the 24 bits of RGB888; therefore, the present invention further studies the PNG image format and its compression method, and provides a coding and decoding scheme that enables PNG images to directly support RGB565 and other formats.
[0008] Specifically, the present invention is implemented through the following scheme:
[0009] In a first aspect of the present invention, the present invention provides a PNG format encoding method, the encoding method comprising the following steps:
[0010] Read the target pixel in the original image and calculate the number of bits of the current target pixel and the number of bits of different channels in each target pixel;
[0011] Identify the color type of the current target pixel and correspond it to a preset definition value; set the bit depth allowed in the encoding process to point to the definition value; wherein the color type at least includes grayscale image, true color image, indexed color image, grayscale image with alpha channel data, true color image with alpha channel data, true color image with unequal bit depth and true color image with unequal bit depth with alpha channel data;
[0012] Determine the size of the definition value of the color type to be encoded, and calculate the bit depth in the pixel according to the first condition for true color images of unequal bit depths and true color images of unequal bit depths with alpha channel data; calculate the bit depth in the pixel according to the second condition for images of other color types;
[0013] Determine the bit depth of each pixel to be encoded, calculate the size of the image, divide the image into blocks, and preset the compression algorithm in the image header block to downsample the image calculated by scanning lines;
[0014] After filtering the scan line data, it is compressed according to a preset compression algorithm to encode an image data block, and an image end block is encoded at the end of the image data block, thereby completing the encoding of the PNG image.
[0015] Furthermore, before determining the size of the definition value of the color type to be encoded, it also includes converting the color type of the target pixel in the read original image, that is, converting from any one of the color types of grayscale image, true color image, indexed color image, grayscale image with alpha channel data, true color image with alpha channel data, true color image with unequal bit depths, and true color image with unequal bit depths with alpha channel data to another color type different from the previous one; and re-determining the definition value of the new color type.
[0016] The identifying of the color type of the target pixel and corresponding to the preset defined value includes assigning an integer value between [0, 6] to a grayscale image, a true color image, an indexed color image, a grayscale image with alpha channel data, and a true color image with alpha channel data, and assigning an integer value between [7, 12] to a true color image with unequal bit depths and a true color image with unequal bit depths with alpha channel data; and the defined value occupies one byte bit space.
[0017] Furthermore, the setting of the bit depth allowed in the encoding process points to the defined value, including setting multiple optional third bit depths for grayscale images, true color images, indexed color images, grayscale images with alpha channel data, and true color images with alpha channel data, and the bit depth occupied by each channel is equal; setting a fixed bit depth for true color images of unequal bit depths and true color images of unequal bit depths with alpha channel data, and the fixed bit depth includes a first bit depth and a second bit depth; the first bit depth is the bit depth occupied by one of the color channels in the color type, and the second bit depth is the total bit depth occupied by a pixel in the color type.
[0018] Furthermore, the preset first function is expressed as f(x)=3A+α, where A represents the bit depth of the channel, and α represents the bit depth occupied by the α channel; the value of the adjustable parameter is ±1 or ±2.
[0019] On the basis of the first aspect of the present invention, in a second aspect of the present invention, the present invention provides a decoding method of the PNG format corresponding to the first aspect, the decoding method comprising the following steps:
[0020] Read out the PNG image, read out the image header block, image data block and image end block;
[0021] Decompressing the image data block and performing filtering and restoration according to the compression algorithm read from the image header block;
[0022] Determine a color type according to a definition value read from an image header block, the color type including a grayscale image, a true color image, an indexed color image, a grayscale image with alpha channel data, a true color image with alpha channel data, a true color image with unequal bit depths, and a true color image with unequal bit depths with alpha channel data;
[0023] According to the allowed bit depth of the color type read from the image header block, the bit depth in the pixel is calculated according to the third condition for true color images of unequal bit depths and true color images of unequal bit depths with alpha channel data; the bit depth in the pixel is calculated according to the second condition for images of other color types;
[0024] Decoding the image information data from the PNG image data block according to the bit depth of each pixel;
[0025] After the information of the image data block is decoded, the decoded information of the image header block and the image end block are loaded before and after the information of the image block respectively, so as to complete the decoding of the PNG image.
[0026] On the basis of the first aspect of the present invention, in a third aspect of the present invention, the present invention further provides an encoding device for implementing the PNG format of the first aspect, the encoding device comprising:
[0027] A first acquisition unit, used for acquiring target pixels in the original image;
[0028] A first recognition unit, used to recognize the color type of the target pixel and correspond it to a preset definition value;
[0029] An encoding preparation unit, used for setting the bit depth allowed in the encoding process to point to the defined value; and for determining the bit depth of each pixel when encoding is required, so as to calculate the image size and determine the content of the image header block;
[0030] The encoding unit is used to perform PNG encoding processing in the image block determined by the encoding preparation unit, and encode an image header block, an image data block and an image end block.
[0031] On the basis of the second and third aspects of the present invention, in a fourth aspect of the present invention, the present invention further provides a decoding device of the PNG format corresponding to the third aspect, the encoding device comprising:
[0032] The second acquisition unit is used to acquire PNG images;
[0033] A second identification unit is used to read an image header block, an image data block and an image end block from the PNG image; and to identify a color type and an allowed bit depth from the image header block;
[0034] A decoding preparation unit, used to determine the number of bits of pixels in the PNG data stream and the number of bits of different channels in the pixels according to the color type;
[0035] The decoding unit is used to parse out the information of the PNG image according to the number of bits of the pixel and the number of bits of different channels in the pixel.
[0036] Beneficial effects of the present invention:
[0037] The present invention extends two color types on the IHDR of PNG to support the encoding and decoding of true color images with unequal bit depths and true color images with unequal bit depths with alpha channel data; the present invention can effectively reduce the storage space of PNG images and accelerate the decoding speed; it is particularly suitable for MCU chips and products that support display screens in color formats such as RGB565; the method of the present invention is also suitable for extending PNG's support for other color types, and can easily realize the interchange between various color types; at the same time, the method of the present invention can also be relatively compatible with the original PNG color type format. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] Figure 1 It is a flow chart of the encoding method of the PNG format of the present invention;
[0039] Figure 2 It is a flow chart of the decoding method of the PNG format of the present invention;
[0040] Figure 3 It is a structural diagram of an encoding device in PNG format of the present invention;
[0041] Figure 4 This is a structural diagram of a decoding device for the PNG format of the present invention. DETAILED DESCRIPTION
[0042] 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.
[0043] Figure 1 : is a flow chart of a coding method in PNG format in an embodiment of the present invention, wherein the coding method comprises the following steps:
[0044] S101, read out the target pixels in the original image, and calculate the number of bits of each target pixel and the number of bits of different channels in each target pixel;
[0045] In this embodiment, the original picture can be a natural picture or a non-natural picture, which is classified based on the way the picture is generated. The natural picture is a naturally generated picture, such as: photos taken by a camera, natural scenery images, etc.; non-natural pictures are usually pictures generated by people or automatically generated by the device, such as: screenshots of the instant messaging interface, game screens, etc.; screenshots of the instant messaging interface and the game screen may contain both parts participated by people and parts automatically generated by the device; more specifically, taking the game screen as an example: in the real-time battle game, players will use text to communicate, in which the text part is the part participated by people; the other parts are the picture content drawn by the device according to computer program instructions or single-chip microcomputer control instructions. It can be understood that in addition to the screenshot of the instant messaging interface, it can also be a screenshot of the entire desktop or other application interface, and in addition to the game screen, it can also be any other picture drawn by the computer. The above examples of natural pictures and non-natural pictures should not be understood as the only limitation of the embodiment of the present invention. In addition, in this embodiment, the original picture is a picture input to the MCU, and the format of the picture can be any picture format that the MCU can support; for example: BMP, JPEG, PNG and GIF format, or other formats.
[0046] S102, identifying the color type of the target pixel and corresponding it to a preset definition value; setting the bit depth allowed in the encoding process to point to the definition value;
[0047] The color types include at least grayscale images, true color images, indexed color images, grayscale images with alpha channel data, true color images with alpha channel data, true color images with unequal bit depths, and true color images with unequal bit depths with alpha channel data;
[0048] For a PNG file, the standard file structure includes the PNG file flag and the PNG data block; PNG defines two types of data blocks, one is called the critical chunk, which is a standard data block, and the other is called the ancillary chunk, which is an optional data block. The critical chunk defines four standard data blocks, which must be included in every PNG file, and the PNG reading and writing software must also support these data blocks. For simplicity, in the PNG file we use, the PLTE palette data block is omitted; the IHDR file header data block, the IDAT image data block, and the IEND image end block are set to be stored in the above order, and each appears only once.
[0049] The present invention extends and improves the IHDR file header data block (image header block) to achieve encoding and decoding of pictures, pixels and data streams of different color types; it includes basic information of image data stored in the PNG file and appears as the first data block in the PNG data stream, and there can only be one file header data block in a PNG data stream. First, the color types defined in the IHDR in the prior art are shown in Table 1:
[0050] Table 1 IHDR color types defined in the PNG standard
[0051] PNG image type Color Type Allowed bit depth Grayscale images 0 1,2,4,8,16 True color image 2 8,16 Indexed color images 3 1,2,4,8 Grayscale image with alpha channel data 4 8,16 True color image with alpha channel data 6 8,16
[0052] Based on the color types defined in the PNG standard, the present invention adds formats for defining two color types, which are used to support true color images with unequal bit depths and true color images with unequal bit depths with alpha channel data, as shown in Table 2.
[0053] Table 2 IHDR color types defined in the PNG standard of the present invention
[0054] PNG image type Color Type Allowed bit depth True color images of varying bit depths 8 2,3,5,6,8,16 Truecolor images of varying bit depths with alpha channel data 12 2,3,5,6,8,24
[0055] Among them, color type = 8 means that the PNG is a true color image with unequal bit depth. Taking the RGB565 format as an example, each pixel occupies 16 bits, the Red channel occupies 5 bits, the Green channel occupies 6 bits, the Blue channel occupies 5 bits, and the bit depth is set to 5 bits; color type = 12 means that the PNG is a true color image with unequal bit depth with α channel data, especially taking the RGB565A format as an example, each pixel occupies 24 bits, the Red channel occupies 5 bits, the Green channel occupies 6 bits, the Blue channel occupies 5 bits, the α channel occupies 8 bits, and the bit depth can still be set to 5 bits.
[0056] S103, determining the size of the definition value of the color type to be encoded, and calculating the bit depth in the pixel according to the first condition for true color images of unequal bit depths and true color images of unequal bit depths with alpha channel data; and calculating the bit depth in the pixel according to the second condition for images of other color types;
[0057] Since the traditional encoding of images with equal bit depth is to call functions for calculation, for example, in true color images, when the allowed bit depth is 8 bits, the type represents the RGB888 format. In order to calculate the encoding byte size used for compression, the traditional PNG encoding directly calls the function 3A+α to calculate the pixel size in the RGB888 format, where A represents the allowed bit depth and α represents the bit depth occupied by the α channel; the function multiplies the allowed bit depth by the number of channels, and the allowed bit depth of the RGB888 format is 8, so the calculated pixel size of the RGB888 format is 3×8+0=24b it, and because the present invention has newly added the setting of true color images with unequal bit depths and true color images with unequal bit depths with alpha channel data, the pixel size calculated by the traditional function is different from its own size; for the convenience of explanation, RGB565 is used to represent true color images with unequal bit depths, and RGB565A is used to represent true color images with unequal bit depths with alpha channel data; it can be understood that true color images with unequal bit depths are not limited to RGB565, but can also be RGB233 and other methods. Similarly, true color images with unequal bit depths with alpha channel data are not limited to RGB565A. In order to be compatible with the traditional PNG encoding method, the present invention sets the allowable bit depth of RGB565 format to 5bit or 6bit; at this time, the pixel bit depth calculated by calling the function is 15bit or 18bit, which is obviously different from the pixel bit depth of the RGB565 format; in order to solve the problem that the calling function cannot obtain the true pixel bit depth of true color images with unequal bit depths or true color images with unequal bit depths with alpha channel data; the embodiment of the present invention provides at least two ways to solve the above problem:
[0058] In an implementable manner, in order to cooperate with the calling function, the present invention processes the allowed bit depth; when it is determined that the size of the definition value is 8 or 12, referring to the record in Definition Table 2, the allowed bit depth of RGB565 and RGB565A is 5 bits; if the color type is equal to 8, then the total bit depth of each pixel needs to be added by 1 during calculation, that is, the three colors R, G, and B are (15+1)=16 bits; if the color type is equal to 12, then the total bit depth of the pixel also needs to be added by 1 during calculation, then the three colors R, G, and B are (15+1)+alpha(8)=24 bits; although the pixel encoding into RGB565 (or RGB565A) breaks the rule that each color is equal, based on the characteristics of the progressiveness of image pixels and the characteristics of filtering and compression operating on bytes, the traditional filtering and compression standard process can still be used.
[0059] In a preferred embodiment, before determining the size of the definition value of the color type to be encoded, the method further includes converting the color type of the target pixel in the read original image, that is, converting any color type from grayscale image, true color image, indexed color image, grayscale image with alpha channel data, true color image with alpha channel data, true color image with unequal bit depth, and true color image with unequal bit depth with alpha channel data to another color type different from the previous one; and re-determining the definition value of the new color type. The conversion functions used for these color type conversions can use existing technologies,
[0060] For ease of understanding, let's take an example of RGB888->RGB565: the specific idea of the conversion is as follows:
[0061] Take the high 5 bits of the first byte in RGB888 as the high 5 bits of the second byte of the converted RGB565; take the high 3 bits of the second byte in RGB888 as the low 3 bits of the second byte of the converted RGB565; take the 4th to 6th bits of the second byte in RGB888 as the high 3 bits of the first byte of the converted RGB565; take the high 5 bits of the third byte of the second byte in RGB888 as the low 5 bits of the first byte of the converted RGB565;
[0062] Conversely, for the conversion from RGB565 to RGB888:
[0063] Take the lower 5 bits of the first byte of RGB565 as the upper 5 bits of RGB888; take the lower 3 bits of the second byte of RGB565, shift it left by 5 bits, and use it as the upper 5 bits of the second byte of RGB888; take the upper 3 bits of the first byte of RGB565 and shift it right by 3 bits as bits 4 to 6 of the second byte of RGB888; take the upper 5 bits of the second byte of RGB565 as the third byte of RGB888.
[0064] In a preferred embodiment, the present invention can also use quantization compression and quantization supplementation to linearly compensate for the lost precision. For example, the original data can be filled to the high bits; for the low bits, the low bits of the original data are used for compensation; if there are still unfilled bits, the low bits of the original data continue to be used for cyclic compensation.
[0065] S104, determining the bit depth of each pixel to be encoded, calculating the size of the image, dividing the image into blocks, and downsampling the image calculated by scanning lines using a compression algorithm preset in the image header block;
[0066] A PNG image is a rectangular array of pixels, with rows of pixels from left to right and scan lines from top to bottom. The size of each pixel is determined by the number of bits per pixel.
[0067] The pixels of a scanline are always packed into a sequence of bytes, with no extra bits between pixels. A scanline always starts on a byte boundary. The allowed bit depths and color types are restricted so that packing is simple and efficient in all cases.
[0068] In PNG images, color type 0 (gray) has only a single sample per pixel, which may have less than a byte of precision (1, 2, or 4 bits). These samples are packed into bytes, with the leftmost sample in the high-order bit of a byte, followed by the rest of the samples.
[0069] In PNG images, color type 3 (indexed color) has only one palette index per pixel. This means that the way the indexes are packed into bytes is the same as for color type 0.
[0070] When a byte has multiple pixels, some low bits of the last byte of a scan line may be unused. The contents of these unused bits are unspecified.
[0071] In an embodiment of the present invention, a non-indexed color image of a PNG image may have a sample value with a bit depth of 16 bits. Such sample values are stored and transmitted in network byte order.
[0072] PNG allows multisampled pixels only with 8-bit or 16-bit sample depth, so multiple samples of a pixel are not packed into a single byte at all.
[0073] Specifically, taking the RGB565 format with a bit depth of 16 bits as an example, the scan line will scan the pixels of the original image from left to right and from top to bottom. One pixel occupies two bytes. One scan line includes multiple bytes, which are packaged in byte sequence for encoding processing.
[0074] S105 , compressing the scan line data according to a preset compression algorithm after filtering, encoding an image data block, and encoding an image end block at the end of the image data block, thereby completing the encoding of the PNG image.
[0075] PNG allows scanline data to be filtered before it is compressed. Filtering can increase the compression ratio of the data.
[0076] There is only one filtering method, but there are multiple filtering types. The filtering method does not affect the size of the data, nor does it lose any information. The only purpose of filtering is to provide better compressed data to the compression method. The filtering step itself makes the resulting sequence of bytes have the same size as the input sequence, but in a different representation, depending on the type of filtering performed. Filtering does not reduce the size of the actual scanned line of data. All filtering is strictly lossless.
[0077] In the international standard, only compression method 0 is defined. Other values of compression method are reserved for future standardization. The sequence of filtered scan lines is compressed and the resulting data stream is placed into multiple data blocks of IDAT respectively. The union of the contents of all IDAT data blocks forms a zlib data stream. This data stream is compressed into the filtered PNG image data stream.
[0078] Based on the above embodiments, Figure 2 is a flow chart of a decoding method of the PNG format of the present invention; the decoding method may include the following steps:
[0079] S201, read out the PNG image, read out the image header block, image data block and image end block;
[0080] First we read out the PNG image and check if it is the byte length of a valid PNG file. After the verification is completed, we can read out the image header block, image data block and image end block.
[0081] S202, decompressing the image data block and performing filtering and restoration according to the compression algorithm read from the image header block;
[0082] The compression algorithm read from the image header block, decompresses the image data block and performs filtering to restore it; to reverse the effect of the filter, the decoding process may need to use the decoded value of the previous pixel on the same line, the pixel just above the current pixel on the previous line, and the pixel to the left of the pixel above. This means that the decoding process always needs to store at least one scan line of image data. Even if some filter types do not reference previous scan lines, the decoding process always needs to store each scan line when decoding, because the next scan line may use a filter type that references it.
[0083] S203, determining a color type according to the defined value;
[0084] The color types include grayscale images, true color images, indexed color images, grayscale images with alpha channel data, true color images with alpha channel data, true color images with unequal bit depths, and true color images with unequal bit depths with alpha channel data;
[0085] S204, according to the allowed bit depth of the color type read from the image header block, the bit depth in the pixel is calculated according to the third condition for the true color image with unequal bit depth and the true color image with unequal bit depth with alpha channel data; the bit depth in the pixel is calculated according to the second condition for the image of other color types;
[0086] Calculating the bit depth of the pixel according to the third condition includes determining whether the allowed bit depth of the true color image of unequal bit depth and the true color image of unequal bit depth with alpha channel data is greater than or equal to 8, and if so, using the allowed bit depth as the bit depth of the pixel, otherwise calling the first function with an adjustable parameter attached to calculate the bit depth of the pixel;
[0087] The step of calculating the bit depth of pixels according to the second condition for images of other color types includes calling a preset first function to calculate the bit depth size in the pixels according to the allowed depth.
[0088] For example, during the decoding process, if the definition value of the color type read out is 8, then the color type can be specifically RGB565 or RGB232, etc. In order to determine the specific color type, the present invention first determines whether the allowed bit depth in the image header block is greater than 8. If it is greater than or equal to 8, according to the settings in Table 2, the bit depth is 16 bits, so 16 bits are directly used as the bit depth of the pixel, and the allowed bit depth is used as the bit depth of the pixel, otherwise the first function with an adjustable parameter attached is called to calculate the bit depth of the pixel; for example, if the allowed bit depth is 5 bits, then the preset first function needs to be called to be expressed as f(x)=3A+α, where A represents the bit depth of the channel, and α represents the bit depth occupied by the α channel; at the same time, an adjustable parameter is attached to the preset first function; the value of the adjustable parameter is ±1 or ±2.
[0089] S205, decoding image information data from the PNG image data block according to the bit depth of each pixel;
[0090] After obtaining the bit depth of each pixel, taking RGB565 as an example, the bit depth of each pixel is 16 bits. In the image data block, the image information data is obtained in a manner of 16 bits as one pixel.
[0091] S206: After the information of the image data block is decoded, the decoded information of the image header block and the image end block are loaded before and after the information of the image block respectively, so as to complete the decoding of the PNG image.
[0092] Figure 3 It is a PNG format encoding device provided by the present invention, such as Figure 3 As shown, the encoding device includes:
[0093] A first acquisition unit, used for acquiring target pixels in the original image;
[0094] A first recognition unit, used to recognize the color type of the target pixel and correspond it to a preset definition value;
[0095] An encoding preparation unit, used for setting the bit depth allowed in the encoding process to point to the defined value; and for determining the bit depth of each pixel when encoding is required, so as to calculate the image size and determine the content of the image header block;
[0096] The encoding unit is used to perform PNG encoding processing in the image block determined by the encoding preparation unit, and encode an image header block, an image data block and an image end block.
[0097] Figure 4 A PNG format decoding device provided by the present invention is as follows: Figure 4 As shown, the decoding device comprises:
[0098] The second acquisition unit is used to acquire PNG images;
[0099] A second identification unit is used to read an image header block, an image data block and an image end block from the PNG image; and to identify a color type and an allowed bit depth from the image header block;
[0100] A decoding preparation unit, used to determine the number of bits of pixels in the PNG data stream and the number of bits of different channels in the pixels according to the color type;
[0101] The decoding unit is used to parse the information of the PNG image according to the number of bits of the pixel and the number of bits of different channels in the pixel.
[0102] It can be understood that some features of the encoding and decoding methods and devices of the PNG format in the present invention can be referenced to each other. In order to avoid tracing back, the present invention will not list them one by one.
[0103] The present invention extends the definition of true color images with unequal bit depths and true color images with unequal bit depths with alpha channel data on the traditional PNG format, so that the present invention can support encoding and decoding of formats such as RGB565 and RGB565A in addition to supporting traditional color types; it is convenient to display on chips such as MCU; the method of the present invention is also suitable for extending PNG's support for the colors of images of other color types, and can easily realize the interchange between various color types; at the same time, the method of the present invention can also be relatively compatible with the original PNG color type format.
[0104] In the present invention, unless otherwise clearly stipulated and limited, the terms such as "installation", "setting", "connection", "fixation" and "rotation" should be understood in a broad sense. For example, it can be a fixed connection, a detachable connection, or an integral one; it can be a mechanical connection or an electrical connection; it can be directly connected or indirectly connected through an intermediate medium; it can be the internal connection of two elements or the interaction relationship between two elements. Unless otherwise clearly defined, ordinary technicians in this field can understand the specific meanings of the above terms in the present invention according to the specific circumstances.
[0105] Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and variations may be made to the embodiments without departing from the principles and spirit of the present invention, and that the scope of the present invention is defined by the appended claims and their equivalents.
Claims
1. A PNG format encoding method, characterized in that: The encoding method comprises the following steps: Read the target pixel in the original image and calculate the number of bits of the current target pixel and the number of bits of different channels in each target pixel; Identify the color type of the current target pixel and correspond it to a preset definition value; set the bit depth allowed in the encoding process to point to the definition value; wherein the color type at least includes grayscale image, true color image, indexed color image, grayscale image with alpha channel data, true color image with alpha channel data, true color image with unequal bit depth and true color image with unequal bit depth with alpha channel data; Determine the size of the definition value of the color type to be encoded, and calculate the bit depth in the pixel according to the first condition for true color images of unequal bit depths and true color images of unequal bit depths with alpha channel data; calculate the bit depth in the pixel according to the second condition for images of other color types; The setting of the bit depth allowed in the encoding process to point to the defined value includes setting multiple optional third bit depths for grayscale images, true color images, indexed color images, grayscale images with alpha channel data, and true color images with alpha channel data, and the bit depth occupied by each channel is equal; setting a fixed bit depth for true color images with unequal bit depths and true color images with unequal bit depths with alpha channel data, and the fixed bit depth includes a first bit depth and a second bit depth; the first bit depth is the bit depth occupied by one of the color channels in the color type, and the second bit depth is the total bit depth occupied by a pixel in the color type; Calculating the pixel bit depths of the true color images with different bit depths and the true color images with different bit depths with alpha channel data according to the first condition includes calling a preset first function with an adjustable parameter to calculate the pixel bit depth according to their respective allowed first bit depths, or directly using the allowed second bit depth as the pixel bit depth; Calculating the bit depth of the image of other color types according to the second condition includes calling the preset first function to calculate the bit depth size in the pixel according to the third bit depth allowed by the first function; The preset first function is expressed as f(x)=3A+α, A represents the bit depth of the channel, and α represents the bit depth occupied by the α channel; the value of the adjustable parameter is ±1 or ±2; Determine the bit depth of each pixel to be encoded, calculate the size of the image, divide the image into blocks, preset the compression algorithm in the image header block and use the scan line pair to downsample the calculated image; After filtering the scan line data, it is compressed according to a preset compression algorithm to encode an image data block, and an image end block is encoded at the end of the image data block, thereby completing the encoding of the PNG image.
2. The PNG format encoding method according to claim 1, characterized in that: Before determining the size of the definition value of the color type to be encoded, it also includes converting the color type of the target pixel in the read original image, that is, converting from any color type among grayscale images, true color images, indexed color images, grayscale images with alpha channel data, true color images with alpha channel data, true color images with unequal bit depths, and true color images with unequal bit depths with alpha channel data to another color type different from the previous one; and re-determining the definition value of the new color type.
3. A PNG format decoding method, characterized in that: The decoding method comprises the following steps: Read out the PNG image, read out the image header block, image data block and image end block; According to the compression algorithm read from the image header block, the image data block is decompressed and then filtered and restored; Determine a color type according to a definition value read from an image header block, the color type including a grayscale image, a true color image, an indexed color image, a grayscale image with alpha channel data, a true color image with alpha channel data, a true color image with unequal bit depths, and a true color image with unequal bit depths with alpha channel data; According to the allowed bit depth of the color type read from the image header block, the bit depth in the pixel is calculated according to the third condition for true color images of unequal bit depths and true color images of unequal bit depths with alpha channel data; the bit depth in the pixel is calculated according to the second condition for images of other color types; Calculating the bit depth of the pixel according to the third condition includes determining whether the allowed bit depth of the true color image of unequal bit depth and the true color image of unequal bit depth with alpha channel data is greater than or equal to 8, and if so, using the allowed bit depth as the bit depth of the pixel, otherwise calling a preset first function with an adjustable parameter to calculate the bit depth of the pixel; Calculating the bit depth of the pixel according to the second condition for the image of other color types includes calling the preset first function to calculate the bit depth size in the pixel according to the allowed depth; The preset first function is expressed as f(x)=3A+α, where A represents the bit depth of the channel and α represents the bit depth occupied by the α channel; the value of the adjustable parameter is ±1 or ±2; Decoding the image information data from the PNG image data block according to the bit depth of each pixel; After the information of the image data block is decoded, the decoded information of the image header block and the image end block are loaded before and after the information of the image block respectively, so as to complete the decoding of the PNG image.
4. A PNG format encoding device, used to implement the encoding method according to claim 1 or 2, characterized in that: The encoding device comprises: A first acquisition unit, used for acquiring target pixels in the original image; A first recognition unit, used to recognize the color type of the target pixel and correspond it to a preset definition value; An encoding preparation unit, used for setting the bit depth allowed in the encoding process to point to the defined value; and for determining the bit depth of each pixel when encoding is required, so as to calculate the image size and determine the content of the image header block; The encoding unit is used to perform PNG encoding processing according to the image block determined by the encoding preparation unit, and encode an image header block, an image data block and an image end block.
5. A PNG format decoding device, characterized in that: For implementing the decoding method according to claim 3, the decoding device comprises: The second acquisition unit is used to acquire PNG images; A second identification unit is used to read an image header block, an image data block and an image end block from the PNG image; and to identify a color type and an allowed bit depth from the image header block; A decoding preparation unit, used to determine the number of bits of pixels in the PNG data stream and the number of bits of different channels in the pixels according to the color type; The decoding unit is used to parse out the information of the PNG image according to the number of bits of the pixel and the number of bits of different channels in the pixel.