bmploader.bmx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. Strict
  2. Rem
  3. bbdoc: Graphics/BMP loader
  4. about:
  5. The BMP loader module provides the ability to load BMP format #pixmaps.
  6. End Rem
  7. Module BRL.BMPLoader
  8. ModuleInfo "Version: 1.07"
  9. ModuleInfo "Author: Simon Armstrong"
  10. ModuleInfo "License: zlib/libpng"
  11. ModuleInfo "Copyright: Blitz Research Ltd"
  12. ModuleInfo "Modserver: BRL"
  13. ModuleInfo "History: 1.07 Release"
  14. ModuleInfo "History: Added 32 bit alpha support"
  15. ModuleInfo "History: 1.06 Release"
  16. ModuleInfo "History: Fixed inverted 1 bit bitmaps"
  17. ModuleInfo "History: 1.05 Release"
  18. ModuleInfo "History: Fixed palettized bitmaps failing when biClrUsed=0"
  19. Import BRL.Pixmap
  20. Import BRL.EndianStream
  21. Type TPixmapLoaderBMP Extends TPixmapLoader
  22. Method LoadPixmap:TPixmap( stream:TStream )
  23. stream=LittleEndianStream( stream )
  24. Local line:Int[],palette:Int[],pix:Byte[],buf:Byte[64]
  25. Local pixmap:TPixmap
  26. Local hsize,hoffset,pad
  27. Local size,width,height
  28. Local planes,bits,compression,isize,xpels,ypels,cols,inuse
  29. Local w,x,y,c0,c1,p
  30. If stream.ReadBytes( buf,2 )=2
  31. If buf[0]=Asc("B") And buf[1]=Asc("M")
  32. hsize=ReadInt(stream)
  33. pad=ReadInt(stream)
  34. hoffset=ReadInt(stream)
  35. size=ReadInt(stream)
  36. width=ReadInt(stream)
  37. height=ReadInt(stream)
  38. planes=ReadShort(stream)
  39. bits=ReadShort(stream)
  40. compression=ReadInt(stream)
  41. isize=ReadInt(stream)
  42. xpels=ReadInt(stream)
  43. ypels=ReadInt(stream)
  44. cols=ReadInt(stream)
  45. inuse=ReadInt(stream)
  46. hoffset:-54
  47. If Not cols cols=1 Shl bits
  48. If bits=32
  49. pixmap=TPixmap.Create( width,height,PF_BGRA8888 )
  50. Else
  51. pixmap=TPixmap.Create( width,height,PF_BGR888 )
  52. EndIf
  53. Select bits
  54. Case 1
  55. c0=ReadInt(stream)
  56. c1=ReadInt(stream)
  57. w=(width+7)/8
  58. w=(w+3)&$fffc
  59. pix=New Byte[w]
  60. For y=height-1 To 0 Step -1
  61. stream.ReadBytes(pix,w)
  62. For x=0 Until width
  63. If pix[x Shr 3]&(128 Shr (x&7))
  64. ConvertPixels(Varptr c1,PF_BGR888,pixmap.pixelptr(x,y),pixmap.format,1)
  65. Else
  66. ConvertPixels(Varptr c0,PF_BGR888,pixmap.pixelptr(x,y),pixmap.format,1)
  67. EndIf
  68. Next
  69. Next
  70. Case 4
  71. palette=New Int[16]
  72. line=New Int[width]
  73. stream.ReadBytes(palette,cols*4)
  74. w=(width+1)/2
  75. w=(w+3)&$fffc
  76. pix=New Byte[w]
  77. For y=height-1 To 0 Step -1
  78. stream.ReadBytes(pix,w)
  79. For x=0 Until width
  80. p=(pix[x Shr 1]Shr((1-x&1)*4))&15
  81. line[x]=palette[p]
  82. Next
  83. ConvertPixels(line,PF_BGRA8888,pixmap.pixelptr(0,y),pixmap.format,width)
  84. Next
  85. Case 8
  86. palette=New Int[256]
  87. line=New Int[width]
  88. stream.ReadBytes(palette,cols*4)
  89. w=(width+3)&$fffc
  90. pix=New Byte[w]
  91. For y=height-1 To 0 Step -1
  92. stream.ReadBytes(pix,w)
  93. For x=0 Until width
  94. line[x]=palette[pix[x]&255]
  95. Next
  96. ConvertPixels(line,PF_BGRA8888,pixmap.pixelptr(0,y),pixmap.format,width)
  97. Next
  98. Case 24
  99. w=width*3
  100. w=(w+3)&$fffc
  101. pix=New Byte[w]
  102. For y=height-1 To 0 Step -1
  103. stream.ReadBytes(pix,w)
  104. ConvertPixels(pix,PF_BGR888,pixmap.pixelptr(0,y),pixmap.format,width)
  105. Next
  106. Case 32
  107. w=width*4
  108. pix=New Byte[w]
  109. For y=height-1 To 0 Step -1
  110. stream.ReadBytes(pix,w)
  111. ConvertPixels(pix,PF_BGRA8888,pixmap.pixelptr(0,y),pixmap.format,width)
  112. Next
  113. Default
  114. pixmap=Null
  115. End Select
  116. Return pixmap
  117. EndIf
  118. EndIf
  119. End Method
  120. End Type
  121. New TPixmapLoaderBMP