Several functions of image synthesis

Source: Internet
Author: User
Tags drawtext transparent color

Occasionally see a question on the forum, the picture B is synthesized to picture a, and write on a

So, a function that is handy to write, the specific code is as follows:

{No idle

2009-02-11}

function Hecheng (a,b:tbitmap;const transpercent:integer=50): Tbitmap;
Var
I,j:integer;
P1,p2:pbytearray;
Count,minbegin:integer;
Minheight:integer;
Minwidth,maxwidth:integer;
R:trect;
Begin
A.pixelformat: = Pf32bit;
B.pixelformat: = Pf32bit;

MinHeight: = Min (a.height,b.height);
MinWidth: = Min (A.width,b.width);
MaxWidth: = Max (A.width,b.width);

Minbegin: = 4 * ((maxwidth-minwidth) Div 2);
Count: = 4 * (maxwidth-(maxwidth-minwidth) Div 2-1);

For I: = 0 to MinHeight-1 do
Begin
If MinHeight = B.height Then
Begin
P1: = A.scanline[i];
P2: = B.scanline[i];
End
Else
Begin
P1: = B.scanline[i];
P2: = A.scanline[i];
End
J: = Minbegin;
While J < Count do
Begin
if (p2[j-minbegin] = 255) and (p2[j-minbegin+1] = 255) and (p2[j-minbegin+2]=255) then
Inc (j,4)
Else
Begin
P1[J]: = p1[j] + transpercent * (P2[j-minbegin]-p1[j]) div 100;
Inc (J);
End
End
End
If MinHeight = B.height Then
Begin
R.top: = a.height-a.canvas.textheight (' Hello ')-5;
R.bottom: = A.height;
R.left: = 0;
R.right: = A.width;
A.canvas.brush.style: = Bsclear;
Windows. DrawText (a.canvas.handle, ' hello ', -1,r,dt_center or Dt_vcenter or dt_singleline);
Result: = A;
End
Else
Begin
R.top: = b.height-b.canvas.textheight (' Hello ')-5;
R.bottom: = B.height;
R.left: = 0;
R.right: = B.width;
B.canvas.brush.style: = Bscleae r;
Windows. DrawText (b.canvas.handle, ' hello ', -1,r,dt_center or Dt_vcenter or dt_singleline);
Result: = B;
End
End

Here, I first convert bitmaps A and b into pf32bit, where each pixel of each bitmap is stored by 4 bytes.

Storage is BGRL, so the total number of bytes should be the width of the

Filter out the white, that is, RGB is 255, when not processing the composition can be.

From above we can extend a function, can be used to filter any color, that is, to specify a color, as long as the image contains the color of the area to filter out, for example, the picture B contains a red and blue color, at this time set a red filter color, which is after the synthesis of B figure in the red is filtered out

The code is as follows:

{-------------------------------------------------------------------------------
Procedure name: Tcolortorgb
Author: no cannot
Date: 2009.02.11
Parameters: const Color:tcolor; var R, G, B:byte
return value: None
Purpose: Gets the RGB value of the color
-------------------------------------------------------------------------------}

Procedure Tcolortorgb (const Color:tcolor; var R, G, b:byte);
Var
C:integer;
Begin
C: = Colortorgb (Color);
R: = C and $FF;
G: = (C shr 8) and $FF;
B: = (C shr) and $FF;
End
{-------------------------------------------------------------------------------
Procedure name: Hecheng
Author: no cannot
Date: 2009.02.11
Parameter: A, B: Specify a composite bitmap
Transpercent: Setting Transparency
Ignorecolor: Setting the color to ignore when compositing
Coloroffset: Edge chromatic aberration of transparent color (the color within the chromatic aberration will be ignored)

return Value: Tbitmap
Purpose: To synthesize two bitmap images
-------------------------------------------------------------------------------}

function Hecheng (a,b:tbitmap;const transpercent:integer=50;const ignorecolor:tcolor = clwhite; Const coloroffset:byte=0): Tbitmap;
Var
I,j:integer;
P1,p2:pbytearray;
Count,minbegin:integer;
Minheight:integer;
Minwidth,maxwidth:integer;
R:trect;
Rcolor,gcolor,bcolor:byte;
Begin
A.pixelformat: = Pf32bit;
B.pixelformat: = Pf32bit;
Tcolortorgb (Ignorecolor,rcolor,gcolor,bcolor);

MinHeight: = Min (a.height,b.height);
MinWidth: = Min (A.width,b.width);
MaxWidth: = Max (A.width,b.width);

