C ++ Image Processing-image color mixing (medium)

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.

 

After the article "C ++ Image Processing-image color mixing (I)" was published, it was well received by many friends, especially those who love to study Graphic programming. Several other friends sent me an email, in which they said that although the principles and formulas of the Photoshop layer mixed mode can be found online, they are mostly conceptual, there are few specific implementation code, especially the implementation code with high accuracy. Another friend asked if other graphic data can be mixed with the code in the article without using GDI + (this may be a beginner ).

This article makes some optimizations based on "C ++ Image Processing-image color mixing (I)", such as the colormix function, mainly to clarify the principle of the Color Mixing Mode in detail, this article will rewrite this function. In addition, the speed of the dyeing function is also optimized. To avoid searching for some data types and functions in "C ++ Image Processing-image color mixing (I)", we will copy all the unmodified code here, of course, to understand the principle of the color mixing mode or understand the calculation formula, you must go to "C ++ Image Processing-image color mixing (I)":

// Define typedef floatbwparams, * pbwparams; // adjust the default parameters in black/white mode: red, yellow, green, foreign red, blue, and blue const int _ bwdefault [] = {410,614,410,819,205,614 }; enum {bwindexblue = 0x40000, bwindexgreen = 0x20000, bwindexred = 0x00000}; Enum {indexblue = 0x00000, indexgreen = 0x10000, indexred = 0x20000}; typedef Union // color component switching Structure {int TMP; // Temporary Variable struct {short value used for switching; // color component value short Index; // color component index };} rgbindex; // transpose // The forceinlinevoid swaprgb (rgbindex & A, rgbindex & B) {. TMP ^ = B. TMP; B. TMP ^ =. TMP;. TMP ^ = B. TMP;} // obtain the grayscale forceinlineintgetbwgray (const pargbquad pixel, const pint bwparams) {rgbindex Max, mid, min; Min. TMP = pix El-> Blue | bwindexblue; mid. TMP = pixel-> green | bwindexgreen; max. TMP = pixel-> Red | bwindexred; If (max. value <mid. value) swaprgb (max, mid); If (max. value <min. value) swaprgb (max, min); If (min. value> mid. value) swaprgb (Min, mid); Return (max. value-mid. value) * bwparams [Max. index] + (MID. value-min. value) * bwparams [Max. index + mid. index-1] + 512)> 10) + min. value ;}//--------------------------- ------------------------------------------------ Void colormix (pargbquad PD, const pargbquad ps, int gray) {// gray computing constant: blue, green, red const int ys [3] = {113,604,307}; rgbindex Max, mid, min; Min. TMP = ps-> Blue | indexblue; mid. TMP = ps-> green | indexgreen; max. TMP = ps-> Red | indexred; If (max. value <mid. value) swaprgb (max, mid); If (max. value <min. value) swaprgb (max, min); If (min. value> mid. value) swaprgb (Min, mi D); int max_min = max. value-min. value; // If the saturation is 0, return the grayscale if (max_min = 0) {Pd-> Blue = Pd-> Green = Pd-> Red = gray; return ;} int mid_min = mid. value-min. value; int newmax, newmid, newmin; gray <= 10; newmax = (gray + (max_min-mid_min) * ys [mid. index] + max_min * ys [Min. index] + 512)> 10; newmin = newmax-max_min; If (newmax> 255) {int huecoef = (mid_min <10)/max_min; int V0 = (YS [mid. index] * huecoef )> 10; int V1 = ys [Min. index] + ys [mid. index]-V0; newmin = (Gray-(YS [Max. index] + V0) * 255 + (V1> 1)/V1; newmid = newmin + (255 ^ newmin) * huecoef + 512)> 10 ); newmax = 255;} else if (newmin <0) {int huecoef = (mid_min <10)/max_min; int TMP = ys [Max. index] + (YS [mid. index] * huecoef + 512)> 10); newmax = (gray + (TMP> 1)/tmp; newmid = (newmax * huecoef + 512)> 10; newmin = 1;} Elsenewmid = newmin + mid_min; (lpbyte) PD) [Max. index] = newmax; (lpbyte) PD) [mid. index] = newmid; (lpbyte) PD) [Min. index] = newmin;} // ----------------------------------------------------------------------------- // adjust the image in black and white. // Adjust the bwparams parameter to be an array pointer with the element number equal to 6, which is respectively red, yellow, green, blue, and foreign red void imageblackwhite (bitmapdata * data, const pbwparams bwparams = NULL) {// copy the pixel grayscale parameter and exchange the blue and foreign red int Params [6], * pparams; If (bwparams) {for (INT I = 0; I <6; I ++) Params [I] = (INT) (bwparams [I] * 1024 + 0.5); Params [3] ^ = Params [5]; params [5] ^ = Params [3]; Params [3] ^ = Params [5]; pparams = Params;} elsepparams = (int *) _ bwdefault; pargbquad P = (pargbquad) Data-> scan0; Int dataoffset = (data-> stride> 2)-(INT) Data-> width; For (uint y = 0; y <data-> height; y ++, P + = dataoffset) {for (uint x = 0; x <data-> width; X ++, P ++) {int gray = getbwgray (p, pparams ); p-> Blue = p-> Green = p-> Red = (Gray &~ 0xff) = 0? Gray: Gray> 255? 255: 0 ;}}// invalid void imagetint (bitmapdata * graydata, argb color) {argbquad colortable [256]; pargbquad P = colortable; For (INT I = 0; I <256; I ++, P ++) {colormix (p, (pargbquad) & color, I); P-> alpha = 255;} p = (pargbquad) graydata-> scan0; int dataoffset = (graydata-> stride> 2)-(INT) graydata-> width; For (uint y = 0; y <graydata-> height; Y ++, P ++ = dataoffset) {for (uint x = 0; x <graydata-> width; X ++, P ++) {P-> color = colortable [p-> Blue]. color ;}}// image color mode mixed void imagecolormixer (bitmapdata * DEST, const bitmapdata * Source) {pargbquad PD, PS; uint width, height; int dstoffset, srcoffset; getdatacopyparams (DEST, source, width, height, PD, PS, dstoffset, srcoffset); For (uint y = 0; y 

All the transformed colormix functions use the Count calculation. The dyeing function imagetint adopts the look-up table method to calculate a 256-color grayscale dyeing table at a time. You only need to assign values directly based on the gray value of each pixel of the grayscale image as the subscript, compared with the pixel-by-pixel call of the colormix function to calculate the dyeing value before modification, the operation efficiency is undoubtedly greatly improved.

This article will focus on improving the color mixing mode.

 

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.