solid.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "precomp.h"
  2. #include "solid.h"
  3. #include <api/xml/xmlparams.h>
  4. #include <api/memmgr/api_memmgr.h>
  5. #ifndef _WASABIRUNTIME
  6. BEGIN_SERVICES(SolidGen_Svc);
  7. DECLARE_SERVICETSINGLE(svc_imageGenerator, SolidImage);
  8. END_SERVICES(SolidGen_Svc, _SolidGen_Svc);
  9. #ifdef _X86_
  10. extern "C" { int _link_SolidGen_Svc; }
  11. #else
  12. extern "C" { int __link_SolidGen_Svc; }
  13. #endif
  14. #endif
  15. int SolidImage::testDesc(const wchar_t *desc)
  16. {
  17. return !WCSICMP(desc, L"$solid");
  18. }
  19. void premultiply(ARGB32 *m_pBits, int nwords)
  20. {
  21. for (; nwords > 0; nwords--, m_pBits++)
  22. {
  23. unsigned __int8 *pixel = (unsigned __int8 *)m_pBits;
  24. unsigned int alpha = pixel[3];
  25. if (alpha == 255) continue;
  26. pixel[0] = (pixel[0] * alpha) >> 8; // blue
  27. pixel[1] = (pixel[1] * alpha) >> 8; // green
  28. pixel[2] = (pixel[2] * alpha) >> 8; // red
  29. }
  30. }
  31. ARGB32 *SolidImage::genImage(const wchar_t *desc, int *has_alpha, int *w, int *h, ifc_xmlreaderparams *params)
  32. {
  33. int _w = params->getItemValueInt(L"w", 1);
  34. if (_w == 0) _w = 1;
  35. int _h = params->getItemValueInt(L"h", 1);
  36. if (_h == 0) _h = 1;
  37. if (_w <= 0 || _h <= 0) return NULL;
  38. ARGB32 color = _byteswap_ulong(WASABI_API_SKIN->parse(params->getItemValue(L"color"), L"color") << 8);
  39. unsigned int alpha = params->getItemValueInt(L"alpha", 255);
  40. color |= ((alpha & 0xff) << 24);
  41. premultiply(&color, 1);
  42. #ifdef WASABI_COMPILE_MEMMGR
  43. ARGB32 *ret = (ARGB32*)WASABI_API_MEMMGR->sysMalloc(_w * _h * sizeof(ARGB32));
  44. #else
  45. ARGB32 *ret = (ARGB32*)MALLOC(_w * _h * sizeof(ARGB32));
  46. #endif
  47. MEMFILL<ARGB32>(ret, color, _w * _h);
  48. *w = _w;
  49. *h = _h;
  50. *has_alpha = (alpha == 255) ? 0 : 1;
  51. return ret;
  52. }