Delphi Image Processing-linear 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.

 

The code in this article is based on "Delphi Image Processing-brightness/contrast adjustment", by changing non-brightness to linear brightness.

Image brightness adjustment can be divided into two methods: non-linear and linear.

The brightness of a non-linear image is to add or subtract a value to the R, G, and B of the image pixel respectively. The advantage is that the code is simple and the brightness adjustment speed is fast. The disadvantage is that the image information is greatly lost, the adjusted image looks dull and has no sense of attention.

The brightness of a linear image is generally used to convert the RGB color of the image pixel to the color space such as HSL (HSV). After the L (v) part is increased or decreased, it is converted to the RGB color space, the advantage is that you have a strong sense of image brightness adjustment. The disadvantage is that the Code is complicated and the adjustment speed is slow, and there is a great distortion when the image brightness increase or decrease is large.

In view of the advantages and disadvantages of the above two methods, I have improved the image brightness adjustment method by referring to the contrast and saturation adjustment principle of Photoshop (see my article, the effect is not bad, mainly including non-distortion adjustment of the range width, a good sense of attention, as much as possible to reduce the loss of image information, etc. At the same time, in the code processing, the grayscale table search method is adopted, first, a 256-element Linear brightness/contrast search table is created. Then, the adjusted data is obtained for image data in the search table based on the R, G, and B values, therefore, the processing speed is basically the same as the non-linear brightness/contrast in "Delphi Image Processing-brightness/contrast adjustment.

The principle is represented by a formula:

If the value range of the brightness increase or decrease is-1 -- + 1, when the value is greater than 0:

RGB = RGB + RGB * (1/(1-value)-1)

When value is <0:

RGB = RGB + RGB * value

The following code adjusts the linear brightness and contrast of an image:

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 ImageLineBrightContrast(var Data: TImageData; Bright, Contrast, Threshold: Integer);var  vs: TGrayTable;  cv, bv: Single;  i, v: Integer;  height, dataOffset: Integer;begin  if (Bright = 0) and (Contrast = 0) then Exit;  if Bright <= -255 then bv := -1 else bv := Bright / 255;  if (Bright > 0) and (Bright < 255) then    bv := 1 / (1 - bv) - 1;  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 + Round(i * bv)) 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 + Round(v * bv)) 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;

 

The following figure shows how to use RGB nonlinear brightness adjustment (medium), HSL linear brightness adjustment (right), and the improved linear brightness adjustment method (left) to map the adjustment results of the same photo:

Source image:

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.