C + + image processing-Image Color Scale adjustment

Source: Internet
Author: User

Reading Tips :

The C + + image processing series is code-clear, readable-oriented, all using C + + code.

the Delphi Image Processing "series with the focus on efficiency, the general code is Pascal, the core code uses BASM.

As much as possible to keep the content consistent, can cross-reference.

This code must include the C + + image processing-data types and common functions the BmpData.h header file in the article.


In Photoshop, image level adjustment is widely used, and the process of Image Color scale adjustment in this paper is basically consistent with Photoshop processing effect.

Photoshop's Color scale adjustment sub-input level adjustment and output level adjustment, where the input level adjustment has 3 adjustment points, that is commonly said black, white and gray field adjustment.

The basic algorithm of input color scale adjustment is not complicated, first calculate the difference between white field and black field, then calculate the difference between the pixel value and the Black field Rgbdiff, if rgbdiff<=0, the pixel weight value equals 0, otherwise, Calculates the power of the inverse of the gray field at the base of the ratio of Rgbdiff to diff. expressed in a formula:

Diff = Highlight-shadow

Rgbdiff = Rgb-shadow

Clrgb = Power (Rgbdiff/diff, 1/midtones)

Where shadow is the input level low-end data (black field), highlight is the input color level high-end data (white field), midtones is the input color scale intermediate data (gray field), diff is the difference between the two (must be greater than 1), RGB is the pre-adjusted pixel component value, The CLRGB is the pixel component value after the input color scale is adjusted.

The output Color scale adjustment is more simple, first calculates the output Color scale white field and the Black field difference and 255 ratio coefficient, then uses the input color scale adjusted pixel component value to multiply this coefficient, plus output black field value can. Expressed in a formula:

Outclrgb = Clrgb * (Outhighlight-outshadow)/255 + Outshadow

Among them, Outshadow is output black field, Outhighlight is the output white field, Outclrgb is the pixel component value of all levels adjusted.

Before already mentioned the input color scale black and white field the dispersion must be greater than 1, and the input color scale does not have this limit, the output black and white field of the difference can be negative, when the output black field and white field is completely reversed, the output color scale adjusted picture is the original picture negative.

The color scale adjustment involves four channels, namely R, G, b each component channel and the whole color channel, if each channel individual adjustment, will be more troublesome and time-consuming, this article uses the Color scale table substitution method, can complete all four channels the color scale adjustment in one time.

The following is the code for Image Color Scale adjustment:

The level item structure typedef struct{uint SHADOW; FLOAT midtones; UINT Highlight; UINT Outshadow; UINT Outhighlight;} Colorlevelitem, *pcolorlevelitem;typedef Struct{colorlevelitem Blue; Colorlevelitem Green; Colorlevelitem Red; Colorlevelitem RGB;} Colorleveldata, *pcolorleveldata; VOID Initcolorleveldata (Pcolorleveldata cldata) {Pcolorlevelitem item = &cldata->blue;for (INT i = 0; i < 4; i + +) , item + +) {Item->shadow = Item->outshadow = 0;item->highlight = Item->outhighlight = 255;item->midtones = 1 .0;}} BOOL getcolorleveltable (Pcolorlevelitem item, Lpbyte cltable) {int diff = (int) (Item->highlight-item->shadow); I NT Outdiff = (INT) (Item->outhighlight-item->outshadow); (Item->highlight <= 255 && diff < 255 && diff >= 2) | | (Item->outshadow <= 255 && item->outhighlight <= 255 && Outdiff < 255) | | (! (Item->midtones > 9.99 && item->midtones > 0.1) && item->midtones! = 1.0)) return FALSe;double Coef = 255.0/diff;double Outcoef = outdiff/255.0;double exponent = 1.0/item->midtones;for (INT i = 0; i < 256; i + +) {INT v;//compute input levels black and white field if (Cltable[i] <= (BYTE) item->shadow) v = 0;else{v = (INT) ((Cltable[i]-item->shadow) * C OEF + 0.5); if (V > 255) v = 255;}  Calculate input Color scale gray field v = (INT) (Pow (v/255.0, exponent) * 255.0 + 0.5);//Calculate output levels cltable[i] = (BYTE) (v * outcoef + Item->outshadow + 0.5);} return TRUE;} BOOL Checkcolorleveldata (Pcolorleveldata cldata, BYTE cltables[][256]) {bool result = False;int I, j;for (i = 0; i < 3; i + +) {for (j = 0; J < N; j + +) Cltables[i][j] = (BYTE) J;} Pcolorlevelitem item = &cldata->blue;for (i = 0; i < 3; i + +, item + +) {if (Getcolorleveltable (item, Cltables[i]) ) result = TRUE;} for (i = 0; i < 3; i + +) {if (! Getcolorleveltable (item, cltables[i])) Break;result = TRUE;} return result;} Image data color scale adjustment void Imagecolorlevel (BitmapData *dest, BitmapData *source, Pcolorleveldata cldata) {pargbquad PD, PS; UINT width, height;int dstoFfset, Srcoffset; Getdatacopyparams (dest, source, width, height, PD, PS, Dstoffset, srcoffset);  BYTE cltables[3][256];if (Checkcolorleveldata (Cldata, Cltables)) {for (UINT y = 0; y < height; y + +, ps + = Srcoffset, PD + = Dstoffset) {for (UINT x = 0; x < width; x + +, PS + +, PD + +) {Pd->blue = Cltables[0][ps->blue];PD->green = cl Tables[1][ps->green];PD->red = cltables[2][ps->red];pd->alpha = Ps->alpha;}}} else if (dest! = Source) {for (uint y = 0; y < height; y + +, ps + = srcoffset, PD + = Dstoffset) {for (uint x = 0; x < w Idth;    x + +, PS + +, PD + +) {Pd->color = Ps->color;}} }}

Here is an example of a simple image color scale adjustment function call:

void __fastcall Tform1::button1click (tobject *sender) {BitmapData dest, source; Bitmap *sbmp = new Bitmap (L ": \\.. \\media\\source1.jpg "); Lockbitmap (Sbmp, &source); Bitmap *dbmp = new Bitmap (source. Width, source. Height, Pixelformat32bppargb); Lockbitmap (Dbmp, &dest); Colorleveldata Cldata;initcolorleveldata (&cldata); ClData.RGB.Shadow = 10;cldata.rgb.midtones = 1.2; ClData.RGB.Highlight = 240;cldata.rgb.outshadow = 50;cldata.rgb.outhighlight = 200;/*cldata.rgb.outshadow = 255; ClData.RGB.OutHighlight = 0;*/imagecolorlevel (&dest, &source, &cldata); Unlockbitmap (Dbmp, &dest); Unlockbitmap (Sbmp, &source); Gdiplus::graphics g (Canvas->handle); G.drawimage (sbmp, 0, 0); G.drawimage (dbmp, source. Width, 0);d elete dbmp;delete sbmp;}

The following is the article "Delphi Image Processing-Image Color Scale adjustment " Example Run interface, the first green channel color scale adjustment, the second is the RGB output color scale adjusted to completely reverse the negative image, For detailed Image Color Scale adjustment Interface example, please refer to "Delphi Image Processing-Image Color Scale adjustment ".


This code is edited and compiled with BCB XE7.


due to the limited level, mistakes are unavoidable, please correct and guidance. Email address:[email protected]

Here you can access theC + + image processing-Article Index .


??

C + + image processing-Image Color Scale adjustment

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.