1
- Import BRL.Bank
1
+ Import " Utility.bmx "
2
+ Import " UtilityPNG.bmx "
2
3
3
4
'//// INDEXED IMAGE WRITER //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
4
5
6
+ Struct RGBColor
7
+ Field m_R:Byte
8
+ Field m_G:Byte
9
+ Field m_B:Byte
10
+ EndStruct
11
+
12
+ '////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
13
+
5
14
Type IndexedImageWriter
6
15
Field m_PalR:Byte [256 ]
7
16
Field m_PalG:Byte [256 ]
8
17
Field m_PalB:Byte [256 ]
9
18
10
- Field m_CRCTable:Int [256 ]
19
+ Field m_Palette:RGBColor [256 ]
20
+
21
+ 'Field m_CRCTable:Int[256]
11
22
12
23
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
13
24
14
25
Method New ()
15
26
LoadDefaultPalette()
16
-
27
+ Rem
17
28
'Initialize CRC table
18
29
For Local i:Int = 0 To 255
19
30
Local value:Int = i
@@ -26,18 +37,19 @@ Type IndexedImageWriter
26
37
Next
27
38
m_CRCTable[i] = value
28
39
Next
40
+ EndRem
29
41
EndMethod
30
42
31
43
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
32
-
44
+ Rem
33
45
Method GenerateCRC32FromBank:Int(bank:TBank)
34
46
Local crcResult:Int = $FFFFFFFF
35
47
For Local i:Int = 0 Until BankSize(bank)
36
48
crcResult = (crcResult Shr 8) ~ m_CRCTable[PeekByte(bank, i) ~ (crcResult & $FF)]
37
49
Next
38
50
Return ~crcResult '~ for bitwise complement
39
51
EndMethod
40
-
52
+ EndRem
41
53
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
42
54
43
55
Method LoadDefaultPalette ()
@@ -50,6 +62,10 @@ Type IndexedImageWriter
50
62
m_PalR[ index] = ReadByte(paletteStream)
51
63
m_PalG[ index] = ReadByte(paletteStream)
52
64
m_PalB[ index] = ReadByte(paletteStream)
65
+
66
+ m_Palette[ index] .m_R = m_PalR[ index]
67
+ m_Palette[ index] .m_G = m_PalG[ index]
68
+ m_Palette[ index] .m_B = m_PalB[ index]
53
69
Next
54
70
CloseStream(paletteStream)
55
71
EndMethod
@@ -147,7 +163,7 @@ Type IndexedImageWriter
147
163
EndMethod
148
164
149
165
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
150
-
166
+ Rem
151
167
Method WriteIndexedPNGFromPixmap:Int(sourcePixmap:TPixmap, filename:String)
152
168
If filename = Null Then
153
169
Return False
@@ -176,15 +192,13 @@ Type IndexedImageWriter
176
192
177
193
WriteInt(outputStream, pngWidth) 'Image Width (4 bytes)
178
194
WriteInt(outputStream, pngHeight) 'Image Height (4 bytes)
179
- WriteByte(outputStream, 4 ) 'Bit Depth (1 byte) - BYTES per pixel not BITS per pixel
195
+ WriteByte(outputStream, 8 ) 'Bit Depth (1 byte)
180
196
WriteByte(outputStream, 3) 'Color Type (1 byte) - 3 for indexed color
181
197
WriteByte(outputStream, 0) 'Compression Method (1 byte)
182
198
WriteByte(outputStream, 0) 'Filter Method (1 byte)
183
199
WriteByte(outputStream, 0) 'Interlace Method (1 byte) - 0 for no interlace
184
200
185
- Rem
186
- Figure out how to generate correct CRC
187
- EndRem
201
+ 'Figure out how to generate correct CRC
188
202
WriteInt(outputStream, 0) 'CRC-32 checksum (4 bytes)
189
203
190
204
'PLTE chunk (color table)
@@ -197,9 +211,7 @@ Type IndexedImageWriter
197
211
WriteByte(outputStream, m_PalR[index]) 'Red (1 byte)
198
212
Next
199
213
200
- Rem
201
- Figure out how to generate correct CRC
202
- EndRem
214
+ 'Figure out how to generate correct CRC
203
215
WriteInt(outputStream, 0) 'CRC-32 checksum (4 bytes)
204
216
205
217
'IDAT chunk (pixel array)
@@ -216,22 +228,58 @@ Type IndexedImageWriter
216
228
Next
217
229
Next
218
230
219
- Rem
220
- Figure out how to generate correct CRC
221
- EndRem
231
+ 'Figure out how to generate correct CRC
222
232
WriteInt(outputStream, 0) 'CRC-32 checksum (4 bytes)
223
233
224
234
'IEND chunk (EOF)
225
235
WriteInt(outputStream, 0) 'Chunk Length (4 bytes) - 0 for IEND
226
236
WriteInt(outputStream, 1229278788) 'Chunk Type (4 bytes) - 1229278788 (decimal) or 49 45 4E 44 (hex) or IEND (ascii)
227
237
228
- Rem
229
- Figure out how to generate correct CRC
230
- EndRem
238
+ 'Figure out how to generate correct CRC
231
239
WriteInt(outputStream, 0) 'CRC-32 checksum (4 bytes)
232
240
233
241
CloseStream(outputStream)
234
242
Return True
235
243
EndIf
236
244
EndMethod
245
+ EndRem
246
+ '////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
247
+
248
+ Method WriteIndexedPNGFromPixmap :Int (sourcePixmap:TPixmap , filename:String , compression:Int = 5 )
249
+ Local outputStream:TStream = WriteStream(filename)
250
+
251
+ Try
252
+ Local pngPtr:Byte Ptr = png_create_write_struct(" 1.6.37" , Null , Null , Null )
253
+ Local pngInfoPtr:Byte Ptr = png_create_info_struct(pngPtr)
254
+
255
+ png_set_write_fn(pngPtr, outputStream, UtilityPNG.PNGWrite, UtilityPNG.PNGFlush)
256
+
257
+ png_set_compression_level(pngPtr, Utility.Clamp(compression, 0 , 9 ))
258
+ png_set_IHDR(pngPtr, pngInfoPtr, sourcePixmap.Width, sourcePixmap.Height, 8 , PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT)
259
+
260
+ Local palettePtr:Byte Ptr = m_Palette
261
+ png_set_PLTE(pngPtr, pngInfoPtr, palettePtr, 256 );
262
+
263
+ 'sourcePixmap = sourcePixmap.Convert(PF_I8)
264
+ For Local pixelY:Int = 0 Until sourcePixmap.Height
265
+ For Local pixelX:Int = 0 Until sourcePixmap.Width
266
+ WritePixel(sourcePixmap, pixelX, pixelY, ConvertColorToClosestIndex(ReadPixel(sourcePixmap, pixelX, pixelY)))
267
+ Next
268
+ Next
269
+
270
+ Local rows:Byte Ptr [ sourcePixmap.Height]
271
+ For Local i = 0 Until sourcePixmap.Height
272
+ rows[ i] = sourcePixmap.PixelPtr(0 , i)
273
+ Next
274
+ png_set_rows(pngPtr, pngInfoPtr, rows)
275
+
276
+ png_write_png(pngPtr, pngInfoPtr, 0 , Null )
277
+ png_destroy_write_struct(Varptr pngPtr, Varptr pngInfoPtr, Null )
278
+
279
+ CloseStream(outputStream)
280
+ Return True
281
+ Catch error:String
282
+ If error <> " PNG ERROR" Throw error
283
+ EndTry
284
+ EndMethod
237
285
EndType
0 commit comments