YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
YPixel.h
浏览该文件的文档.
1 /*
2  © 2013 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_YPixel_h_
29 #define YSL_INC_Service_YPixel_h_ 1
30 
31 #include "YModules.h"
32 #include YFM_YSLib_Core_YGDIBase
33 #include <ystdex/rational.hpp>
34 
35 namespace YSLib
36 {
37 
38 namespace Drawing
39 {
40 
51 namespace Shaders
52 {
53 
61 {
63  template<typename _tOut, typename _tIn>
64  inline void
65  operator()(_tOut& dst_iter, _tIn& src_iter)
66  {
67  if(FetchAlpha(*src_iter))
68  *dst_iter = *src_iter;
69  }
71  template<typename _tOut>
72  inline void
73  operator()(_tOut& dst_iter, IteratorPair& src_iter)
74  {
75  *dst_iter = *src_iter.base().second & 0x80 ? FetchOpaque(*src_iter)
76  : FetchOpaque(PixelType());
77  }
78 };
79 
80 
91 template<size_t _vDstAlphaBits, size_t _vSrcAlphaBits>
93 {
102  template<typename _tDstAlpha, typename _tSrcAlpha>
103  static yconstfn ystdex::conditional_t<(_vDstAlphaBits < _vSrcAlphaBits),
104  _tSrcAlpha, _tDstAlpha>
105  CompositeAlphaOver(_tDstAlpha da, _tSrcAlpha sa)
106  {
108  "Non-normalizable destination alpha type found.");
110  "Non-normalizable source alpha type found.");
111 
112  return sa + da * (1 - sa);
113  }
114 
125  template<typename _tDst, typename _tSrc, typename _tSrcAlpha,
126  typename _tAlpha>
127  static yconstfn _tDst
128  CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa, _tAlpha a)
129  {
131  "Non-normalizable destination type found.");
133  "Non-normalizable source type found.");
135  "Non-normalizable source alpha type found.");
137  "Non-normalizable alpha type found.");
138 
139  return a != 0 ? (s < d ? _tDst(d - sa * (d - s) / a)
140  : _tDst(sa * (s - d) / a + d)) : _tDst(0);
141  }
142 };
143 
145 template<size_t _vDstAlphaBits>
146 struct GPixelCompositor<_vDstAlphaBits, 1>
147 {
154  template<typename _tDstAlpha, typename _tSrcAlpha>
155  static yconstfn _tDstAlpha
156  CompositeAlphaOver(_tDstAlpha da, _tSrcAlpha sa)
157  {
159  "Non-normalizable destination alpha type found.");
161  "Non-normalizable source alpha type found.");
162 
163  return sa != 0 ? ystdex::normalized_max<_tDstAlpha>::get() : da;
164  }
165 
173  template<typename _tDst, typename _tSrc, typename _tSrcAlpha,
174  typename _tAlpha>
175  static yconstfn _tDst
176  CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa, _tAlpha a)
177  {
179  "Non-normalizable destination type found.");
181  "Non-normalizable source type found.");
183  "Non-normalizable source alpha type found.");
185  "Non-normalizable alpha type found.");
186 
187  return a != 0 ? (sa != 0 ? (s < d ? _tDst(d - (d - s) / a)
188  : _tDst((s - d) / a + d)) : d) : _tDst(0);
189  }
190 };
191 
193 template<size_t _vDstAlphaBits>
194 struct GPixelCompositor<_vDstAlphaBits, 0>
195 {
204  template<typename _tDstAlpha>
205  static yconstfn _tDstAlpha
206  CompositeAlphaOver(_tDstAlpha)
207  {
209  "Non-normalizable destination alpha type found.");
210 
212  }
213  template<typename _tDstAlpha, typename _tSrcAlpha>
214  static yconstfn _tDstAlpha
215  CompositeAlphaOver(_tDstAlpha da, _tSrcAlpha)
216  {
218  "Non-normalizable source alpha type found.");
219 
220  return CompositeAlphaOver(da);
221  }
223 
234  template<typename _tSrc>
236  static yconstfn _tSrc
238  {
240  "Non-normalizable source type found.");
241 
242  return s;
243  }
244  template<typename _tDst, typename _tSrc, typename _tSrcAlpha,
245  typename _tAlpha>
246  static yconstfn _tSrc
247  CompositeComponentOver(_tDst, _tSrc s, _tSrcAlpha, _tAlpha)
248  {
250  "Non-normalizable destination type found.");
252  "Non-normalizable source alpha type found.");
254  "Non-normalizable alpha type found.");
255 
256  return CompositeComponentOver(s);
257  }
259 };
260 
262 template<size_t _vSrcAlphaBits>
263 struct GPixelCompositor<1, _vSrcAlphaBits>
264 {
271  template<typename _tDstAlpha, typename _tSrcAlpha>
272  static yconstfn _tSrcAlpha
273  CompositeAlphaOver(_tDstAlpha da, _tSrcAlpha sa)
274  {
276  "Non-normalizable destination alpha type found.");
278  "Non-normalizable source alpha type found.");
279 
280  return da != 0 ? ystdex::normalized_max<_tSrcAlpha>::get() : sa;
281  }
282 
290  template<typename _tDst, typename _tSrc, typename _tSrcAlpha,
291  typename _tAlpha>
292  static yconstfn _tDst
293  CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa, _tAlpha a)
294  {
296  "Non-normalizable destination type found.");
298  "Non-normalizable source type found.");
300  "Non-normalizable source alpha type found.");
302  "Non-normalizable alpha type found.");
303 
305  ::CompositeComponentOver(d, s, sa) : _tDst(0);
306  }
307 };
308 
310 template<size_t _vSrcAlphaBits>
311 struct GPixelCompositor<0, _vSrcAlphaBits>
312  : private GPixelCompositor<2, 0>
313 {
323 
333  template<typename _tDst, typename _tSrc, typename _tSrcAlpha>
334  static yconstfn _tDst
335  CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa)
336  {
338  "Non-normalizable destination type found.");
340  "Non-normalizable source type found.");
342  "Non-normalizable source alpha type found.");
343 
344  return s < d ? _tDst(d - sa * (d - s)) : _tDst(sa * (s - d) + d);
345  }
346  template<typename _tDst, typename _tSrc, typename _tSrcAlpha,
347  typename _tAlpha>
348  static yconstfn _tDst
349  CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa, _tAlpha)
350  {
352  "Non-normalizable alpha type found.");
353 
354  return CompositeComponentOver(d, s, sa);
355  }
357 };
358 
360 template<>
361 struct GPixelCompositor<0, 1> : private GPixelCompositor<2, 0>
362 {
364 
375  template<typename _tDst, typename _tSrc, typename _tSrcAlpha>
376  static yconstfn _tDst
377  CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa)
378  {
380  "Non-normalizable destination type found.");
382  "Non-normalizable source type found.");
384  "Non-normalizable source alpha type found.");
385 
386  return sa != 0 ? _tDst(s) : _tDst(d);
387  }
388  template<typename _tDst, typename _tSrc, typename _tSrcAlpha,
389  typename _tAlpha>
390  static yconstfn _tDst
391  CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa, _tAlpha)
392  {
394  "Non-normalizable alpha type found.");
395 
396  return CompositeComponentOver(d, s, sa);
397  }
399 };
400 
405 template<>
406 struct GPixelCompositor<1, 0> : public GPixelCompositor<2, 0>
407 {};
408 
410 template<>
411 struct GPixelCompositor<1, 1>
412 {
419  template<typename _tDstAlpha, typename _tSrcAlpha>
420  static yconstfn _tDstAlpha
421  CompositeAlphaOver(_tDstAlpha da, _tSrcAlpha sa)
422  {
424  "Non-normalizable destination alpha type found.");
426  "Non-normalizable source alpha type found.");
427 
428  return sa != 0 || da != 0 ? ystdex::normalized_max<_tDstAlpha>::get()
429  : _tDstAlpha(0);
430  }
431 
440  template<typename _tDst, typename _tSrc, typename _tSrcAlpha,
441  typename _tAlpha>
442  static yconstfn _tDst
443  CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa, _tAlpha a)
444  {
446  "Non-normalizable destination type found.");
448  "Non-normalizable source type found.");
450  "Non-normalizable source alpha type found.");
452  "Non-normalizable alpha type found.");
453 
454  return a != 0 ? GPixelCompositor<0, 1>::CompositeComponentOver(d, s, sa)
455  : _tDst(0);
456  }
457 };
458 
460 template<>
461 struct GPixelCompositor<0, 0> : private GPixelCompositor<2, 0>
462 {
464 
466 };
468 
469 
476 template<size_t _vSrcAlphaBits, typename _tDstInt, typename _tSrcInt,
477  typename _tSrcAlphaInt>
478 yconstfn _tDstInt
479 BlendComponent(_tDstInt d, _tSrcInt s, _tSrcAlphaInt sa)
480 {
481  using namespace ystdex;
482  static_assert(integer_width<_tDstInt>::value == _vSrcAlphaBits,
483  "Invalid integer destination type found.");
484  static_assert(integer_width<_tSrcInt>::value == _vSrcAlphaBits,
485  "Invalid integer source type found.");
486  static_assert(integer_width<_tSrcAlphaInt>::value == _vSrcAlphaBits,
487  "Invalid integer source alpha type found.");
489  ::unsigned_least_type, _vSrcAlphaBits>;
490 
492  pix(d, raw_tag()), pix(s, raw_tag()), pix(sa, raw_tag()),
493  normalized_max<pix>::get()).get();
494 }
495 
502 template<size_t _vDstAlphaBits, size_t _vSrcAlphaBits, typename _tDstInt,
503  typename _tSrcInt, typename _tSrcAlphaInt, typename _tAlphaInt>
504 yconstfn _tDstInt
505 CompositeComponent(_tDstInt d, _tSrcInt s, _tSrcAlphaInt sa, _tAlphaInt a)
506 {
507  using namespace ystdex;
508  static_assert(integer_width<_tDstInt>::value == _vSrcAlphaBits,
509  "Invalid integer destination type found.");
510  static_assert(integer_width<_tSrcInt>::value == _vSrcAlphaBits,
511  "Invalid integer source type found.");
512  static_assert(integer_width<_tSrcAlphaInt>::value == _vSrcAlphaBits,
513  "Invalid integer source alpha type found.");
514  static_assert(integer_width<_tAlphaInt>::value > _vDstAlphaBits,
515  "Invalid integer result alpha type found.");
517  ::unsigned_least_type, _vDstAlphaBits>;
519  ::unsigned_least_type, _vSrcAlphaBits>;
520 
522  ::CompositeComponentOver(pix(d, raw_tag()), pix(s, raw_tag()),
523  pix(sa, raw_tag()), pixd(a, raw_tag())).get();
524 }
525 
526 
547 template<size_t _vAlphaBits>
548 u16
550 {
551  static_assert(_vAlphaBits > 0 && _vAlphaBits < 16 - 5,
552  "Wrong number of alpha bits found.");
553 
554  u32 dbr((d & 0x1F) | (d << 6 & 0x1F0000)), dg(d & 0x3E0);
555 
556  yunseq(dbr += ((((s & 0x1F) | (s << 6 & 0x1F0000)) - dbr) * a)
557  >> _vAlphaBits, dg += (((s & 0x3E0) - dg) * a) >> _vAlphaBits);
558  return (dbr & 0x1F) | (dg & 0x3E0) | (dbr >> 6 & 0x7C00) | 1 << 15;
559 }
560 
561 
571 template<size_t _vSrcAlphaBits, typename _tPixel, typename _tSrcAlphaInt>
572 yconstfn _tPixel
573 Blend(const _tPixel& d, const _tPixel& s, _tSrcAlphaInt sa)
574 {
575  static_assert(std::is_integral<_tSrcAlphaInt>::value,
576  "Invalid integer source alpha type found.");
577 
578  return Color(BlendComponent<_vSrcAlphaBits>(d.GetR(), s.GetR(), sa),
579  BlendComponent<_vSrcAlphaBits>(d.GetG(), s.GetG(), sa), BlendComponent<
580  _vSrcAlphaBits>(d.GetB(), s.GetB(), sa), (1 << _vSrcAlphaBits) - 1);
581 }
582 template<size_t _vSrcAlphaBits, typename _tSrcAlphaInt>
583 RGBA<5, 5, 5, 1>
584 Blend(const RGBA<5, 5, 5, 1>& d, const RGBA<5, 5, 5, 1>& s, _tSrcAlphaInt sa)
585 {
586  static_assert(std::is_integral<_tSrcAlphaInt>::value,
587  "Invalid integer source alpha type found.");
588 
589  return BlendCore<_vSrcAlphaBits>(d, s, sa);
590 }
592 
600 template<size_t _vDstAlphaBits, size_t _vSrcAlphaBits, typename _tPixel,
602  typename _tSrcAlphaInt, typename _tAlphaInt>
603 yconstfn _tPixel
604 Composite(const _tPixel& d, const _tPixel& s, _tSrcAlphaInt sa, _tAlphaInt a)
605 {
606  static_assert(std::is_integral<_tSrcAlphaInt>::value,
607  "Invalid integer source alpha type found.");
608  static_assert(std::is_integral<_tAlphaInt>::value,
609  "Invalid integer result alpha type found.");
610 
611  return Color(CompositeComponent<_vDstAlphaBits, _vSrcAlphaBits>(d.GetR(),
612  s.GetR(), sa, a), CompositeComponent<_vDstAlphaBits, _vSrcAlphaBits>(
613  d.GetG(), s.GetG(), sa, a), CompositeComponent<_vDstAlphaBits,
614  _vSrcAlphaBits>(d.GetB(), s.GetB(), sa, a), a);
615 }
617 template<size_t _vDstAlphaBits, size_t _vSrcAlphaBits, typename _tPixel,
618  typename _tAlphaInt>
619 yconstfn _tPixel
620 Composite(const _tPixel& d, const _tPixel& s, _tAlphaInt a)
621 {
622  return Composite<_vDstAlphaBits, _vSrcAlphaBits>(d, s, s.GetA(), a);
623 }
625 template<size_t _vDstAlphaBits, size_t _vSrcAlphaBits, typename _tPixel>
626 yconstfn _tPixel
627 Composite(const _tPixel& d, const _tPixel& s)
628 {
629  using namespace ystdex;
631  ::unsigned_least_type, _vDstAlphaBits>;
633  ::unsigned_least_type, _vSrcAlphaBits>;
634 
635  return Composite<_vDstAlphaBits, _vSrcAlphaBits>(d, s, GPixelCompositor<
636  _vDstAlphaBits, _vSrcAlphaBits>::CompositeAlphaOver(pixd(d.GetA(),
637  raw_tag()), pix(s.GetA(), raw_tag())).get());
638 }
640 
641 
648 {
649  template<typename _tOut>
650  inline void
651  operator()(_tOut dst_iter, IteratorPair src_iter)
652  {
653  static_assert(std::is_convertible<ystdex::remove_reference_t<
654  decltype(*dst_iter)>, PixelType>::value, "Wrong type found.");
655 
656  const AlphaType a(*src_iter.base().second);
657 
658  *dst_iter = Blend<8>(*dst_iter, *src_iter, a);
659  }
660  template<typename _tOut, typename _tIn>
661  inline void
664  {
665  static_assert(std::is_convertible<ystdex::remove_reference_t<
666  decltype(*dst_iter)>, PixelType>::value, "Wrong type found.");
667 
668  const AlphaType a(*src_iter.base().second);
669 
670  *dst_iter = Blend<8>(*dst_iter, *src_iter, a);
671  }
673  template<typename _tOut, typename _tIn>
674  inline void
675  operator()(_tOut dst_iter, _tIn src_iter)
676  {
677  *dst_iter = Shaders::Composite<ystdex::remove_reference_t<decltype(
678  *dst_iter)>::Trait::ABitsN, 8>(*dst_iter, *src_iter);
679  }
680 };
681 
682 } // namespace Shaders;
683 
684 } // namespace Drawing;
685 
686 } // namespace YSLib;
687 
688 #endif
689 
static yconstfn _tDst CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa, _tAlpha a)
Alpha 组合非 Alpha 分量。
Definition: YPixel.h:176
void operator()(_tOut dst_iter, ystdex::pair_iterator< ystdex::pseudo_iterator< const PixelType >, _tIn > src_iter)
Definition: YPixel.h:662
yconstfn _tDstInt BlendComponent(_tDstInt d, _tSrcInt s, _tSrcAlphaInt sa)
像素分量混合。
Definition: YPixel.h:479
取指定整数类型的位宽度。
Definition: cstdint.hpp:43
static yconstfn _tDstAlpha CompositeAlphaOver(_tDstAlpha)
Alpha 组合 Alpha 分量。
Definition: YPixel.h:206
通用定点数。
Definition: rational.hpp:139
std::uint32_t u32
Definition: yadaptor.h:69
typename remove_reference< _type >::type remove_reference_t
Definition: type_op.hpp:234
static yconstfn _tSrc CompositeComponentOver(_tDst, _tSrc s, _tSrcAlpha, _tAlpha)
Definition: YPixel.h:247
typename conditional< _bCond, _type, _type2 >::type conditional_t
Definition: type_op.hpp:277
static yconstfn _tDst CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa, _tAlpha a)
Alpha 组合非 Alpha 分量。
Definition: YPixel.h:443
static yconstfn _tDst CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa, _tAlpha a)
Alpha 组合非 Alpha 分量。
Definition: YPixel.h:293
yconstfn _tPixel Blend(const _tPixel &d, const _tPixel &s, _tSrcAlphaInt sa)
像素混合:使用指定的源 Alpha 。
Definition: YPixel.h:573
static yconstfn _tDstAlpha CompositeAlphaOver(_tDstAlpha da, _tSrcAlpha sa)
Alpha 组合 Alpha 分量。
Definition: YPixel.h:156
void operator()(_tOut dst_iter, IteratorPair src_iter)
Definition: YPixel.h:651
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:748
static yconstfn _tDst CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa, _tAlpha)
Definition: YPixel.h:391
static yconstfn _tDst CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa)
Alpha 组合非 Alpha 分量。
Definition: YPixel.h:335
static yconstfn _tSrcAlpha CompositeAlphaOver(_tDstAlpha da, _tSrcAlpha sa)
Alpha 组合 Alpha 分量。
Definition: YPixel.h:273
判断类型是否可满足归一化要求。
Definition: rational.hpp:83
static yconstfn _tDst CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa, _tAlpha)
Definition: YPixel.h:349
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
像素计算:Alpha 混合。
Definition: YPixel.h:647
static yconstfn _tDst CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa)
Alpha 组合非 Alpha 分量。
Definition: YPixel.h:377
取算术类型的正规化后的最大值。
Definition: rational.hpp:46
Color
控制台颜色枚举。
Definition: Video.h:458
像素迭代器透明操作。
Definition: YPixel.h:60
std::uint8_t u8
通用数据类型。
Definition: yadaptor.h:67
static yconstfn _tDstAlpha CompositeAlphaOver(_tDstAlpha da, _tSrcAlpha sa)
Alpha 组合 Alpha 分量。
Definition: YPixel.h:421
const pair_type & base() const
Definition: iterator.hpp:986
u16 BlendCore(u32 d, u32 s, u8 a)
AXYZ1555 格式 PixelType 的 Alpha 混合。
Definition: YPixel.h:549
yconstfn _tDstInt CompositeComponent(_tDstInt d, _tSrcInt s, _tSrcAlphaInt sa, _tAlphaInt a)
像素分量组合。
Definition: YPixel.h:505
伪迭代器。
Definition: iterator.hpp:351
void operator()(_tOut &dst_iter, _tIn &src_iter)
Definition: YPixel.h:65
static yconstfn _tDst CompositeComponentOver(_tDst d, _tSrc s, _tSrcAlpha sa, _tAlpha a)
Alpha 组合非 Alpha 分量。
Definition: YPixel.h:128
empty_base<> raw_tag
直接构造类型(直接构造重载用)。
Definition: ydef.h:665
void operator()(_tOut &dst_iter, IteratorPair &src_iter)
Definition: YPixel.h:73
static yconstfn ystdex::conditional_t<(_vDstAlphaBits< _vSrcAlphaBits), _tSrcAlpha, _tDstAlpha > CompositeAlphaOver(_tDstAlpha da, _tSrcAlpha sa)
Alpha 组合 Alpha 分量。
Definition: YPixel.h:105
std::uint16_t u16
Definition: yadaptor.h:68
static yconstfn _tSrc CompositeComponentOver(_tSrc s)
Alpha 组合非 Alpha 分量。
Definition: YPixel.h:237
有理数运算。
void operator()(_tOut dst_iter, _tIn src_iter)
Definition: YPixel.h:675
static yconstfn _tDstAlpha CompositeAlphaOver(_tDstAlpha da, _tSrcAlpha)
Definition: YPixel.h:215
ystdex::octet AlphaType
Definition: Video.h:186
成对迭代器。
Definition: iterator.hpp:835
yconstfn _tPixel Composite(const _tPixel &d, const _tPixel &s, _tSrcAlphaInt sa, _tAlphaInt a)
像素组合:使用指定的源 Alpha 和结果 Alpha 。
Definition: YPixel.h:604