Delphi Image Processing-set Gamma

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.


A friend sent me an email asking me how to use the imageattributes. setgamma method of GDI +. He said that when using the GDI + setgamma method, he found that the larger the value given to the parameter, the darker the image, and vice versa, it seems different from what he understood. I tested it. I checked the. net2.0 class library documentation. The description of this method parameter is:

The typical GAMMA value ranges from 1.0 to 2.2, but in some cases, values between 0.1 and 5.0 are also useful.

The imageattributes object maintains the color and grayscale settings of the five adjustment categories: Default, bitmap, paint brush, pen, and text. For example, you can specify a gamma value for the default category, another GAMMA value for the bitmap category, and another GAMMA value for the pen category.

The default color adjustment settings and grayscale adjustment settings apply to all categories without adjustment settings. For example, if you have never specified any adjustment settings for the pen category, the default settings apply to the pen category.

Once a color or grayscale adjustment setting is specified for a category, the default adjustment setting no longer applies to this category. For example, assume that you have specified a set of adjustment settings for the default category. If you set a gamma value for the pen category by passing the pen to the setgamma method, any default settings will not be applied to the pen.

Gamma values outside the normal range can be used for legacy CRT monitors, or for monitors under unusual lighting conditions (such as industrial environments or window display.

There is also an example, which is excerpted below:


   void SetGammaExample( PaintEventArgs^ e )   {      // Create an Image object from the file Camera.jpg, and draw it to      // the screen.      Image^ myImage = Image::FromFile( "Camera.jpg" );      e->Graphics->DrawImage( myImage, 20, 20 );      // Create an ImageAttributes object and set the gamma to 2.2.      ImageAttributes^ imageAttr = gcnew ImageAttributes;      imageAttr->SetGamma( 2.2f );      // Draw the image with gamma set to 2.2.      Rectangle rect = Rectangle(250,20,200,200);      e->Graphics->DrawImage( myImage, rect, 0, 0, 200, 200, GraphicsUnit::Pixel, imageAttr );   }

According to the example code, after setting a Gamma Correction Value of 2.2, the image display result should be brighter than before Gamma Correction is not used. But now the result is the opposite! Is there a wrong understanding, or is it a GDI + bug ???

With doubts, I searched the internet and found the same question on the msdn Forum: Answer.

I wrote a function for setting Gamma Correction, which is the opposite of the imageattributes. setgamma method of GDI +. However, if the setgamma method parameter of GDI + is given in the form of reciprocal, the effect is the same as that of the function I wrote (you can also say that the exponent in the function I wrote is: change the = 1/gamma statement to exponent: = gamma, which is the same as the setgamma method of GDI + ):

Procedure imagesetgamma (VAR data: timagedata; GAMMA: single); var I: integer; exponent: Double; gammatab: array [0 .. 255] of byte; begin exponent: = 1/gamma; // This sentence is changed to exponent: = gamma;, which has the same effect as the setgamma parameter of GDI + for I: = 0 to 255 do begin gammatab [I]: = round (Power (I + 0.5)/256, exponent) * 256-0.5); end; ASM push EBP push ESI push EDI push EBX mov eax, data call _ setdataregs Lea ESI, gammatab mov EBP, EDX @ yloop: Push ECx @ xloop: movzx eax, [EDI]. targbquad. blue movzx edX, [EDI]. targbquad. green mov Al, [ESI + eax] mov DL, [ESI + EDX] mov [EDI]. targbquad. blue, Al mov [EDI]. targbquad. green, DL movzx eax, [EDI]. targbquad. red mov Al, [ESI + eax] mov [EDI]. targbquad. red, Al add EDI, 4 loop @ xloop pop ECx add EDI, EBX dec EBP jnz @ yloop pop EBX pop EDI pop ESI pop EBP end; // export procedure tform1.button3click (Sender: tobject); var BMP: tgpbitmap; G: tgpgraphics; Data: timagedata; begin BMP: = tgpbitmap. create ('.. \ media \ source.bmp '); G: = tgpgraphics. create (canvas. handle); G. drawimage (BMP, 0, 0); Data: = lockgpbitmap (BMP); imagesetgamma (data, 2.2); unlockgpbitmap (BMP, data); G. drawimage (BMP, 0, Data. height); G. free; BMP. free; end;


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

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: 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.