draw.c 54 KB

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