File: img2asm.pb - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

001: EnableExplicit
002: If OpenConsole() = 0
003:   MessageRequester("OpenConsole() error","Unable to open the console.",0)
004:   End
005: EndIf
006: Global NewList color.l()
007: Define i.l
008: Global str1$
009: For i = 1 To 17
010:   str1$ = str1$ + Chr(205)
011: Next
012: ConsoleTitle ("img2asm")
013: EnableGraphicalConsole(1)
014: PrintN(Chr(201)+str1$+Chr(187))
015: PrintN(Chr(186)+" img2asm - deeph "+Chr(186))
016: PrintN(Chr(200)+str1$+Chr(188))
017: PrintN("")
018: If CountProgramParameters() > 0
019:   Define file.s = ProgramParameter()
020:   If GetExtensionPart(file) = "jpg" Or GetExtensionPart(file) = "jpeg"
021:     UseJPEG2000ImageDecoder()
022:   ElseIf GetExtensionPart(file) = "png"
023:     UsePNGImageDecoder()
024:   ElseIf GetExtensionPart(file) = "tif"
025:     UseTIFFImageDecoder()
026:   ElseIf GetExtensionPart(file) = "tga"
027:     UseTGAImageDecoder()
028:   ElseIf GetExtensionPart(file) <> "bmp"
029:     PrintN("Error : image format unsupported.")
030:     End
031:   EndIf
032:   If LoadImage(0, file)
033:     If ImageDepth(0, #PB_Image_OriginalDepth) <> 5
034:       If CreateFile(0, GetPathPart(ProgramFilename())+ReplaceString(GetFilePart(file), GetExtensionPart(file), "asm"))
035:         If StartDrawing(ImageOutput(0))
036:           Define y.l, x.l, i.l = 0, new_color.l = 1
037:           ; on commence par d�finir la palette
038:           For y = 0 To ImageHeight(0)-1
039:             For x = 0 To ImageWidth(0)-1
040:               new_color = 1
041:               ForEach color()
042:                 If Int(Red(Point(x, y))/8)+Int(Green(Point(x, y))/8)<<5+Int(Blue(Point(x, y))/8)<<10 = color()
043:                   new_color = 0
044:                   Break
045:                 EndIf
046:               Next color()
047:               If new_color
048:                 AddElement(color())
049:                 color() = Int(Red(Point(x, y))/8)+Int(Green(Point(x, y))/8)<<5+Int(Blue(Point(x, y))/8)<<10
050:               EndIf
051:             Next x
052:           Next y
053:           AddElement(color())
054:           color() = 0
055:           SortList(color(), #PB_Sort_Ascending)
056:           WriteStringN(0, ReplaceString(GetFilePart(file), "."+GetExtensionPart(file), "")+"_palette:")
057:           WriteString(0, Chr(9)+".hword ")
058:           ForEach color()
059:             If i = 8
060:               WriteStringN(0, "")
061:               WriteString(0, Chr(9)+".hword ")
062:               i = 0
063:             ElseIf i>0
064:               WriteString(0, ",")
065:             EndIf
066:             i+1
067:             WriteString(0, "0x"+RSet(Hex(color()), 4, "0"))
068:           Next
069:           WriteStringN(0, "")
070:           WriteStringN(0, "")
071:          
072:           If CountProgramParameters() > 1
073:             StopDrawing()
074:             file.s = ProgramParameter()
075:             LoadImage(0, file)
076:             StartDrawing(ImageOutput(0))
077:           EndIf
078:           ; puis le sprite
079:          
080:             WriteStringN(0, ReplaceString(GetFilePart(file), "."+GetExtensionPart(file), "")+":")
081:             Define y2.l, x2.l
082:             For y = 0 To ImageHeight(0)-1 Step 8
083:               For x = 0 To ImageWidth(0)-1 Step 8
084:                 For y2 = 0 To 7
085:                   WriteString(0, Chr(9)+".word 0x")
086:                   For x2 = 7 To 0 Step -1
087:                     ForEach color()
088:                       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
089:                         WriteString(0, Hex(ListIndex(color())))
090:                         Break
091:                       EndIf
092:                     Next color()
093:                   Next x2
094:                   WriteStringN(0, "")
095:                 Next y2
096:               Next x
097:             Next y
098:           StopDrawing()
099:         EndIf
100:         PrintN(ReplaceString(GetFilePart(file), GetExtensionPart(file), "asm")+" correctly generated.")
101:         CloseFile(0)
102:       Else
103:         PrintN("Error : unable to create the output file.")
104:       EndIf
105:     Else
106:       PrintN("Error : incorrect image depth (use 16 colors).")
107:     EndIf
108:   Else
109:     PrintN("Error : unable to open the image.")
110:   EndIf
111: Else
112:   PrintN("Error : no input.")
113: EndIf
114: End
115: ; IDE Options = PureBasic 4.61 (Linux - x86)
116: ; CursorPosition = 72
117: ; FirstLine = 50
118: ; EnableXP
119: ; EnableUser
120: ; Executable = ../Projects/test/img2asm
121: ; CommandLine = "sprite.bmp"