@@ -25,6 +25,9 @@ Type TBitmapIndex
25
25
Global palR:Byte [256 ]
26
26
Global palG:Byte [256 ]
27
27
Global palB:Byte [256 ]
28
+
29
+ 'Data Streams
30
+ Global dataStream:TStream
28
31
29
32
'Load color table file
30
33
Function FLoadPalette (paletteFile:String )
@@ -42,11 +45,11 @@ Type TBitmapIndex
42
45
43
46
'Indexed Bitmap File Writer
44
47
Function FPixmapToIndexedBitmap (image:TPixmap ,filename:String )
45
- 'Variables
48
+ 'Variables
49
+ Local paletteIndex:Int
46
50
Local bmpWidth:Int , bmpWidthM4:Int
47
51
Local bmpHeight:Int
48
52
Local bmpSizeTotal:Int , bmpSizeTotalM4:Int
49
- Local paletteIndex:Int = 0
50
53
51
54
'Dimensions calc
52
55
bmpWidth = PixmapWidth(image)
@@ -58,24 +61,19 @@ Type TBitmapIndex
58
61
bmpSizeTotalM4 = ((bmpSizeTotal + 3 ) / 4 ) * 4
59
62
60
63
'Begin writing BMP file manually
61
- Local dataStream: TStream = WriteFile(filename)
64
+ dataStream = WriteFile(filename)
62
65
63
66
'------ Bitmap File Header
64
- 'Header file data is stored in little-endian format (least-significant byte first)
67
+ 'Data is stored in little-endian format (least-significant byte first)
65
68
dataStream = LittleEndianStream(dataStream)
66
69
67
70
WriteShort(dataStream,19778 ) 'File ID (2 bytes (short)) - 19778 (deci) or 42 4D (hex) or BM (ascii) for bitmap
68
71
WriteInt(dataStream,bmpSizeTotalM4) 'File Size (4 bytes (signed int))
69
72
WriteShort(dataStream,0 ) 'Reserved (2 bytes)
70
73
WriteShort(dataStream,0 ) 'Reserved (2 bytes)
71
74
WriteInt(dataStream,54 ) 'Pixel Array Offset (4 bytes) - pixel array starts at 54th byte
72
- 'Print("wrote header")
73
-
74
- 'CloseStream(headerStream)
75
75
76
76
'------ DIB Header (File Info)
77
- 'dataStream = BigEndianStream(dataStream)
78
-
79
77
WriteInt(dataStream,40 ) 'DIB Header Size (4 bytes) - 40 bytes
80
78
WriteInt(dataStream,bmpWidth) 'Bitmap Width (4 bytes)
81
79
WriteInt(dataStream,bmpHeight) 'Bitmap Height (4 bytes)
@@ -87,59 +85,60 @@ Type TBitmapIndex
87
85
WriteInt(dataStream,2835 ) 'Vertical resolution of the image (4 bytes) - Pixels Per Metre (2835 PPM equals 72.009 DPI/PPI)
88
86
WriteInt(dataStream,256 ) 'Number of colors in the color palette (4 bytes)
89
87
WriteInt(dataStream,0 ) 'Number of important colors (4 bytes) - 0 when every color is important
90
- 'Print("wrote dib")
91
88
92
89
'------ Color Table
93
90
For paletteIndex = 0 To 255
94
- WriteByte(dataStream,PalB [ paletteIndex] ) 'Blue (4 bytes) - offset 54
95
- WriteByte(dataStream,PalG [ paletteIndex] ) 'Green (4 bytes) - offset 58
96
- WriteByte(dataStream,PalR [ paletteIndex] ) 'Red (4 bytes) - offset 62
91
+ WriteByte(dataStream,palB [ paletteIndex] ) 'Blue (4 bytes) - offset 54
92
+ WriteByte(dataStream,palG [ paletteIndex] ) 'Green (4 bytes) - offset 58
93
+ WriteByte(dataStream,palR [ paletteIndex] ) 'Red (4 bytes) - offset 62
97
94
WriteByte(dataStream,0 ) 'Alpha (4 bytes) - offset 66
98
- If paletteIndex = 255 Then
99
- 'Print("wrote color table")
100
- EndIf
101
95
Next
102
96
103
97
'------ Pixel Array
104
98
Local px:Int , py:Int
105
- Local R:Int , G:Int , B:Int , ARGB:Long
106
- Local bestDistance:Int , bestIndex:Int , distance:Int
107
- Local RDIFF:Int , GDIFF:Int , BDIFF:Int
108
-
99
+ Local pixelData:Long
100
+ Local bestIndex:Int = 0
101
+ Local magenta:Int = 16711935
109
102
For py = bmpHeight - 1 To 0 Step - 1
110
103
For px = 0 To bmpWidthM4 - 1
111
- If px < bmpWidth 'if a valid pixel on canvas
112
- bestDistance = 17000000
113
- bestIndex = 0
114
- distance = 0
115
-
116
- For paletteIndex = 0 To 255 ' Check all color indexes for best match by pythagora
117
- ARGB = ReadPixel(image,px,py)
118
- R = (ARGB & $00FF0000 ) Shr 16
119
- G = (ARGB & $FF00 ) Shr 8
120
- B = (ARGB & $FF )
121
- RDIFF = Abs(R - palR[ paletteIndex] )
122
- GDIFF = Abs(G - palG[ paletteIndex] )
123
- BDIFF = Abs(B - palB[ paletteIndex] )
124
- distance = (RDIFF^ 2 + GDIFF^ 2 + BDIFF^ 2 )
125
- If distance <= bestDistance Then
126
- bestIndex = paletteIndex
127
- bestDistance = distance
128
- EndIf
129
- 'Print("working" + paletteIndex)
130
- Next
104
+ 'if a valid pixel on canvas
105
+ If px < bmpWidth
106
+ 'Read pixel data
107
+ pixelData = ReadPixel(image,px,py)
108
+ 'skip diffing magenta
109
+ If pixelData = 16711935 Then
110
+ WriteByte(dataStream,1 )
111
+ Else
112
+ 'Check all color indexes for best match by pythagora
113
+ Local R:Int , G:Int , B:Int
114
+ Local RDIFF:Int , GDIFF:Int , BDIFF:Int
115
+ Local bestDistance:Int = 17000000
116
+ Local distance:Int = 0
117
+ For paletteIndex = 0 To 255
118
+ R = (pixelData & $00FF0000 ) Shr 16
119
+ G = (pixelData & $FF00 ) Shr 8
120
+ B = (pixelData & $FF )
121
+ RDIFF = Abs(R - palR[ paletteIndex] )
122
+ GDIFF = Abs(G - palG[ paletteIndex] )
123
+ BDIFF = Abs(B - palB[ paletteIndex] )
124
+ distance = (RDIFF^ 2 + GDIFF^ 2 + BDIFF^ 2 )
125
+ If distance <= bestDistance Then
126
+ bestIndex = paletteIndex
127
+ bestDistance = distance
128
+ EndIf
129
+ Next
130
+ EndIf
131
131
WriteByte(dataStream,bestIndex)
132
132
Else
133
- WriteByte(dataStream,0 ) ' line padding
133
+ WriteByte(dataStream,0 ) 'line padding
134
134
EndIf
135
135
Next
136
136
Next
137
- For paletteIndex = 1 To bmpSizeTotalM4 - bmpSizeTotal ' eof padding
137
+ For paletteIndex = 1 To bmpSizeTotalM4 - bmpSizeTotal 'eof padding
138
138
WriteByte(dataStream,0 )
139
139
Next
140
140
141
141
CloseStream(dataStream)
142
- 'Print ("stream closed")
143
142
EndFunction
144
143
145
144
EndType
@@ -384,7 +383,6 @@ Type TAppOutput
384
383
SetRotation(0 )
385
384
'Output copy for saving
386
385
TAppFileIO.tempOutputImage = GrabPixmap(0 ,96 ,768 ,384 )
387
- 'ConvertPixmap(TAppFileIO.tempOutputImage,PF_BGR888)
388
386
Flip(1 )
389
387
If TAppFileIO.prepForSave
390
388
TAppFileIO.FPrepForSave()
0 commit comments