morph.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. #include "morph.hpp"
  2. long trans(long x1, long x2, long frame)
  3. {
  4. return (((x2-x1)<<5)*frame+(x1<<8))>>8;
  5. }
  6. void jmorph::show_step_frame(image *screen, int x, int y, int frame_on,
  7. color_filter *fli, palette *pal)
  8. {
  9. short x1,y1,x2,y2;
  10. unsigned char r1,g1,b1,r2,g2,b2;
  11. screen->get_clip(x1,y1,x2,y2);
  12. int i;
  13. long xx,yy;
  14. if (small)
  15. {
  16. morph_point8 *m8=(morph_point8 *)p;
  17. for (i=0;i<total;i++,m8++)
  18. {
  19. xx=x+trans(m8->x1,m8->x2,frame_on);
  20. yy=y+trans(m8->y1,m8->y2,frame_on);
  21. if (xx>=x1 && xx<=x2 && yy>=y1 && yy<=y2)
  22. {
  23. pal->get(m8->start_color,r1,g1,b1);
  24. pal->get(m8->end_color,r2,g2,b2);
  25. long r=trans(r1,r2,frame_on)>>3,
  26. g=trans(g1,g2,frame_on)>>3,
  27. b=trans(b1,b2,frame_on)>>3;
  28. *(screen->scan_line(yy)+xx)=fli->lookup_color(r,g,b);
  29. }
  30. }
  31. }
  32. }
  33. void patched_morph::show_frame(image *screen, int x, int y,
  34. int frame_on, color_filter *fli, palette *pal)
  35. {
  36. jmorph::show_step_frame(screen,x,y,frame_on,fli,pal);
  37. int tot=pats[frame_on].patches,xx,yy;
  38. unsigned char *p=pats[frame_on].patch_data;
  39. short cx1,cy1,cx2,cy2;
  40. screen->get_clip(cx1,cy1,cx2,cy2);
  41. while (tot--)
  42. {
  43. xx=*(p++)+x;
  44. if (xx<cx1 || xx>cx2)
  45. p+=2;
  46. else
  47. {
  48. yy=*(p++)+y;
  49. if (yy<cy1 || yy>cy2)
  50. p++;
  51. else
  52. *(screen->scan_line(yy)+xx)=*(p++);
  53. }
  54. }
  55. }
  56. void patched_morph::show_8(image *screen, int x, int y, int frame_on, color_filter *fli, palette *pal)
  57. {
  58. jmorph::show_8(screen,x,y,frame_on,fli,pal);
  59. int tot=pats[frame_on].patches,xx,yy;
  60. unsigned char *p=pats[frame_on].patch_data;
  61. short cx1,cy1,cx2,cy2;
  62. screen->get_clip(cx1,cy1,cx2,cy2);
  63. while (tot--)
  64. {
  65. xx=*(p++)+x;
  66. if (xx<cx1 || xx>cx2)
  67. p+=2;
  68. else
  69. {
  70. yy=*(p++)+y;
  71. if (yy<cy1 || yy>cy2)
  72. p++;
  73. else
  74. *(screen->scan_line(yy)+xx)=*(p++);
  75. }
  76. }
  77. }
  78. patched_morph::patched_morph(image *i1, image *hint1, image *i2, image *hint2, int aneal_steps,
  79. color_filter *fli, palette *pal, int frames) : jmorph(i1,hint1,i2,hint2,aneal_steps)
  80. {
  81. int i,j,w,h,x,y,tot,r,g,b,mark_color,dist;
  82. unsigned char *cur_patch,*sl,or,og,ob;
  83. frames=9;
  84. patches=frames;
  85. pats=(morph_patch *)jmalloc(sizeof(morph_patch)*patches,"morph::patch array");
  86. w=max(bound_x2(0),bound_x2(1));
  87. h=max(bound_y2(0),bound_y2(1));
  88. image *im=new image(w,h);
  89. for (i=0;i<patches;i++)
  90. {
  91. pats[i].patch_data=NULL;
  92. pats[i].patches=0;
  93. if (i!=0 && i!=patches-1)
  94. {
  95. im->clear();
  96. jmorph::show_step_frame(im,0,0,i,fli,pal);
  97. for (j=0;j<4;j++)
  98. {
  99. for (y=0;y<h;y++)
  100. for (sl=im->scan_line(y),x=0;x<w;x++,sl++)
  101. {
  102. mark_color=-1;
  103. tot=r=g=b=0;
  104. if (x!=0 && *(sl-1))
  105. { tot++;
  106. r+=pal->red(*(sl-1));
  107. g+=pal->green(*(sl-1));
  108. b+=pal->blue(*(sl-1));
  109. }
  110. if (x!=w-1 && *(sl+1))
  111. { tot++;
  112. r+=pal->red(*(sl+1));
  113. g+=pal->green(*(sl+1));
  114. b+=pal->blue(*(sl+1));
  115. }
  116. if (y!=0 && im->pixel(x,y-1))
  117. { tot++;
  118. r+=pal->red(im->pixel(x,y-1));
  119. g+=pal->green(im->pixel(x,y-1));
  120. b+=pal->blue(im->pixel(x,y-1));
  121. }
  122. if (y!=h-1 && im->pixel(x,y+1))
  123. { tot++;
  124. r+=pal->red(im->pixel(x,y+1));
  125. g+=pal->green(im->pixel(x,y+1));
  126. b+=pal->blue(im->pixel(x,y+1));
  127. }
  128. if (*sl && tot==0) // kill any seperate pixels
  129. mark_color=0;
  130. else if (*sl)
  131. {
  132. pal->get(*sl,or,og,ob); // see if we need to blur this on in
  133. r/=tot;
  134. g/=tot;
  135. b/=tot;
  136. dist=((int)or-r)*((int)or-r)+((int)og-g)*((int)og-g)+((int)ob-b)*((int)ob-b);
  137. if (i>0 && i<patches-1 && dist>3000)
  138. {
  139. // printf("adding blur at %d %d to frame %d, dist=%d\n",x,y,i,dist);
  140. mark_color=fli->lookup_color(r>>3,g>>3,b>>3);
  141. }
  142. }
  143. else if (*sl==0 && tot>=3)
  144. mark_color=fli->lookup_color((r/tot)>>3,(g/tot)>>3,(b/tot)>>3);
  145. if (mark_color>=0)
  146. {
  147. pats[i].patches++;
  148. pats[i].patch_data=(unsigned char *)realloc(pats[i].patch_data,3*pats[i].patches);
  149. cur_patch=pats[i].patch_data+ (pats[i].patches-1)*3;
  150. *(cur_patch++)=x;
  151. *(cur_patch++)=y;
  152. *sl=mark_color;
  153. *(cur_patch++)=*sl;
  154. }
  155. }
  156. }
  157. }
  158. }
  159. delete im;
  160. }
  161. void jmorph::show_8(image *screen, int x, int y, int frame_on, color_filter *fli, palette *pal)
  162. {
  163. int pixelx,pixely,i;
  164. short cx1,cy1,cx2,cy2;
  165. unsigned char r,g,b;
  166. unsigned char *scolor,*ecolor,*addr=(unsigned char *)pal->addr();
  167. screen->get_clip(cx1,cy1,cx2,cy2);
  168. if (small)
  169. {
  170. morph_point8 *p_on=(morph_point8 *)p;
  171. for (i=0;i<total;i++,p_on++)
  172. {
  173. pixelx=(long)((int)p_on->x2-(int)p_on->x1)*(long)frame_on/8+p_on->x1+x;
  174. pixely=(long)((int)p_on->y2-(int)p_on->y1)*(long)frame_on/8+p_on->y1+y;
  175. if (pixelx>=cx1 && pixely>=cy1 && pixelx<=cx2 && pixely<=cy2)
  176. {
  177. scolor=addr+((int)p_on->start_color)*3;
  178. ecolor=addr+((int)p_on->end_color)*3;
  179. r=(((int)*(ecolor++))-((int)*scolor))*frame_on/8+*scolor; scolor++;
  180. g=(((int)*(ecolor++))-((int)*scolor))*frame_on/8+*scolor; scolor++;
  181. b=((int)(*ecolor)-(int)(*scolor))*frame_on/8+*scolor;
  182. *(screen->scan_line(pixely)+pixelx)=fli->lookup_color(r>>3,g>>3,b>>3);
  183. }
  184. }
  185. }
  186. else
  187. {
  188. morph_point16 *p_on=(morph_point16 *)p;
  189. for (i=0;i<total;i++,p_on++)
  190. {
  191. pixelx=(long)((int)p_on->x2-(int)p_on->x1)*(long)frame_on/8+p_on->x1+x;
  192. pixely=(long)((int)p_on->y2-(int)p_on->y1)*(long)frame_on/8+p_on->y1+y;
  193. if (pixelx>=cx1 && pixely>=cy1 && pixelx<=cx2 && pixely<=cy2)
  194. {
  195. scolor=addr+((int)p_on->start_color)*3;
  196. ecolor=addr+((int)p_on->end_color)*3;
  197. r=(((int)*(ecolor++))-((int)*scolor))*frame_on/8+*scolor; scolor++;
  198. g=(((int)*(ecolor++))-((int)*scolor))*frame_on/8+*scolor; scolor++;
  199. b=((int)(*ecolor)-(int)(*scolor))*frame_on/8+*scolor;
  200. *(screen->scan_line(pixely)+pixelx)=fli->lookup_color(r>>3,g>>3,b>>3);
  201. }
  202. }
  203. }
  204. }
  205. void jmorph::add_filler(int frames)
  206. {
  207. int w=max(bound_x2(0),bound_x2(1)),h=max(bound_y2(0),bound_y2(1)),
  208. i,pixelx,pixely,k,l,frame_on;
  209. morph_point8 **middle_map,*other;
  210. unsigned char *end_map;
  211. CONDITION(small,"add filler not defined for 16bit morphs\n");
  212. if (frames<3) return ;
  213. middle_map=(morph_point8 **)jmalloc(w*h*sizeof(morph_point8 *),
  214. "morph::middle_map"); // create an image of pointers
  215. end_map=(unsigned char *)jmalloc(w*h,
  216. "morph::end_map"); // maps all the ending pixels
  217. for (frame_on=2;frame_on<frames-1;frame_on++)
  218. {
  219. memset(middle_map,0,w*h*sizeof(morph_point8 *)); // initialize them middle pointers NULL
  220. memset(end_map,0,w*h); // clear all end pixels
  221. morph_point8 *p_on=(morph_point8 *)p; // p is the array of morph points
  222. for (i=0;i<total;i++,p_on++)
  223. {
  224. pixelx=(long)((int)p_on->x2-(int)p_on->x1)*(long) frame_on /(long) frames +p_on->x1;
  225. pixely=(long)((int)p_on->y2-(int)p_on->y1)*(long) frame_on /(long) frames +p_on->y1;
  226. middle_map[pixelx+pixely*w]=p_on; // map this poisiton back to the morph point
  227. end_map[p_on->x2+p_on->y2*w]=p_on->end_color; // note an ending map here
  228. }
  229. int skipped=0;
  230. for (pixely=0;pixely<h;pixely++)
  231. {
  232. for (pixelx=0;pixelx<w;pixelx++)
  233. {
  234. if (middle_map[pixelx+pixely*w]==NULL) // we are checking for 'duds' (misplaced black pixels)
  235. {
  236. int tot;
  237. if (pixelx>0) if (middle_map[pixelx-1+pixely*w]) tot=1; else tot=0;
  238. if (pixelx<w-1) if (middle_map[pixelx+1+pixely*w]) tot++;
  239. if (pixely>0) if (middle_map[pixelx+(pixely-1)*w]) tot++;
  240. if (pixely<h-1) if (middle_map[pixelx+(pixely+1)*w]) tot++;
  241. if (tot>=3) // it is surronded by 3 non black squares, this is a dud
  242. {
  243. int distance,shortest_distance,shortest_end_x,shortest_end_y;
  244. morph_point8 *shortest=NULL;
  245. for (k=0;k<w;k++)
  246. for (l=0;l<h;l++)
  247. {
  248. other=middle_map[k+(l)*w];
  249. if (other)
  250. {
  251. int end_x=frames*(pixelx-other->x1)/frame_on+other->x1,
  252. end_y=frames*(pixely-other->y1)/frame_on+other->y1;
  253. if (end_x>=0 && end_y>=0 && end_x<w && end_y<h && end_map[end_x+end_y*w])
  254. {
  255. distance=(other->x1-end_x)*(other->x1-end_x)+
  256. (other->y1-end_y)*(other->y1-end_y);
  257. if (!shortest || shortest_distance>distance)
  258. {
  259. shortest_distance=distance;
  260. shortest=other;
  261. shortest_end_x=end_x;
  262. shortest_end_y=end_y;
  263. }
  264. }
  265. }
  266. }
  267. if (shortest)
  268. {
  269. total++;
  270. p=(void *)realloc(p,sizeof(morph_point8)*total);
  271. morph_point8 *mod=((morph_point8 *)p)+total-1;
  272. mod->x1=shortest->x1;
  273. mod->y1=shortest->y1;
  274. mod->start_color=shortest->start_color;
  275. mod->x2=shortest_end_x;
  276. mod->y2=shortest_end_y;
  277. mod->end_color=end_map[shortest_end_x+shortest_end_y*w];
  278. }
  279. else
  280. {
  281. skipped++;
  282. printf("skiped so far : %d (frame %d)\n",skipped,frame_on);
  283. }
  284. }
  285. }
  286. }
  287. }
  288. }
  289. jfree(middle_map);
  290. jfree(end_map);
  291. }
  292. jmorph::jmorph(spec_entry *e, bFILE *fp)
  293. {
  294. int i;
  295. fp->seek(e->offset,0);
  296. fp->read(&total,4);
  297. total=long_to_local(total);
  298. if (e->type==SPEC_MORPH_POINTS_8 || e->type==SPEC_PATCHED_MORPH)
  299. {
  300. p=(void *)jmalloc(sizeof(morph_point8)*total,"morph8::point array");
  301. fp->read(p,sizeof(morph_point8)*total);
  302. small=1;
  303. }
  304. else
  305. {
  306. p=(void *)jmalloc(sizeof(morph_point16)*total,"morph16::point array");
  307. for (i=0;i<total;i++)
  308. {
  309. ((morph_point16 *)p+i)->x1=fp->read_short();
  310. ((morph_point16 *)p+i)->y1=fp->read_short();
  311. ((morph_point16 *)p+i)->x2=fp->read_short();
  312. ((morph_point16 *)p+i)->y2=fp->read_short();
  313. fp->read( &((morph_point16 *)p+i)->start_color,1);
  314. fp->read( &((morph_point16 *)p+i)->end_color,1);
  315. }
  316. small=0;
  317. }
  318. w[0]=fp->read_short();
  319. h[0]=fp->read_short();
  320. w[1]=fp->read_short();
  321. h[1]=fp->read_short();
  322. }
  323. void jmorph::show_frame(image *screen, int x, int y,
  324. int frames, int frame_on, color_filter *fli, palette *pal)
  325. {
  326. int pixelx,pixely,i;
  327. short cx1,cy1,cx2,cy2;
  328. unsigned char r,g,b;
  329. unsigned char *scolor,*ecolor,*addr=(unsigned char *)pal->addr();
  330. screen->get_clip(cx1,cy1,cx2,cy2);
  331. if (small)
  332. {
  333. morph_point8 *p_on=(morph_point8 *)p;
  334. for (i=0;i<total;i++,p_on++)
  335. {
  336. pixelx=(long)((int)p_on->x2-(int)p_on->x1)*(long)frame_on/(long)frames+p_on->x1+x;
  337. pixely=(long)((int)p_on->y2-(int)p_on->y1)*(long)frame_on/(long)frames+p_on->y1+y;
  338. if (pixelx>=cx1 && pixely>=cy1 && pixelx<=cx2 && pixely<=cy2)
  339. {
  340. scolor=addr+((int)p_on->start_color)*3;
  341. ecolor=addr+((int)p_on->end_color)*3;
  342. r=(((int)*(ecolor++))-((int)*scolor))*frame_on/frames+*scolor; scolor++;
  343. g=(((int)*(ecolor++))-((int)*scolor))*frame_on/frames+*scolor; scolor++;
  344. b=((int)(*ecolor)-(int)(*scolor))*frame_on/frames+*scolor;
  345. *(screen->scan_line(pixely)+pixelx)=fli->lookup_color(r>>3,g>>3,b>>3);
  346. }
  347. }
  348. }
  349. else
  350. {
  351. morph_point16 *p_on=(morph_point16 *)p;
  352. for (i=0;i<total;i++,p_on++)
  353. {
  354. pixelx=(long)((int)p_on->x2-(int)p_on->x1)*(long)frame_on/(long)frames+p_on->x1+x;
  355. pixely=(long)((int)p_on->y2-(int)p_on->y1)*(long)frame_on/(long)frames+p_on->y1+y;
  356. if (pixelx>=cx1 && pixely>=cy1 && pixelx<=cx2 && pixely<=cy2)
  357. {
  358. scolor=addr+((int)p_on->start_color)*3;
  359. ecolor=addr+((int)p_on->end_color)*3;
  360. r=(((int)*(ecolor++))-((int)*scolor))*frame_on/frames+*scolor; scolor++;
  361. g=(((int)*(ecolor++))-((int)*scolor))*frame_on/frames+*scolor; scolor++;
  362. b=((int)(*ecolor)-(int)(*scolor))*frame_on/frames+*scolor;
  363. *(screen->scan_line(pixely)+pixelx)=fli->lookup_color(r>>3,g>>3,b>>3);
  364. }
  365. }
  366. }
  367. }
  368. void jmorph::show_24frame(unsigned char *screen, int width, int height,
  369. int x, int y, int frames, int frame_on, palette *pal)
  370. {
  371. int pixelx,pixely,i;
  372. unsigned char *scolor,*ecolor,*addr=(unsigned char *)pal->addr(),*pix;
  373. if (small)
  374. {
  375. morph_point8 *p_on=(morph_point8 *)p;
  376. for (i=0;i<total;i++,p_on++)
  377. {
  378. pixelx=(long)((int)p_on->x2-(int)p_on->x1)*(long)frame_on/(long)frames+p_on->x1+x;
  379. pixely=(long)((int)p_on->y2-(int)p_on->y1)*(long)frame_on/(long)frames+p_on->y1+y;
  380. if (pixelx>=0 && pixely>=0 && pixelx<width && pixely<height)
  381. {
  382. scolor=addr+((int)p_on->start_color)*3;
  383. ecolor=addr+((int)p_on->end_color)*3;
  384. pix=screen+pixelx*3+pixely*3*width;
  385. *pix=(((int)*(ecolor++))-((int)*scolor))*frame_on/frames+*scolor; scolor++; pix++;
  386. *pix=(((int)*(ecolor++))-((int)*scolor))*frame_on/frames+*scolor; scolor++; pix++;
  387. *pix=((int)(*ecolor)-(int)(*scolor))*frame_on/frames+*scolor;
  388. }
  389. }
  390. }
  391. else
  392. {
  393. morph_point16 *p_on=(morph_point16 *)p;
  394. for (i=0;i<total;i++,p_on++)
  395. {
  396. pixelx=(long)((int)p_on->x2-(int)p_on->x1)*(long)frame_on/(long)frames+p_on->x1+x;
  397. pixely=(long)((int)p_on->y2-(int)p_on->y1)*(long)frame_on/(long)frames+p_on->y1+y;
  398. if (pixelx>=0 && pixely>=0 && pixelx<width && pixely<height)
  399. {
  400. scolor=addr+((int)p_on->start_color)*3;
  401. ecolor=addr+((int)p_on->end_color)*3;
  402. pix=screen+pixelx*3+pixely*3*width;
  403. *pix=(((int)*(ecolor++))-((int)*scolor))*frame_on/frames+*scolor; scolor++; pix++;
  404. *pix=(((int)*(ecolor++))-((int)*scolor))*frame_on/frames+*scolor; scolor++; pix++;
  405. *pix=((int)(*ecolor)-(int)(*scolor))*frame_on/frames+*scolor;
  406. }
  407. }
  408. }
  409. }
  410. jmorph::jmorph(image *i1, image *hint1, image *i2, image *hint2,
  411. int aneal_steps)
  412. {
  413. w[0]=i1->width();
  414. h[0]=i1->height();
  415. w[1]=i2->width();
  416. h[1]=i2->height();
  417. struct { int start,end; } crange[256];
  418. int hint_hist1[256],hint_hist2[256],x,y,total_hints=0,randp,cur_pix,
  419. last=0,first=0,i,j,k,findx1,findy1,findx2,findy2,total1,total2,points;
  420. unsigned char *sl,color;
  421. void *plist;
  422. CONDITION(i1->width()==hint1->width() && i1->height()==hint1->height() &&
  423. i2->width()==hint2->width() && i2->height()==hint2->height(),
  424. "Image sizes do not correspond with hint sizes");
  425. if (i1->width()>255 || i2->width()>255 || i1->height()>255 || i2->height()>255)
  426. small=0;
  427. else small=1;
  428. memset(hint_hist1,0,sizeof(hint_hist1));
  429. memset(hint_hist2,0,sizeof(hint_hist2));
  430. total=0;
  431. // first found out how many hints we have to follow
  432. for (y=0;y<hint1->height();y++)
  433. { sl=hint1->scan_line(y);
  434. for (x=hint1->width();x;x--,sl++)
  435. if (*sl) hint_hist1[*sl]++;
  436. }
  437. for (y=0;y<hint2->height();y++)
  438. { sl=hint2->scan_line(y);
  439. for (x=hint2->width();x;x--,sl++)
  440. if (*sl) hint_hist2[*sl]++;
  441. }
  442. // check the user and see if the mask match up
  443. for (x=0;x<256;x++)
  444. {
  445. if ((hint_hist1[x]!=0 && hint_hist2[x]==0) ||
  446. (hint_hist1[x]==0 && hint_hist2[x]!=0))
  447. { printf("Color %d used for hinting is prent in one hint mask, but not the other\n",x);
  448. exit(1);
  449. } else if (hint_hist1[x])
  450. {
  451. if (!first) first=x;
  452. total_hints++;
  453. if (hint_hist1[x]>hint_hist2[x])
  454. y=hint_hist1[x];
  455. else
  456. y=hint_hist2[x];
  457. total+=y;
  458. crange[x].start=last;
  459. crange[x].end=last+y-1;
  460. last+=y;
  461. }
  462. }
  463. if (small)
  464. plist=(void *)jmalloc(sizeof(morph_point8)*total,"morph8::point array");
  465. else
  466. plist=(void *)jmalloc(sizeof(morph_point16)*total,"morph16::point array");
  467. CHECK(plist);
  468. for (i=0,color=0;i<total_hints;i++)
  469. {
  470. color++; while (!hint_hist1[color]) color++;
  471. findx1=findx2=-1; findy1=findy2=0;
  472. total1=hint_hist1[color];
  473. total2=hint_hist2[color];
  474. if (total1>total2) points=total1; else points=total2;
  475. for (j=0;j<points;j++)
  476. {
  477. if (total1) // are there any more pixels left in this image?
  478. { total1--;
  479. do {
  480. findx1++;
  481. if (findx1>=hint1->width()) { findx1=0; findy1++; }
  482. } while (hint1->pixel(findx1,findy1)!=color);
  483. }
  484. else
  485. {
  486. randp=rand()%j+crange[color].start;
  487. if (small)
  488. { findx1=((morph_point8 *)plist)[randp].x1;
  489. findy1=((morph_point8 *)plist)[randp].y1;
  490. } else
  491. { findx1=((morph_point16 *)plist)[randp].x1;
  492. findy1=((morph_point16 *)plist)[randp].y1;
  493. }
  494. }
  495. if (total2) // are there any more pixels left in this image?
  496. {
  497. total2--;
  498. do {
  499. findx2++;
  500. if (findx2>=hint2->width()) { findx2=0; findy2++; }
  501. } while (hint2->pixel(findx2,findy2)!=color);
  502. }
  503. else
  504. {
  505. randp=rand()%j+crange[color].start;
  506. if (small)
  507. { findx2=((morph_point8 *)plist)[randp].x2;
  508. findy2=((morph_point8 *)plist)[randp].y2;
  509. } else
  510. { findx2=((morph_point16 *)plist)[randp].x2;
  511. findy2=((morph_point16 *)plist)[randp].y2;
  512. }
  513. }
  514. cur_pix=j+crange[color].start;
  515. CHECK(cur_pix<total);
  516. if (small)
  517. {
  518. ((morph_point8 *)plist)[cur_pix].x1=findx1;
  519. ((morph_point8 *)plist)[cur_pix].x2=findx2;
  520. ((morph_point8 *)plist)[cur_pix].y1=findy1;
  521. ((morph_point8 *)plist)[cur_pix].y2=findy2;
  522. ((morph_point8 *)plist)[cur_pix].start_color=i1->pixel(findx1,findy1);
  523. ((morph_point8 *)plist)[cur_pix].end_color=i2->pixel(findx2,findy2);
  524. }
  525. else
  526. {
  527. ((morph_point16 *)plist)[cur_pix].x1=findx1;
  528. ((morph_point16 *)plist)[cur_pix].x2=findx2;
  529. ((morph_point16 *)plist)[cur_pix].y1=findy1;
  530. ((morph_point16 *)plist)[cur_pix].y2=findy2;
  531. ((morph_point16 *)plist)[cur_pix].start_color=i1->pixel(findx1,findy1);
  532. ((morph_point16 *)plist)[cur_pix].end_color=i2->pixel(findx2,findy2);
  533. }
  534. }
  535. }
  536. int swap_point,distance,new_distance,hint_color,first_point,range,start;
  537. int sx2,sy2,sec;
  538. for (i=0;i<aneal_steps;i++)
  539. {
  540. for (j=0,first_point=0;j<total_hints;j++)
  541. {
  542. if (small)
  543. hint_color=hint1->pixel(((morph_point8 *)plist)[first_point].x1,((morph_point8 *)plist)[first_point].y1);
  544. else
  545. hint_color=hint1->pixel(((morph_point16 *)plist)[first_point].x1,((morph_point16 *)plist)[first_point].y1);
  546. start=crange[hint_color].start;
  547. range=crange[hint_color].end-start+1;
  548. for(k=crange[hint_color].start;k<=crange[hint_color].end;k++)
  549. {
  550. swap_point=rand()%range+start;
  551. if (small)
  552. {
  553. morph_point8 *pk,*ps;
  554. pk=(morph_point8 *)plist+k;
  555. ps=(morph_point8 *)plist+swap_point;
  556. distance=(pk->x2-pk->x1)*(pk->x2-pk->x1)+(pk->y2-pk->y1)*(pk->y2-pk->y1)+
  557. (ps->x2-ps->x1)*(ps->x2-ps->x1)+(ps->y2-ps->y1)*(ps->y2-ps->y1);
  558. new_distance=(ps->x2-pk->x1)*(ps->x2-pk->x1)+(ps->y2-pk->y1)*(ps->y2-pk->y1)+
  559. (pk->x2-ps->x1)*(pk->x2-ps->x1)+(pk->y2-ps->y1)*(pk->y2-ps->y1);
  560. if (new_distance<distance)
  561. {
  562. sx2=pk->x2; sy2=pk->y2;
  563. sec=pk->end_color;
  564. pk->x2=ps->x2; pk->y2=ps->y2;
  565. pk->end_color=ps->end_color;
  566. ps->x2=sx2; ps->y2=sy2;
  567. ps->end_color=sec;
  568. }
  569. } else
  570. {
  571. morph_point16 *pk,*ps;
  572. pk=(morph_point16 *)plist+k;
  573. ps=(morph_point16 *)plist+swap_point;
  574. distance=(pk->x2-pk->x1)*(pk->x2-pk->x1)+(pk->y2-pk->y1)*(pk->y2-pk->y1)+
  575. (ps->x2-ps->x1)*(ps->x2-ps->x1)+(ps->y2-ps->y1)*(ps->y2-ps->y1);
  576. new_distance=(ps->x2-pk->x1)*(ps->x2-pk->x1)+(ps->y2-pk->y1)*(ps->y2-pk->y1)+
  577. (pk->x2-ps->x1)*(pk->x2-ps->x1)+(pk->y2-ps->y1)*(pk->y2-ps->y1);
  578. if (new_distance<distance)
  579. {
  580. sx2=pk->x2; sy2=pk->y2;
  581. sec=pk->end_color;
  582. pk->x2=ps->x2; pk->y2=ps->y2;
  583. pk->end_color=ps->end_color;
  584. ps->x2=sx2; ps->y2=sy2;
  585. ps->end_color=sec;
  586. }
  587. }
  588. }
  589. first_point=crange[hint_color].end+1;
  590. }
  591. }
  592. p=plist;
  593. }
  594. int morph_width;
  595. int morph8_compare(const void *p1, const void *p2)
  596. {
  597. int pos1=((morph_point8 *)p1)->x1+((morph_point8 *)p1)->y1*morph_width;
  598. int pos2=((morph_point8 *)p2)->x1+((morph_point8 *)p2)->y1*morph_width;
  599. if (pos1<pos2) return -1;
  600. else if (pos1==pos2) return 0;
  601. else return 1;
  602. }
  603. int jmorph::write(bFILE *fp)
  604. {
  605. int i;
  606. fp->write_long(total);
  607. if (small)
  608. {
  609. // if the points are put in order then they can be compressed easier
  610. morph_width=max(bound_x2(0),bound_x2(1)); // global used by qsort compare routine
  611. qsort(p,total,sizeof(morph_point8),morph8_compare);
  612. fp->write(p,sizeof(morph_point8)*total);
  613. }
  614. else
  615. {
  616. for (i=0;i<total;i++)
  617. { fp->write_short(((morph_point16 *)p+i)->x1);
  618. fp->write_short(((morph_point16 *)p+i)->y1);
  619. fp->write_short(((morph_point16 *)p+i)->x2);
  620. fp->write_short(((morph_point16 *)p+i)->y2);
  621. fp->write( &((morph_point16 *)p+i)->start_color,1);
  622. fp->write( &((morph_point16 *)p+i)->end_color,1);
  623. }
  624. }
  625. fp->write_short(w[0]);
  626. fp->write_short(h[0]);
  627. fp->write_short(w[1]);
  628. fp->write_short(h[1]);
  629. return 1;
  630. }
  631. step_morph::step_morph(patched_morph *mor, palette *pal, int frame_direction, int face_direction)
  632. {
  633. int i;
  634. pm=mor;
  635. total=mor->total_points();
  636. points=(step_struct *)jmalloc(sizeof(step_struct)*total,"step_morph::points");
  637. dir=frame_direction;
  638. face_dir=face_direction;
  639. if (dir>0)
  640. frame_on=0;
  641. else
  642. frame_on=mor->patches-1;
  643. if (mor->small_morph())
  644. {
  645. int x1,y1,x2,y2;
  646. if (frame_direction>0)
  647. {
  648. morph_point8 *m8=mor->small_points();
  649. for (i=0;i<total;i++,m8++)
  650. {
  651. x1=(int)m8->x1;
  652. x2=(int)m8->x2;
  653. y1=(int)m8->y1;
  654. y2=(int)m8->y2;
  655. points[i].x=x1<<8;
  656. points[i].y=y1<<8;
  657. points[i].dx=(x2-x1)<<5;
  658. points[i].dy=(y2-y1)<<5;
  659. unsigned char r1,g1,b1,r2,g2,b2;
  660. pal->get(m8->start_color,r1,g1,b1);
  661. pal->get(m8->end_color,r2,g2,b2);
  662. points[i].r=r1<<8;
  663. points[i].g=g1<<8;
  664. points[i].b=b1<<8;
  665. points[i].dr=(long)((int)r2-(int)r1)<<5;
  666. points[i].dg=(long)((int)g2-(int)g1)<<5;
  667. points[i].db=(long)((int)b2-(int)b1)<<5;
  668. }
  669. }
  670. else
  671. {
  672. morph_point8 *m8=mor->small_points();
  673. for (i=0;i<total;i++,m8++)
  674. {
  675. x1=(int)m8->x1;
  676. x2=(int)m8->x2;
  677. y1=(int)m8->y1;
  678. y2=(int)m8->y2;
  679. points[i].x=x2<<8;
  680. points[i].y=y2<<8;
  681. points[i].dx=(x1-x2)<<5;
  682. points[i].dy=(y1-y2)<<5;
  683. unsigned char r1,g1,b1,r2,g2,b2;
  684. pal->get(m8->start_color,r2,g2,b2);
  685. pal->get(m8->end_color,r1,g1,b1);
  686. points[i].r=r1<<8;
  687. points[i].g=g1<<8;
  688. points[i].b=b1<<8;
  689. points[i].dr=(long)((int)r2-(int)r1)<<5;
  690. points[i].dg=(long)((int)g2-(int)g1)<<5;
  691. points[i].db=(long)((int)b2-(int)b1)<<5;
  692. }
  693. }
  694. }
  695. }
  696. void step_morph::show_frame(image *screen, int x, int y, color_filter *fli)
  697. {
  698. short x1,y1,x2,y2;
  699. screen->get_clip(x1,y1,x2,y2);
  700. int i,px,py,ww=max(pm->bound_x2(0),pm->bound_x2(1))-1;
  701. step_struct *ss;
  702. morph_patch *pat=pm->pats+frame_on;
  703. int j,tp=pat->patches;
  704. unsigned char *sl=pat->patch_data;
  705. if (face_dir>0)
  706. {
  707. for (i=0,ss=points;i<total;i++,ss++)
  708. {
  709. px=x+(ss->x>>(16-8));
  710. py=y+(ss->y>>(16-8));
  711. if (px>=x1 && px<=x2 && py>=y1 && py<=y2)
  712. *(screen->scan_line(py)+px)=fli->lookup_color(ss->r>>(19-8),ss->g>>(19-8),ss->b>>(19-8));
  713. ss->x+=ss->dx;
  714. ss->y+=ss->dy;
  715. ss->r+=ss->dr;
  716. ss->g+=ss->dg;
  717. ss->b+=ss->db;
  718. }
  719. for (j=0;j<tp;j++,sl++)
  720. {
  721. px=x+*(sl++);
  722. py=y+*(sl++);
  723. if (px>=x1 && px<=x2 && py>=y1 && py<=y2)
  724. *(screen->scan_line(py)+px)=*sl;
  725. }
  726. } else
  727. {
  728. for (i=0,ss=points;i<total;i++,ss++)
  729. {
  730. px=x+ww-(ss->x>>(16-8));
  731. py=y+(ss->y>>(16-8));
  732. if (px>=x1 && px<=x2 && py>=y1 && py<=y2)
  733. *(screen->scan_line(py)+px)=fli->lookup_color(ss->r>>(19-8),ss->g>>(19-8),ss->b>>(19-8));
  734. ss->x+=ss->dx;
  735. ss->y+=ss->dy;
  736. ss->r+=ss->dr;
  737. ss->g+=ss->dg;
  738. ss->b+=ss->db;
  739. }
  740. for (j=0;j<tp;j++,sl++)
  741. {
  742. px=x+(ww-(int)(*(sl++)));
  743. py=y+*(sl++);
  744. if (px>=x1 && px<=x2 && py>=y1 && py<=y2)
  745. *(screen->scan_line(py)+px)=*sl;
  746. }
  747. }
  748. frame_on+=dir;
  749. }
  750. void step_morph::reverse_direction()
  751. {
  752. step_struct *s=points;
  753. int i;
  754. for (i=0;i<total;i++,s++)
  755. {
  756. s->dx=-s->dx;
  757. s->dy=-s->dy;
  758. s->dr=-s->dr;
  759. s->dg=-s->dg;
  760. s->db=-s->db;
  761. }
  762. dir=-dir;
  763. }
  764. patched_morph::patched_morph(spec_entry *e, bFILE *fp) : jmorph(e,fp)
  765. {
  766. int i;
  767. patches=fp->read_short();
  768. pats=(morph_patch *)jmalloc(sizeof(morph_patch)*patches,"patched_morph::points");
  769. for (i=0;i<patches;i++)
  770. {
  771. pats[i].patches=fp->read_short();
  772. if (pats[i].patches)
  773. {
  774. pats[i].patch_data=(unsigned char *)jmalloc(3*pats[i].patches,"patched_morph::patch_data");
  775. fp->read(pats[i].patch_data,3*pats[i].patches);
  776. }
  777. else
  778. pats[i].patch_data=NULL;
  779. }
  780. }