Application of GDI + in Delphi-adjust image brightness

Source: Internet
Author: User

The Application of GDI + in the Delphi Program-there are many ways to adjust the brightness of an image, the most common method is to increase or decrease a certain value at the same time for the R, G, and B components of the image pixel to adjust the brightness. Here I use the Scan Line of the GDI + image for processing. The core processing adopts two identical processes, one is the PASCAL process, and the other is the embedded assembly process. Through comparison, there is almost no difference in small images, and there are still some differences in processing large images (for specific test results, see the comments in the Code). This shows that Delphi's code optimization is still very good. Tgpbitmap must be called for processing scanned lines using GDI + images. lockbits and tgpbitmap. the unlockbits process, and the call of these two processes is quite time-consuming. Taking an image of 2304*1728 as an example, the time consumption actually reached 188 milliseconds! However, the brightness adjustment process takes up to 63 milliseconds. From this we can see that using GDI + image scanning lines to process images is not ideal. It should be noted that the gdiplus unit in this example and in the subsequent examples of GDI + for Delphi is self-written, which is different from the current versions available on the Internet. You need to refer to the example to be tested, you can modify the settings or send an email to request the gdiplus unit. 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". 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 brightness (data: timagedata; Value: integer );
  3. ASM
  4. Push ESI
  5. Push EDI
  6. MoV EDI, [eax + 16]
  7. MoV ESI, [eax + 4]
  8. Imul ESI, [eax]
  9. ClD
  10. @ Pixelloop:
  11. MoV ECx, 3
  12. @ Rgbloop:
  13. Movzx eax, [EDI]
  14. Add eax, EDX
  15. JNS @ 1
  16. XOR eax, eax
  17. JMP @ 2
  18. @ 1:
  19. CMP eax 255
  20. Jle @ 2
  21. MoV eax, 255
  22. @ 2:
  23. Stosb
  24. Loop @ rgbloop
  25. INC EDI
  26. Dec ESI
  27. Jnz @ pixelloop
  28. Pop EDI
  29. Pop ESI
  30. End;
  31. // Adjust the GDI + image brightness
  32. Procedure gdipbrightness (BMP: tgpbitmap; Value: integer );
  33. VaR
  34. Data: tbitmapdata;
  35. Begin
  36. If value = 0 Then exit;
  37. Data: = BMP. lockbits (gprect (0, 0, BMP. Width, BMP. Height), [imread, imwrite], pf32bppargb );
  38. Try
  39. Brightness (timagedata (data), value );
  40. Finally
  41. BMP. unlockbits (data );
  42. End;
  43. End;
  44. // Adjust the brightness of the tbitmap Image
  45. Procedure bitmapbrightness (BMP: tbitmap; Value: integer );
  46. Begin
  47. If value <> 0 then
  48. Brightness (getimagedata (BMP), value );
  49. End;

Test code:

  1. // To help beginners understand the BaSm code, write a pure pas code process for processing the GDI + image brightness.
  2. // You can change the brightness of a tbitmap image.
  3. Procedure gdipbrightness_pas (BMP: tgpbitmap; Value: integer );
  4. Function setrgbvalue (RGB: byte): integer;
  5. Begin
  6. Result: = value + RGB; // pixel color component + Brightness Value
  7. If result <0 then // pixel color component> = 0 and <= 255
  8. Result: = 0
  9. Else if Result & gt; 255 then
  10. Result: = 255;
  11. End;
  12. VaR
  13. Data: tbitmapdata;
  14. P: prgbquad;
  15. I, Count: longword;
  16. Begin
  17. If value = 0 Then exit;
  18. Data: = BMP. lockbits (gprect (0, 0, BMP. Width, BMP. Height), [imread, imwrite], pf32bppargb );
  19. Try
  20. Count: = data. Width * Data. height;
  21. P: = data. scan0;
  22. For I: = 1 to count do
  23. Begin
  24. P ^. rgbblue: = setrgbvalue (P ^. rgbblue );
  25. P ^. rgbgreen: = setrgbvalue (P ^. rgbgreen );
  26. P ^. rgbred: = setrgbvalue (P ^. rgbred );
  27. INC (P );
  28. End;
  29. Finally
  30. BMP. unlockbits (data );
  31. End;
  32. End;
  33. // Test the GDI + image
  34. Procedure tform1.button1click (Sender: tobject );
  35. VaR
  36. Image: tgpbitmap;
  37. G: tgpgraphics;
  38. Begin
  39. Image: = tgpbitmap. Create ('d:/vcllib/gdiplusdemo/Media/20041001.jpg ');
  40. G: = tgpgraphics. Create (handle, false );
  41. G. drawimage (image, 10, 10 );
  42. // Gdipbrightness_pas (image, 30 );
  43. Gdipbrightness (image, 30 );
  44. G. drawimage (images, 10,220 );
  45. Image. Free;
  46. G. Free;
  47. End;
  48. // Test the tbitmap Image
  49. Procedure tform1.button2click (Sender: tobject );
  50. VaR
  51. Image: tbitmap;
  52. Begin
  53. Image: = tbitmap. Create;
  54. Image. loadfromfile ('d:/vcllib/gdiplusdemo/Media/20041001.bmp ');
  55. Canvas. Draw (10, 10, image );
  56. Bitmapbrightness (image,-30 );
  57. Canvas. Draw (10,220, image );
  58. Image. Free;
  59. End;

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.