placeholder.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <windows.h>
  2. #include <strsafe.h>
  3. #include <gdiplus.h>
  4. #include <stdint.h>
  5. #include <vector>
  6. using namespace Gdiplus;
  7. extern HINSTANCE dll_inst;
  8. static std::vector<uint8_t> placeholder;
  9. static bool initialized = false;
  10. int cx, cy;
  11. /* XXX: optimize this later. or don't, it's only called once. */
  12. static void convert_placeholder(const uint8_t *rgb_in, int width, int height)
  13. {
  14. size_t size = width * height * 3;
  15. size_t linesize = width * 3;
  16. std::vector<uint8_t> yuv_out;
  17. yuv_out.resize(size);
  18. const uint8_t *in = rgb_in;
  19. const uint8_t *end = in + size;
  20. uint8_t *out = &yuv_out[0];
  21. while (in < end) {
  22. const int16_t b = *(in++);
  23. const int16_t g = *(in++);
  24. const int16_t r = *(in++);
  25. *(out++) = (uint8_t)(((66 * r + 129 * g + 25 * b + 128) >> 8) +
  26. 16);
  27. *(out++) = (uint8_t)(((-38 * r - 74 * g + 112 * b + 128) >> 8) +
  28. 128);
  29. *(out++) = (uint8_t)(((112 * r - 94 * g - 18 * b + 128) >> 8) +
  30. 128);
  31. }
  32. placeholder.resize(width * height * 3 / 2);
  33. in = &yuv_out[0];
  34. end = in + size;
  35. out = &placeholder[0];
  36. uint8_t *chroma = out + width * height;
  37. while (in < end) {
  38. const uint8_t *in2 = in + linesize;
  39. const uint8_t *end2 = in2;
  40. uint8_t *out2 = out + width;
  41. while (in < end2) {
  42. int16_t u;
  43. int16_t v;
  44. *(out++) = *(in++);
  45. u = *(in++);
  46. v = *(in++);
  47. *(out++) = *(in++);
  48. u += *(in++);
  49. v += *(in++);
  50. *(out2++) = *(in2++);
  51. u += *(in2++);
  52. v += *(in2++);
  53. *(out2++) = *(in2++);
  54. u += *(in2++);
  55. v += *(in2++);
  56. *(chroma++) = (uint8_t)(u / 4);
  57. *(chroma++) = (uint8_t)(v / 4);
  58. }
  59. in = in2;
  60. out = out2;
  61. }
  62. }
  63. static bool load_placeholder_internal()
  64. {
  65. Status s;
  66. wchar_t file[MAX_PATH];
  67. if (!GetModuleFileNameW(dll_inst, file, MAX_PATH)) {
  68. return false;
  69. }
  70. wchar_t *slash = wcsrchr(file, '\\');
  71. if (!slash) {
  72. return false;
  73. }
  74. slash[1] = 0;
  75. StringCbCat(file, sizeof(file), L"placeholder.png");
  76. Bitmap bmp(file);
  77. if (bmp.GetLastStatus() != Status::Ok) {
  78. return false;
  79. }
  80. cx = bmp.GetWidth();
  81. cy = bmp.GetHeight();
  82. BitmapData bmd = {};
  83. Rect r(0, 0, cx, cy);
  84. s = bmp.LockBits(&r, ImageLockModeRead, PixelFormat24bppRGB, &bmd);
  85. if (s != Status::Ok) {
  86. return false;
  87. }
  88. convert_placeholder((const uint8_t *)bmd.Scan0, cx, cy);
  89. bmp.UnlockBits(&bmd);
  90. return true;
  91. }
  92. bool initialize_placeholder()
  93. {
  94. GdiplusStartupInput si;
  95. ULONG_PTR token;
  96. GdiplusStartup(&token, &si, nullptr);
  97. initialized = load_placeholder_internal();
  98. GdiplusShutdown(token);
  99. return initialized;
  100. }
  101. const uint8_t *get_placeholder_ptr()
  102. {
  103. if (initialized)
  104. return placeholder.data();
  105. return nullptr;
  106. }
  107. const bool get_placeholder_size(int *out_cx, int *out_cy)
  108. {
  109. if (initialized) {
  110. *out_cx = cx;
  111. *out_cy = cy;
  112. return true;
  113. }
  114. return false;
  115. }