draw.c 56 KB

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