Data Section maker

Developed or developing a new product in PureBasic? Tell the world about it.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Data Section maker

Post by IdeasVacuum »

A bit crude and concentrated on images but it works and you can modify it to your heart's content. Saves a lot of time typing and prevents typos causing hard to find bugs.

Code: Select all

;Create Image DataSection from folder of images
;IdeasVacuum 2012
;Tested PB4.60 Win32

Enumeration
#WinMain
#Path
#Folder
#List
#TabPanel
#BtnBrowse
#BtnCreate
#BtnCopy
#Opt1
#Opt2
#SpinLabel
#SpinEnum
#Code
EndEnumeration

Global FontID1 = LoadFont(1, "CourierNew", 10, #PB_Font_HighQuality)
Global igJpgPluginRequired.i = #False
Global igPngPluginRequired.i = #False
Global NewList sgFileFull.s()
Global igLongestName.i = 0
Global  igLocalCatch.i = #True

Procedure ListImageFiles()
;------------------------
Shared igJpgPluginRequired
Shared igPngPluginRequired
Shared igLongestName

Protected        iLen.i = 0
Protected       sName.s = ""
Protected   sFileFull.s = ""
Protected       sFile.s = ""
Protected        sExt.s = ""
Protected       sPath.s = PathRequester("Browse to and select Folder containing images", "")

          SetGadgetText(#Folder,sPath)
       ClearGadgetItems(#List)
       ClearGadgetItems(#Code)

  If ExamineDirectory(#Path, sPath, "*.*")

    While NextDirectoryEntry(#Path)

       If DirectoryEntryType(#Path) = #PB_DirectoryEntry_File

                              sFile = DirectoryEntryName(#Path)
                               sExt = GetExtensionPart(sFile)
                              sName = GetFilePart(sFile)

                    If( (sExt = "jpg") Or (sExt = "png") Or (sExt = "bmp") )

                               AddElement(sgFileFull())
                               sgFileFull() = sPath + sFile
                            AddGadgetItem(#List, -1, sgFileFull())
                                          iLen = Len(sName)
                                       If(iLen > igLongestName) : igLongestName = iLen : EndIf
                    EndIf

                    If(LCase(sExt) = "jpg") : igJpgPluginRequired = #True : EndIf
                    If(LCase(sExt) = "png") : igPngPluginRequired = #True : EndIf
       EndIf


    Wend

    FinishDirectory(#Path)

  EndIf

EndProcedure

Procedure MakeCode()
;-------------------

Protected iTotalFiles.i = 0
Protected        iRow.i = 0
Protected        iLen.i = 0
Protected        iPad.i = 0
Protected       sName.s = ""
Protected        sRow.s = ""
Protected Dim  sLabel.s(1)
Protected Dim   sEnum.s(1)

         ClearGadgetItems(#Code)

         If(igJpgPluginRequired = #True)

                 AddGadgetItem(#Code, -1, "UseJPEGImageDecoder()")
                 AddGadgetItem(#Code, -1, "UseJPEGImageEncoder()")
         EndIf

         If(igPngPluginRequired = #True)

                 AddGadgetItem(#Code, -1, "UsePNGImageDecoder()")
                 AddGadgetItem(#Code, -1, "UsePNGImageEncoder()")
         EndIf

         iEnum = GetGadgetState(#SpinEnum)

         AddGadgetItem(#Code, -1, "")

         If(igLocalCatch = #True) : AddGadgetItem(#Code, -1, "Enumeration " + Str(iEnum) + " Step 1") : EndIf

         iTotalFiles = ListSize(sgFileFull())
         Dim sLabel.s(iTotalFiles)
         Dim  sEnum.s(iTotalFiles)

         FirstElement(sgFileFull())

         For iRow = 0 To (iTotalFiles - 1)

                                        sName = GetFilePart(sgFileFull())
                   ReplaceString(sName, " ", "_", #PB_String_InPlace , 1) ;Replace Space chars
                        iLen = Len(sName)

             If(igLocalCatch = #True)

                      sEnum(iRow) = "#Image" + Left(sName,(iLen - 4))
                     sLabel(iRow) = Left(sName,(iLen - 4)) + ":"
                     AddGadgetItem(#Code, -1, sEnum(iRow))

             Else
                     sLabel(iRow) = Left(sName,(iLen - 4))
                     iPad = (igLongestName - iLen) + 1
                     sRow = "Global" + Space(iPad) + "igImg" + sLabel(iRow) + ".i = CatchImage(" + Str(iRow) + ", ?img" + sLabel(iRow) + ")"
                     AddGadgetItem(#Code, -1, sRow)
                     sLabel(iRow) = Left(sName,(iLen - 4)) + ":"
             EndIf

                     NextElement(sgFileFull())
         Next iRow

         If(igLocalCatch = #True) : AddGadgetItem(#Code, -1, "EndEnumeration") : EndIf

         AddGadgetItem(#Code, -1, "")
         AddGadgetItem(#Code, -1, "DataSection")

         FirstElement(sgFileFull())

         For iRow = 0 To (iTotalFiles - 1)

                    AddGadgetItem(#Code, -1, "img" + sLabel(iRow))

                    sRow = "IncludeBinary " + Chr(34) + sgFileFull() + Chr(34)
                    AddGadgetItem(#Code, -1, sRow)

                    NextElement(sgFileFull())
         Next iRow

         AddGadgetItem(#Code, -1, "EndDataSection")
         AddGadgetItem(#Code, -1, "")
         AddGadgetItem(#Code, -1, ";Image Gadgets (manual edit!)")
         AddGadgetItem(#Code, -1, "")

                sCatch.s = ""

         If(igLocalCatch = #True)

                For iRow = 0 To (iTotalFiles - 1)

                          sCatch = RTrim(sLabel(iRow),":")
                            sRow = "ImageGadget(#PB_Any, 0, 0, 0, 0, CatchImage(" + sEnum(iRow) + ", ?img" + sCatch + "))"
                          AddGadgetItem(#Code, -1, sRow)
                Next iRow

         Else
                For iRow = 0 To (iTotalFiles - 1)
 
                          sCatch = RTrim(sLabel(iRow),":")
                            sRow = "ImageGadget(#PB_Any, 0, 0, 0, 0, igImg" + sCatch + ")"
                          AddGadgetItem(#Code, -1, sRow)
                Next iRow
         EndIf

EndProcedure

Procedure CopyCode()
;-------------------

          iCnt.i = 0
     iRowTotal.i = CountGadgetItems(#Code)
          sRow.s = ""
          sAll.s = ""

     For iCnt = 0 To iRowTotal

            sRow = GetGadgetItemText(#Code,iCnt,0)
            sAll = sAll + sRow + #CRLF$
     Next

     SetClipboardText(sAll)

EndProcedure

Procedure OpenWinMain()
;----------------------

  If OpenWindow(#WinMain,0,0,700,450,"Data Section Maker",#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered)

                       StringGadget(#Folder,      5, 10, 610,  25, "File Path")
                       ButtonGadget(#BtnBrowse, 625, 10,  70,  25, "Browse")
                        PanelGadget(#TabPanel,    5, 40, 690, 400)

                     ;TAB 0: Files List
                     ;---------------------------------------------------------------------------
                      AddGadgetItem(#TabPanel, -1, "Files")
                     ListIconGadget(#List,    10, 10, 660, 355, "Files Found",630,#PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect)
                    CloseGadgetList()

                     ;TAB 1: Data Section
                     ;---------------------------------------------------------------------------
                     OpenGadgetList(#TabPanel)
                      AddGadgetItem(#TabPanel, -1, "DataSection")
                       OptionGadget(#Opt1,        10, 10, 100,  25, "Local Cache")
                       OptionGadget(#Opt2,       120, 10, 100,  25, "Global Cache")
                         TextGadget(#SpinLabel,  265, 16,  80,  20, "Enum Start")
                         SpinGadget(#SpinEnum,   325, 13,  50,  20, 0, 5000, #PB_Spin_Numeric)
                      SetGadgetText(#SpinEnum,"0")
                       ButtonGadget(#BtnCreate,  475, 10,  80,  25, "Create")
                       ButtonGadget(#BtnCopy,    555, 10, 120,  25, "Copy to Clipboard")
                     ListIconGadget(#Code,        10, 40, 665, 325, "Code",630,#PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect)

                      SetGadgetFont(#Code,FontID1)
                    CloseGadgetList()
  EndIf

EndProcedure

OpenWinMain()

;MAIN LOOP
                        iGdgID.i = 0
                        iEvent.i = 0
          Repeat
                        iEvent = WaitWindowEvent(1)
                 Select iEvent

                         Case #PB_Event_Gadget

                                     iGdgID = EventGadget()
                              Select iGdgID

                                          Case #BtnBrowse: ListImageFiles()
                                          Case #BtnCreate: MakeCode()
                                          Case #BtnCopy: CopyCode()
                                          Case #Opt1: igLocalCatch = #True
                                          Case #Opt2: igLocalCatch = #False

                              EndSelect

                 EndSelect

          Until iEvent = #PB_Event_CloseWindow

CloseWindow(#WinMain)

End
Edit: Re Idle's suggestion, Procedure MakeCode, line 118 ReplaceString inserted. If you have any suggestions for improvement, please post them here for the benefit of all. I won't be updating the code though as it already suits my purposes and I'm sure everyone can modify it to suit theirs.
Last edited by IdeasVacuum on Tue Feb 07, 2012 11:38 pm, edited 2 times in total.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
idle
Always Here
Always Here
Posts: 5879
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Data Section maker

Post by idle »

Looks like a nice solution, thanks!
you might want to look at filling in spaces in a file name with "_" for the enums
Windows 11, Manjaro, Raspberry Pi OS
Image
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Data Section maker

Post by IdeasVacuum »

you might want to look at filling in spaces in a file name with "_" for the enums
Thanks for that suggestion idle, I've added a line to the code above. I actually never have spaces in file names myself.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Data Section maker

Post by Kwai chang caine »

Thanks for sharing, works good 8)
I thought it's better, to send the size of image for be sure ??

Code: Select all

DataSection
imgbmp:
IncludeBinary "C:\bmp.bmp"
imgbmp1:
IncludeBinary "C:\bmp1.bmp"
EndDataSection

;Image Gadgets (manual edit!)

ImageGadget(#PB_Any, 0, 0, 0, 0, CatchImage(#Imagebmp, ?imgbmp, ?imgbmp1 - ?imgbmp))
ImageGadget(#PB_Any, 0, 0, 0, 0, CatchImage(#Imagebmp1, ?imgbmp1))
ImageThe happiness is a road...
Not a destination
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Data Section maker

Post by IdeasVacuum »

I thought it's better, to send the size of image for be sure ??
Well, yes and no.

The purpose of this routine is to build a Data Section from your image files that your app requires - so these are 'known' images and you know that they are not corrupted in any way since you can see them working during your development and once they are part of the exe, there is nothing more to worry you.

If on the other hand you use CatchImage() for files browsed to by the User of your app at run time, then those are 'unknown' images and it is possible that they have faults - in which case yes, the file size should be passed to CatchImage().
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Data Section maker

Post by Kwai chang caine »

Aaaah ok !!!
Thanks to your explanation 8)
ImageThe happiness is a road...
Not a destination
Post Reply