pcx.bmx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ' Copyright (c) 2022 Bruce A Henderson
  2. '
  3. ' This software is provided 'as-is', without any express or implied
  4. ' warranty. In no event will the authors be held liable for any damages
  5. ' arising from the use of this software.
  6. '
  7. ' Permission is granted to anyone to use this software for any purpose,
  8. ' including commercial applications, and to alter it and redistribute it
  9. ' freely, subject to the following restrictions:
  10. '
  11. ' 1. The origin of this software must not be misrepresented; you must not
  12. ' claim that you wrote the original software. If you use this software
  13. ' in a product, an acknowledgment in the product documentation would be
  14. ' appreciated but is not required.
  15. ' 2. Altered source versions must be plainly marked as such, and must not be
  16. ' misrepresented as being the original software.
  17. ' 3. This notice may not be removed or altered from any source distribution.
  18. '
  19. SuperStrict
  20. Rem
  21. bbdoc: Image/PCX loader
  22. about:
  23. The PCX loader module provides the ability to load PCX format #pixmaps.
  24. End Rem
  25. Module Image.PCX
  26. ModuleInfo "Version: 1.00"
  27. ModuleInfo "License: ZLib/PNG License"
  28. ModuleInfo "Author: Bruce A Henderson"
  29. ModuleInfo "History: 1.00"
  30. ModuleInfo "History: Initial Release."
  31. ModuleInfo "C_OPTS: -std=c99"
  32. Import BRL.pixmap
  33. Import "common.bmx"
  34. Rem
  35. bbdoc: PCX image format files.
  36. End Rem
  37. Type TPcxImage
  38. Rem
  39. bbdoc: Loads PCX image from a file as a #TPixmap.
  40. End Rem
  41. Function Load:TPixmap(file:String, channels:Int = 0)
  42. Local stream:TStream = ReadStream(file)
  43. If stream Then
  44. Local pix:TPixmap = Load(stream, channels)
  45. stream.Close()
  46. Return pix
  47. End If
  48. End Function
  49. Rem
  50. bbdoc: Loads PCX image from stream as a #TPixmap.
  51. End Rem
  52. Function Load:TPixmap(stream:TStream, channels:Int = 0)
  53. Local width:Int
  54. Local height:Int
  55. Local components:Int
  56. Local pixels:Byte Ptr = drpcx_load(_ReadCallback, stream, False, width, height, components, 0)
  57. If pixels Then
  58. Local format:Int = PF_RGB888
  59. Local align:Int = 3
  60. If components = 4 Then
  61. format = PF_RGBA8888
  62. align = 4
  63. End If
  64. Local pix:TPixmap = TPixmap.Create(width, height, format, align)
  65. MemCopy(pix.pixels, pixels, Size_T(width * height * align))
  66. drpcx_free(pixels)
  67. Return pix
  68. End If
  69. End Function
  70. Function _ReadCallback:Size_T(streamObj:Object, bufferOut:Byte Ptr, bytesToRead:Size_T)
  71. Try
  72. Local stream:TStream = TStream(streamObj)
  73. If stream Then
  74. Local res:Long = stream.ReadBytes(bufferOut, Long(bytesToRead))
  75. Return res
  76. End If
  77. Catch e:Object
  78. End Try
  79. Return 0
  80. End Function
  81. End Type
  82. Private
  83. Type TPixmapLoaderPcx Extends TPixmapLoader
  84. Method LoadPixmap:TPixmap( stream:TStream ) Override
  85. Return TPcxImage.Load(stream)
  86. End Method
  87. End Type
  88. New TPixmapLoaderPcx