November 28, 2020, 02:24:39 AM

Author Topic: [bmx] Image sheet and strip maker by AdamRedwoods [ 1+ years ago ]  (Read 371 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Image sheet and strip maker
Author : AdamRedwoods
Posted : 1+ years ago

Description : Quick Image strip/sheet maker for BMax+MaxGUI.
exports PNG files.

Assumes the first image loaded is the size of all the images.
Hold shift to multi-select files.

You could change this for mac or linux by changing the top MaxGUI import.

You may not sell this program or code for profit, but you may use the files it makes for commercial uses. Makes sense.


Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. ''image strip maker
  4. '' copyright 2011 Adam Piette
  5. '' You may not sell this program or code for profit, but you may use the files it makes for commercial uses. Makes sense.
  6. '
  7. 'makes image strips For animated textures
  8. 'assumes sheet and strip images are all same sizes of first image loaded
  9. 'only does PNG so far
  10.  
  11. Framework MaxGUI.Win32MaxGUIEx
  12. Import BRL.pngloader
  13. Import BRL.jpgloader
  14. Import BRL.tgaloader
  15. Import BRL.bmploader
  16. Import BRL.Event
  17. Import BRL.EventQueue
  18. Import "multifilerequestor.bmx"
  19.  
  20. Global window:TGadget =CreateWindow("Image Strip Maker",100,40,700,500)
  21.  
  22. Global panel:TGadget = CreatePanel(10,10,690,150,window,0,"")
  23. Global loadbtn:TGadget  = CreateButton("Load",10,10,100,30,panel,BUTTON_PUSH)
  24. Global filelabel:TGadget  = CreateLabel(".",130,10,520,30,panel,LABEL_LEFT|LABEL_SUNKENFRAME)
  25. CreateLabel("Save File: ",10,50,100,30,panel,LABEL_RIGHT)
  26. Global file2label:TGadget = CreateLabel(".",130,50,520,30,panel,LABEL_LEFT|LABEL_SUNKENFRAME)
  27.  
  28. Global combobox:TGadget = CreateComboBox(10,90,200,25,panel)
  29. AddGadgetItem combobox, "Make Image Strip", True
  30. AddGadgetItem combobox, "Make Image Sheet", False
  31.  
  32.  
  33.  
  34. Global runbtn:TGadget  = CreateButton("RUN",10,120,100,30,panel,BUTTON_PUSH)
  35.  
  36. Global imagepanel:TGadget = CreatePanel(10,170,690,200,window,PANEL_BORDER,"")
  37.  
  38. Global files:String[] = New String[101]
  39. Global currDir:String = ""
  40. Global outfile:String = ""
  41. Global ext:String=""
  42.  
  43. Global imageFinal:TPixmap
  44.  
  45. SetGadgetLayout( panel,EDGE_ALIGNED,0,EDGE_ALIGNED,0 )
  46. SetGadgetLayout( imagepanel,EDGE_ALIGNED,0,EDGE_ALIGNED,0 )
  47.  
  48. Repeat
  49.         Select WaitEvent()
  50.                 Case EVENT_WINDOWCLOSE, EVENT_APPTERMINATE
  51.                         End
  52.                 Case EVENT_GADGETACTION
  53.                         ''
  54.                         DoGadget(TGadget(EventSource()), EventData() )
  55.         EndSelect
  56.        
  57. Forever
  58.  
  59. End
  60.  
  61. Function DoGadget(tg:TGadget, edata:Int)
  62.         If tg=loadbtn
  63.                 files = RequestMultiFile( "Open Files", "jpg,png,bmp,tga")
  64.                 If (files.length>2)
  65.                         Print
  66.                         Print "dir:"+files[0]
  67.                         currDir = files[0]
  68.                         SetGadgetText( filelabel, (StripDir(files[1])+"..."+StripDir(files[files.length-1])))
  69.                        
  70.                         outfile = files[1]
  71.                         ext:String = "."+ExtractExt(outfile).toLower()
  72.                         outfile = StripExt(outfile)
  73.                         For Local i:Int = outfile.length-1 To 1 Step -1
  74.                                 If outfile[i] >=48 And outfile[i] <=57
  75.                                         outfile = outfile[..i]
  76.                                 Else
  77.                                         Exit
  78.                                 EndIf
  79.                         Next
  80.                         outfile :+ ext
  81.                         SetGadgetText( file2label, outfile)
  82.                         outfile = currDir+outfile
  83.                 EndIf
  84.         EndIf
  85.         If tg=runbtn
  86.                 Local action:Int = SelectedGadgetItem(combobox)
  87.                 If files.length>2 And outfile<>""
  88.                         Local err:Int
  89.  
  90.                         If action = 0 Then  Print "image strip" ; err = MakeImageStrip()
  91.                         If action = 1 Then Print "image sheet" ; err = MakeImageSheet()
  92.                         If err =-1 Then Notify("First Image load failed.")
  93.                         If err =-2 Then Notify("Multi Image load failed.")
  94.                         If Not err
  95.                                 Print "done."
  96.                                 SetGadgetPixmap( imagepanel, imageFinal, PANELPIXMAP_FIT )
  97.                                
  98.                                 If ext = ".png"
  99.                                         SavePixmapPNG(imageFinal,outfile,7)
  100.                                 EndIf
  101.                                 Print"Saved."
  102.                         EndIf
  103.                 EndIf
  104.         EndIf
  105. EndFunction
  106.  
  107. Function MakeImageStrip:Int()
  108.         Local firstpix:TPixmap = LoadPixmap(currDir+files[1])
  109.        
  110.         If Not firstpix Then Return -1
  111.        
  112.         Local w:Int = firstpix.width
  113.         Local h:Int = firstpix.height
  114.         Local p:Int = firstpix.pitch
  115.         Local total:Int = files.length-1
  116.         Print "total:"+total+" width:"+w+" p:"+p
  117.         Local newpix:TPixmap = CreatePixmap(w*total,h,PF_RGBA8888)
  118.         Local srcpix:TPixmap
  119.         Local destptr:Byte Ptr, srcptr:Byte Ptr
  120.        
  121.         For Local j:Int = 0 To total-1
  122.                 srcpix = LoadPixmap(currDir+files[j+1])
  123.                 Print currDir+files[j+1]
  124.                 If Not srcpix Then Return -2
  125.                 srcpix = ConvertPixmap( srcpix,PF_RGBA8888)
  126.                 srcptr = srcpix.pixelptr(0,0)
  127.                
  128.                 For Local y:Int = 0 To h-1
  129.                         destptr = newpix.pixelptr(w*j,y)
  130.                         srcptr = srcpix.pixelptr(0,y)
  131.                         'MemCopy (destptr, srcptr, w*3)
  132.                         For Local x:Int = 0 To w*4 ''use width since we're reading from source, pasting to dest
  133.                                 destptr[x] = srcptr[x]
  134.                         Next
  135.                        
  136.                        
  137.                 Next
  138.         Next
  139.        
  140.         imageFinal = newpix
  141.  
  142. EndFunction
  143.  
  144. Function MakeImageSheet:Int()
  145.         Local firstpix:TPixmap = LoadPixmap(currDir+files[1])
  146.        
  147.         If Not firstpix Then Return -1
  148.        
  149.         Local w:Int = firstpix.width
  150.         Local h:Int = firstpix.height
  151.         Local p:Int = firstpix.pitch
  152.         Local total:Int = files.length-1
  153.        
  154.         Local sheetsize:Int = Ceil(Sqr(Float(total) ))
  155.        
  156.         Print "total:"+total+" width:"+w+" p:"+p+" sqsize:"+sheetsize
  157.         Local newpix:TPixmap = CreatePixmap(sheetsize*w,sheetsize*h,PF_RGBA8888)
  158.         Local srcpix:TPixmap
  159.         Local destptr:Byte Ptr, srcptr:Byte Ptr
  160.        
  161.         Local offx:Int=0, offy:Int=0
  162.        
  163.         For Local j:Int = 0 To total-1
  164.                 srcpix = LoadPixmap(currDir+files[j+1])
  165.                 Print currDir+files[j+1]
  166.                 If Not srcpix Then Return -2
  167.                 srcpix = ConvertPixmap( srcpix,PF_RGBA8888)
  168.                 srcptr = srcpix.pixelptr(0,0)
  169.                
  170.                 For Local y:Int = 0 To h-1
  171.                         destptr = newpix.pixelptr(w*offx,y+h*offy)
  172.                         srcptr = srcpix.pixelptr(0,y)
  173.                         'MemCopy (destptr, srcptr, w*3)
  174.                         For Local x:Int = 0 To w*4 ''use width since we're reading from source, pasting to dest
  175.                                 destptr[x] = srcptr[x]
  176.                         Next
  177.                        
  178.                        
  179.                 Next
  180.                
  181.                 offx=offx+1
  182.                 If offx>sheetsize-1
  183.                         offy=offy+1
  184.                         offx=0
  185.                 EndIf
  186.         Next
  187.        
  188.         imageFinal = newpix
  189.  
  190. EndFunction


Comments :


AdamRedwoods(Posted 1+ years ago)

 Forogt the MULTIFILEREQUESTOR.bmxNot sure if that's available on MacOS, Linux.multifilerequestor.bmx:
Code: [Select]
SuperStrict
?Win32
Import PUB.Win32

Private

Const MAX_BUFFER_SIZE:Int = 8192

Const OFN_ALLOWMULTISELECT:Int = 512
Const OFN_CREATEPROMPT:Int = $2000
Const OFN_ENABLEHOOK:Int = 32
Const OFN_ENABLESIZING:Int = $800000
Const OFN_ENABLETEMPLATE:Int = 64
Const OFN_ENABLETEMPLATEHANDLE:Int = 128
Const OFN_EXPLORER:Int = $80000
Const OFN_EXTENSIONDIFFERENT:Int = $400
Const OFN_FILEMUSTEXIST:Int = $1000
Const OFN_HIDEREADONLY:Int = 4
Const OFN_LONGNAMES:Int = $200000
Const OFN_NOCHANGEDIR:Int = 8
Const OFN_NODEREFERENCELINKS:Int = $100000
Const OFN_NOLONGNAMES:Int = $40000
Const OFN_NONETWORKBUTTON:Int = $20000
Const OFN_NOREADONLYRETURN:Int = $8000
Const OFN_NOTESTFILECREATE:Int = $10000
Const OFN_NOVALIDATE:Int = 256
Const OFN_OVERWRITEPROMPT:Int = 2
Const OFN_PATHMUSTEXIST:Int = $800
Const OFN_READONLY:Int = 1
Const OFN_SHAREAWARE:Int = $4000
Const OFN_SHOWHELP:Int = 16
Const OFN_SHAREFALLTHROUGH:Int = 2
Const OFN_SHARENOWARN:Int = 1
Const OFN_SHAREWARN:Int = 0

Type TOpenFileNameA
Field lStructSize:Int
Field hwndOwner:Int
Field hInstance:Int
Field lpstrFilter:Byte Ptr
Field lpstrCustomFilter:Int
Field nMaxCustFilter:Int
Field nFilterIndex:Int
Field lpstrFile:Byte Ptr
Field nMaxFile:Int
Field lpstrFileTitle:Byte Ptr
Field nMaxFileTitle:Int
Field lpstrInitialDir:Byte Ptr
Field lpstrTitle:Byte Ptr
Field Flags:Int
Field nFileOffset:Short
Field nFileExtension:Short
Field lpstrDefExt:Byte Ptr
Field lCustData:Int
Field lpfnHook:Byte Ptr
Field lpTemplateName:Byte Ptr
EndType

Extern "Win32"
Function GetOpenFileName:Int( of:Byte Ptr) = "GetOpenFileNameA@4"
EndExtern

Extern "C"
Function memcpy( dst:Byte Ptr, src$z, sz:Int)
EndExtern

Public
?

Function RequestMultiFile:String[]( text:String, exts:String = Null, path:String = Null)
?MacOS | Linux
Local res:String = RequestFile( test, exts, False, path)
If res.Length <= 0 Then Return Null
Return [res]
?
?Win32
Global hwndFocus:Int

' prepare filename / path (ripped from BRL's RequestFile())
Local file:String, dir:String
path = path.Replace( "/","" )
Local i:Int = path.FindLast( "" )
If i <> -1 Then
dir = path[..i]
file = path[i+1..]
Else
file = path
EndIf
' calculate default index of extension in extension list from path name
Local ext:String, defext:Int,p:Int,q:Int
p = path.Find(".")
If (p>-1) Then
ext = "," + path[p+1..].toLower() + ","
Local exs:String = exts.toLower()
exs = exs.Replace(":",":,")
exs = exs.Replace(";",",;")
p = exs.find(ext)
If p >-1 Then
Local q:Int = -1
defext = 1
While True
q = exs.find(";",q+1)
If q > p Then Exit
If q = -1 Then
defext = 0
Exit
EndIf
defext :+ 1
Wend
EndIf
EndIf
If exts Then
If exts.Find(":") = -1 Then
exts = "Files~0*." + exts
Else
exts = exts.Replace(":","~0*.")
EndIf
exts = exts.Replace(";","~0")
exts = exts.Replace(",",";*.") + "~0"
EndIf

