1
0

imageCache.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. #include "main.h"
  2. #include "./imageCache.h"
  3. #include <wincodec.h>
  4. #include <vector>
  5. #include <algorithm>
  6. struct DeviceColoredImage
  7. {
  8. size_t ref;
  9. DeviceImage *base;
  10. HBITMAP bitmap;
  11. COLORREF color1;
  12. COLORREF color2;
  13. DeviceImageFilter filter;
  14. void *filterParam;
  15. };
  16. typedef std::vector<DeviceColoredImage*> DeviceColoredImageList;
  17. struct DeviceImage
  18. {
  19. size_t ref;
  20. DeviceImageCache *cache;
  21. wchar_t *source;
  22. HBITMAP bitmap;
  23. HBITMAP exactBitmap;
  24. int width;
  25. int height;
  26. DeviceImageLoader loader;
  27. void *loaderParam;
  28. DeviceColoredImageList list;
  29. };
  30. typedef std::vector<DeviceImage*> DeviceImageList;
  31. struct DeviceImageCache
  32. {
  33. DeviceImageList list;
  34. IWICImagingFactory *wicFactory;
  35. };
  36. typedef struct DeviceImageSeachParam
  37. {
  38. const wchar_t *path;
  39. int width;
  40. int height;
  41. } DeviceImageSeachParam;
  42. typedef struct DeviceColoredImageSeachParam
  43. {
  44. COLORREF color1;
  45. COLORREF color2;
  46. } DeviceColoredImageSeachParam;
  47. DeviceImageCache *
  48. DeviceImageCache_Create()
  49. {
  50. DeviceImageCache *self;
  51. self = new DeviceImageCache();
  52. if (NULL == self)
  53. return NULL;
  54. self->wicFactory = NULL;
  55. return self;
  56. }
  57. void
  58. DeviceImageCache_Free(DeviceImageCache *self)
  59. {
  60. if (NULL == self)
  61. return;
  62. size_t index = self->list.size();
  63. while(index--)
  64. {
  65. DeviceImage *image = self->list[index];
  66. image->cache = NULL;
  67. }
  68. if (NULL != self->wicFactory)
  69. self->wicFactory->Release();
  70. delete(self);
  71. }
  72. static DeviceImage *
  73. DeviceImage_Create(DeviceImageCache *cache, const wchar_t *path, int width, int height,
  74. DeviceImageLoader loader, void *loaderParam)
  75. {
  76. DeviceImage *self;
  77. if (NULL == loader)
  78. return NULL;
  79. self = new DeviceImage();
  80. if (NULL == self)
  81. return NULL;
  82. self->ref = 1;
  83. self->cache = cache;
  84. self->source = ResourceString_Duplicate(path);
  85. self->loader = loader;
  86. self->loaderParam = loaderParam;
  87. self->bitmap = NULL;
  88. self->exactBitmap = NULL;
  89. self->width = width;
  90. self->height = height;
  91. return self;
  92. }
  93. static void
  94. DeviceImage_Free(DeviceImage *self)
  95. {
  96. if (NULL == self)
  97. return;
  98. if (NULL != self->source)
  99. ResourceString_Free(self->source);
  100. if (NULL != self->bitmap)
  101. DeleteObject(self->bitmap);
  102. if (NULL != self->exactBitmap &&
  103. self->exactBitmap != self->bitmap)
  104. {
  105. DeleteObject(self->exactBitmap);
  106. }
  107. delete(self);
  108. }
  109. static int
  110. DeviceImageCache_SearchCb(const void *key, const void *element)
  111. {
  112. DeviceImageSeachParam *search;
  113. DeviceImage *image;
  114. int result;
  115. search = (DeviceImageSeachParam*)key;
  116. image = (DeviceImage*)element;
  117. result = search->height - image->height;
  118. if (0 != result)
  119. return result;
  120. result = search->width - image->width;
  121. if (0 != result)
  122. return result;
  123. if (FALSE != IS_INTRESOURCE(search->path) ||
  124. FALSE != IS_INTRESOURCE(image->source))
  125. {
  126. return (int)(INT_PTR)(search->path - image->source);
  127. }
  128. return CompareString(CSTR_INVARIANT, 0, search->path, -1, image->source, -1) - 2;
  129. }
  130. static int
  131. DeviceImageCache_SortCb(const void *element1, const void *element2)
  132. {
  133. DeviceImage *image1;
  134. DeviceImage *image2;
  135. int result;
  136. image1 = (DeviceImage*)element1;
  137. image2 = (DeviceImage*)element2;
  138. result = image1->height - image2->height;
  139. if (0 != result)
  140. return result;
  141. result = image1->width - image2->width;
  142. if (0 != result)
  143. return result;
  144. if (FALSE != IS_INTRESOURCE(image1->source) ||
  145. FALSE != IS_INTRESOURCE(image2->source))
  146. {
  147. return (int)(INT_PTR)(image1->source - image2->source);
  148. }
  149. return CompareString(CSTR_INVARIANT, 0, image1->source, -1, image2->source, -1) - 2;
  150. }
  151. static int
  152. DeviceImageCache_SortCb_V2(const void* element1, const void* element2)
  153. {
  154. return DeviceImageCache_SortCb(element1, element2) < 0;
  155. }
  156. static HBITMAP
  157. DeviceImage_DefaultImageLoader(const wchar_t *path, int width, int height, void *param)
  158. {
  159. HBITMAP bitmap;
  160. unsigned int flags;
  161. flags = ISF_PREMULTIPLY;
  162. if (FALSE == IS_INTRESOURCE(path))
  163. flags |= ISF_LOADFROMFILE;
  164. bitmap = Image_Load(path, SRC_TYPE_PNG, flags, 0, 0);
  165. return bitmap;
  166. }
  167. DeviceImage *
  168. DeviceImageCache_GetImage(DeviceImageCache *self, const wchar_t *path, int width, int height, DeviceImageLoader loader, void *user)
  169. {
  170. DeviceImage *image, *image_ptr = 0;
  171. DeviceImageSeachParam searchParam;
  172. if (width < 1)
  173. width = 0;
  174. if (height < 1)
  175. height = 0;
  176. if (NULL == self)
  177. return NULL;
  178. if (NULL == path ||
  179. (FALSE == IS_INTRESOURCE(path) && L'\0' == *path))
  180. {
  181. return NULL;
  182. }
  183. searchParam.height = height;
  184. searchParam.width = width;
  185. searchParam.path = path;
  186. //image_ptr = (DeviceImage**)bsearch(&searchParam, &self->list[0], self->list.size(),
  187. // sizeof(DeviceImage**),
  188. // DeviceImageCache_SearchCb);
  189. auto it = std::find_if(self->list.begin(), self->list.end(),
  190. [&](DeviceImage* upT) -> bool
  191. {
  192. return DeviceImageCache_SearchCb(&searchParam, upT) == 0;
  193. }
  194. );
  195. if (it != self->list.end())
  196. {
  197. image_ptr = *it;
  198. }
  199. if (NULL != image_ptr)
  200. {
  201. image = image_ptr;
  202. DeviceImage_AddRef(image);
  203. return image;
  204. }
  205. if (NULL == loader)
  206. loader = DeviceImage_DefaultImageLoader;
  207. image = DeviceImage_Create(self, path, width, height, loader, user);
  208. if (NULL != image)
  209. {
  210. self->list.push_back(image);
  211. //qsort(&self->list[0], self->list.size(), sizeof(DeviceImage**), DeviceImageCache_SortCb);
  212. std::sort(self->list.begin(), self->list.end(), DeviceImageCache_SortCb_V2);
  213. }
  214. return image;
  215. }
  216. static BOOL
  217. DeviceImageCache_RemoveImage(DeviceImageCache *self, DeviceImage *image)
  218. {
  219. size_t index;
  220. if (NULL == self || NULL == image)
  221. return FALSE;
  222. index = self->list.size();
  223. while(index--)
  224. {
  225. if (self->list[index] == image)
  226. {
  227. self->list.erase(self->list.begin() + index);
  228. return TRUE;
  229. }
  230. }
  231. return FALSE;
  232. }
  233. size_t
  234. DeviceImage_AddRef(DeviceImage *self)
  235. {
  236. if (NULL == self)
  237. return 0;
  238. return InterlockedIncrement((LONG*)&self->ref);
  239. }
  240. size_t
  241. DeviceImage_Release(DeviceImage *self)
  242. {
  243. size_t r;
  244. if (NULL == self || 0 == self->ref)
  245. return 0;
  246. r = InterlockedDecrement((LONG*)&self->ref);
  247. if (0 == r)
  248. {
  249. if (NULL != self->cache)
  250. DeviceImageCache_RemoveImage(self->cache, self);
  251. DeviceImage_Free(self);
  252. return 0;
  253. }
  254. return r;
  255. }
  256. BOOL
  257. DeviceImage_GetSize(DeviceImage *self, int *width, int *height)
  258. {
  259. if (NULL == self)
  260. return FALSE;
  261. if (NULL != width)
  262. *width = self->width;
  263. if (NULL != height)
  264. *height = self->height;
  265. return TRUE;
  266. }
  267. static IWICImagingFactory*
  268. DeviceImage_GetWicFactory(DeviceImageCache *cache)
  269. {
  270. IWICImagingFactory *wicFactory;
  271. HRESULT hr;
  272. if (NULL != cache &&
  273. NULL != cache->wicFactory)
  274. {
  275. cache->wicFactory->AddRef();
  276. return cache->wicFactory;
  277. }
  278. hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
  279. IID_PPV_ARGS(&wicFactory));
  280. if (FAILED(hr))
  281. return NULL;
  282. if (NULL != cache)
  283. {
  284. wicFactory->AddRef();
  285. cache->wicFactory = wicFactory;
  286. }
  287. return wicFactory;
  288. }
  289. static HBITMAP
  290. DeviceImage_HBitmapFromWicSource(IWICBitmapSource *wicSource, unsigned int targetWidth,
  291. unsigned int targetHeight, DeviceImageFlags flags)
  292. {
  293. HRESULT hr;
  294. HBITMAP bitmap;
  295. BITMAPINFO bitmapInfo;
  296. unsigned int width, height, bitmapWidth, bitmapHeight;
  297. void *pixelData = NULL;
  298. WICPixelFormatGUID pixelFormat;
  299. HDC windowDC;
  300. unsigned int strideSize, imageSize;
  301. if (NULL == wicSource)
  302. return NULL;
  303. hr = wicSource->GetPixelFormat(&pixelFormat);
  304. if (FAILED(hr) ||
  305. (GUID_WICPixelFormat32bppPBGRA != pixelFormat &&
  306. GUID_WICPixelFormat32bppBGR != pixelFormat &&
  307. GUID_WICPixelFormat32bppBGRA != pixelFormat &&
  308. GUID_WICPixelFormat32bppRGBA != pixelFormat &&
  309. GUID_WICPixelFormat32bppPRGBA != pixelFormat))
  310. {
  311. return NULL;
  312. }
  313. hr = wicSource->GetSize(&width, &height);
  314. if (FAILED(hr))
  315. return NULL;
  316. if (0 != (DeviceImage_ExactSize & flags))
  317. {
  318. bitmapWidth = (targetWidth > width) ? targetWidth : width;
  319. bitmapHeight = (targetHeight > height) ? targetHeight : height;
  320. }
  321. else
  322. {
  323. bitmapWidth = width;
  324. bitmapHeight = height;
  325. }
  326. ZeroMemory(&bitmapInfo, sizeof(bitmapInfo));
  327. bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  328. bitmapInfo.bmiHeader.biWidth = bitmapWidth;
  329. bitmapInfo.bmiHeader.biHeight = -(LONG)bitmapHeight;
  330. bitmapInfo.bmiHeader.biPlanes = 1;
  331. bitmapInfo.bmiHeader.biBitCount = 32;
  332. bitmapInfo.bmiHeader.biCompression = BI_RGB;
  333. windowDC = GetDCEx(NULL, NULL, DCX_WINDOW | DCX_CACHE);
  334. bitmap = CreateDIBSection(windowDC, &bitmapInfo, DIB_RGB_COLORS, &pixelData, NULL, 0);
  335. if (NULL != windowDC)
  336. ReleaseDC(NULL, windowDC);
  337. if (NULL == bitmap)
  338. return NULL;
  339. hr = UIntMult(bitmapWidth, sizeof(DWORD), &strideSize);
  340. if (SUCCEEDED(hr))
  341. {
  342. if (0 != (DeviceImage_ExactSize & flags) &&
  343. (bitmapWidth > width || bitmapHeight > height))
  344. {
  345. if (SUCCEEDED(UIntMult(strideSize, bitmapHeight, &imageSize)))
  346. ZeroMemory(pixelData, imageSize);
  347. }
  348. hr = UIntMult(strideSize, height, &imageSize);
  349. if (SUCCEEDED(hr))
  350. {
  351. unsigned int offset, delta;
  352. offset = 0;
  353. if (0 != (DeviceImage_AlignVCenter & flags))
  354. {
  355. delta = bitmapHeight - height;
  356. delta = delta/2 + delta%2;
  357. }
  358. else if (0 != (DeviceImage_AlignBottom & flags))
  359. delta = bitmapHeight - height;
  360. else
  361. delta = 0;
  362. if (0 != delta && SUCCEEDED(UIntMult(delta, strideSize, &delta)))
  363. offset += delta;
  364. if (0 != (DeviceImage_AlignHCenter & flags))
  365. {
  366. delta = bitmapWidth - width;
  367. delta = delta/2;
  368. }
  369. else if (0 != (DeviceImage_AlignRight & flags))
  370. delta = bitmapWidth - width;
  371. else
  372. delta = 0;
  373. if (0 != delta && SUCCEEDED(UIntMult(delta, sizeof(DWORD), &delta)))
  374. offset += delta;
  375. hr = wicSource->CopyPixels(NULL, strideSize, imageSize, ((BYTE*)pixelData) + offset);
  376. }
  377. }
  378. if (FAILED(hr))
  379. {
  380. DeleteObject(bitmap);
  381. bitmap = NULL;
  382. }
  383. return bitmap;
  384. }
  385. static BOOL
  386. DeviceImage_ScaleBitmap(HBITMAP sourceBitmap, int width, int height, DeviceImageCache *cache,
  387. DeviceImageFlags flags, HBITMAP *scalledBitmap)
  388. {
  389. HBITMAP resultBitmap;
  390. BITMAP sourceInfo;
  391. IWICImagingFactory *wicFactory;
  392. unsigned int requestedWidth, requestedHeight;
  393. double scale, scaleH;
  394. int t;
  395. if (NULL == sourceBitmap || NULL == scalledBitmap)
  396. return FALSE;
  397. if (sizeof(sourceInfo) != GetObject(sourceBitmap, sizeof(sourceInfo), &sourceInfo))
  398. return FALSE;
  399. if (sourceInfo.bmHeight < 0)
  400. sourceInfo.bmHeight = -sourceInfo.bmHeight;
  401. scale = (double)width / sourceInfo.bmWidth;
  402. scaleH = (double)height / sourceInfo.bmHeight;
  403. if (scale > scaleH)
  404. scale = scaleH;
  405. if (1.0 == scale)
  406. {
  407. *scalledBitmap = NULL;
  408. return TRUE;
  409. }
  410. requestedWidth = width;
  411. requestedHeight = height;
  412. t = (int)((sourceInfo.bmWidth * scale) + 0.5);
  413. if (t < width)
  414. width = t;
  415. t = (int)((sourceInfo.bmHeight * scale) + 0.5);
  416. if (t < height)
  417. height = t;
  418. resultBitmap = NULL;
  419. wicFactory = DeviceImage_GetWicFactory(cache);
  420. if (NULL != wicFactory)
  421. {
  422. HRESULT hr;
  423. IWICBitmap *wicBitmap;
  424. hr = wicFactory->CreateBitmapFromHBITMAP(sourceBitmap, NULL,
  425. WICBitmapUsePremultipliedAlpha, &wicBitmap);
  426. if (SUCCEEDED(hr))
  427. {
  428. IWICBitmapScaler *wicScaler;
  429. hr = wicFactory->CreateBitmapScaler(&wicScaler);
  430. if (SUCCEEDED(hr))
  431. {
  432. hr = wicScaler->Initialize(wicBitmap, width, height, WICBitmapInterpolationModeFant);
  433. if (SUCCEEDED(hr))
  434. {
  435. resultBitmap = DeviceImage_HBitmapFromWicSource(wicScaler,
  436. requestedWidth, requestedHeight, flags);
  437. }
  438. wicScaler->Release();
  439. }
  440. wicBitmap->Release();
  441. }
  442. wicFactory->Release();
  443. }
  444. *scalledBitmap = resultBitmap;
  445. return (NULL != resultBitmap);
  446. }
  447. static HBITMAP
  448. DeviceImage_GetExactBitmap(DeviceImage *self, DeviceImageFlags flags)
  449. {
  450. if (NULL == self)
  451. return NULL;
  452. if (NULL == self->exactBitmap)
  453. {
  454. if (NULL != self->bitmap)
  455. {
  456. BITMAP bi;
  457. if (sizeof(bi) == GetObject(self->bitmap, sizeof(bi), &bi) &&
  458. bi.bmWidth == self->width && bi.bmHeight == self->height)
  459. {
  460. self->exactBitmap = self->bitmap;
  461. return self->exactBitmap;
  462. }
  463. }
  464. if (NULL != self->loader)
  465. {
  466. HBITMAP bitmap;
  467. bitmap = self->loader(self->source, self->width, self->height, self->loaderParam);
  468. if (NULL != bitmap)
  469. {
  470. if (FALSE != DeviceImage_ScaleBitmap(bitmap, self->width, self->height,
  471. self->cache, flags, &self->exactBitmap))
  472. {
  473. if (NULL == self->exactBitmap)
  474. {
  475. self->exactBitmap = bitmap;
  476. bitmap = NULL;
  477. }
  478. }
  479. if (NULL != bitmap)
  480. DeleteObject(bitmap);
  481. }
  482. }
  483. }
  484. return self->exactBitmap;
  485. }
  486. HBITMAP
  487. DeviceImage_GetBitmap(DeviceImage *self, DeviceImageFlags flags)
  488. {
  489. if (NULL == self)
  490. return NULL;
  491. if (0 != (DeviceImage_ExactSize & flags))
  492. return DeviceImage_GetExactBitmap(self, flags);
  493. if (NULL == self->bitmap)
  494. {
  495. if (NULL != self->loader)
  496. {
  497. HBITMAP bitmap;
  498. bitmap = self->loader(self->source, self->width, self->height, self->loaderParam);
  499. if (NULL != bitmap)
  500. {
  501. if (FALSE == DeviceImage_ScaleBitmap(bitmap, self->width, self->height,
  502. self->cache, flags, &self->bitmap))
  503. {
  504. self->bitmap = NULL;
  505. }
  506. if (NULL != self->bitmap)
  507. DeleteObject(bitmap);
  508. else
  509. self->bitmap = bitmap;
  510. }
  511. }
  512. }
  513. return self->bitmap;
  514. }
  515. static DeviceColoredImage *
  516. DeveiceColoredImage_Create(DeviceImage *base, COLORREF color1, COLORREF color2,
  517. DeviceImageFilter filter, void *user)
  518. {
  519. DeviceColoredImage *coloredImage;
  520. if (NULL == base)
  521. return NULL;
  522. coloredImage = (DeviceColoredImage*)malloc(sizeof(DeviceColoredImage));
  523. if (NULL == coloredImage)
  524. return NULL;
  525. ZeroMemory(coloredImage, sizeof(DeviceColoredImage));
  526. coloredImage->ref = 1;
  527. coloredImage->base = base;
  528. coloredImage->color1 = color1;
  529. coloredImage->color2 = color2;
  530. coloredImage->filter = filter;
  531. coloredImage->filterParam = user;
  532. DeviceImage_AddRef(base);
  533. return coloredImage;
  534. }
  535. static void
  536. DeviceColoredImage_Free(DeviceColoredImage *self)
  537. {
  538. if (NULL == self)
  539. return;
  540. if (NULL != self->bitmap)
  541. DeleteObject(self->bitmap);
  542. free(self);
  543. }
  544. static int
  545. DeviceColoredImage_SearchCb(const void *key, const void *element)
  546. {
  547. DeviceColoredImageSeachParam *search;
  548. DeviceColoredImage *image;
  549. int result;
  550. search = (DeviceColoredImageSeachParam*)key;
  551. image = (DeviceColoredImage*)element;
  552. result = search->color1 - image->color1;
  553. if (0 != result)
  554. return result;
  555. return search->color2- image->color2;
  556. }
  557. static int
  558. DeviceColoredImage_SortCb(const void *element1, const void *element2)
  559. {
  560. DeviceColoredImage *image1;
  561. DeviceColoredImage *image2;
  562. int result;
  563. image1 = (DeviceColoredImage*)element1;
  564. image2 = (DeviceColoredImage*)element2;
  565. result = image1->color1 - image2->color1;
  566. if (0 != result)
  567. return result;
  568. return image1->color2- image2->color2;
  569. }
  570. static int
  571. DeviceColoredImage_SortCb_V2(const void* element1, const void* element2)
  572. {
  573. return DeviceColoredImage_SortCb(element1, element2) < 0;
  574. }
  575. DeviceColoredImage *
  576. DeviceImage_GetColoredImage(DeviceImage *self, COLORREF color1, COLORREF color2,
  577. DeviceImageFilter filter, void *user)
  578. {
  579. size_t listSize;
  580. DeviceColoredImage *image;
  581. DeviceColoredImageSeachParam searchParam;
  582. searchParam.color1 = color1;
  583. searchParam.color2 = color2;
  584. listSize = self->list.size();
  585. if (listSize > 0)
  586. {
  587. DeviceColoredImage* image_ptr = NULL;
  588. //DeviceColoredImage **image_ptr = (DeviceColoredImage**)bsearch(&searchParam, &self->list[0], listSize,
  589. // sizeof(DeviceColoredImage**),
  590. // DeviceColoredImage_SearchCb);
  591. auto it = std::find_if(self->list.begin(), self->list.end(),
  592. [&](DeviceColoredImage* upT) -> bool
  593. {
  594. return DeviceColoredImage_SearchCb(&searchParam, upT) == 0;
  595. }
  596. );
  597. if (it != self->list.end())
  598. {
  599. image_ptr = *it;
  600. }
  601. if (NULL != image_ptr)
  602. {
  603. image = image_ptr;
  604. DeviceColoredImage_AddRef(image);
  605. return image;
  606. }
  607. }
  608. image = DeveiceColoredImage_Create(self, color1, color2, filter, user);
  609. if (NULL == image)
  610. return NULL;
  611. self->list.push_back(image);
  612. listSize++;
  613. if (listSize > 1)
  614. {
  615. //qsort(&self->list[0], self->list.size(), sizeof(DeviceColoredImage**),
  616. // DeviceColoredImage_SortCb);
  617. std::sort(self->list.begin(), self->list.end(), DeviceColoredImage_SortCb_V2);
  618. }
  619. return image;
  620. }
  621. static void
  622. DeviceImage_RemoveColored(DeviceImage *self, DeviceColoredImage *coloredImage)
  623. {
  624. size_t index;
  625. if (NULL == self || NULL == coloredImage)
  626. return;
  627. index = self->list.size();
  628. while(index--)
  629. {
  630. if (coloredImage == self->list[index])
  631. {
  632. self->list.erase(self->list.begin() + index);
  633. }
  634. }
  635. }
  636. size_t
  637. DeviceColoredImage_AddRef(DeviceColoredImage *self)
  638. {
  639. if (NULL == self)
  640. return 0;
  641. return InterlockedIncrement((LONG*)&self->ref);
  642. }
  643. size_t
  644. DeviceColoredImage_Release(DeviceColoredImage *self)
  645. {
  646. size_t r;
  647. if (NULL == self || 0 == self->ref)
  648. return 0;
  649. r = InterlockedDecrement((LONG*)&self->ref);
  650. if (0 == r)
  651. {
  652. if (NULL != self->base)
  653. {
  654. DeviceImage_RemoveColored(self->base, self);
  655. DeviceImage_Release(self->base);
  656. }
  657. DeviceColoredImage_Free(self);
  658. return 0;
  659. }
  660. return r;
  661. }
  662. static BOOL
  663. DeviceColoredImage_DefaultFilter(HBITMAP bitmap, COLORREF color1, COLORREF color2, void *user)
  664. {
  665. DIBSECTION bitmapDib;
  666. BITMAP *bitmapInfo;
  667. MLIMAGEFILTERAPPLYEX filter;
  668. if (sizeof(bitmapDib) != GetObjectW(bitmap, sizeof(bitmapDib), &bitmapDib))
  669. return FALSE;
  670. bitmapInfo = &bitmapDib.dsBm;
  671. filter.cbSize = sizeof(filter);
  672. filter.pData = (BYTE*)bitmapInfo->bmBits;
  673. filter.cx = bitmapInfo->bmWidth;
  674. filter.cy = bitmapInfo->bmHeight;
  675. filter.bpp = bitmapInfo->bmBitsPixel;
  676. filter.imageTag = NULL;
  677. filter.filterUID = MLIF_GRAYSCALE_UID;
  678. MLImageFilter_ApplyEx(Plugin_GetLibraryWindow(), &filter);
  679. filter.rgbBk = color1;
  680. filter.rgbFg = color2;
  681. if (32 == bitmapInfo->bmBitsPixel)
  682. {
  683. filter.filterUID = MLIF_FILTER1_PRESERVE_ALPHA_UID;
  684. MLImageFilter_ApplyEx(Plugin_GetLibraryWindow(), &filter);
  685. }
  686. else
  687. {
  688. filter.filterUID = MLIF_FILTER1_UID;
  689. MLImageFilter_ApplyEx(Plugin_GetLibraryWindow(), &filter);
  690. }
  691. return TRUE;
  692. }
  693. HBITMAP
  694. DeviceColoredImage_GetBitmap(DeviceColoredImage *self, DeviceImageFlags flags)
  695. {
  696. if (NULL == self)
  697. return NULL;
  698. if (NULL == self->bitmap)
  699. {
  700. HBITMAP bitmap;
  701. bitmap = DeviceImage_GetBitmap(self->base, flags);
  702. if (NULL != bitmap)
  703. {
  704. self->bitmap = Image_DuplicateDib(bitmap);
  705. if (NULL != self->bitmap)
  706. {
  707. DeviceImageFilter filter;
  708. if (NULL == self->filter)
  709. filter = DeviceColoredImage_DefaultFilter;
  710. else
  711. filter = self->filter;
  712. Image_Demultiply(self->bitmap, NULL);
  713. filter(self->bitmap, self->color1, self->color2, self->filterParam);
  714. Image_Premultiply(self->bitmap, NULL);
  715. }
  716. }
  717. }
  718. return self->bitmap;
  719. }
  720. DeviceImage*
  721. DeviceColoredImage_GetBaseImage(DeviceColoredImage *self)
  722. {
  723. if (NULL == self || NULL == self->base)
  724. return NULL;
  725. DeviceImage_AddRef(self->base);
  726. return self->base;
  727. }