Ooops
November 28, 2020, 02:36:45 AM

Author Topic: [bb] GenImage by Perturbatio [ 1+ years ago ]  (Read 650 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] GenImage by Perturbatio [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : GenImage
Author : Perturbatio
Posted : 1+ years ago

Description : A simple piece of code to generate opposite direction animations.  i.e. you have a file with only the left directions drawn, run it through this, and it will produce frames facing the opposite direction but they will be in the same order as the original ones.

i.e.
[1<][2<][3<][4<][5<][6<]
will become:
[1<][2<][3<][4<][5<][6<][>1][>2][>3][>4][>5][>6]


Code :
Code: BlitzBasic
  1. Graphics 640,480,32,2
  2.  
  3. ;setting these here so that I can modify this later to work with commandline or drag n drop
  4. Global ImageName$ = "elfmoves.bmp"
  5. Global FrameWidth% = 32
  6. Global FrameHeight% = 32
  7. Global FirstFrame% = 0
  8. Global tempImage = LoadImage(ImageName)
  9. Global FrameCount% = ImageWidth(tempImage)/FrameWidth
  10. Global OutImageName$ = "new_" + Left(ImageName$, Len(ImageName$)-Len(ExtractFileExt(ImageName$))-1)+".bmp"
  11. Global XOffset = FrameCount * FrameWidth
  12.  
  13.         FreeImage tempImage
  14.         tempImage = 0
  15.  
  16. ;images
  17. Global inImage = LoadAnimImage(ImageName, FrameWidth , FrameHeight, FirstFrame, FrameCount)
  18. Global NewImage = CreateImage((FrameWidth * FrameCount)*2, FrameHeight, FrameCount * 2)
  19. tempImage = CreateImage(FrameWidth, FrameHeight)
  20.  
  21.  
  22. ;first, copy the original frames to the new image
  23. SetBuffer ImageBuffer(NewImage)
  24. For Count = 0 To FrameCount -1
  25.        
  26.         DrawImage inImage, Count * FrameWidth, 0, Count
  27.                
  28. Next
  29.  
  30.  
  31.  
  32. ;next copy and flip the frames and place them in the new image in the same sequence
  33. StartTime = MilliSecs()
  34.  
  35. For Count = 0 To FrameCount -1
  36.         SetBuffer ImageBuffer(tempImage)
  37.         Cls
  38.         DrawImage inImage, 0, 0, Count
  39.         tempImage = FlipImageH(tempImage)
  40.         SetBuffer ImageBuffer(NewImage)
  41.         DrawImage tempImage, XOffset + (Count * FrameWidth), 0
  42. Next
  43. EndTime = MilliSecs() - StartTime
  44. ;SAVE THE IMAGE
  45. SaveImage(NewImage, OutImageName$)
  46.  
  47.  
  48. ;DISPLAY THE END RESULTS
  49. SetBuffer BackBuffer()
  50.  
  51. DrawImage NewImage, 0,0
  52.  
  53. Text 0, ImageHeight(NewImage), "Done - file saved as: " + OutImageName$
  54. Text 0, ImageHeight(NewImage) + 24, "Time taken to convert:" + EndTime + "ms"
  55.  
  56. ;END OF PROGRAM
  57.  
  58. WaitKey()
  59.  
  60. FreeImage NewImage
  61. FreeImage inImage
  62. FreeImage tempImage
  63.  
  64. EndGraphics
  65.  
  66. End
  67.  
  68. ;FUNCTION FileImageH
  69. ;Accepts an image, flips it horizontally (using CopyRect), then returns the resulting image
  70.  
  71. Function FlipImageH(srcImage)
  72. ;LOCAL VARS
  73. Local Width = ImageWidth(srcImage)
  74. Local Height = ImageHeight(srcImage)
  75. Local imgTemp = CreateImage(Width, Height)
  76.  
  77. ;MAIN
  78.  
  79. For X = 0 To ImageWidth(srcImage)-1
  80.         CopyRect X, 0, 1, Height, (Width-1) - X, 0, ImageBuffer(srcImage), ImageBuffer(imgTemp)
  81. Next
  82.  
  83. Return imgTemp
  84.  
  85. End Function
  86.  
  87. ;FUNCTION ExtractFileName
  88. ;Accepts a filepath and returns the filename
  89.  
  90. Function ExtractFileName$(sFilePath$)
  91. ;LOCAL VARS
  92. Local iStartPos% = 0
  93. Local iSearchPos% = 0
  94. Local iFilePathLength = 0
  95. Local sFileName$ = ""
  96.        
  97. ;BEGIN FUNCTION CODE
  98. iFilePathLength = Len(sFilePath$)
  99. iSearchPos% = iFilePathLength
  100.  
  101. While (iStartPos% < 1) And (iSearchPos% > 1)
  102.  
  103.         iStartPos% = Instr(sFilePath$, "", iSearchPos%)
  104.         iSearchPos% = iSearchPos% - 1
  105.        
  106. Wend
  107.  
  108. If iStartPos = 0 Then ;if the filepath contains no backslashes
  109.         sFileName$ = sFilePath$
  110. Else
  111.         sFileName$ = Right$(sFilePath$, iFilePathLength% - iStartPos%)
  112. EndIf
  113.  
  114.  
  115. Return sFileName$
  116.  
  117. End Function
  118.  
  119. ;FUNCTION ExtractFileExt
  120. ;Accepts a filepath and returns the extension for the file
  121.  
  122. Function ExtractFileExt$(sFilePath$)
  123. ;LOCAL VARS
  124. Local iStartPos% = 0
  125. Local iSearchPos% = 0
  126. Local iFilePathLength = 0
  127. Local sFileExt$ = ""
  128.        
  129. ;BEGIN FUNCTION CODE
  130. iFilePathLength = Len(sFilePath$)
  131. iSearchPos% = iFilePathLength
  132.  
  133. While (iStartPos% < 1) And (iSearchPos% > 1)
  134.  
  135.         iStartPos% = Instr(sFilePath$, ".", iSearchPos%)
  136.         iSearchPos% = iSearchPos% - 1
  137.        
  138. Wend
  139.  
  140. If iStartPos = 0 Then ;if the filepath contains no .
  141.         sFileExt$ = sFilePath$
  142. Else
  143.         sFileExt$ = Right$(sFilePath$, iFilePathLength% - iStartPos%)
  144. EndIf
  145.  
  146.  
  147. Return sFileExt$
  148.  
  149. End Function
  150.  
  151. ;FUNCTION ExtractFilePath
  152. ;Accepts a filepath with filename and returns only the path
  153. ;i.e. pass c:   emp     est.txt the return value will be c:     emp
  154.  
  155. Function ExtractFilePath$(sFilePath$)
  156. ;LOCAL VARS
  157. Local iStartPos% = 0
  158. Local iSearchPos% = 0
  159. Local iFilePathLength = 0
  160. Local sFileExt$ = ""
  161.        
  162. ;BEGIN FUNCTION CODE
  163. iFilePathLength = Len(sFilePath$)
  164. iSearchPos% = iFilePathLength
  165.  
  166. While (iStartPos% < 1) And (iSearchPos% > 1)
  167.  
  168.         iStartPos% = Instr(sFilePath$, "", iSearchPos%)
  169.         iSearchPos% = iSearchPos% - 1
  170.        
  171. Wend
  172.  
  173. If iStartPos = 0 Then ;if the filepath contains no backslashes
  174.         sFileExt$ = sFilePath$
  175. Else
  176.         sFileExt$ = Left$(sFilePath$, iStartPos%)
  177. EndIf
  178.  
  179.  
  180. Return sFileExt$
  181.  
  182. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal