loadimage.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "main.h"
  2. #include "./loadimage.h"
  3. #include "./api.h"
  4. #include <api/service/waServiceFactory.h>
  5. #include <api/service/svcs/svc_imgload.h>
  6. #define ABS(x) (((x) > 0) ? (x) : (-x))
  7. static HANDLE ReadFromFile(LPCWSTR pszImage, DWORD *size, HANDLE *hres)
  8. {
  9. HANDLE hFile = CreateFileW(pszImage, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL), hmem(NULL);
  10. *size = 0;
  11. if (INVALID_HANDLE_VALUE != hFile)
  12. {
  13. *size = GetFileSize(hFile, NULL);
  14. if (INVALID_FILE_SIZE != *size)
  15. {
  16. hmem = HeapAlloc(GetProcessHeap(), 0, *size);
  17. if (hmem)
  18. {
  19. DWORD readed = 0;
  20. if (!ReadFile(hFile, hmem, *size, &readed, NULL) || *size != readed)
  21. {
  22. HeapFree(GetProcessHeap(), 0, hmem);
  23. hmem = NULL;
  24. }
  25. }
  26. }
  27. CloseHandle(hFile);
  28. }
  29. *hres = hmem;
  30. return hmem;
  31. }
  32. static HANDLE ReadFromRes(HMODULE hMod, LPCWSTR pszSection, LPCWSTR pszImage, DWORD *size, HANDLE *hres)
  33. {
  34. *size = 0;
  35. HRSRC res = FindResourceW(hMod, pszImage, pszSection);
  36. if (res)
  37. {
  38. *hres = LoadResource(hMod, res);
  39. *size = SizeofResource(hMod, res);
  40. return LockResource(*hres);
  41. }
  42. return NULL;
  43. }
  44. static HANDLE LoadImg(HANDLE data, DWORD size, INT *cx, INT *cy, BOOL premult)
  45. {
  46. FOURCC imgload = svc_imageLoader::getServiceType();
  47. int n = (int) WASABI_API_SVC->service_getNumServices(imgload);
  48. for (int i=0; i<n; i++)
  49. {
  50. waServiceFactory *sf = WASABI_API_SVC->service_enumService(imgload,i);
  51. if (sf)
  52. {
  53. svc_imageLoader * l = (svc_imageLoader*)sf->getInterface();
  54. if (l)
  55. {
  56. if (l->testData(data,size))
  57. {
  58. HANDLE ret;
  59. ret = (premult) ? l->loadImage(data, size, cx, cy) : l->loadImageData(data, size, cx, cy);
  60. sf->releaseInterface(l);
  61. return ret;
  62. }
  63. sf->releaseInterface(l);
  64. }
  65. }
  66. }
  67. return NULL;
  68. }
  69. HBITMAP WALoadImage(HMODULE hMod, LPCWSTR pszSection, LPCWSTR pszImage, BOOL bPremult)
  70. {
  71. BITMAPINFOHEADER header = {0};
  72. HBITMAP hbmp = NULL;
  73. HANDLE hres = NULL;
  74. LPVOID dib = NULL;
  75. DWORD size = 0;
  76. header.biBitCount = 32;
  77. header.biPlanes = 1;
  78. header.biSize = sizeof(BITMAPINFOHEADER);
  79. LPVOID data = (!hMod) ? ReadFromFile(pszImage, &size, &hres) : ReadFromRes(hMod, pszSection, pszImage, &size, &hres);
  80. if (data) data = LoadImg(data, size, (int*)&header.biWidth, (int*)&header.biHeight, bPremult);
  81. if (hres)
  82. {
  83. (hMod) ? FreeResource(hres) : HeapFree(GetProcessHeap(), 0, hres);
  84. }
  85. if (data)
  86. {
  87. header.biHeight = 0 - header.biHeight;
  88. hbmp = CreateDIBSection(NULL, (LPBITMAPINFO)&header, DIB_RGB_COLORS, &dib, NULL, 0);
  89. if (hbmp) CopyMemory(dib, data, header.biWidth * ABS(header.biHeight) * sizeof(DWORD));
  90. WASABI_API_MEMMGR->sysFree(data);
  91. }
  92. else hbmp = NULL;
  93. return hbmp;
  94. }
  95. HBITMAP WAResizeImage(HBITMAP hbmp, INT cx, INT cy, HBRUSH hbBk)
  96. {
  97. BITMAP bi = {0};
  98. if (!hbmp || !GetObject(hbmp, sizeof(BITMAP), &bi)) return hbmp;
  99. if (bi.bmWidth != cx || bi.bmHeight != cy)
  100. {
  101. INT ix = cx, iy = cy;
  102. HDC hdc = GetDC(NULL),
  103. hdcSrc = CreateCompatibleDC(hdc),
  104. hdcDst = CreateCompatibleDC(hdc);
  105. HBITMAP hbmpOld1 = (HBITMAP)SelectObject(hdcSrc, hbmp);
  106. hbmp = CreateCompatibleBitmap(hdc, cx, cy);
  107. HBITMAP hbmpOld2 = (HBITMAP)SelectObject(hdcDst, hbmp);
  108. if (ix != cx || iy != cy)
  109. {
  110. RECT r;
  111. SetRect(&r, 0, 0, cx, cy);
  112. FillRect(hdcDst, &r, hbBk);
  113. }
  114. SetStretchBltMode(hdcDst, HALFTONE);
  115. StretchBlt(hdcDst, (cx - ix)/2, (cy - iy)/2, ix, iy, hdcSrc, 0, 0, bi.bmWidth, bi.bmHeight, SRCCOPY);
  116. SelectObject(hdcDst, hbmpOld2);
  117. hbmpOld2 = (HBITMAP)SelectObject(hdcSrc, hbmpOld1);
  118. if (hbmpOld2) DeleteObject(hbmpOld2);
  119. DeleteDC(hdcSrc);
  120. DeleteDC(hdcDst);
  121. ReleaseDC(NULL, hdc);
  122. }
  123. return hbmp;
  124. }
  125. HBITMAP WABlendOnColor(HBITMAP hbmp, COLORREF rgbBk)
  126. {
  127. DIBSECTION dibsec = {0};
  128. LONG pitch, x;
  129. INT cx, cy;
  130. LPBYTE cursor = NULL, line = NULL, pData = NULL;
  131. if (!hbmp || sizeof(DIBSECTION) != GetObjectW(hbmp, sizeof(DIBSECTION), &dibsec) ||
  132. BI_RGB != dibsec.dsBmih.biCompression || 1 != dibsec.dsBmih.biPlanes || 32 != dibsec.dsBm.bmBitsPixel) return hbmp;
  133. cx = dibsec.dsBm.bmWidth;
  134. cy = dibsec.dsBm.bmHeight;
  135. pitch = dibsec.dsBm.bmWidth*4;
  136. pData = (LPBYTE)dibsec.dsBm.bmBits;
  137. for (line = pData; cy-- != 0; line += pitch )
  138. {
  139. for (x = cx, cursor = line; x-- != 0; cursor += 4)
  140. {
  141. if (0x00 == cursor[3])
  142. {
  143. cursor[0] = GetBValue(rgbBk);
  144. cursor[1] = GetGValue(rgbBk);
  145. cursor[2] = GetRValue(rgbBk);
  146. cursor[3] = 0xFF;
  147. }
  148. else if (cursor[3] != 0xFF)
  149. {
  150. cursor[0] = (cursor[0]*cursor[3] + (((255 - cursor[3])*255 + 127)/255)*GetBValue(rgbBk) + 127)/255;
  151. cursor[1] = (cursor[1]*cursor[3] + (((255 - cursor[3])*255 + 127)/255)*GetGValue(rgbBk) + 127)/255;
  152. cursor[2] = (cursor[2]*cursor[3] + (((255 - cursor[3])*255 + 127)/255)*GetRValue(rgbBk) + 127)/255;
  153. cursor[3] = 0xFF;
  154. }
  155. }
  156. }
  157. return hbmp;
  158. }
  159. HBITMAP WACreatePatternBitmap(INT cx, INT cy, INT bpp, HBRUSH hbPattern, LPBYTE *ppData)
  160. {
  161. BITMAPINFOHEADER bi = {0};
  162. RECT r;
  163. HDC hdcTmp = CreateCompatibleDC(NULL);
  164. bi.biSize = sizeof (bi);
  165. bi.biWidth= cx;
  166. bi.biHeight = -cy;
  167. bi.biPlanes = 1;
  168. bi.biBitCount= (WORD)bpp;
  169. HBITMAP hbmpPattern = CreateDIBSection(hdcTmp, (BITMAPINFO *)&bi, DIB_RGB_COLORS, (LPVOID*)ppData, NULL, NULL);
  170. if (!hbmpPattern) return NULL;
  171. HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcTmp, hbmpPattern);
  172. SetRect(&r, 0, 0, cx, cy);
  173. FillRect(hdcTmp, &r, hbPattern);
  174. SelectObject(hdcTmp, hbmpOld);
  175. DeleteDC(hdcTmp);
  176. return hbmpPattern;
  177. }
  178. HBITMAP WABlendOnARGB32(HBITMAP hbmp, LPBYTE pRGB)
  179. {
  180. DIBSECTION dibsec = {0};
  181. LONG pitch, x;
  182. INT cx, cy;
  183. LPBYTE cursor = NULL, cursor2 = NULL, line = NULL, line2 = NULL, pData = NULL;
  184. if (!hbmp || sizeof(DIBSECTION) != GetObjectW(hbmp, sizeof(DIBSECTION), &dibsec) ||
  185. BI_RGB != dibsec.dsBmih.biCompression || 1 != dibsec.dsBmih.biPlanes || 32 != dibsec.dsBm.bmBitsPixel) return hbmp;
  186. cx = dibsec.dsBm.bmWidth;
  187. cy = dibsec.dsBm.bmHeight;
  188. pitch = dibsec.dsBm.bmWidth*4;
  189. pData = (LPBYTE)dibsec.dsBm.bmBits;
  190. for (line = pData, line2 = pRGB; cy-- != 0; line += pitch, line2 += pitch )
  191. {
  192. for (x = cx, cursor = line, cursor2 = line2; x-- != 0; cursor += 4, cursor2 += 4)
  193. {
  194. if (0x00 == cursor[3]) *((DWORD*)cursor) = *((DWORD*)cursor2);
  195. else if (cursor[3] != 0xFF)
  196. {
  197. cursor[0] = (cursor[0]*cursor[3] + (((255 - cursor[3])*255 + 127)/255)*cursor2[0] + 127)/255;
  198. cursor[1] = (cursor[1]*cursor[3] + (((255 - cursor[3])*255 + 127)/255)*cursor2[1] + 127)/255;
  199. cursor[2] = (cursor[2]*cursor[3] + (((255 - cursor[3])*255 + 127)/255)*cursor2[2] + 127)/255;
  200. }
  201. cursor[3] = 0xFF;
  202. }
  203. }
  204. return hbmp;
  205. }