Minbegin: = 4 * ((maxwidth-minwidth) Div 2);
Count: = 4 * (maxwidth-(maxwidth-minwidth) Div 2-1);

For I: = 0 to MinHeight-1 do
Begin
If MinHeight = B.height Then
Begin
P1: = A.scanline[i];
P2: = B.scanline[i];
End
Else
Begin
P1: = B.scanline[i];
P2: = A.scanline[i];
End
J: = Minbegin;
While J < Count do
Begin

//compares the value of the byte, whether the RGB value of the point pixel is the color value that needs to be filtered, and if so, filters out
if (ABS (P2[j-minbegin]-Bcolor) <=coloroffset) and

(ABS (P2[j-minbegin+1]-Gcolor) <=coloroffset) and

(ABS (P2[j-minbegin+2]-rcolor) <=coloroffset) Then
Inc (j,4)
Else
Begin
P1[J]: = p1[j] + transpercent * (P2[j-minbegin]-p1[j]) div 100;
Inc (J);
End
End
End
If MinHeight = B.height Then
Begin
R.top: = a.height-a.canvas.textheight (' Hello ')-5;
R.bottom: = A.height;
R.left: = 0;
R.right: = A.width;
A.canvas.brush.style: = Bsclear;
Windows. DrawText (a.canvas.handle, ' hello ', -1,r,dt_center or Dt_vcenter or dt_singleline);
Result: = A;
End
Else
Begin
R.top: = b.height-b.canvas.textheight (' Hello ')-5;
R.bottom: = B.height;
R.left: = 0;
R.right: = B.width;
B.canvas.brush.style: = Bsclear;
Windows. DrawText (b.canvas.handle, ' hello ', -1,r,dt_center or Dt_vcenter or dt_singleline);
Result: = B;
End
End

For example, now to be transparent, a picture is composited up.

Hecheng (image1.picture.bitmap,image4.picture.bitmap,100,image4.canvas.pixels[0,0],20);

In addition to a transparent drawing method of the function, the effect is not good

{-------------------------------------------------------------------------------
Procedure name: Transparentdraw
Author: no cannot
Date: 2009.02.12
Parameter: Destcanvas: Target Canvas
Destrect: Target Area
Graphic: Bitmap
Coloroffset color difference near the background color, the color within the difference will be transparent
return value: None
-------------------------------------------------------------------------------}

Procedure Transparentdraw (Destcanvas:tcanvas;destrect:trect; Graphic:tbitmap;const coloroffset:byte=0);
Var
I,j,count:integer;
Recth,rectw:integer;
P:pbytearray;
Rcolor,gcolor,bcolor:byte;
Begin
//Area height
Graphic.pixelformat: = Pf32bit;
RECTH: = Destrect.bottom-destrect.top;
If Recth > Graphic.height Then
RECTH: = Graphic.height;
RECTH: = Destrect.top + recth;
RECTW: = Destrect.right-destrect.left;
Tcolortorgb (Graphic.canvas.pixels[0,0],rcolor,gcolor,bcolor);
If rectw > Graphic.width Then
RECTW: = Graphic.width;
Count: = 4*rectw-1;
For I: = Destrect.top to RectH-1 do
Begin
P: = Graphic.scanline[i-destrect.top];
J: = 0;
While J < Count do
Begin
if (ABS (P[j]-Bcolor) <=coloroffset) and (ABS (P[j+1]-Gcolor) <= Coloroffset) and (ABS (p[j+2]-Rcolor) <=coloroff Set) Then
Inc (j,4)
Else
Begin
Bcolor: = P[j];
Gcolor: = p[j + 1];
Rcolor: = p[j+2];
Destcanvas.pixels[j div 4,i]: = RGB (Rcolor,gcolor,bcolor);
Inc (j,4);
End
End
End
End

At the same time, also wrote a change the background color of the image function, the code is as follows:

{-------------------------------------------------------------------------------
Procedure name: Changebmpbackground
Author: no cannot
Date: 2009.02.12
Parameters: Bmp:tbitmap;
Changetoback: The background color to modify to
Coloroffset color difference near the background color, the color within the difference will also be modified
return value: None
-------------------------------------------------------------------------------}

