draw.c 55 KB

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