EnableExplicit
If OpenConsole() = 0
MessageRequester("OpenConsole() error","Unable to open the console.",0)
End
EndIf
Global NewList color.l()
Define i.l
Global str1$
For i = 1 To 17
str1$ = str1$ + Chr(205)
Next
ConsoleTitle ("img2asm")
EnableGraphicalConsole(1)
PrintN(Chr(201)+str1$+Chr(187))
PrintN(Chr(186)+" img2asm - deeph "+Chr(186))
PrintN(Chr(200)+str1$+Chr(188))
PrintN("")
If CountProgramParameters() > 0
Define file.s = ProgramParameter()
If GetExtensionPart(file) = "jpg" Or GetExtensionPart(file) = "jpeg"
UseJPEG2000ImageDecoder()
ElseIf GetExtensionPart(file) = "png"
UsePNGImageDecoder()
ElseIf GetExtensionPart(file) = "tif"
UseTIFFImageDecoder()
ElseIf GetExtensionPart(file) = "tga"
UseTGAImageDecoder()
ElseIf GetExtensionPart(file) <> "bmp"
PrintN("Error : image format unsupported.")
End
EndIf
If LoadImage(0, file)
If ImageDepth(0, #PB_Image_OriginalDepth) <> 5
If CreateFile(0, GetPathPart(ProgramFilename())+ReplaceString(GetFilePart(file), GetExtensionPart(file), "asm"))
If StartDrawing(ImageOutput(0))
Define y.l, x.l, i.l = 0, new_color.l = 1
; on commence par d�finir la palette
For y = 0 To ImageHeight(0)-1
For x = 0 To ImageWidth(0)-1
new_color = 1
ForEach color()
If Int(Red(Point(x, y))/8)+Int(Green(Point(x, y))/8)<<5+Int(Blue(Point(x, y))/8)<<10 = color()
new_color = 0
Break
EndIf
Next color()
If new_color
AddElement(color())
color() = Int(Red(Point(x, y))/8)+Int(Green(Point(x, y))/8)<<5+Int(Blue(Point(x, y))/8)<<10
EndIf
Next x
Next y
AddElement(color())
color() = 0
SortList(color(), #PB_Sort_Ascending)
WriteStringN(0, ReplaceString(GetFilePart(file), "."+GetExtensionPart(file), "")+"_palette:")
WriteString(0, Chr(9)+".hword ")
ForEach color()
If i = 8
WriteStringN(0, "")
WriteString(0, Chr(9)+".hword ")
i = 0
ElseIf i>0
WriteString(0, ",")
EndIf
i+1
WriteString(0, "0x"+RSet(Hex(color()), 4, "0"))
Next
WriteStringN(0, "")
WriteStringN(0, "")
If CountProgramParameters() > 1
StopDrawing()
file.s = ProgramParameter()
LoadImage(0, file)
StartDrawing(ImageOutput(0))
EndIf
; puis le sprite
WriteStringN(0, ReplaceString(GetFilePart(file), "."+GetExtensionPart(file), "")+":")
Define y2.l, x2.l
For y = 0 To ImageHeight(0)-1 Step 8
For x = 0 To ImageWidth(0)-1 Step 8
For y2 = 0 To 7
WriteString(0, Chr(9)+".word 0x")
For x2 = 7 To 0 Step -1
ForEach color()
If color() = Int(Red(Point(x+x2, y+y2))/8)+Int(Green(Point(x+x2, y+y2))/8)<<5+Int(Blue(Point(x+x2, y+y2))/8)<<10
WriteString(0, Hex(ListIndex(color())))
Break
EndIf
Next color()
Next x2
WriteStringN(0, "")
Next y2
Next x
Next y
StopDrawing()
EndIf
PrintN(ReplaceString(GetFilePart(file), GetExtensionPart(file), "asm")+" correctly generated.")
CloseFile(0)
Else
PrintN("Error : unable to create the output file.")
EndIf
Else
PrintN("Error : incorrect image depth (use 16 colors).")
EndIf
Else
PrintN("Error : unable to open the image.")
EndIf
Else
PrintN("Error : no input.")
EndIf
End
; IDE Options = PureBasic 4.61 (Linux - x86)
; CursorPosition = 72
; FirstLine = 50
; EnableXP
; EnableUser
; Executable = ../Projects/test/img2asm
; CommandLine = "sprite.bmp"