1
0

bitmap.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874
  1. #include <tataki/api__tataki.h>
  2. //#define NO_SIMPLEFASTMODE
  3. #include <api/imgldr/api_imgldr.h>
  4. #include <tataki/blending/blending.h>
  5. #include "bitmap.h"
  6. #include <bfc/wasabi_std.h>
  7. #include <tataki/canvas/bltcanvas.h>
  8. #include <api/memmgr/api_memmgr.h>
  9. #if !defined(WIN32) && !defined(LINUX)
  10. #error port me!
  11. #endif
  12. #define ERRORBMP L"wasabi.bitmapnotfound"
  13. #define HARDERRORBMP L"xml/wasabi/window/error.png"
  14. // do not define NO_MMX in this file. :)
  15. #ifndef NO_MMX
  16. #ifdef WIN32
  17. #define MMX_CONST const
  18. #else
  19. #define MMX_CONST
  20. #endif
  21. static unsigned int MMX_CONST SkinBitmap_mmx_revn2[2]={0x01000100,0x01000100};
  22. static unsigned int MMX_CONST SkinBitmap_mmx_zero[2];
  23. static unsigned int MMX_CONST SkinBitmap_mmx_one[2]={1,0};
  24. #define HAS_MMX Blenders::MMX_AVAILABLE()
  25. #else
  26. //NO_MMX defined
  27. #define HAS_MMX 0
  28. #endif
  29. #if 0
  30. int bitmap_x = bitmap->getX();
  31. int bitmap_y = bitmap->getY();
  32. int bmpheight = bitmap->getHeight();
  33. int fullimage_w = bitmap->getFullWidth();
  34. void *bits = bitmap->getBits();
  35. int xp=xe-xs;
  36. for (int yp = ys; yp < ye; yp ++)
  37. {
  38. int t=yv>>16;
  39. if (t < 0) t=0;
  40. if (t >= bmpheight) t=bmpheight-1;
  41. int *psrc=((int*)bits) + (t+bitmap_y)*fullimage_w + bitmap_x;
  42. int *dest=((int*)dib) + pitch*yp + xs;
  43. C::stretch(xp, psrc, dest, xstart, dxv, alpha);
  44. yv+=dyv;
  45. }
  46. }
  47. #endif
  48. static __inline int shc(int offset, int offset16, int pos, int delta)
  49. {
  50. __int64 num = (Int32x32To64(pos, delta) + (__int64)offset16)/65536LL;
  51. return (int)(num+offset);
  52. }
  53. static __inline void *ROUNDUP16(void *ptr)
  54. {
  55. return (void *)(((size_t)ptr+15) & ~15);
  56. }
  57. static void Shrink(SkinBitmap *src_, int ys, int ye, int xe, int xs, int xstart, int yv, void *dst_, int dstpitch, int dxv, int dyv, int alpha)
  58. {
  59. // to scale an image
  60. // where the destination image is both smaller on X *and* Y.
  61. // inputs:
  62. int srcpitch = src_->getFullWidth();
  63. int bitmap_x = src_->getX();
  64. int bitmap_height = src_->getHeight();
  65. int bitmap_fullheight = src_->getFullHeight();
  66. int bitmap_y = src_->getY();
  67. uint8_t *src=(uint8_t *)src_->getBits();;
  68. ARGB32 *dst= (ARGB32 *)dst_;
  69. //#define NEW_X(x__) ((srcxs+bitmap_x)+MulDiv(x__, dxv, 65536))
  70. #define NEW_X(x__) shc(bitmap_x, xstart,(x__), dxv)
  71. #define NEW_Y(y__) shc(bitmap_y, yv, (y__)-ys, dyv)
  72. int dstw = xe-xs;
  73. // make sure we have enough room. I don't think anyone is going to have destination bitmaps this big, but might as well play it safe
  74. ASSERT(dstw <= 4096);
  75. if (dstw > 4096) return;
  76. // all this weird stuff is to align on 16 byte boundary
  77. char *_new_x[4096*sizeof(int)+15] = {0};
  78. char *new_x_aligned = (char *)_new_x;
  79. ROUNDUP16(new_x_aligned);
  80. int *new_x = (int *)new_x_aligned;
  81. for (int x=0; x<=dstw; x++)
  82. new_x[x] = NEW_X(x);
  83. // FOR EACH DEST PIXEL
  84. for (int y=ys; y<ye; y++)
  85. {
  86. int sy1 = NEW_Y(y);
  87. int sy2 = NEW_Y(y+1);
  88. for (int x=0; x<dstw; x++)
  89. {
  90. uint32_t sum[4] = { 0, 0, 0, 0 };
  91. int sx1 = new_x[x];
  92. int sx2 = new_x[x+1];
  93. // FOR EACH CONTRIBUTING SOURCE PIXEL: in a block [sx1..sx2) x [sy1..sy2)
  94. for (int b=sy1; b<sy2; b++)
  95. for (int a=sx1; a<sx2; a++)
  96. {
  97. uint8_t *ps = &src[(b*srcpitch + a)*4];
  98. sum[0] += (uint32_t)ps[0];
  99. sum[1] += (uint32_t)ps[1];
  100. sum[2] += (uint32_t)ps[2];
  101. sum[3] += (uint32_t)ps[3];
  102. }
  103. // write downsampled pixel:
  104. ARGB32 &dstpix = dst[(y*dstpitch + x+xs)];
  105. uint32_t out;
  106. uint8_t *pd = (uint8_t *)&out;
  107. int pixels_covered = (sy2-sy1)*(sx2-sx1);
  108. pd[0] = (uint32_t)(sum[0] / pixels_covered);
  109. pd[1] = (uint32_t)(sum[1] / pixels_covered);
  110. pd[2] = (uint32_t)(sum[2] / pixels_covered);
  111. pd[3] = (uint32_t)(sum[3] / pixels_covered);
  112. #ifdef NO_MMX
  113. dstpix = Blenders::BLEND_ADJ3(dstpix, out, alpha);
  114. #else
  115. dstpix = Blenders::BLEND_ADJ3_MMX(dstpix, out, alpha);
  116. #endif
  117. }
  118. }
  119. }
  120. static wchar_t skinBitmapBlankName[] = L"";
  121. SkinBitmap::SkinBitmap(HINSTANCE hInstance, int id, const wchar_t *forcegroup)
  122. {
  123. high_quality_resampling = false;
  124. references = 1;
  125. bitmapname = skinBitmapBlankName;
  126. subimage_w = -1;
  127. subimage_h = -1;
  128. x_offset = -1;
  129. y_offset = -1;
  130. fullimage_w = fullimage_h = 0;
  131. has_alpha = 0;
  132. ASSERT(hInstance != NULL);
  133. ownbits = 1;
  134. fromskin = 0;
  135. bits = WASABI_API_IMGLDR->imgldr_makeBmp(hInstance, id,&has_alpha,&fullimage_w,&fullimage_h, forcegroup);
  136. last_failed = 0;
  137. if (bits == NULL)
  138. {
  139. //last_failed = 1;
  140. //TODO: bits = WASABI_API_IMGLDR->imgldr_requestSkinBitmap(ERRORBMP, &has_alpha, &x_offset, &y_offset, &subimage_w, &subimage_h, &fullimage_w, &fullimage_h,_cached);
  141. }
  142. if (bits == NULL)
  143. {
  144. last_failed = 1;
  145. bits = WASABI_API_IMGLDR->imgldr_makeBmp(HARDERRORBMP, &has_alpha, &fullimage_w, &fullimage_h);
  146. }
  147. }
  148. SkinBitmap::SkinBitmap(ARGB32 *_bits, int w, int h, bool own)
  149. {
  150. high_quality_resampling = false;
  151. references = 1;
  152. subimage_w = -1;
  153. subimage_h = -1;
  154. x_offset = -1;
  155. y_offset = -1;
  156. bitmapname = skinBitmapBlankName;
  157. fullimage_w = w;
  158. fullimage_h = h;
  159. has_alpha = 1;
  160. if (own)
  161. ownbits = OWNBITS_USECFREE;
  162. else
  163. ownbits = OWNBITS_NOTOURS;
  164. bits = _bits;
  165. fromskin = 0;
  166. last_failed = 0;
  167. }
  168. // TODO: benski> could we be using GetDIBits here?
  169. void SkinBitmap::bmpToBits(HBITMAP hbmp, HDC defaultDC)
  170. {
  171. #ifdef WIN32
  172. if (hbmp && !bits)
  173. {
  174. BITMAPINFO srcbmi={0};
  175. HDC hMemDC, hMemDC2;
  176. HBITMAP hprev,hprev2=0;
  177. HBITMAP hsrcdib;
  178. void *srcdib;
  179. BITMAP bm;
  180. int r = GetObject(hbmp, sizeof(BITMAP), &bm);
  181. ASSERT(r != 0);
  182. fullimage_w=bm.bmWidth;
  183. fullimage_h=ABS(bm.bmHeight);
  184. int bmw=getWidth();
  185. int bmh=getHeight();
  186. int xo=getX();
  187. int yo=getY();
  188. srcbmi.bmiHeader.biSize=sizeof(srcbmi.bmiHeader);
  189. srcbmi.bmiHeader.biWidth=bmw;
  190. srcbmi.bmiHeader.biHeight=-bmh;
  191. srcbmi.bmiHeader.biPlanes=1;
  192. srcbmi.bmiHeader.biBitCount=32;
  193. srcbmi.bmiHeader.biCompression=BI_RGB;
  194. hMemDC = CreateCompatibleDC(NULL);
  195. hsrcdib=CreateDIBSection(hMemDC,&srcbmi,DIB_RGB_COLORS,&srcdib,NULL,0);
  196. ASSERTPR(hsrcdib != 0, "CreateDIBSection() failed #6");
  197. if (defaultDC)
  198. hMemDC2 = defaultDC;
  199. else
  200. {
  201. hMemDC2 = CreateCompatibleDC(NULL);
  202. hprev2 = (HBITMAP) SelectObject(hMemDC2, hbmp);
  203. }
  204. hprev = (HBITMAP) SelectObject(hMemDC, hsrcdib);
  205. BitBlt(hMemDC,0,0,bmw,bmh,hMemDC2,xo,yo,SRCCOPY);
  206. SelectObject(hMemDC, hprev);
  207. if (!defaultDC)
  208. {
  209. SelectObject(hMemDC2, hprev2);
  210. DeleteDC(hMemDC2);
  211. }
  212. DeleteDC(hMemDC);
  213. bits=(ARGB32*)MALLOC(bmw*bmh*4);
  214. if (getHeight()+getY() > bm.bmHeight || getWidth()+getX() > bm.bmWidth)
  215. {
  216. ASSERTALWAYS(StringPrintf("Subbitmap coordinates outside master bitmap [%d,%d,%d,%d in 0,0,%d,%d]", getX(), getY(), getWidth(), getHeight(), bm.bmWidth, bm.bmHeight));
  217. }
  218. MEMCPY32(bits,srcdib,bmw*bmh/**sizeof(ARGB32)*/);
  219. DeleteObject(hsrcdib);
  220. x_offset=-1;
  221. y_offset=-1;
  222. subimage_w=-1;
  223. subimage_h=-1;
  224. fullimage_w=bmw;
  225. fullimage_h=bmh;
  226. }
  227. #endif
  228. #ifdef LINUX
  229. if (! bits)
  230. {
  231. fullimage_w=hbmp.bmWidth;
  232. fullimage_h=ABS(hbmp.bmHeight);
  233. bits=(ARGB32*)MALLOC_(fullimage_w * fullimage_h * 4);
  234. MEMCPY32(bits, hbmp.shmseginfo->shmaddr, fullimage_w * fullimage_h);
  235. x_offset=-1;
  236. y_offset=-1;
  237. subimage_w=-1;
  238. subimage_h=-1;
  239. }
  240. #endif
  241. }
  242. SkinBitmap::SkinBitmap(const wchar_t *elementname, int _cached)
  243. {
  244. ASSERT(elementname!= NULL);
  245. high_quality_resampling=false;
  246. references=1;
  247. bitmapname = _wcsdup(elementname);
  248. x_offset = -1;
  249. y_offset = -1;
  250. subimage_w = -1;
  251. subimage_h = -1;
  252. fullimage_w = fullimage_h = 0;
  253. ownbits = OWNBITS_USEIMGLDR;
  254. bits = NULL;
  255. fromskin = 0;
  256. last_failed = 0;
  257. if (WASABI_API_IMGLDR) // someone might be using us in Classic Skin so explicitly check this
  258. {
  259. bits = WASABI_API_IMGLDR->imgldr_requestSkinBitmap(elementname, &has_alpha, &x_offset, &y_offset, &subimage_w, &subimage_h, &fullimage_w, &fullimage_h,_cached);
  260. fromskin = (bits != NULL);
  261. if (bits == NULL)
  262. bits = WASABI_API_IMGLDR->imgldr_makeBmp(elementname, &has_alpha, &fullimage_w, &fullimage_h);
  263. if (bits == NULL)
  264. {
  265. bits = WASABI_API_IMGLDR->imgldr_requestSkinBitmap(ERRORBMP, &has_alpha, &x_offset, &y_offset, &subimage_w, &subimage_h, &fullimage_w, &fullimage_h,_cached);
  266. last_failed = 1;
  267. }
  268. if (bits == NULL)
  269. {
  270. bits = WASABI_API_IMGLDR->imgldr_makeBmp(HARDERRORBMP, &has_alpha, &fullimage_w, &fullimage_h);
  271. last_failed = 1;
  272. }
  273. // check that coordinates are correct
  274. if (x_offset!=-1 && x_offset>fullimage_w) x_offset=fullimage_w-1;
  275. if (y_offset!=-1 && y_offset>fullimage_h) y_offset=fullimage_h-1;
  276. if (subimage_w!=-1 && (x_offset+subimage_w)>fullimage_w) subimage_w=fullimage_w-x_offset;
  277. if (subimage_h!=-1 && (y_offset+subimage_h)>fullimage_h) subimage_h=fullimage_h-y_offset;
  278. // ASSERTPR(bits != NULL, elementname);
  279. if (bits == NULL)
  280. {
  281. DebugStringW(L"element not found ! %s\n", elementname);
  282. int n = 10*10;
  283. bits = (ARGB32 *)WASABI_API_MEMMGR->sysMalloc(n * sizeof(ARGB32));
  284. fromskin = OWNBITS_USESYSFREE;
  285. ARGB32 *p = bits;
  286. while (n--)
  287. *p++ = 0xFFFF00FF;
  288. }
  289. }
  290. else
  291. last_failed = 1;
  292. }
  293. SkinBitmap::SkinBitmap(HBITMAP bitmap)
  294. {
  295. #ifdef WIN32
  296. ASSERT(bitmap != NULL);
  297. #endif
  298. high_quality_resampling = false;
  299. references = 1;
  300. subimage_w = -1;
  301. subimage_h = -1;
  302. x_offset = -1;
  303. y_offset = -1;
  304. bitmapname = skinBitmapBlankName;
  305. fullimage_w = fullimage_h = 0;
  306. has_alpha = 0;
  307. ownbits = OWNBITS_USESTDFREE;
  308. bits = NULL;
  309. fromskin = 0;
  310. last_failed = 0;
  311. bmpToBits(bitmap,NULL);
  312. }
  313. SkinBitmap::SkinBitmap(HBITMAP bitmap, HDC dc, int _has_alpha, void *_bits)
  314. {
  315. high_quality_resampling=false;
  316. references=1;
  317. subimage_w=-1;
  318. subimage_h=-1;
  319. x_offset=-1;
  320. y_offset=-1;
  321. fromskin = 0;
  322. last_failed = 0;
  323. bitmapname = skinBitmapBlankName;
  324. fullimage_w=fullimage_h=0;
  325. #ifdef WIN32
  326. ASSERT(bitmap != NULL);
  327. #endif
  328. has_alpha = _has_alpha;
  329. bits = (ARGB32*)_bits;
  330. if (!_bits)
  331. {
  332. ownbits = OWNBITS_USESTDFREE;
  333. bmpToBits(bitmap,dc);
  334. }
  335. else
  336. {
  337. #ifdef WIN32
  338. BITMAP bm;
  339. ownbits = OWNBITS_NOTOURS;
  340. int r = GetObject(bitmap, sizeof(BITMAP), &bm);
  341. ASSERT(r != 0);
  342. fullimage_w = bm.bmWidth;
  343. fullimage_h = ABS(bm.bmHeight);
  344. #endif
  345. #ifdef LINUX
  346. ownbits = OWNBITS_NOTOURS;
  347. fullimage_w = bitmap.bmWidth;
  348. fullimage_h = ABS(bitmap.bmHeight);
  349. #endif
  350. //port me
  351. }
  352. }
  353. SkinBitmap::SkinBitmap(ifc_canvas *canvas)
  354. {
  355. high_quality_resampling=false;
  356. references=1;
  357. subimage_w=-1;
  358. subimage_h=-1;
  359. x_offset=-1;
  360. y_offset=-1;
  361. bits = NULL;
  362. void *canvas_bits = canvas->getBits();
  363. if (canvas_bits)
  364. {
  365. canvas->getDim(&subimage_w, &fullimage_h, &fullimage_w);
  366. fullimage_w/=4;
  367. canvas->getOffsets(&x_offset, &y_offset);
  368. size_t copylen = fullimage_h*fullimage_w*sizeof(ARGB32);
  369. bits =(ARGB32 *)MALLOC(copylen);
  370. memcpy(bits, canvas_bits, copylen);
  371. last_failed = 0;
  372. ownbits = OWNBITS_USESTDFREE;
  373. }
  374. else
  375. {
  376. last_failed = 1;
  377. ownbits = OWNBITS_NOTOURS;
  378. }
  379. subimage_h=-1;
  380. bitmapname = skinBitmapBlankName;
  381. has_alpha = 1;
  382. fromskin = 0;
  383. }
  384. SkinBitmap::SkinBitmap(int w, int h, ARGB32 bgcolor)
  385. {
  386. high_quality_resampling=false;
  387. references=1;
  388. subimage_w=-1;
  389. subimage_h=-1;
  390. x_offset=-1;
  391. y_offset=-1;
  392. fullimage_w=w;
  393. bitmapname = skinBitmapBlankName;
  394. fullimage_h=h;
  395. fromskin = 0;
  396. last_failed = 0;
  397. int memsize = w*h*sizeof(ARGB32);
  398. if (memsize == 0) memsize++; // +1 so no failure when 0x0
  399. bits = (ARGB32*)MALLOC(memsize);
  400. DWORD *dw = (DWORD *)bits;
  401. MEMFILL<DWORD>(dw, bgcolor, w*h);
  402. has_alpha = TRUE;
  403. ownbits = OWNBITS_USESTDFREE; // 2 specifies should be FREE()'d
  404. }
  405. SkinBitmap::~SkinBitmap()
  406. {
  407. if (bits)
  408. {
  409. switch(ownbits)
  410. {
  411. case OWNBITS_USESTDFREE:
  412. FREE(bits);
  413. break;
  414. case OWNBITS_USECFREE:
  415. free(bits);
  416. break;
  417. case OWNBITS_USESYSFREE:
  418. WASABI_API_MEMMGR->sysFree(bits);
  419. break;
  420. case OWNBITS_USEIMGLDR:
  421. {
  422. if (WASABI_API_IMGLDR)
  423. {
  424. if (fromskin)
  425. WASABI_API_IMGLDR->imgldr_releaseSkinBitmap(bits);
  426. else
  427. #ifndef _WASABIRUNTIME
  428. WASABI_API_IMGLDR->imgldr_releaseBmp(bits);
  429. #else
  430. WASABI_API_IMGLDR->imgldr_releaseSkinBitmap(bits);
  431. #endif
  432. }
  433. }
  434. break;
  435. }
  436. }
  437. bits = NULL;
  438. if (bitmapname && bitmapname != skinBitmapBlankName)
  439. free(bitmapname);
  440. }
  441. void SkinBitmap::blit(ifc_canvas *canvas, int x, int y)
  442. {
  443. RECT src, dst;
  444. src.left=0;
  445. src.top=0;
  446. src.bottom=getHeight();
  447. src.right=getWidth();
  448. dst.left=x;
  449. dst.right=x+getWidth();
  450. dst.top=y;
  451. dst.bottom=y+getHeight();
  452. blitToRect(canvas,&src,&dst,255);
  453. }
  454. void SkinBitmap::blitRectToTile(ifc_canvas *canvas, RECT *dest, RECT *src, int xoffs, int yoffs, int alpha)
  455. {
  456. int startx,starty;
  457. int w,h;
  458. w = src->right-src->left;
  459. h = src->bottom-src->top;
  460. if (w <= 0 || h <= 0) return; //wtfmf
  461. RECT c;
  462. if (canvas->getClipBox(&c) == NULLREGION)
  463. {
  464. c = *dest;
  465. }
  466. else
  467. {
  468. if (dest->left > c.left) c.left = dest->left;
  469. if (dest->top > c.top) c.top = dest->top;
  470. if (dest->right < c.right) c.right = dest->right;
  471. if (dest->bottom < c.bottom) c.bottom = dest->bottom;
  472. }
  473. starty = c.top-((c.top - dest->top) % h)- yoffs;
  474. startx = c.left-((c.left - dest->left) % w) - xoffs;
  475. for (int j=starty;j<c.bottom;j+=h)
  476. for (int i=startx;i<c.right;i+=w)
  477. {
  478. int xp=i;
  479. int yp=j;
  480. int xo=0;
  481. int yo=0;
  482. int _w=getWidth();
  483. int _h=getHeight();
  484. if (xp < c.left)
  485. {
  486. xo=c.left-xp;
  487. _w+=xo;
  488. xp=c.left;
  489. }
  490. if (yp < c.top)
  491. {
  492. yo=c.top-yp;
  493. _h+=yo;
  494. yp=c.top;
  495. }
  496. if (xp + _w >= c.right) _w=c.right-xp;
  497. if (yp + _h >= c.bottom) _h=c.bottom-yp;
  498. RECT _s={xo, yo, xo+_w, yo+_h};
  499. RECT _d={xp, yp, xp+_w, yp+_h};
  500. blitToRect(canvas, &_s, &_d, alpha);
  501. }
  502. }
  503. void SkinBitmap::blitTile(ifc_canvas *canvas, RECT *dest, int xoffs, int yoffs, int alpha)
  504. {
  505. RECT r={0,0,getWidth(),getHeight()};
  506. blitRectToTile(canvas, dest, &r, xoffs, yoffs, alpha);
  507. }
  508. #ifdef WIN32
  509. #pragma warning(push)
  510. #pragma warning(disable : 4799)
  511. #endif
  512. #define DEFAULT_CACHE_WIDTH 64
  513. #define DEFAULT_CACHE_HEIGHT 64
  514. extern DWORD bitmap_cache_tls;
  515. void SkinBitmap::blitToRect(ifc_canvas *canvas, RECT *src, RECT *dst, int alpha) // only dst(top,left) are used
  516. {
  517. if (alpha <= 0) return;
  518. if (alpha > 255) alpha = 255;
  519. BltCanvas *blitToRectCanvas=0;
  520. HDC hdc = canvas->getHDC();
  521. if (hdc == NULL) return;
  522. void *dib=canvas->getBits();
  523. int cwidth,cheight, pitch;
  524. BaseCloneCanvas clone(canvas);
  525. bool usingBlitCanvas = false;
  526. RECT destrect=*dst;
  527. destrect.bottom=destrect.top+(src->bottom-src->top);
  528. destrect.right=destrect.left+(src->right-src->left);
  529. RECT c;
  530. int ctype=canvas->getClipBox(&c);
  531. if (c.top > destrect.top) destrect.top=c.top;
  532. if (c.left > destrect.left) destrect.left=c.left;
  533. if (c.bottom < destrect.bottom) destrect.bottom=c.bottom;
  534. if (c.right < destrect.right) destrect.right=c.right;
  535. #ifdef NO_SIMPLEFASTMODE
  536. dib=NULL;
  537. #endif
  538. if (destrect.right <= destrect.left || destrect.bottom <= destrect.top) return;
  539. int xs,yp,xe,ye;
  540. if (!dib || canvas->getDim(NULL,&cheight,&cwidth) || !cwidth || cheight < 1 || ctype == COMPLEXREGION)
  541. {
  542. cwidth=destrect.right-destrect.left;
  543. cheight=destrect.bottom-destrect.top;
  544. /* create cached canvas for this thread if it doesn't exist */
  545. blitToRectCanvas = (BltCanvas *)TlsGetValue(bitmap_cache_tls);
  546. if (!blitToRectCanvas)
  547. {
  548. blitToRectCanvas = new BltCanvas(cwidth, cheight);
  549. TlsSetValue(bitmap_cache_tls, (LPVOID)blitToRectCanvas);
  550. }
  551. int cacheWidth, cacheHeight;
  552. blitToRectCanvas->getDim(&cacheWidth, &cacheHeight, NULL);
  553. /* resize cache if necessary */
  554. if (cwidth > cacheWidth || cheight > cacheHeight)
  555. {
  556. cacheWidth=MAX(cacheWidth, cwidth);
  557. cacheHeight=MAX(cacheHeight, cheight);
  558. blitToRectCanvas->DestructiveResize(cacheWidth, cacheHeight);
  559. }
  560. dib = blitToRectCanvas->getBits();
  561. if (has_alpha || alpha < 255)
  562. clone.blit(destrect.left, destrect.top, blitToRectCanvas, 0, 0, cwidth, cheight);
  563. xs=0;
  564. yp=0;
  565. xe=cwidth;
  566. ye=cheight;
  567. pitch=cacheWidth;
  568. usingBlitCanvas = true;
  569. }
  570. else
  571. {
  572. xs=destrect.left;
  573. xe=destrect.right;
  574. yp=destrect.top;
  575. ye=destrect.bottom;
  576. cwidth/=4;
  577. pitch=cwidth;
  578. }
  579. int xpo=(dst->left-destrect.left+xs)-(getX()+src->left);
  580. int ypo=(dst->top-destrect.top+yp)-(getY()+src->top);
  581. if (yp < 0) yp=0;
  582. if (xs < 0) xs=0;
  583. if (yp<getY()+ypo) yp=ypo+getY();
  584. if (xs<getX()+xpo) xs=xpo+getX();
  585. if (xe > getWidth()+getX()+xpo) xe=getWidth()+getX()+xpo;
  586. if (ye > getHeight()+getY()+ypo) ye=getHeight()+getY()+ypo;
  587. // blend bitmap to dib
  588. if (xs<xe) for (; yp < ye; yp ++)
  589. {
  590. int xp=xe-xs;
  591. unsigned int *dest=((unsigned int*)dib) + pitch*yp + xs;
  592. unsigned int *src=((unsigned int*)bits) + (yp-ypo)*fullimage_w + (xs-xpo);
  593. if (!has_alpha && alpha==255) // simple copy
  594. {
  595. MEMCPY32(dest,src,xp);
  596. }
  597. else if (!has_alpha) // no alpha channel info, but just a simple blend
  598. {
  599. if (!HAS_MMX)
  600. while (xp--) *dest++ = Blenders::BLEND_ADJ1(*src++, *dest, alpha);
  601. #ifndef NO_MMX
  602. else
  603. {
  604. #ifdef WIN32
  605. if (xp>1) __asm
  606. {
  607. movd mm3, [alpha]
  608. mov ecx, xp
  609. movq mm4, [SkinBitmap_mmx_revn2]
  610. packuswb mm3, mm3 // 0000HHVV
  611. paddusw mm3, [SkinBitmap_mmx_one]
  612. mov edi, dest
  613. punpcklwd mm3, mm3 // HHVVHHVV
  614. mov esi, src
  615. punpckldq mm3, mm3 // HHVVHHVV HHVVHHVV
  616. shr ecx, 1
  617. psubw mm4, mm3
  618. align 16
  619. _blitAlpha_Loop1:
  620. movd mm0, [edi]
  621. movd mm1, [esi]
  622. punpcklbw mm0, [SkinBitmap_mmx_zero]
  623. movd mm7, [edi+4]
  624. punpcklbw mm1, [SkinBitmap_mmx_zero]
  625. pmullw mm0, mm4
  626. pmullw mm1, mm3
  627. movd mm6, [esi+4]
  628. punpcklbw mm7, [SkinBitmap_mmx_zero]
  629. punpcklbw mm6, [SkinBitmap_mmx_zero]
  630. pmullw mm7, mm4
  631. pmullw mm6, mm3
  632. paddw mm0, mm1
  633. psrlw mm0, 8
  634. packuswb mm0, mm0
  635. add esi, 8
  636. movd [edi], mm0
  637. paddw mm7, mm6
  638. psrlw mm7, 8
  639. packuswb mm7, mm7
  640. movd [edi+4], mm7
  641. add edi, 8
  642. dec ecx
  643. jnz _blitAlpha_Loop1
  644. mov src, esi
  645. mov dest, edi
  646. #else
  647. if (xp > 1)
  648. {
  649. __asm__ volatile(
  650. "movd %6, %%mm3\n"
  651. "mov %2, %%ecx\n"
  652. "movq (SkinBitmap_mmx_revn2), %%mm4\n"
  653. "packuswb %%mm3, %%mm3\n"
  654. "paddusw (SkinBitmap_mmx_one), %%mm3\n"
  655. "mov %0, %%edi\n"
  656. "punpcklwd %%mm3, %%mm3\n"
  657. "mov %1, %%esi\n"
  658. "punpckldq %%mm3, %%mm3\n"
  659. "shr $1, %%ecx\n"
  660. "psubw %%mm3, %%mm4\n"
  661. ".align 16\n"
  662. "_blitAlpha_Loop1:\n"
  663. "movd (%%edi), %%mm0\n"
  664. "movd (%%esi), %%mm1\n"
  665. "punpcklbw (SkinBitmap_mmx_zero), %%mm0\n"
  666. "movd 4(%%edi), %%mm7\n"
  667. "punpcklbw (SkinBitmap_mmx_zero), %%mm1\n"
  668. "pmullw %%mm3, %%mm0\n"
  669. "pmullw %%mm4, %%mm1\n"
  670. "movd 4(%%esi), %%mm6\n"
  671. "punpcklbw (SkinBitmap_mmx_zero), %%mm7\n"
  672. "punpcklbw (SkinBitmap_mmx_zero), %%mm6\n"
  673. "pmullw %%mm4, %%mm6\n"
  674. "pmullw %%mm3, %%mm7\n"
  675. "paddw %%mm1, %%mm0\n"
  676. "psrlw $8, %%mm0\n"
  677. "packuswb %%mm0, %%mm0\n"
  678. "add $8, %%esi\n"
  679. "movd %%mm0, (%%edi)\n"
  680. "paddw %%mm6, %%mm7\n"
  681. "psrlw $8, %%mm7\n"
  682. "packuswb %%mm7, %%mm7\n"
  683. "movd %%mm7, 4(%%edi)\n"
  684. "add $8, %%edi\n"
  685. "dec %%ecx\n"
  686. "jnz _blitAlpha_Loop1\n"
  687. "mov %%esi, %1\n"
  688. "mov %%edi, %0\n"
  689. : "=m"(dest), "=m"(src), "=m"(xp)
  690. : "0"(dest), "1"(src), "2"(xp), "m"(alpha)
  691. : "%eax", "%ecx", "%esi", "%edi");
  692. #endif
  693. }
  694. if (xp & 1) *dest++ = Blenders::BLEND_ADJ1_MMX(*src++, *dest, alpha);
  695. } // mmx available
  696. #endif // !NO_MMX
  697. }
  698. else if (alpha == 255) // no global alpha, just alpha channel
  699. {
  700. if (!HAS_MMX)
  701. while (xp--) *dest++ = Blenders::BLEND_ADJ2(*dest, *src++);
  702. #ifndef NO_MMX
  703. else
  704. {
  705. #ifdef WIN32
  706. if (xp > 1) __asm
  707. {
  708. mov ecx, xp
  709. shr ecx, 1
  710. mov edi, dest
  711. mov esi, src
  712. align 16
  713. _blitAlpha_Loop2:
  714. movd mm3, [esi]
  715. movd mm5, [esi+4]
  716. movq mm2, [SkinBitmap_mmx_revn2]
  717. psrld mm3, 24
  718. movq mm4, [SkinBitmap_mmx_revn2]
  719. psrld mm5, 24
  720. movd mm0, [edi]
  721. packuswb mm3, mm3 // 0000HHVV
  722. movd mm1, [esi]
  723. packuswb mm5, mm5 // 0000HHVV
  724. movd mm6, [esi+4]
  725. paddusw mm3, [SkinBitmap_mmx_one]
  726. punpcklwd mm3, mm3 // HHVVHHVV
  727. paddusw mm5, [SkinBitmap_mmx_one]
  728. movd mm7, [edi+4]
  729. punpcklwd mm5, mm5 // HHVVHHVV
  730. punpckldq mm3, mm3 // HHVVHHVV HHVVHHVV
  731. punpckldq mm5, mm5 // HHVVHHVV HHVVHHVV
  732. punpcklbw mm6, [SkinBitmap_mmx_zero]
  733. psubw mm4, mm5
  734. punpcklbw mm0, [SkinBitmap_mmx_zero]
  735. psubw mm2, mm3
  736. punpcklbw mm7, [SkinBitmap_mmx_zero]
  737. pmullw mm0, mm2
  738. pmullw mm7, mm4
  739. punpcklbw mm1, [SkinBitmap_mmx_zero]
  740. psubw mm2, mm3
  741. psrlw mm0, 8
  742. psrlw mm7, 8
  743. paddw mm0, mm1
  744. paddw mm7, mm6
  745. packuswb mm0, mm0
  746. movd [edi], mm0
  747. packuswb mm7, mm7
  748. movd [edi+4], mm7
  749. add esi, 8
  750. add edi, 8
  751. dec ecx
  752. jnz _blitAlpha_Loop2
  753. mov src, esi
  754. mov dest, edi
  755. #else
  756. if (xp > 1)
  757. {
  758. __asm__ volatile(
  759. "mov %4, %%ecx\n"
  760. "shr $1, %%ecx\n"
  761. "mov %0, %%edi\n"
  762. "mov %1, %%esi\n"
  763. ".align 16\n"
  764. "_blitAlpha_Loop2:\n"
  765. "movd (%%esi), %%mm3\n"
  766. "movd 4(%%esi), %%mm5\n"
  767. "movq (SkinBitmap_mmx_revn2), %%mm2\n"
  768. "psrld $24, %%mm3\n"
  769. "movq (SkinBitmap_mmx_revn2), %%mm4\n"
  770. "psrld $24, %%mm5\n"
  771. "movd (%%edi), %%mm0\n"
  772. "packuswb %%mm3, %%mm3\n"
  773. "movd (%%esi), %%mm1\n"
  774. "packuswb %%mm5, %%mm5\n"
  775. "movd 4(%%esi), %%mm6\n"
  776. "paddusw (SkinBitmap_mmx_one), %%mm3\n"
  777. "punpcklwd %%mm3, %%mm3\n"
  778. "paddusw (SkinBitmap_mmx_one), %%mm5\n"
  779. "movd 4(%%edi), %%mm7\n"
  780. "punpcklwd %%mm5, %%mm5\n"
  781. "punpckldq %%mm3, %%mm3\n"
  782. "punpckldq %%mm5, %%mm5\n"
  783. "punpcklbw (SkinBitmap_mmx_zero), %%mm6\n"
  784. "psubw %%mm5, %%mm4\n"
  785. "punpcklbw (SkinBitmap_mmx_zero), %%mm0\n"
  786. "psubw %%mm3, %%mm2\n"
  787. "punpcklbw (SkinBitmap_mmx_zero), %%mm7\n"
  788. "pmullw %%mm2, %%mm0\n"
  789. "pmullw %%mm4, %%mm7\n"
  790. "punpcklbw (SkinBitmap_mmx_zero), %%mm1\n"
  791. "psubw %%mm3, %%mm2\n"
  792. "psrlw $8, %%mm0\n"
  793. "psrlw $8, %%mm7\n"
  794. "paddw %%mm1, %%mm0\n"
  795. "paddw %%mm6, %%mm7\n"
  796. "packuswb %%mm0, %%mm0\n"
  797. "movd %%mm0, (%%edi)\n"
  798. "packuswb %%mm7, %%mm7\n"
  799. "movd %%mm7, 4(%%edi)\n"
  800. "add $8, %%esi\n"
  801. "add $8, %%edi\n"
  802. "dec %%ecx\n"
  803. "jnz _blitAlpha_Loop2\n"
  804. "mov %%esi, %1\n"
  805. "mov %%edi, %0\n"
  806. : "=m"(dest), "=m"(src)
  807. : "0"(dest), "1"(src), "m"(xp)
  808. : "%eax", "%ecx", "%esi", "%edi");
  809. #endif
  810. }
  811. if (xp&1) *dest++ = Blenders::BLEND_ADJ2_MMX(*dest, *src++);
  812. } // HAS_MMX
  813. #endif // ifndef NO_MMX
  814. }
  815. else // both
  816. {
  817. if (!HAS_MMX)
  818. while (xp--) *dest++ = Blenders::BLEND_ADJ3(*dest, *src++, alpha);
  819. #ifndef NO_MMX
  820. else
  821. {
  822. #ifdef WIN32
  823. if (xp > 1) __asm
  824. {
  825. movd mm5, [alpha]
  826. mov ecx, xp
  827. packuswb mm5, mm5
  828. shr ecx, 1
  829. paddusw mm5, [SkinBitmap_mmx_one]
  830. punpcklwd mm5, mm5
  831. mov edi, dest
  832. punpckldq mm5, mm5
  833. mov esi, src
  834. align 16
  835. _blitAlpha_Loop3:
  836. movd mm3, [esi] // VVVVVVVV
  837. movd mm4, [esi+4] // VVVVVVVV
  838. movd mm0, [edi]
  839. psrld mm3, 24
  840. movd mm1, [esi]
  841. psrld mm4, 24
  842. paddusw mm3, [SkinBitmap_mmx_one]
  843. paddusw mm4, [SkinBitmap_mmx_one]
  844. movd mm7, [edi+4]
  845. punpcklwd mm3, mm3
  846. movd mm6, [esi+4]
  847. punpcklwd mm4, mm4
  848. punpckldq mm3, mm3
  849. punpckldq mm4, mm4
  850. pmullw mm3, mm5
  851. pmullw mm4, mm5
  852. punpcklbw mm7, [SkinBitmap_mmx_zero]
  853. punpcklbw mm6, [SkinBitmap_mmx_zero]
  854. movq mm2, [SkinBitmap_mmx_revn2]
  855. psrlw mm3, 8
  856. psrlw mm4, 8
  857. punpcklbw mm0, [SkinBitmap_mmx_zero]
  858. punpcklbw mm1, [SkinBitmap_mmx_zero]
  859. psubw mm2, mm3
  860. pmullw mm0, mm2
  861. pmullw mm1, mm5
  862. add esi, 8
  863. movq mm2, [SkinBitmap_mmx_revn2]
  864. pmullw mm6, mm5
  865. paddusw mm0, mm1
  866. psubw mm2, mm4
  867. pmullw mm7, mm2
  868. psrlw mm0, 8
  869. packuswb mm0, mm0
  870. paddusw mm7, mm6
  871. movd [edi], mm0
  872. psrlw mm7, 8
  873. packuswb mm7, mm7
  874. movd [edi+4], mm7
  875. add edi, 8
  876. dec ecx
  877. jnz _blitAlpha_Loop3
  878. mov src, esi
  879. mov dest, edi
  880. #else
  881. if (xp > 1)
  882. {
  883. __asm__ volatile(
  884. "movd %5, %%mm5\n"
  885. "mov %4, %%ecx\n"
  886. "packuswb %%mm5, %%mm5 \n"
  887. "shr $1, %%ecx\n"
  888. "paddusw (SkinBitmap_mmx_one), %%mm5\n"
  889. "punpcklwd %%mm5, %%mm5\n"
  890. "mov %0, %%edi\n"
  891. "punpckldq %%mm5, %%mm5\n"
  892. "mov %1, %%esi\n"
  893. ".align 16\n"
  894. "_blitAlpha_Loop3:\n"
  895. "movd (%%esi), %%mm3\n"
  896. "movd 4(%%esi), %%mm4\n"
  897. "movd (%%edi), %%mm0\n"
  898. "psrld $24, %%mm3\n"
  899. "movd (%%esi), %%mm1\n"
  900. "psrld $24, %%mm4\n"
  901. "paddusw (SkinBitmap_mmx_one), %%mm3\n"
  902. "paddusw (SkinBitmap_mmx_one), %%mm4\n"
  903. "movd 4(%%edi), %%mm7\n"
  904. "punpcklwd %%mm3, %%mm3\n"
  905. "movd 4(%%esi), %%mm6\n"
  906. "punpcklwd %%mm4, %%mm4\n"
  907. "punpckldq %%mm3, %%mm3\n"
  908. "punpckldq %%mm4, %%mm4\n"
  909. "pmullw %%mm5, %%mm3\n"
  910. "pmullw %%mm5, %%mm4\n"
  911. "punpcklbw (SkinBitmap_mmx_zero), %%mm7\n"
  912. "punpcklbw (SkinBitmap_mmx_zero), %%mm6\n"
  913. "movq (SkinBitmap_mmx_revn2), %%mm2\n"
  914. "psrlw $8, %%mm3\n"
  915. "psrlw $8, %%mm4\n"
  916. "punpcklbw (SkinBitmap_mmx_zero), %%mm0\n"
  917. "punpcklbw (SkinBitmap_mmx_zero), %%mm1\n"
  918. "psubw %%mm3, %%mm2\n"
  919. "pmullw %%mm2, %%mm0\n"
  920. "pmullw %%mm5, %%mm1\n"
  921. "add $8, %%esi\n"
  922. "movq (SkinBitmap_mmx_revn2), %%mm2\n"
  923. "pmullw %%mm5, %%mm6\n"
  924. "paddusw %%mm1, %%mm0\n"
  925. "psubw %%mm4, %%mm2\n"
  926. "pmullw %%mm2, %%mm7\n"
  927. "psrlw $8, %%mm0\n"
  928. "packuswb %%mm0, %%mm0\n"
  929. "paddusw %%mm6, %%mm7\n"
  930. "movd %%mm0, (%%edi)\n"
  931. "psrlw $8, %%mm7\n"
  932. "packuswb %%mm7, %%mm7\n"
  933. "movd %%mm7, 4(%%edi)\n"
  934. "add $8, %%edi\n"
  935. "dec %%ecx\n"
  936. "jnz _blitAlpha_Loop3\n"
  937. "mov %%esi, %1\n"
  938. "mov %%edi, %0\n"
  939. : "=m"(dest), "=m"(src)
  940. : "0"(dest), "1"(src), "m"(xp), "m"(alpha)
  941. : "%eax", "%ecx", "%esi", "%edi");
  942. #endif
  943. }
  944. if (xp&1) *dest++ = Blenders::BLEND_ADJ3_MMX(*dest, *src++, alpha);
  945. } // HAS_MMX
  946. #endif // ifndef NO_MMX
  947. }
  948. }
  949. #ifndef NO_MMX
  950. Blenders::BLEND_MMX_END();
  951. #endif
  952. // write bits back to dib.
  953. if (usingBlitCanvas)
  954. {
  955. blitToRectCanvas->blit(0, 0, &clone, destrect.left, destrect.top, cwidth, cheight);
  956. }
  957. }
  958. #ifdef WIN32
  959. #pragma warning(pop)
  960. #endif
  961. void SkinBitmap::stretch(ifc_canvas *canvas, int x, int y, int w, int h)
  962. {
  963. RECT src, dst;
  964. src.left=0;
  965. src.top=0;
  966. src.right=getWidth();
  967. src.bottom=getHeight();
  968. dst.left=x;
  969. dst.right=x+w;
  970. dst.top=y;
  971. dst.bottom=y+h;
  972. stretchToRectAlpha(canvas,&src,&dst,255);
  973. }
  974. void SkinBitmap::stretchToRect(ifc_canvas *canvas, RECT *r)
  975. {
  976. stretch(canvas, r->left, r->top, r->right - r->left, r->bottom - r->top);
  977. }
  978. void SkinBitmap::stretchRectToRect(ifc_canvas *canvas, RECT *src, RECT *dst)
  979. {
  980. stretchToRectAlpha(canvas,src,dst,255);
  981. }
  982. void SkinBitmap::stretchToRectAlpha(ifc_canvas *canvas, RECT *r, int alpha)
  983. {
  984. RECT re;
  985. re.left=0; re.top=0;
  986. re.right=getWidth(); re.bottom=getHeight();
  987. stretchToRectAlpha(canvas,&re,r,alpha);
  988. }
  989. void SkinBitmap::blitAlpha(ifc_canvas *canvas, int x, int y, int alpha)
  990. {
  991. RECT dst,src;
  992. dst.left=x;
  993. dst.top=y;
  994. src.left=0;
  995. src.top=0;
  996. src.bottom=getHeight();
  997. src.right=getWidth();
  998. blitToRect(canvas,&src,&dst,alpha);
  999. }
  1000. #ifdef WIN32
  1001. #pragma warning(push)
  1002. #pragma warning(disable : 4799)
  1003. #endif
  1004. template <class C>
  1005. class Stretcher
  1006. {
  1007. public:
  1008. static void _stretchToRectAlpha(SkinBitmap *bitmap, int ys, int ye, int xe, int xs, int xstart, int yv, void *dib, int pitch, int dxv, int dyv, int alpha)
  1009. {
  1010. int bitmap_x = bitmap->getX();
  1011. int bitmap_y = bitmap->getY();
  1012. int bmpheight = bitmap->getHeight();
  1013. int fullimage_w = bitmap->getFullWidth();
  1014. void *bits = bitmap->getBits();
  1015. int xp=xe-xs;
  1016. for (int yp = ys; yp < ye; yp ++)
  1017. {
  1018. int t=yv>>16;
  1019. if (t < 0) t=0;
  1020. if (t >= bmpheight) t=bmpheight-1;
  1021. int *psrc=((int*)bits) + (t+bitmap_y)*fullimage_w + bitmap_x;
  1022. int *dest=((int*)dib) + pitch*yp + xs;
  1023. C::stretch(xp, psrc, dest, xstart, dxv, alpha);
  1024. yv+=dyv;
  1025. }
  1026. }
  1027. };
  1028. // no alpha, just stretch
  1029. class Stretch
  1030. {
  1031. public:
  1032. static void stretch(int xp, int *psrc, int *dest, int xv, int dxv, int alpha)
  1033. {
  1034. while (xp--) //JFtodo: assembly optimize - these first two modes aren't used that much anyway
  1035. {
  1036. *dest++ = psrc[xv>>16];
  1037. xv+=dxv;
  1038. }
  1039. }
  1040. };
  1041. // no alpha channel, just a global alpha val
  1042. class StretchGlobal
  1043. {
  1044. public:
  1045. static void stretch(int xp, int *psrc, int *dest, int xv, int dxv, int alpha)
  1046. {
  1047. while (xp--) //JFTODO: make MMX optimized version
  1048. {
  1049. *dest++ = Blenders::BLEND_ADJ1(psrc[xv>>16], *dest, alpha);
  1050. xv+=dxv;
  1051. }
  1052. }
  1053. };
  1054. // alpha channel, no global alpha val
  1055. class StretchChannel
  1056. {
  1057. public:
  1058. static void stretch(int xp, int *psrc, int *dest, int xv, int dxv, int alpha)
  1059. {
  1060. while (xp--)
  1061. {
  1062. *dest++ = Blenders::BLEND_ADJ2(*dest, psrc[xv>>16]);
  1063. xv+=dxv;
  1064. }
  1065. }
  1066. };
  1067. class StretchGlobalChannel
  1068. {
  1069. public:
  1070. static void stretch(int xp, int *psrc, int *dest, int xv, int dxv, int alpha)
  1071. {
  1072. while (xp--)
  1073. {
  1074. *dest++ = Blenders::BLEND_ADJ3(*dest, psrc[xv>>16], alpha);
  1075. xv+=dxv;
  1076. }
  1077. }
  1078. };
  1079. #ifndef NO_MMX
  1080. // no alpha channel, just a global alpha val
  1081. class StretchGlobalMMX
  1082. {
  1083. public:
  1084. static void stretch(int xp, int *psrc, int *dest, int xv, int dxv, int alpha)
  1085. {
  1086. while (xp--) //JFTODO: make MMX optimized version
  1087. {
  1088. *dest++ = Blenders::BLEND_ADJ1_MMX(psrc[xv>>16], *dest, alpha);
  1089. xv+=dxv;
  1090. }
  1091. }
  1092. };
  1093. // alpha channel, no global alpha val
  1094. class StretchChannelMMX
  1095. {
  1096. public:
  1097. static void stretch(int xp, int *psrc, int *dest, int xv, int dxv, int alpha)
  1098. {
  1099. #ifdef WIN32
  1100. if (xp>1) __asm
  1101. {
  1102. mov ecx, xp
  1103. mov edi, dest
  1104. shr ecx, 1
  1105. mov esi, psrc
  1106. mov edx, xv
  1107. mov ebx, dxv
  1108. align 16
  1109. _stretchAlpha_Loop2:
  1110. mov eax, edx
  1111. movd mm0, [edi]
  1112. movq mm4, [SkinBitmap_mmx_revn2]
  1113. shr eax, 16
  1114. movq mm2, [SkinBitmap_mmx_revn2]
  1115. punpcklbw mm0, [SkinBitmap_mmx_zero]
  1116. movd mm3, [esi+eax*4]
  1117. movd mm1, [esi+eax*4]
  1118. lea eax, [edx+ebx]
  1119. shr eax, 16
  1120. movd mm7, [edi+4]
  1121. psrld mm3, 24
  1122. packuswb mm3, mm3 // 0000HHVV
  1123. movd mm5, [esi+eax*4]
  1124. movd mm6, [esi+eax*4]
  1125. psrld mm5, 24
  1126. paddusw mm3, [SkinBitmap_mmx_one]
  1127. punpcklbw mm6, [SkinBitmap_mmx_zero]
  1128. packuswb mm5, mm5 // 0000HHVV
  1129. lea edx, [edx+ebx*2]
  1130. paddusw mm5, [SkinBitmap_mmx_one]
  1131. punpcklwd mm3, mm3 // HHVVHHVV
  1132. punpcklwd mm5, mm5 // HHVVHHVV
  1133. add edi, 8
  1134. punpckldq mm3, mm3 // HHVVHHVV HHVVHHVV
  1135. punpckldq mm5, mm5 // HHVVHHVV HHVVHHVV
  1136. psubw mm4, mm5
  1137. psubw mm2, mm3
  1138. punpcklbw mm7, [SkinBitmap_mmx_zero]
  1139. pmullw mm0, mm2
  1140. pmullw mm7, mm4
  1141. punpcklbw mm1, [SkinBitmap_mmx_zero]
  1142. psubw mm2, mm3
  1143. psrlw mm0, 8
  1144. psrlw mm7, 8
  1145. paddw mm0, mm1
  1146. paddw mm7, mm6
  1147. packuswb mm0, mm0
  1148. movd [edi-8], mm0
  1149. packuswb mm7, mm7
  1150. movd [edi-4], mm7
  1151. dec ecx
  1152. jnz _stretchAlpha_Loop2
  1153. mov dest, edi
  1154. mov xv, edx
  1155. }
  1156. #else
  1157. if (xp>1)
  1158. {
  1159. __asm__ volatile(
  1160. "mov %5, %%ecx\n"
  1161. "mov %0, %%edi\n"
  1162. "shr $1, %%ecx\n"
  1163. "mov %1, %%esi\n"
  1164. "mov %2, %%edx\n"
  1165. "mov %7, %%ebx\n"
  1166. ".align 16\n"
  1167. "_stretchAlpha_Loop2:\n"
  1168. "mov %%edx, %%eax\n"
  1169. "movd (%%edi), %%mm0\n"
  1170. "movq (SkinBitmap_mmx_revn2), %%mm4\n"
  1171. "shr $16, %%eax\n"
  1172. "movq (SkinBitmap_mmx_revn2), %%mm2\n"
  1173. "punpcklbw (SkinBitmap_mmx_zero), %%mm0\n"
  1174. "movd (%%esi,%%eax,4), %%mm3\n"
  1175. "movd (%%esi,%%eax,4), %%mm1\n"
  1176. "lea (%%edx,%%ebx), %%eax\n"
  1177. "shr $16, %%eax\n"
  1178. "movd 4(%%edi), %%mm7\n"
  1179. "psrld $24, %%mm3\n"
  1180. "packuswb %%mm3, %%mm3\n"
  1181. "movd (%%esi,%%eax,4), %%mm5\n"
  1182. "movd (%%esi,%%eax,4), %%mm6\n"
  1183. "psrld $24, %%mm5\n"
  1184. "paddusw (SkinBitmap_mmx_one), %%mm3\n"
  1185. "punpcklbw (SkinBitmap_mmx_zero), %%mm6\n"
  1186. "packuswb %%mm5, %%mm5\n"
  1187. "lea (%%edx,%%ebx,2), %%edx\n"
  1188. "paddusw (SkinBitmap_mmx_one), %%mm5\n"
  1189. "punpcklwd %%mm3, %%mm3\n"
  1190. "punpcklwd %%mm5, %%mm5\n"
  1191. "add $8, %%edi\n"
  1192. "punpckldq %%mm3, %%mm3\n"
  1193. "punpckldq %%mm5, %%mm5\n"
  1194. "psubw %%mm5, %%mm4\n"
  1195. "psubw %%mm3, %%mm2\n"
  1196. "punpcklbw (SkinBitmap_mmx_zero), %%mm7\n"
  1197. "pmullw %%mm2, %%mm0\n"
  1198. "pmullw %%mm4, %%mm7\n"
  1199. "punpcklbw (SkinBitmap_mmx_zero), %%mm1\n"
  1200. "psubw %%mm3, %%mm2\n"
  1201. "psrlw $8, %%mm0\n"
  1202. "psrlw $8, %%mm7\n"
  1203. "paddw %%mm1, %%mm0\n"
  1204. "paddw %%mm6, %%mm7\n"
  1205. "packuswb %%mm0, %%mm0\n"
  1206. "movd %%mm0, -8(%%edi)\n"
  1207. "packuswb %%mm7, %%mm7\n"
  1208. "movd %%mm7, -4(%%edi)\n"
  1209. "dec %%ecx\n"
  1210. "jnz _stretchAlpha_Loop2\n"
  1211. "mov %%edi, %0\n"
  1212. "mov %%edx, %2\n"
  1213. : "=m"(dest), "=m"(psrc), "=m"(xv)
  1214. : "0"(dest), "1"(psrc), "m"(xp),
  1215. "2"(xv), "m"(dxv), "m"(alpha)
  1216. : "%eax", "%ebx", "%ecx", "%edx",
  1217. "%esi", "%edi");
  1218. }
  1219. #endif
  1220. if (xp&1) *dest++ = Blenders::BLEND_ADJ2_MMX(*dest, psrc[xv>>16]);
  1221. }
  1222. };
  1223. class StretchGlobalChannelMMX
  1224. {
  1225. public:
  1226. static void stretch(int xp, int *psrc, int *dest, int xv, int dxv, int alpha)
  1227. {
  1228. #ifdef WIN32
  1229. if (xp>1) __asm
  1230. {
  1231. movd mm5, [alpha]
  1232. mov ecx, xp
  1233. packuswb mm5, mm5
  1234. shr ecx, 1
  1235. paddusw mm5, [SkinBitmap_mmx_one]
  1236. punpcklwd mm5, mm5
  1237. mov edi, dest
  1238. punpckldq mm5, mm5
  1239. mov esi, psrc
  1240. mov edx, xv
  1241. mov ebx, dxv
  1242. align 16
  1243. _stretchAlpha_Loop3:
  1244. movd mm0, [edi]
  1245. mov eax, edx
  1246. movd mm7, [edi+4]
  1247. shr eax, 16
  1248. movd mm1, [esi+eax*4]
  1249. movd mm3, [esi+eax*4] // VVVVVVVV
  1250. lea eax, [edx+ebx]
  1251. psrld mm3, 24
  1252. paddusw mm3, [SkinBitmap_mmx_one]
  1253. punpcklwd mm3, mm3
  1254. shr eax, 16
  1255. punpckldq mm3, mm3
  1256. pmullw mm3, mm5
  1257. movd mm4, [esi+eax*4] // VVVVVVVV
  1258. movd mm6, [esi+eax*4]
  1259. movq mm2, [SkinBitmap_mmx_revn2]
  1260. psrld mm4, 24
  1261. paddusw mm4, [SkinBitmap_mmx_one]
  1262. punpcklbw mm7, [SkinBitmap_mmx_zero]
  1263. punpcklwd mm4, mm4
  1264. lea edx, [edx+ebx*2]
  1265. punpckldq mm4, mm4
  1266. add edi, 8
  1267. punpcklbw mm6, [SkinBitmap_mmx_zero]
  1268. pmullw mm4, mm5
  1269. psrlw mm3, 8
  1270. punpcklbw mm0, [SkinBitmap_mmx_zero]
  1271. punpcklbw mm1, [SkinBitmap_mmx_zero]
  1272. psubw mm2, mm3
  1273. pmullw mm0, mm2
  1274. pmullw mm1, mm5
  1275. pmullw mm6, mm5
  1276. psrlw mm4, 8
  1277. movq mm2, [SkinBitmap_mmx_revn2]
  1278. paddusw mm0, mm1
  1279. psubw mm2, mm4
  1280. pmullw mm7, mm2
  1281. psrlw mm0, 8
  1282. packuswb mm0, mm0
  1283. paddusw mm7, mm6
  1284. movd [edi-8], mm0
  1285. psrlw mm7, 8
  1286. packuswb mm7, mm7
  1287. movd [edi-4], mm7
  1288. dec ecx
  1289. jnz _stretchAlpha_Loop3
  1290. mov xv, edx
  1291. mov dest, edi
  1292. }
  1293. #else
  1294. if (xp>1)
  1295. {
  1296. __asm__ volatile(
  1297. "movd %8, %%mm5\n"
  1298. "mov %5, %%ecx\n"
  1299. "packuswb %%mm5, %%mm5 \n"
  1300. "shr $1, %%ecx\n"
  1301. "paddusw (SkinBitmap_mmx_one), %%mm5\n"
  1302. "punpcklwd %%mm5, %%mm5\n"
  1303. "mov %0, %%edi\n"
  1304. "punpckldq %%mm5, %%mm5\n"
  1305. "mov %1, %%esi\n"
  1306. "mov %6, %%edx\n"
  1307. "mov %7, %%ebx\n"
  1308. ".align 16\n"
  1309. "_stretchAlpha_Loop3:\n"
  1310. "movd (%%edi), %%mm0\n"
  1311. "mov %%edx, %%eax\n"
  1312. "movd 4(%%edi), %%mm7\n"
  1313. "shr $16, %%eax\n"
  1314. "movd (%%esi,%%eax,4), %%mm1\n"
  1315. "movd (%%esi,%%eax,4), %%mm3\n"
  1316. "lea (%%edx,%%ebx), %%eax\n"
  1317. "psrld $24, %%mm3\n"
  1318. "paddusw (SkinBitmap_mmx_one), %%mm3\n"
  1319. "punpcklwd %%mm3, %%mm3\n"
  1320. "shr $16, %%eax\n"
  1321. "punpckldq %%mm3, %%mm3\n"
  1322. "pmullw %%mm5, %%mm3\n"
  1323. "movd (%%esi,%%eax,4), %%mm4\n"
  1324. "movd (%%esi,%%eax,4), %%mm6\n"
  1325. "movq (SkinBitmap_mmx_revn2), %%mm2\n"
  1326. "psrld $24, %%mm4\n"
  1327. "paddusw (SkinBitmap_mmx_one), %%mm4\n"
  1328. "punpcklbw (SkinBitmap_mmx_zero), %%mm7\n"
  1329. "punpcklwd %%mm4, %%mm4\n"
  1330. "lea (%%edx,%%ebx,2), %%edx\n"
  1331. "punpckldq %%mm4, %%mm4\n"
  1332. "add $8, %%edi\n"
  1333. "punpcklbw (SkinBitmap_mmx_zero), %%mm6\n"
  1334. "pmullw %%mm5, %%mm4\n"
  1335. "psrlw $8, %%mm3\n"
  1336. "punpcklbw (SkinBitmap_mmx_zero), %%mm0\n"
  1337. "punpcklbw (SkinBitmap_mmx_zero), %%mm1\n"
  1338. "psubw %%mm3, %%mm2\n"
  1339. "pmullw %%mm2, %%mm0\n"
  1340. "pmullw %%mm5, %%mm1\n"
  1341. "pmullw %%mm5, %%mm6\n"
  1342. "psrlw $8, %%mm4\n"
  1343. "movq (SkinBitmap_mmx_revn2), %%mm2\n"
  1344. "paddusw %%mm1, %%mm0\n"
  1345. "psubw %%mm4, %%mm2\n"
  1346. "pmullw %%mm2, %%mm7\n"
  1347. "psrlw $8, %%mm0\n"
  1348. "packuswb %%mm0, %%mm0\n"
  1349. "paddusw %%mm6, %%mm7\n"
  1350. "movd %%mm0, -8(%%edi)\n"
  1351. "psrlw $8, %%mm7\n"
  1352. "packuswb %%mm7, %%mm7\n"
  1353. "movd %%mm7, -4(%%edi)\n"
  1354. "dec %%ecx\n"
  1355. "jnz _stretchAlpha_Loop3\n"
  1356. "mov %%edi, %0\n"
  1357. "mov %%edx, %2\n"
  1358. : "=m"(dest), "=m"(psrc), "=m"(xv)
  1359. : "0"(dest), "1"(psrc), "m"(xp),
  1360. "m"(xv), "m"(dxv), "m"(alpha)
  1361. : "%eax", "%ebx", "%ecx", "%edx",
  1362. "%esi", "%edi");
  1363. }
  1364. #endif
  1365. if (xp&1) *dest++ = Blenders::BLEND_ADJ3_MMX(*dest, psrc[xv>>16], alpha);
  1366. }
  1367. };
  1368. #endif
  1369. class __Stretch : public Stretcher<Stretch> {};
  1370. class __StretchGlobal : public Stretcher<StretchGlobal> {};
  1371. class __StretchChannel : public Stretcher<StretchChannel> {};
  1372. class __StretchGlobalChannel : public Stretcher<StretchGlobalChannel> {};
  1373. #ifndef NO_MMX
  1374. class __StretchGlobalMMX : public Stretcher<StretchGlobalMMX> {};
  1375. class __StretchChannelMMX : public Stretcher<StretchChannelMMX> {};
  1376. class __StretchGlobalChannelMMX : public Stretcher<StretchGlobalChannelMMX> {};
  1377. #endif
  1378. #ifdef WIN32
  1379. #pragma warning(pop)
  1380. #endif
  1381. void SkinBitmap::stretchToRectAlpha(ifc_canvas *canvas, RECT *_src, RECT *_dst, int alpha)
  1382. {
  1383. if (alpha <= 0) return;
  1384. if (alpha > 255) alpha = 255;
  1385. RECT src=*_src;
  1386. RECT dst=*_dst;
  1387. if ((src.right-src.left) == (dst.right-dst.left) &&
  1388. (src.bottom-src.top) == (dst.bottom-dst.top))
  1389. {
  1390. blitToRect(canvas,_src,_dst,alpha);
  1391. return;
  1392. }
  1393. //FG> this is a hack, we should support subpixels instead
  1394. if (src.left == src.right)
  1395. {
  1396. if (src.right < getWidth())
  1397. src.right++;
  1398. else
  1399. src.left--;
  1400. }
  1401. if (src.top== src.bottom)
  1402. {
  1403. if (src.bottom < getHeight())
  1404. src.bottom++;
  1405. else
  1406. src.top--;
  1407. }
  1408. if (src.left >= src.right || src.top >= src.bottom) return;
  1409. if (dst.left >= dst.right || dst.top >= dst.bottom) return;
  1410. BltCanvas *blitToRectCanvas=0;
  1411. void *dib=canvas->getBits();
  1412. bool usingBlitCanvas = false;
  1413. BaseCloneCanvas clone(canvas);
  1414. int cwidth, cheight, pitch;
  1415. int dyv=((src.bottom-src.top)<<16)/(dst.bottom-dst.top);
  1416. int dxv=((src.right-src.left)<<16)/(dst.right-dst.left);
  1417. int yv=(src.top<<16);
  1418. int xstart=(src.left<<16);
  1419. RECT c;
  1420. int ctype=canvas->getClipBox(&c);
  1421. if (c.top > dst.top)
  1422. {
  1423. yv+=(c.top-dst.top)*dyv;
  1424. dst.top=c.top;
  1425. }
  1426. if (c.left > dst.left)
  1427. {
  1428. xstart+=(c.left-dst.left)*dxv;
  1429. dst.left=c.left;
  1430. }
  1431. if (c.bottom < dst.bottom)
  1432. dst.bottom=c.bottom;
  1433. if (c.right < dst.right)
  1434. dst.right=c.right;
  1435. if (dst.right <= dst.left || dst.bottom <= dst.top) return;
  1436. int xs,xe,ys,ye;
  1437. #ifdef NO_SIMPLEFASTMODE
  1438. dib=NULL;
  1439. #endif
  1440. if (!dib || canvas->getDim(NULL,&cheight,&cwidth) || !cwidth || cheight < 1 || ctype == COMPLEXREGION)
  1441. {
  1442. cwidth=dst.right-dst.left;
  1443. cheight=dst.bottom-dst.top;
  1444. /* create cached canvas for this thread if it doesn't exist */
  1445. blitToRectCanvas = (BltCanvas *)TlsGetValue(bitmap_cache_tls);
  1446. if (!blitToRectCanvas)
  1447. {
  1448. blitToRectCanvas = new BltCanvas(cwidth, cheight);
  1449. TlsSetValue(bitmap_cache_tls, (LPVOID)blitToRectCanvas);
  1450. }
  1451. int cacheWidth, cacheHeight;
  1452. blitToRectCanvas->getDim(&cacheWidth, &cacheHeight, NULL);
  1453. /* resize cache, if necessary */
  1454. if (cwidth > cacheWidth || cheight > cacheHeight)
  1455. {
  1456. cacheWidth=MAX(cacheWidth, cwidth);
  1457. cacheHeight=MAX(cacheHeight, cheight);
  1458. blitToRectCanvas->DestructiveResize(cacheWidth, cacheHeight);
  1459. }
  1460. dib = blitToRectCanvas->getBits();
  1461. if (has_alpha || alpha < 255)
  1462. clone.blit(dst.left, dst.top, blitToRectCanvas, 0, 0, cwidth, cheight);
  1463. xs=0;
  1464. ys=0;
  1465. xe=cwidth;
  1466. ye=cheight;
  1467. pitch=cacheWidth;
  1468. usingBlitCanvas=true;
  1469. }
  1470. else
  1471. {
  1472. xs=dst.left;
  1473. xe=dst.right;
  1474. ys=dst.top;
  1475. ye=dst.bottom;
  1476. cwidth/=4;
  1477. pitch=cwidth;
  1478. }
  1479. // stretch and blend bitmap to dib
  1480. if (xstart < 0) xstart=0;
  1481. // Martin> TODO
  1482. // we should separate the stretching from this function and move it to the Shrink() function (avoiding breaks from this fragile class)
  1483. // HQSkinBitmap will then override the Shrink funtion in order to apply it's own resizing stuff.
  1484. // and if we ever want a new algorithm we can just create a new UltraFastAndCoolSkinBitmap class overriding the function again
  1485. if (xs<xe)
  1486. {
  1487. if (!has_alpha) // doesn't have alpha channel
  1488. {
  1489. if (!high_quality_resampling || dxv<65536 || dyv<65536)
  1490. {
  1491. if (alpha == 255) // no global alpha
  1492. {
  1493. __Stretch::_stretchToRectAlpha(this, ys, ye, xe, xs, xstart, yv, dib, pitch, dxv, dyv, alpha);
  1494. }
  1495. else // has global alpha
  1496. {
  1497. #ifndef NO_MMX
  1498. if (HAS_MMX)
  1499. {
  1500. __StretchGlobalMMX::_stretchToRectAlpha(this, ys, ye, xe, xs, xstart, yv, dib, pitch, dxv, dyv, alpha);
  1501. }
  1502. else
  1503. #endif
  1504. {
  1505. __StretchGlobal::_stretchToRectAlpha(this, ys, ye, xe, xs, xstart, yv, dib, pitch, dxv, dyv, alpha);
  1506. }
  1507. }
  1508. }
  1509. else
  1510. {
  1511. Shrink(this, ys,ye,xe,xs, xstart,yv, (ARGB32 *)dib, pitch, dxv, dyv, alpha);
  1512. }
  1513. }
  1514. else // has alpha channel
  1515. {
  1516. // FUCKO: JF> BRENNAN FIX THESE BITCHES :)
  1517. if (alpha == 255) // no global alpha
  1518. {
  1519. if (!high_quality_resampling || dxv<65536 || dyv<65536)
  1520. {
  1521. #ifndef NO_MMX
  1522. if (HAS_MMX)
  1523. {
  1524. __StretchChannelMMX::_stretchToRectAlpha(this, ys, ye, xe, xs, xstart, yv, dib, pitch, dxv, dyv, alpha);
  1525. }
  1526. else
  1527. #endif
  1528. {
  1529. __StretchChannel::_stretchToRectAlpha(this, ys, ye, xe, xs, xstart, yv, dib, pitch, dxv, dyv, alpha);
  1530. }
  1531. }
  1532. else
  1533. {
  1534. Shrink(this, ys,ye,xe,xs, xstart,yv, (ARGB32 *)dib, pitch, dxv, dyv, alpha);
  1535. }
  1536. }
  1537. else // has global alpha
  1538. {
  1539. if (!high_quality_resampling || dxv<65536 || dyv<65536)
  1540. {
  1541. #ifndef NO_MMX
  1542. if (HAS_MMX)
  1543. {
  1544. __StretchGlobalChannelMMX::_stretchToRectAlpha(this, ys, ye, xe, xs, xstart, yv, dib, pitch, dxv, dyv, alpha);
  1545. }
  1546. else
  1547. #endif
  1548. {
  1549. __StretchGlobalChannel::_stretchToRectAlpha(this, ys, ye, xe, xs, xstart, yv, dib, pitch, dxv, dyv, alpha);
  1550. }
  1551. }
  1552. else
  1553. {
  1554. Shrink(this, ys, ye, xe, xs, xstart, yv, (ARGB32 *)dib, pitch, dxv, dyv,alpha);
  1555. }
  1556. }
  1557. }
  1558. }
  1559. #ifndef NO_MMX
  1560. Blenders::BLEND_MMX_END();
  1561. #endif
  1562. // write bits back to dib.
  1563. if (usingBlitCanvas)
  1564. {
  1565. blitToRectCanvas->blit(0, 0, &clone, dst.left, dst.top, cwidth, cheight);
  1566. }
  1567. }
  1568. COLORREF SkinBitmap::getPixel(int x, int y)
  1569. {
  1570. ASSERT(bits != NULL);
  1571. if (x < 0 || y < 0 || x >= getFullWidth()-getX() || y>= getFullHeight()-getY()) return (COLORREF)0;
  1572. return (COLORREF)(((int*)bits)[x+getX()+(y+getY())*getFullWidth()]);
  1573. }
  1574. void *SkinBitmap::getBits()
  1575. {
  1576. return bits;
  1577. }
  1578. int SkinBitmap::isInvalid()
  1579. {
  1580. return last_failed;
  1581. }
  1582. void SkinBitmap::setHasAlpha(int ha)
  1583. {
  1584. has_alpha=ha;
  1585. }
  1586. const wchar_t *SkinBitmap::getBitmapName()
  1587. {
  1588. return bitmapname;
  1589. }
  1590. void SkinBitmap::AddRef()
  1591. {
  1592. references++;
  1593. }
  1594. void SkinBitmap::Release()
  1595. {
  1596. if (--references == 0)
  1597. delete this;
  1598. }