placeholder.cpp 2.7 KB

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