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.

343 lines
10 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. int pitch = width*4;
  24. BYTE* row = new BYTE[width*4];
  25. for(int j = 0; j < height / 2; j++)
  26. {
  27. memcpy(row,pixels + j * pitch,pitch );
  28. memcpy(pixels + j * pitch,pixels + (height - j - 1) * pitch,pitch );
  29. memcpy(pixels + (height - j - 1) * pitch,row,pitch );
  30. }
  31. delete []row;
  32. Image* image = new Image(width,height,pixels);
  33. FreeImage_Unload(dib);
  34. return image;
  35. }
  36. Raster::Raster( int w,int h,void* buffer )
  37. {
  38. _width = w;
  39. _height = h;
  40. _buffer = (uint*)buffer;
  41. }
  42. Raster::~Raster( void )
  43. {
  44. }
  45. void Raster::clear()
  46. {
  47. memset(_buffer,0,_width * _height * sizeof(Rgba));
  48. }
  49. void Raster::drawPoint( int x,int y, Rgba color,int ptSize )
  50. {
  51. switch(ptSize)
  52. {
  53. case 1:
  54. setPixel(x,y,color);
  55. break;
  56. case 2:
  57. setPixel(x + 0, y + 0,color);
  58. setPixel(x + 1, y + 0,color);
  59. setPixel(x + 0, y + 1,color);
  60. setPixel(x + 1, y + 1,color);
  61. break;
  62. case 3:
  63. setPixel(x - 1, y - 1,color);
  64. setPixel(x + 0, y - 1,color);
  65. setPixel(x + 1, y - 1,color);
  66. setPixel(x - 1, y + 0,color);
  67. setPixel(x + 0, y + 0,color);
  68. setPixel(x + 1, y + 0,color);
  69. setPixel(x - 1, y + 1,color);
  70. setPixel(x + 0, y + 1,color);
  71. setPixel(x + 1, y + 1,color);
  72. break;
  73. }
  74. }
  75. void Raster::drawLine( float2 pt1,float2 pt2,Rgba color1,Rgba color2 )
  76. {
  77. float xOffset = pt1.x - pt2.x;
  78. float yOffset = pt1.y - pt2.y;
  79. if (xOffset == 0 && yOffset == 0)
  80. {
  81. setPixel(pt1.x,pt1.y,color1);
  82. }
  83. if (fabs(xOffset) > fabs(yOffset))
  84. {
  85. float xMin;
  86. float xMax;
  87. if (pt1.x < pt2.x)
  88. {
  89. xMin = pt1.x;
  90. xMax = pt2.x;
  91. }
  92. else
  93. {
  94. xMin = pt2.x;
  95. xMax = pt1.x;
  96. }
  97. float lenth = xMax - xMin;
  98. float slope = yOffset / xOffset;
  99. for (float x = xMin; x <= xMax ; x += 1.0f)
  100. {
  101. float y = pt1.y + (x - pt1.x) * slope;
  102. float scaler = (x - xMin)/lenth;
  103. Rgba color = colorLerp(color1,color2,scaler);
  104. setPixel(x,y,color);
  105. }
  106. }
  107. else
  108. {
  109. float yMin;
  110. float yMax;
  111. if (pt1.y < pt2.y)
  112. {
  113. yMin = pt1.y;
  114. yMax = pt2.y;
  115. }
  116. else
  117. {
  118. yMin = pt2.y;
  119. yMax = pt1.y;
  120. }
  121. float lenth = yMax - yMin;
  122. float slope = xOffset / yOffset;
  123. for (float y = yMin; y <= yMax ; y += 1.0f)
  124. {
  125. float x = pt1.x + (y - pt1.y) * slope;
  126. float scaler = (y - yMin)/lenth;
  127. Rgba color = colorLerp(color1,color2,scaler);
  128. setPixel(x,y,color);
  129. }
  130. }
  131. }
  132. void Raster::drawArrays( DRAWMODE mode,const float2* points,int count )
  133. {
  134. switch (mode)
  135. {
  136. case DM_POINTS:
  137. {
  138. for (int i = 0 ;i < count ; ++ i)
  139. {
  140. drawPoints(points[i],_color);
  141. }
  142. }
  143. break;
  144. case DM_LINES:
  145. {
  146. count = count/2 * 2;
  147. for (int i = 0 ;i < count ; i += 2)
  148. {
  149. drawLine(points[i],points[i + 1],_color,_color);
  150. }
  151. }
  152. break;
  153. case DM_LINE_LOOP:
  154. {
  155. drawLine(points[0],points[1],_color,_color);
  156. for (int i = 2 ;i < count ; ++ i)
  157. {
  158. drawLine(points[i - 1],points[i],_color,_color);
  159. }
  160. drawLine(points[0],points[count - 1],_color,_color);
  161. }
  162. break;
  163. case DM_LINE_STRIP:
  164. {
  165. drawLine(points[0],points[1],_color,_color);
  166. for (int i = 2 ;i < count ; ++ i)
  167. {
  168. drawLine(points[i - 1],points[i],_color,_color);
  169. }
  170. }
  171. break;
  172. default:
  173. break;
  174. }
  175. }
  176. void Raster::drawFilleRect( int startX,int startY,int w,int h )
  177. {
  178. int left = tmax<int>(startX,0);
  179. int top = tmax<int>(startY,0);
  180. int right = tmin<int>(startX + w,_width);
  181. int bottom = tmin<int>(startY + h,_height);
  182. for (int x = left ; x < right ; ++ x)
  183. {
  184. for (int y = top ; y < bottom ; ++ y)
  185. {
  186. setPixelEx(x,y,_color);
  187. }
  188. }
  189. }
  190. void Raster::drawRect( const int2* points,const Rgba* colors )
  191. {
  192. int left = tmax<int>(points[0].x,0);
  193. int top = tmax<int>(points[0].y,0);
  194. int right = tmin<int>(points[2].x,_width);
  195. int bottom = tmin<int>(points[2].y,_height);
  196. float w = right - left;
  197. float h = bottom - top;
  198. for (int x = left ; x < right ; ++ x)
  199. {
  200. Rgba color1 = colorLerp(colors[0],colors[1],(x - left)/w);
  201. Rgba color2 = colorLerp(colors[3],colors[2],(x - left)/w);
  202. for (int y = top ; y < bottom ; ++ y)
  203. {
  204. Rgba color = colorLerp(color1,color2,(y - top)/h);
  205. setPixelEx(x,y,color);
  206. }
  207. }
  208. }
  209. void Raster::drawImage( int startX,int startY,const Image* image )
  210. {
  211. int left = tmax<int>(startX,0);
  212. int top = tmax<int>(startY,0);
  213. int right = tmin<int>(startX + image->width(),_width);
  214. int bottom = tmin<int>(startY + image->height(),_height);
  215. for (int x = left ; x < right ; ++ x)
  216. {
  217. for (int y = top ; y < bottom ; ++ y)
  218. {
  219. Rgba color = image->pixelAt(x - left,y - top);
  220. setPixelEx(x,y,color);
  221. }
  222. }
  223. }
  224. void Raster::drawImageWidthColorKey( int startX,int startY,const Image* image,Rgba key )
  225. {
  226. int left = tmax<int>(startX,0);
  227. int top = tmax<int>(startY,0);
  228. int right = tmin<int>(startX + image->width(),_width);
  229. int bottom = tmin<int>(startY + image->height(),_height);
  230. for (int x = left ; x < right ; ++ x)
  231. {
  232. for (int y = top ; y < bottom ; ++ y)
  233. {
  234. Rgba color = image->pixelAt(x - left,y - top);
  235. if (color != key)
  236. {
  237. setPixelEx(x,y,color);
  238. }
  239. }
  240. }
  241. }
  242. void Raster::drawImageAlphaTest( int startX,int startY,const Image* image,byte alpha )
  243. {
  244. int left = tmax<int>(startX,0);
  245. int top = tmax<int>(startY,0);
  246. int right = tmin<int>(startX + image->width(),_width);
  247. int bottom = tmin<int>(startY + image->height(),_height);
  248. for (int x = left ; x < right ; ++ x)
  249. {
  250. for (int y = top ; y < bottom ; ++ y)
  251. {
  252. Rgba color = image->pixelAt(x - left,y - top);
  253. if (color._a > alpha)
  254. {
  255. setPixelEx(x,y,color);
  256. }
  257. }
  258. }
  259. }
  260. void Raster::drawImageAlphaBlend( int startX,int startY,const Image* image,float alpha )
  261. {
  262. int left = tmax<int>(startX,0);
  263. int top = tmax<int>(startY,0);
  264. int right = tmin<int>(startX + image->width(),_width);
  265. int bottom = tmin<int>(startY + image->height(),_height);
  266. for (int x = left ; x < right ; ++ x)
  267. {
  268. for (int y = top ; y < bottom ; ++ y)
  269. {
  270. Rgba srcColor = image->pixelAt(x - left,y - top);
  271. Rgba dstColor = getPixel(x,y);
  272. Rgba color = colorLerp(dstColor,srcColor,srcColor._a/255.0f * alpha);
  273. setPixelEx(x,y,color);
  274. }
  275. }
  276. }
  277. void Raster::drawImageAlpha( int startX,int startY,const Image* image,float alpha )
  278. {
  279. int left = tmax<int>(startX,0);
  280. int top = tmax<int>(startY,0);
  281. int right = tmin<int>(startX + image->width(),_width);
  282. int bottom = tmin<int>(startY + image->height(),_height);
  283. for (int x = left ; x < right ; ++ x)
  284. {
  285. for (int y = top ; y < bottom ; ++ y)
  286. {
  287. Rgba srcColor = image->pixelAt(x - left,y - top);
  288. Rgba dstColor = getPixel(x,y);
  289. Rgba color = colorLerp(dstColor,srcColor,alpha);
  290. setPixelEx(x,y,color);
  291. }
  292. }
  293. }
  294. }