Application of GDI + in Delphi-brightness adjustment of imitation Photoshop

Source: Internet
Author: User

Over the past few days, I have studied Photoshop's color phase/saturation command, which is the so-called HSB color mode. I didn't fully understand it, and I did not have any online search results, I have read some articles about the HSB algorithm, but they actually talk about the HSV or HSL algorithm.

We don't need to study the color phases in PS/saturation. The principle is the same as that in HSV or hsl h.

The effect of saturation at the three points of-100, 0, and + 100 is exactly the same as that of HSL. The other ranges are different, especially in the range of 0 -- +, the adjustment time is smoother than the H adjustment of HSL, so the effective adjustment range is large, some images are imperceptible and distorted when they are adjusted to more than 50% (here, the "distortion" is for the ugly spots in the color, not to say that the whole picture is not distorted ), however, it is difficult to see the positive adjustment of HSL h by more than 10%. It is not the same as HSV. It can be seen that the color phase/saturation algorithm of PS should be improved based on HSL.

The most confusing thing is the brightness adjustment of PS, which seems to be "independent" from the color saturation. We know that to adjust V or L in HSV or HSL mode in a program, we usually need to Convert RGB to HSV or HSL first, or at least separate the V or l parts, after modification, convert it to RGB mode (see my article "Application of GDI + in Delphi Program-linear adjustment of image brightness" to separate the L part of HSL to adjust the brightness ), the brightness adjustment of PS is different. You do not need to Convert RGB to the so-called HSB for adjustment. You can simply write a function, please refer to the following Delphi process and test code (using the tgpbitmap of GDI + and the tbitmap test of Delphi respectively) to simulate the PS brightness adjustment (strictly speaking, it is not called imitation, but the actual PS brightness adjustment process ):

 

Note: In order to unify the data types and image processing formats used in the "Application of GDI + in Delphi" series of articles, the code in this article has been revised, for details about the gdiplus unit and bug correction used in the code, see the article "GDI + for VCL basics-GDI + and VCL". (8.8.18)

Data Type:

  1. Type
  2. // Image data structure compatible with the GDI + tbitmapdata Structure
  3. Timagedata = packed record
  4. Width: longword; // The image width.
  5. Height: longword; // Image Height
  6. Stride: longword; // The length of the scanned line of the image in bytes.
  7. Pixelformat: longword; // unused
  8. Scan0: pointer; // image data address
  9. Reserved: longword; // Reserved
  10. End;
  11. Pimagedata = ^ timagedata;
  12. // Obtain the timagedata data structure of the tbitmap image to facilitate the processing of the tbitmap Image
  13. Function getimagedata (BMP: tbitmap): timagedata;
  14. Begin
  15. BMP. pixelformat: = pf32bit;
  16. Result. Width: = BMP. width;
  17. Result. Height: = BMP. height;
  18. Result. scan0: = BMP. scanline [BMP. Height-1];
  19. Result. stride: = result. Width SHL 2;
  20. // Result. stride: = (32 * BMP. width) + 31) and $ ffffffe0) SHR 3;
  21. End;

Process Code:

  1. // Adjust the image brightness
  2. Procedure psbrightness (data: timagedata; Value: integer );
  3. ASM
  4. Push EBP
  5. Push ESI
  6. Push EDI
  7. Push EBX
  8. MoV EDI, [eax + 16] // EDI = data. scan0
  9. MoV EBP, [eax + 4] // EDP = data. Height * Data. Width
  10. Imul EBP, [eax]
  11. MoV ESI, EDX // ESI = Value
  12. MoV EBX, 255 // EBX = 255
  13. ClD
  14. @ Pixelloop: // for (I = EBP; I> 0; I --)
  15. MoV ECx, 3 //{
  16. @ Vloop: // For (j = 3; j> 0; j --)
  17. Movzx eax, [EDI] // {
  18. Push eax
  19. Test ESI, ESI
  20. JS @ 1
  21. Neg eax // If (value> 0)
  22. Add eax, EBX // RGB = RGB + (255-RGB) * value/255
  23. @ 1:
  24. Imul eax, ESI // else
  25. CDQ // RGB = RGB + RGB * value/255
  26. Idiv EBX
  27. Pop edX
  28. Add eax, EDX
  29. JNS @ 2 // RGB = max (0, min (255, RGB ))
  30. XOR eax, eax
  31. JMP @ 3
  32. @ 2:
  33. CMP eax, EBX
  34. Jle @ 3
  35. MoV eax, EBX
  36. @ 3:
  37. Stosb // * EDI ++ = RGB
  38. Loop @ vloop //}
  39. Inc edi // EDI ++
  40. Dec EBP
  41. Jnz @ pixelloop //}
  42. Pop EBX
  43. Pop EDI
  44. Pop ESI
  45. Pop EBP
  46. End;
  47. // Adjust the brightness of the GDI + image
  48. Procedure gdippsbrightness (BMP: tgpbitmap; Value: integer );
  49. VaR
  50. Data: tbitmapdata;
  51. Begin
  52. If value = 0 Then exit;
  53. Data: = BMP. lockbits (gprect (0, 0, BMP. Width, BMP. Height), [imread, imwrite], pf32bppargb );
  54. Try
  55. Psbrightness (timagedata (data), value );
  56. Finally
  57. BMP. unlockbits (data );
  58. End;
  59. End;
  60. // Adjust the brightness of the tbitmap Image
  61. Procedure bitmappsbrightness (BMP: tbitmap; Value: integer );
  62. Begin
  63. If value <> 0 then
  64. Hslbrightness (getimagedata (BMP), value );
  65. End;

