AFIN DE COMBINER DEUX IMAGES ENTRE -ELLES SELON UN TAUX DE TRANSPARENCE RÉGLABLE

Caribensila Messages postés 2527 Date d'inscription jeudi 15 janvier 2004 Statut Membre Dernière intervention 16 octobre 2019 - 17 mars 2012 à 02:37
cs_Denis007 Messages postés 22 Date d'inscription vendredi 30 décembre 2005 Statut Membre Dernière intervention 7 octobre 2012 - 18 mars 2012 à 18:00
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/54154-afin-de-combiner-deux-images-entre-elles-selon-un-taux-de-transparence-reglable

cs_Denis007 Messages postés 22 Date d'inscription vendredi 30 décembre 2005 Statut Membre Dernière intervention 7 octobre 2012
18 mars 2012 à 18:00
Pour te répondre les objets n'on pas a être libérer et toutes les déclarations
de la rubrique var sont nécessaires dans cette fonction comme tu peut le voir.
mais je te remercie de m'indiquer la fonction AlphaBlend que je ne connaissait pas.
Cette fonction ajoute un élément de dessin la combinainon de deux autre image et l'unité
précédante permet d'ajuster l'effet en temps réel ainsi il dispose de fonction de validation
comme tu peut le voir.
cs_Denis007 Messages postés 22 Date d'inscription vendredi 30 décembre 2005 Statut Membre Dernière intervention 7 octobre 2012
18 mars 2012 à 17:48
{Voici le code-source complet d'affichage de deux image en temps réel}
unit U_two_trans; {Written by denis bertin le 16.3.2012}

interface

uses windows,whelp,wbase,col_plan,dialbase,messages;

{Cf : wproche.TDisque_window.Boite_magique_Combiner_deux_images}
{Afin de combiner deux images entre -elles selon un taux de transparence réglable.}

type

TTrans_two_image_Dialog = class(Whelp.ThelpDialog)
constructor Create(AParent:WBase.TWindow; une_col_plan:col_plan.TCalque);
procedure SetupWindow; override;
procedure WMCommand(var Msg: TMessage); override;
procedure Combiner_deux_images;
procedure WMHSCROLL(var Msg: TMessage); override;
procedure wmnum_edit_change(var msg:TMessage); override;
private
col_plan:col_plan.TCalque;
Px:dialbase.TNumEdit_plus_moins;
Py:dialbase.TNumEdit_plus_moins;
Pt:dialbase.TNumEdit_plus_moins;
scoll_bar:WBase.TScrollbar;
bool_deja_essayer:boolean;
bool_temp_real:wbase.Tcheckbox;
end;

implementation

uses wformebm,math,g_base,Graphics,wmain,U_fast_bitmap,haide;

const id_bouton_essayer = 1000;
id_scoll_bar = 600;

type

Trec_two_transparent = record
a,b,t:longint;
bool_time_real:bool;
end;

var rec_two_transparent : Trec_two_transparent;

constructor TTrans_two_image_Dialog.Create(AParent:WBase.TWindow; une_col_plan:col_plan.TCalque);
const id_bool_temp_real = 500;
const dial_id_two_image = 1775;
var first_selected,second_selected:wformebm.Tforme_TBitMap;
min_left,min_top,max_right,max_bottom:integer;
begin
inherited Create(AParent,dial_id_two_image,0);
TransferBuffer := @rec_two_transparent;
Px:=dialbase.TNumEdit_plus_moins.Create(self,100,4,1,2000,0);
Px.Enable_Previent_ton_Parent(true);
Py:=dialbase.TNumEdit_plus_moins.Create(self,200,4,1,2000,0);
Py.Enable_Previent_ton_Parent(true);
Pt:=dialbase.TNumEdit_plus_moins.Create(self,300,3,0,100,0);
Pt.Enable_Previent_ton_Parent(true);
bool_temp_real:=wbase.Tcheckbox.create(self,id_bool_temp_real,0);
scoll_bar:=WBase.TScrollbar.Create(self,id_scoll_bar,0);

Self.col_plan:=une_col_plan;
Self.bool_deja_essayer:=false;

first_selected:=wformebm.Tforme_TBitMap(col_plan.first_selection);
second_selected:=wformebm.Tforme_TBitMap(col_plan.last_selection);

min_left:=math.min(first_selected.rect.left,second_selected.rect.left);
min_top:=math.min(first_selected.rect.top,second_selected.rect.top);
max_right:=math.max(first_selected.rect.right,second_selected.rect.right);
max_bottom:=math.max(first_selected.rect.bottom,second_selected.rect.bottom);

rec_two_transparent.b:=round(rec_two_transparent.a*(max_bottom-min_top)/(max_right-min_left));
end;

procedure TTrans_two_image_Dialog.SetupWindow;
begin
inherited SetupWindow;
scoll_bar.SetRange(0,100);
scoll_bar.SetPosition(rec_two_transparent.t);
postmessage(self.hwindow,wm_command,0,0);
end;

procedure TTrans_two_image_Dialog.WMCommand(var Msg: TMessage);
begin
inherited WMCommand(Msg);
case loword(Msg.wparam) of
id_cancel:
begin
if bool_deja_essayer then
begin
col_plan.atfree(pred(col_plan.count));
end;
end;
id_ok:
begin
if not bool_deja_essayer then
Combiner_deux_images;
end;
id_bouton_essayer:
begin
self.TransferData(wbase.TDirT_GetData);
if bool_deja_essayer then
begin
col_plan.atfree(pred(col_plan.count));
end;
Combiner_deux_images;
Self.bool_deja_essayer:=true;
end;
end;

if bool_temp_real.GetCheck=bf_checked then
scoll_bar.show(sw_show)
else
scoll_bar.show(sw_hide);
end; {TTrans_two_image_Dialog.WMCommand}

procedure TTrans_two_image_Dialog.WMHSCROLL(var Msg: TMessage);
begin
inherited WMHSCROLL(Msg);
Pt.set_num_value(scoll_bar.GetPosition);
postmessage(self.hwindow,wm_command,id_bouton_essayer,0);
end; {TTrans_two_image_Dialog.WMHSCROLL}

procedure TTrans_two_image_Dialog.wmnum_edit_change(var msg:TMessage);
begin
if bool_temp_real.GetCheck=bf_checked then
begin
postmessage(self.hwindow,wm_command,id_bouton_essayer,0);
scoll_bar.SetPosition(Pt.Get_num_value);
end;
end;

procedure TTrans_two_image_Dialog.Combiner_deux_images;
var i,j:integer;
first_selected,second_selected:wformebm.Tforme_TBitMap;
une_forme_obtenue:wformebm.Tforme_TBitMap;
min_left,min_top,max_right,max_bottom:integer;
bitmap_ombre:integer;
commentaire,nom_de_la_fonte:string;
BitMap:Graphics.TBitMap;
taille_du_corps_du_texte:integer;
couleur_du_texte,couleur:tcolorref;
digitised:tpoint;
une_couleur,deux_couleur:tcolorref;
x,y:real;
v_transparency:real;
un,deux,trois:U_fast_bitmap.TFastBitmap2;
acursor:hcursor;
xx,yy:integer;
rapport_x,rapport_y:real;
old_show_cursor_when_redraw:boolean;
begin
if not bool_temp_real.GetCheck=bf_checked then
acursor:=setcursor(haide.G_Cursor.wait_cursor);
v_transparency:=rec_two_transparent.t/100;
first_selected:=wformebm.Tforme_TBitMap(col_plan.first_selection);
second_selected:=wformebm.Tforme_TBitMap(col_plan.last_selection);
if (first_selected<>nil) and (second_selected<>nil) then
begin
if (first_selected.ClassType=wformebm.Tforme_TBitMap)
and (second_selected.ClassType=wformebm.Tforme_TBitMap) then
begin
min_left:=math.min(first_selected.rect.left,second_selected.rect.left);
min_top:=math.min(first_selected.rect.top,second_selected.rect.top);
max_right:=math.max(first_selected.rect.right,second_selected.rect.right);
max_bottom:=math.max(first_selected.rect.bottom,second_selected.rect.bottom);

bitmap_ombre:=0;
commentaire:='';
couleur_du_texte:=g_base.rgb_noir;
taille_du_corps_du_texte:=12;
nom_de_la_fonte:='Arial';
BitMap:=Graphics.TBitMap.create;
BitMap.Width:=rec_two_transparent.a;
BitMap.height:=rec_two_transparent.b;
une_forme_obtenue:=wformebm.Tforme_TBitMap.Create( min_left,min_top,max_right,max_bottom,BitMap,bitmap_ombre,
commentaire,taille_du_corps_du_texte,nom_de_la_fonte,couleur_du_texte);
col_plan.Add(une_forme_obtenue);
un.Copy(first_selected.BitMap);
deux.Copy(second_selected.BitMap);
trois.Copy(BitMap);
rapport_x:=1/pred(BitMap.Width)*une_forme_obtenue.la_largeur;
rapport_y:=1/pred(BitMap.height)*une_forme_obtenue.la_hauteur;
for i:=0 to pred(BitMap.Width) do for j:=0 to pred(BitMap.height) do
begin
digitised.x:=une_forme_obtenue.left+round(i*rapport_x);
digitised.y:=une_forme_obtenue.top+round(j*rapport_y);
if ptinrect(first_selected.rect,digitised) then
begin
x:=digitised.x-first_selected.rect.left;
y:=digitised.y-first_selected.rect.top;
x:=x/first_selected.la_largeur*first_selected.BitMap.width;
y:=y/first_selected.la_hauteur*first_selected.BitMap.height;
xx:=round(x); yy:=round(y);
if (xx<un.Bmp.Width) and (yy<un.Bmp.height) then
une_couleur:=un.GetPixel(round(x),round(y))
else
une_couleur:=g_base.RGB_Blanc;
end
else
une_couleur:=g_base.RGB_Blanc;
if ptinrect(second_selected.rect,digitised) then
begin
x:=digitised.x-second_selected.rect.left;
y:=digitised.y-second_selected.rect.top;
x:=x/second_selected.la_largeur*second_selected.BitMap.width;
y:=y/second_selected.la_hauteur*second_selected.BitMap.height;
xx:=round(x); yy:=round(y);
if (xx<deux.Bmp.Width) and (yy<deux.Bmp.height) then
deux_couleur:=deux.GetPixel(xx,yy)
else
une_couleur:=g_base.RGB_Blanc;
end
else
deux_couleur:=g_base.RGB_Blanc;
couleur:=
rgb(math.max(0,math.min(255,round(getrvalue(une_couleur)*v_transparency+getrvalue(deux_couleur)*(1-v_transparency)))),
math.max(0,math.min(255,round(getgvalue(une_couleur)*v_transparency+getgvalue(deux_couleur)*(1-v_transparency)))),
math.max(0,math.min(255,round(getbvalue(une_couleur)*v_transparency+getbvalue(deux_couleur)*(1-v_transparency)))) );
trois.SetPixel(i,j,couleur);
end;

old_show_cursor_when_redraw:=wmain.mainwindow.bool_show_cursor_when_redraw;
wmain.mainwindow.bool_show_cursor_when_redraw:=false;
invalidaterect(wmain.mainwindow.hwindow,nil,false);
updatewindow(wmain.mainwindow.hwindow);
wmain.mainwindow.bool_show_cursor_when_redraw:=old_show_cursor_when_redraw;
end;
end;
if not bool_temp_real.GetCheck=bf_checked then
setcursor(acursor);
end;

begin
with rec_two_transparent do
begin
a:=320; b:=320; t:=50;
bool_time_real:=false;
end;
end.
Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 50
17 mars 2012 à 12:55
c'est effectivement une question que l'on peut se poser ...

1°) le code est inutilisable en l'état et ne
peut même pas servir d'exemple.

2°) quand on réinvente la roue c'est pour l'améliorer et non l'inverse !!!
utiliser "Canvas.Pixels" pour combiner les couleurs dans un code tagué "Initié"
ça fait très très mal. Regarde la fonction "AlphaBlend" de l'unité "Windows.pas"
elle est faite pour ça et est bien plus performante que ton code.

3°) je suis pas certain que tous les objets crées dans ce codes soient bien libérés !!!!

conclusion: cette contribution ne sera pas conservée pour les raison citées plus haut.
Caribensila Messages postés 2527 Date d'inscription jeudi 15 janvier 2004 Statut Membre Dernière intervention 16 octobre 2019 18
17 mars 2012 à 02:37
- Est-ce que le Schmilblick peut-il voler ?
Rejoignez-nous