draw.c 54 KB

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