We can see that the above psbrightness process does not rely on any color mode conversion, but uses the following pseudocode formula:

If (value> = 0)
RGB = RGB + (255-RGB) * value/255;
Else
RGB = RGB + RGB * value/255;

Here, RGB represents the R, G, and B colors respectively, and the value is the brightness value. What is the meaning of this formula? It is actually the deformation of the formula for converting HSL to the L part of RGB, the formula I used in "Application of GDI + in Delphi Program-linear adjustment of image brightness" is the same as that in its form, except that the calculation base is different:

L = L-128;
If (L> = 0)
RGB = RGB + (255-RGB) * l/128;
Else
RGB = RGB + RGB * l/128;

The former uses the full range of value 255 or 100% (the/255 after the formula is changed to/100), while the latter uses the 1/2 of L, that is, 128 or 50% (in this case, the effect is quite different), and to adjust the brightness by using L, you must obtain L from the HSL space and add the adjusted value, the PS brightness adjustment does not need to get the original B from HSB, which means that B is always "0" in HSB "! Although the brightness adjustment of PS is put together with the color phase/saturation adjustment, it is completely independent of the color phase/saturation, which is why I feel like it is "independent, it is also an important reason for the study of the failure of the PS saturation algorithm (a B = 0 HSB mode, how to correctly convert the HS part to R, G, B? You need to know that the L in the V and HSL of HSV is crucial in the correct conversion to the RGB mode !).

If my feeling is correct, what is the principle of Ps "independent" brightness adjustment? We know that the general non-linear RGB brightness adjustment is only achieved by adding and reducing a certain amount based on the original R, G, and B values, the brightness adjustment principle of PS must be found from the previous formula. We will adjust the positive brightness formula: RGB = RGB + (255-RGB) * value/255 to RGB = (RGB * (255-value) + 255 * value)/255, if the value represents the maximum value of 255 with 1, it is RGB = RGB * (1-value) + 255 * value. what can be seen? Anyone who knows about image synthesis knows this formula. In fact, the brightness adjustment of PS adopts the Alpha synthesis method. Here the value is Alpha, and the first part of the formula is RGB * (1-value) the image part is followed by the 255 * value part, which is a white shadow layer. The greater the brightness, the greater the Alpha of the Shadow layer, the more the image is discussed, and vice versa. The negative adjustment of brightness is done by a black shadow layer. Negative 100% is all black. The full picture is displayed only when alpha = 0, or the brightness value is 0. It is easy to verify the above statement. One is to run my test code, but to overwrite an image with a full white or black layer in PS, and adjust the opacity of this layer, it can be seen that the effect is exactly the same as that of brightness adjustment!

In fact, I am only interested in the adjustment of the saturation of PS, because we have already said that the effect is better than that of the adjustment of the saturation of HSV and HSL, the range is large, and the saturation algorithm has not been studied, then we made a brightness adjustment process. I am not very grateful to anyone who want to know the PS saturation algorithm!

 

Test code:

  1. Procedure tform1.button1click (Sender: tobject );
  2. VaR
  3. Image: tgpbitmap;
  4. G: tgpgraphics;
  5. Begin
  6. Image: = tgpbitmap. Create ('d:/vcllib/gdiplusdemo/Media/20041001.jpg ');
  7. G: = tgpgraphics. Create (handle, false );
  8. G. drawimage (image, 10, 10 );
  9. Gdippsbrightness (image, 30 );
  10. G. drawimage (images, 10,220 );
  11. Image. Free;
  12. G. Free;
  13. End;
  14. Procedure tform1.button2click (Sender: tobject );
  15. VaR
  16. Image: tbitmap;
  17. Begin
  18. Image: = tbitmap. Create;
  19. Image. loadfromfile ('d:/vcllib/gdiplusdemo/Media/20041001.bmp ');
  20. Canvas. Draw (10, 10, image );
  21. Bitmappsbrightness (image,-30 );
  22. Canvas. Draw (10,220, image );
  23. Image. Free;
  24. End;

If there is an error please send a letter to correct: maozefa@hotmail.com

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.