Disclosure of Invention
The technical problem to be solved by the invention is to provide a color format conversion method, a color format conversion device, an electronic device and a storage medium, aiming at overcoming the defects that in the prior art, the conversion from one format to another format is realized in an independent mode, if the conversion is carried out for multiple times, multiple corresponding methods are required to be realized, the repeatability and fragmentation of the realization mode are caused, the transplantation of the conversion method is not facilitated, the specified conversion method needs to be realized by a user every time, and the efficient program development is not facilitated.
The invention solves the technical problems through the following technical scheme:
the invention provides a color format conversion method, which comprises the following steps:
acquiring color data, an input color format of the color data, and an output color format specified for the color data;
calling a conversion process for realizing conversion from the input color format to the output color format according to the input color format and the output color format;
and converting the color data through the conversion process and then outputting the color data.
Preferably, when the input color format comprises an original sampling mode, an original storage format and an original combination mode of components of Y, U and V in the YUV format, and the output color format comprises an appointed sampling mode, an appointed storage format and an appointed combination mode of components of Y, U and V in the YUV format, the YUV internal conversion process is called;
the YUV internal conversion process comprises the following steps:
converting from the original sampling mode to the specified sampling mode;
a conversion from the original storage format to the specified storage format;
and converting the original combination mode into the specified combination mode.
Preferably, when the input color format includes the original arrangement of three channels R, G, B in RGB format and the original pixel bits of the components R, G, B, and the output color format includes the specified arrangement of three channels R, G, B in color data and the specified pixel bits of the components R, G, B, the RGB internal conversion process is invoked;
the RGB internal conversion process comprises the following steps:
converting from the original arrangement mode to the specified arrangement mode;
a conversion from the primary pixel bit number to the specified pixel bit number.
Preferably, the conversion from the primary pixel bit number to the designated pixel bit number comprises:
judging whether the conversion from high precision to low precision or the conversion from low precision to high precision is carried out by comparing the original pixel digit with the appointed pixel digit;
if the conversion from high precision to low precision is carried out, executing a quantization compression step, wherein the quantization compression step comprises filling R, G and B components of the converted color data with high-order data of the appointed pixel bit number of the R, G and B components in the color data before the conversion;
and if the color data is converted from low precision to high precision, executing a quantization compensation step, wherein the quantization compensation step comprises the steps of filling data of the components R, G and B in the color data before conversion into high bits of the components R, G and B in the color data after conversion, compensating by using the low bits of the data of the components R, G and B in the color data before conversion, and if the color data after conversion still has unfilled bits, continuing to use the low bits of the components R, G and B in the color data before conversion for cyclic compensation until the bits of the components R, G and B in the color data after conversion are filled.
Preferably, when the input color format includes an original sampling mode and an original storage format of a YUV format and an original combination mode of Y, U and V components, and the output color format includes an assigned combination mode of R, G and B channels and an assigned pixel bit number of R, G and B components of an RGB format, a conversion process from YUV to RGB is invoked;
the process of converting YUV to RGB comprises the following steps:
converting the original sampling mode, the original storage format and the original combination mode into a YUV universal sampling mode, a universal storage format and a universal combination mode of Y, U and V components;
converting the YUV universal sampling mode, the universal storage format and the universal combination mode of the Y, U and V components into the universal combination mode of the R, G and B channels and the universal pixel bit number of the R, G and B components;
converting from the general arrangement of the R, G, B three channels and the general pixel bit number of the R, G, B components to the specific arrangement of the R, G, B three channels and the specific pixel bit number of the R, G, B components;
or, when the input color format comprises an original combination mode of R, G and B channels in an RGB format and an original pixel bit number of R, G and B components, and the output color format comprises a specified sampling mode, a specified storage format and a specified combination mode of Y, U and V components in a YUV format, calling a conversion process from RGB to YUV;
the conversion process from RGB to YUV comprises the following steps:
converting the original arrangement mode of the R, G and B channels and the original pixel digits of the R, G and B components into the universal arrangement mode of the R, G and B channels and the universal pixel digits of the R, G and B components;
converting the general arrangement mode of the R, G and B channels and the general pixel bit number of the R, G and B components into a YUV general sampling mode, a general storage format and a general combination mode of the Y, U and V components;
and converting the YUV universal sampling mode, the universal storage format and the universal combination mode of the Y, U and V components into the specified combination mode of the R, G and B channels and the specified pixel bit number of the R, G and B components.
The present invention provides a color format conversion apparatus, comprising:
an obtaining module for obtaining color data, an input color format of the color data, and an output color format specified for the color data;
and the conversion module is used for converting the color data from the input color format to the output color format and outputting the converted color data according to the input color format and the output color format.
Preferably, the conversion module comprises a YUV internal conversion module;
when the input color format comprises an original sampling mode, an original storage format and an original combination mode of Y, U and V components of a YUV format, and the output color format comprises an appointed sampling mode, an appointed storage format and an appointed combination mode of Y, U and V components of the YUV format, the YUV internal conversion module is used for realizing:
converting from the original sampling mode to the specified sampling mode;
a conversion from the original storage format to the specified storage format;
and converting the original combination mode into the specified combination mode.
Preferably, the conversion module comprises an RGB internal conversion module;
when the input color format includes the original arrangement of three channels R, G, and B in RGB format and the original pixel bits of the components R, G, and B, and the output color format includes the specified arrangement of three channels R, G, and B in color data and the specified pixel bits of the components R, G, and B, the RGB internal conversion mode is used to implement:
converting from the original arrangement mode to the specified arrangement mode;
a conversion from the primary pixel bit number to the specified pixel bit number.
Preferably, the conversion from the primary pixel bit number to the designated pixel bit number comprises:
judging whether the conversion from high precision to low precision or the conversion from low precision to high precision is carried out by comparing the original pixel digit with the appointed pixel digit;
if conversion from high precision to low precision is performed, performing quantization compression, the quantization compression including filling R, G, B components of the converted color data with upper data of the specified number of pixel bits of the R, G, B components in the color data before conversion;
if the conversion is from low precision to high precision, executing a quantization compensation step, wherein the quantization compensation comprises the steps of filling data of R, G and B components in the color data before conversion into high bits of the R, G and B components in the color data after conversion, compensating by using the low bits of the data of the R, G and B components in the color data before conversion, and if the color data after conversion still has unfilled bits, continuing to use the low bits of the R, G and B components in the color data before conversion for cyclic compensation until the bits of the R, G and B components in the color data after conversion are filled.
Preferably, the conversion module comprises a YUV and RGB interconversion module;
when the input color format comprises an original sampling mode and an original storage format of a YUV format and an original combination mode of Y, U and V components, and the output color format comprises an appointed arrangement mode of R, G and B channels of an RGB format and an appointed pixel bit number of the R, G and B components, the YUV and RGB mutual conversion module is used for realizing:
converting the original sampling mode, the original storage format and the original combination mode into a YUV universal sampling mode, a universal storage format and a universal combination mode of Y, U and V components;
converting the YUV universal sampling mode, the universal storage format and the universal combination mode of the Y, U and V components into a universal arrangement mode of R, G and B channels and a universal pixel bit number of the R, G and B components;
converting from the general arrangement of the R, G, B three channels and the general pixel bit number of the R, G, B components to the specific arrangement of the R, G, B three channels and the specific pixel bit number of the R, G, B components;
or, when the input color format includes an original arrangement mode of R, G, B three channels in RGB format and an original pixel bit number of R, G, B components, and the output color format includes an appointed sampling mode in YUV format, an appointed storage format, and an appointed combination mode of Y, U, V components, the YUV and RGB interconversion module is configured to implement:
converting the original arrangement mode of the R, G and B channels and the original pixel digits of the R, G and B components into the universal arrangement mode of the R, G and B channels and the universal pixel digits of the R, G and B components;
converting the general arrangement mode of the R, G and B channels and the general pixel bit number of the R, G and B components into a YUV general sampling mode, a general storage format and a general combination mode of the Y, U and V components;
and converting the YUV universal sampling mode, the universal storage format and the universal combination mode of the Y, U and V components into the specified combination mode of the R, G and B channels and the specified pixel bit number of the R, G and B components.
The present invention provides a color format conversion apparatus, comprising: the device comprises an input unit, a central processing unit, an output unit, a memory unit and a storage unit;
the storage unit is used for storing the conversion flow;
the input unit is used for receiving the input of color data, an input color format and an output color format;
the central processing unit is used for calling a conversion flow in the storage unit to process the color data in the memory unit according to the input color format and the output color format, and outputting the processed color data through the output unit.
The invention provides an electronic device comprising a memory, a processor and a computer program stored on the memory and operable on the processor, wherein the processor implements the color format conversion method as described above when executing the program.
The present invention provides a computer-readable storage medium having stored thereon a computer program, characterized in that the program, when executed by a processor, implements the steps of the color format conversion method as described above.
On the basis of the common knowledge in the field, the above preferred conditions can be combined randomly to obtain the preferred embodiments of the invention.
The positive progress effects of the invention are as follows: by the invention, a user does not need to realize a specific conversion mode for each format conversion, and only needs to specify the input format, the output format and the input data to obtain the data of the specified output format. The problems of poor reusability and fragmentation of YUV and RGB format conversion are reduced, and efficient program development is facilitated for users.
Detailed Description
The invention is further illustrated by the following examples, which are not intended to limit the scope of the invention.
Example 1
Fig. 1 shows a color format conversion method of the present embodiment. It includes:
step 11: color data, an input color format of the color data, and an output color format specified for the color data are acquired.
Step 12: and calling a conversion flow for realizing conversion from the input color format to the output color format according to the input color format and the output color format.
Step 13: and converting the color data through a conversion process and then outputting the color data.
The above-described transformations can be roughly classified into three categories: YUV to YUV conversion, RGB to RGB conversion, and YUV and RGB interconversion.
The conversion from YUV to YUV can be realized by YUV internal conversion flow. Specifically, when the input color format comprises an original sampling format and an original storage format of a YUV format and an original combination mode of Y, U and V components, and the output color format comprises a specified sampling format and a specified storage format of the YUV format and a specified combination mode of Y, U and V components, a YUV internal conversion process is called.
As shown in fig. 2, the YUV internal conversion process includes:
conversion from an original sampling format to a specified sampling format, for example, conversion between YUV420, YUV422 and YUV444 different sampling formats, which may be referred to as one-stage conversion;
the conversion from the original storage format to the specified storage format, for example, the conversion between different storage formats of Planar, Semi-Planar and Packed, may be referred to as a secondary conversion;
the conversion from the original combination mode to the specified combination mode, for example, the conversion between YUVUV and YUVUV, may be referred to as three-level conversion.
And after the first-stage conversion, the second-stage conversion and the third-stage conversion are completed, obtaining color data in an output color format and outputting the color data.
For the conversion from RGB to RGB, the conversion can be realized by an RGB internal conversion flow. Specifically, when the input color format includes the original arrangement of three channels of R, G, and B in RGB format and the original pixel bit number of the components of R, G, and B, and the output color format includes the designated arrangement of three channels of R, G, and B in color data and the designated pixel bit number of the components of R, G, and B, the RGB internal conversion process is invoked.
As shown in fig. 3, the RGB internal conversion process includes:
the conversion from the original arrangement to the specified arrangement, for example, the conversion between RGB and BGR, may be referred to as channel conversion;
the conversion from the original pixel bit number to the designated pixel bit number, for example, the conversion between 888 and 444, may be referred to as precision conversion, where the conversion from the original pixel bit number to the designated pixel bit number may specifically include:
judging whether the conversion from high precision to low precision or the conversion from low precision to high precision is carried out by comparing the original pixel digit with the designated pixel digit;
if the conversion from high precision to low precision is carried out, executing a quantization compression step, wherein the quantization compression step comprises filling R, G and B components of the converted color data with high-order data of specified pixel digits of the R, G and B components in the color data before the conversion;
if the conversion is from low precision to high precision, a quantization compensation step is executed, wherein the quantization compensation step comprises the steps of filling data of R, G and B components in the color data before conversion into high bits of the R, G and B components in the color data after conversion, compensating by using the low bits of the data of the R, G and B components in the color data before conversion, and if the color data after conversion still has unfilled bits, continuously performing cyclic compensation by using the low bits of the R, G and B components in the color data before conversion until the bits of the R, G and B components in the color data after conversion are filled.
And after the channel conversion and the precision conversion are completed, obtaining color data in an output color format and outputting the color data.
The mutual conversion between YUV and RGB can be realized through an internal conversion process of RGB. Specifically, the conversion from YUV to RGB and the conversion from RGB to YUV may be included.
As shown in fig. 4, the conversion from YUV to RGB specifically includes: when the input color format comprises an original sampling format and an original storage format of a YUV format and an original combination mode of Y, U and V components, and the output color format comprises an appointed combination mode of R, G and B channels of an RGB format and an appointed pixel bit number of the R, G and B components, the conversion process from YUV to RGB is called.
The conversion process from YUV to RGB comprises the following steps:
converting from an original sampling format, an original storage format and an original combination mode to a YUV universal sampling format, a universal storage format and a universal combination mode of Y, U and V components;
converting the universal combination mode of the components of YUV universal sampling format, universal storage format and Y, U and V to the universal combination mode of three channels of R, G and B and the universal pixel bit number of the components of R, G and B;
the conversion from the general arrangement of the three channels R, G, B and the general pixel bit number of the components R, G, B to the specific arrangement of the three channels R, G, B and the specific pixel bit number of the components R, G, B.
The process converts the original YUV format into the universal YUV format, converts the universal YUV format into the universal RGB format, and converts the universal RGB format into the specified RGB format, so that the conversion from any one YUV format to any one RGB format is realized, and the color data in the output color format is obtained and output.
The conversion from RGB to YUV specifically includes: when the input color format comprises an original combination mode of R, G and B channels and an original pixel bit number of R, G and B components in an RGB format, and the output color format comprises a specified sampling format, a specified storage format and a specified combination mode of Y, U and V components in a YUV format, calling a conversion process from RGB to YUV;
the conversion process from RGB to YUV comprises the following steps:
converting from the original arrangement mode of the R, G and B channels and the original pixel digits of the R, G and B components to the universal arrangement mode of the R, G and B channels and the universal pixel digits of the R, G and B components;
converting the general arrangement mode of the three channels R, G and B and the general pixel bit number of the components R, G and B into a YUV general sampling format, a general storage format and a general combination mode of the components Y, U and V;
and converting the YUV universal sampling format, the universal storage format and the universal combination mode of the Y, U and V components into the specified combination mode of the R, G and B channels and the specified pixel bit number of the R, G and B components.
The process converts the original RGB format into the universal RGB format, converts the universal RGB format into the universal YUV format and converts the universal YUV format into the specified YUV format, so that the conversion from any one RGB format to any one YUV format is realized, and the color data in the output color format is obtained and output.
The conversion between the universal YUV format (including YUV universal sampling format, universal storage format, and universal combination mode of Y, U, and V components) and the universal RGB format (including universal arrangement mode of three channels R, G, and B and universal pixel bit number of components R, G, and B) can be performed by the following method: conversion matrix, table look-up method and conversion method based on assembly language. It should be noted that, if the format conversion between YUV and RGB is performed in other manners based on the content of the present invention, the content of the present invention is not affected, and the scope of the present invention is considered to be the protection scope of the present invention.
The embodiment aims at the problems of poor reusability and fragmentation of YUV and RGB mutual conversion and internal conversion, and realizes a color format conversion method for facilitating the transplantation of the format conversion method and improving the program development efficiency of developers, and by the method, a user does not need to realize a specific conversion mode for each format conversion, only needs to specify an input color format, an output color format and input color data, and can obtain data of the specified output color format, thereby achieving the following beneficial effects:
1. the automatic conversion from the input color format to the output color format is realized, and no manual intervention is needed.
2. The method not only can convert YUV and RGB, but also can convert YUV internally and convert RGB internally.
3. And (3) providing a YUV internal conversion process: and three-stage conversion, namely obtaining the data in the specified output format through the three-stage conversion.
4. And module multiplexing is realized, and the conversion between YUV and RGB multiplexes YUV internal conversion and RGB internal conversion.
Example 2
Fig. 5 shows a color format conversion apparatus of the present embodiment. It includes: an acquisition module 21 and a conversion module 22.
The acquisition module 21 is configured to acquire color data, an input color format of the color data, and an output color format specified for the color data.
The conversion module 22 is configured to perform conversion of the color data from the input color format to the output color format according to the input color format and the output color format, and then output the converted color data.
The conversion module 22 may include a YUV internal conversion module 221.
When the input color format includes an original sampling format and an original storage format of the YUV format and an original combination mode of the Y, U, and V components, and the output color format includes an appointed sampling format and an appointed storage format of the YUV format and an appointed combination mode of the Y, U, and V components, the YUV internal conversion module 221 is configured to implement:
converting from an original sampling format to a specified sampling format;
converting from an original storage format to a specified storage format;
and (4) converting the original combination mode into the specified combination mode.
The conversion module 22 may also include an RGB internal conversion module 222.
When the input color format includes the original arrangement of three channels R, G, and B in RGB format and the original pixel bit number of the components R, G, and B, and the output color format includes the designated arrangement of three channels R, G, and B in color data and the designated pixel bit number of the components R, G, and B, the RGB internal conversion mode 222 is used to implement:
converting from an original arrangement mode to a specified arrangement mode;
conversion from the original pixel bit number to the specified pixel bit number.
The conversion from the original pixel bit number to the designated pixel bit number may specifically include:
judging whether the conversion from high precision to low precision or the conversion from low precision to high precision is carried out by comparing the original pixel digit with the designated pixel digit;
if the conversion from high precision to low precision is carried out, carrying out quantization compression, wherein the quantization compression comprises filling R, G and B components of the converted color data with high-order data of specified pixel digits of the R, G and B components in the color data before conversion;
if the conversion is from low precision to high precision, executing a quantization compensation step, wherein the quantization compensation comprises the steps of filling data of R, G and B components in the color data before conversion into high bits of the R, G and B components in the color data after conversion, compensating by using the low bits of the data of the R, G and B components in the color data before conversion, and if the color data after conversion still has unfilled bits, continuously performing cyclic compensation by using the low bits of the R, G and B components in the color data before conversion until the bits of the R, G and B components in the color data after conversion are filled.
The conversion module 22 may further include a YUV and RGB interconversion module 223.
When the input color format includes an original sampling format and an original storage format of a YUV format and an original combination mode of Y, U, and V components, and the output color format includes an assigned arrangement mode of three channels of R, G, and B and an assigned pixel bit number of the R, G, and B components of an RGB format, the YUV and RGB interconversion module 223 is configured to implement:
converting from an original sampling format, an original storage format and an original combination mode to a YUV universal sampling format, a universal storage format and a universal combination mode of Y, U and V components;
converting the universal arrangement mode of R, G and B channels and the universal pixel digit of R, G and B components from a YUV universal sampling format, a universal storage format and a universal combination mode of Y, U and V components;
the conversion from the general arrangement of the three channels R, G, B and the general pixel bit number of the components R, G, B to the specific arrangement of the three channels R, G, B and the specific pixel bit number of the components R, G, B.
Or, when the input color format includes the original arrangement mode of R, G, B three channels and the original pixel bit number of R, G, B components in RGB format, and the output color format includes the designated sampling format, the designated storage format, and the designated combination mode of Y, U, V components in YUV format, the YUV and RGB interconversion module 223 is configured to implement:
converting from the original arrangement mode of the R, G and B channels and the original pixel digits of the R, G and B components to the universal arrangement mode of the R, G and B channels and the universal pixel digits of the R, G and B components;
converting the general arrangement mode of the three channels R, G and B and the general pixel bit number of the components R, G and B into a YUV general sampling format, a general storage format and a general combination mode of the components Y, U and V;
and converting the YUV universal sampling format, the universal storage format and the universal combination mode of the Y, U and V components into the specified combination mode of the R, G and B channels and the specified pixel bit number of the R, G and B components.
Example 3
Fig. 6 shows a color format conversion apparatus of the present embodiment. It includes: an input Unit 31, a Central Processing Unit (CPU) 32, an output Unit 33, a memory Unit 34, and a storage Unit 35.
The storage unit 35 is used to store the conversion flow in embodiment 1.
The input unit 31 is used for receiving input of color data, an input color format and an output color format;
the central processing unit 32 is configured to invoke a conversion process in the storage unit 35 to process the color data in the memory unit 34 according to the input color format and the output color format, and output the processed color data through the output unit 35.
The implementation platform of the method and the device in the embodiment comprises a CPU based on X86, ARM, MIPIS and Powpc architectures, and the implementation system comprises an operating system based on Android, iOS, windows and Linux. It should be noted that, if the method and apparatus implemented by using other hardware systems and software systems based on the content of the present invention do not affect the essence of the present invention, they are all considered to be the protection scope of the present invention.
Example 4
Fig. 7 is a schematic structural diagram of an electronic device according to embodiment 4 of the present invention. The electronic device comprises a memory, a processor and a computer program stored on the memory and executable on the processor, the processor implementing the color format conversion method of embodiment 1 when executing the program. The electronic device 40 shown in fig. 7 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiment of the present invention.
As shown in fig. 7, the electronic device 40 may be embodied in the form of a general purpose computing device, which may be, for example, a server device. The components of electronic device 40 may include, but are not limited to: the at least one processor 41, the at least one memory 42, and a bus 43 connecting the various system components (including the memory 42 and the processor 41).
The bus 43 includes a data bus, an address bus, and a control bus.
The memory 42 may include volatile memory, such as Random Access Memory (RAM)421 and/or cache memory 422, and may further include Read Only Memory (ROM) 423.
Memory 42 may also include a program/utility 425 having a set (at least one) of program modules 424, such program modules 424 including, but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which, or some combination thereof, may comprise an implementation of a network environment.
The processor 41 executes various functional applications and data processing, such as a color format conversion method provided in embodiment 1 of the present invention, by running a computer program stored in the memory 42.
The electronic device 40 may also communicate with one or more external devices 44 (e.g., keyboard, pointing device, etc.). Such communication may be through an input/output (I/O) interface 45. Also, model-generating device 40 may also communicate with one or more networks (e.g., a Local Area Network (LAN), a Wide Area Network (WAN), and/or a public network, such as the Internet) via network adapter 46. As shown in FIG. 7, the network adapter 46 communicates with the other modules of the model-generated device 40 via the bus 43. It should be understood that although not shown in the figures, other hardware and/or software modules may be used in conjunction with the model-generating device 40, including but not limited to: microcode, device drivers, redundant processors, external disk drive arrays, RAID (disk array) systems, tape drives, and data backup storage systems, etc.
It should be noted that although in the above detailed description several units/modules or sub-units/modules of the electronic device are mentioned, such a division is merely exemplary and not mandatory. Indeed, the features and functionality of two or more of the units/modules described above may be embodied in one unit/module according to embodiments of the invention. Conversely, the features and functions of one unit/module described above may be further divided into embodiments by a plurality of units/modules.
Example 5
The present embodiment provides a computer-readable storage medium on which a computer program is stored, which when executed by a processor implements the steps of any one of the color format conversion methods provided in embodiment 1.
More specific examples, among others, that the readable storage medium may employ may include, but are not limited to: a portable disk, a hard disk, random access memory, read only memory, erasable programmable read only memory, optical storage device, magnetic storage device, or any suitable combination of the foregoing.
In a possible implementation, the present invention can also be implemented in the form of a program product comprising program code for causing a terminal device to perform the steps of implementing the color format conversion method described in embodiment 1 when the program product is run on the terminal device.
Where program code for carrying out the invention is written in any combination of one or more programming languages, the program code may execute entirely on the user device, partly on the user device, fill a separate software package, partly on the user device and partly on a remote device or entirely on the remote device.
While specific embodiments of the invention have been described above, it will be appreciated by those skilled in the art that these are by way of example only, and that the scope of the invention is defined by the appended claims. Various changes and modifications to these embodiments may be made by those skilled in the art without departing from the spirit and scope of the invention, and these changes and modifications are within the scope of the invention.