Delphi Image Processing-brightness/contrast Adjustment

Source: Internet
Author: User

Reading Tips:

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

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

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

The code in this article must include the imagedata. Pas unit in "Delphi Image Processing-data type and public process.

I have written several articles on brightness adjustment, but I have not written about the process and article on image contrast adjustment. The reason is that I have never found a good algorithm. Some people may say that adjusting the brightness and contrast of an image is the simplest graphic operation. The algorithm can be called a search operator on the Internet. Indeed, this is the simplest operation, I have tried a few articles on the Internet. It seems that they are not ideal. The key is that the algorithm is too simple and the actual operation is not good. So I thought that the contrast of Photoshop is still good, it is also common, but I did not introduce its algorithm on the Internet. I spent most of my time studying it. I spent another hour writing a Delphi Process and tried it, it works exactly the same as the contrast adjustment of Photoshop! So I carefully wrote a test program and adjusted the brightness and contrast together (the brightness and contrast processing process are independent of each other, the brightness process is basically the code in this blog article "Application of GDI + in Delphi -- adjust image brightness"), but the effect is quite different from that in Photoshop. Why, the brightness adjustment algorithm of Photoshop is the simplest one, the brightness process is the same as that in my brightness process (for the Effect Comparison diagram, see "Application of GDI + in Delphi Program-linear adjustment of image brightness"). As mentioned above, the Contrast process algorithm is the same as that of Photoshop. It can be adjusted together, either by adjusting the brightness or by adjusting the contrast first. After careful analysis, Photoshop uses a function to process the brightness/contrast, and the brightness adjustment is based on the positive and negative contrast. The following is the implementation code (including a simple example ):

function _CheckRgb(Rgb: Integer): Integer;asm    test    eax, eax    jge     @@1    xor     eax, eax    ret@@1:    cmp     eax, 255    jle     @@2    mov     eax, 255@@2:end;procedure ImageBrightContrast(var Data: TImageData; Bright, Contrast, Threshold: Integer);var  vs: TGrayTable;  cv: Single;  i, v: Integer;  height, dataOffset: Integer;begin  if (Bright = 0) and (Contrast = 0) then Exit;  if Contrast <= -255 then cv := -1 else cv := Contrast / 255;  if (Contrast > 0) and (Contrast < 255) then    cv := 1 / (1 - cv) - 1;  for i := 0 to 255 do  begin    if Contrast > 0 then v := _CheckRgb(i + bright) else v := i;    if Contrast >= 255 then    begin      if v >= Threshold then v := 255 else v := 0;    end    else      v := _CheckRgb(v + Round((v - Threshold) * cv));    if Contrast <= 0 then vs[i] := _CheckRgb(v + bright) else vs[i] := v;  end;  asm    mov     eax, Data    call    _SetDataRegs    mov     height, edx    mov     dataOffset, ebx    lea     esi, vs;@@yLoop:    push    ecx@@xLoop:    movzx   eax, [edi].TARGBQuad.Blue    movzx   ebx, [edi].TARGBQuad.Green    movzx   edx, [edi].TARGBQuad.Red    mov     al, [esi+eax]    mov     bl, [esi+ebx]    mov     dl, [esi+edx]    mov     [edi].TARGBQuad.Blue, al    mov     [edi].TARGBQuad.Green, bl    mov     [edi].TARGBQuad.Red, dl    add     edi, 4    loop    @@xLoop    pop     ecx    add     edi, dataOffset    dec     height    jnz     @@yLoop@@Exit:  end;end;procedure TForm1.Button3Click(Sender: TObject);var  bmp: TGpBitmap;  g: TGpGraphics;  data: TImageData;begin  bmp := TGpBitmap.Create('..\media\source1.jpg');  g := TGpGraphics.Create(Canvas.Handle);  g.DrawImage(bmp, 0, 0);  data := LockGpBitmap(bmp);  ImageBrightContrast(data, 50, 0, 121);  UnlockGpBitmap(bmp, data);  g.DrawImage(bmp, data.Width, 0);  g.Free;  bmp.Free;end;

 

The following is a brief introduction to the principle of brightness/contrast.

I. Photoshop contrast algorithm. The following formula can be used for representation:

(1), nrgb = RGB + (RGB-threshold) * contrast/255

In the formula, nrgb indicates the new R, G, and B components of the image pixel, RGB indicates the R, G, and B components of the image pixel, and threshold is the given threshold value, contrast is the contrast increment processed.

Photoshop processes contrast increments based on the positive and negative values of the given values:

When the increment is-255, It is the lower limit of the image contrast. At this time, all the RGB components of the image are equal to the threshold. The image is completely gray, and there is only one line on the grayscale image, that is, the threshold gray level;

When the increment is greater than-255 and less than 0, the following formula is used to calculate the pixel components of the image;

When the increment is equal to 255, It is the upper limit of the image contrast. It is actually equal to the set image threshold. The image consists of up to eight colors and consists of up to eight lines on the grayscale image, red, yellow, green, blue, purple, black, and white;

When the increment is greater than 0 and less than 255, the increment is processed according to the following formula (2), and then the contrast is calculated based on the formula (1) above:

(2) ncontrast = 255*255/(255-contrast)-255

In the formula, ncontrast is the contrast increment after processing, and contrast is the given contrast increment.

Ii. Adjust the image brightness. This article uses the most commonly used nonlinear brightness adjustment (this is also the brightness adjustment method for versions earlier than phoposhop CS3 and later versions also retain the options for this brightness adjustment method ).

3. image brightness/contrast comprehensive adjustment algorithm. This is very simple. When the brightness and contrast are adjusted at the same time, if the contrast is greater than 0, the brightness is adjusted, and then the contrast is adjusted. If the contrast is smaller than 0, the contrast is adjusted first, then adjust the brightness.

In the brightness/contrast adjustment function imagebrightcontrast, A 256-element search table is created based on the principles described earlier, then, the adjusted data is obtained for the image data pixel by R, G, and B component values in the search table, so the processing speed is quite fast.

Below is the running of contrast 255:

 

For details about the use of GDI + units and descriptions in the "Delphi image processing" series, see the article 《GDI + for VCL basics-GDI + and VCL.

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

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

 

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.