1
1
Import " Utility.bmx "
2
- Import " UtilityPNG .bmx"
2
+ Import " IndexedPixmap .bmx"
3
3
4
4
'//// INDEXED IMAGE WRITER //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
5
5
@@ -246,40 +246,47 @@ EndRem
246
246
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
247
247
248
248
Method WriteIndexedPNGFromPixmap :Int (sourcePixmap:TPixmap , filename:String , compression:Int = 5 )
249
- Local outputStream:TStream = WriteStream(filename)
249
+ If filename = Null Then
250
+ Return False
251
+ Else
252
+ 'Begin writing PNG file manually
253
+ Local outputStream:TStream = WriteStream(filename)
250
254
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)
255
+ Try
256
+ Local pngPtr:Byte Ptr = png_create_write_struct(" 1.6.37" , Null , Null , Null )
257
+ Local pngInfoPtr:Byte Ptr = png_create_info_struct(pngPtr)
254
258
255
- png_set_write_fn(pngPtr, outputStream, UtilityPNG.PNGWrite, UtilityPNG.PNGFlush )
259
+ png_set_write_fn(pngPtr, outputStream, Utility.PNGWriteStream, Utility.PNGFlushStream )
256
260
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)
261
+ png_set_compression_level(pngPtr, Utility.Clamp(compression, 0 , 9 ))
262
+ 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
263
260
- Local palettePtr:Byte Ptr = m_Palette
261
- png_set_PLTE(pngPtr, pngInfoPtr, palettePtr, 256 );
264
+ Local palettePtr:Byte Ptr = m_Palette
265
+ png_set_PLTE(pngPtr, pngInfoPtr, palettePtr, 256 );
262
266
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
+ Local convertingPixmap:IndexedPixmap = New IndexedPixmap (sourcePixmap.Width, sourcePixmap.Height)
268
+ For Local pixelY:Int = 0 Until sourcePixmap.Height
269
+ For Local pixelX:Int = 0 Until sourcePixmap.Width
270
+ convertingPixmap.WritePixel(pixelX, pixelY, ConvertColorToClosestIndex(ReadPixel(sourcePixmap, pixelX, pixelY)))
271
+ Next
267
272
Next
268
- Next
269
273
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)
274
+ Local rows:Byte Ptr [ sourcePixmap.Height]
275
+ For Local i = 0 Until sourcePixmap.Height
276
+ rows[ i] = convertingPixmap .PixelPtr(0 , i)
277
+ Next
278
+ png_set_rows(pngPtr, pngInfoPtr, rows)
275
279
276
- png_write_png(pngPtr, pngInfoPtr, 0 , Null )
277
- png_destroy_write_struct(Varptr pngPtr, Varptr pngInfoPtr, Null )
280
+ png_write_png(pngPtr, pngInfoPtr, 0 , Null )
281
+ png_destroy_write_struct(Varptr pngPtr, Varptr pngInfoPtr, Null )
278
282
279
- CloseStream(outputStream)
280
- Return True
281
- Catch error:String
282
- If error <> " PNG ERROR" Throw error
283
- EndTry
283
+ CloseStream(outputStream)
284
+ Return True
285
+ Catch error:String
286
+ If error <> " PNG ERROR" Then
287
+ Throw error
288
+ EndIf
289
+ EndTry
290
+ EndIf
284
291
EndMethod
285
292
EndType
0 commit comments