YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
yblit.h
浏览该文件的文档.
1 /*
2  © 2011-2014 FrankHB.
3 
4  This file is part of the YSLib project, and may only be used,
5  modified, and distributed under the terms of the YSLib project
6  license, LICENSE.TXT. By continuing to use, modify, or distribute
7  this file you indicate that you have read the license and
8  understand and accept it fully.
9 */
10 
28 #ifndef YSL_INC_Service_yblit_h_
29 #define YSL_INC_Service_yblit_h_ 1
30 
31 #include "YModules.h"
32 #include YFM_YSLib_Core_YGDIBase
33 #include YFM_YSLib_Core_YCoreUtilities
34 #include <ystdex/algorithm.hpp>
35 #include <ystdex/iterator.hpp>
36 #include <ystdex/rational.hpp>
37 
38 namespace YSLib
39 {
40 
41 namespace Drawing
42 {
43 
44 //基本仿函数。
45 
50 template<typename _tPixel>
52 {
53  _tPixel Color;
54 
58  explicit inline
59  PixelFiller(_tPixel c)
60  : Color(c)
61  {}
62 
68  template<typename _tOut>
69  inline void
70  operator()(_tOut dst)
71  {
72  *dst = Color;
73  }
74 };
75 
81 {
88  template<typename _tOut, class _fTransformPixel>
89  void
90  operator()(_tOut dst, size_t n, _fTransformPixel tp) const
91  {
93 
94  YAssert(n == 0 || !is_undereferenceable(dst),
95  "Invalid iterator found.");
96  for(const auto e(dst + n); dst != e; ++dst)
97  tp(dst);
98  }
99 };
100 
106 {
113  template<typename _tOut, class _fTransformPixel>
114  void
115  operator()(_tOut dst, size_t n, SDst dw, _fTransformPixel tp) const
116  {
118 
119  YAssert(n == 0 || !is_undereferenceable(dst),
120  "Invalid iterator found.");
121 
122  while(n-- != 0)
123  {
124  tp(dst);
125  dst += dw;
126  }
127  }
128 };
129 
130 
140 YF_API bool
141 BlitBounds(const Point&, const Point&, const Size&, const Size&, const Size&,
142  SDst&, SDst&, SDst&, SDst&);
143 
149 template<bool>
150 yconstfn size_t
152 {
153  return d < 0 ? 0 : d;
154 }
155 template<>
156 yconstfn size_t
158 {
159  return d < 0 ? 0 : d + delta - 1;
160 }
162 
163 
184 template<bool _bDec, typename _tScalar, typename _tDiff, typename _tOut,
185  typename _tIn, typename _fBlitLoop>
186 void
187 BlitScan(_fBlitLoop loop, _tOut dst, _tIn src, _tScalar d_width,
188  _tScalar s_width, _tDiff delta_x, _tDiff delta_y)
189 {
190  loop(dst, src, delta_x, delta_y, (_bDec ? -1 : 1) * d_width - delta_x,
191  s_width - delta_x);
192 }
193 
194 
228 template<bool _bSwapLR, bool _bSwapUD, typename _tOut, typename _tIn,
229  typename _fBlitLoop>
230 void
231 Blit(_fBlitLoop loop, _tOut dst, _tIn src, const Size& ds, const Size& ss,
232  const Point& dp, const Point& sp, const Size& sc)
233 {
234  SDst min_x, min_y, delta_x, delta_y;
235 
236  if(BlitBounds(dp, sp, ds, ss, sc, min_x, min_y, delta_x, delta_y))
237  BlitScan<_bSwapLR != _bSwapUD>(loop, dst + BlitScaleComponent<_bSwapUD>(
238  dp.Y - (sp.Y < 0 ? sp.Y : 0), delta_y) * ds.Width
239  + BlitScaleComponent<_bSwapLR>(dp.X - (sp.X < 0 ? sp.X : 0),
240  delta_x), src + min_y * ss.Width + min_x, ds.Width, ss.Width,
241  delta_x, delta_y);
242 }
243 
244 
251 template<bool _bPositiveScan>
253 {
255  template<typename _tOut, typename _tIn, typename _fBlitScanner>
256  void
257  operator()(_fBlitScanner scanner, _tOut dst_iter, _tIn src_iter,
258  SDst delta_x, SDst delta_y, SPos dst_inc, SPos src_inc) const
259  {
260  while(delta_y-- > 0)
261  {
262  scanner(dst_iter, src_iter, delta_x);
263  src_iter += src_inc;
264  ystdex::delta_assign<_bPositiveScan>(dst_iter, dst_inc);
265  }
266  }
267 };
268 
269 
291 template<bool _bSwapLR, bool _bSwapUD, typename _tOut, typename _tIn,
292  typename _fBlitScanner>
293 void
294 BlitLines(_fBlitScanner scanner, _tOut dst, _tIn src, const Size& ds,
295  const Size& ss, const Point& dp, const Point& sp, const Size& sc)
296 {
297  using namespace std::placeholders;
298 
299  Blit<_bSwapLR, _bSwapUD, _tOut, _tIn>(std::bind(BlitScannerLoop<!_bSwapLR>(
300  ), scanner, _1, _2, _3, _4, _5, _6), dst, src, ds, ss, dp, sp, sc);
301 }
302 
303 
311 template<bool _bPositiveScan>
313 {
314  template<typename _tOut, typename _tIn, typename _fPixelShader>
315  void
316  operator()(_fPixelShader shader, _tOut& dst_iter, _tIn& src_iter,
317  SDst delta_x)
318  {
319  for(SDst x(0); x < delta_x; ++x)
320  {
321  shader(dst_iter, src_iter);
322  ++src_iter;
323  ystdex::xcrease<_bPositiveScan>(dst_iter);
324  }
325  }
326 };
327 
328 
351 template<bool _bSwapLR, bool _bSwapUD, typename _tOut, typename _tIn,
352  typename _fPixelShader>
353 void
354 BlitPixels(_fPixelShader shader, _tOut dst, _tIn src, const Size& ds,
355  const Size& ss, const Point& dp, const Point& sp, const Size& sc)
356 {
357  BlitLines<_bSwapLR, _bSwapUD, _tOut, _tIn>(
358  [shader](_tOut& dst_iter, _tIn& src_iter, SDst delta_x){
359  BlitLineLoop<!_bSwapLR>()(shader, dst_iter, src_iter, delta_x);
360  }, dst, src, ds, ss, dp, sp, sc);
361 }
362 
363 
369 {
377  template<typename _tOut, class _fTransformPixel, class _fTransformLine>
378  void
379  operator()(_tOut dst, const Size& ds, const Point& dp, const Size& sc,
380  _fTransformPixel tp, _fTransformLine tl)
381  {
382  Blit<false, false>([&](_tOut dst_iter, _tOut, SDst delta_x,
383  SDst delta_y, SPos dst_inc, SPos){
384  while(delta_y-- > 0)
385  {
386  tl(dst_iter, delta_x, tp);
387  dst_iter += dst_inc + delta_x;
388  }
389  }, dst, dst, ds, ds, dp, dp, sc);
390  }
391  template<typename _tOut, class _fTransformPixel, class _fTransformLine>
392  inline void
393  operator()(_tOut dst, const Size& ds, const Rect& r, _fTransformPixel tp,
394  _fTransformLine tl)
395  {
396  operator()<_tOut, _fTransformPixel, _fTransformLine>(dst, ds,
397  r.GetPoint(), r.GetSize(), tp, tl);
398  }
399  template<typename _tOut, class _fTransformPixel, class _fTransformLine>
400  inline void
401  operator()(_tOut dst, SDst dw, SDst dh, SPos x, SPos y,
402  SDst w, SDst h, _fTransformPixel tp, _fTransformLine tl)
403  {
404  operator()<_tOut, _fTransformPixel, _fTransformLine>(dst, {dw, dh},
405  {x, y}, {w, h}, tp, tl);
406  }
408 };
409 
410 
411 /*
412 \brief 显示缓存操作:清除/以纯色像素填充。
413 \tparam _tOut 输出迭代器类型(需要支持 += 操作,一般应是随机迭代器)。
414 \since build 438
415 */
417 
422 template<typename _tOut>
423 inline _tOut
424 ClearPixel(_tOut dst, size_t n) ynothrow
425 {
426  ClearSequence(dst, n);
427  return dst;
428 }
429 
433 template<typename _tPixel, typename _tOut>
434 inline void
435 FillPixel(_tOut dst, size_t n, _tPixel c)
436 {
438 }
439 
443 template<typename _tPixel, typename _tOut>
444 inline void
445 FillVerticalLine(_tOut dst, size_t n, SDst dw, _tPixel c)
446 {
448 }
450 
456 template<typename _tPixel, typename _tOut>
457 inline void
458 FillRectRaw(_tOut dst, const Size& ds, const Point& sp, const Size& sc,
459  _tPixel c)
460 {
461  RectTransformer()(dst, ds, sp, sc, PixelFiller<_tPixel>(c),
463 }
464 template<typename _tPixel, typename _tOut>
465 inline void
466 FillRectRaw(_tOut dst, const Size& ds, const Rect& r, _tPixel c)
467 {
470 }
471 template<typename _tPixel, typename _tOut>
472 inline void
473 FillRectRaw(_tOut dst, SDst dw, SDst dh, SPos x, SPos y, SDst w, SDst h,
474  _tPixel c)
475 {
476  RectTransformer()(dst, dw, dh, x, y, w, h, PixelFiller<_tPixel>(c),
478 }
480 
481 
489 template<bool _bPositiveScan>
490 struct CopyLine
491 {
492  template<typename _tOut, typename _tIn>
493  void
494  operator()(_tOut& dst_iter, _tIn& src_iter, SDst delta_x) const
495  {
496  std::copy_n(src_iter, delta_x, dst_iter);
497  yunseq(src_iter += delta_x, dst_iter += delta_x);
498  }
499 };
500 
502 template<>
503 struct CopyLine<false>
504 {
505  template<typename _tOut, typename _tIn>
506  void
507  operator()(_tOut& dst_iter, _tIn& src_iter, SDst delta_x) const
508  {
509  while(delta_x-- > 0)
510  *dst_iter-- = *src_iter++;
511  }
512 };
514 
515 
521 
522 
527 template<class _fTransformPixel>
528 bool
529 TransformRect(const Graphics& g, const Point& pt, const Size& s,
530  _fTransformPixel tp)
531 {
532  if(YB_LIKELY(g))
533  {
534  RectTransformer()(g.GetBufferPtr(), g.GetSize(), pt, s, tp,
536  return true;
537  }
538  return false;
539 }
544 template<class _fTransformPixel>
545 inline bool
546 TransformRect(const Graphics& g, const Rect& r, _fTransformPixel tp)
547 {
548  return TransformRect<_fTransformPixel>(g, r.GetPoint(), r.GetSize(), tp);
549 }
550 
551 
558 YF_API void
559 CopyBuffer(const Graphics&, const Graphics&);
560 
565 YF_API void
566 ClearImage(const Graphics&);
567 
572 YF_API void
573 Fill(const Graphics&, Color);
574 
575 } // namespace Drawing;
576 
577 } // namespace YSLib;
578 
579 #endif
580 
竖直线转换器。
Definition: yblit.h:105
贴图扫描线循环操作。
Definition: yblit.h:252
PixelFiller(_tPixel c)
构造:使用指定颜色。
Definition: yblit.h:59
void Blit(_fBlitLoop loop, _tOut dst, _tIn src, const Size &ds, const Size &ss, const Point &dp, const Point &sp, const Size &sc)
贴图函数模板。
Definition: yblit.h:231
#define YF_API
Definition: Platform.h:64
void operator()(_tOut dst, SDst dw, SDst dh, SPos x, SPos y, SDst w, SDst h, _fTransformPixel tp, _fTransformLine tl)
Definition: yblit.h:401
序列转换器。
Definition: yblit.h:80
void FillVerticalLine(_tOut dst, size_t n, SDst dw, _tPixel c)
使用 n 个指定像素竖直填充指定位置。
Definition: yblit.h:445
_tOut ClearPixel(_tOut dst, size_t n) ynothrow
清除指定位置的 n 个连续像素。
Definition: yblit.h:424
YF_API void ClearImage(const Graphics &)
清除图形接口上下文缓冲区。
Definition: yblit.cpp:89
void BlitLines(_fBlitScanner scanner, _tOut dst, _tIn src, const Size &ds, const Size &ss, const Point &dp, const Point &sp, const Size &sc)
扫描线贴图函数模板。
Definition: yblit.h:294
void BlitScan(_fBlitLoop loop, _tOut dst, _tIn src, _tScalar d_width, _tScalar s_width, _tDiff delta_x, _tDiff delta_y)
贴图扫描函数模板。
Definition: yblit.h:187
void ClearSequence(_tOut dst, size_t n) ynothrow
清除指定的连续对象。
Definition: ycutil.h:344
void operator()(_tOut &dst_iter, _tIn &src_iter, SDst delta_x) const
Definition: yblit.h:507
void operator()(_fPixelShader shader, _tOut &dst_iter, _tIn &src_iter, SDst delta_x)
Definition: yblit.h:316
贴图扫描点循环操作。
Definition: yblit.h:312
std::uint16_t SDst
屏幕坐标距离。
Definition: Video.h:39
std::int16_t SPos
屏幕坐标度量。
Definition: Video.h:38
标准矩形转换器。
Definition: yblit.h:368
void operator()(_tOut &dst_iter, _tIn &src_iter, SDst delta_x) const
Definition: yblit.h:494
YF_API bool BlitBounds(const Point &, const Point &, const Size &, const Size &, const Size &, SDst &, SDst &, SDst &, SDst &)
贴图边界计算器。
Definition: yblit.cpp:58
void operator()(_tOut dst, const Size &ds, const Rect &r, _fTransformPixel tp, _fTransformLine tl)
Definition: yblit.h:393
void operator()(_fBlitScanner scanner, _tOut dst_iter, _tIn src_iter, SDst delta_x, SDst delta_y, SPos dst_inc, SPos src_inc) const
Definition: yblit.h:257
像素填充器。
Definition: yblit.h:51
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:748
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
通用迭代器。
屏幕标准矩形:表示屏幕矩形区域。
Definition: ygdibase.h:416
void FillRectRaw(_tOut dst, const Size &ds, const Point &sp, const Size &sc, _tPixel c)
使用指定像素填充指定的标准矩形区域。
Definition: yblit.h:458
void operator()(_tOut dst, size_t n, SDst dw, _fTransformPixel tp) const
渲染竖直线上的像素。
Definition: yblit.h:115
二维图形接口上下文。
Definition: ygdibase.h:721
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
YF_API void Fill(const Graphics &, Color)
使用指定颜色填充图形接口上下文缓冲区。
Definition: yblit.cpp:95
yconstfn size_t BlitScaleComponent(SPos d, SDst)
贴图偏移分量计算器。
Definition: yblit.h:151
泛型算法。
bool is_undereferenceable(const any_input_iterator< _type, _tDifference, _tPointer, _tReference > &i)
void operator()(_tOut dst)
像素填充函数。
Definition: yblit.h:70
void operator()(_tOut dst, size_t n, _fTransformPixel tp) const
渲染连续像素。
Definition: yblit.h:90
void FillPixel(_tOut dst, size_t n, _tPixel c)
使用 n 个指定像素连续填充指定位置。
Definition: yblit.h:435
void BlitPixels(_fPixelShader shader, _tOut dst, _tIn src, const Size &ds, const Size &ss, const Point &dp, const Point &sp, const Size &sc)
像素贴图函数模板。
Definition: yblit.h:354
bounds & r
Definition: ydraw.h:220
bool TransformRect(const Graphics &g, const Point &pt, const Size &s, _fTransformPixel tp)
以像素为单位变换标准矩形。
Definition: yblit.h:529
c yconstfn g
Definition: ystyle.h:104
#define YB_LIKELY(expr)
Definition: ydef.h:297
YF_API void CopyBuffer(const Graphics &, const Graphics &)
以第一个参数作为目标,复制第二个参数的缓冲区内容。
Definition: yblit.cpp:77
void operator()(_tOut dst, const Size &ds, const Point &dp, const Size &sc, _fTransformPixel tp, _fTransformLine tl)
渲染标准矩形内的像素。
Definition: yblit.h:379
颜色。
Definition: Video.h:339
有理数运算。
屏幕区域大小。
Definition: ygdibase.h:249
扫描线:按指定扫描顺序复制一行像素。
Definition: yblit.h:490
#define YAssert(_expr, _msg)
Definition: cassert.h:73
yconstfn size_t BlitScaleComponent< true >(SPos d, SDst delta)
Definition: yblit.h:157
成对迭代器。
Definition: iterator.hpp:835