qb64-include/savebinarypbm.bm

36 lines
1.1 KiB
Text
Raw Normal View History

2022-05-21 00:18:24 +02:00
Sub saveBinaryPBM (handle As Long, filename As String)
Dim imgwidth As Long
Dim imgheight As Long
Dim oldsource As Long
2022-06-13 09:42:00 +02:00
Dim header As String
2022-05-21 00:18:24 +02:00
Dim y As _Unsigned Long
Dim x As _Unsigned Long
2022-06-13 09:42:00 +02:00
Dim bytecount As _Unsigned Long
2022-05-21 00:18:24 +02:00
Dim c As _Byte
2022-06-13 09:42:00 +02:00
Dim outbyte As _Unsigned _Byte
2022-05-21 00:18:24 +02:00
oldsource = _Source
_Source handle
imgwidth = _Width(handle)
imgheight = _Height(handle)
Open filename For Binary As #1
2022-06-13 09:42:00 +02:00
header = "P4" + Chr$(10)
header = header + "# Created with QB64" + Chr$(10)
header = header + LTrim$(Str$(imgwidth) + Str$(imgheight)) + Chr$(10)
Put #1, , header
2022-05-21 00:18:24 +02:00
For y = 0 To imgheight - 1
For x = 0 To imgwidth - 1
c = (Point(x, y) = _RGB(0, 0, 0))
If c <> 0 Then
2022-06-13 09:42:00 +02:00
outbyte = _SetBit(outbyte, 7 - (bytecount Mod 8))
2022-05-21 00:18:24 +02:00
Else
2022-06-13 09:42:00 +02:00
outbyte = _ResetBit(outbyte, 7 - (bytecount Mod 8))
2022-05-21 00:18:24 +02:00
End If
2022-06-13 09:42:00 +02:00
If bytecount Mod 8 = 7 Then
Put #1, , outbyte
2022-05-21 00:18:24 +02:00
End If
2022-06-13 09:42:00 +02:00
bytecount = bytecount + 1
2022-05-21 00:18:24 +02:00
Next
Next
2022-06-13 09:42:00 +02:00
If (imgheight * imgwidth) Mod 8 <> 0 Then Put #1, , outbyte
2022-05-21 00:18:24 +02:00
Close #1
End Sub