draw.c 54 KB

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