You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
6.3 KiB

5 years ago
  1. #include "Raster.h"
  2. #include "FreeImage.h"
  3. namespace CELL
  4. {
  5. unsigned loadImage(const char* fileName)
  6. {
  7. //1 ��ȡͼƬ��ʽ
  8. FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(fileName, 0);
  9. if (fifmt == FIF_UNKNOWN)
  10. {
  11. return 0;
  12. }
  13. //2 ����ͼƬ
  14. FIBITMAP *dib = FreeImage_Load(fifmt, fileName,0);
  15. FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(dib);
  16. //! ��ȡ����ָ��
  17. FIBITMAP* temp = dib;
  18. dib = FreeImage_ConvertTo32Bits(dib);
  19. FreeImage_Unload(temp);
  20. BYTE* pixels = (BYTE*)FreeImage_GetBits(dib);
  21. int width = FreeImage_GetWidth(dib);
  22. int height = FreeImage_GetHeight(dib);
  23. FreeImage_Unload(dib);
  24. }
  25. Raster::Raster( int w,int h,void* buffer )
  26. {
  27. _width = w;
  28. _height = h;
  29. _buffer = (uint*)buffer;
  30. }
  31. Raster::~Raster( void )
  32. {
  33. }
  34. void Raster::clear()
  35. {
  36. memset(_buffer,0,_width * _height * sizeof(Rgba));
  37. }
  38. void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
  39. {
  40. switch(ptSize)
  41. {
  42. case 1:
  43. setPixel(x,y,color);
  44. break;
  45. case 2:
  46. setPixel(x + 0, y + 0,color);
  47. setPixel(x + 1, y + 0,color);
  48. setPixel(x + 0, y + 1,color);
  49. setPixel(x + 1, y + 1,color);
  50. break;
  51. case 3:
  52. setPixel(x - 1, y - 1,color);
  53. setPixel(x + 0, y - 1,color);
  54. setPixel(x + 1, y - 1,color);
  55. setPixel(x - 1, y + 0,color);
  56. setPixel(x + 0, y + 0,color);
  57. setPixel(x + 1, y + 0,color);
  58. setPixel(x - 1, y + 1,color);
  59. setPixel(x + 0, y + 1,color);
  60. setPixel(x + 1, y + 1,color);
  61. break;
  62. }
  63. }
  64. void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
  65. {
  66. float xOffset = pt1.x - pt2.x;
  67. float yOffset = pt1.y - pt2.y;
  68. if (xOffset == 0 && yOffset == 0)
  69. {
  70. setPixel(pt1.x,pt1.y,color1);
  71. }
  72. if (fabs(xOffset) > fabs(yOffset))
  73. {
  74. float xMin;
  75. float xMax;
  76. if (pt1.x < pt2.x)
  77. {
  78. xMin = pt1.x;
  79. xMax = pt2.x;
  80. }
  81. else
  82. {
  83. xMin = pt2.x;
  84. xMax = pt1.x;
  85. }
  86. float lenth = xMax - xMin;
  87. float slope = yOffset / xOffset;
  88. for (float x = xMin; x <= xMax ; x += 1.0f)
  89. {
  90. float y = pt1.y + (x - pt1.x) * slope;
  91. float scaler = (x - xMin)/lenth;
  92. Rgba color = colorLerp(color1,color2,scaler);
  93. setPixel(x,y,color);
  94. }
  95. }
  96. else
  97. {
  98. float yMin;
  99. float yMax;
  100. if (pt1.y < pt2.y)
  101. {
  102. yMin = pt1.y;
  103. yMax = pt2.y;
  104. }
  105. else
  106. {
  107. yMin = pt2.y;
  108. yMax = pt1.y;
  109. }
  110. float lenth = yMax - yMin;
  111. float slope = xOffset / yOffset;
  112. for (float y = yMin; y <= yMax ; y += 1.0f)
  113. {
  114. float x = pt1.x + (y - pt1.y) * slope;
  115. float scaler = (y - yMin)/lenth;
  116. Rgba color = colorLerp(color1,color2,scaler);
  117. setPixel(x,y,color);
  118. }
  119. }
  120. }
  121. void Raster::drawArrays( DRAWMODE mode,const float2* points,int count )
  122. {
  123. switch (mode)
  124. {
  125. case DM_POINTS:
  126. {
  127. for (int i = 0 ;i < count ; ++ i)
  128. {
  129. drawPoints(points[i],_color);
  130. }
  131. }
  132. break;
  133. case DM_LINES:
  134. {
  135. count = count/2 * 2;
  136. for (int i = 0 ;i < count ; i += 2)
  137. {
  138. drawLine(points[i],points[i + 1],_color,_color);
  139. }
  140. }
  141. break;
  142. case DM_LINE_LOOP:
  143. {
  144. drawLine(points[0],points[1],_color,_color);
  145. for (int i = 2 ;i < count ; ++ i)
  146. {
  147. drawLine(points[i - 1],points[i],_color,_color);
  148. }
  149. drawLine(points[0],points[count - 1],_color,_color);
  150. }
  151. break;
  152. case DM_LINE_STRIP:
  153. {
  154. drawLine(points[0],points[1],_color,_color);
  155. for (int i = 2 ;i < count ; ++ i)
  156. {
  157. drawLine(points[i - 1],points[i],_color,_color);
  158. }
  159. }
  160. break;
  161. default:
  162. break;
  163. }
  164. }
  165. void Raster::drawFilleRect( int startX,int startY,int w,int h )
  166. {
  167. int left = tmax<int>(startX,0);
  168. int top = tmax<int>(startY,0);
  169. int right = tmin<int>(startX + w,_width);
  170. int bottom = tmin<int>(startY + h,_height);
  171. for (int x = left ; x < right ; ++ x)
  172. {
  173. for (int y = top ; y < bottom ; ++ y)
  174. {
  175. setPixelEx(x,y,_color);
  176. }
  177. }
  178. }
  179. void Raster::drawRect( const int2* points,const Rgba* colors )
  180. {
  181. int left = tmax<int>(points[0].x,0);
  182. int top = tmax<int>(points[0].y,0);
  183. int right = tmin<int>(points[2].x,_width);
  184. int bottom = tmin<int>(points[2].y,_height);
  185. float w = right - left;
  186. float h = bottom - top;
  187. for (int x = left ; x < right ; ++ x)
  188. {
  189. Rgba color1 = colorLerp(colors[0],colors[1],(x - left)/w);
  190. Rgba color2 = colorLerp(colors[3],colors[2],(x - left)/w);
  191. for (int y = top ; y < bottom ; ++ y)
  192. {
  193. Rgba color = colorLerp(color1,color2,(y - top)/h);
  194. setPixelEx(x,y,color);
  195. }
  196. }
  197. }
  198. }