blitter.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Flexlay - A Generic 2D Game Editor
  2. // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include <assert.h>
  17. #include <iostream>
  18. #include <ClanLib/Display/pixel_format.h>
  19. #include <ClanLib/Display/palette.h>
  20. #include "blitter.hpp"
  21. void
  22. blit_opaque(CL_PixelBuffer target, CL_PixelBuffer brush, int x_pos, int y_pos)
  23. {
  24. assert(target.get_format().get_type() == pixelformat_rgba);
  25. assert(target.get_format().get_depth() == 32);
  26. target.lock();
  27. brush.lock();
  28. int start_x = std::max(0, -x_pos);
  29. int start_y = std::max(0, -y_pos);
  30. int end_x = std::min(brush.get_width(), target.get_width() - x_pos);
  31. int end_y = std::min(brush.get_height(), target.get_height() - y_pos);
  32. unsigned char* target_buf = static_cast<unsigned char*>(target.get_data());
  33. unsigned char* brush_buf = static_cast<unsigned char*>(brush.get_data());
  34. int target_pitch = target.get_pitch();
  35. int brush_pitch = brush.get_pitch();
  36. if (brush.get_format().get_type() == pixelformat_rgba)
  37. {
  38. if (brush.get_format().get_depth() == 32)
  39. {
  40. for (int y = start_y; y < end_y; ++y)
  41. for (int x = start_x; x < end_x; ++x)
  42. {
  43. int target_pos = (y + y_pos) * target_pitch + 4*(x + x_pos);
  44. int brush_pos = y * brush_pitch + x*4;
  45. target_buf[target_pos + 0] = brush_buf[brush_pos + 0];
  46. target_buf[target_pos + 1] = brush_buf[brush_pos + 1];
  47. target_buf[target_pos + 2] = brush_buf[brush_pos + 2];
  48. target_buf[target_pos + 3] = brush_buf[brush_pos + 3];
  49. }
  50. }
  51. else if (brush.get_format().get_depth() == 24)
  52. {
  53. for (int y = start_y; y < end_y; ++y)
  54. for (int x = start_x; x < end_x; ++x)
  55. {
  56. int target_pos = (y + y_pos) * target_pitch + 4*(x + x_pos);
  57. int brush_pos = y * brush_pitch + 3*x;
  58. target_buf[target_pos + 0] = 255;
  59. target_buf[target_pos + 1] = brush_buf[brush_pos + 0];
  60. target_buf[target_pos + 2] = brush_buf[brush_pos + 1];
  61. target_buf[target_pos + 3] = brush_buf[brush_pos + 2];
  62. }
  63. }
  64. else
  65. {
  66. std::cout << "Unsupported bpp: " << brush.get_format().get_depth() << std::endl;
  67. }
  68. }
  69. else if (brush.get_format().get_type() == pixelformat_index)
  70. {
  71. CL_Palette palette = brush.get_palette();
  72. for (int y = start_y; y < end_y; ++y)
  73. for (int x = start_x; x < end_x; ++x)
  74. {
  75. int target_pos = (y + y_pos) * target_pitch + 4*(x + x_pos);
  76. int brush_pos = y * brush_pitch + x;
  77. target_buf[target_pos + 0] = 255;
  78. target_buf[target_pos + 1] = palette.colors[brush_buf[brush_pos]].get_blue();
  79. target_buf[target_pos + 2] = palette.colors[brush_buf[brush_pos]].get_green();
  80. target_buf[target_pos + 3] = palette.colors[brush_buf[brush_pos]].get_red();
  81. }
  82. }
  83. else
  84. {
  85. assert(!"Unknown pixelformat type");
  86. }
  87. brush.unlock();
  88. target.unlock();
  89. }
  90. void
  91. blit(CL_PixelBuffer target, CL_PixelBuffer brush, int x_pos, int y_pos)
  92. {
  93. target.lock();
  94. brush.lock();
  95. int start_x = std::max(0, -x_pos);
  96. int start_y = std::max(0, -y_pos);
  97. int end_x = std::min(brush.get_width(), target.get_width() - x_pos);
  98. int end_y = std::min(brush.get_height(), target.get_height() - y_pos);
  99. unsigned char* target_buf = static_cast<unsigned char*>(target.get_data());
  100. unsigned char* brush_buf = static_cast<unsigned char*>(brush.get_data());
  101. // FIXME: This doesn't take pitch into account
  102. int target_width = target.get_width();
  103. int brush_width = brush.get_width();
  104. if (brush.get_format().get_type() == pixelformat_rgba)
  105. {
  106. if (brush.get_format().get_depth() == 32)
  107. {
  108. for (int y = start_y; y < end_y; ++y)
  109. for (int x = start_x; x < end_x; ++x)
  110. {
  111. int target_pos = (y + y_pos) * target_width + x + x_pos;
  112. int brush_pos = y * brush_width + x;
  113. unsigned char a = brush_buf[4*brush_pos + 0];
  114. unsigned char r = brush_buf[4*brush_pos + 1];
  115. unsigned char g = brush_buf[4*brush_pos + 2];
  116. unsigned char b = brush_buf[4*brush_pos + 3];
  117. unsigned char ta = target_buf[4*target_pos + 0];
  118. unsigned char tr = target_buf[4*target_pos + 1];
  119. unsigned char tg = target_buf[4*target_pos + 2];
  120. unsigned char tb = target_buf[4*target_pos + 3];
  121. float alpha = a/255.0f;
  122. target_buf[4*target_pos + 0] = std::min(255, ta + a);
  123. target_buf[4*target_pos + 1] = std::min(255, int((1-alpha)*tr + alpha*r));
  124. target_buf[4*target_pos + 2] = std::min(255, int((1-alpha)*tg + alpha*g));
  125. target_buf[4*target_pos + 3] = std::min(255, int((1-alpha)*tb + alpha*b));
  126. }
  127. }
  128. else if (brush.get_format().get_depth() == 24)
  129. {
  130. for (int y = start_y; y < end_y; ++y)
  131. for (int x = start_x; x < end_x; ++x)
  132. {
  133. int target_pos = (y + y_pos) * target_width + x + x_pos;
  134. int brush_pos = y * brush_width + x;
  135. target_buf[4*target_pos + 0] = 255;
  136. target_buf[4*target_pos + 1] = brush_buf[3*brush_pos + 0];
  137. target_buf[4*target_pos + 2] = brush_buf[3*brush_pos + 1];
  138. target_buf[4*target_pos + 3] = brush_buf[3*brush_pos + 2];
  139. }
  140. }
  141. else
  142. {
  143. std::cout << "Unsupported bpp: " << brush.get_format().get_depth() << std::endl;
  144. }
  145. }
  146. else if (brush.get_format().get_type() == pixelformat_index)
  147. {
  148. CL_Palette palette = brush.get_palette();
  149. for (int y = start_y; y < end_y; ++y)
  150. for (int x = start_x; x < end_x; ++x)
  151. {
  152. int target_pos = (y + y_pos) * target_width + x + x_pos;
  153. int brush_pos = y * brush_width + x;
  154. target_buf[4*target_pos + 0] = 255;
  155. target_buf[4*target_pos + 1] = palette.colors[brush_buf[brush_pos]].get_blue();
  156. target_buf[4*target_pos + 2] = palette.colors[brush_buf[brush_pos]].get_green();
  157. target_buf[4*target_pos + 3] = palette.colors[brush_buf[brush_pos]].get_red();
  158. }
  159. }
  160. else
  161. {
  162. assert(!"Unknown pixelformat type");
  163. }
  164. brush.unlock();
  165. target.unlock();
  166. }
  167. void clear(CL_PixelBuffer canvas)
  168. {
  169. unsigned char* buffer;
  170. canvas.lock();
  171. buffer = static_cast<unsigned char*>(canvas.get_data());
  172. memset(buffer, 0, sizeof(unsigned char) * canvas.get_pitch() * canvas.get_height());
  173. canvas.unlock();
  174. }
  175. /* EOF */