daily pastebin goal
15%
SHARE
TWEET

Merges2Images.au3 - rev B.0

a guest Sep 25th, 2012 127 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Region ;**** Directives created by AutoIt3Wrapper_GUI ****
  2. #AutoIt3Wrapper_Compression=0
  3. #AutoIt3Wrapper_UseUpx=n
  4. #AutoIt3Wrapper_UseX64=n
  5. #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
  6. #include <GDIPlus.au3>
  7. #include <GuiConstants.au3>
  8. Opt("GUIOnEventMode", 1) ;0=disabled, 1=OnEvent mode enabled
  9.  
  10. $ScriptTitle = @ScriptName ; "Merge2Images"
  11. $ScriptRev = "B.0"
  12.  
  13. Global $Title = $ScriptTitle & " - " & $ScriptRev
  14. Local $ShowMergeGUIWindow, $SleepTimeShowMergeGUIWindow, $MergedImageBackgroundColor, $ShowFinaleMergedImage, $CenterImageVertically, $StrechImageVertically, $GuiSizeX, $GuiSizeY, $FirstPicName, $ImageName1, $ImageName2, $ImageCompositeName, $MergeImageMode, $MergeExtension, $MergeMode
  15.  
  16. ; -----------------------------------8<-----------------------------------
  17. ;For debugging purpose only, please do not modify this !
  18. $ShowMergeGUIWindow = False
  19. $SleepTimeShowMergeGUIWindow = 2000
  20. ;For debugging purpose only, please do not modify this !
  21. ; -----------------------------------8<-----------------------------------
  22.  
  23. ; -----------------------------------8<-----------------------------------
  24. ; TBD (not specially in this order) :
  25. ;     - Define size of merged image X * Y ;Done, but FTM wondering how to use it.
  26. ;     - Center Horizontally (X) images ? ;Done ;... need "Define size of merged image X * Y" predefined ! ;No.
  27. ;     - Strech Horizontally (X) images ? ;Done ;... need "Define size of merged image X * Y" predefined ! ;No.
  28. ; -----------------------------------8<-----------------------------------
  29.  
  30.  
  31. $MergedImageBackgroundColor = 0x00000000 ;Specify 00 on first "digits" for transparent color (.png only !), all other values (......) will be ignored.
  32. $ShowFinaleMergedImage = False
  33. $CenterImageVertically = False
  34. $StrechImageVertically = False
  35. $CenterImageHorizontally = False
  36. $StrechImageHorizontally = False
  37.  
  38. $GuiSizeX = 0
  39. $GuiSizeY = 0
  40.  
  41. $FirstPicName = 0 ;1 = First pic first / 2 = First pic second
  42. $ImageName1 = ""
  43. $ImageName2 = ""
  44. $ImageCompositeName = ""
  45. $MergeImageMode = 0 ;0 New Image / 1 Merge on 1st / 2 Merge on 2nd
  46. $MergeExtension = ".png" ; .jpg -or- .png
  47. $MergeMode = 0 ;0 = Merge vertically (add on same line) / 1 = Merge horizontally (add on same colon)
  48.  
  49. $temp = 1 ;Mise en place de la configuration
  50. $Ub = UBound($CmdLine)
  51. While $temp < $Ub ;Paramètres passés en raccourcis
  52.     Switch $CmdLine[$temp]
  53.     Case "?", "/?", "-?", "--?", "h", "/h", "-h", "--h", "help", "/help", "-help", "--help"
  54.         MsgBox(0+48,$Title,"Usage is:" & @CR & _
  55.             @TAB & @ScriptName & "    F:\ull\path\to\ImageName1<.png|.jpg>    [F:\ull\path\to\ImageName2<.png|.jpg>    [F:\ull\path\to\MergedImageName<.png|.jpg>]]" & @CR & @CR & _
  56.             "Size of MergedImage destination file will be on Vertically merged mode:" & @CR & _
  57.             @TAB & "X Size = " & @TAB & "X ImageName1 size    +    X ImageName2" & @CR & _
  58.             @TAB & "Y Size = " & @TAB & "Size Max [ Y ImageName1    ,    Y ImageName2]")
  59.         Exit
  60.     Case "s", "/s", "-s", "--s", "source", "/source", "-source", "--source", "o", "/o", "-o", "--o", "origine", "/origine", "-origine", "--origine"
  61.         If $temp < $Ub - 1 Then
  62.             $temp += 1
  63.             $ImageName1 = $CmdLine[$temp]
  64.         EndIf
  65.         If $FirstPicName = 0 Then $FirstPicName = 1
  66.     Case "a", "/a", "-a", "--a", "/alt", "-alt", "--alt", "/alternative", "-alternative", "--alternative"
  67.         If $temp < $Ub - 1 Then
  68.             $temp += 1
  69.             $ImageName1 = $CmdLine[$temp]
  70.         EndIf
  71.         If $FirstPicName = 0 Then $FirstPicName = 2
  72.     Case "d", "/d", "-d", "--d", "dest", "/dest", "-dest", "--dest", "destination", "/destination", "-destination", "--destination"
  73.         If $temp < $Ub - 1 Then
  74.             $temp += 1
  75.             $ImageCompositeName = $CmdLine[$temp]
  76.         EndIf
  77.     Case "imagemode", "/imagemode", "-imagemode", "--imagemode", "mergeimagemode", "/mergeimagemode", "-mergeimagemode", "--mergeimagemode"
  78.         If $temp < $Ub - 1 Then
  79.             $temp += 1
  80.             $MergeImageMode = $CmdLine[$temp]
  81.         EndIf
  82.     Case "extension", "mergeextension" ;etc / - --.
  83.         If $temp < $Ub - 1 Then
  84.             $temp += 1
  85.             $MergeExtension = $CmdLine[$temp]
  86.         EndIf
  87.     Case "mode", "mergemode"
  88.         If $temp < $Ub - 1 Then
  89.             $temp += 1
  90.             $MergeMode = $CmdLine[$temp]
  91.         EndIf
  92.     Case "firstpicname"
  93.         If $temp < $Ub - 1 Then
  94.             $temp += 1
  95.             $FirstPicName = $CmdLine[$temp]
  96.         EndIf
  97.     Case "showmergeguiwindow"
  98.         If $temp < $Ub - 1 Then
  99.             $temp += 1
  100.             $ShowMergeGUIWindow = $CmdLine[$temp]
  101.         EndIf
  102.     Case "stretchimagevertically"
  103.         If $temp < $Ub - 1 Then
  104.             $temp += 1
  105.             $StrechImageVertically = $CmdLine[$temp]
  106.         EndIf
  107.     Case "stretchimagehorizontally"
  108.         If $temp < $Ub - 1 Then
  109.             $temp += 1
  110.             $StrechImageHorizontally = $CmdLine[$temp]
  111.         EndIf
  112.     Case "centerimagevertically"
  113.         If $temp < $Ub - 1 Then
  114.             $temp += 1
  115.             $CenterImageVertically = $CmdLine[$temp]
  116.         EndIf
  117.     Case "centerimagehorizontally"
  118.         If $temp < $Ub - 1 Then
  119.             $temp += 1
  120.             $CenterImageHorizontally = $CmdLine[$temp]
  121.         EndIf
  122.     Case "showfinallymergedimage"
  123.         If $temp < $Ub - 1 Then
  124.             $temp += 1
  125.             $ShowFinaleMergedImage = $CmdLine[$temp]
  126.         EndIf
  127.     Case "mergedimagebackgroundcolor"
  128.         If $temp < $Ub - 1 Then
  129.             $temp += 1
  130.             $MergedImageBackgroundColor = $CmdLine[$temp]
  131.         EndIf
  132.     Case "w", "x", "sizex", "guisizex"
  133.         If $temp < $Ub - 1 Then
  134.             $temp += 1
  135.             $GuiSizeX = $CmdLine[$temp]
  136.         EndIf
  137.     Case "h", "y", "sizey", "guisizey"
  138.         If $temp < $Ub - 1 Then
  139.             $temp += 1
  140.             $GuiSizeY = $CmdLine[$temp]
  141.         EndIf
  142.     ;You shall keep at least one "" (blank) Case, in order to keep the Else Case safe.
  143.     Case ""
  144. ;~         If $temp < $Ub - 1 Then
  145. ;~             $temp += 1
  146. ;~             $ = $CmdLine[$temp]
  147. ;~         EndIf
  148.     Case Else
  149.         If $ImageName1 = "" Then
  150.             $ImageName1 = $CmdLine[$temp]
  151.             If $FirstPicName = 0 Then $FirstPicName = 1
  152.         ElseIf $ImageName2 = "" Then
  153.             $ImageName2 = $CmdLine[$temp]
  154.         ElseIf $ImageCompositeName = "" Then
  155.             $ImageCompositeName = $CmdLine[$temp]
  156.         EndIf
  157.     EndSwitch
  158.     $temp += 1
  159. WEnd
  160.  
  161. ; -----------------------------------8)-----------------------------------
  162. ;Traitement des résidus
  163. If $ImageName1 = "" Then ;Résidu : pas de source
  164.     If $ImageName2 = "" Then ;Résidu : pas d'alternative non-plus (rien à traiter)
  165.         $ImageName1 = FileOpenDialog(@ScriptName & " - Image source ?", "", "Images (*.jpg; *.png)")
  166.         $ImageName2 = FileOpenDialog(@ScriptName & " - Image alternative ?", "", "Images (*.jpg; *.png)")
  167.         $ImageCompositeName = FileOpenDialog(@ScriptName & " - Image destination ?", "", "Images (*.jpg; *.png)")
  168.         $FirstPicName = 1
  169.     Else
  170.         $ImageName1 = $ImageName2 ;Résidu : seulement l'alternative
  171.     EndIf
  172. Else
  173.     If $ImageName2 = "" Then
  174.         $ImageName2 = $ImageName1 ;Résidu : seulement la source
  175.     EndIf
  176. EndIf
  177.  
  178. If $ImageCompositeName = "" Then ;Résidu : pas de destination renseignée
  179.     If $ImageName1 = $ImageName2 And $MergeImageMode <> 0 Then
  180.         $ImageCompositeName = $ImageName1 ;Résidu : destination unique en écrasement
  181.     ElseIf $MergeImageMode = 1 Then
  182.         $ImageCompositeName = $ImageName1 ;Résidu : destination unique source
  183.     ElseIf $MergeImageMode = 2 Then
  184.         $ImageCompositeName = $ImageName2 ;Résidu : destination unique alternative
  185.     Else
  186.         If Not $FirstPicName = 1 Then
  187.             $ImageCompositeName = $ImageName2 & "+" & $ImageName1 & $MergeExtension ;Résidu : destination=alternative+source.ext
  188.         Else
  189.             $ImageCompositeName = $ImageName1 & "+" & $ImageName2 & $MergeExtension ;Résidu : destination=source+alternative.ext
  190.         EndIf
  191.     EndIf
  192. EndIf
  193. ; -----------------------------------8)-----------------------------------
  194.  
  195. _GDIPlus_Startup()
  196.  
  197. ; Load image 1
  198. $hImage1 = _GDIPlus_ImageLoadFromFile($ImageName1)
  199. $iX1 = _GDIPlus_ImageGetWidth($hImage1)
  200. If $iX1 = 4294967295 Then $iX1 = 0 ;4294967295 en cas d'erreur, soit 32 bits à 1 (11111...1111111).
  201. $iY1 = _GDIPlus_ImageGetHeight($hImage1)
  202.  
  203. ; Load image 2
  204. $hImage2 = _GDIPlus_ImageLoadFromFile($ImageName2)
  205. $iX2 = _GDIPlus_ImageGetWidth($hImage2)
  206. If $iX2 = 4294967295 Then $iX2 = 0 ;4294967295 en cas d'erreur, soit 32 bits à 1 (11111...1111111).
  207. $iY2 = _GDIPlus_ImageGetHeight($hImage2)
  208.  
  209. ; Define the Merged (Composite) image size
  210. If $MergeMode = 0 Then ;Horizontally merge
  211.     If $GuiSizeX = 0 Then $GuiSizeX = $iX1 + $iX2
  212.     If $GuiSizeY = 0 Then
  213.         If $iY1 > $iY2 then
  214.             $GuiSizeY = $iY1
  215.         Else
  216.             $GuiSizeY = $iY2
  217.         EndIf
  218.     EndIf
  219.     $VPos1 = 0
  220.     $HPos1 = 0
  221.     $XStrech1 = $iX1
  222.     $YStrech1 = $iY1
  223.     If $StrechImageVertically Then $YStrech1 = $GuiSizeY
  224.     If $CenterImageVertically and Not $StrechImageVertically Then $VPos1 = ($GuiSizeY - $iY1)/2
  225.     $VPos2 = 0
  226.     $HPos2 = $XStrech1 ;/!\
  227.     $XStrech2 = $iX2
  228.     $YStrech2 = $iY2
  229.     If $StrechImageVertically Then $YStrech2 = $GuiSizeY
  230.     If $CenterImageVertically and Not $StrechImageVertically Then $VPos2 = ($GuiSizeY - $iY2)/2
  231. Else ;Vertically merge
  232.     If $GuiSizeY = 0 Then $GuiSizeY = $iY1 + $iY2
  233.     If $GuiSizeX = 0 Then
  234.         If $iX1 > $iX2 then
  235.             $GuiSizeX = $iX1
  236.         Else
  237.             $GuiSizeX = $iX2
  238.         EndIf
  239.     EndIf
  240.     $VPos1 = 0
  241.     $HPos1 = 0
  242.     $XStrech1 = $iX1
  243.     $YStrech1 = $iY1
  244.     If $StrechImageHorizontally Then $XStrech1 = $GuiSizeX
  245.     If $CenterImageHorizontally and Not $StrechImageHorizontally Then $HPos1 = ($GuiSizeX - $iX1)/2
  246.     $VPos2 = $YStrech1 ;/!\
  247.     $HPos2 = 0
  248.     $XStrech2 = $iX2
  249.     $YStrech2 = $iY2
  250.     If $StrechImageHorizontally Then $XStrech2 = $GuiSizeX
  251.     If $CenterImageHorizontally and Not $StrechImageHorizontally Then $HPos2 = ($GuiSizeX - $iX2)/2
  252. EndIf
  253.  
  254. ; Initialise the Drawing windows/composite image...
  255. $hGui = GUICreate("GDIPlus Example", $GuiSizeX, $GuiSizeY)
  256. ; Show drawing window ?
  257. If $ShowMergeGUIWindow Then
  258.     GUISetState()
  259.     GUISetOnEvent(-3, "_Close")
  260. EndIf
  261.  
  262. ; Create Double Buffer, so the doesn't need to be repainted on PAINT-Event
  263. $hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGui) ;Draw to this graphics, $hGraphicGUI, to display on GUI
  264. $hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($GuiSizeX, $GuiSizeY, $hGraphicGUI) ; $hBMPBuff is a bitmap in memory
  265. $hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff) ; Draw to this graphics, $hGraphic, being the graphics of $hBMPBuff
  266.  
  267. ;Fill the Graphic Background (0x00000000 for transparent background in .png files)
  268. _GDIPlus_GraphicsClear($hGraphic, $MergedImageBackgroundColor)
  269.  
  270. ;Put the previously opened images together on the same Graphic and closing them
  271. _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage1, 0, 0, $iX1, $iY1, $HPos1, $VPos1, $XStrech1, $YStrech1)
  272. _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage2, 0, 0, $iX2, $iY2, $HPos2, $VPos2, $XStrech2, $YStrech2)
  273. _GDIPlus_ImageDispose($hImage1)
  274. _GDIPlus_ImageDispose($hImage2)
  275.  
  276. ; Save composite image (need that previously opened images have been closed if self-merging a file)
  277. Local $sNewName = $ImageCompositeName
  278. _GDIPlus_ImageSaveToFile($hBMPBuff, $sNewName) ; $hBMPBuff the bitmap
  279.  
  280. ; Show drawing window ?
  281. If $ShowMergeGUIWindow Then
  282.     _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0) ; Draw bitmap, $hBMPBuff, to the GUI's graphics, $hGraphicGUI.
  283.     Sleep($SleepTimeShowMergeGUIWindow) ; Wait $SleepTimeShowMergeGUIWindow ms, or press Esc.
  284. EndIf
  285.  
  286. ; Show result saved file ?
  287. If $ShowFinaleMergedImage Then ShellExecute($sNewName)
  288.  
  289. _Quit()
  290.  
  291.  
  292. Func _Close() ;Close the previously opened images, only called when gui window is manually closed
  293.     _GDIPlus_ImageDispose($hImage1)
  294.     _GDIPlus_ImageDispose($hImage2)
  295.     _Quit()
  296. EndFunc
  297.  
  298. Func _Quit()
  299.     _GDIPlus_GraphicsDispose($hGraphic)
  300.     _GDIPlus_GraphicsDispose($hGraphicGUI)
  301.     _WinAPI_DeleteObject($hBMPBuff)
  302.     _GDIPlus_Shutdown()
  303.     Exit
  304. EndFunc     ;==>_Quit
  305.  
  306.  
  307. ; -----------------------------------8<-----------------------------------
  308. #comments-start
  309. Global $IniFile = StringTrimRight(@ScriptFullPath, 4) & ".ini"
  310. ;;This is the content of the original IniFile;;
  311. [Config]
  312.  
  313. MergedImageBackgroundColor= 0xFF000000
  314.     #~ 0x00...... : Specify 00 on first "digits" for transparent color (.png only !), all other values (......) will be ignored.
  315.     #~ 0xFFRRGGBB : RGB : RR=red color level / GG=green color level / BB=blue color level
  316.     #~ 0xFFFFFFFF : white
  317.     #~ 0xFF000000 : black
  318.     #~ 0xFF0000FF : blue
  319.  
  320. CenterImageVertically = True
  321. StrechImageVertically = False
  322.  
  323. ShowFinaleMergedImage = true
  324.  
  325. #~ -----------------------------------8<-----------------------------------
  326. #~ For Debug only (you should not touch this !):
  327. [Debug]
  328. ShowMergeGUIWindow= False
  329. SleepTimeShowMergeGUIWindow     = 2000
  330. ;;End of the original content of the IniFile;;
  331. #comments-end
  332. ; -----------------------------------8<-----------------------------------
  333.  
  334. ; -----------------------------------8<-----------------------------------
  335. ; Fill Graphics zone in black color : _GDIPlus_GraphicsClear($hGraphic)
  336. ; Fill Graphics zone in white color : _GDIPlus_GraphicsClear($hGraphic, 0xFFFFFFFF)
  337. ; Fill Graphics zone in transparent : <nothing if Merged ImageExt is .png, default back color if Merged ImageExt is .jpg>
  338. ; _GDIPlus_GraphicsClear($hGraphic, $MergedImageBackgroundColor)
  339. ; -----------------------------------8<-----------------------------------
  340.  
  341. ; -----------------------------------8<-----------------------------------
  342. ;~ ; Merge 2 images (default)
  343. ;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage1, 0, 0, $iX1, $iY1,    0, 0, $iX1, $iY1)
  344. ;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage2, 0, 0, $iX2, $iY2, $iX1, 0, $iX2, $iY2)
  345.  
  346.  
  347. ; Image 2 is VCentered
  348. ;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage1, 0, 0, $iX1, $iY1,    0, 0, $iX1, $iY1)
  349. ;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage2, 0, 0, $iX2, $iY2, $iX1, ($iY1-$iY2)/2, $iX2, $iY2)
  350.  
  351.  
  352. ; VCenter both images
  353. ;~ $VPos = 0
  354. ;~ If $CenterImageVertically Then $VPos = ($GuiSizeY - $iY1)/2
  355. ;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage1, 0, 0, $iX1, $iY1,    0, $VPos, $iX1, $iY1)
  356. ;~ If $CenterImageVertically Then $VPos = ($GuiSizeY - $iY2)/2
  357. ;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage2, 0, 0, $iX2, $iY2, $iX1, $VPos, $iX2, $iY2)
  358.  
  359. ;~ Vertical Strech
  360. ;~ $YStrech = $iY1
  361. ;~ If $StrechImageVertically Then $YStrech = $GuiSizeY
  362. ;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage1, 0, 0, $iX1, $iY1,    0, 0, $iX1, $YStrech)
  363. ;~ $YStrech = $iY2
  364. ;~ If $StrechImageVertically Then $YStrech = $GuiSizeY
  365. ;~ _GDIPlus_GraphicsDrawImageRectRect($hGraphic, $hImage2, 0, 0, $iX2, $iY2, $iX1, 0, $iX2, $YStrech)
  366. ; -----------------------------------8<-----------------------------------
  367.  
  368. ; -----------------------------------8<-----------------------------------
  369. ;Func to auto-redraw on Windows internal PAINT messages.
  370. Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
  371.     _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)
  372.     ;_WinAPI_RedrawWindow($hGui, "", "", BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME)) ; , $RDW_ALLCHILDREN
  373.     Return $GUI_RUNDEFMSG
  374. EndFunc     ;==>MY_PAINT
  375. ; -----------------------------------8<-----------------------------------
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top