CN115550661B - Image compression method, restoration method, computing device and readable storage medium - Google Patents

Image compression method, restoration method, computing device and readable storage medium Download PDF

Info

Publication number
CN115550661B
CN115550661B CN202211487190.5A CN202211487190A CN115550661B CN 115550661 B CN115550661 B CN 115550661B CN 202211487190 A CN202211487190 A CN 202211487190A CN 115550661 B CN115550661 B CN 115550661B
Authority
CN
China
Prior art keywords
data
image
pixel
color
compressed
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202211487190.5A
Other languages
Chinese (zh)
Other versions
CN115550661A (en
Inventor
张继德
陈斌
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Uniontech Software Technology Co Ltd
Original Assignee
Uniontech Software Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Uniontech Software Technology Co Ltd filed Critical Uniontech Software Technology Co Ltd
Priority to CN202211487190.5A priority Critical patent/CN115550661B/en
Publication of CN115550661A publication Critical patent/CN115550661A/en
Application granted granted Critical
Publication of CN115550661B publication Critical patent/CN115550661B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04NPICTORIAL COMMUNICATION, e.g. TELEVISION
    • H04N19/00Methods or arrangements for coding, decoding, compressing or decompressing digital video signals
    • H04N19/10Methods or arrangements for coding, decoding, compressing or decompressing digital video signals using adaptive coding
    • H04N19/169Methods or arrangements for coding, decoding, compressing or decompressing digital video signals using adaptive coding characterised by the coding unit, i.e. the structural portion or semantic portion of the video signal being the object or the subject of the adaptive coding
    • H04N19/186Methods or arrangements for coding, decoding, compressing or decompressing digital video signals using adaptive coding characterised by the coding unit, i.e. the structural portion or semantic portion of the video signal being the object or the subject of the adaptive coding the unit being a colour or a chrominance component
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04NPICTORIAL COMMUNICATION, e.g. TELEVISION
    • H04N19/00Methods or arrangements for coding, decoding, compressing or decompressing digital video signals
    • H04N19/42Methods or arrangements for coding, decoding, compressing or decompressing digital video signals characterised by implementation details or hardware specially adapted for video compression or decompression, e.g. dedicated software implementation

Landscapes

  • Engineering & Computer Science (AREA)
  • Multimedia (AREA)
  • Signal Processing (AREA)
  • Image Processing (AREA)

Abstract

The invention discloses an image compression method, an image restoration method, computing equipment and a readable storage medium, and relates to the field of image processing. The image compression method comprises the following steps: the method comprises the steps of obtaining a data file of an image to be compressed, wherein a pixel buffer area of the data file comprises transparency data of transparency channels of all pixels and color data of a plurality of color channels. Then, based on the color data of each color channel of each pixel, it is detected whether the image to be compressed is a pure color image. If so: extracting pure color data of an image to be compressed and storing the pure color data to a preset position; converting the transparency data of each pixel to be compressed into color data of a single-channel image; deleting the transparency data of each pixel and the color data of each color channel in the pixel buffer area, and reducing the size of the pixel buffer area to the width of a single channel; and writing the color data converted from the transparency data of each pixel to be compressed into the corresponding pixel buffer area. The invention can greatly improve the compression ratio of the pure color image and can realize lossless restoration.

Description

Image compression method, restoration method, computing device and readable storage medium
Technical Field
The present invention relates to the field of image processing, and in particular, to an image compression method, a restoration method, a computing device, and a readable storage medium.
Background
The more the pixel points of the image are, the larger the occupied storage space is, and the more inconvenient the transmission is. Based on this, various picture formats (Image formats) such as PNG, JPEG, WEBP, and the like have been derived. However, these file format compression techniques are not very effective in compressing the original pixel data of each image and ensuring the integrity of the data.
Therefore, a new image compression method is needed to solve the above technical problems.
Disclosure of Invention
To this end, the present invention provides an image compression method, a restoration method, a computing device and a readable storage medium in an attempt to solve or at least alleviate the above-presented problems.
According to an aspect of the present invention, there is provided an image compression method including: acquiring a data file of an image to be compressed, wherein a pixel buffer area of the data file comprises transparency data of a transparency channel of each pixel and color data of a plurality of color channels; detecting whether an image to be compressed is a pure color image or not based on color data of a plurality of color channels of each pixel; if so: extracting pure color data of an image to be compressed and storing the pure color data to a preset position; converting the transparency data of each pixel to be compressed into color data of a single-channel image, wherein the pixel to be compressed is a pixel with transparency data not being 0, and the pure color data is the color data of all color channels of the pixel to be compressed; deleting the transparency data of each pixel and the color data of a plurality of color channels in the pixel buffer area, and reducing the size of the pixel buffer area to a single channel width; and writing the color data converted from the transparency data of each pixel to be compressed into the corresponding pixel buffer area to obtain a compressed image.
Optionally, in an image compression method according to the present invention, detecting whether an image to be compressed is a solid image includes: detecting whether the color data of the same color channel of all the pixels are the same; or detecting whether the color data of the same color channel of all the pixels to be compressed are the same.
Optionally, in an image compression method according to the present invention, converting transparency data of each pixel to be compressed into color data of a single-channel image includes: and taking the transparency data of each pixel to be compressed as the color data of the single-channel image.
Alternatively, in the image compression method according to the present invention, the single-channel image is a grayscale image, and accordingly, the color data of the single-channel image is the luminance of the grayscale image.
Alternatively, in the image compression method according to the present invention, the predetermined location is a predetermined field in the data file, or a predetermined field in the file name of the data file, or an internal buffer of an application program that opens the compressed image.
Optionally, in the image compression method according to the present invention, the image to be compressed is an icon image.
Optionally, in the image compression method according to the present invention, the plurality of color channels includes a red channel, a green channel, and a blue channel.
According to still another aspect of the present invention, there is provided a method of restoring a compressed image obtained by the above method, comprising: constructing color data of each color channel of each pixel before compression of the compressed image based on the pure color data stored in the predetermined position; constructing transparency data of a transparency channel of each pixel before compression of the compressed image based on color data of a color channel of each pixel in a pixel buffer of a data file of the compressed image; and restoring the compressed image based on the transparency data of the transparency channel and the color data of each color channel of each pixel.
Alternatively, in a restoration method according to the present invention, restoring a compressed image includes: deleting the color data of each pixel in the pixel buffer area; expanding the size of the pixel buffer area to the size before compression, and writing the constructed transparency data of the transparency channel of each pixel and the color data of each color channel into the pixel buffer area; the solid color data stored in the predetermined location is deleted.
Alternatively, in a restoration method according to the present invention, restoring a compressed image includes: and rendering each pixel to an interface of an application program for opening the compressed image by using a drawing system according to the constructed transparency data of the transparency channel of each pixel and the constructed color data of each color channel.
Alternatively, in the restoration method according to the present invention, the predetermined location is a predetermined field in the data file, or a predetermined field in the file name of the data file, or an internal buffer of an application program that opens the compressed image.
Alternatively, in the restoration method according to the present invention, each color channel of each pixel before compression of the compressed image includes a red channel, a green channel, and a blue channel.
According to yet another aspect of the invention, there is provided a computing device comprising: at least one processor; and a memory storing program instructions, wherein the program instructions are configured to be executed by the at least one processor, the program instructions comprising instructions for performing any of the methods above.
According to yet another aspect of the present invention, there is provided a readable storage medium storing program instructions which, when read and executed by a computing device, cause the computing device to perform any of the above methods.
The invention provides an image compression method of a single color channel for a pure color image, and the method can greatly improve the compression rate of the image under the condition of not changing the format of the original image. Moreover, the invention also provides a corresponding image restoration method, which can carry out lossless restoration on the image compressed by the image compression method of the single color channel.
Drawings
To the accomplishment of the foregoing and related ends, certain illustrative aspects are described herein in connection with the following description and the annexed drawings, which are indicative of various ways in which the principles disclosed herein may be practiced, and all aspects and equivalents thereof are intended to be within the scope of the claimed subject matter. The above and other objects, features and advantages of the present disclosure will become more apparent from the following detailed description read in conjunction with the accompanying drawings. Throughout this disclosure, like reference numerals generally refer to like parts or elements.
FIG. 1 shows a block diagram of a computing device 100, according to one embodiment of the invention;
FIG. 2 shows a flow diagram of a method 200 of image compression according to one embodiment of the invention;
FIG. 3 is a diagram illustrating a distribution of pixel data structures for a pixel site according to one embodiment of the invention;
FIG. 4 is a diagram illustrating a transformed pixel data structure distribution of pixel points, according to one embodiment of the invention;
FIG. 5 is a schematic diagram illustrating a flow chart for writing data into a PNG file according to one embodiment of the present invention;
FIG. 6 shows a schematic diagram of a flow chart of an image compression method according to a further embodiment of the invention;
FIG. 7 shows a flow diagram of an image restoration method 700 according to one embodiment of the invention;
FIG. 8 is a schematic diagram illustrating a flow chart of a method for restoring an image at a file layer according to one embodiment of the invention;
fig. 9 shows a schematic diagram of an image restoration process according to an embodiment of the present invention.
Detailed Description
Exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be embodied in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the scope of the disclosure to those skilled in the art.
The compression effect of the current compression technologies of PNG, JPEG, WEBP and other formats is not good. For example, taking the PNG format as an example, table 1 below shows the size of the original file and the size of the PNG file when one solid icon contains different numbers of pixels:
Figure DEST_PATH_IMAGE001
TABLE 1
It can be seen that as the size of the solid icon increases, the size of the PNG file also increases. Obviously, if the icons stored in the computer are all of the size shown above, the resource files required by the application will be a large part of the application and will be inseparable, resulting in a large waste of system resources. Based on the above, the present invention provides an image compression method and a corresponding image restoration method for a single color channel of a pure color image, which can greatly improve the compression rate of the image and can realize lossless restoration.
Fig. 1 illustrates a block diagram of the physical components (i.e., hardware) of a computing device 100. In a basic configuration, computing device 100 includes at least one processing unit 102 and system memory 104. According to one aspect, the processing unit 102 may be implemented as a processor depending on the configuration and type of computing device. The system memory 104 includes, but is not limited to, volatile storage (e.g., random access memory), non-volatile storage (e.g., read-only memory), flash memory, or any combination of such memories. According to one aspect, operating system 105 and program modules 106 are included in system memory 104, image compression module 120 and image restoration module 130 are included in program modules 106, image compression module 120 is configured to perform image compression method 200 of the present invention, and image restoration module 130 is configured to perform image restoration method 700 of the present invention.
According to one aspect, the operating system 105 is, for example, suitable for controlling the operation of the computing device 100. Further, the examples are practiced in conjunction with a graphics library, other operating systems, or any other application program, and are not limited to any particular application or system. This basic configuration is illustrated in fig. 1 by those components within dashed line 108. According to one aspect, the computing device 100 has additional features or functionality. For example, according to one aspect, computing device 100 includes additional data storage devices (removable and/or non-removable) such as, for example, magnetic disks, optical disks, or tape. Such additional storage is illustrated in FIG. 1 by removable storage 109 and non-removable storage 110.
As stated hereinabove, according to one aspect, program modules are stored in the system memory 104. According to one aspect, the program modules may include one or more applications, the invention not being limited to the type of application, for example, the applications may include: email and contacts applications, word processing applications, spreadsheet applications, database applications, slide show applications, drawing or computer-aided applications, web browser applications, and the like.
According to one aspect, examples may be practiced in a circuit comprising discrete electronic elements, a packaged or integrated electronic chip containing logic gates, a circuit utilizing a microprocessor, or on a single chip containing electronic elements or microprocessors. For example, an example may be practiced via a system on a chip (SOC) in which each or many of the components shown in fig. 1 may be integrated on a single integrated circuit. According to one aspect, such SOC devices may include one or more processing units, graphics units, communication units, system virtualization units, and various application functions, all integrated (or "burned") onto a chip substrate as a single integrated circuit. When operating via an SOC, the functions described herein may be operated via application-specific logic integrated with other components of the computing device 100 on a single integrated circuit (chip). Embodiments of the invention may also be practiced using other technologies capable of performing logical operations (e.g., AND, OR, AND NOT), including but NOT limited to mechanical, optical, fluidic, AND quantum technologies. In addition, embodiments of the invention may be practiced within a general purpose computer or in any other circuit or system.
According to one aspect, computing device 100 may also have one or more input devices 112, such as a keyboard, mouse, pen, voice input device, touch input device, etc. Output device(s) 114 such as a display, speakers, printer, etc. may also be included. The foregoing devices are examples and other devices may also be used. Computing device 100 may include one or more communication connections 116 that allow communication with other computing devices 118. Examples of suitable communication connections 116 include, but are not limited to: RF transmitter, receiver and/or transceiver circuitry; universal Serial Bus (USB), parallel, and/or serial ports.
The term computer readable media as used herein includes computer storage media. Computer storage media may include volatile and nonvolatile, removable and non-removable media implemented in any method or technology for storage of information, such as computer readable instructions, data structures, or program modules. System memory 104, removable storage 109, and non-removable storage 110 are all examples of computer storage media (i.e., memory storage). Computer storage media may include Random Access Memory (RAM), read Only Memory (ROM), electrically erasable read-only memory (EEPROM), flash memory or other memory technology, CD-ROM, digital Versatile Disks (DVD) or other optical storage, magnetic cassettes, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other article of manufacture that can be used to store information and that can be accessed by computing device 100. In accordance with one aspect, any such computer storage media may be part of computing device 100. Computer storage media does not include a carrier wave or other propagated data signal.
According to one aspect, communication media is embodied by computer readable instructions, data structures, program modules, or other data in a modulated data signal (e.g., a carrier wave or other transport mechanism) and includes any information delivery media. According to one aspect, the term "modulated data signal" describes a signal that has one or more feature sets or that has been altered in such a manner as to encode information in the signal. By way of example, and not limitation, communication media includes wired media such as a wired network or direct-wired connection, and wireless media such as acoustic, radio Frequency (RF), infrared, and other wireless media.
FIG. 2 illustrates a flow diagram of a method 200 of image compression, the method 200 being suitable for execution in a computing device (e.g., the computing device 100 shown in FIG. 1), according to one embodiment of the invention. As shown in fig. 2, the method 200 begins at 210.
In 210, a data file of an image to be compressed is obtained, and a pixel buffer of the data file includes transparency data of a transparency channel of each pixel and color data of a plurality of color channels. That is, each pixel in the image to be compressed in the present embodiment is composed of a transparency channel and several color channels (that is, the image to be compressed is a multi-channel image whose pixels are composed of a plurality of channels). In some embodiments, the several color channels may specifically include a red color channel, a green color channel, and a blue color channel, that is, each pixel in the image to be compressed is composed of four channels of transparency (a), red (R), green (G), and blue (B) (in other words, the image to be compressed is an image in the ARGB color mode).
A. Since each of the channels R, G, and B is stored with data having a size of 8 bits, the data of each of the channels a, R, G, and B can be represented by an assigned char. Since each channel of a, R, G, and B is stored by data with a size of 8 bits, the size occupied by each pixel point is 32 bits (4 × 8= 32), and the size is the same as the signaled int type in the computing device, so that the value of each pixel point can be represented by the signaled int type, as shown in fig. 3, which shows the structural distribution of the pixel data (or pixel value) of one pixel point.
An exemplary code of the structural distribution of pixel data shown in fig. 3, according to one embodiment of the present invention, is as follows:
typedef unsigned int PixelData;
typedef unsigned char ChannelData;
static ChannelData redChannel(PixelData pixel)
{
return ((pixel >> 16) & 0xff);
}
static ChannelData greenChannel(PixelData pixel)
{
return ((pixel >> 8) & 0xff);
}
static ChannelData blueChannel(PixelData pixel)
{
return (pixel & 0xff);
}
static ChannelData alphaChannel(PixelData pixel)
{
return (pixel >> 24);
}
static PixelData makePixel(ChannelData r, ChannelData g, ChannelData b, ChannelData a)
{
return ((a & 0xffu) << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | (b & 0xffu);
}
static PixelData convertPixelARGBToRGB(PixelData argb)
{
if (alphaChannel(argb) == 0)
return argb;
return makePixel(redChannel(argb), greenChannel(argb), blueChannel(argb), 0);
}
some picture formats (such as PNG) have different data byte writing modes on different large and small end machines, the large end machine uses ARGB for storage, the small end uses BGRA for storage, and accordingly, data reading can use ARGB or BGRA. Specifically, in the present embodiment, data reading can be performed using ARGB.
As an example, if the image to be compressed is in PNG format, the data file of the image to be compressed is obtained in ARGB format through the reading interface provided by the PNG dynamic library, and the exemplary code is as follows:
bool PNGHander::readPNGHeader(png_struct *png_ptr, png_info *info_ptr)
{
png_get_IHDR(png_ptr, info_ptr, &data->width, &data->height, &data->depth, &data->color_type, nullptr, nullptr, nullptr);
data->bytes_per_line = png_get_rowbytes(png_ptr, info_ptr);
data->bytes = new ChannelData [data->height * data->bytes_per_line];
data->nbytes = data->bytes_per_line * data->height;
png_set_bgr(png_ptr);
return true;
}
bool PNGHander::readPNGData(std::string src_file_name)
{
png_struct* png_ptr;
png_info *info_ptr;
if (!(png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr)))
return false;
if (!(info_ptr = png_create_info_struct(png_ptr))) {
png_destroy_read_struct(&png_ptr, nullptr, nullptr);
png_ptr = nullptr;
return false;
}
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, nullptr, nullptr);
png_ptr = nullptr;
return false;
}
std::ifstream file;
file.open(src_file_name, std::ios::in);
if (!file.is_open())
return false;
png_set_read_fn(png_ptr, (png_voidp)&file, read_png_fn);
png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, const_cast<png_bytep>((const png_byte *)RGBSIG_NAME), 1);
png_set_read_user_chunk_fn(png_ptr, &data->color_sig, read_color_sig_chunk);
png_read_info(png_ptr, info_ptr);
if (!readPNGHeader(png_ptr, info_ptr))
return false;
png_bytepp buffer = (png_bytepp)png_malloc(png_ptr, data->height * sizeof (png_bytepp));
for (PixelData i = 0; i < data->height; ++i)
buffer[i] = data->bytes + i * data->bytes_per_line;
png_read_image(png_ptr, buffer);
png_free(png_ptr, buffer);
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
png_ptr = nullptr;
info_ptr = nullptr;
file.close();
return true;
}
reading each data of the image to be compressed into a data structure in the class, wherein the data members of the structure are shown as follows:
struct PNGData {
png_uint_32 width;
png_uint_32 height;
int depth;
int color_type;
unsigned long bytes_per_line;
unsigned long nbytes;
PixelData color_sig;
ChannelData *bytes = nullptr;
} *data;
it can be seen that, in this embodiment, the data file of the image to be compressed includes, in addition to the data of the four channels of red, green, blue and transparency of each pixel, the size width and height (i.e. the number of pixel points) of the image to be compressed; a color depth, where the PNG only has color depths of 1 and 8 in a normal case, such as ARGB, RGB, gray (grayscale) depth of 8, and other types of pixel depth of 1, where the depth is mainly used for internal parsing and encoding of the PNG; type of picture color _ type; a pixel byte size byte _ per _ line for each line, which indicates the size of a pixel channel in each line of data, is normally equal to width × number of bytes of channel, but since there are cases where the width is singular, there are some algorithms where the size is equal to ((width × number of bytes of channel + 31)/32) × 4; the number nbytes of all bytes of the image value; the extracted pure color data color is the RGB value after Alpha is removed; and all image channel byte numbers bytes.
It should be noted that the color channels shown above are red, green, and blue channels, which is only an example, and the present invention is not limited to the color channels. In addition, the present invention is not limited to the image to be compressed, and may be an icon image, for example.
Thus, a data file of the image to be compressed is obtained, and then 220 is entered, and whether the image to be compressed is a pure color image is detected based on the color data of the plurality of color channels of each pixel.
According to one embodiment of the present invention, whether the image to be compressed is a pure color image may be determined by detecting whether the color data of the same color channel of all pixels is the same. That is, for each color channel except for the transparency channel, whether the color data of the color channel of all pixels are the same is detected, if all the color data are the same, the image to be compressed is determined to be a pure color image, otherwise, the image to be compressed is determined to be a non-pure color image.
In addition, in some embodiments, in consideration of a pixel with the transparency data of 0, regardless of the specific value of the color data of the color channel, the final display effect of the pixel is pure transparent, so in order to reduce the detected data amount and shorten the detection time, when determining whether the image to be compressed is a pure color image, it may be further performed to only detect whether the color data of the same color channel of the pixel with the transparency data of 0 is the same, if the color data of the same color channel of the pixel with the transparency data of 0 is the same, the image to be compressed is determined to be a pure color image, otherwise, the image is determined to be a non-pure color image.
If the image to be compressed is a pure color image, the process proceeds to 230, and the pure color data of the image to be compressed is extracted and stored to a predetermined position. That is, the solid color data of the image to be compressed is extracted and stored to a predetermined position.
The following description will be given first of all to the solid color data. In the pure color image, all pixels use the same color data (it has been indicated above that the color data of the same color channel of all pixels in the pure color image are identical), and the common color data is the pure color data of the pure color image. Therefore, the pure color data of the image to be compressed is the common color data of all pixels in the image to be compressed, and further, the pure color data of the image to be compressed is the color data of all color channels of any pixel in the image to be compressed. Based on this, the pure color data of the image to be compressed is extracted, specifically, the color data of all the color channels of any pixel except the transparency channel is extracted (the extracted color data of all the color channels is the pure color data of the image to be compressed). For example, when the color channels are R, G, and B (i.e., the image to be compressed has four channels, i.e., a, R, G, and B), the RGB field data of all pixels in the image to be compressed is a same fixed value, and further, the 24-bit values formed by the RGB fields of each pixel are the same, and the value is extracted as the pure color data of the image to be compressed.
The predetermined position may be a predetermined field customized in the data file of the image to be compressed, a predetermined field customized in the file name of the data file of the image to be compressed, or an internal buffer of the application program for opening the image to be compressed. That is, when the extracted solid color data is stored to a predetermined position, the extracted solid color data may be stored to a predetermined field of a data file, or the extracted solid color data may be stored to a predetermined field of a file name of the data file, or the extracted solid color data may be stored to an internal buffer of an application program that opens an image to be compressed. The present invention is not limited to the specific manner of storing the extracted pure color data, and in a specific embodiment, a person skilled in the art can set the data according to actual needs.
After extracting and storing the pure color data of the image to be compressed, the color data of each color channel of the pixel with the transparency of not 0 in the pixel buffer area can be modified (or converted) to 0, so that the pixel data of the pixel point with the transparency of not 0 in the image to be compressed can be reduced from the size of N bytes to Alpha data with the size of 1 byte and '0' data with N-1 bytes. For the pixel point with the transparency of 0, the display effect is considered to be pure transparency, and the pixel point can be directly deleted.
Continuing to take the color channels R, G, and B as an example, specifically, after determining that the image to be compressed is a pure color image, deleting the data of the channels a, R, G, and B of the pixel point with the transparency of 0 in the image to be compressed, extracting the uniform RGB value of the image to be compressed, and converting the color data of the channels R, G, and B of the pixel point with the transparency of 0 into 0, so that the pixel data of the pixel point with the transparency of 0 in the image to be compressed can be reduced from the size of 4 bytes to Alpha data with the size of 1 byte and "0" data with the size of 3 bytes, and the distribution of the converted pixel data can be shown in fig. 4.
In the above example, the exemplary code for determining whether the image to be compressed is a pure color image, traversing all pixel data of the image to be compressed, deleting the pixel points with the transparency of 0, extracting the uniform RGB value of the image to be compressed, and converting the RGB data of the pixel points with the transparency of 0 into 0 is as follows:
bool PNGHander::canExtractRGB()
{
if (!data)
return false;
// Not supported for RGB or RGBA.
if ((data->color_type != PNG_COLOR_TYPE_RGBA)
&& (data->color_type != PNG_COLOR_TYPE_RGB)) {
return false;
}
if (!data->bytes)
return false;
return true;
}
long PNGHander::extractRGB()
{
if (!canExtractRGB())
return -1;
static PixelData pp = 0;
PixelData *pixels = (PixelData *)data->bytes;
bool extracted = std::all_of(pixels, pixels + data->nbytes / sizeof (PixelData), [](PixelData data) -> bool {
PixelData rgb = convertPixelARGBToRGB(data);
if (pp == 0) {
pp = rgb;
return true;
}
return (data == 0) || (pp == rgb);
});
if (extracted)
return pp;
return -1;// Can't be extract.
}
therefore, it can be seen that the image to be compressed can be compressed by deleting the pixel points with the transparency of 0 in the image to be compressed and converting the data of each color channel of the pixel points with the transparency of not 0 into 0, so that the space occupied by the image to be compressed is reduced. However, the "0" data still occupies the memory, so in some embodiments, in order to further reduce the space occupied by the compressed image, the writing of the compressed data can be completed by rearranging the data of a plurality of bytes into the data of one byte and writing the data into the image, thereby compressing the image to be compressed. For example, when the image to be compressed is an image in the ARGB color mode, the image to be compressed may be compressed by rearranging four bytes of data of pixels into one byte of data and writing the data into the image. How to rearrange the data of a plurality of bytes of pixels in the image to be compressed into data of one byte is described below by 240, specifically as follows.
At 240, the transparency data for each pixel to be compressed is converted to color data for a single channel image. For convenience of description, the present embodiment refers to a pixel whose transparency is not 0 as a pixel to be compressed.
Since the pixel point with the transparency of 0 is displayed as pure transparency, in this embodiment, the pixel with the transparency of 0 can also be deleted directly, and only the pixel with the transparency of not 0 is processed by converting the multi-byte into the one-byte, that is, only the pixel to be compressed is processed by converting the multi-byte into the one-byte. Specifically, when the image to be compressed is determined to be a solid image, the solid data of the image to be compressed is extracted, and therefore, for each pixel to be compressed, only the transparency data of the pixel needs to be stored, and the data (i.e., the pixel data) of each channel can be known. Based on this, when a plurality of bytes of each pixel to be compressed are rearranged into one byte, the color data of the color channel thereof can be deleted, and only the transparency data is converted to record the transparency data. Considering that a single-channel image has only one color channel, when each pixel to be compressed is processed from multiple bytes to one byte, the transparency data of the pixel can be converted into color data of the single-channel image, that is, the transparency data is converted into color data of the unique color channel of the single-channel image. Further, in some embodiments, when converting the transparency data of each pixel to be compressed into color data of a unique color channel possessed by a single-channel image, the transparency data may be directly regarded as the color data of the single-channel image. Of course, this is only an example, and the present invention is not limited thereto.
The storage mode of the gray (gray-8 bit) image is 8-bit storage, and the data of the gray (gray-8 bit) image is changed from 0 to 255 and is used for storing the gray brightness. Therefore, the gray image does not have the pixel data of ARGB, which is only one of the pixel luminances. Based on this, in some embodiments, the single-channel image may be a grayscale image, and accordingly, the color data of the single-channel image is the brightness of the grayscale image, that is, when the pixels to be compressed are processed by converting the bytes into one byte, the opacity data is converted into the brightness of the grayscale image, so as to store the image to be compressed as the grayscale image, which does not lose the byte data, but can reduce the file storage size.
It should be noted that, when the transparency data of each pixel to be compressed is converted into the brightness of a Gray channel, data in Gray (Gray scale) format needs to be adapted so that the pixel point with the correct picture depth can be matched in the subsequent data writing process.
The transparency data of each pixel to be compressed is converted into color data of a single-channel image, and then the process proceeds to 250, the transparency data of each pixel and the color data of a plurality of color channels in the pixel buffer are deleted, and the size of the pixel buffer is reduced to the width of a single channel. That is, the pixel data (i.e., transparency data and color data of each color channel) of each pixel in the pixel buffer of the image to be compressed is deleted, and the size of the pixel buffer of the image to be compressed is reduced from the multi-channel width to the single-channel width. For example, when the image to be compressed is an image of the ARGB color mode and the single-channel image is a grayscale image, the size of the pixel buffer of the pixel to be compressed is reduced to 1/4 of that.
And then entering 260, and writing the color data converted from the transparency data of each pixel to be compressed into a corresponding pixel buffer area to obtain a compressed image. That is, the data of the unique channel of the single-channel image converted from the transparency data of each pixel to be compressed is written into the pixel buffer corresponding to the pixel, so as to obtain the compressed image after the image to be compressed is compressed. For example, when the single-channel image is a grayscale image, the transparency data of each pixel to be compressed may be filled into the pixel buffer corresponding to the pixel as the brightness data of the gray channel.
For better understanding of the writing process, the color mode of the image to be compressed is ARGB, the format is PNG, the single-channel image is a grayscale image, and the predetermined position is a predetermined field in the data file of the image to be compressed, which is specifically shown in fig. 5. Firstly, the pixel data (i.e. original data) of each pixel to be compressed is subjected to image depth adaptation processing, the pixel data is converted into data in a Gray scale (Gray) format, the image depth is reduced from 32 bits to 8 bits, then transparency data of each pixel to be compressed is written into a PNG data file as the brightness of a Gray scale image by using a standard image writing interface provided by PNG, and common RGB data of the extracted image to be compressed is stored into a predetermined field of the PNG data file, so that the data writing is completed.
According to an embodiment of the present invention, exemplary codes for writing each data after compressing an image to be compressed into a data file through a standard image writing interface provided by the PNG are as follows:
// deleting RGB data in image
void PNGHander::removeRGBColorData()
{
// recalculating data size
data->nbytes /= sizeof(PixelData);
ChannelData *buffer = new ChannelData[data->nbytes];
PixelData *pixels = (PixelData *)data->bytes;
ChannelData *dest = buffer;
for (PixelData *p = pixels; p < pixels + (data->nbytes); ++p, ++dest)
* dest = alphaChannel ([ p) ]/compressed data is filled with Alpha values in the source data
delete[] data->bytes;
data->bytes = buffer;
data- > bytes _ per _ line/= sizeof (PixelData)// updating attribute information
}
bool PNGHander::writePNGData(const std::string &out_file_name, int out_ct, int quality, PixelData rgb)
{
png_struct* png_ptr;
png_info *info_ptr;
PNG _ ptr = PNG _ create _ write _ struct (PNG _ LIBPNG _ VER _ STRING, nulptr);/create PNG data block
if (!png_ptr)
return false;
info _ ptr = PNG _ create _ info _ struct (PNG _ ptr)// create PNG information block
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, nullptr);
return false;
}
if (setjmp (png _ jmpbuf (png _ ptr))) {// multithread operation protection
png_destroy_write_struct(&png_ptr, &info_ptr);
return false;
}
std: stream file// output file stream
Open (out _ file _ name, std:: ios:: out | std:: ios:: trunc | std:: ios:: binary), write in binary mode
if (!file.is_open())
return false;
I/O callback function with write-in settings
png_set_write_fn(png_ptr, (png_voidp)&file, write_png_fn, write_flush_fn);
// set PNG header information
png_set_IHDR(png_ptr, info_ptr, data->width, data->height, data->depth, out_ct, 0, 0, 0);
// BGRA storage for the cell therefore requires a switch
png_set_bgr(png_ptr);// Big Endian: ARGB Little Endian BGRA.
if (rgb != 0) {
uchar color_sig[3];
color_sig[0] = redChannel(rgb);
color_sig[1] = greenChannel(rgb);
color_sig[2] = blueChannel(rgb);
png_unknown_chunk color_chunk = {
RGBSIG_NAME,
color_sig,
sizeof (color_sig),
PNG_HAVE_IHDR
};
// writing RGB data blocks into custom data
png_set_unknown_chunks(png_ptr, info_ptr, &color_chunk, 1);
}
I/write information contains header information and custom data block information
png_write_info(png_ptr, info_ptr);
png_set_packing(png_ptr);
Setting the compression level to take values from 0 to 9, 0 being no compression
png_set_compression_level(png_ptr, quality);
png_bytepp buffer = (png_bytepp)png_malloc(png_ptr, data->height * sizeof (png_bytepp));
for (int y = 0; y < data->height; y++)
buffer[y] = data->bytes + y * data->bytes_per_line;
// writing picture data
png_write_image(png_ptr, buffer);
png_free(png_ptr, buffer);
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
file.close();
return true;
}
static void write_png_fn(png_structp png_ptr, png_bytep data, png_size_t length)
{
std::ofstream *ofs = (std::ofstream *)png_get_io_ptr(png_ptr);
if (!ofs || !ofs->is_open())
return;
// write back direct operation offset write
ofs->write((char*)data, length);
}
static void write_flush_fn(png_structp png_ptr)
{
std::ofstream *ofs = (std::ofstream *)png_get_io_ptr(png_ptr);
if (!ofs || !ofs->is_open())
return;
// write refresh direct refresh
ofs->flush();
}
At this point, the color data converted from the transparency data of each pixel to be compressed is written into the data file, and the extracted pure color data is stored to a preset position, thereby completing the compression of the image to be compressed. Therefore, the invention provides an image compression method for a single color channel of a pure color image, and the compression method can greatly improve the compression rate on the premise of ensuring the data integrity, thereby greatly reducing the size of the pure color image and enabling the pure color image to be more convenient and efficient in storage and transmission.
For better understanding of the present invention, the whole process of the image compression method of the present invention is described below by a specific example with reference to fig. 6. In this example, the image to be compressed is an icon image in PNG format, and has four channels of a, R, G, and B (i.e., each pixel point in the image to be compressed is a color channel value of ARGB), and the specific compression process is as follows.
Firstly, acquiring a PNG file of an image to be compressed, wherein the PNG file comprises pixel data of each pixel, namely four channel data of A, R, G and B.
And secondly, detecting whether the image to be compressed is a pure color image. Specifically, whether R channel data, G channel data and B channel data of all pixels are the same or not is detected, if the R channel data, the G channel data and the B channel data of all pixels are the same (namely, all pixels adopt the same RGB data), the image to be compressed is judged to be a pure color image, and if not, the image to be compressed is judged to be a non-pure color image, and the method is finished.
And thirdly, when the image to be compressed is judged to be a pure color image, traversing the pixel data of each pixel, eliminating the pixel data with the transparency (namely, the A channel data) of 0, and extracting the uniform RGB data (namely, the pure color data in the embodiment) adopted by the image to be compressed.
And fourthly, sorting the pixel data with the transparency of not 0, storing the gray scale of the four channels of the ARGB by using one channel, specifically, deleting the RGB data of the pixel with the transparency of not 0, converting the transparency of the pixel into the brightness of the gray scale image, and adapting the depth of the gray scale image during conversion.
And fifthly, writing the brightness converted from the transparency of each pixel with the transparency not being 0 into the PNG file of the image to be compressed, and writing the uniform RGB data adopted by the image to be compressed into a preset field of the file name of the PNG file of the image to be compressed, thereby completing the compression of the image to be compressed.
Obviously, after the image to be compressed is compressed, each pixel in the compressed data has only one gray channel data, and each pixel before compression has four channel data of a, R, G and B. Therefore, the compression method of the single color channel is used for compressing the pure color image in the ARGB color mode, and about three-quarters of the original image data can be reduced. A specific example will be described below.
When the size of the image to be compressed is 1000 × 1000, since the ARGBs are stored by 8 bits, each pixel needs 4 bytes of physical disk data to be stored, and the disk size required for storing the image to be compressed is: 4 × 1000=4000000b =3906.3kb =3.9m. When the image to be compressed is compressed by the image compression method of a single color channel of the present invention, each pixel has only one gray channel data stored by 8 bits, and thus the disk size required for storing the compressed image is 1 × 1000=1000000b =976.6kb =0.9m. Obviously, after the compression method of the single color channel is used for compressing the image to be compressed, the size of a disk for storing the image can be reduced to one fourth, so that the load of the disk space can be greatly reduced, and the method is greatly helpful for optimizing a physical disk and reducing the size of a resource file by a computer.
The foregoing has shown the image compression method of the present invention for a single color channel of a solid color image, and for this purpose, the present invention also provides a corresponding image restoration method. That is, there is provided a method of restoring an image compressed by the image compression method using the above-described single color channel.
FIG. 7 illustrates a flow diagram of a method 700 of image restoration, the method 700 adapted to be performed in a computing device (e.g., the computing device 100 shown in FIG. 1), according to one embodiment of the invention. As shown in fig. 7, the method 700 begins at 710. Before 710, it is determined whether the image to be restored is a compressed image obtained by compressing the image using the image compression method of the single color channel. Specifically, detection can be performed as follows.
Firstly, a data file of an image to be restored is obtained, wherein the data file comprises a pixel format of the image to be restored. The pixel format includes Gray (i.e., gray scale) format, ARGB format, and the like. Then, whether the pixel format of the image to be restored is a predetermined format is detected. The predetermined format of the pixels has only one color channel, and thus the predetermined format may be a gray scale format, which is only an example and is not limited to the present invention.
If the pixel format of the image to be restored is not the preset format, which indicates that the image to be restored is not a single-channel image, the image to be restored is judged not to be compressed by adopting the single-color-channel image compression method, and then the process is finished. And if the format of the pixels of the image to be restored is the preset format, the image to be restored is a single-channel image. Then, whether pure color data are stored in the preset position is detected, namely whether the image to be restored is a pure color image before compression is detected.
The predetermined location may be a predetermined field in a data file of the image to be restored, a predetermined field in a file name of the data file of the image to be restored, or an internal buffer of an application program that opens the image to be restored. That is, the step may be to detect whether the solid color data is stored in a predetermined field in the data file of the image to be restored, to detect whether the solid color data is stored in a predetermined field in the file name of the data file of the image to be restored, or to detect whether the solid color data is stored in an internal buffer of the application program that opens the image to be restored.
If the predetermined position stores pure color data, it indicates that the image to be restored is a pure color image before compression, then it is determined that the image to be restored is a compressed image obtained by compression by the single color channel image compression method, and then the process proceeds to 710.
At 710, color data for each color channel of each pixel prior to compression of the compressed image is constructed based on the solid color data stored in the predetermined location. Based on the above description, the pure color data stored in the predetermined location is the color data of all channels except the transparency channel of the pixel before the compression of the compressed image (i.e., the image to be restored), i.e., the color data of each color channel of each pixel, and the pure color data is obtained from the predetermined location.
Specifically, if the predetermined position is a predetermined field in the file name of the data file of the compressed image, parsing reading is performed according to the parsing rule of the file name, if the predetermined position is a predetermined field in the data file of the compressed image, the predetermined field is read from the data file, and if the predetermined position is an internal buffer of an application program that opens the compressed image, reading is directly performed from the buffer.
An example code for reading pure color data from a predetermined field of a data file of a compressed image (in this example, the compressed image is in PNG format, and the stored pure color data is RGB data) is shown below:
#define RGBSIG_NAME "rgb"
static int read_color_sig_chunk(png_structp png_ptr, png_unknown_chunkp chunk)
{
PixelData *color_sig = (PixelData *)png_get_user_chunk_ptr(png_ptr);
if (!color_sig)
return 0;
if (!strcmp((const char *)chunk->name, RGBSIG_NAME))
*color_sig = makePixel((ChannelData)chunk->data[0], (ChannelData)chunk->data[1], (ChannelData)chunk->data[2], 0);
return 1;
}
png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, const_cast<png_bytep>((const png_byte *)RGBSIG_NAME), 1);
png_set_read_user_chunk_fn(png_ptr, &data->color_sig, read_color_sig_chunk);
png_read_info(png_ptr, info_ptr);
next, based on the obtained pure color data, color data of each color channel of each pixel before compression of the compressed image is constructed. Wherein, in some embodiments, the pure color data may be split into color data for each color channel of each pixel. Specifically, each predetermined number of bits in the solid color data may be used as color data of one color channel in order from the upper bits to the lower bits. Specifically, the color channels may include a red channel, a green channel, and a blue channel, and accordingly, the pure color data is 24 bits and the predetermined number is 8. In this way, when the pure color data is divided into the color data of each color channel of each pixel, each 8 bits of the pure color data can be used as the color data of one color channel in the order from the upper bits to the lower bits. Further, the upper 8 bits, the middle 8 bits, and the lower 8 bits of the solid color data may be sequentially used as red channel data, green channel data, and blue channel data of each pixel before compression of the compressed image. Of course, this is only an example, and the invention is not limited to how to construct the color data of each color channel of each pixel before compressing the compressed image.
Subsequently, proceeding to 720, transparency data for a transparency channel of each pixel before compression of the compressed image is constructed based on color data for the color channel of each pixel in the pixel buffer of the data file of the compressed image. When the number of bits of the color data of the color channel of the pixel in the compressed image is consistent with the number of bits of the transparency data of the transparency channel of the pixel before compression of the compressed image, the color data of the color channel of each pixel in the current pixel buffer area can be directly used as the transparency data of the transparency channel of the pixel before compression. Of course, this is merely an example, and the present invention is not limited thereto.
The transparency data of the transparency channel and the color data of each color channel of each pixel to be compressed are constructed before the compressed image is compressed, and then the process proceeds to 730, and the compressed image is restored based on the constructed transparency data of each pixel and the constructed color data of each color channel. The compressed image may be restored at a file layer or an application rendering layer, as follows.
When a compressed image is restored at the file level, first, color data of each pixel in a pixel buffer of the compressed image is deleted.
Then, the pixel buffer size is expanded to the size before compression, and the constructed transparency data of the transparency channel and color data of each color channel of each pixel are written in the pixel buffer. That is, when writing the constructed data of each channel of each pixel into the pixel buffer, the bit number of the pixel data of each pixel in the pixel buffer of the data file of the compressed image is expanded, specifically to the bit number of the pixel data of each pixel before the compressed image is compressed, and then the constructed pixel data of each pixel (i.e. the transparency data of the transparency channel and the color data of each color channel) is written into the pixel buffer corresponding to the pixel. The pixels and the buffer areas are in one-to-one correspondence, so that after the pixel data of each constructed pixel is written into the corresponding pixel buffer area, whether the buffer area without filled pixel data exists can be further detected, if yes, it is indicated that the compressed image has a pixel with the transparency of 0 before compression, and the pixel corresponding to the pixel buffer area with empty current pixel data is the pixel with the transparency of 0 before compression, then the pixel buffer area with empty current pixel data is filled with the corresponding pixel data, specifically, 0 is used as the transparency data, and the pure color data is used as the color data of all color channels. At this point, the pixel data of each pixel before compression of the compressed image is recovered.
And finally, deleting the pure color data stored in the preset position.
As an example, the compressed image is a grayscale image, and the compressed image is an image in an ARGB color mode before compression, the number of bits of pixel data of each pixel in a pixel buffer of the compressed image is first expanded from 8bit to 32bit, then RGB data (i.e., the above-mentioned pure color data) shared before compression of the compressed image stored in a predetermined position is used as RGB data (i.e., R, G, and B channel data) of each pixel, the luminance of each pixel stored in the pixel buffer of the compressed image is used as transparency data (i.e., a channel data) of each pixel before compression of the compressed image and written into a corresponding pixel buffer, if there is an empty pixel buffer, 0 is used as the transparency data, and the RGB data stored in the predetermined position is filled as the R, G, and B channel data, so that the ARGB data before compression is constructed and stored in an ARGB format, and thus the compressed image is restored to the original image before compression.
The bit number of the pixel data in the pixel buffer area of the compressed image is expanded from 8bit to 32bit, and then the general RGB data and the stored Alpha data are written into the pixel buffer area, so that the ARGB data before compression is correctly constructed and stored as an exemplary code in the ARGB format is as follows:
void PNGHander::restorePNGData()
{
unsigned long expandedLength = data->nbytes * sizeof(PixelData);
ChannelData *buffer = new ChannelData[expandedLength];
ChannelData *b = data->bytes;
PixelData *dest = (PixelData *)buffer;
for (ChannelData *p = b; p < b + data->nbytes; ++p, ++dest) {
*dest = makePixel(redChannel(data->color_sig), greenChannel(data->color_sig), blueChannel(data->color_sig), *p);// Only save the Alpha channel.
}
delete[] data->bytes;
data->bytes = buffer;
data->nbytes = expandedLength;
data->bytes_per_line *= sizeof(PixelData);// Only save alpha.
}
thus, the restoration of the compressed image is realized at the file layer. Thus, the file layer data restoration is to restore the compressed file to a file with the size consistent with the original file.
In order to better understand the method for restoring an image at the file layer according to the present invention, the whole process of the restoration method is described below with reference to fig. 8 by a specific example. In this example, the predetermined format is a Gray format (i.e., only when the image to be restored is a grayscale image, restoration can be performed), and the pure color data is RGB data, which is described below.
Firstly, reading a data file of an image to be restored, wherein the data file comprises a pixel format of the image to be restored. And then, judging whether the pixel format of the image to be restored is a Gray format, if so, indicating that the image to be restored is a Gray image, then detecting whether the preset position stores general RGB data, and if the preset position also stores general RGB data, judging that the image to be restored can be restored, namely, the image to be restored is a compressed image obtained by compressing by adopting the image compression method of the single color channel. Subsequently, the common RGB data is extracted from the predetermined position. Then, the pixel data in the data file of the image to be restored is expanded to 32 bits. And finally, filling ARGB data by using the extracted universal RGB data and the brightness data of each pixel stored in the data file of the restored image, and storing the ARGB data in an ARGB format. Exemplary code for this example is as follows:
bool PNGHander::restorePNG(const std::string &src_file_name, const std::string &out_file_name)
{
if (!readPNGData(src_file_name))
return false;
if (data->color_type != PNG_COLOR_TYPE_GRAY)
return false;// Not a grayscale image.
restorePNGData();
writePNGData(out_file_name, PNG_COLOR_TYPE_RGB_ALPHA, 0, 0);
return true;
}
in addition, the present invention also uses the above-mentioned image compression method of single color channel and image restoration method of file layer to compress and restore a pure color icon image in sequence, and the data of the compressed icon image and the restored icon image are as the following table 2:
size of pixel File size Pixel format Whether to perform PNG compression Whether or not to support previews
Original icon image 225*200 2755B ARGB Is that Is that
Compressed image 225*200 1857B Gray Is that Is that
Restored image 225*200 2755B ARGB Is that Is that
TABLE 2
A method for restoring a compressed image in an application rendering layer will be described below. And the application rendering layer restores the image, namely the image is rendered in the software layer, and the compatible compressed file directly renders the image to an application program interface required to be displayed. Specifically, in the present embodiment, each pixel may be rendered into an interface of an application program that opens a compressed image by using a rendering system according to the configured transparency data of the transparency channel and color data of each color channel of each pixel. The drawing system can be an application drawing system provided by Qt. That is, when a certain application needs to use a compressed image, each pixel is rendered into the interface of the application through an image composition mode provided in the Qt rendering system.
Here, image composition (image composition) will be described below. Image synthesis is a technique for combining pixels in a source image with pixels in a target image to form a final image. The image synthesis mode used in this embodiment is a Source image overlapping mode (Source in), and when there is an overlapping region between the Source image and the target image, the final image only stores the overlapping region of the Source image. Although the final image size area of the Source image coincidence mode is consistent with that of the target image coincidence mode (Destination in), when the image has an Alpha channel, the Alpha channel is different from the Source image coincidence mode, the Alpha channel of Source in is changed along with the Alpha channel of the target image (namely the Source in mode can keep the Alpha channel of the Source image and combine the target color into the Source image), and the Alpha channel of Destination in is changed along with the Alpha channel of the Source image.
Taking the compressed image as PNG format and the pixel format as Gray format as an example, when software is used for rendering, only the read Gray format needs to be converted into Alpha8 format, however, since the internal pixel format, size and data stored in the compressed image are all consistent, the compressed image can be directly replaced by source file data, and the compressed image is rendered into an interface of an application program through a QImage object after the conversion is successful. Exemplary code for this example is as follows:
QImage layer(restoreFileName);
QImage restoreImage(layer.bits(), layer.width(), layer.width(), layer.bytesPerLine(), QImage::Format_Alpha8);
QImage outImage(layer.width(), layer.height(), QImage::Format_ARGB32);
outImage.fill(Qt::transparent);
QPainter render;
render.begin(&outImage);
render.drawImage(outImage.rect(), restoreImage);
render.setCompositionMode(QPainter::CompositionMode_SourceIn);
render.fillRect(restoreImage.rect(), commonRGBColor);
render.end();
it should be noted that the above is only an example, the present invention is not limited to the image in the PNG format, and the Qt component provides compatibility for most image formats, so the present invention supports most image formats. Exemplary code for Qt-based multi-format generic compression recovery is as follows:
// Qt-based compression
static bool convertImageTo(const QString &originPath, const QString &targetPath)
{
QImage originImage(originPath);
if (originImage.isNull())
return false;
const auto fi = QFileInfo(originPath);
const QString &suffix = fi.suffix();
if ((originImage.width() % 2) || (originImage.height() % 2))
return false;
auto tmp = originImage.convertToFormat(QImage::Format_Alpha8);
QImage alpha(tmp.constBits(), tmp.width(), tmp.height(), tmp.bytesPerLine(), QImage::Format_Grayscale8);
return alpha.save(targetPath, suffix.toLocal8Bit());
}
// Qt-based recovery
static bool convertImageFrom(const QString &originPath, const QString &targetPath) {
QString path(originPath);
auto fi = QFileInfo(originPath);
QString suffix = fi.suffix();
if (suffix.compare(ALPHA8STRING, Qt::CaseInsensitive))
return false;// No convert to alpha8.
path.remove(QLatin1Char('.') + fi.suffix());
QImage originImage(originPath);
if (originPath.isNull() || originImage.format() != QImage::Format_Grayscale8)
return false;
suffix = QFileInfo(path).suffix();
QImage targetImage(originImage.bits(), originImage.width(), originImage.height(),
originImage.bytesPerLine(), QImage::Format_Alpha8);
return targetImage.save(targetPath, suffix.toLocal8Bit());
}
Thus, the restoration of the compressed image is realized at the software layer. In addition, the present invention also uses the above-mentioned image compression method of single color channel and the image restoration method of software layer to successively compress and restore the pure color icon images with different sizes and formats, and the size ratio of the compressed icon image and the icon image before compression is as follows 3:
size of pixel Image format Original file size Compressing file size
225*200 PNG 4451B 1878B
225*200 WEBP 2318B 1316B
563*500 PNG 17049B 5526B
563*500 WEBP 6172B 2992B
1125*1000 PNG 46905B 11754B
1125*1000 WEBP 12772B 6792B
2250*2000 PNG 137155B 25728B
2250*2000 WEBP 29226B 17164B
TABLE 3
It can be seen that, the compression effects of the images with different formats are different due to the data compression of the images, but the single-channel image compression method and the corresponding image restoration method based on the invention can finally reduce the size of the original image and realize lossless restoration.
Here, although the pixel format of the image to be restored is a predetermined format, a case where pure color data is not stored in the predetermined format will be described. In this case, when the file layer is restored, although the image to be restored is a single-channel image, the restoration cannot be performed because data padding cannot be performed because there is no solid-color data. However, this case is more specific in software rendering, and in this scenario, the application program may dynamically specify the color to be rendered, that is, the color (for example, RGB color) that the application program needs to customize is defined, and then the definition data is used to perform dynamic synthesis to achieve lossless restoration, so that the case can be restored at the software layer.
The scheme is very useful when an application program does not need to care about the color of a specific pixel point of a picture, for example, when the program needs to switch the foreground color of a specific icon in mouse clicking and non-clicking states, the scheme can perform foreground color rendering on the icon when restoring the file, and then select different colors to perform final image synthesis according to different states required by the program.
In order to better understand the image restoration process of the present invention, a specific example is given below with reference to fig. 9. In this example, the predetermined format is a grayscale format and the pure color data is RGB data, as follows.
Firstly, reading image data of an image to be restored, wherein the image data comprises a pixel format of the image to be restored. Then, whether the pixel format of the image to be restored is a gray scale format is detected. If not, the image to be restored is judged not to be the compressed image obtained by the single color channel image compression method, so that the image to be restored can not be restored. If so, it is then detected whether the predetermined position stores common RGB data. If the common RGB data are stored in the predetermined position, the image to be restored is judged to be a compressed image obtained by compression by using the single color channel image compression method, so that the image to be restored can be restored in a file layer or can be restored in a rendering mode in a software layer, and if the common RGB data are not stored in the predetermined position, whether the software layer is restored in the rendering mode is judged. And if the software layer is subjected to rendering restoration, restoring, otherwise, judging that the image to be restored cannot be restored.
For specific details of the embodiment, after determining that the image to be restored is recoverable, refer to the description based on fig. 7 and fig. 8, and are not repeated herein.
The invention provides an image compression method of a single color channel for a pure color image, and the method can greatly improve the compression rate of the image under the condition of not changing the format of the original image. Moreover, the invention also provides a corresponding image restoration method, which can carry out lossless restoration on the image compressed by the image compression method of the single color channel.
The various techniques described herein may be implemented in connection with hardware or software or, alternatively, with a combination of both. Thus, the methods and apparatus of the present invention, or certain aspects or portions thereof, may take the form of program code (i.e., instructions) embodied in tangible media, such as removable hard drives, U.S. disks, floppy disks, CD-ROMs, or any other machine-readable storage medium, wherein, when the program is loaded into and executed by a machine, such as a computer, the machine becomes an apparatus for practicing the invention.
In the case of program code execution on programmable computers, the computing device will generally include a processor, a storage medium readable by the processor (including volatile and non-volatile memory and/or storage elements), at least one input device, and at least one output device. Wherein the memory is configured to store program code; the processor is configured to execute the image compression method or the image restoration method of the present invention according to instructions in the program code stored in the memory.
By way of example, and not limitation, readable media may comprise readable storage media and communication media. Readable storage media store information such as computer readable instructions, data structures, program modules or other data. Communication media typically embodies computer readable instructions, data structures, program modules or other data in a modulated data signal such as a carrier wave or other transport mechanism and includes any information delivery media. Combinations of any of the above are also included within the scope of readable media.
In the description provided herein, algorithms and displays are not inherently related to any particular computer, virtual system, or other apparatus. Various general purpose systems may also be used with examples of this invention. The required structure for constructing such a system will be apparent from the description above. Moreover, the present invention is not directed to any particular programming language. It is appreciated that a variety of programming languages may be used to implement the teachings of the present invention as described herein, and any descriptions of specific languages are provided above to disclose the best mode of the invention.
In the description provided herein, numerous specific details are set forth. It is understood, however, that embodiments of the invention may be practiced without these specific details. In some instances, well-known methods, structures and techniques have not been shown in detail in order not to obscure an understanding of this description.
It should be appreciated that in the foregoing description of exemplary embodiments of the invention, various features of the invention are sometimes grouped together in a single embodiment, figure, or description thereof for the purpose of streamlining the disclosure and aiding in the understanding of one or more of the various inventive aspects. However, the disclosed method should not be interpreted as reflecting an intention that: that the invention as claimed requires more features than are expressly recited in each claim.
Those skilled in the art will appreciate that the modules or units or components of the devices in the examples disclosed herein may be arranged in a device as described in this embodiment, or alternatively may be located in one or more devices different from the device in this example. The modules in the foregoing examples may be combined into one module or may be further divided into multiple sub-modules.
Those skilled in the art will appreciate that the modules in the device in an embodiment may be adaptively changed and disposed in one or more devices different from the embodiment. The modules or units or components of the embodiments may be combined into one module or unit or component, and furthermore they may be divided into a plurality of sub-modules or sub-units or sub-components. All of the features disclosed in this specification (including any accompanying claims, abstract and drawings), and all of the processes or elements of any method or apparatus so disclosed, may be combined in any combination, except combinations where at least some of such features and/or processes or elements are mutually exclusive. Each feature disclosed in this specification (including any accompanying claims, abstract and drawings) may be replaced by alternative features serving the same, equivalent or similar purpose, unless expressly stated otherwise.
Moreover, those skilled in the art will appreciate that although some embodiments described herein include some features included in other embodiments, not others, combinations of features of different embodiments are meant to be within the scope of the invention and form different embodiments.
Furthermore, some of the described embodiments are described herein as a method or combination of method elements that can be performed by a processor of a computer system or by other means of performing the described functions. A processor having the necessary instructions for carrying out the method or method elements thus forms a means for carrying out the method or method elements. Further, the elements of the apparatus embodiments described herein are examples of the following apparatus: the apparatus is used to implement the functions performed by the elements for the purpose of carrying out the invention.
As used herein, unless otherwise specified the use of the ordinal adjectives "first", "second", "third", etc., to describe a common object, merely indicate that different instances of like objects are being referred to, and are not intended to imply that the objects so described must be in a given sequence, either temporally, spatially, in ranking, or in any other manner.
While the invention has been described with respect to a limited number of embodiments, those skilled in the art, having benefit of this description, will appreciate that other embodiments can be devised which do not depart from the scope of the invention as described herein. Furthermore, it should be noted that the language used in the specification has been principally selected for readability and instructional purposes, and may not have been selected to delineate or circumscribe the inventive subject matter. Accordingly, many modifications and variations will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the appended claims. The present invention has been disclosed in an illustrative rather than a restrictive sense, and the scope of the present invention is defined by the appended claims.

