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.

230 lines
6.4 KiB

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