pixel.bmx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. SuperStrict
  2. Const PF_I8:Int= 1
  3. Const PF_A8:Int= 2
  4. Const PF_BGR888:Int= 3
  5. Const PF_RGB888:Int= 4
  6. Const PF_BGRA8888:Int= 5
  7. Const PF_RGBA8888:Int= 6
  8. Const PF_ARGB8888:Int= 13
  9. Const PF_ABGR8888:Int= 14
  10. Const PF_STDFORMAT:Int= PF_RGBA8888
  11. 'New pixel formats
  12. '
  13. 'These are GL compatible - all are 8 bit versions.
  14. '
  15. 'NOT FULLY IMPLEMENTED YET!!!!!
  16. '
  17. Const PF_RED:Int= 7
  18. Const PF_GREEN:Int= 8
  19. Const PF_BLUE:Int= 9
  20. Const PF_ALPHA:Int= 10
  21. Const PF_INTENSITY:Int= 11
  22. Const PF_LUMINANCE:Int= 12
  23. Const PF_RGB:Int= PF_RGB888
  24. Const PF_BGR:Int= PF_BGR888
  25. Const PF_RGBA:Int= PF_RGBA8888
  26. Const PF_BGRA:Int= PF_BGRA8888
  27. Const PF_ARGB:Int= PF_ARGB8888
  28. Const PF_ABGR:Int= PF_ABGR8888
  29. ?BigEndian
  30. Const PF_COLOR:Int= PF_RGB
  31. Const PF_COLORALPHA:Int= PF_RGBA
  32. ?LittleEndian
  33. Const PF_COLOR:Int= PF_BGR
  34. Const PF_COLORALPHA:Int= PF_BGRA
  35. ?
  36. Global BytesPerPixel:Int[]= [0,1,1,3,3,4,4, 1,1,1,1,1,1,4,4]
  37. Global RedBitsPerPixel:Int[]= [1,0,0,8,8,8,8, 8,0,0,0,0,0,8,8] ' Max2d compressed textures version
  38. Global GreenBitsPerPixel:Int[]= [0,0,0,8,8,8,8, 0,8,0,0,0,0,8,8] ' stores dds format
  39. Global BlueBitsPerPixel:Int[]= [0,0,0,8,8,8,8, 0,0,8,0,0,0,8,8] ' stores texture name
  40. Global AlphaBitsPerPixel:Int[]= [0,0,8,0,0,8,8, 0,0,0,8,0,0,8,8]
  41. Global IntensityBitsPerPixel:Int[]= [0,0,0,0,0,0,0, 0,0,0,0,8,0,0,0]
  42. Global LuminanceBitsPerPixel:Int[]= [0,0,0,0,0,0,0, 0,0,0,0,0,8,0,0]
  43. Global BitsPerPixel:Int[]= [0,8,8,24,24,32,32, 4,4,4,4,4,4,32,32]
  44. Global ColorBitsPerPixel:Int[]= [0,0,0,24,24,24,24, 8,8,8,0,0,0,24,24]
  45. Function CopyPixels( in_buf:Byte Ptr,out_buf:Byte Ptr,format:Int,count:Int )
  46. MemCopy out_buf,in_buf, Size_T(count*BytesPerPixel[format])
  47. End Function
  48. Function ConvertPixels( in_buf:Byte Ptr,in_format:Int,out_buf:Byte Ptr,out_format:Int,count:Int )
  49. If in_format=out_format
  50. CopyPixels in_buf,out_buf,out_format,count
  51. Else If in_format=PF_STDFORMAT
  52. ConvertPixelsFromStdFormat in_buf,out_buf,out_format,count
  53. Else If out_format=PF_STDFORMAT
  54. ConvertPixelsToStdFormat in_buf,out_buf,in_format,count
  55. Else
  56. Local tmp_buf:Int[count]
  57. ConvertPixelsToStdFormat in_buf,tmp_buf,in_format,count
  58. ConvertPixelsFromStdFormat tmp_buf,out_buf,out_format,count
  59. EndIf
  60. End Function
  61. Function ConvertPixelsToStdFormat( in_buf:Byte Ptr,out_buf:Byte Ptr,format:Int,count:Int )
  62. Local in:Byte Ptr=in_buf
  63. Local out:Byte Ptr=out_buf
  64. Local out_end:Byte Ptr=out+count*BytesPerPixel[PF_STDFORMAT]
  65. Select format
  66. Case PF_A8
  67. While out<>out_end
  68. out[0]=255
  69. out[1]=255
  70. out[2]=255
  71. out[3]=in[0]
  72. in:+1;out:+4
  73. Wend
  74. Case PF_I8
  75. While out<>out_end
  76. out[0]=in[0]
  77. out[1]=in[0]
  78. out[2]=in[0]
  79. out[3]=255
  80. in:+1;out:+4
  81. Wend
  82. Case PF_RGB888
  83. While out<>out_end
  84. out[0]=in[0]
  85. out[1]=in[1]
  86. out[2]=in[2]
  87. out[3]=255
  88. in:+3;out:+4
  89. Wend
  90. Case PF_BGR888
  91. While out<>out_end
  92. out[0]=in[2]
  93. out[1]=in[1]
  94. out[2]=in[0]
  95. out[3]=255
  96. in:+3;out:+4
  97. Wend
  98. Case PF_BGRA8888
  99. While out<>out_end
  100. out[0]=in[2]
  101. out[1]=in[1]
  102. out[2]=in[0]
  103. out[3]=in[3]
  104. in:+4;out:+4
  105. Wend
  106. Case PF_ARGB8888
  107. While out<>out_end
  108. out[0]=in[1]
  109. out[1]=in[2]
  110. out[2]=in[3]
  111. out[3]=in[0]
  112. in:+4;out:+4
  113. Wend
  114. Case PF_ABGR8888
  115. While out<>out_end
  116. out[0]=in[3]
  117. out[1]=in[2]
  118. out[2]=in[1]
  119. out[3]=in[0]
  120. in:+4;out:+4
  121. Wend
  122. Case PF_RED
  123. While out<>out_end
  124. out[0]=in[0]
  125. out[1]=0
  126. out[2]=0
  127. out[3]=1
  128. in:+1;out:+4
  129. Wend
  130. Case PF_GREEN
  131. While out<>out_end
  132. out[0]=0
  133. out[1]=in[0]
  134. out[2]=0
  135. out[3]=1
  136. in:+1;out:+4
  137. Wend
  138. Case PF_BLUE
  139. While out<>out_end
  140. out[0]=0
  141. out[1]=0
  142. out[2]=in[0]
  143. out[3]=1
  144. in:+1;out:+4
  145. Wend
  146. Case PF_ALPHA
  147. While out<>out_end
  148. out[0]=0
  149. out[1]=0
  150. out[2]=0
  151. out[3]=in[0]
  152. in:+1;out:+4
  153. Wend
  154. Case PF_INTENSITY
  155. While out<>out_end
  156. out[0]=in[0]
  157. out[1]=in[0]
  158. out[2]=in[0]
  159. out[3]=in[0]
  160. in:+1;out:+4
  161. Wend
  162. Case PF_LUMINANCE
  163. While out<>out_end
  164. out[0]=in[0]
  165. out[1]=in[0]
  166. out[2]=in[0]
  167. out[3]=1
  168. in:+1;out:+4
  169. Wend
  170. Case PF_STDFORMAT
  171. CopyPixels in_buf,out_buf,PF_STDFORMAT,count
  172. End Select
  173. End Function
  174. Function ConvertPixelsFromStdFormat( in_buf:Byte Ptr,out_buf:Byte Ptr,format:Int,count:Int )
  175. Local out:Byte Ptr=out_buf
  176. Local in:Byte Ptr=in_buf
  177. Local in_end:Byte Ptr=in+count*BytesPerPixel[PF_STDFORMAT]
  178. Select format
  179. Case PF_A8
  180. While in<>in_end
  181. out[0]=in[3]
  182. in:+4;out:+1
  183. Wend
  184. Case PF_I8
  185. While in<>in_end
  186. out[0]=(in[0]+in[1]+in[2])/3
  187. in:+4;out:+1
  188. Wend
  189. Case PF_RGB888
  190. While in<>in_end
  191. out[0]=in[0]
  192. out[1]=in[1]
  193. out[2]=in[2]
  194. in:+4;out:+3
  195. Wend
  196. Case PF_BGR888
  197. While in<>in_end
  198. out[0]=in[2]
  199. out[1]=in[1]
  200. out[2]=in[0]
  201. in:+4;out:+3
  202. Wend
  203. Case PF_BGRA8888
  204. While in<>in_end
  205. out[0]=in[2]
  206. out[1]=in[1]
  207. out[2]=in[0]
  208. out[3]=in[3]
  209. in:+4;out:+4
  210. Wend
  211. Case PF_ARGB8888 ' RGBA -> ARGB
  212. While in<>in_end
  213. out[0]=in[3]
  214. out[1]=in[0]
  215. out[2]=in[1]
  216. out[3]=in[2]
  217. in:+4;out:+4
  218. Wend
  219. Case PF_ABGR8888 ' RGBA -> ABGR
  220. While in<>in_end
  221. out[0]=in[3]
  222. out[1]=in[2]
  223. out[2]=in[1]
  224. out[3]=in[0]
  225. in:+4;out:+4
  226. Wend
  227. Case PF_RED
  228. While in<>in_end
  229. out[0]=in[0]
  230. in:+4;out:+1
  231. Wend
  232. Case PF_GREEN
  233. While in<>in_end
  234. out[0]=in[1]
  235. in:+4;out:+1
  236. Wend
  237. Case PF_BLUE
  238. While in<>in_end
  239. out[0]=in[2]
  240. in:+4;out:+1
  241. Wend
  242. Case PF_ALPHA
  243. While in<>in_end
  244. out[0]=in[3]
  245. in:+4;out:+1
  246. Wend
  247. Case PF_INTENSITY
  248. While in<>in_end
  249. out[0]=(in[0]+in[1]+in[2]+in[3])/4
  250. in:+4;out:+1
  251. Wend
  252. Case PF_LUMINANCE
  253. While in<>in_end
  254. out[0]=(in[0]+in[1]+in[2])/3
  255. in:+4;out:+1
  256. Wend
  257. Case PF_STDFORMAT
  258. CopyPixels in_buf,out_buf,PF_STDFORMAT,count
  259. End Select
  260. End Function