Claims (14)

1. An image compression method comprising:
acquiring a data file of an image to be compressed, wherein a pixel buffer area of the data file comprises transparency data of a transparency channel of each pixel and color data of a plurality of color channels;
detecting whether the image to be compressed is a pure color image or not based on the color data of a plurality of color channels of each pixel;
if so:
extracting the pure color data of the image to be compressed and storing the pure color data to a preset position;
converting the transparency data of each pixel to be compressed into color data of a single-channel image, wherein the pixel to be compressed is a pixel with transparency data not being 0, and the pure color data is color data of all color channels of the pixel to be compressed;
deleting the transparency data of each pixel and the color data of a plurality of color channels in the pixel buffer area, and reducing the size of the pixel buffer area to a single channel width;
and writing the color data converted from the transparency data of each pixel to be compressed into the corresponding pixel buffer area to obtain a compressed image.
2. The method of claim 1, wherein detecting whether the image to be compressed is a solid image comprises:
detecting whether the color data of the same color channel of all pixels are the same; or
And detecting whether the color data of the same color channel of all the pixels to be compressed are the same.
3. The method of claim 1 or 2, wherein converting the transparency data for each pixel to be compressed into color data for a single channel image comprises:
and taking the transparency data of each pixel to be compressed as the color data of the single-channel image.
4. The method of claim 1 or 2, wherein the single channel image is a grayscale image, and accordingly, the color data of the single channel image is the brightness of the grayscale image.
5. The method according to claim 1 or 2, wherein the predetermined location is a predetermined field in the data file, or a predetermined field in a file name of the data file, or an internal buffer of an application program that opens the compressed image.
6. The method of claim 1 or 2, wherein the image to be compressed is an icon image.
7. The method of claim 1 or 2, wherein the number of color channels includes a red channel, a green channel, and a blue channel.
8. A method of restoring a compressed image obtained by the method of claim 1, comprising:
constructing color data of each color channel of each pixel before compression of the compressed image based on pure color data stored in a predetermined position;
constructing transparency data of a transparency channel of each pixel before compression of the compressed image based on color data of a color channel of each pixel in a pixel buffer of a data file of the compressed image;
and restoring the compressed image based on the constructed transparency data of the transparency channel of each pixel and the color data of each color channel.
9. The method of claim 8, wherein restoring the compressed image comprises:
deleting the color data of each pixel in the pixel buffer area;
expanding the size of the pixel buffer area to the size before compression, and writing the constructed transparency data of the transparency channel of each pixel and the color data of each color channel into the pixel buffer area;
and deleting the pure color data stored in the preset position.
10. The method of claim 8, wherein restoring the compressed image comprises:
and rendering each pixel to an interface of an application program opening the compressed image by using a drawing system according to the constructed transparency data of the transparency channel of each pixel and the constructed color data of each color channel.
11. The method according to any of claims 8-10, wherein the predetermined location is a predetermined field in the data file, or a predetermined field in a file name of the data file, or an internal buffer of an application that opens the compressed image.
12. The method of any of claims 8-10, wherein each color channel of each pixel prior to compression of the compressed image comprises a red channel, a green channel, and a blue channel.
13. A computing device, comprising:
at least one processor; and
a memory storing program instructions, wherein the program instructions are configured to be adapted to be executed by the at least one processor, the program instructions comprising instructions for performing the method of any of claims 1-12.
14. A readable storage medium storing program instructions that, when read and executed by a computing device, cause the computing device to perform the method of any of claims 1-12.
CN202211487190.5A 2022-11-25 2022-11-25 Image compression method, restoration method, computing device and readable storage medium Active CN115550661B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211487190.5A CN115550661B (en) 2022-11-25 2022-11-25 Image compression method, restoration method, computing device and readable storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211487190.5A CN115550661B (en) 2022-11-25 2022-11-25 Image compression method, restoration method, computing device and readable storage medium

