Last library version (15/12/2021) :
ManagePicturePNG.lg32 (134.81 KB)
2 New Functions :
PngCompilation(Options%, Dimension%, CompilationFile$, PngFileName$()) as Long
PngGetCutlist(CompilationFile$, *PngCpl() as PNGCPL_STRUCT) as Long
PngCompilation(Option%,Dimension%,CompilationFile,PngFileName$()) creates a PNG file grouping together all the PNG files passed by the PngFileName$() array passed as a parameter. The function return the size in pixels of the image created (Width*Height).
Options% possibles values who can be combined :
PNGCPL_FIXEDWIDTH : The
Dimension% parameter define a fixed widh for the result image
PNGCPL_FIXEDHEIGHT : The Dimension% parameter define a fixed height for the result image
PNGCPL_ONLYCOMPUTE : Only compute the size of the result image
PNGCPL_COMPUTEANDSAVE : Create the file CompilationFile$
The resulting file then contains a tEXt chunck whose keyword is "CutList". The text contains the list of source files compiled in this form:
identifier1, X, Y, W, H, OX, OY; identifier2, X, Y, W, H, OX, OY; ...; identifierN, X, Y, W, H, OX, OY;
identifier is formed from the name of the file and cannot be longer than 260 characters, note that identifier is NOT the name of the file and may be different especially if the name of the file contains a comma "," or a semicolon ";". In these cases the character is replaced by an underscore "_". In addition, if the full name of the file contains more than 260 characters it is truncated by removing the beginning of the name.
X,Y,W and H indicate the position X,Y in the image compilation and the width and height of the source.
Ox, Oy indicates the offset if it was present in the source file (chunck oFFs) otherwise they have the value 0.
PngGetCutlist(CompilationFile$, *PngCpl() as PNGCPL_STRUCT) fill the PngCpl() array while reading le tEXt "CutList". if there's more sprites than array dimension the array is automatically redimed. The function return the number of sprites found, and the first PngCpl() index is 1 (not 0 even if the array starts at index 0).
The structure exported by the library is :
Type PNGCPL_STRUCT
-String*260 Key
-Long X, Y, W, H, OX, OY
EndType
In this example we fill a string array with all the "rover*.png" files, we create a PNG "compilation" testpngcompil.png" who have a width fixed to 400 pixels, then we display the list of keywords and coordinates contained in this file :
$Library "ManagePicturePng"
Mode StrSpace 0
Global Dim PngFileName() As String
Global Long i, PngFileCount = 0, Area, N
FullW 1
Ocx TextBox TB = "", 0, 0, Me.ScaleWidth, Me.ScaleHeight
TB.MultiLine = True : TB.ScrollBars = basBoth : TB.BorderStyle = 1
AutomaticFileList
Global String CompilFile = "testpngcompil.png"
Area = PngCompilation(PNGCPL_COMPUTEANDSAVE | PNGCPL_FIXEDWIDTH, 400, CompilFile, PngFileName())
Global Dim tcut() As PNGCPL_STRUCT
N = PngGetCutlist(CompilFile, tcut())
TB.SelText = "AREA = " & Area & ", N=" & N & #13#10
For i = 1 To N
TB.SelText = "Key="#34 & Trim(tcut(i).Key) & #34" "
TB.SelText = "X=" & tcut(i).X & ",Y=" & tcut(i).Y & ", W=" & tcut(i).W & ", H=" & tcut(i).H & ", Ox=" & tcut(i).OX & ", Oy=" & tcut(i).OY & #13#10
Next i
Do
Sleep
Loop Until Me Is Nothing
Proc AutomaticFileList
Local String Contents$, f
ChDir App.Path
Contents$ = Dir("rover*.png", $16)
While Len(Contents$)
f = Contents
If Exist(f)
Inc PngFileCount
If PngFileCount + 1 > Dim?(PngFileName())
ReDim PngFileName(PngFileCount + 1)
EndIf
PngFileName(PngFileCount - 1) = f
EndIf
Contents$ = Dir$
Wend
EndProc