Delphi Image Processing-set key image colors

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.

 

Set the Key Color of the image to make the color of an image or within a certain range transparent. It is a commonly used image processing method in image synthesis and animation display. The following is the implementation code:

 

Procedure colorkeyeq; ASM punpckldq mm2, mm2 // mm2 = colorkey mov eax, ECx SHR ECx, 1 // compare two pixels each time @ yloop: Push ECx @ xloop: dec ECx JS @ 1 movq mm0, [EDI] // mm0 = A1 R1 G1 B1 A0 R0 G0 B0 pcmpeqd mm0, mm2 // compare two pixels by 32 digits pandn mm0, [EDI] // compare the result with the original pixel value movq [EDI], mm0 // Add EDI to the stored result, 8 JMP @ xloop @ 1: Test eax, 1 // compare the possible last pixel JZ @ 2 movd mm0, [EDI] pcmpeqd mm0, mm2 movd ECx, mm0 Not ECx and [EDI], ECx add EDI, 4 @ 2: Pop ECx add EDI, EBX dec edX jnz @ yloopend; Procedure colorkeyarea; ASM pxor mm7, mm7 // The comparison command is run based on the number of symbols. Therefore, punpcklbw mm2 and mm7 // colorlow are expanded to punpcklbw mm3 in bytes, mm7 // colorhigh is expanded by byte to @ yloop: Push ECx @ xloop: movd mm0, [EDI] // mm0 = 00 00 00 00 a r g B punpcklbw mm0, mm7 // mm0 = 00 A 00 R 00g 00 B movq MM1, mm2 pcmpgtw MM1, mm0 // COMPARE WHETHER colorlow is greater than mm0 packss WB MM1, MM1 // compress the result into byte movd eax, MM1 test eax, eax jnz @ next // If colorlow is less than or equal to mm0 pcmpgtw mm0, mm3 // compare whether mm0 is greater than colorhigh packsswb mm0, mm0 // compress the result into byte movd eax, mm0 test eax, eax jnz @ next // If (argb <colorlow | argb> colorlor) and [EDI], eax // * (int *) EDI = 0 @ next: add EDI, 4 loop @ xloop pop ECx add EDI, EBX dec edX jnz @ yloopend; // set the Color Key (transparent range ). Colorlow low color key value; colorhigh high color key value // procedure imagesetcolorkey (VAR data: timagedata; colorlow, colorhigh: longword); ASM push EDI push EBX movd mm2, EDX movd mm3, ECx XOR edX, ECx push edX call _ setdataregs pop eax test eax, eax JZ @ 1 call colorkeyarea JMP @ exit @ 1: Call colorkeyeq @ Exit: Emms pop EBX pop ediend; // set the Color Key by the coordinate color. X, Y image coordinate points, precision Color Key tolerance procedure imagesetcolorkeypoint (VAR data: timagedata; X, Y: integer; precision: longword); ASM push EDI push EBX test edX, edX JS @ exit CMP edX, [eax]. timagedata. width Jae @ exit test ECx, ECx JS @ exit CMP ECx, [eax]. timagedata. height Jae @ exit imul ECx, [eax]. timagedata. stride SHL edX, 2 // edX = data-> scan0 + Add edX, ECx // y * Data-> stride + x * 4 Add edX, [eax]. timagedata. scan0 pxor mm7, mm7 movd mm0, precision // mm0 = precision (4 word) punpcklwd mm0, mm0 punpcklwd mm0, mm0 movd mm2, [edX] // mm2 = mm3 = argb (byte --> word) punpcklbwmm2, mm7 movq mm3, mm2 paddswmm3, mm0 // mm3 + = mm0 (colorhigh) psubswmm2, mm0 // mm2-= mm0 (colorlow) packuswbmm3, mm3 packuswbmm2, mm2 call _ setdataregs movq mm0, mm2 pxor mm0, mm3 movd eax, mm0 test eax, eax JZ @ 2 call colorkeyarea JMP @ end @ 2: Call colorkeyeq @ end: Emms @ Exit: Pop EBX pop ediend;

 

The above Code provides two key color processes for setting the image:

During the imagesetcolorkey process, the Key Color Range of the image is set. colorlow and colorhigh are the low and high key values of the key colors of the image respectively. As long as the color is within the high or low key range, it will become transparent, if colorlow is equal to colorhigh, only one color becomes transparent.

In the imagesetcolorkeypoint process, key colors are set by the position and color tolerance of the image. Because the background color of many images is not a simple color, it is difficult to determine the high and low color keys of key colors by using the imagesetcolorkey process. In this case, an image coordinate can be given, based on the pixel color at the Coordinate Position, any color within the upper and lower tolerance range of the baseline color will become transparent.

The following is a simple call example:

procedure TForm1.Button3Click(Sender: TObject);var  bmp: TGpBitmap;  g: TGpGraphics;  data: TImageData;begin  bmp := TGpBitmap.Create('..\media\56-3.jpg');  g := TGpGraphics.Create(Canvas.Handle);  g.DrawImage(bmp, 0, 0);  data := LockGpBitmap(bmp);  ImageSetColorKeyPoint(data, 175, 5, 39);  UnlockGpBitmap(bmp, data);  g.DrawImage(bmp, data.Width, 0);  g.Free;  bmp.Free;end;

The following figure shows the running effect:

 

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.