agg_pixfmt_gray.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. //----------------------------------------------------------------------------
  2. // Anti-Grain Geometry - Version 2.4
  3. // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
  4. //
  5. // Permission to copy, use, modify, sell and distribute this software
  6. // is granted provided this copyright notice appears in all copies.
  7. // This software is provided "as is" without express or implied
  8. // warranty, and with no claim as to its suitability for any purpose.
  9. //
  10. //----------------------------------------------------------------------------
  11. // Contact: mcseem@antigrain.com
  12. // mcseemagg@yahoo.com
  13. // http://www.antigrain.com
  14. //----------------------------------------------------------------------------
  15. //
  16. // Adaptation for high precision colors has been sponsored by
  17. // Liberty Technology Systems, Inc., visit http://lib-sys.com
  18. //
  19. // Liberty Technology Systems, Inc. is the provider of
  20. // PostScript and PDF technology for software developers.
  21. //
  22. //----------------------------------------------------------------------------
  23. #ifndef AGG_PIXFMT_GRAY_INCLUDED
  24. #define AGG_PIXFMT_GRAY_INCLUDED
  25. #include <string.h>
  26. #include "agg_pixfmt_base.h"
  27. #include "agg_rendering_buffer.h"
  28. namespace agg
  29. {
  30. //============================================================blender_gray
  31. template<class ColorT> struct blender_gray
  32. {
  33. typedef ColorT color_type;
  34. typedef typename color_type::value_type value_type;
  35. typedef typename color_type::calc_type calc_type;
  36. typedef typename color_type::long_type long_type;
  37. // Blend pixels using the non-premultiplied form of Alvy-Ray Smith's
  38. // compositing function. Since the render buffer is opaque we skip the
  39. // initial premultiply and final demultiply.
  40. static AGG_INLINE void blend_pix(value_type* p,
  41. value_type cv, value_type alpha, cover_type cover)
  42. {
  43. blend_pix(p, cv, color_type::mult_cover(alpha, cover));
  44. }
  45. static AGG_INLINE void blend_pix(value_type* p,
  46. value_type cv, value_type alpha)
  47. {
  48. *p = color_type::lerp(*p, cv, alpha);
  49. }
  50. };
  51. //======================================================blender_gray_pre
  52. template<class ColorT> struct blender_gray_pre
  53. {
  54. typedef ColorT color_type;
  55. typedef typename color_type::value_type value_type;
  56. typedef typename color_type::calc_type calc_type;
  57. typedef typename color_type::long_type long_type;
  58. // Blend pixels using the premultiplied form of Alvy-Ray Smith's
  59. // compositing function.
  60. static AGG_INLINE void blend_pix(value_type* p,
  61. value_type cv, value_type alpha, cover_type cover)
  62. {
  63. blend_pix(p, color_type::mult_cover(cv, cover), color_type::mult_cover(alpha, cover));
  64. }
  65. static AGG_INLINE void blend_pix(value_type* p,
  66. value_type cv, value_type alpha)
  67. {
  68. *p = color_type::prelerp(*p, cv, alpha);
  69. }
  70. };
  71. //=====================================================apply_gamma_dir_gray
  72. template<class ColorT, class GammaLut> class apply_gamma_dir_gray
  73. {
  74. public:
  75. typedef typename ColorT::value_type value_type;
  76. apply_gamma_dir_gray(const GammaLut& gamma) : m_gamma(gamma) {}
  77. AGG_INLINE void operator () (value_type* p)
  78. {
  79. *p = m_gamma.dir(*p);
  80. }
  81. private:
  82. const GammaLut& m_gamma;
  83. };
  84. //=====================================================apply_gamma_inv_gray
  85. template<class ColorT, class GammaLut> class apply_gamma_inv_gray
  86. {
  87. public:
  88. typedef typename ColorT::value_type value_type;
  89. apply_gamma_inv_gray(const GammaLut& gamma) : m_gamma(gamma) {}
  90. AGG_INLINE void operator () (value_type* p)
  91. {
  92. *p = m_gamma.inv(*p);
  93. }
  94. private:
  95. const GammaLut& m_gamma;
  96. };
  97. //=================================================pixfmt_alpha_blend_gray
  98. template<class Blender, class RenBuf, unsigned Step = 1, unsigned Offset = 0>
  99. class pixfmt_alpha_blend_gray
  100. {
  101. public:
  102. typedef pixfmt_gray_tag pixfmt_category;
  103. typedef RenBuf rbuf_type;
  104. typedef typename rbuf_type::row_data row_data;
  105. typedef Blender blender_type;
  106. typedef typename blender_type::color_type color_type;
  107. typedef int order_type; // A fake one
  108. typedef typename color_type::value_type value_type;
  109. typedef typename color_type::calc_type calc_type;
  110. enum
  111. {
  112. pix_width = sizeof(value_type) * Step,
  113. pix_step = Step,
  114. pix_offset = Offset,
  115. };
  116. struct pixel_type
  117. {
  118. value_type c[pix_step];
  119. void set(value_type v)
  120. {
  121. c[0] = v;
  122. }
  123. void set(const color_type& color)
  124. {
  125. set(color.v);
  126. }
  127. void get(value_type& v) const
  128. {
  129. v = c[0];
  130. }
  131. color_type get() const
  132. {
  133. return color_type(c[0]);
  134. }
  135. pixel_type* next()
  136. {
  137. return this + 1;
  138. }
  139. const pixel_type* next() const
  140. {
  141. return this + 1;
  142. }
  143. pixel_type* advance(int n)
  144. {
  145. return this + n;
  146. }
  147. const pixel_type* advance(int n) const
  148. {
  149. return this + n;
  150. }
  151. };
  152. private:
  153. //--------------------------------------------------------------------
  154. AGG_INLINE void blend_pix(pixel_type* p,
  155. value_type v, value_type a,
  156. unsigned cover)
  157. {
  158. blender_type::blend_pix(p->c, v, a, cover);
  159. }
  160. //--------------------------------------------------------------------
  161. AGG_INLINE void blend_pix(pixel_type* p, value_type v, value_type a)
  162. {
  163. blender_type::blend_pix(p->c, v, a);
  164. }
  165. //--------------------------------------------------------------------
  166. AGG_INLINE void blend_pix(pixel_type* p, const color_type& c, unsigned cover)
  167. {
  168. blender_type::blend_pix(p->c, c.v, c.a, cover);
  169. }
  170. //--------------------------------------------------------------------
  171. AGG_INLINE void blend_pix(pixel_type* p, const color_type& c)
  172. {
  173. blender_type::blend_pix(p->c, c.v, c.a);
  174. }
  175. //--------------------------------------------------------------------
  176. AGG_INLINE void copy_or_blend_pix(pixel_type* p, const color_type& c, unsigned cover)
  177. {
  178. if (!c.is_transparent())
  179. {
  180. if (c.is_opaque() && cover == cover_mask)
  181. {
  182. p->set(c);
  183. }
  184. else
  185. {
  186. blend_pix(p, c, cover);
  187. }
  188. }
  189. }
  190. //--------------------------------------------------------------------
  191. AGG_INLINE void copy_or_blend_pix(pixel_type* p, const color_type& c)
  192. {
  193. if (!c.is_transparent())
  194. {
  195. if (c.is_opaque())
  196. {
  197. p->set(c);
  198. }
  199. else
  200. {
  201. blend_pix(p, c);
  202. }
  203. }
  204. }
  205. public:
  206. //--------------------------------------------------------------------
  207. explicit pixfmt_alpha_blend_gray(rbuf_type& rb) :
  208. m_rbuf(&rb)
  209. {}
  210. void attach(rbuf_type& rb) { m_rbuf = &rb; }
  211. //--------------------------------------------------------------------
  212. template<class PixFmt>
  213. bool attach(PixFmt& pixf, int x1, int y1, int x2, int y2)
  214. {
  215. rect_i r(x1, y1, x2, y2);
  216. if (r.clip(rect_i(0, 0, pixf.width()-1, pixf.height()-1)))
  217. {
  218. int stride = pixf.stride();
  219. m_rbuf->attach(pixf.pix_ptr(r.x1, stride < 0 ? r.y2 : r.y1),
  220. (r.x2 - r.x1) + 1,
  221. (r.y2 - r.y1) + 1,
  222. stride);
  223. return true;
  224. }
  225. return false;
  226. }
  227. //--------------------------------------------------------------------
  228. AGG_INLINE unsigned width() const { return m_rbuf->width(); }
  229. AGG_INLINE unsigned height() const { return m_rbuf->height(); }
  230. AGG_INLINE int stride() const { return m_rbuf->stride(); }
  231. //--------------------------------------------------------------------
  232. int8u* row_ptr(int y) { return m_rbuf->row_ptr(y); }
  233. const int8u* row_ptr(int y) const { return m_rbuf->row_ptr(y); }
  234. row_data row(int y) const { return m_rbuf->row(y); }
  235. //--------------------------------------------------------------------
  236. AGG_INLINE int8u* pix_ptr(int x, int y)
  237. {
  238. return m_rbuf->row_ptr(y) + sizeof(value_type) * (x * pix_step + pix_offset);
  239. }
  240. AGG_INLINE const int8u* pix_ptr(int x, int y) const
  241. {
  242. return m_rbuf->row_ptr(y) + sizeof(value_type) * (x * pix_step + pix_offset);
  243. }
  244. // Return pointer to pixel value, forcing row to be allocated.
  245. AGG_INLINE pixel_type* pix_value_ptr(int x, int y, unsigned len)
  246. {
  247. return (pixel_type*)(m_rbuf->row_ptr(x, y, len) + sizeof(value_type) * (x * pix_step + pix_offset));
  248. }
  249. // Return pointer to pixel value, or null if row not allocated.
  250. AGG_INLINE const pixel_type* pix_value_ptr(int x, int y) const
  251. {
  252. int8u* p = m_rbuf->row_ptr(y);
  253. return p ? (pixel_type*)(p + sizeof(value_type) * (x * pix_step + pix_offset)) : 0;
  254. }
  255. // Get pixel pointer from raw buffer pointer.
  256. AGG_INLINE static pixel_type* pix_value_ptr(void* p)
  257. {
  258. return (pixel_type*)((value_type*)p + pix_offset);
  259. }
  260. // Get pixel pointer from raw buffer pointer.
  261. AGG_INLINE static const pixel_type* pix_value_ptr(const void* p)
  262. {
  263. return (const pixel_type*)((const value_type*)p + pix_offset);
  264. }
  265. //--------------------------------------------------------------------
  266. AGG_INLINE static void write_plain_color(void* p, color_type c)
  267. {
  268. // Grayscale formats are implicitly premultiplied.
  269. c.premultiply();
  270. pix_value_ptr(p)->set(c);
  271. }
  272. //--------------------------------------------------------------------
  273. AGG_INLINE static color_type read_plain_color(const void* p)
  274. {
  275. return pix_value_ptr(p)->get();
  276. }
  277. //--------------------------------------------------------------------
  278. AGG_INLINE static void make_pix(int8u* p, const color_type& c)
  279. {
  280. ((pixel_type*)p)->set(c);
  281. }
  282. //--------------------------------------------------------------------
  283. AGG_INLINE color_type pixel(int x, int y) const
  284. {
  285. if (const pixel_type* p = pix_value_ptr(x, y))
  286. {
  287. return p->get();
  288. }
  289. return color_type::no_color();
  290. }
  291. //--------------------------------------------------------------------
  292. AGG_INLINE void copy_pixel(int x, int y, const color_type& c)
  293. {
  294. pix_value_ptr(x, y, 1)->set(c);
  295. }
  296. //--------------------------------------------------------------------
  297. AGG_INLINE void blend_pixel(int x, int y, const color_type& c, int8u cover)
  298. {
  299. copy_or_blend_pix(pix_value_ptr(x, y, 1), c, cover);
  300. }
  301. //--------------------------------------------------------------------
  302. AGG_INLINE void copy_hline(int x, int y,
  303. unsigned len,
  304. const color_type& c)
  305. {
  306. pixel_type* p = pix_value_ptr(x, y, len);
  307. do
  308. {
  309. p->set(c);
  310. p = p->next();
  311. }
  312. while(--len);
  313. }
  314. //--------------------------------------------------------------------
  315. AGG_INLINE void copy_vline(int x, int y,
  316. unsigned len,
  317. const color_type& c)
  318. {
  319. do
  320. {
  321. pix_value_ptr(x, y++, 1)->set(c);
  322. }
  323. while (--len);
  324. }
  325. //--------------------------------------------------------------------
  326. void blend_hline(int x, int y,
  327. unsigned len,
  328. const color_type& c,
  329. int8u cover)
  330. {
  331. if (!c.is_transparent())
  332. {
  333. pixel_type* p = pix_value_ptr(x, y, len);
  334. if (c.is_opaque() && cover == cover_mask)
  335. {
  336. do
  337. {
  338. p->set(c);
  339. p = p->next();
  340. }
  341. while (--len);
  342. }
  343. else
  344. {
  345. do
  346. {
  347. blend_pix(p, c, cover);
  348. p = p->next();
  349. }
  350. while (--len);
  351. }
  352. }
  353. }
  354. //--------------------------------------------------------------------
  355. void blend_vline(int x, int y,
  356. unsigned len,
  357. const color_type& c,
  358. int8u cover)
  359. {
  360. if (!c.is_transparent())
  361. {
  362. if (c.is_opaque() && cover == cover_mask)
  363. {
  364. do
  365. {
  366. pix_value_ptr(x, y++, 1)->set(c);
  367. }
  368. while (--len);
  369. }
  370. else
  371. {
  372. do
  373. {
  374. blend_pix(pix_value_ptr(x, y++, 1), c, cover);
  375. }
  376. while (--len);
  377. }
  378. }
  379. }
  380. //--------------------------------------------------------------------
  381. void blend_solid_hspan(int x, int y,
  382. unsigned len,
  383. const color_type& c,
  384. const int8u* covers)
  385. {
  386. if (!c.is_transparent())
  387. {
  388. pixel_type* p = pix_value_ptr(x, y, len);
  389. do
  390. {
  391. if (c.is_opaque() && *covers == cover_mask)
  392. {
  393. p->set(c);
  394. }
  395. else
  396. {
  397. blend_pix(p, c, *covers);
  398. }
  399. p = p->next();
  400. ++covers;
  401. }
  402. while (--len);
  403. }
  404. }
  405. //--------------------------------------------------------------------
  406. void blend_solid_vspan(int x, int y,
  407. unsigned len,
  408. const color_type& c,
  409. const int8u* covers)
  410. {
  411. if (!c.is_transparent())
  412. {
  413. do
  414. {
  415. pixel_type* p = pix_value_ptr(x, y++, 1);
  416. if (c.is_opaque() && *covers == cover_mask)
  417. {
  418. p->set(c);
  419. }
  420. else
  421. {
  422. blend_pix(p, c, *covers);
  423. }
  424. ++covers;
  425. }
  426. while (--len);
  427. }
  428. }
  429. //--------------------------------------------------------------------
  430. void copy_color_hspan(int x, int y,
  431. unsigned len,
  432. const color_type* colors)
  433. {
  434. pixel_type* p = pix_value_ptr(x, y, len);
  435. do
  436. {
  437. p->set(*colors++);
  438. p = p->next();
  439. }
  440. while (--len);
  441. }
  442. //--------------------------------------------------------------------
  443. void copy_color_vspan(int x, int y,
  444. unsigned len,
  445. const color_type* colors)
  446. {
  447. do
  448. {
  449. pix_value_ptr(x, y++, 1)->set(*colors++);
  450. }
  451. while (--len);
  452. }
  453. //--------------------------------------------------------------------
  454. void blend_color_hspan(int x, int y,
  455. unsigned len,
  456. const color_type* colors,
  457. const int8u* covers,
  458. int8u cover)
  459. {
  460. pixel_type* p = pix_value_ptr(x, y, len);
  461. if (covers)
  462. {
  463. do
  464. {
  465. copy_or_blend_pix(p, *colors++, *covers++);
  466. p = p->next();
  467. }
  468. while (--len);
  469. }
  470. else
  471. {
  472. if (cover == cover_mask)
  473. {
  474. do
  475. {
  476. copy_or_blend_pix(p, *colors++);
  477. p = p->next();
  478. }
  479. while (--len);
  480. }
  481. else
  482. {
  483. do
  484. {
  485. copy_or_blend_pix(p, *colors++, cover);
  486. p = p->next();
  487. }
  488. while (--len);
  489. }
  490. }
  491. }
  492. //--------------------------------------------------------------------
  493. void blend_color_vspan(int x, int y,
  494. unsigned len,
  495. const color_type* colors,
  496. const int8u* covers,
  497. int8u cover)
  498. {
  499. if (covers)
  500. {
  501. do
  502. {
  503. copy_or_blend_pix(pix_value_ptr(x, y++, 1), *colors++, *covers++);
  504. }
  505. while (--len);
  506. }
  507. else
  508. {
  509. if (cover == cover_mask)
  510. {
  511. do
  512. {
  513. copy_or_blend_pix(pix_value_ptr(x, y++, 1), *colors++);
  514. }
  515. while (--len);
  516. }
  517. else
  518. {
  519. do
  520. {
  521. copy_or_blend_pix(pix_value_ptr(x, y++, 1), *colors++, cover);
  522. }
  523. while (--len);
  524. }
  525. }
  526. }
  527. //--------------------------------------------------------------------
  528. template<class Function> void for_each_pixel(Function f)
  529. {
  530. unsigned y;
  531. for (y = 0; y < height(); ++y)
  532. {
  533. row_data r = m_rbuf->row(y);
  534. if (r.ptr)
  535. {
  536. unsigned len = r.x2 - r.x1 + 1;
  537. pixel_type* p = pix_value_ptr(r.x1, y, len);
  538. do
  539. {
  540. f(p->c);
  541. p = p->next();
  542. }
  543. while (--len);
  544. }
  545. }
  546. }
  547. //--------------------------------------------------------------------
  548. template<class GammaLut> void apply_gamma_dir(const GammaLut& g)
  549. {
  550. for_each_pixel(apply_gamma_dir_gray<color_type, GammaLut>(g));
  551. }
  552. //--------------------------------------------------------------------
  553. template<class GammaLut> void apply_gamma_inv(const GammaLut& g)
  554. {
  555. for_each_pixel(apply_gamma_inv_gray<color_type, GammaLut>(g));
  556. }
  557. //--------------------------------------------------------------------
  558. template<class RenBuf2>
  559. void copy_from(const RenBuf2& from,
  560. int xdst, int ydst,
  561. int xsrc, int ysrc,
  562. unsigned len)
  563. {
  564. if (const int8u* p = from.row_ptr(ysrc))
  565. {
  566. memmove(m_rbuf->row_ptr(xdst, ydst, len) + xdst * pix_width,
  567. p + xsrc * pix_width,
  568. len * pix_width);
  569. }
  570. }
  571. //--------------------------------------------------------------------
  572. // Blend from single color, using grayscale surface as alpha channel.
  573. template<class SrcPixelFormatRenderer>
  574. void blend_from_color(const SrcPixelFormatRenderer& from,
  575. const color_type& color,
  576. int xdst, int ydst,
  577. int xsrc, int ysrc,
  578. unsigned len,
  579. int8u cover)
  580. {
  581. typedef typename SrcPixelFormatRenderer::pixel_type src_pixel_type;
  582. typedef typename SrcPixelFormatRenderer::color_type src_color_type;
  583. if (const src_pixel_type* psrc = from.pix_value_ptr(xsrc, ysrc))
  584. {
  585. pixel_type* pdst = pix_value_ptr(xdst, ydst, len);
  586. do
  587. {
  588. copy_or_blend_pix(pdst, color, src_color_type::scale_cover(cover, psrc->c[0]));
  589. psrc = psrc->next();
  590. pdst = pdst->next();
  591. }
  592. while (--len);
  593. }
  594. }
  595. //--------------------------------------------------------------------
  596. // Blend from color table, using grayscale surface as indexes into table.
  597. // Obviously, this only works for integer value types.
  598. template<class SrcPixelFormatRenderer>
  599. void blend_from_lut(const SrcPixelFormatRenderer& from,
  600. const color_type* color_lut,
  601. int xdst, int ydst,
  602. int xsrc, int ysrc,
  603. unsigned len,
  604. int8u cover)
  605. {
  606. typedef typename SrcPixelFormatRenderer::pixel_type src_pixel_type;
  607. if (const src_pixel_type* psrc = from.pix_value_ptr(xsrc, ysrc))
  608. {
  609. pixel_type* pdst = pix_value_ptr(xdst, ydst, len);
  610. do
  611. {
  612. copy_or_blend_pix(pdst, color_lut[psrc->c[0]], cover);
  613. psrc = psrc->next();
  614. pdst = pdst->next();
  615. }
  616. while (--len);
  617. }
  618. }
  619. private:
  620. rbuf_type* m_rbuf;
  621. };
  622. typedef blender_gray<gray8> blender_gray8;
  623. typedef blender_gray<sgray8> blender_sgray8;
  624. typedef blender_gray<gray16> blender_gray16;
  625. typedef blender_gray<gray32> blender_gray32;
  626. typedef blender_gray_pre<gray8> blender_gray8_pre;
  627. typedef blender_gray_pre<sgray8> blender_sgray8_pre;
  628. typedef blender_gray_pre<gray16> blender_gray16_pre;
  629. typedef blender_gray_pre<gray32> blender_gray32_pre;
  630. typedef pixfmt_alpha_blend_gray<blender_gray8, rendering_buffer> pixfmt_gray8;
  631. typedef pixfmt_alpha_blend_gray<blender_sgray8, rendering_buffer> pixfmt_sgray8;
  632. typedef pixfmt_alpha_blend_gray<blender_gray16, rendering_buffer> pixfmt_gray16;
  633. typedef pixfmt_alpha_blend_gray<blender_gray32, rendering_buffer> pixfmt_gray32;
  634. typedef pixfmt_alpha_blend_gray<blender_gray8_pre, rendering_buffer> pixfmt_gray8_pre;
  635. typedef pixfmt_alpha_blend_gray<blender_sgray8_pre, rendering_buffer> pixfmt_sgray8_pre;
  636. typedef pixfmt_alpha_blend_gray<blender_gray16_pre, rendering_buffer> pixfmt_gray16_pre;
  637. typedef pixfmt_alpha_blend_gray<blender_gray32_pre, rendering_buffer> pixfmt_gray32_pre;
  638. }
  639. #endif