Publications (2)

Publication Number Publication Date
CN115550661A CN115550661A (en) 2022-12-30
CN115550661B true CN115550661B (en) 2023-03-24

Family

ID=84720471

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211487190.5A Active CN115550661B (en) 2022-11-25 2022-11-25 Image compression method, restoration method, computing device and readable storage medium

Country Status (1)

Country Link
CN (1) CN115550661B (en)

Family Cites Families (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US8457396B2 (en) * 2009-08-11 2013-06-04 Microsoft Corporation Digital image compression and decompression
CN103533352B (en) * 2013-09-27 2017-01-25 桂林力港网络科技有限公司 Compression method and device and decompression method and device for image with transparency
CN105938625B (en) * 2016-03-18 2018-10-12 浙江万朋教育科技股份有限公司 A method of alpha channel datas in processing PNG picture formats
CN108668169B (en) * 2018-06-01 2021-10-29 北京市商汤科技开发有限公司 Image information processing method and device, and storage medium
CN110619667B (en) * 2019-09-27 2023-03-21 恒信东方文化股份有限公司 PNG picture compression method and system
CN113436284A (en) * 2021-07-30 2021-09-24 上海商汤智能科技有限公司 Image processing method and device, computer equipment and storage medium

Also Published As

Publication number Publication date
CN115550661A (en) 2022-12-30

Similar Documents

Publication Publication Date Title
US8019166B2 (en) Image data compression method and apparatuses, image display method and apparatuses
US20190139265A1 (en) Data compression using entropy encoding
US7978922B2 (en) Compressing images in documents
JP6703032B2 (en) Backward compatibility extended image format
US20090010533A1 (en) Method and apparatus for displaying an encoded image
JPH0473667B2 (en)
CN111405294B (en) Storage and transmission method of image raster data
CN110189384B (en) Image compression method, device, computer equipment and storage medium based on Unity3D
US6501852B1 (en) Method and component for serialization of images
CN115550661B (en) Image compression method, restoration method, computing device and readable storage medium
JP2017211936A (en) Document storage processing system
US20090154802A1 (en) Method and apparatus for image compression and decompression
CN113934955A (en) Method for generating display PPT file by browser, browser and storage medium
US11263786B2 (en) Decoding data arrays
RU2331085C2 (en) Two-component integration of messages into image
CN115795066A (en) Method and related device for interconversion of iBootim image and PNG image
US20020081038A1 (en) Graphic image coding
US8494288B2 (en) Methods for digital image compression
CN108154542B (en) Method for adding semitransparent property to JPG file
US7468733B2 (en) Method and system for improving color reduction
US20230334028A1 (en) Data compression and decompression
CN116719483B (en) Data deduplication method, apparatus, storage device and computer readable storage medium
CN111737388B (en) Geological map data storage processing method
US20230325150A1 (en) Multiple-input floating-point number processing method and apparatus
CN111263155B (en) Compression method and system for equal-resolution CR image

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant