C ++ Image Processing-Image Synthesis

Source: Internet
Author: User

Reading Tips:

The C ++ image processing series focuses on code clarity and readability, all using C ++ code.

Delphi Image ProcessingThe series focuses on efficiency. The general code is Pascal, and the core code is BaSm.

Make sure that the two items are consistent and can be compared with each other.

The code in this article must include "C ++ Image Processing-data types and common functions"The header file of BMP data. h in this article.

 

During image processing, image synthesis is the most frequently used, such as display, image copy, image stitching, and layer combination and superposition.

Image synthesis is actually a mixture of pixel colors. In Photoshop, color mixing is a very complicated thing. different blending modes will produce different synthesis effects, if you thoroughly study all of this, you may have to write a book. Therefore, this article only talks about the most basic image synthesis, that is, the normal mixing mode in Photoshop.

As long as you have been involved in image processing, we all know that there is an image pixel mixing formula:

1) dstrgb = srcrgb * Alpha + dstrgb * (1-alpha)

Dstrgb indicates the pixel value of the target image, srcrgb indicates the pixel value of the source image, and Alpha indicates the blending ratio of the pixel value of the source image (opacity, range: 0-1 ).

In fact, this pixel mixing formula has great limitations and is only suitable for images without Alpha information.

To process a mix of images (layers) with Alpha channels, the complete formula should be:

2-1) srcrgb = srcrgb * srcalpha * alpha/255 (source image pre-multiplication to pargb)

2-2) dstrgb = dstrgb * dstalpha/255 (pre-multiplication of target image pixels to pargb)

2-3) dstrgb = dstrgb + srcrgb-dstrgb * srcalpha * alpha/255 (the source image pixel value is mixed with the target image pixel value)

2-4) dstalpha = dstalpha + srcalpha * alpha-dstalpha * srcalpha * alpha/255 (the alpha channel value of the target image after mixing)

2-5) dstrgb = dstrgb * 255/dstalpha (after blending, the target image pixel is converted to argb)

Dstrgb indicates the pixel value of the target image, srcrgb indicates the pixel value of the source image, dstalpha indicates the alpha channel value of the target image, srcalpha indicates the alpha channel value of the source image, and dstargb indicates the pixel value of the alpha target image; alpha is the blending ratio of the source image pixel values (opacity, range: 0-1 ).

2-1 in formula 2 is substituted into the 2-3 formula to simplify the process:

3-1) dstrgb = dstrgb * dstalpha/255

3-2) dstrgb = dstrgb + (srcrgb-dstrgb) * srcalpha * alpha/255

3-3) dstalpha = dstalpha + srcalpha * alpha-dstalpha * srcalpha * alpha/255

3-4) dstrgb = dstrgb * 255/dstalpha

When dstalpha = srcalpha = 255, the 3-1, 3-3, and 3-4 types in Formula 3 are meaningless, and the 3-2 type also changes:

4) dstrgb = dstrgb + (srcrgb-dstrgb) * alpha

It is not hard to see that formula 4 is the deformation of Formula 1. Therefore, Formula 1 is only a special case when Formula 3 (or formula 2) does not contain Alpha information (or alpha = 255) in the target image and source image.

When alpha = 1 in formula 4, the target image pixel is equal to the source image pixel. Therefore, image copying is also a field of image synthesis.

The above detailed analysis shows that even the most basic normal image mixing mode is complex. In fact, the above is not a complete analysis, because it is a complete arrangement and combination of three elements, namely the target image Alpha information, the source image Alpha information, and the source image synthesis ratio, A maximum of 8 formulas can be derived.

The following describes the code implementation in all eight cases of the normal hybrid mode (two items overlap, but the actual situation is 7), and the above text description can also be improved and supplemented:

// Configure forceinlinestatic void argbmixer (pargbquad PD, const pargbquad ps, int alpha) {Pd-> blue + = (PS-> blue-Pd-> blue) * Alpha + 127)/255); Pd-> green + = (PS-> green-Pd-> green) * Alpha + 127)/255 ); pd-> Red + = (PS-> Red-Pd-> Red) * Alpha + 127)/255 );}//--------------------------------------------------------------------- ------ Forceinlinestatic void pargbmixer (pargbquad PD, const pargbquad ps, int alpha) {Pd-> Blue = (Pd-> blue * Pd-> alpha + 127)/255; pd-> Green = (Pd-> green * Pd-> alpha + 127)/255; Pd-> Red = (Pd-> red * Pd-> alpha + 127) /255; Pd-> blue + = (PS-> blue-Pd-> blue) * Alpha + 127)/255 ); pd-> green + = (PS-> green-Pd-> green) * Alpha + 127)/255 ); pd-> Red + = (PS-> Red-Pd-> Red) * Alpha + 127)/255); Pd-> Alpha + = (alpha-(Pd-> alpha * Alpha + 127)/255); Pd-> Blue = Pd-> blue * 255/Pd-> alpha; pd-> Green = Pd-> green * 255/Pd-> alpha; Pd-> Red = Pd-> red * 255/Pd-> alpha ;} // outputs // source alpha = false, DEST alpha = false, Alpha <255 static void mixer0 (bitmapdata * DEST, const bitmapdata * Source, int alpha) {pargbquad PD, PS; uint widt H, height; int dstoffset, srcoffset; getdatacopyparams (DEST, source, width, height, PD, PS, dstoffset, srcoffset); For (uint y = 0; y 

The imagemixer function has three parameters: the target image data structure (borrow the bitmapdata structure of GDI +) pointer, the source Image Data Structure pointer, and the source image Pixel blending ratio (opacity, value Range: 0-1 ). The proc array in the function body includes all the eight cases of image mixing, the index is based on the mix ratio, the Alpha information of the target image, and the Alpha information of the source image. The sub-function calls the lower value (the Alpha information is in the reserved field of the bitmapdata structure ).

Of course, in actual use, all eight cases seem to be a little more important. You can choose to merge them as needed to take into account the complexity and execution efficiency of the Code. Below are some simple imagemixer functions that I think are reasonable:

// Image synthesis. Parameter: 32-bit target data, 32-bit source data, opacity (0-1.0) void imagemixer (bitmapdata * DEST, const bitmapdata * Source, float alpha) {int alphai = (INT) (alpha * 255); If (alphai <= 0) return; If (alphai> 255) alphai = 255; If (alphai = 255 &&! Hasalphaflag (source) mixer1 (DEST, source, alphai); // copy and synthesize else if (hasalphaflag (DEST) mixer6 (DEST, source, alphai ); // pargb synthesis elsemixer4 (DEST, source, alphai); // argb synthesis }//---------------------------------------------------------------------------

This imagemixer function retains only three sub-functions. Among them, mixer6 is a completely normal mixed mode, that is, the implementation of the preceding Formula 3; mixer4 is a mixture of target Graphs without Alpha information, that is, it is slightly expanded based on formula 4, while mixer1 is the copy mode.

The following is an example of calling the imagemixer function using bcb2010 and GDI +:

//---------------------------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){Gdiplus::Bitmap *dest =  new Gdiplus::Bitmap(L"d:\\xmas_011.png");Gdiplus::Bitmap *source =  new Gdiplus::Bitmap(L"d:\\Apple.png");Gdiplus::Graphics *g = new Gdiplus::Graphics(Canvas->Handle);g->DrawImage(dest, 0, 0);g->DrawImage(source, dest->GetWidth(), 0);BitmapData dst, src;LockBitmap(dest, &dst);LockBitmap(source, &src);ImageMixer(&dst, &src, 0.75);UnlockBitmap(source, &src);UnlockBitmap(dest, &dst);g->DrawImage(dest, dest->GetWidth() << 1, 0);delete g;delete source;delete dest;}//---------------------------------------------------------------------------

The following figure shows the running effect:

On the left is the target image, in the middle is the source image, and on the right is the normal mix of the source image by opacity 0.75.

 

Due to limited levels, errors are inevitable. Correction and guidance are welcome. Email Address:Maozefa@hotmail.com

Here, you can access "C ++ Image Processing-Article Index".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.