pixel.bmx 4.9 KB

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