image.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /********************************************************************** <BR>
  2. This file is part of Crack dot Com's free source code release of
  3. Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
  4. information about compiling & licensing issues visit this URL</a>
  5. <PRE> If that doesn't help, contact Jonathan Clark at
  6. golgotha_source@usa.net (Subject should have "GOLG" in it)
  7. ***********************************************************************/
  8. #include "image/image.hh"
  9. #include "error/error.hh"
  10. #include "image/image32.hh"
  11. #include "image/image16.hh"
  12. #include "image/image8.hh"
  13. #include "palette/pal.hh"
  14. #include "area/rectlist.hh"
  15. #include "image/context.hh"
  16. #ifndef abs
  17. #define abs(x) ((x)<0 ? -(x) : (x))
  18. #endif
  19. // void i4_image_class::widget(i4_coord x1, i4_coord y1, i4_coord x2, i4_coord y2, i4_color bright, i4_color med, i4_color dark, i4_draw_context_class &context)
  20. // {
  21. // add_dirty(x1,y1,x2,y2,context); // to keep from creating a dirty for each operation below
  22. // bar(x1,y1,x2,y1,bright,context);
  23. // bar(x1,y1+1,x1,y2,bright,context);
  24. // bar(x2,y1+1,x2,y2,dark,context);
  25. // bar(x1+1,y2,x2-1,y2,dark,context);
  26. // bar(x1+1,y1+1,x2-1,y2-1,med,context);
  27. // }
  28. void i4_image_class::put_pixel(i4_coord x, i4_coord y, w32 color, i4_draw_context_class &context)
  29. {
  30. for (i4_rect_list_class::area_iter c=context.clip.list.begin();c!=context.clip.list.end();++c)
  31. {
  32. if (x>=c->x1 && x<=c->x2 && y>=c->y1 && y<=c->y2)
  33. put_pixel(x + context.xoff, y + context.yoff, color);
  34. }
  35. }
  36. w32 i4_image_class::get_pixel(i4_coord x, i4_coord y, i4_draw_context_class &context)
  37. {
  38. return get_pixel(x + context.xoff, y + context.yoff);
  39. }
  40. void i4_image_class::xor_bar(i4_coord x1, i4_coord y1,
  41. i4_coord x2, i4_coord y2,
  42. i4_color color, i4_draw_context_class &context)
  43. {
  44. for (i4_rect_list_class::area_iter c=context.clip.list.begin();c!=context.clip.list.end();++c)
  45. {
  46. i4_coord lx1,ly1,lx2,ly2;
  47. if (x1<c->x1) lx1=c->x1; else lx1=x1;
  48. if (y1<c->y1) ly1=c->y1; else ly1=y1;
  49. if (x2>c->x2) lx2=c->x2; else lx2=x2;
  50. if (y2>c->y2) ly2=c->y2; else ly2=y2;
  51. if (!(lx1>lx2 || ly1>ly2))
  52. {
  53. add_dirty(lx1,ly1,lx2,ly2,context);
  54. for (;ly1<=ly2; ly1++)
  55. for (int x=lx1; x<=lx2; x++)
  56. put_pixel(x + context.xoff, ly1 + context.yoff, get_pixel(x,ly1)^0xffffff);
  57. }
  58. }
  59. }
  60. void i4_image_class::bar(i4_coord x1, i4_coord y1,
  61. i4_coord x2, i4_coord y2,
  62. i4_color color, i4_draw_context_class &context)
  63. {
  64. for (i4_rect_list_class::area_iter c=context.clip.list.begin();c!=context.clip.list.end();++c)
  65. {
  66. i4_coord lx1,ly1,lx2,ly2;
  67. if (x1<c->x1) lx1=c->x1; else lx1=x1;
  68. if (y1<c->y1) ly1=c->y1; else ly1=y1;
  69. if (x2>c->x2) lx2=c->x2; else lx2=x2;
  70. if (y2>c->y2) ly2=c->y2; else ly2=y2;
  71. if (!(lx1>lx2 || ly1>ly2))
  72. {
  73. add_dirty(lx1,ly1,lx2,ly2,context);
  74. for (;ly1<=ly2; ly1++)
  75. for (int x=lx1; x<=lx2; x++)
  76. put_pixel(x+ context.xoff, ly1+ context.yoff, color);
  77. }
  78. }
  79. }
  80. void i4_image_class::line(i4_coord ox1, i4_coord oy1,
  81. i4_coord ox2, i4_coord oy2,
  82. i4_color color, i4_draw_context_class &context)
  83. {
  84. i4_coord x1,y1,x2,y2;
  85. i4_coord cx1,cy1,cx2,cy2;
  86. i4_bool skip;
  87. for (i4_rect_list_class::area_iter c=context.clip.list.begin();c!=context.clip.list.end();++c)
  88. {
  89. x1=ox1;
  90. y1=oy1;
  91. x2=ox2;
  92. y2=oy2;
  93. skip=i4_F;
  94. i4_coord i,xc,yc,er,n,m,xi,yi,xcxi,ycyi,xcyi;
  95. unsigned dcy,dcx;
  96. // check to make sure that both endpoint are on the screen
  97. cx1=c->x1;
  98. cy1=c->y1;
  99. cx2=c->x2;
  100. cy2=c->y2;
  101. // check to see if the line is completly clipped off
  102. if (!((x1<cx1 && x2<cx1) || (x1>cx2 && x2>cx2) ||
  103. (y1<cy1 && y2<cy1) || (y1>cy2 && y2>cy2)))
  104. {
  105. if (x1>x2) // make sure that x1 is to the left
  106. {
  107. i=x1; x1=x2; x2=i; // if not swap points
  108. i=y1; y1=y2; y2=i;
  109. }
  110. // clip the left side
  111. if (x1<cx1)
  112. {
  113. int my=(y2-y1);
  114. int mx=(x2-x1),b;
  115. if (!mx) skip=i4_T;
  116. if (my)
  117. {
  118. b=y1-(y2-y1)*x1/mx;
  119. y1=my*cx1/mx+b;
  120. x1=cx1;
  121. }
  122. else x1=cx1;
  123. }
  124. // clip the right side
  125. if (x2>cx2)
  126. {
  127. int my=(y2-y1);
  128. int mx=(x2-x1),b;
  129. if (!mx) skip=i4_T;
  130. else if (my)
  131. {
  132. b=y1-(y2-y1)*x1/mx;
  133. y2=my*cx2/mx+b;
  134. x2=cx2;
  135. }
  136. else x2=cx2;
  137. }
  138. if (y1>y2) // make sure that y1 is on top
  139. {
  140. i=x1; x1=x2; x2=i; // if not swap points
  141. i=y1; y1=y2; y2=i;
  142. }
  143. // clip the bottom
  144. if (y2>cy2)
  145. {
  146. int mx=(x2-x1);
  147. int my=(y2-y1),b;
  148. if (!my)
  149. skip=i4_T;
  150. else if (mx)
  151. {
  152. b=y1-(y2-y1)*x1/mx;
  153. x2=(cy2-b)*mx/my;
  154. y2=cy2;
  155. }
  156. else y2=cy2;
  157. }
  158. // clip the top
  159. if (y1<cy1)
  160. {
  161. int mx=(x2-x1);
  162. int my=(y2-y1),b;
  163. if (!my)
  164. skip=i4_T;
  165. else if (mx)
  166. {
  167. b=y1-(y2-y1)*x1/mx;
  168. x1=(cy1-b)*mx/my;
  169. y1=cy1;
  170. }
  171. else y1=cy1;
  172. }
  173. // see if it got cliped into the box, out out
  174. if (x1<cx1 || x2<cx1 || x1>cx2 || x2>cx2 || y1<cy1 || y2 <cy1 || y1>cy2 || y2>cy2)
  175. skip=i4_T;
  176. if (x1>x2)
  177. { xc=x2; xi=x1; }
  178. else { xi=x2; xc=x1; }
  179. if (!skip)
  180. {
  181. // assume y1<=y2 from above swap operation
  182. yi=y2; yc=y1;
  183. add_dirty(xc,yc,xi,yi,context);
  184. dcx=x1+context.xoff; dcy=y1+context.yoff;
  185. xc=(x2-x1); yc=(y2-y1);
  186. if (xc<0) xi=-1; else xi=1;
  187. if (yc<0) yi=-1; else yi=1;
  188. n=abs(xc); m=abs(yc);
  189. ycyi=abs(2*yc*xi);
  190. er=0;
  191. if (n>m)
  192. {
  193. xcxi=abs(2*xc*xi);
  194. for (i=0;i<=n;i++)
  195. {
  196. put_pixel(dcx, dcy, color);
  197. if (er>0)
  198. {
  199. dcy+=yi;
  200. er-=xcxi;
  201. }
  202. er+=ycyi;
  203. dcx+=xi;
  204. }
  205. }
  206. else
  207. {
  208. xcyi=abs(2*xc*yi);
  209. for (i=0;i<=m;i++)
  210. {
  211. put_pixel(dcx, dcy, color);
  212. if (er>0)
  213. {
  214. dcx+=xi;
  215. er-=ycyi;
  216. }
  217. er+=xcyi;
  218. dcy+=yi;
  219. }
  220. }
  221. }
  222. }
  223. }
  224. }
  225. void i4_image_class::put_part(i4_image_class *to,
  226. i4_coord _x, i4_coord _y,
  227. i4_coord x1, i4_coord y1, i4_coord x2, i4_coord y2,
  228. i4_draw_context_class &context)
  229. {
  230. for (i4_rect_list_class::area_iter c=context.clip.list.begin();c!=context.clip.list.end();++c)
  231. {
  232. i4_coord lx1,ly1,lx2,ly2,x=_x,y=_y;
  233. if (x1<0) { lx1=0; _x+=-x1; } else lx1=x1;
  234. if (y1<0) { ly1=0; _y+=-y1; } else ly1=y1;
  235. if (x2>=width()) lx2=width()-1; else lx2=x2;
  236. if (y2>=height()) ly2=height()-1; else ly2=y2;
  237. if (!(lx1>lx2 || ly1>ly2))
  238. {
  239. if (x<c->x1)
  240. { lx1+=(c->x1-x); x=c->x1; }
  241. if (y<c->y1)
  242. { ly1+=(c->y1-y); y=c->y1; }
  243. if (x+lx2-lx1+1>c->x2)
  244. lx2=c->x2-x+lx1;
  245. if (y+ly2-ly1+1>c->y2)
  246. ly2=c->y2-y+ly1;
  247. const i4_pixel_format *from_format=&get_pal()->source;
  248. const i4_pixel_format *to_format=&to->get_pal()->source;
  249. if (from_format->alpha_mask==0 || to_format->alpha_mask)
  250. {
  251. if (!(lx1>lx2 || ly1>ly2))
  252. {
  253. to->add_dirty(x,y,x+(lx2-lx1+1),y+(ly2-ly1+1),context);
  254. while (ly1<=ly2)
  255. {
  256. int tx=x;
  257. for (int fx=lx1;fx<=lx2; fx++, tx++)
  258. {
  259. w32 c=get_pixel(fx, ly1);
  260. to->put_pixel(tx+ context.xoff, y+ context.yoff, c);
  261. }
  262. ly1++;
  263. y++;
  264. }
  265. }
  266. }
  267. else
  268. {
  269. if (!(lx1>lx2 || ly1>ly2))
  270. {
  271. to->add_dirty(x,y,x+(lx2-lx1+1),y+(ly2-ly1+1),context);
  272. while (ly1<=ly2)
  273. {
  274. int tx=x;
  275. for (int fx=lx1;fx<=lx2; fx++, tx++)
  276. {
  277. w32 c=get_pixel(fx, ly1);
  278. float a=c>>24;
  279. if (a)
  280. {
  281. a/=255.0;
  282. int r=(c>>16)&0xff, g=(c>>8)&0xff, b=c&0xff;
  283. w32 tc=to->get_pixel(tx, y, context);
  284. int tr=(tc>>16)&0xff, tg=(tc>>8)&0xff, tb=tc&0xff;
  285. r=(int)((r-tr)*a + tr);
  286. g=(int)((g-tg)*a + tg);
  287. b=(int)((b-tb)*a + tb);
  288. to->put_pixel(tx+ context.xoff, y+ context.yoff, (r<<16) | (g<<8) | b);
  289. }
  290. }
  291. ly1++;
  292. y++;
  293. }
  294. }
  295. }
  296. }
  297. }
  298. }
  299. void i4_image_class::put_part_trans(i4_image_class *to,
  300. i4_coord _x, i4_coord _y,
  301. i4_coord x1, i4_coord y1, i4_coord x2, i4_coord y2,
  302. i4_color trans_color,
  303. i4_draw_context_class &context)
  304. {
  305. for (i4_rect_list_class::area_iter c=context.clip.list.begin();c!=context.clip.list.end();++c)
  306. {
  307. i4_coord lx1,ly1,lx2,ly2,x=_x,y=_y;
  308. if (x1<0) { lx1=0; _x+=-x1; } else lx1=x1;
  309. if (y1<0) { ly1=0; _y+=-y1; } else ly1=y1;
  310. if (x2>=width()) lx2=width()-1; else lx2=x2;
  311. if (y2>=height()) ly2=height()-1; else ly2=y2;
  312. if (!(lx1>lx2 || ly1>ly2))
  313. {
  314. if (x<c->x1)
  315. { lx1+=(c->x1-x); x=c->x1; }
  316. if (y<c->y1)
  317. { ly1+=(c->y1-y); y=c->y1; }
  318. if (x+lx2-lx1+1>c->x2)
  319. lx2=c->x2-x+lx1;
  320. if (y+ly2-ly1+1>c->y2)
  321. ly2=c->y2-y+ly1;
  322. const i4_pixel_format *from_format=&get_pal()->source;
  323. const i4_pixel_format *to_format=&to->get_pal()->source;
  324. if (!(lx1>lx2 || ly1>ly2))
  325. {
  326. to->add_dirty(x,y,x+(lx2-lx1+1),y+(ly2-ly1+1),context);
  327. while (ly1<=ly2)
  328. {
  329. int tx=x;
  330. for (int fx=lx1;fx<=lx2; fx++, tx++)
  331. {
  332. w32 c=get_pixel(fx, ly1);
  333. if (c!=trans_color)
  334. to->put_pixel(tx+context.xoff, y+ context.yoff, c);
  335. }
  336. ly1++;
  337. y++;
  338. }
  339. }
  340. }
  341. }
  342. }
  343. i4_image_class *i4_create_image(int width, int height, const i4_pal *pal)
  344. {
  345. switch (pal->source.pixel_depth)
  346. {
  347. case I4_32BIT :
  348. return new i4_image32(width, height, pal);
  349. break;
  350. case I4_16BIT :
  351. return new i4_image16(width, height, pal);
  352. break;
  353. case I4_8BIT :
  354. return new i4_image8(width, height, pal);
  355. break;
  356. default:
  357. i4_error("don't know how");
  358. }
  359. return 0;
  360. }
  361. i4_image_class *i4_create_image(int width, int height,
  362. const i4_pal *pal,
  363. void *data,
  364. int bpl)
  365. {
  366. switch (pal->source.pixel_depth)
  367. {
  368. case I4_32BIT :
  369. return new i4_image32(width, height, pal, data, bpl);
  370. break;
  371. case I4_16BIT :
  372. return new i4_image16(width, height, pal, data, bpl);
  373. break;
  374. case I4_8BIT :
  375. return new i4_image8(width, height, pal, data, bpl);
  376. break;
  377. default:
  378. i4_error("don't know how");
  379. }
  380. return 0;
  381. }
  382. i4_image_class *i4_image_class::copy()
  383. {
  384. i4_image_class *im=i4_create_image(width(), height(), pal);
  385. for (int y=0; y<h; y++)
  386. for (int x=0; x<w; x++)
  387. im->put_pixel(x,y, get_pixel(x,y));
  388. return im;
  389. }
  390. void i4_image_class::add_single_dirty(i4_coord x1, i4_coord y1, i4_coord x2,
  391. i4_coord y2, i4_draw_context_class &context)
  392. {
  393. context.add_single_dirty(x1+context.xoff,y1+context.yoff,
  394. x2+context.xoff,y2+context.yoff);
  395. }
  396. void i4_image_class::add_dirty(i4_coord x1, i4_coord y1, i4_coord x2,
  397. i4_coord y2, i4_draw_context_class &context)
  398. {
  399. context.add_both_dirty(x1+context.xoff,y1+context.yoff,
  400. x2+context.xoff,y2+context.yoff);
  401. }
  402. void i4_image_class::rectangle(i4_coord x1, i4_coord y1,
  403. i4_coord x2, i4_coord y2, i4_color color,
  404. i4_draw_context_class &context)
  405. {
  406. bar(x1,y1,x1,y2,color,context);
  407. bar(x2,y1,x2,y2,color,context);
  408. bar(x1+1,y1,x2-1,y1,color,context);
  409. bar(x1+1,y2,x2-1,y2,color,context);
  410. }