draw.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <memdraw.h>
  5. #include <pool.h>
  6. extern Pool* imagmem;
  7. int drawdebug;
  8. static int tablesbuilt;
  9. /* perfect approximation to NTSC = .299r+.587g+.114b when 0 ≤ r,g,b < 256 */
  10. #define RGB2K(r,g,b) ((156763*(r)+307758*(g)+59769*(b))>>19)
  11. /*
  12. * for 0 ≤ x ≤ 255*255, (x*0x0101+0x100)>>16 is a perfect approximation.
  13. * for 0 ≤ x < (1<<16), x/255 = ((x+1)*0x0101)>>16 is a perfect approximation.
  14. * the last one is perfect for all up to 1<<16, avoids a multiply, but requires a rathole.
  15. */
  16. /* #define DIV255(x) (((x)*257+256)>>16) */
  17. #define DIV255(x) ((((x)+1)*257)>>16)
  18. /* #define DIV255(x) (tmp=(x)+1, (tmp+(tmp>>8))>>8) */
  19. #define MUL(x, y, t) (t = (x)*(y)+128, (t+(t>>8))>>8)
  20. #define MASK13 0xFF00FF00
  21. #define MASK02 0x00FF00FF
  22. #define MUL13(a, x, t) (t = (a)*(((x)&MASK13)>>8)+128, ((t+((t>>8)&MASK02))>>8)&MASK02)
  23. #define MUL02(a, x, t) (t = (a)*(((x)&MASK02)>>0)+128, ((t+((t>>8)&MASK02))>>8)&MASK02)
  24. #define MUL0123(a, x, s, t) ((MUL13(a, x, s)<<8)|MUL02(a, x, t))
  25. #define MUL2(u, v, x, y) (t = (u)*(v)+(x)*(y)+256, (t+(t>>8))>>8)
  26. static void mktables(void);
  27. typedef int Subdraw(Memdrawparam*);
  28. static Subdraw chardraw, alphadraw, memoptdraw;
  29. static Memimage* memones;
  30. static Memimage* memzeros;
  31. Memimage *memwhite;
  32. Memimage *memblack;
  33. Memimage *memtransparent;
  34. Memimage *memopaque;
  35. int _ifmt(Fmt*);
  36. void
  37. memimageinit(void)
  38. {
  39. static int didinit = 0;
  40. if(didinit)
  41. return;
  42. didinit = 1;
  43. if(strcmp(imagmem->name, "Image") == 0 || strcmp(imagmem->name, "image") == 0)
  44. imagmem->move = memimagemove;
  45. mktables();
  46. _memmkcmap();
  47. fmtinstall('R', Rfmt);
  48. fmtinstall('P', Pfmt);
  49. fmtinstall('b', _ifmt);
  50. memones = allocmemimage(Rect(0,0,1,1), GREY1);
  51. memones->flags |= Frepl;
  52. memones->clipr = Rect(-0x3FFFFFF, -0x3FFFFFF, 0x3FFFFFF, 0x3FFFFFF);
  53. *byteaddr(memones, ZP) = ~0;
  54. memzeros = allocmemimage(Rect(0,0,1,1), GREY1);
  55. memzeros->flags |= Frepl;
  56. memzeros->clipr = Rect(-0x3FFFFFF, -0x3FFFFFF, 0x3FFFFFF, 0x3FFFFFF);
  57. *byteaddr(memzeros, ZP) = 0;
  58. if(memones == nil || memzeros == nil)
  59. assert(0 /*cannot initialize memimage library */); /* RSC BUG */
  60. memwhite = memones;
  61. memblack = memzeros;
  62. memopaque = memones;
  63. memtransparent = memzeros;
  64. }
  65. static ulong imgtorgba(Memimage*, ulong);
  66. static ulong rgbatoimg(Memimage*, ulong);
  67. static ulong pixelbits(Memimage*, Point);
  68. #define DBG if(0)
  69. void
  70. memimagedraw(Memimage *dst, Rectangle r, Memimage *src, Point p0, Memimage *mask, Point p1, int op)
  71. {
  72. static int n = 0;
  73. Memdrawparam par;
  74. if(mask == nil)
  75. mask = memopaque;
  76. DBG print("memimagedraw %p/%luX %R @ %p %p/%luX %P %p/%luX %P... ", dst, dst->chan, r, dst->data->bdata, src, src->chan, p0, mask, mask->chan, p1);
  77. if(drawclip(dst, &r, src, &p0, mask, &p1, &par.sr, &par.mr) == 0){
  78. // if(drawdebug)
  79. // iprint("empty clipped rectangle\n");
  80. return;
  81. }
  82. if(op < Clear || op > SoverD){
  83. // if(drawdebug)
  84. // iprint("op out of range: %d\n", op);
  85. return;
  86. }
  87. par.op = op;
  88. par.dst = dst;
  89. par.r = r;
  90. par.src = src;
  91. /* par.sr set by drawclip */
  92. par.mask = mask;
  93. /* par.mr set by drawclip */
  94. par.state = 0;
  95. if(src->flags&Frepl){
  96. par.state |= Replsrc;
  97. if(Dx(src->r)==1 && Dy(src->r)==1){
  98. par.sval = pixelbits(src, src->r.min);
  99. par.state |= Simplesrc;
  100. par.srgba = imgtorgba(src, par.sval);
  101. par.sdval = rgbatoimg(dst, par.srgba);
  102. if((par.srgba&0xFF) == 0 && (op&DoutS)){
  103. // if (drawdebug) iprint("fill with transparent source\n");
  104. return; /* no-op successfully handled */
  105. }
  106. }
  107. }
  108. if(mask->flags & Frepl){
  109. par.state |= Replmask;
  110. if(Dx(mask->r)==1 && Dy(mask->r)==1){
  111. par.mval = pixelbits(mask, mask->r.min);
  112. if(par.mval == 0 && (op&DoutS)){
  113. // if(drawdebug) iprint("fill with zero mask\n");
  114. return; /* no-op successfully handled */
  115. }
  116. par.state |= Simplemask;
  117. if(par.mval == ~0)
  118. par.state |= Fullmask;
  119. par.mrgba = imgtorgba(mask, par.mval);
  120. }
  121. }
  122. // if(drawdebug)
  123. // iprint("dr %R sr %R mr %R...", r, par.sr, par.mr);
  124. DBG print("draw dr %R sr %R mr %R %lux\n", r, par.sr, par.mr, par.state);
  125. /*
  126. * Now that we've clipped the parameters down to be consistent, we
  127. * simply try sub-drawing routines in order until we find one that was able
  128. * to handle us. If the sub-drawing routine returns zero, it means it was
  129. * unable to satisfy the request, so we do not return.
  130. */
  131. /*
  132. * Hardware support. Each video driver provides this function,
  133. * which checks to see if there is anything it can help with.
  134. * There could be an if around this checking to see if dst is in video memory.
  135. */
  136. DBG print("test hwdraw\n");
  137. if(hwdraw(&par)){
  138. //if(drawdebug) iprint("hw handled\n");
  139. DBG print("hwdraw handled\n");
  140. return;
  141. }
  142. /*
  143. * Optimizations using memmove and memset.
  144. */
  145. DBG print("test memoptdraw\n");
  146. if(memoptdraw(&par)){
  147. //if(drawdebug) iprint("memopt handled\n");
  148. DBG print("memopt handled\n");
  149. return;
  150. }
  151. /*
  152. * Character drawing.
  153. * Solid source color being painted through a boolean mask onto a high res image.
  154. */
  155. DBG print("test chardraw\n");
  156. if(chardraw(&par)){
  157. //if(drawdebug) iprint("chardraw handled\n");
  158. DBG print("chardraw handled\n");
  159. return;
  160. }
  161. /*
  162. * General calculation-laden case that does alpha for each pixel.
  163. */
  164. DBG print("do alphadraw\n");
  165. alphadraw(&par);
  166. //if(drawdebug) iprint("alphadraw handled\n");
  167. DBG print("alphadraw handled\n");
  168. }
  169. #undef DBG
  170. /*
  171. * Clip the destination rectangle further based on the properties of the
  172. * source and mask rectangles. Once the destination rectangle is properly
  173. * clipped, adjust the source and mask rectangles to be the same size.
  174. * Then if source or mask is replicated, move its clipped rectangle
  175. * so that its minimum point falls within the repl rectangle.
  176. *
  177. * Return zero if the final rectangle is null.
  178. */
  179. int
  180. drawclip(Memimage *dst, Rectangle *r, Memimage *src, Point *p0, Memimage *mask, Point *p1, Rectangle *sr, Rectangle *mr)
  181. {
  182. Point rmin, delta;
  183. int splitcoords;
  184. Rectangle omr;
  185. if(r->min.x>=r->max.x || r->min.y>=r->max.y)
  186. return 0;
  187. splitcoords = (p0->x!=p1->x) || (p0->y!=p1->y);
  188. /* clip to destination */
  189. rmin = r->min;
  190. if(!rectclip(r, dst->r) || !rectclip(r, dst->clipr))
  191. return 0;
  192. /* move mask point */
  193. p1->x += r->min.x-rmin.x;
  194. p1->y += r->min.y-rmin.y;
  195. /* move source point */
  196. p0->x += r->min.x-rmin.x;
  197. p0->y += r->min.y-rmin.y;
  198. /* map destination rectangle into source */
  199. sr->min = *p0;
  200. sr->max.x = p0->x+Dx(*r);
  201. sr->max.y = p0->y+Dy(*r);
  202. /* sr is r in source coordinates; clip to source */
  203. if(!(src->flags&Frepl) && !rectclip(sr, src->r))
  204. return 0;
  205. if(!rectclip(sr, src->clipr))
  206. return 0;
  207. /* compute and clip rectangle in mask */
  208. if(splitcoords){
  209. /* move mask point with source */
  210. p1->x += sr->min.x-p0->x;
  211. p1->y += sr->min.y-p0->y;
  212. mr->min = *p1;
  213. mr->max.x = p1->x+Dx(*sr);
  214. mr->max.y = p1->y+Dy(*sr);
  215. omr = *mr;
  216. /* mr is now rectangle in mask; clip it */
  217. if(!(mask->flags&Frepl) && !rectclip(mr, mask->r))
  218. return 0;
  219. if(!rectclip(mr, mask->clipr))
  220. return 0;
  221. /* reflect any clips back to source */
  222. sr->min.x += mr->min.x-omr.min.x;
  223. sr->min.y += mr->min.y-omr.min.y;
  224. sr->max.x += mr->max.x-omr.max.x;
  225. sr->max.y += mr->max.y-omr.max.y;
  226. *p1 = mr->min;
  227. }else{
  228. if(!(mask->flags&Frepl) && !rectclip(sr, mask->r))
  229. return 0;
  230. if(!rectclip(sr, mask->clipr))
  231. return 0;
  232. *p1 = sr->min;
  233. }
  234. /* move source clipping back to destination */
  235. delta.x = r->min.x - p0->x;
  236. delta.y = r->min.y - p0->y;
  237. r->min.x = sr->min.x + delta.x;
  238. r->min.y = sr->min.y + delta.y;
  239. r->max.x = sr->max.x + delta.x;
  240. r->max.y = sr->max.y + delta.y;
  241. /* move source rectangle so sr->min is in src->r */
  242. if(src->flags&Frepl) {
  243. delta.x = drawreplxy(src->r.min.x, src->r.max.x, sr->min.x) - sr->min.x;
  244. delta.y = drawreplxy(src->r.min.y, src->r.max.y, sr->min.y) - sr->min.y;
  245. sr->min.x += delta.x;
  246. sr->min.y += delta.y;
  247. sr->max.x += delta.x;
  248. sr->max.y += delta.y;
  249. }
  250. *p0 = sr->min;
  251. /* move mask point so it is in mask->r */
  252. *p1 = drawrepl(mask->r, *p1);
  253. mr->min = *p1;
  254. mr->max.x = p1->x+Dx(*sr);
  255. mr->max.y = p1->y+Dy(*sr);
  256. assert(Dx(*sr) == Dx(*mr) && Dx(*mr) == Dx(*r));
  257. assert(Dy(*sr) == Dy(*mr) && Dy(*mr) == Dy(*r));
  258. assert(ptinrect(*p0, src->r));
  259. assert(ptinrect(*p1, mask->r));
  260. assert(ptinrect(r->min, dst->r));
  261. return 1;
  262. }
  263. /*
  264. * Conversion tables.
  265. */
  266. static uchar replbit[1+8][256]; /* replbit[x][y] is the replication of the x-bit quantity y to 8-bit depth */
  267. static uchar conv18[256][8]; /* conv18[x][y] is the yth pixel in the depth-1 pixel x */
  268. static uchar conv28[256][4]; /* ... */
  269. static uchar conv48[256][2];
  270. /*
  271. * bitmap of how to replicate n bits to fill 8, for 1 ≤ n ≤ 8.
  272. * the X's are where to put the bottom (ones) bit of the n-bit pattern.
  273. * only the top 8 bits of the result are actually used.
  274. * (the lower 8 bits are needed to get bits in the right place
  275. * when n is not a divisor of 8.)
  276. *
  277. * Should check to see if its easier to just refer to replmul than
  278. * use the precomputed values in replbit. On PCs it may well
  279. * be; on machines with slow multiply instructions it probably isn't.
  280. */
  281. #define a ((((((((((((((((0
  282. #define X *2+1)
  283. #define _ *2)
  284. static int replmul[1+8] = {
  285. 0,
  286. a X X X X X X X X X X X X X X X X,
  287. a _ X _ X _ X _ X _ X _ X _ X _ X,
  288. a _ _ X _ _ X _ _ X _ _ X _ _ X _,
  289. a _ _ _ X _ _ _ X _ _ _ X _ _ _ X,
  290. a _ _ _ _ X _ _ _ _ X _ _ _ _ X _,
  291. a _ _ _ _ _ X _ _ _ _ _ X _ _ _ _,
  292. a _ _ _ _ _ _ X _ _ _ _ _ _ X _ _,
  293. a _ _ _ _ _ _ _ X _ _ _ _ _ _ _ X,
  294. };
  295. #undef a
  296. #undef X
  297. #undef _
  298. static void
  299. mktables(void)
  300. {
  301. int i, j, mask, sh, small;
  302. if(tablesbuilt)
  303. return;
  304. fmtinstall('R', Rfmt);
  305. fmtinstall('P', Pfmt);
  306. tablesbuilt = 1;
  307. /* bit replication up to 8 bits */
  308. for(i=0; i<256; i++){
  309. for(j=0; j<=8; j++){ /* j <= 8 [sic] */
  310. small = i & ((1<<j)-1);
  311. replbit[j][i] = (small*replmul[j])>>8;
  312. }
  313. }
  314. /* bit unpacking up to 8 bits, only powers of 2 */
  315. for(i=0; i<256; i++){
  316. for(j=0, sh=7, mask=1; j<8; j++, sh--)
  317. conv18[i][j] = replbit[1][(i>>sh)&mask];
  318. for(j=0, sh=6, mask=3; j<4; j++, sh-=2)
  319. conv28[i][j] = replbit[2][(i>>sh)&mask];
  320. for(j=0, sh=4, mask=15; j<2; j++, sh-=4)
  321. conv48[i][j] = replbit[4][(i>>sh)&mask];
  322. }
  323. }
  324. static uchar ones = 0xff;
  325. /*
  326. * General alpha drawing case. Can handle anything.
  327. */
  328. typedef struct Buffer Buffer;
  329. struct Buffer {
  330. /* used by most routines */
  331. uchar *red;
  332. uchar *grn;
  333. uchar *blu;
  334. uchar *alpha;
  335. uchar *grey;
  336. ulong *rgba;
  337. int delta; /* number of bytes to add to pointer to get next pixel to the right */
  338. /* used by boolcalc* for mask data */
  339. uchar *m; /* ptr to mask data r.min byte; like p->bytermin */
  340. int mskip; /* no. of left bits to skip in *m */
  341. uchar *bm; /* ptr to mask data img->r.min byte; like p->bytey0s */
  342. int bmskip; /* no. of left bits to skip in *bm */
  343. uchar *em; /* ptr to mask data img->r.max.x byte; like p->bytey0e */
  344. int emskip; /* no. of right bits to skip in *em */
  345. };
  346. typedef struct Param Param;
  347. typedef Buffer Readfn(Param*, uchar*, int);
  348. typedef void Writefn(Param*, uchar*, Buffer);
  349. typedef Buffer Calcfn(Buffer, Buffer, Buffer, int, int, int);
  350. enum {
  351. MAXBCACHE = 16
  352. };
  353. /* giant rathole to customize functions with */
  354. struct Param {
  355. Readfn *replcall;
  356. Readfn *greymaskcall;
  357. Readfn *convreadcall;
  358. Writefn *convwritecall;
  359. Memimage *img;
  360. Rectangle r;
  361. int dx; /* of r */
  362. int needbuf;
  363. int convgrey;
  364. int alphaonly;
  365. uchar *bytey0s; /* byteaddr(Pt(img->r.min.x, img->r.min.y)) */
  366. uchar *bytermin; /* byteaddr(Pt(r.min.x, img->r.min.y)) */
  367. uchar *bytey0e; /* byteaddr(Pt(img->r.max.x, img->r.min.y)) */
  368. int bwidth;
  369. int replcache; /* if set, cache buffers */
  370. Buffer bcache[MAXBCACHE];
  371. ulong bfilled;
  372. uchar *bufbase;
  373. int bufoff;
  374. int bufdelta;
  375. int dir;
  376. int convbufoff;
  377. uchar *convbuf;
  378. Param *convdpar;
  379. int convdx;
  380. };
  381. static uchar *drawbuf;
  382. static int ndrawbuf;
  383. static int mdrawbuf;
  384. static Readfn greymaskread, replread, readptr;
  385. static Writefn nullwrite;
  386. static Calcfn alphacalc0, alphacalc14, alphacalc2810, alphacalc3679, alphacalc5, alphacalc11, alphacalcS;
  387. static Calcfn boolcalc14, boolcalc236789, boolcalc1011;
  388. static Readfn* readfn(Memimage*);
  389. static Readfn* readalphafn(Memimage*);
  390. static Writefn* writefn(Memimage*);
  391. static Calcfn* boolcopyfn(Memimage*, Memimage*);
  392. static Readfn* convfn(Memimage*, Param*, Memimage*, Param*, int*);
  393. static Readfn* ptrfn(Memimage*);
  394. static Calcfn *alphacalc[Ncomp] =
  395. {
  396. alphacalc0, /* Clear */
  397. alphacalc14, /* DoutS */
  398. alphacalc2810, /* SoutD */
  399. alphacalc3679, /* DxorS */
  400. alphacalc14, /* DinS */
  401. alphacalc5, /* D */
  402. alphacalc3679, /* DatopS */
  403. alphacalc3679, /* DoverS */
  404. alphacalc2810, /* SinD */
  405. alphacalc3679, /* SatopD */
  406. alphacalc2810, /* S */
  407. alphacalc11, /* SoverD */
  408. };
  409. static Calcfn *boolcalc[Ncomp] =
  410. {
  411. alphacalc0, /* Clear */
  412. boolcalc14, /* DoutS */
  413. boolcalc236789, /* SoutD */
  414. boolcalc236789, /* DxorS */
  415. boolcalc14, /* DinS */
  416. alphacalc5, /* D */
  417. boolcalc236789, /* DatopS */
  418. boolcalc236789, /* DoverS */
  419. boolcalc236789, /* SinD */
  420. boolcalc236789, /* SatopD */
  421. boolcalc1011, /* S */
  422. boolcalc1011, /* SoverD */
  423. };
  424. /*
  425. * Avoid standard Lock, QLock so that can be used in kernel.
  426. */
  427. typedef struct Dbuf Dbuf;
  428. struct Dbuf
  429. {
  430. uchar *p;
  431. int n;
  432. Param spar, mpar, dpar;
  433. int inuse;
  434. };
  435. static Dbuf dbuf[10];
  436. static Dbuf*
  437. allocdbuf(void)
  438. {
  439. int i;
  440. for(i=0; i<nelem(dbuf); i++){
  441. if(dbuf[i].inuse)
  442. continue;
  443. if(!_tas(&dbuf[i].inuse))
  444. return &dbuf[i];
  445. }
  446. return nil;
  447. }
  448. static void
  449. getparam(Param *p, Memimage *img, Rectangle r, int convgrey, int needbuf, int *ndrawbuf)
  450. {
  451. int nbuf;
  452. memset(p, 0, sizeof *p);
  453. p->img = img;
  454. p->r = r;
  455. p->dx = Dx(r);
  456. p->needbuf = needbuf;
  457. p->convgrey = convgrey;
  458. assert(img->r.min.x <= r.min.x && r.min.x < img->r.max.x);
  459. p->bytey0s = byteaddr(img, Pt(img->r.min.x, img->r.min.y));
  460. p->bytermin = byteaddr(img, Pt(r.min.x, img->r.min.y));
  461. p->bytey0e = byteaddr(img, Pt(img->r.max.x, img->r.min.y));
  462. p->bwidth = sizeof(ulong)*img->width;
  463. assert(p->bytey0s <= p->bytermin && p->bytermin <= p->bytey0e);
  464. if(p->r.min.x == p->img->r.min.x)
  465. assert(p->bytermin == p->bytey0s);
  466. nbuf = 1;
  467. if((img->flags&Frepl) && Dy(img->r) <= MAXBCACHE && Dy(img->r) < Dy(r)){
  468. p->replcache = 1;
  469. nbuf = Dy(img->r);
  470. }
  471. p->bufdelta = 4*p->dx;
  472. p->bufoff = *ndrawbuf;
  473. *ndrawbuf += p->bufdelta*nbuf;
  474. }
  475. static void
  476. clipy(Memimage *img, int *y)
  477. {
  478. int dy;
  479. dy = Dy(img->r);
  480. if(*y == dy)
  481. *y = 0;
  482. else if(*y == -1)
  483. *y = dy-1;
  484. assert(0 <= *y && *y < dy);
  485. }
  486. static void
  487. dumpbuf(char *s, Buffer b, int n)
  488. {
  489. int i;
  490. uchar *p;
  491. print("%s", s);
  492. for(i=0; i<n; i++){
  493. print(" ");
  494. if(p=b.grey){
  495. print(" k%.2uX", *p);
  496. b.grey += b.delta;
  497. }else{
  498. if(p=b.red){
  499. print(" r%.2uX", *p);
  500. b.red += b.delta;
  501. }
  502. if(p=b.grn){
  503. print(" g%.2uX", *p);
  504. b.grn += b.delta;
  505. }
  506. if(p=b.blu){
  507. print(" b%.2uX", *p);
  508. b.blu += b.delta;
  509. }
  510. }
  511. if((p=b.alpha) != &ones){
  512. print(" α%.2uX", *p);
  513. b.alpha += b.delta;
  514. }
  515. }
  516. print("\n");
  517. }
  518. /*
  519. * For each scan line, we expand the pixels from source, mask, and destination
  520. * into byte-aligned red, green, blue, alpha, and grey channels. If buffering is not
  521. * needed and the channels were already byte-aligned (grey8, rgb24, rgba32, rgb32),
  522. * the readers need not copy the data: they can simply return pointers to the data.
  523. * If the destination image is grey and the source is not, it is converted using the NTSC
  524. * formula.
  525. *
  526. * Once we have all the channels, we call either rgbcalc or greycalc, depending on
  527. * whether the destination image is color. This is allowed to overwrite the dst buffer (perhaps
  528. * the actual data, perhaps a copy) with its result. It should only overwrite the dst buffer
  529. * with the same format (i.e. red bytes with red bytes, etc.) A new buffer is returned from
  530. * the calculator, and that buffer is passed to a function to write it to the destination.
  531. * If the buffer is already pointing at the destination, the writing function is a no-op.
  532. */
  533. #define DBG if(0)
  534. static int
  535. alphadraw(Memdrawparam *par)
  536. {
  537. int isgrey, starty, endy, op;
  538. int needbuf, dsty, srcy, masky;
  539. int y, dir, dx, dy, ndrawbuf;
  540. uchar *drawbuf;
  541. Buffer bsrc, bdst, bmask;
  542. Readfn *rdsrc, *rdmask, *rddst;
  543. Calcfn *calc;
  544. Writefn *wrdst;
  545. Memimage *src, *mask, *dst;
  546. Rectangle r, sr, mr;
  547. Dbuf *z;
  548. r = par->r;
  549. dx = Dx(r);
  550. dy = Dy(r);
  551. z = allocdbuf();
  552. if(z == nil)
  553. return 0;
  554. src = par->src;
  555. mask = par->mask;
  556. dst = par->dst;
  557. sr = par->sr;
  558. mr = par->mr;
  559. op = par->op;
  560. isgrey = dst->flags&Fgrey;
  561. /*
  562. * Buffering when src and dst are the same bitmap is sufficient but not
  563. * necessary. There are stronger conditions we could use. We could
  564. * check to see if the rectangles intersect, and if simply moving in the
  565. * correct y direction can avoid the need to buffer.
  566. */
  567. needbuf = (src->data == dst->data);
  568. ndrawbuf = 0;
  569. getparam(&z->spar, src, sr, isgrey, needbuf, &ndrawbuf);
  570. getparam(&z->dpar, dst, r, isgrey, needbuf, &ndrawbuf);
  571. getparam(&z->mpar, mask, mr, 0, needbuf, &ndrawbuf);
  572. dir = (needbuf && byteaddr(dst, r.min) > byteaddr(src, sr.min)) ? -1 : 1;
  573. z->spar.dir = z->mpar.dir = z->dpar.dir = dir;
  574. /*
  575. * If the mask is purely boolean, we can convert from src to dst format
  576. * when we read src, and then just copy it to dst where the mask tells us to.
  577. * This requires a boolean (1-bit grey) mask and lack of a source alpha channel.
  578. *
  579. * The computation is accomplished by assigning the function pointers as follows:
  580. * rdsrc - read and convert source into dst format in a buffer
  581. * rdmask - convert mask to bytes, set pointer to it
  582. * rddst - fill with pointer to real dst data, but do no reads
  583. * calc - copy src onto dst when mask says to.
  584. * wrdst - do nothing
  585. * This is slightly sleazy, since things aren't doing exactly what their names say,
  586. * but it avoids a fair amount of code duplication to make this a case here
  587. * rather than have a separate booldraw.
  588. */
  589. //if(drawdebug) iprint("flag %lud mchan %lux=?%x dd %d\n", src->flags&Falpha, mask->chan, GREY1, dst->depth);
  590. if(!(src->flags&Falpha) && mask->chan == GREY1 && dst->depth >= 8 && op == SoverD){
  591. //if(drawdebug) iprint("boolcopy...");
  592. rdsrc = convfn(dst, &z->dpar, src, &z->spar, &ndrawbuf);
  593. rddst = readptr;
  594. rdmask = readfn(mask);
  595. calc = boolcopyfn(dst, mask);
  596. wrdst = nullwrite;
  597. }else{
  598. /* usual alphadraw parameter fetching */
  599. rdsrc = readfn(src);
  600. rddst = readfn(dst);
  601. wrdst = writefn(dst);
  602. calc = alphacalc[op];
  603. /*
  604. * If there is no alpha channel, we'll ask for a grey channel
  605. * and pretend it is the alpha.
  606. */
  607. if(mask->flags&Falpha){
  608. rdmask = readalphafn(mask);
  609. z->mpar.alphaonly = 1;
  610. }else{
  611. z->mpar.greymaskcall = readfn(mask);
  612. z->mpar.convgrey = 1;
  613. rdmask = greymaskread;
  614. /*
  615. * Should really be above, but then boolcopyfns would have
  616. * to deal with bit alignment, and I haven't written that.
  617. *
  618. * This is a common case for things like ellipse drawing.
  619. * When there's no alpha involved and the mask is boolean,
  620. * we can avoid all the division and multiplication.
  621. */
  622. if(mask->chan == GREY1 && !(src->flags&Falpha))
  623. calc = boolcalc[op];
  624. else if(op == SoverD && !(src->flags&Falpha))
  625. calc = alphacalcS;
  626. }
  627. }
  628. /*
  629. * If the image has a small enough repl rectangle,
  630. * we can just read each line once and cache them.
  631. */
  632. if(z->spar.replcache){
  633. z->spar.replcall = rdsrc;
  634. rdsrc = replread;
  635. }
  636. if(z->mpar.replcache){
  637. z->mpar.replcall = rdmask;
  638. rdmask = replread;
  639. }
  640. if(z->n < ndrawbuf){
  641. free(z->p);
  642. if((z->p = mallocz(ndrawbuf, 0)) == nil){
  643. z->inuse = 0;
  644. return 0;
  645. }
  646. z->n = ndrawbuf;
  647. }
  648. drawbuf = z->p;
  649. /*
  650. * Before we were saving only offsets from drawbuf in the parameter
  651. * structures; now that drawbuf has been grown to accomodate us,
  652. * we can fill in the pointers.
  653. */
  654. z->spar.bufbase = drawbuf+z->spar.bufoff;
  655. z->mpar.bufbase = drawbuf+z->mpar.bufoff;
  656. z->dpar.bufbase = drawbuf+z->dpar.bufoff;
  657. z->spar.convbuf = drawbuf+z->spar.convbufoff;
  658. if(dir == 1){
  659. starty = 0;
  660. endy = dy;
  661. }else{
  662. starty = dy-1;
  663. endy = -1;
  664. }
  665. /*
  666. * srcy, masky, and dsty are offsets from the top of their
  667. * respective Rectangles. they need to be contained within
  668. * the rectangles, so clipy can keep them there without division.
  669. */
  670. srcy = (starty + sr.min.y - src->r.min.y)%Dy(src->r);
  671. masky = (starty + mr.min.y - mask->r.min.y)%Dy(mask->r);
  672. dsty = starty + r.min.y - dst->r.min.y;
  673. assert(0 <= srcy && srcy < Dy(src->r));
  674. assert(0 <= masky && masky < Dy(mask->r));
  675. assert(0 <= dsty && dsty < Dy(dst->r));
  676. for(y=starty; y!=endy; y+=dir, srcy+=dir, masky+=dir, dsty+=dir){
  677. clipy(src, &srcy);
  678. clipy(dst, &dsty);
  679. clipy(mask, &masky);
  680. bsrc = rdsrc(&z->spar, z->spar.bufbase, srcy);
  681. DBG print("[");
  682. bmask = rdmask(&z->mpar, z->mpar.bufbase, masky);
  683. DBG print("]\n");
  684. bdst = rddst(&z->dpar, z->dpar.bufbase, dsty);
  685. DBG dumpbuf("src", bsrc, dx);
  686. DBG dumpbuf("mask", bmask, dx);
  687. DBG dumpbuf("dst", bdst, dx);
  688. bdst = calc(bdst, bsrc, bmask, dx, isgrey, op);
  689. wrdst(&z->dpar, z->dpar.bytermin+dsty*z->dpar.bwidth, bdst);
  690. }
  691. z->inuse = 0;
  692. return 1;
  693. }
  694. #undef DBG
  695. static Buffer
  696. alphacalc0(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
  697. {
  698. USED(grey);
  699. USED(op);
  700. USED(b1);
  701. USED(b2);
  702. memset(bdst.rgba, 0, dx*bdst.delta);
  703. return bdst;
  704. }
  705. static Buffer
  706. alphacalc14(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
  707. {
  708. Buffer obdst;
  709. int fd, sadelta;
  710. int i, sa, ma, q;
  711. ulong s, t;
  712. obdst = bdst;
  713. sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
  714. q = bsrc.delta == 4 && bdst.delta == 4;
  715. for(i=0; i<dx; i++){
  716. sa = *bsrc.alpha;
  717. ma = *bmask.alpha;
  718. fd = MUL(sa, ma, t);
  719. if(op == DoutS)
  720. fd = 255-fd;
  721. if(grey){
  722. *bdst.grey = MUL(fd, *bdst.grey, t);
  723. bsrc.grey += bsrc.delta;
  724. bdst.grey += bdst.delta;
  725. }else{
  726. if(q){
  727. *bdst.rgba = MUL0123(fd, *bdst.rgba, s, t);
  728. bsrc.rgba++;
  729. bdst.rgba++;
  730. bsrc.alpha += sadelta;
  731. bmask.alpha += bmask.delta;
  732. continue;
  733. }
  734. *bdst.red = MUL(fd, *bdst.red, t);
  735. *bdst.grn = MUL(fd, *bdst.grn, t);
  736. *bdst.blu = MUL(fd, *bdst.blu, t);
  737. bsrc.red += bsrc.delta;
  738. bsrc.blu += bsrc.delta;
  739. bsrc.grn += bsrc.delta;
  740. bdst.red += bdst.delta;
  741. bdst.blu += bdst.delta;
  742. bdst.grn += bdst.delta;
  743. }
  744. if(bdst.alpha != &ones){
  745. *bdst.alpha = MUL(fd, *bdst.alpha, t);
  746. bdst.alpha += bdst.delta;
  747. }
  748. bmask.alpha += bmask.delta;
  749. bsrc.alpha += sadelta;
  750. }
  751. return obdst;
  752. }
  753. static Buffer
  754. alphacalc2810(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
  755. {
  756. Buffer obdst;
  757. int fs, sadelta;
  758. int i, ma, da, q;
  759. ulong s, t;
  760. obdst = bdst;
  761. sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
  762. q = bsrc.delta == 4 && bdst.delta == 4;
  763. for(i=0; i<dx; i++){
  764. ma = *bmask.alpha;
  765. da = *bdst.alpha;
  766. if(op == SoutD)
  767. da = 255-da;
  768. fs = ma;
  769. if(op != S)
  770. fs = MUL(fs, da, t);
  771. if(grey){
  772. *bdst.grey = MUL(fs, *bsrc.grey, t);
  773. bsrc.grey += bsrc.delta;
  774. bdst.grey += bdst.delta;
  775. }else{
  776. if(q){
  777. *bdst.rgba = MUL0123(fs, *bsrc.rgba, s, t);
  778. bsrc.rgba++;
  779. bdst.rgba++;
  780. bmask.alpha += bmask.delta;
  781. bdst.alpha += bdst.delta;
  782. continue;
  783. }
  784. *bdst.red = MUL(fs, *bsrc.red, t);
  785. *bdst.grn = MUL(fs, *bsrc.grn, t);
  786. *bdst.blu = MUL(fs, *bsrc.blu, t);
  787. bsrc.red += bsrc.delta;
  788. bsrc.blu += bsrc.delta;
  789. bsrc.grn += bsrc.delta;
  790. bdst.red += bdst.delta;
  791. bdst.blu += bdst.delta;
  792. bdst.grn += bdst.delta;
  793. }
  794. if(bdst.alpha != &ones){
  795. *bdst.alpha = MUL(fs, *bsrc.alpha, t);
  796. bdst.alpha += bdst.delta;
  797. }
  798. bmask.alpha += bmask.delta;
  799. bsrc.alpha += sadelta;
  800. }
  801. return obdst;
  802. }
  803. static Buffer
  804. alphacalc3679(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
  805. {
  806. Buffer obdst;
  807. int fs, fd, sadelta;
  808. int i, sa, ma, da, q;
  809. ulong s, t, u, v;
  810. obdst = bdst;
  811. sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
  812. q = bsrc.delta == 4 && bdst.delta == 4;
  813. for(i=0; i<dx; i++){
  814. sa = *bsrc.alpha;
  815. ma = *bmask.alpha;
  816. da = *bdst.alpha;
  817. if(op == SatopD)
  818. fs = MUL(ma, da, t);
  819. else
  820. fs = MUL(ma, 255-da, t);
  821. if(op == DoverS)
  822. fd = 255;
  823. else{
  824. fd = MUL(sa, ma, t);
  825. if(op != DatopS)
  826. fd = 255-fd;
  827. }
  828. if(grey){
  829. *bdst.grey = MUL(fs, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
  830. bsrc.grey += bsrc.delta;
  831. bdst.grey += bdst.delta;
  832. }else{
  833. if(q){
  834. *bdst.rgba = MUL0123(fs, *bsrc.rgba, s, t)+MUL0123(fd, *bdst.rgba, u, v);
  835. bsrc.rgba++;
  836. bdst.rgba++;
  837. bsrc.alpha += sadelta;
  838. bmask.alpha += bmask.delta;
  839. bdst.alpha += bdst.delta;
  840. continue;
  841. }
  842. *bdst.red = MUL(fs, *bsrc.red, s)+MUL(fd, *bdst.red, t);
  843. *bdst.grn = MUL(fs, *bsrc.grn, s)+MUL(fd, *bdst.grn, t);
  844. *bdst.blu = MUL(fs, *bsrc.blu, s)+MUL(fd, *bdst.blu, t);
  845. bsrc.red += bsrc.delta;
  846. bsrc.blu += bsrc.delta;
  847. bsrc.grn += bsrc.delta;
  848. bdst.red += bdst.delta;
  849. bdst.blu += bdst.delta;
  850. bdst.grn += bdst.delta;
  851. }
  852. if(bdst.alpha != &ones){
  853. *bdst.alpha = MUL(fs, sa, s)+MUL(fd, da, t);
  854. bdst.alpha += bdst.delta;
  855. }
  856. bmask.alpha += bmask.delta;
  857. bsrc.alpha += sadelta;
  858. }
  859. return obdst;
  860. }
  861. static Buffer
  862. alphacalc5(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
  863. {
  864. USED(dx);
  865. USED(grey);
  866. USED(op);
  867. USED(b1);
  868. USED(b2);
  869. return bdst;
  870. }
  871. static Buffer
  872. alphacalc11(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
  873. {
  874. Buffer obdst;
  875. int fd, sadelta;
  876. int i, sa, ma, q;
  877. ulong s, t, u, v;
  878. USED(op);
  879. obdst = bdst;
  880. sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
  881. q = bsrc.delta == 4 && bdst.delta == 4;
  882. for(i=0; i<dx; i++){
  883. sa = *bsrc.alpha;
  884. ma = *bmask.alpha;
  885. fd = 255-MUL(sa, ma, t);
  886. if(grey){
  887. *bdst.grey = MUL(ma, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
  888. bsrc.grey += bsrc.delta;
  889. bdst.grey += bdst.delta;
  890. }else{
  891. if(q){
  892. *bdst.rgba = MUL0123(ma, *bsrc.rgba, s, t)+MUL0123(fd, *bdst.rgba, u, v);
  893. bsrc.rgba++;
  894. bdst.rgba++;
  895. bsrc.alpha += sadelta;
  896. bmask.alpha += bmask.delta;
  897. continue;
  898. }
  899. *bdst.red = MUL(ma, *bsrc.red, s)+MUL(fd, *bdst.red, t);
  900. *bdst.grn = MUL(ma, *bsrc.grn, s)+MUL(fd, *bdst.grn, t);
  901. *bdst.blu = MUL(ma, *bsrc.blu, s)+MUL(fd, *bdst.blu, t);
  902. bsrc.red += bsrc.delta;
  903. bsrc.blu += bsrc.delta;
  904. bsrc.grn += bsrc.delta;
  905. bdst.red += bdst.delta;
  906. bdst.blu += bdst.delta;
  907. bdst.grn += bdst.delta;
  908. }
  909. if(bdst.alpha != &ones){
  910. *bdst.alpha = MUL(ma, sa, s)+MUL(fd, *bdst.alpha, t);
  911. bdst.alpha += bdst.delta;
  912. }
  913. bmask.alpha += bmask.delta;
  914. bsrc.alpha += sadelta;
  915. }
  916. return obdst;
  917. }
  918. /*
  919. not used yet
  920. source and mask alpha 1
  921. static Buffer
  922. alphacalcS0(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
  923. {
  924. Buffer obdst;
  925. int i;
  926. USED(op);
  927. obdst = bdst;
  928. if(bsrc.delta == bdst.delta){
  929. memmove(bdst.rgba, bsrc.rgba, dx*bdst.delta);
  930. return obdst;
  931. }
  932. for(i=0; i<dx; i++){
  933. if(grey){
  934. *bdst.grey = *bsrc.grey;
  935. bsrc.grey += bsrc.delta;
  936. bdst.grey += bdst.delta;
  937. }else{
  938. *bdst.red = *bsrc.red;
  939. *bdst.grn = *bsrc.grn;
  940. *bdst.blu = *bsrc.blu;
  941. bsrc.red += bsrc.delta;
  942. bsrc.blu += bsrc.delta;
  943. bsrc.grn += bsrc.delta;
  944. bdst.red += bdst.delta;
  945. bdst.blu += bdst.delta;
  946. bdst.grn += bdst.delta;
  947. }
  948. if(bdst.alpha != &ones){
  949. *bdst.alpha = 255;
  950. bdst.alpha += bdst.delta;
  951. }
  952. }
  953. return obdst;
  954. }
  955. */
  956. /* source alpha 1 */
  957. static Buffer
  958. alphacalcS(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
  959. {
  960. Buffer obdst;
  961. int fd;
  962. int i, ma;
  963. ulong s, t;
  964. USED(op);
  965. obdst = bdst;
  966. for(i=0; i<dx; i++){
  967. ma = *bmask.alpha;
  968. fd = 255-ma;
  969. if(grey){
  970. *bdst.grey = MUL(ma, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
  971. bsrc.grey += bsrc.delta;
  972. bdst.grey += bdst.delta;
  973. }else{
  974. *bdst.red = MUL(ma, *bsrc.red, s)+MUL(fd, *bdst.red, t);
  975. *bdst.grn = MUL(ma, *bsrc.grn, s)+MUL(fd, *bdst.grn, t);
  976. *bdst.blu = MUL(ma, *bsrc.blu, s)+MUL(fd, *bdst.blu, t);
  977. bsrc.red += bsrc.delta;
  978. bsrc.blu += bsrc.delta;
  979. bsrc.grn += bsrc.delta;
  980. bdst.red += bdst.delta;
  981. bdst.blu += bdst.delta;
  982. bdst.grn += bdst.delta;
  983. }
  984. if(bdst.alpha != &ones){
  985. *bdst.alpha = ma+MUL(fd, *bdst.alpha, t);
  986. bdst.alpha += bdst.delta;
  987. }
  988. bmask.alpha += bmask.delta;
  989. }
  990. return obdst;
  991. }
  992. static Buffer
  993. boolcalc14(Buffer bdst, Buffer b1, Buffer bmask, int dx, int grey, int op)
  994. {
  995. Buffer obdst;
  996. int i, ma, zero;
  997. USED(b1);
  998. obdst = bdst;
  999. for(i=0; i<dx; i++){
  1000. ma = *bmask.alpha;
  1001. zero = ma ? op == DoutS : op == DinS;
  1002. if(grey){
  1003. if(zero)
  1004. *bdst.grey = 0;
  1005. bdst.grey += bdst.delta;
  1006. }else{
  1007. if(zero)
  1008. *bdst.red = *bdst.grn = *bdst.blu = 0;
  1009. bdst.red += bdst.delta;
  1010. bdst.blu += bdst.delta;
  1011. bdst.grn += bdst.delta;
  1012. }
  1013. bmask.alpha += bmask.delta;
  1014. if(bdst.alpha != &ones){
  1015. if(zero)
  1016. *bdst.alpha = 0;
  1017. bdst.alpha += bdst.delta;
  1018. }
  1019. }
  1020. return obdst;
  1021. }
  1022. static Buffer
  1023. boolcalc236789(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
  1024. {
  1025. Buffer obdst;
  1026. int fs, fd;
  1027. int i, ma, da, zero;
  1028. ulong s, t;
  1029. obdst = bdst;
  1030. zero = !(op&1);
  1031. for(i=0; i<dx; i++){
  1032. ma = *bmask.alpha;
  1033. da = *bdst.alpha;
  1034. fs = da;
  1035. if(op&2)
  1036. fs = 255-da;
  1037. fd = 0;
  1038. if(op&4)
  1039. fd = 255;
  1040. if(grey){
  1041. if(ma)
  1042. *bdst.grey = MUL(fs, *bsrc.grey, s)+MUL(fd, *bdst.grey, t);
  1043. else if(zero)
  1044. *bdst.grey = 0;
  1045. bsrc.grey += bsrc.delta;
  1046. bdst.grey += bdst.delta;
  1047. }else{
  1048. if(ma){
  1049. *bdst.red = MUL(fs, *bsrc.red, s)+MUL(fd, *bdst.red, t);
  1050. *bdst.grn = MUL(fs, *bsrc.grn, s)+MUL(fd, *bdst.grn, t);
  1051. *bdst.blu = MUL(fs, *bsrc.blu, s)+MUL(fd, *bdst.blu, t);
  1052. }
  1053. else if(zero)
  1054. *bdst.red = *bdst.grn = *bdst.blu = 0;
  1055. bsrc.red += bsrc.delta;
  1056. bsrc.blu += bsrc.delta;
  1057. bsrc.grn += bsrc.delta;
  1058. bdst.red += bdst.delta;
  1059. bdst.blu += bdst.delta;
  1060. bdst.grn += bdst.delta;
  1061. }
  1062. bmask.alpha += bmask.delta;
  1063. if(bdst.alpha != &ones){
  1064. if(ma)
  1065. *bdst.alpha = fs+MUL(fd, da, t);
  1066. else if(zero)
  1067. *bdst.alpha = 0;
  1068. bdst.alpha += bdst.delta;
  1069. }
  1070. }
  1071. return obdst;
  1072. }
  1073. static Buffer
  1074. boolcalc1011(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
  1075. {
  1076. Buffer obdst;
  1077. int i, ma, zero;
  1078. obdst = bdst;
  1079. zero = !(op&1);
  1080. for(i=0; i<dx; i++){
  1081. ma = *bmask.alpha;
  1082. if(grey){
  1083. if(ma)
  1084. *bdst.grey = *bsrc.grey;
  1085. else if(zero)
  1086. *bdst.grey = 0;
  1087. bsrc.grey += bsrc.delta;
  1088. bdst.grey += bdst.delta;
  1089. }else{
  1090. if(ma){
  1091. *bdst.red = *bsrc.red;
  1092. *bdst.grn = *bsrc.grn;
  1093. *bdst.blu = *bsrc.blu;
  1094. }
  1095. else if(zero)
  1096. *bdst.red = *bdst.grn = *bdst.blu = 0;
  1097. bsrc.red += bsrc.delta;
  1098. bsrc.blu += bsrc.delta;
  1099. bsrc.grn += bsrc.delta;
  1100. bdst.red += bdst.delta;
  1101. bdst.blu += bdst.delta;
  1102. bdst.grn += bdst.delta;
  1103. }
  1104. bmask.alpha += bmask.delta;
  1105. if(bdst.alpha != &ones){
  1106. if(ma)
  1107. *bdst.alpha = 255;
  1108. else if(zero)
  1109. *bdst.alpha = 0;
  1110. bdst.alpha += bdst.delta;
  1111. }
  1112. }
  1113. return obdst;
  1114. }
  1115. /*
  1116. * Replicated cached scan line read. Call the function listed in the Param,
  1117. * but cache the result so that for replicated images we only do the work once.
  1118. */
  1119. static Buffer
  1120. replread(Param *p, uchar *s, int y)
  1121. {
  1122. Buffer *b;
  1123. USED(s);
  1124. b = &p->bcache[y];
  1125. if((p->bfilled & (1<<y)) == 0){
  1126. p->bfilled |= 1<<y;
  1127. *b = p->replcall(p, p->bufbase+y*p->bufdelta, y);
  1128. }
  1129. return *b;
  1130. }
  1131. /*
  1132. * Alpha reading function that simply relabels the grey pointer.
  1133. */
  1134. static Buffer
  1135. greymaskread(Param *p, uchar *buf, int y)
  1136. {
  1137. Buffer b;
  1138. b = p->greymaskcall(p, buf, y);
  1139. b.alpha = b.grey;
  1140. return b;
  1141. }
  1142. #define DBG if(0)
  1143. static Buffer
  1144. readnbit(Param *p, uchar *buf, int y)
  1145. {
  1146. Buffer b;
  1147. Memimage *img;
  1148. uchar *repl, *r, *w, *ow, bits;
  1149. int i, n, sh, depth, x, dx, npack, nbits;
  1150. b.rgba = (ulong*)buf;
  1151. b.grey = w = buf;
  1152. b.red = b.blu = b.grn = w;
  1153. b.alpha = &ones;
  1154. b.delta = 1;
  1155. dx = p->dx;
  1156. img = p->img;
  1157. depth = img->depth;
  1158. repl = &replbit[depth][0];
  1159. npack = 8/depth;
  1160. sh = 8-depth;
  1161. /* copy from p->r.min.x until end of repl rectangle */
  1162. x = p->r.min.x;
  1163. n = dx;
  1164. if(n > p->img->r.max.x - x)
  1165. n = p->img->r.max.x - x;
  1166. r = p->bytermin + y*p->bwidth;
  1167. DBG print("readnbit dx %d %p=%p+%d*%d, *r=%d fetch %d ", dx, r, p->bytermin, y, p->bwidth, *r, n);
  1168. bits = *r++;
  1169. nbits = 8;
  1170. if(i=x&(npack-1)){
  1171. DBG print("throwaway %d...", i);
  1172. bits <<= depth*i;
  1173. nbits -= depth*i;
  1174. }
  1175. for(i=0; i<n; i++){
  1176. if(nbits == 0){
  1177. DBG print("(%.2ux)...", *r);
  1178. bits = *r++;
  1179. nbits = 8;
  1180. }
  1181. *w++ = repl[bits>>sh];
  1182. DBG print("bit %x...", repl[bits>>sh]);
  1183. bits <<= depth;
  1184. nbits -= depth;
  1185. }
  1186. dx -= n;
  1187. if(dx == 0)
  1188. return b;
  1189. assert(x+i == p->img->r.max.x);
  1190. /* copy from beginning of repl rectangle until where we were before. */
  1191. x = p->img->r.min.x;
  1192. n = dx;
  1193. if(n > p->r.min.x - x)
  1194. n = p->r.min.x - x;
  1195. r = p->bytey0s + y*p->bwidth;
  1196. DBG print("x=%d r=%p...", x, r);
  1197. bits = *r++;
  1198. nbits = 8;
  1199. if(i=x&(npack-1)){
  1200. bits <<= depth*i;
  1201. nbits -= depth*i;
  1202. }
  1203. DBG print("nbits=%d...", nbits);
  1204. for(i=0; i<n; i++){
  1205. if(nbits == 0){
  1206. bits = *r++;
  1207. nbits = 8;
  1208. }
  1209. *w++ = repl[bits>>sh];
  1210. DBG print("bit %x...", repl[bits>>sh]);
  1211. bits <<= depth;
  1212. nbits -= depth;
  1213. DBG print("bits %x nbits %d...", bits, nbits);
  1214. }
  1215. dx -= n;
  1216. if(dx == 0)
  1217. return b;
  1218. assert(dx > 0);
  1219. /* now we have exactly one full scan line: just replicate the buffer itself until we are done */
  1220. ow = buf;
  1221. while(dx--)
  1222. *w++ = *ow++;
  1223. return b;
  1224. }
  1225. #undef DBG
  1226. #define DBG if(0)
  1227. static void
  1228. writenbit(Param *p, uchar *w, Buffer src)
  1229. {
  1230. uchar *r;
  1231. ulong bits;
  1232. int i, sh, depth, npack, nbits, x, ex;
  1233. assert(src.grey != nil && src.delta == 1);
  1234. x = p->r.min.x;
  1235. ex = x+p->dx;
  1236. depth = p->img->depth;
  1237. npack = 8/depth;
  1238. i=x&(npack-1);
  1239. bits = i ? (*w >> (8-depth*i)) : 0;
  1240. nbits = depth*i;
  1241. sh = 8-depth;
  1242. r = src.grey;
  1243. for(; x<ex; x++){
  1244. bits <<= depth;
  1245. DBG print(" %x", *r);
  1246. bits |= (*r++ >> sh);
  1247. nbits += depth;
  1248. if(nbits == 8){
  1249. *w++ = bits;
  1250. nbits = 0;
  1251. }
  1252. }
  1253. if(nbits){
  1254. sh = 8-nbits;
  1255. bits <<= sh;
  1256. bits |= *w & ((1<<sh)-1);
  1257. *w = bits;
  1258. }
  1259. DBG print("\n");
  1260. return;
  1261. }
  1262. #undef DBG
  1263. static Buffer
  1264. readcmap(Param *p, uchar *buf, int y)
  1265. {
  1266. Buffer b;
  1267. int a, convgrey, copyalpha, dx, i, m;
  1268. uchar *q, *cmap, *begin, *end, *r, *w;
  1269. begin = p->bytey0s + y*p->bwidth;
  1270. r = p->bytermin + y*p->bwidth;
  1271. end = p->bytey0e + y*p->bwidth;
  1272. cmap = p->img->cmap->cmap2rgb;
  1273. convgrey = p->convgrey;
  1274. copyalpha = (p->img->flags&Falpha) ? 1 : 0;
  1275. w = buf;
  1276. dx = p->dx;
  1277. if(copyalpha){
  1278. b.alpha = buf++;
  1279. a = p->img->shift[CAlpha]/8;
  1280. m = p->img->shift[CMap]/8;
  1281. for(i=0; i<dx; i++){
  1282. *w++ = r[a];
  1283. q = cmap+r[m]*3;
  1284. r += 2;
  1285. if(r == end)
  1286. r = begin;
  1287. if(convgrey){
  1288. *w++ = RGB2K(q[0], q[1], q[2]);
  1289. }else{
  1290. *w++ = q[2]; /* blue */
  1291. *w++ = q[1]; /* green */
  1292. *w++ = q[0]; /* red */
  1293. }
  1294. }
  1295. }else{
  1296. b.alpha = &ones;
  1297. for(i=0; i<dx; i++){
  1298. q = cmap+*r++*3;
  1299. if(r == end)
  1300. r = begin;
  1301. if(convgrey){
  1302. *w++ = RGB2K(q[0], q[1], q[2]);
  1303. }else{
  1304. *w++ = q[2]; /* blue */
  1305. *w++ = q[1]; /* green */
  1306. *w++ = q[0]; /* red */
  1307. }
  1308. }
  1309. }
  1310. b.rgba = (ulong*)(buf-copyalpha);
  1311. if(convgrey){
  1312. b.grey = buf;
  1313. b.red = b.blu = b.grn = buf;
  1314. b.delta = 1+copyalpha;
  1315. }else{
  1316. b.blu = buf;
  1317. b.grn = buf+1;
  1318. b.red = buf+2;
  1319. b.grey = nil;
  1320. b.delta = 3+copyalpha;
  1321. }
  1322. return b;
  1323. }
  1324. static void
  1325. writecmap(Param *p, uchar *w, Buffer src)
  1326. {
  1327. uchar *cmap, *red, *grn, *blu;
  1328. int i, dx, delta;
  1329. cmap = p->img->cmap->rgb2cmap;
  1330. delta = src.delta;
  1331. red= src.red;
  1332. grn = src.grn;
  1333. blu = src.blu;
  1334. dx = p->dx;
  1335. for(i=0; i<dx; i++, red+=delta, grn+=delta, blu+=delta)
  1336. *w++ = cmap[(*red>>4)*256+(*grn>>4)*16+(*blu>>4)];
  1337. }
  1338. #define DBG if(0)
  1339. static Buffer
  1340. readbyte(Param *p, uchar *buf, int y)
  1341. {
  1342. Buffer b;
  1343. Memimage *img;
  1344. int dx, isgrey, convgrey, alphaonly, copyalpha, i, nb;
  1345. uchar *begin, *end, *r, *w, *rrepl, *grepl, *brepl, *arepl, *krepl;
  1346. uchar ured, ugrn, ublu;
  1347. ulong u;
  1348. img = p->img;
  1349. begin = p->bytey0s + y*p->bwidth;
  1350. r = p->bytermin + y*p->bwidth;
  1351. end = p->bytey0e + y*p->bwidth;
  1352. w = buf;
  1353. dx = p->dx;
  1354. nb = img->depth/8;
  1355. convgrey = p->convgrey; /* convert rgb to grey */
  1356. isgrey = img->flags&Fgrey;
  1357. alphaonly = p->alphaonly;
  1358. copyalpha = (img->flags&Falpha) ? 1 : 0;
  1359. DBG print("copyalpha %d alphaonly %d convgrey %d isgrey %d\n", copyalpha, alphaonly, convgrey, isgrey);
  1360. /* if we can, avoid processing everything */
  1361. if(!(img->flags&Frepl) && !convgrey && (img->flags&Fbytes)){
  1362. memset(&b, 0, sizeof b);
  1363. if(p->needbuf){
  1364. memmove(buf, r, dx*nb);
  1365. r = buf;
  1366. }
  1367. b.rgba = (ulong*)r;
  1368. if(copyalpha)
  1369. b.alpha = r+img->shift[CAlpha]/8;
  1370. else
  1371. b.alpha = &ones;
  1372. if(isgrey){
  1373. b.grey = r+img->shift[CGrey]/8;
  1374. b.red = b.grn = b.blu = b.grey;
  1375. }else{
  1376. b.red = r+img->shift[CRed]/8;
  1377. b.grn = r+img->shift[CGreen]/8;
  1378. b.blu = r+img->shift[CBlue]/8;
  1379. }
  1380. b.delta = nb;
  1381. return b;
  1382. }
  1383. DBG print("2\n");
  1384. rrepl = replbit[img->nbits[CRed]];
  1385. grepl = replbit[img->nbits[CGreen]];
  1386. brepl = replbit[img->nbits[CBlue]];
  1387. arepl = replbit[img->nbits[CAlpha]];
  1388. krepl = replbit[img->nbits[CGrey]];
  1389. for(i=0; i<dx; i++){
  1390. u = r[0] | (r[1]<<8) | (r[2]<<16) | (r[3]<<24);
  1391. if(copyalpha) {
  1392. *w++ = arepl[(u>>img->shift[CAlpha]) & img->mask[CAlpha]];
  1393. DBG print("a %x\n", w[-1]);
  1394. }
  1395. if(isgrey)
  1396. *w++ = krepl[(u >> img->shift[CGrey]) & img->mask[CGrey]];
  1397. else if(!alphaonly){
  1398. ured = rrepl[(u >> img->shift[CRed]) & img->mask[CRed]];
  1399. ugrn = grepl[(u >> img->shift[CGreen]) & img->mask[CGreen]];
  1400. ublu = brepl[(u >> img->shift[CBlue]) & img->mask[CBlue]];
  1401. if(convgrey){
  1402. DBG print("g %x %x %x\n", ured, ugrn, ublu);
  1403. *w++ = RGB2K(ured, ugrn, ublu);
  1404. DBG print("%x\n", w[-1]);
  1405. }else{
  1406. *w++ = brepl[(u >> img->shift[CBlue]) & img->mask[CBlue]];
  1407. *w++ = grepl[(u >> img->shift[CGreen]) & img->mask[CGreen]];
  1408. *w++ = rrepl[(u >> img->shift[CRed]) & img->mask[CRed]];
  1409. }
  1410. }
  1411. r += nb;
  1412. if(r == end)
  1413. r = begin;
  1414. }
  1415. b.alpha = copyalpha ? buf : &ones;
  1416. b.rgba = (ulong*)buf;
  1417. if(alphaonly){
  1418. b.red = b.grn = b.blu = b.grey = nil;
  1419. if(!copyalpha)
  1420. b.rgba = nil;
  1421. b.delta = 1;
  1422. }else if(isgrey || convgrey){
  1423. b.grey = buf+copyalpha;
  1424. b.red = b.grn = b.blu = buf+copyalpha;
  1425. b.delta = copyalpha+1;
  1426. DBG print("alpha %x grey %x\n", b.alpha ? *b.alpha : 0xFF, *b.grey);
  1427. }else{
  1428. b.blu = buf+copyalpha;
  1429. b.grn = buf+copyalpha+1;
  1430. b.grey = nil;
  1431. b.red = buf+copyalpha+2;
  1432. b.delta = copyalpha+3;
  1433. }
  1434. return b;
  1435. }
  1436. #undef DBG
  1437. #define DBG if(0)
  1438. static void
  1439. writebyte(Param *p, uchar *w, Buffer src)
  1440. {
  1441. Memimage *img;
  1442. int i, isalpha, isgrey, nb, delta, dx, adelta;
  1443. uchar ff, *red, *grn, *blu, *grey, *alpha;
  1444. ulong u, mask;
  1445. img = p->img;
  1446. red = src.red;
  1447. grn = src.grn;
  1448. blu = src.blu;
  1449. alpha = src.alpha;
  1450. delta = src.delta;
  1451. grey = src.grey;
  1452. dx = p->dx;
  1453. nb = img->depth/8;
  1454. mask = (nb==4) ? 0 : ~((1<<img->depth)-1);
  1455. isalpha = img->flags&Falpha;
  1456. isgrey = img->flags&Fgrey;
  1457. adelta = src.delta;
  1458. if(isalpha && (alpha == nil || alpha == &ones)){
  1459. ff = 0xFF;
  1460. alpha = &ff;
  1461. adelta = 0;
  1462. }
  1463. for(i=0; i<dx; i++){
  1464. u = w[0] | (w[1]<<8) | (w[2]<<16) | (w[3]<<24);
  1465. DBG print("u %.8lux...", u);
  1466. u &= mask;
  1467. DBG print("&mask %.8lux...", u);
  1468. if(isgrey){
  1469. u |= ((*grey >> (8-img->nbits[CGrey])) & img->mask[CGrey]) << img->shift[CGrey];
  1470. DBG print("|grey %.8lux...", u);
  1471. grey += delta;
  1472. }else{
  1473. u |= ((*red >> (8-img->nbits[CRed])) & img->mask[CRed]) << img->shift[CRed];
  1474. u |= ((*grn >> (8-img->nbits[CGreen])) & img->mask[CGreen]) << img->shift[CGreen];
  1475. u |= ((*blu >> (8-img->nbits[CBlue])) & img->mask[CBlue]) << img->shift[CBlue];
  1476. red += delta;
  1477. grn += delta;
  1478. blu += delta;
  1479. DBG print("|rgb %.8lux...", u);
  1480. }
  1481. if(isalpha){
  1482. u |= ((*alpha >> (8-img->nbits[CAlpha])) & img->mask[CAlpha]) << img->shift[CAlpha];
  1483. alpha += adelta;
  1484. DBG print("|alpha %.8lux...", u);
  1485. }
  1486. w[0] = u;
  1487. w[1] = u>>8;
  1488. w[2] = u>>16;
  1489. w[3] = u>>24;
  1490. w += nb;
  1491. }
  1492. }
  1493. #undef DBG
  1494. static Readfn*
  1495. readfn(Memimage *img)
  1496. {
  1497. if(img->depth < 8)
  1498. return readnbit;
  1499. if(img->nbits[CMap] == 8)
  1500. return readcmap;
  1501. return readbyte;
  1502. }
  1503. static Readfn*
  1504. readalphafn(Memimage *m)
  1505. {
  1506. USED(m);
  1507. return readbyte;
  1508. }
  1509. static Writefn*
  1510. writefn(Memimage *img)
  1511. {
  1512. if(img->depth < 8)
  1513. return writenbit;
  1514. if(img->chan == CMAP8)
  1515. return writecmap;
  1516. return writebyte;
  1517. }
  1518. static void
  1519. nullwrite(Param *p, uchar *s, Buffer b)
  1520. {
  1521. USED(p);
  1522. USED(s);
  1523. USED(b);
  1524. }
  1525. static Buffer
  1526. readptr(Param *p, uchar *s, int y)
  1527. {
  1528. Buffer b;
  1529. uchar *q;
  1530. USED(s);
  1531. q = p->bytermin + y*p->bwidth;
  1532. b.red = q; /* ptr to data */
  1533. b.grn = b.blu = b.grey = b.alpha = nil;
  1534. b.rgba = (ulong*)q;
  1535. b.delta = p->img->depth/8;
  1536. return b;
  1537. }
  1538. static Buffer
  1539. boolmemmove(Buffer bdst, Buffer bsrc, Buffer b1, int dx, int i, int o)
  1540. {
  1541. USED(i);
  1542. USED(o);
  1543. USED(b1);
  1544. USED(bsrc);
  1545. memmove(bdst.red, bsrc.red, dx*bdst.delta);
  1546. return bdst;
  1547. }
  1548. static Buffer
  1549. boolcopy8(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
  1550. {
  1551. uchar *m, *r, *w, *ew;
  1552. USED(i);
  1553. USED(o);
  1554. m = bmask.grey;
  1555. w = bdst.red;
  1556. r = bsrc.red;
  1557. ew = w+dx;
  1558. for(; w < ew; w++,r++)
  1559. if(*m++)
  1560. *w = *r;
  1561. return bdst; /* not used */
  1562. }
  1563. static Buffer
  1564. boolcopy16(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
  1565. {
  1566. uchar *m;
  1567. ushort *r, *w, *ew;
  1568. USED(i);
  1569. USED(o);
  1570. m = bmask.grey;
  1571. w = (ushort*)bdst.red;
  1572. r = (ushort*)bsrc.red;
  1573. ew = w+dx;
  1574. for(; w < ew; w++,r++)
  1575. if(*m++)
  1576. *w = *r;
  1577. return bdst; /* not used */
  1578. }
  1579. static Buffer
  1580. boolcopy24(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
  1581. {
  1582. uchar *m;
  1583. uchar *r, *w, *ew;
  1584. USED(i);
  1585. USED(o);
  1586. m = bmask.grey;
  1587. w = bdst.red;
  1588. r = bsrc.red;
  1589. ew = w+dx*3;
  1590. while(w < ew){
  1591. if(*m++){
  1592. *w++ = *r++;
  1593. *w++ = *r++;
  1594. *w++ = *r++;
  1595. }else{
  1596. w += 3;
  1597. r += 3;
  1598. }
  1599. }
  1600. return bdst; /* not used */
  1601. }
  1602. static Buffer
  1603. boolcopy32(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
  1604. {
  1605. uchar *m;
  1606. ulong *r, *w, *ew;
  1607. USED(i);
  1608. USED(o);
  1609. m = bmask.grey;
  1610. w = (ulong*)bdst.red;
  1611. r = (ulong*)bsrc.red;
  1612. ew = w+dx;
  1613. for(; w < ew; w++,r++)
  1614. if(*m++)
  1615. *w = *r;
  1616. return bdst; /* not used */
  1617. }
  1618. static Buffer
  1619. genconv(Param *p, uchar *buf, int y)
  1620. {
  1621. Buffer b;
  1622. int nb;
  1623. uchar *r, *w, *ew;
  1624. /* read from source into RGB format in convbuf */
  1625. b = p->convreadcall(p, p->convbuf, y);
  1626. /* write RGB format into dst format in buf */
  1627. p->convwritecall(p->convdpar, buf, b);
  1628. if(p->convdx){
  1629. nb = p->convdpar->img->depth/8;
  1630. r = buf;
  1631. w = buf+nb*p->dx;
  1632. ew = buf+nb*p->convdx;
  1633. while(w<ew)
  1634. *w++ = *r++;
  1635. }
  1636. b.red = buf;
  1637. b.blu = b.grn = b.grey = b.alpha = nil;
  1638. b.rgba = (ulong*)buf;
  1639. b.delta = 0;
  1640. return b;
  1641. }
  1642. static Readfn*
  1643. convfn(Memimage *dst, Param *dpar, Memimage *src, Param *spar, int *ndrawbuf)
  1644. {
  1645. if(dst->chan == src->chan && !(src->flags&Frepl)){
  1646. //if(drawdebug) iprint("readptr...");
  1647. return readptr;
  1648. }
  1649. if(dst->chan==CMAP8 && (src->chan==GREY1||src->chan==GREY2||src->chan==GREY4)){
  1650. /* cheat because we know the replicated value is exactly the color map entry. */
  1651. //if(drawdebug) iprint("Readnbit...");
  1652. return readnbit;
  1653. }
  1654. spar->convreadcall = readfn(src);
  1655. spar->convwritecall = writefn(dst);
  1656. spar->convdpar = dpar;
  1657. /* allocate a conversion buffer */
  1658. spar->convbufoff = *ndrawbuf;
  1659. *ndrawbuf += spar->dx*4;
  1660. if(spar->dx > Dx(spar->img->r)){
  1661. spar->convdx = spar->dx;
  1662. spar->dx = Dx(spar->img->r);
  1663. }
  1664. //if(drawdebug) iprint("genconv...");
  1665. return genconv;
  1666. }
  1667. static ulong
  1668. pixelbits(Memimage *i, Point pt)
  1669. {
  1670. uchar *p;
  1671. ulong val;
  1672. int off, bpp, npack;
  1673. val = 0;
  1674. p = byteaddr(i, pt);
  1675. switch(bpp=i->depth){
  1676. case 1:
  1677. case 2:
  1678. case 4:
  1679. npack = 8/bpp;
  1680. off = pt.x%npack;
  1681. val = p[0] >> bpp*(npack-1-off);
  1682. val &= (1<<bpp)-1;
  1683. break;
  1684. case 8:
  1685. val = p[0];
  1686. break;
  1687. case 16:
  1688. val = p[0]|(p[1]<<8);
  1689. break;
  1690. case 24:
  1691. val = p[0]|(p[1]<<8)|(p[2]<<16);
  1692. break;
  1693. case 32:
  1694. val = p[0]|(p[1]<<8)|(p[2]<<16)|(p[3]<<24);
  1695. break;
  1696. }
  1697. while(bpp<32){
  1698. val |= val<<bpp;
  1699. bpp *= 2;
  1700. }
  1701. return val;
  1702. }
  1703. static Calcfn*
  1704. boolcopyfn(Memimage *img, Memimage *mask)
  1705. {
  1706. if(mask->flags&Frepl && Dx(mask->r)==1 && Dy(mask->r)==1 && pixelbits(mask, mask->r.min)==~0)
  1707. return boolmemmove;
  1708. switch(img->depth){
  1709. case 8:
  1710. return boolcopy8;
  1711. case 16:
  1712. return boolcopy16;
  1713. case 24:
  1714. return boolcopy24;
  1715. case 32:
  1716. return boolcopy32;
  1717. default:
  1718. assert(0 /* boolcopyfn */);
  1719. }
  1720. return nil;
  1721. }
  1722. /*
  1723. * Optimized draw for filling and scrolling; uses memset and memmove.
  1724. */
  1725. static void
  1726. memsetb(void *vp, uchar val, int n)
  1727. {
  1728. uchar *p, *ep;
  1729. p = vp;
  1730. ep = p+n;
  1731. while(p<ep)
  1732. *p++ = val;
  1733. }
  1734. static void
  1735. memsets(void *vp, ushort val, int n)
  1736. {
  1737. ushort *p, *ep;
  1738. p = vp;
  1739. ep = p+n;
  1740. while(p<ep)
  1741. *p++ = val;
  1742. }
  1743. static void
  1744. memsetl(void *vp, ulong val, int n)
  1745. {
  1746. ulong *p, *ep;
  1747. p = vp;
  1748. ep = p+n;
  1749. while(p<ep)
  1750. *p++ = val;
  1751. }
  1752. static void
  1753. memset24(void *vp, ulong val, int n)
  1754. {
  1755. uchar *p, *ep;
  1756. uchar a,b,c;
  1757. p = vp;
  1758. ep = p+3*n;
  1759. a = val;
  1760. b = val>>8;
  1761. c = val>>16;
  1762. while(p<ep){
  1763. *p++ = a;
  1764. *p++ = b;
  1765. *p++ = c;
  1766. }
  1767. }
  1768. static ulong
  1769. imgtorgba(Memimage *img, ulong val)
  1770. {
  1771. uchar r, g, b, a;
  1772. int nb, ov, v;
  1773. ulong chan;
  1774. uchar *p;
  1775. a = 0xFF;
  1776. r = g = b = 0xAA; /* garbage */
  1777. for(chan=img->chan; chan; chan>>=8){
  1778. nb = NBITS(chan);
  1779. ov = v = val&((1<<nb)-1);
  1780. val >>= nb;
  1781. while(nb < 8){
  1782. v |= v<<nb;
  1783. nb *= 2;
  1784. }
  1785. v >>= (nb-8);
  1786. switch(TYPE(chan)){
  1787. case CRed:
  1788. r = v;
  1789. break;
  1790. case CGreen:
  1791. g = v;
  1792. break;
  1793. case CBlue:
  1794. b = v;
  1795. break;
  1796. case CAlpha:
  1797. a = v;
  1798. break;
  1799. case CGrey:
  1800. r = g = b = v;
  1801. break;
  1802. case CMap:
  1803. p = img->cmap->cmap2rgb+3*ov;
  1804. r = *p++;
  1805. g = *p++;
  1806. b = *p;
  1807. break;
  1808. }
  1809. }
  1810. return (r<<24)|(g<<16)|(b<<8)|a;
  1811. }
  1812. static ulong
  1813. rgbatoimg(Memimage *img, ulong rgba)
  1814. {
  1815. ulong chan;
  1816. int d, nb;
  1817. ulong v;
  1818. uchar *p, r, g, b, a, m;
  1819. v = 0;
  1820. r = rgba>>24;
  1821. g = rgba>>16;
  1822. b = rgba>>8;
  1823. a = rgba;
  1824. d = 0;
  1825. for(chan=img->chan; chan; chan>>=8){
  1826. nb = NBITS(chan);
  1827. switch(TYPE(chan)){
  1828. case CRed:
  1829. v |= (r>>(8-nb))<<d;
  1830. break;
  1831. case CGreen:
  1832. v |= (g>>(8-nb))<<d;
  1833. break;
  1834. case CBlue:
  1835. v |= (b>>(8-nb))<<d;
  1836. break;
  1837. case CAlpha:
  1838. v |= (a>>(8-nb))<<d;
  1839. break;
  1840. case CMap:
  1841. p = img->cmap->rgb2cmap;
  1842. m = p[(r>>4)*256+(g>>4)*16+(b>>4)];
  1843. v |= (m>>(8-nb))<<d;
  1844. break;
  1845. case CGrey:
  1846. m = RGB2K(r,g,b);
  1847. v |= (m>>(8-nb))<<d;
  1848. break;
  1849. }
  1850. d += nb;
  1851. }
  1852. // print("rgba2img %.8lux = %.*lux\n", rgba, 2*d/8, v);
  1853. return v;
  1854. }
  1855. #define DBG if(0)
  1856. static int
  1857. memoptdraw(Memdrawparam *par)
  1858. {
  1859. int m, y, dy, dx, op;
  1860. ulong v;
  1861. Memimage *src;
  1862. Memimage *dst;
  1863. dx = Dx(par->r);
  1864. dy = Dy(par->r);
  1865. src = par->src;
  1866. dst = par->dst;
  1867. op = par->op;
  1868. DBG print("state %lux mval %lux dd %d\n", par->state, par->mval, dst->depth);
  1869. /*
  1870. * If we have an opaque mask and source is one opaque pixel we can convert to the
  1871. * destination format and just replicate with memset.
  1872. */
  1873. m = Simplesrc|Simplemask|Fullmask;
  1874. if((par->state&m)==m && (par->srgba&0xFF) == 0xFF && (op ==S || op == SoverD)){
  1875. uchar *dp, p[4];
  1876. int d, dwid, ppb, np, nb;
  1877. uchar lm, rm;
  1878. DBG print("memopt, dst %p, dst->data->bdata %p\n", dst, dst->data->bdata);
  1879. dwid = dst->width*sizeof(ulong);
  1880. dp = byteaddr(dst, par->r.min);
  1881. v = par->sdval;
  1882. DBG print("sdval %lud, depth %d\n", v, dst->depth);
  1883. switch(dst->depth){
  1884. case 1:
  1885. case 2:
  1886. case 4:
  1887. for(d=dst->depth; d<8; d*=2)
  1888. v |= (v<<d);
  1889. ppb = 8/dst->depth; /* pixels per byte */
  1890. m = ppb-1;
  1891. /* left edge */
  1892. np = par->r.min.x&m; /* no. pixels unused on left side of word */
  1893. dx -= (ppb-np);
  1894. nb = 8 - np * dst->depth; /* no. bits used on right side of word */
  1895. lm = (1<<nb)-1;
  1896. DBG print("np %d x %d nb %d lm %ux ppb %d m %ux\n", np, par->r.min.x, nb, lm, ppb, m);
  1897. /* right edge */
  1898. np = par->r.max.x&m; /* no. pixels used on left side of word */
  1899. dx -= np;
  1900. nb = 8 - np * dst->depth; /* no. bits unused on right side of word */
  1901. rm = ~((1<<nb)-1);
  1902. DBG print("np %d x %d nb %d rm %ux ppb %d m %ux\n", np, par->r.max.x, nb, rm, ppb, m);
  1903. DBG print("dx %d Dx %d\n", dx, Dx(par->r));
  1904. /* lm, rm are masks that are 1 where we should touch the bits */
  1905. if(dx < 0){ /* just one byte */
  1906. lm &= rm;
  1907. for(y=0; y<dy; y++, dp+=dwid)
  1908. *dp ^= (v ^ *dp) & lm;
  1909. }else if(dx == 0){ /* no full bytes */
  1910. if(lm)
  1911. dwid--;
  1912. for(y=0; y<dy; y++, dp+=dwid){
  1913. if(lm){
  1914. DBG print("dp %p v %lux lm %ux (v ^ *dp) & lm %lux\n", dp, v, lm, (v^*dp)&lm);
  1915. *dp ^= (v ^ *dp) & lm;
  1916. dp++;
  1917. }
  1918. *dp ^= (v ^ *dp) & rm;
  1919. }
  1920. }else{ /* full bytes in middle */
  1921. dx /= ppb;
  1922. if(lm)
  1923. dwid--;
  1924. dwid -= dx;
  1925. for(y=0; y<dy; y++, dp+=dwid){
  1926. if(lm){
  1927. *dp ^= (v ^ *dp) & lm;
  1928. dp++;
  1929. }
  1930. memset(dp, v, dx);
  1931. dp += dx;
  1932. *dp ^= (v ^ *dp) & rm;
  1933. }
  1934. }
  1935. return 1;
  1936. case 8:
  1937. for(y=0; y<dy; y++, dp+=dwid)
  1938. memset(dp, v, dx);
  1939. return 1;
  1940. case 16:
  1941. p[0] = v; /* make little endian */
  1942. p[1] = v>>8;
  1943. v = *(ushort*)p;
  1944. DBG print("dp=%p; dx=%d; for(y=0; y<%d; y++, dp+=%d)\nmemsets(dp, v, dx);\n",
  1945. dp, dx, dy, dwid);
  1946. for(y=0; y<dy; y++, dp+=dwid)
  1947. memsets(dp, v, dx);
  1948. return 1;
  1949. case 24:
  1950. for(y=0; y<dy; y++, dp+=dwid)
  1951. memset24(dp, v, dx);
  1952. return 1;
  1953. case 32:
  1954. p[0] = v; /* make little endian */
  1955. p[1] = v>>8;
  1956. p[2] = v>>16;
  1957. p[3] = v>>24;
  1958. v = *(ulong*)p;
  1959. for(y=0; y<dy; y++, dp+=dwid)
  1960. memsetl(dp, v, dx);
  1961. return 1;
  1962. default:
  1963. assert(0 /* bad dest depth in memoptdraw */);
  1964. }
  1965. }
  1966. /*
  1967. * If no source alpha, an opaque mask, we can just copy the
  1968. * source onto the destination. If the channels are the same and
  1969. * the source is not replicated, memmove suffices.
  1970. */
  1971. m = Simplemask|Fullmask;
  1972. if((par->state&(m|Replsrc))==m && src->depth >= 8
  1973. && src->chan == dst->chan && !(src->flags&Falpha) && (op == S || op == SoverD)){
  1974. uchar *sp, *dp;
  1975. long swid, dwid, nb;
  1976. int dir;
  1977. if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min))
  1978. dir = -1;
  1979. else
  1980. dir = 1;
  1981. swid = src->width*sizeof(ulong);
  1982. dwid = dst->width*sizeof(ulong);
  1983. sp = byteaddr(src, par->sr.min);
  1984. dp = byteaddr(dst, par->r.min);
  1985. if(dir == -1){
  1986. sp += (dy-1)*swid;
  1987. dp += (dy-1)*dwid;
  1988. swid = -swid;
  1989. dwid = -dwid;
  1990. }
  1991. nb = (dx*src->depth)/8;
  1992. for(y=0; y<dy; y++, sp+=swid, dp+=dwid)
  1993. memmove(dp, sp, nb);
  1994. return 1;
  1995. }
  1996. /*
  1997. * If we have a 1-bit mask, 1-bit source, and 1-bit destination, and
  1998. * they're all bit aligned, we can just use bit operators. This happens
  1999. * when we're manipulating boolean masks, e.g. in the arc code.
  2000. */
  2001. if((par->state&(Simplemask|Simplesrc|Replmask|Replsrc))==0
  2002. && dst->chan==GREY1 && src->chan==GREY1 && par->mask->chan==GREY1
  2003. && (par->r.min.x&7)==(par->sr.min.x&7) && (par->r.min.x&7)==(par->mr.min.x&7)){
  2004. uchar *sp, *dp, *mp;
  2005. uchar lm, rm;
  2006. long swid, dwid, mwid;
  2007. int i, x, dir;
  2008. sp = byteaddr(src, par->sr.min);
  2009. dp = byteaddr(dst, par->r.min);
  2010. mp = byteaddr(par->mask, par->mr.min);
  2011. swid = src->width*sizeof(ulong);
  2012. dwid = dst->width*sizeof(ulong);
  2013. mwid = par->mask->width*sizeof(ulong);
  2014. if(src->data == dst->data && byteaddr(dst, par->r.min) > byteaddr(src, par->sr.min)){
  2015. dir = -1;
  2016. }else
  2017. dir = 1;
  2018. lm = 0xFF>>(par->r.min.x&7);
  2019. rm = 0xFF<<(8-(par->r.max.x&7));
  2020. dx -= (8-(par->r.min.x&7)) + (par->r.max.x&7);
  2021. if(dx < 0){ /* one byte wide */
  2022. lm &= rm;
  2023. if(dir == -1){
  2024. dp += dwid*(dy-1);
  2025. sp += swid*(dy-1);
  2026. mp += mwid*(dy-1);
  2027. dwid = -dwid;
  2028. swid = -swid;
  2029. mwid = -mwid;
  2030. }
  2031. for(y=0; y<dy; y++){
  2032. *dp ^= (*dp ^ *sp) & *mp & lm;
  2033. dp += dwid;
  2034. sp += swid;
  2035. mp += mwid;
  2036. }
  2037. return 1;
  2038. }
  2039. dx /= 8;
  2040. if(dir == 1){
  2041. i = (lm!=0)+dx+(rm!=0);
  2042. mwid -= i;
  2043. swid -= i;
  2044. dwid -= i;
  2045. for(y=0; y<dy; y++, dp+=dwid, sp+=swid, mp+=mwid){
  2046. if(lm){
  2047. *dp ^= (*dp ^ *sp++) & *mp++ & lm;
  2048. dp++;
  2049. }
  2050. for(x=0; x<dx; x++){
  2051. *dp ^= (*dp ^ *sp++) & *mp++;
  2052. dp++;
  2053. }
  2054. if(rm){
  2055. *dp ^= (*dp ^ *sp++) & *mp++ & rm;
  2056. dp++;
  2057. }
  2058. }
  2059. return 1;
  2060. }else{
  2061. /* dir == -1 */
  2062. i = (lm!=0)+dx+(rm!=0);
  2063. dp += dwid*(dy-1)+i-1;
  2064. sp += swid*(dy-1)+i-1;
  2065. mp += mwid*(dy-1)+i-1;
  2066. dwid = -dwid+i;
  2067. swid = -swid+i;
  2068. mwid = -mwid+i;
  2069. for(y=0; y<dy; y++, dp+=dwid, sp+=swid, mp+=mwid){
  2070. if(rm){
  2071. *dp ^= (*dp ^ *sp--) & *mp-- & rm;
  2072. dp--;
  2073. }
  2074. for(x=0; x<dx; x++){
  2075. *dp ^= (*dp ^ *sp--) & *mp--;
  2076. dp--;
  2077. }
  2078. if(lm){
  2079. *dp ^= (*dp ^ *sp--) & *mp-- & lm;
  2080. dp--;
  2081. }
  2082. }
  2083. }
  2084. return 1;
  2085. }
  2086. return 0;
  2087. }
  2088. #undef DBG
  2089. /*
  2090. * Boolean character drawing.
  2091. * Solid opaque color through a 1-bit greyscale mask.
  2092. */
  2093. #define DBG if(0)
  2094. static int
  2095. chardraw(Memdrawparam *par)
  2096. {
  2097. ulong bits;
  2098. int i, ddepth, dy, dx, x, bx, ex, y, npack, bsh, depth, op;
  2099. ulong v, maskwid, dstwid;
  2100. uchar *wp, *rp, *q, *wc;
  2101. ushort *ws;
  2102. ulong *wl;
  2103. uchar sp[4];
  2104. Rectangle r, mr;
  2105. Memimage *mask, *src, *dst;
  2106. if(0) if(drawdebug) iprint("chardraw? mf %lux md %d sf %lux dxs %d dys %d dd %d ddat %p sdat %p\n",
  2107. par->mask->flags, par->mask->depth, par->src->flags,
  2108. Dx(par->src->r), Dy(par->src->r), par->dst->depth, par->dst->data, par->src->data);
  2109. mask = par->mask;
  2110. src = par->src;
  2111. dst = par->dst;
  2112. r = par->r;
  2113. mr = par->mr;
  2114. op = par->op;
  2115. if((par->state&(Replsrc|Simplesrc|Replmask)) != (Replsrc|Simplesrc)
  2116. || mask->depth != 1 || src->flags&Falpha || dst->depth<8 || dst->data==src->data
  2117. || op != SoverD)
  2118. return 0;
  2119. //if(drawdebug) iprint("chardraw...");
  2120. depth = mask->depth;
  2121. maskwid = mask->width*sizeof(ulong);
  2122. rp = byteaddr(mask, mr.min);
  2123. npack = 8/depth;
  2124. bsh = (mr.min.x % npack) * depth;
  2125. wp = byteaddr(dst, r.min);
  2126. dstwid = dst->width*sizeof(ulong);
  2127. DBG print("bsh %d\n", bsh);
  2128. dy = Dy(r);
  2129. dx = Dx(r);
  2130. ddepth = dst->depth;
  2131. /*
  2132. * for loop counts from bsh to bsh+dx
  2133. *
  2134. * we want the bottom bits to be the amount
  2135. * to shift the pixels down, so for n≡0 (mod 8) we want
  2136. * bottom bits 7. for n≡1, 6, etc.
  2137. * the bits come from -n-1.
  2138. */
  2139. bx = -bsh-1;
  2140. ex = -bsh-1-dx;
  2141. SET(bits);
  2142. v = par->sdval;
  2143. /* make little endian */
  2144. sp[0] = v;
  2145. sp[1] = v>>8;
  2146. sp[2] = v>>16;
  2147. sp[3] = v>>24;
  2148. //print("sp %x %x %x %x\n", sp[0], sp[1], sp[2], sp[3]);
  2149. for(y=0; y<dy; y++, rp+=maskwid, wp+=dstwid){
  2150. q = rp;
  2151. if(bsh)
  2152. bits = *q++;
  2153. switch(ddepth){
  2154. case 8:
  2155. //if(drawdebug) iprint("8loop...");
  2156. wc = wp;
  2157. for(x=bx; x>ex; x--, wc++){
  2158. i = x&7;
  2159. if(i == 8-1)
  2160. bits = *q++;
  2161. DBG print("bits %lux sh %d...", bits, i);
  2162. if((bits>>i)&1)
  2163. *wc = v;
  2164. }
  2165. break;
  2166. case 16:
  2167. ws = (ushort*)wp;
  2168. v = *(ushort*)sp;
  2169. for(x=bx; x>ex; x--, ws++){
  2170. i = x&7;
  2171. if(i == 8-1)
  2172. bits = *q++;
  2173. DBG print("bits %lux sh %d...", bits, i);
  2174. if((bits>>i)&1)
  2175. *ws = v;
  2176. }
  2177. break;
  2178. case 24:
  2179. wc = wp;
  2180. for(x=bx; x>ex; x--, wc+=3){
  2181. i = x&7;
  2182. if(i == 8-1)
  2183. bits = *q++;
  2184. DBG print("bits %lux sh %d...", bits, i);
  2185. if((bits>>i)&1){
  2186. wc[0] = sp[0];
  2187. wc[1] = sp[1];
  2188. wc[2] = sp[2];
  2189. }
  2190. }
  2191. break;
  2192. case 32:
  2193. wl = (ulong*)wp;
  2194. v = *(ulong*)sp;
  2195. for(x=bx; x>ex; x--, wl++){
  2196. i = x&7;
  2197. if(i == 8-1)
  2198. bits = *q++;
  2199. DBG iprint("bits %lux sh %d...", bits, i);
  2200. if((bits>>i)&1)
  2201. *wl = v;
  2202. }
  2203. break;
  2204. }
  2205. }
  2206. DBG print("\n");
  2207. return 1;
  2208. }
  2209. #undef DBG
  2210. /*
  2211. * Fill entire byte with replicated (if necessary) copy of source pixel,
  2212. * assuming destination ldepth is >= source ldepth.
  2213. *
  2214. * This code is just plain wrong for >8bpp.
  2215. *
  2216. ulong
  2217. membyteval(Memimage *src)
  2218. {
  2219. int i, val, bpp;
  2220. uchar uc;
  2221. unloadmemimage(src, src->r, &uc, 1);
  2222. bpp = src->depth;
  2223. uc <<= (src->r.min.x&(7/src->depth))*src->depth;
  2224. uc &= ~(0xFF>>bpp);
  2225. /* pixel value is now in high part of byte. repeat throughout byte
  2226. val = uc;
  2227. for(i=bpp; i<8; i<<=1)
  2228. val |= val>>i;
  2229. return val;
  2230. }
  2231. *
  2232. */
  2233. void
  2234. memfillcolor(Memimage *i, ulong val)
  2235. {
  2236. ulong bits;
  2237. int d, y;
  2238. if(val == DNofill)
  2239. return;
  2240. bits = rgbatoimg(i, val);
  2241. switch(i->depth){
  2242. case 24: /* 24-bit images suck */
  2243. for(y=i->r.min.y; y<i->r.max.y; y++)
  2244. memset24(byteaddr(i, Pt(i->r.min.x, y)), bits, Dx(i->r));
  2245. break;
  2246. default: /* 1, 2, 4, 8, 16, 32 */
  2247. for(d=i->depth; d<32; d*=2)
  2248. bits = (bits << d) | bits;
  2249. memsetl(wordaddr(i, i->r.min), bits, i->width*Dy(i->r));
  2250. break;
  2251. }
  2252. }