Procedure Changebmpbackground (Bmp:tbitmap; Changetoback:tcolor;const coloroffset:byte = 0);
Var
I,j,count:integer;
Rcolor,gcolor,bcolor:byte;
Trcolor,tgcolor,tbcolor:byte;
P:pbytearray;
Begin
Bmp. PixelFormat: = Pf32bit;
Tcolortorgb (BMP. Canvas.pixels[0,0],rcolor,gcolor,bcolor);
Tcolortorgb (Changetoback,trcolor,tgcolor,tbcolor);
Count: = 4 * bmp. Width-1;
For I: = 0 to BMP. Height-1 do
Begin
J: = 0;
P: = BMP. Scanline[i];
While J < Count do
Begin
if (ABS (P[j]-Bcolor) <=coloroffset) and (ABS (P[j+1]-Gcolor) <= Coloroffset) and (ABS (p[j+2]-Rcolor) <=coloroff Set) Then
Begin
P[J]: = Tbcolor;
P[j+1]: = Tgcolor;
P[J+2]: = Trcolor;
End
Inc (j,4);
End
End
End

Today in the company due to the use of a picture mask effect, so according to the same idea wrote an image mask function:

The code is as follows:

{-------------------------------------------------------------------------------
Procedure name: Softbmp
Author: no cannot
Date: 2009.02.13
Parameters: Bmp:tbitmap;
Darkrect: Non-masked area
Softcolor: Specifying Matte color

Softpercent Specify mask degree (take 1-100, 100 is full matte)

return value: None
-------------------------------------------------------------------------------}

Procedure Softbmp (Bmp:tbitmap;var darkrect:trect;const softcolor:tcolor;const softpercent:integer=50);
Var
I,j:integer;
Pb:pbytearray;
Bmpformatxs:integer;
W,h:integer;
R,g,b:integer;
Begin
If BMP. PixelFormat <> Pf32bit Then
Bmp. PixelFormat: = Pf32bit;
Bmpformatxs: = 4;

W:= Darkrect.right-darkrect.left;
H:= Darkrect.bottom-darkrect.top;

If Darkrect.right > BMP. Width Then
Begin
Darkrect.left:=bmp. Width-w;
Darkrect.right:=bmp. Width;
End
if (Darkrect.bottom > BMP. Height) Then
Begin
darkrect.top:= bmp. Height-h;
Darkrect.bottom:=bmp. Height;
End
If Darkrect.left <0 Then
Begin
darkrect.left:=0;
Darkrect.right:=w;
End
If Darkrect.top <0 Then
Begin
darkrect.top:=0;
Darkrect.bottom:=h;
End
Tcolortorgb (SOFTCOLOR,R,G,B);
For I: = 0 to Darkrect.top-1 do
Begin
Pb:=bmp. Scanline[i];
J: = 0;
While J < Bmpformatxs*bmp. Width-1 do
Begin
PB[J]: = B + (100-softpercent) * (Pb[j]-B) div 100;
Pb[j+1]: = G + (100-softpercent) * (Pb[j+1]-G) div 100;
PB[J+2]: = R + (100-softpercent) * (pb[j+2]-r) div 100;
Inc (J,BMPFORMATXS);
End
End

For I: = Darkrect.top to BMP. Height-1 do
Begin
Pb:=bmp. Scanline[i];
J: = 0;
While J < bmpformatxs*darkrect.left-1 do
Begin
PB[J]: = B + (100-softpercent) * (Pb[j]-B) div 100;
Pb[j+1]: = G + (100-softpercent) * (Pb[j+1]-G) div 100;
PB[J+2]: = R + (100-softpercent) * (pb[j+2]-r) div 100;
Inc (J,BMPFORMATXS);
End
End
For I: = Darkrect.bottom to BMP. Height-1 do
Begin
Pb:=bmp. Scanline[i];
J: = Bmpformatxs*darkrect.left;
While J < Bmpformatxs*bmp. Width-1 do
Begin
PB[J]: = B + (100-softpercent) * (Pb[j]-B) div 100;
Pb[j+1]: = G + (100-softpercent) * (Pb[j+1]-G) div 100;
PB[J+2]: = R + (100-softpercent) * (pb[j+2]-r) div 100;
Inc (J,BMPFORMATXS);
End
End

For I: = Darkrect.top to Darkrect.bottom-1 do
Begin
Pb:=bmp. Scanline[i];
J: = Bmpformatxs*darkrect.right;
While J < Bmpformatxs*bmp. Width-1 do
Begin
PB[J]: = B + (100-softpercent) * (Pb[j]-B) div 100;
Pb[j+1]: = G + (100-softpercent) * (Pb[j+1]-G) div 100;
PB[J+2]: = R + (100-softpercent) * (pb[j+2]-r) div 100;
Inc (J,BMPFORMATXS);
End
End
End

http://blog.csdn.net/suiyunonghen/article/details/3876813

Several functions of image synthesis

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.