ddpf_conversion.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. XCC Utilities and Library
  3. Copyright (C) 2000 Olaf van der Spek <[email protected]>
  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. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "stdafx.h"
  16. #include "ddpf_conversion.h"
  17. static int get_shift(unsigned int v)
  18. {
  19. if (!v)
  20. return 0;
  21. int r = 0;
  22. while (~v & 1)
  23. {
  24. r++;
  25. v >>= 1;
  26. }
  27. return r;
  28. }
  29. static int get_size(unsigned int v)
  30. {
  31. int r = 0;
  32. while (v)
  33. {
  34. if (v & 1)
  35. r++;
  36. v >>= 1;
  37. }
  38. return r;
  39. }
  40. void Cddpf_conversion::set_pf(const DDPIXELFORMAT& pf)
  41. {
  42. a_shift = get_shift(pf.dwRGBAlphaBitMask);
  43. r_shift = get_shift(pf.dwRBitMask);
  44. g_shift = get_shift(pf.dwGBitMask);
  45. b_shift = get_shift(pf.dwBBitMask);
  46. a_size = get_size(pf.dwRGBAlphaBitMask);
  47. r_size = get_size(pf.dwRBitMask);
  48. g_size = get_size(pf.dwGBitMask);
  49. b_size = get_size(pf.dwBBitMask);
  50. }
  51. int Cddpf_conversion::get_color(int a, int r, int g, int b)
  52. {
  53. return a >> (8 - a_size) << a_shift | r >> (8 - r_size) << r_shift | g >> (8 - g_size) << g_shift | b >> (8 - b_size) << b_shift;
  54. }
  55. int Cddpf_conversion::get_color(int r, int g, int b)
  56. {
  57. return get_color(0, r, g, b);
  58. }
  59. int Cddpf_conversion::get_color(int v)
  60. {
  61. return get_color(v >> 24, v >> 16 & 0xff, v >> 8 & 0xff, v & 0xff);
  62. }