
Pour mon prog en cours et en vue d'une lib, j'avais besoin de créer des effets sur des images.
Je me suis orienté vers le stockage des pixels dans un tableau afin de ne pas avoir à réanalyser l'image source à chaque effet.
Je pense que ça pourra servir. N'hésitez pas à proposer d'autres effets (en restant dans l'utilisation du tableau).
Effets :
1.1 : conversion en tableau à 2 dimensions
> Créer une image 'bruit' en mixant les pixels de façon aléatoire
1.0
> Symétrie Horizontale
> Symétrie Verticale
> Convertir l'image en 2 couleurs
> Convertir l'image en Gris
> Applique un effet miroir avec la partie gauche de l'image
> Applique un effet miroir avec la partie droite de l'image
> Retourne l'image à 180 °
> Pivote l'image de 90° vers la gauche
> Pivote l'image de 90° vers la droite
L'image pour l'exemple

A récupérer (png) : ICI (enregistrer la cible du lien sous)
Le code et les infos :
Code : Tout sélectionner
; Ar-S FX Lib 1.1 - Multi Effets sur image par Ar-S // 2013
; Merci à Dobro ;)
; Uniquement en jouant sur un tableau à 2 dimensions contenant les couleurs des pixels
;
; PRINCIPE
;
; 1 - Créer un tableau à 2 dimensions
; 2 - Stocker les couleurs de chaque pixels dans ce tableau
; 3 - Appliquer un effet
;
;
; INSTRUCTIONS
;
; StockPixel(image_originale)
; ARS_FX(image_a_traiter, Flag)
;
; > Liste des Flag
;
; #Image_Flip_X : Symétrie Horizontale
; #Image_Flip_Y : Symétrie Verticale
; #Image_2colors : Convertir l'image en 2 couleurs
; #Image_Gray : Convertir l'image en Gris
; #Image_Mirror_L : Applique un effet miroir avec la partie gauche de l'image
; #Image_Mirror_R : Applique un effet miroir avec la partie droite de l'image
; #Image_Rotate_180 : Retourne l'image à 180 °
; #Image_Rotate_90L : Pivote l'image de 90° vers la gauche
; #Image_Rotate_90R : Pivote l'image de 90° vers la droite
; #Image_MixPixel : Crée une image 'bruit' en mixant les pixels
;
; ------- IMPORTANT --------
; Pour les Flag #Image_Rotate_90L et #Image_Rotate_90R
; Vous devez AVOIR CREE AU PREALABLE une image vierge aux bonnes dimensions (largeur devient hauteur et hauteur devient largeur)
; ici #IMAGE_FX_90
;
; ************ INITIALISATION **********************
UsePNGImageDecoder()
UsePNGImageEncoder()
Enumeration 10
#IMG_ORIGINALE ; Image originale
#IMAGE_FX ; Image cible
#IMAGE_FX_90 ; pour l'image retournée qui nécessitera une taille différente de l'originale
EndEnumeration
#Image_Flip_X = 1
#Image_Flip_Y = 2
#Image_Rotate_90L = 3
#Image_Rotate_90R = 4
#Image_2colors = 5
#Image_Gray = 6
#Image_Mirror_L = 7
#Image_Mirror_R = 8
#Image_Rotate_180 = 9
#Image_MixPixel = 10
Declare StockPixelTab(IMG)
Declare MixPixel()
Declare ARS_FX(IMG, FX)
Lwin = 741 : Hwin = 480
Global Canvas_MAX_L = Lwin - 101
Global Canvas_MAX_H = Hwin
Global Dim TabPixel(1,1)
Global Dim TabMix(1,1)
Global Diff = 54
; ********Exemple**********
; ********************* PROGRAMME **********************
LoadImage(#IMG_ORIGINALE,GetCurrentDirectory()+"pb.png")
CreateImage(#IMAGE_FX, ImageWidth(#IMG_ORIGINALE), ImageHeight(#IMG_ORIGINALE))
CreateImage(#IMAGE_FX_90, ImageHeight(#IMG_ORIGINALE), ImageWidth(#IMG_ORIGINALE))
StockPixelTab(#IMG_ORIGINALE)
hwnd = OpenWindow(0, #PB_Any, #PB_Any, Lwin, Hwin, "Image FX Lib by Ar-S // 2013", #PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered)
StickyWindow(0,1)
Clic = 0
Clac = 0
CanvasGadget(1, 101, 1, Canvas_MAX_L, Canvas_MAX_H , #PB_Canvas_DrawFocus|#PB_Canvas_ClipMouse)
StartDrawing(CanvasOutput(1))
DrawingMode(#PB_2DDrawing_AlphaBlend)
Box(0,0, Canvas_MAX_L, Canvas_MAX_H, $FFFFFF)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(5,5, "Vide", $0)
StopDrawing()
ButtonGadget(2,1,1,100,29,"Image originale")
ButtonGadget(3,1,30,100,29,"Niveau de Gris")
ButtonGadget(4,1,60,100,29,"2 Couleurs ")
TrackBarGadget(5, 45, 90, 20, 100, 0, 255, #PB_TrackBar_Vertical)
ButtonGadget(6,1,200,100,29,"Rotation 180° ")
ButtonGadget(7,1,230,100,29,"Sym Verticale ")
ButtonGadget(8,1,260,100,29,"Sym Horizontale ")
ButtonGadget(9,1,290,100,29,"90° gauche")
ButtonGadget(10,1,320,100,29,"Miroir Droite")
ButtonGadget(11,1,350,100,29,"Mixer les pixels")
SetGadgetState(5, Diff)
; ************ EVENT et BOUCLE **********************
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget
Select EventGadget()
Case 2 ; Voir Image originale
StartDrawing(CanvasOutput(1))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawImage(ImageID(#IMG_ORIGINALE),0,0)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(5,5, "Image originale", $0)
StopDrawing()
Case 3 ; Voir Image GRIS
ARS_FX(#IMAGE_FX, #Image_Gray)
StartDrawing(CanvasOutput(1))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawImage(ImageID(#IMAGE_FX),0,0)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(5,5, "Image Gray", $0)
StopDrawing()
Case 4 ; Voir image 2 COL
start = ElapsedMilliseconds()
ARS_FX(#IMAGE_FX, #Image_2colors)
endms = ElapsedMilliseconds() - start
Debug endms
StartDrawing(CanvasOutput(1))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawImage(ImageID(#IMAGE_FX),0,0)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(5,5, "Image 2 col : Diff = "+Str(Diff), $0)
StopDrawing()
;
Case 5 ; Réglage de l'intensité (valeur de Diff) via la trackbar en mode 2 couleurs
Diff = GetGadgetState(5)
ARS_FX(#IMAGE_FX, #Image_2colors)
StartDrawing(CanvasOutput(1))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawImage(ImageID(#IMAGE_FX),0,0)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(5,5, "Image 2 col : Diff = "+Str(Diff), $0)
StopDrawing()
Case 6 ; Rotation 180 °
ARS_FX(#IMAGE_FX, #Image_Rotate_180)
StartDrawing(CanvasOutput(1))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawImage(ImageID(#IMAGE_FX),0,0)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(105,ImageHeight(#IMAGE_FX)-30, "Rotation 180°", $0)
StopDrawing()
Case 7 ; Symétrie Verticale
ARS_FX(#IMAGE_FX, #Image_Flip_Y)
StartDrawing(CanvasOutput(1))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawImage(ImageID(#IMAGE_FX),0,0)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(5,ImageHeight(#IMAGE_FX)-30, "Symétrie Verticale", $0)
StopDrawing()
Case 8 ; Symétrie Horizontale
ARS_FX(#IMAGE_FX, #Image_Flip_X)
StartDrawing(CanvasOutput(1))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawImage(ImageID(#IMAGE_FX),0,0)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(105,5, "Symétrie Horizontale", $0)
StopDrawing()
Case 9 ; Rotation 90° de l'image
If clic = 0
ARS_FX(#IMAGE_FX_90, #Image_Rotate_90L)
clic +1
SetGadgetText(9, "90° Droite")
Else
ARS_FX(#IMAGE_FX_90, #Image_Rotate_90R)
clic = 0
SetGadgetText(9, "90° Gauche")
EndIf
StartDrawing(CanvasOutput(1))
Box (0,0,GadgetWidth(1), GadgetHeight(1),$FFFFFF)
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawImage(ImageID(#IMAGE_FX_90),0,0)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(105,5, "Rotation 90°", $0)
StopDrawing()
Case 10 ; Mirroring
If Clac = 0
ARS_FX(#IMAGE_FX, #Image_Mirror_L)
Clac +1
MirTXT.s = " Droite"
SetGadgetText(10, "Miroir Gauche")
Else
ARS_FX(#IMAGE_FX, #Image_Mirror_R)
Clac = 0
MirTXT.s = " Gauche"
SetGadgetText(10, "Miroir Droite")
EndIf
StartDrawing(CanvasOutput(1))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawImage(ImageID(#IMAGE_FX),0,0)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(105,5, "Miroir" +MirTXT, $0)
StopDrawing()
Case 11 ; Mixer les Pixels
MixPixel()
ARS_FX(#IMAGE_FX, #Image_MixPixel)
StartDrawing(CanvasOutput(1))
DrawingMode(#PB_2DDrawing_AlphaBlend)
DrawImage(ImageID(#IMAGE_FX),0,0)
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(5,ImageHeight(#IMAGE_FX)-30, "Mixer les pixels", $0)
StopDrawing()
EndSelect
Case #PB_Event_CloseWindow
Select EventWindow()
Case 0
quite=1
EndSelect
EndSelect
Until quite=1
For i = #IMG_ORIGINALE To #IMAGE_FX_90
If IsImage(i)
FreeImage(i)
EndIf
Next
End
; ************ PROCEDURES **********************
Procedure StockPixelTab(IMG)
Protected Couleur
DimX = ImageWidth(IMG)
DimY = ImageHeight(IMG)
Dim TabPixel(DimX, DimY)
Dim TabMix( DimX,DimY )
StartDrawing(ImageOutput(IMG))
For y = 0 To DimY-1
For x = 0 To DimX-1
TabPixel(x,y) = Point (x,y)
Next x
Next y
StopDrawing()
EndProcedure
Procedure MixPixel()
Dim TabMix (ArraySize (TabPixel(),1 ), ArraySize (TabPixel(),2) )
CopyArray(TabPixel(), TabMix())
RandomizeArray(TabMix())
EndProcedure
Procedure ARS_FX(IMG, FX)
; Effets multiples sur image.
Protected Image_Grab
If IsImage(IMG) = 0
ProcedureReturn 0
EndIf
StartDrawing(ImageOutput(IMG))
DrawImage(ImageID(IMG),0,0)
Select FX
Case #Image_2colors
For y = 0 To ImageHeight(IMG)-1
For x = 0 To ImageWidth(IMG)-1
Couleur = TabPixel(x,y)
R = Red(Couleur)
G = Green(Couleur)
B = Blue(Couleur)
Gray = (R+G+B)/3
If Gray >= Diff
Gray = 255
Else
;Gray = (R+G+B)/3 ; Blanc jusqu'à niveau de gris
Gray = 10 ; Blanc jusqu'à (presque) noire
EndIf
NewCOLOR = RGBA(Gray,Gray,Gray,255)
Plot (x, y , NewCOLOR)
Next x
Next y
Case #Image_Flip_X
For y = 0 To ImageHeight(IMG)-1
For x = ImageWidth(IMG)-1 To 0 Step -1
Plot ((ImageWidth(IMG)-1)-x, y , TabPixel(x,y))
Next x
Next y
Case #Image_Flip_Y
For y = 0 To ImageHeight(IMG)-1
For x = 0 To ImageWidth(IMG)-1
Plot (x, (ImageHeight(IMG)-1)-y , TabPixel(x,y))
Next x
Next y
Case #Image_Gray
For y = 0 To ImageHeight(IMG)-1
For x = 0 To ImageWidth(IMG)-1
R = Red(TabPixel(x,y))
G = Green(TabPixel(x,y))
B = Blue(TabPixel(x,y))
Gray = (R+G+B) / 3
Plot (x, y , RGBA(Gray, Gray, Gray, 255))
Next x
Next y
Case #Image_Mirror_R
Image_Grab = GrabImage(#IMG_ORIGINALE, #PB_Any, 0, 0, ImageWidth(IMG) / 2, ImageHeight(IMG))
Box (0,0,ImageWidth(IMG), ImageHeight(IMG))
DrawImage(ImageID(Image_Grab),0,0)
For y = 0 To ImageHeight(IMG)-1
For x = ImageWidth(IMG)-1 To 0 Step -1
Plot ((ImageWidth(IMG)-1)-x, y , TabPixel(x,y))
Next x
Next y
DrawImage(ImageID(Image_Grab),0,0)
FreeImage(Image_Grab)
Case #Image_Mirror_L
Image_Grab = GrabImage(#IMG_ORIGINALE, #PB_Any, ImageWidth(IMG) /2, 0, ImageWidth(IMG) / 2, ImageHeight(IMG))
Box (0,0,ImageWidth(IMG), ImageHeight(IMG))
DrawImage(ImageID(Image_Grab),ImageWidth(IMG) /2 ,0)
For y = 0 To ImageHeight(IMG)-1
For x = ImageWidth(IMG)-1 To 0 Step -1
Plot ((ImageWidth(IMG)-1)-x, y , TabPixel(x,y))
Next x
Next y
DrawImage(ImageID(Image_Grab),ImageWidth(IMG) /2 ,0)
FreeImage(Image_Grab)
Case #Image_Rotate_180
For y = ImageHeight(IMG)-1 To 0 Step -1
For x = ImageWidth(IMG)-1 To 0 Step -1
Plot ((ImageWidth(IMG)-1)-x, y , TabPixel(x,y))
Next x
Next y
Case #Image_Rotate_90L
For x = 0 To ImageWidth(IMG)-1
For y = ImageHeight(IMG)-1 To 0 Step -1
Plot (x, y , TabPixel(y,x))
Next y
Next x
Case #Image_Rotate_90R
For x = ImageWidth(IMG)-1 To 0 Step -1
For y = 0 To ImageHeight(IMG)-1
Plot ((ImageWidth(IMG)-1)-x, y , TabPixel(y,x))
Next y
Next x
Case #Image_MixPixel
For y = 0 To ImageHeight(IMG)-1
For x = ImageWidth(IMG)-1 To 0 Step -1
Plot (x, y , TabMix(x,y))
Next x
Next y
FreeArray(TabMix())
Default
StopDrawing()
ProcedureReturn 0
EndSelect
StopDrawing()
ProcedureReturn 1
EndProcedure