' allocate cstrings
Local textp:Byte Ptr = text.ToCString()
Local extsp:Byte Ptr = exts.ToCString()
Local dirp:Byte Ptr
If dir.Length > 0 Then dirp = dir.ToCString()

' prepare file buffer
Local buf:Byte[MAX_BUFFER_SIZE]
memcpy( buf, file, file.Length)

' initialize dialog options
Local of:TOpenFileNameA = New TOpenFileNameA
of.lStructSize = SizeOf(TOpenFileNameA)
of.hwndOwner = GetActiveWindow()
of.lpstrTitle = textp
of.lpstrFilter = extsp
of.nFilterIndex = defext
of.lpstrFile = buf
of.lpstrInitialDir = dirp
of.nMaxFile = buf.Length
of.Flags = OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_EXPLORER ' | OFN_LONGNAMES

' display dialog
hwndFocus = GetFocus()
Local n:Int = GetOpenFileName( of)
SetFocus( hwndFocus)

' free cstrings
MemFree textp
MemFree extsp
If dirp Then MemFree dirp

' failure ?
If n <= 0 Then Return Null

' count the number of files
Local s:Byte Ptr = buf
Local count:Int = 0
While s[0] <> 0
If s[1] = 0 Then
count :+ 1
s :+ 2
If s[0] = 0 Then Exit
EndIf
s :+ 1
Wend
If count <= 0 Then Return Null

' extract filenames into String array
If count = 1 Then
'MARK: im following RequestFile() convention here, and returing "" path seperators #1
'Return [ String.FromCString( buf).Replace( "", "/") ]
Return [ String.FromCString( buf) ]
Else
Local result:String[] = New String[count]
s = buf
For Local i:Int = 0 Until count
result[i] = String.FromCString( s)
s :+ result[i].Length + 1
Next
'MARK: im following RequestFile() convention here, and returing "" path seperators #2
'result[0] = result[0].Replace( "", "/") + "/"
result[0] :+ ""
Return result
EndIf
EndFunction



Matty(Posted 1+ years ago)

 You did read this right at the top of the code archives didn't you:This code has been declared by its author to be Public Domain code.Not sure how that fits with:<div class="quote"> You may not sell this program or code for profit, but you may use the files it makes for commercial uses. Makes sense. </div> [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal