bmp.bmx 3.7 KB

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