draw.c 53 KB

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