ximadsp.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 55k
Category:

Graph program

Development Platform:

Visual C++

  1. // xImaDsp.cpp : DSP functions
  2. /* 07/08/2001 v1.00 - Davide Pizzolato - www.xdp.it
  3.  * CxImage version 5.99a 08/Feb/2004
  4.  */
  5. #include "ximage.h"
  6. #include "ximaiter.h"
  7. #if CXIMAGE_SUPPORT_DSP
  8. ////////////////////////////////////////////////////////////////////////////////
  9. bool CxImage::Threshold(BYTE level)
  10. {
  11. if (!pDib) return false;
  12. if (head.biBitCount == 1) return true;
  13. GrayScale();
  14. CxImage tmp(head.biWidth,head.biHeight,1);
  15. for (long y=0;y<head.biHeight;y++){
  16. info.nProgress = (long)(100*y/head.biHeight);
  17. if (info.nEscape) break;
  18. for (long x=0;x<head.biWidth;x++){
  19. if (GetPixelIndex(x,y)>level)
  20. tmp.SetPixelIndex(x,y,1);
  21. else
  22. tmp.SetPixelIndex(x,y,0);
  23. }
  24. }
  25. tmp.SetPaletteColor(0,0,0,0);
  26. tmp.SetPaletteColor(1,255,255,255);
  27. Transfer(tmp);
  28. return true;
  29. }
  30. ////////////////////////////////////////////////////////////////////////////////
  31. bool CxImage::SplitRGB(CxImage* r,CxImage* g,CxImage* b)
  32. {
  33. if (!pDib) return false;
  34. if (r==NULL && g==NULL && b==NULL) return false;
  35. CxImage tmpr(head.biWidth,head.biHeight,8);
  36. CxImage tmpg(head.biWidth,head.biHeight,8);
  37. CxImage tmpb(head.biWidth,head.biHeight,8);
  38. RGBQUAD color;
  39. for(long y=0; y<head.biHeight; y++){
  40. for(long x=0; x<head.biWidth; x++){
  41. color = GetPixelColor(x,y);
  42. if (r) tmpr.SetPixelIndex(x,y,color.rgbRed);
  43. if (g) tmpg.SetPixelIndex(x,y,color.rgbGreen);
  44. if (b) tmpb.SetPixelIndex(x,y,color.rgbBlue);
  45. }
  46. }
  47. if (r) tmpr.SetGrayPalette();
  48. if (g) tmpg.SetGrayPalette();
  49. if (b) tmpb.SetGrayPalette();
  50. /*for(long j=0; j<256; j++){
  51. BYTE i=(BYTE)j;
  52. if (r) tmpr.SetPaletteColor(i,i,0,0);
  53. if (g) tmpg.SetPaletteColor(i,0,i,0);
  54. if (b) tmpb.SetPaletteColor(i,0,0,i);
  55. }*/
  56. if (r) r->Transfer(tmpr);
  57. if (g) g->Transfer(tmpg);
  58. if (b) b->Transfer(tmpb);
  59. return true;
  60. }
  61. ////////////////////////////////////////////////////////////////////////////////
  62. bool CxImage::SplitCMYK(CxImage* c,CxImage* m,CxImage* y,CxImage* k)
  63. {
  64. if (!pDib) return false;
  65. if (c==NULL && m==NULL && y==NULL && k==NULL) return false;
  66. CxImage tmpc(head.biWidth,head.biHeight,8);
  67. CxImage tmpm(head.biWidth,head.biHeight,8);
  68. CxImage tmpy(head.biWidth,head.biHeight,8);
  69. CxImage tmpk(head.biWidth,head.biHeight,8);
  70. RGBQUAD color;
  71. for(long yy=0; yy<head.biHeight; yy++){
  72. for(long xx=0; xx<head.biWidth; xx++){
  73. color = GetPixelColor(xx,yy);
  74. if (c) tmpc.SetPixelIndex(xx,yy,(BYTE)(255-color.rgbRed));
  75. if (m) tmpm.SetPixelIndex(xx,yy,(BYTE)(255-color.rgbGreen));
  76. if (y) tmpy.SetPixelIndex(xx,yy,(BYTE)(255-color.rgbBlue));
  77. if (k) tmpk.SetPixelIndex(xx,yy,(BYTE)RGB2GRAY(color.rgbRed,color.rgbGreen,color.rgbBlue));
  78. }
  79. }
  80. if (c) tmpc.SetGrayPalette();
  81. if (m) tmpm.SetGrayPalette();
  82. if (y) tmpy.SetGrayPalette();
  83. if (k) tmpk.SetGrayPalette();
  84. if (c) c->Transfer(tmpc);
  85. if (m) m->Transfer(tmpm);
  86. if (y) y->Transfer(tmpy);
  87. if (k) k->Transfer(tmpk);
  88. return true;
  89. }
  90. ////////////////////////////////////////////////////////////////////////////////
  91. bool CxImage::SplitYUV(CxImage* y,CxImage* u,CxImage* v)
  92. {
  93. if (!pDib) return false;
  94. if (y==NULL && u==NULL && v==NULL) return false;
  95. CxImage tmpy(head.biWidth,head.biHeight,8);
  96. CxImage tmpu(head.biWidth,head.biHeight,8);
  97. CxImage tmpv(head.biWidth,head.biHeight,8);
  98. RGBQUAD color;
  99. for(long yy=0; yy<head.biHeight; yy++){
  100. for(long x=0; x<head.biWidth; x++){
  101. color = RGBtoYUV(GetPixelColor(x,yy));
  102. if (y) tmpy.SetPixelIndex(x,yy,color.rgbRed);
  103. if (u) tmpu.SetPixelIndex(x,yy,color.rgbGreen);
  104. if (v) tmpv.SetPixelIndex(x,yy,color.rgbBlue);
  105. }
  106. }
  107. if (y) tmpy.SetGrayPalette();
  108. if (u) tmpu.SetGrayPalette();
  109. if (v) tmpv.SetGrayPalette();
  110. if (y) y->Transfer(tmpy);
  111. if (u) u->Transfer(tmpu);
  112. if (v) v->Transfer(tmpv);
  113. return true;
  114. }
  115. ////////////////////////////////////////////////////////////////////////////////
  116. bool CxImage::SplitYIQ(CxImage* y,CxImage* i,CxImage* q)
  117. {
  118. if (!pDib) return false;
  119. if (y==NULL && i==NULL && q==NULL) return false;
  120. CxImage tmpy(head.biWidth,head.biHeight,8);
  121. CxImage tmpi(head.biWidth,head.biHeight,8);
  122. CxImage tmpq(head.biWidth,head.biHeight,8);
  123. RGBQUAD color;
  124. for(long yy=0; yy<head.biHeight; yy++){
  125. for(long x=0; x<head.biWidth; x++){
  126. color = RGBtoYIQ(GetPixelColor(x,yy));
  127. if (y) tmpy.SetPixelIndex(x,yy,color.rgbRed);
  128. if (i) tmpi.SetPixelIndex(x,yy,color.rgbGreen);
  129. if (q) tmpq.SetPixelIndex(x,yy,color.rgbBlue);
  130. }
  131. }
  132. if (y) tmpy.SetGrayPalette();
  133. if (i) tmpi.SetGrayPalette();
  134. if (q) tmpq.SetGrayPalette();
  135. if (y) y->Transfer(tmpy);
  136. if (i) i->Transfer(tmpi);
  137. if (q) q->Transfer(tmpq);
  138. return true;
  139. }
  140. ////////////////////////////////////////////////////////////////////////////////
  141. bool CxImage::SplitXYZ(CxImage* x,CxImage* y,CxImage* z)
  142. {
  143. if (!pDib) return false;
  144. if (x==NULL && y==NULL && z==NULL) return false;
  145. CxImage tmpx(head.biWidth,head.biHeight,8);
  146. CxImage tmpy(head.biWidth,head.biHeight,8);
  147. CxImage tmpz(head.biWidth,head.biHeight,8);
  148. RGBQUAD color;
  149. for(long yy=0; yy<head.biHeight; yy++){
  150. for(long xx=0; xx<head.biWidth; xx++){
  151. color = RGBtoXYZ(GetPixelColor(xx,yy));
  152. if (x) tmpx.SetPixelIndex(xx,yy,color.rgbRed);
  153. if (y) tmpy.SetPixelIndex(xx,yy,color.rgbGreen);
  154. if (z) tmpz.SetPixelIndex(xx,yy,color.rgbBlue);
  155. }
  156. }
  157. if (x) tmpx.SetGrayPalette();
  158. if (y) tmpy.SetGrayPalette();
  159. if (z) tmpz.SetGrayPalette();
  160. if (x) x->Transfer(tmpx);
  161. if (y) y->Transfer(tmpy);
  162. if (z) z->Transfer(tmpz);
  163. return true;
  164. }////////////////////////////////////////////////////////////////////////////////
  165. bool CxImage::SplitHSL(CxImage* h,CxImage* s,CxImage* l)
  166. {
  167. if (!pDib) return false;
  168. if (h==NULL && s==NULL && l==NULL) return false;
  169. CxImage tmph(head.biWidth,head.biHeight,8);
  170. CxImage tmps(head.biWidth,head.biHeight,8);
  171. CxImage tmpl(head.biWidth,head.biHeight,8);
  172. RGBQUAD color;
  173. for(long y=0; y<head.biHeight; y++){
  174. for(long x=0; x<head.biWidth; x++){
  175. color = RGBtoHSL(GetPixelColor(x,y));
  176. if (h) tmph.SetPixelIndex(x,y,color.rgbRed);
  177. if (s) tmps.SetPixelIndex(x,y,color.rgbGreen);
  178. if (l) tmpl.SetPixelIndex(x,y,color.rgbBlue);
  179. }
  180. }
  181. if (h) tmph.SetGrayPalette();
  182. if (s) tmps.SetGrayPalette();
  183. if (l) tmpl.SetGrayPalette();
  184. /* pseudo-color generator for hue channel (visual debug)
  185. if (h) for(long j=0; j<256; j++){
  186. BYTE i=(BYTE)j;
  187. RGBQUAD hsl={120,240,i,0};
  188. tmph.SetPaletteColor(i,HSLtoRGB(hsl));
  189. }*/
  190. if (h) h->Transfer(tmph);
  191. if (s) s->Transfer(tmps);
  192. if (l) l->Transfer(tmpl);
  193. return true;
  194. }
  195. ////////////////////////////////////////////////////////////////////////////////
  196. #define  HSLMAX   255 /* H,L, and S vary over 0-HSLMAX */
  197. #define  RGBMAX   255   /* R,G, and B vary over 0-RGBMAX */
  198.                         /* HSLMAX BEST IF DIVISIBLE BY 6 */
  199.                         /* RGBMAX, HSLMAX must each fit in a BYTE. */
  200. /* Hue is undefined if Saturation is 0 (grey-scale) */
  201. /* This value determines where the Hue scrollbar is */
  202. /* initially set for achromatic colors */
  203. #define UNDEFINED (HSLMAX*2/3)
  204. ////////////////////////////////////////////////////////////////////////////////
  205. RGBQUAD CxImage::RGBtoHSL(RGBQUAD lRGBColor)
  206. {
  207. BYTE R,G,B; /* input RGB values */
  208. BYTE H,L,S; /* output HSL values */
  209. BYTE cMax,cMin; /* max and min RGB values */
  210. WORD Rdelta,Gdelta,Bdelta; /* intermediate value: % of spread from max*/
  211. R = lRGBColor.rgbRed; /* get R, G, and B out of DWORD */
  212. G = lRGBColor.rgbGreen;
  213. B = lRGBColor.rgbBlue;
  214. cMax = max( max(R,G), B); /* calculate lightness */
  215. cMin = min( min(R,G), B);
  216. L = (BYTE)((((cMax+cMin)*HSLMAX)+RGBMAX)/(2*RGBMAX));
  217. if (cMax==cMin){ /* r=g=b --> achromatic case */
  218. S = 0; /* saturation */
  219. H = UNDEFINED; /* hue */
  220. } else { /* chromatic case */
  221. if (L <= (HSLMAX/2)) /* saturation */
  222. S = (BYTE)((((cMax-cMin)*HSLMAX)+((cMax+cMin)/2))/(cMax+cMin));
  223. else
  224. S = (BYTE)((((cMax-cMin)*HSLMAX)+((2*RGBMAX-cMax-cMin)/2))/(2*RGBMAX-cMax-cMin));
  225. /* hue */
  226. Rdelta = (WORD)((((cMax-R)*(HSLMAX/6)) + ((cMax-cMin)/2) ) / (cMax-cMin));
  227. Gdelta = (WORD)((((cMax-G)*(HSLMAX/6)) + ((cMax-cMin)/2) ) / (cMax-cMin));
  228. Bdelta = (WORD)((((cMax-B)*(HSLMAX/6)) + ((cMax-cMin)/2) ) / (cMax-cMin));
  229. if (R == cMax)
  230. H = (BYTE)(Bdelta - Gdelta);
  231. else if (G == cMax)
  232. H = (BYTE)((HSLMAX/3) + Rdelta - Bdelta);
  233. else /* B == cMax */
  234. H = (BYTE)(((2*HSLMAX)/3) + Gdelta - Rdelta);
  235. // if (H < 0) H += HSLMAX;     //always false
  236. if (H > HSLMAX) H -= HSLMAX;
  237. }
  238. RGBQUAD hsl={L,S,H,0};
  239. return hsl;
  240. }
  241. ////////////////////////////////////////////////////////////////////////////////
  242. float CxImage::HueToRGB(float n1,float n2, float hue)
  243. {
  244. //<F. Livraghi> fixed implementation for HSL2RGB routine
  245. float rValue;
  246. if (hue > 360)
  247. hue = hue - 360;
  248. else if (hue < 0)
  249. hue = hue + 360;
  250. if (hue < 60)
  251. rValue = n1 + (n2-n1)*hue/60.0f;
  252. else if (hue < 180)
  253. rValue = n2;
  254. else if (hue < 240)
  255. rValue = n1+(n2-n1)*(240-hue)/60;
  256. else
  257. rValue = n1;
  258. return rValue;
  259. }
  260. ////////////////////////////////////////////////////////////////////////////////
  261. RGBQUAD CxImage::HSLtoRGB(COLORREF cHSLColor)
  262. {
  263. return HSLtoRGB(RGBtoRGBQUAD(cHSLColor));
  264. }
  265. ////////////////////////////////////////////////////////////////////////////////
  266. RGBQUAD CxImage::HSLtoRGB(RGBQUAD lHSLColor)
  267. //<F. Livraghi> fixed implementation for HSL2RGB routine
  268. float h,s,l;
  269. float m1,m2;
  270. BYTE r,g,b;
  271. h = (float)lHSLColor.rgbRed * 360.0f/255.0f;
  272. s = (float)lHSLColor.rgbGreen/255.0f;
  273. l = (float)lHSLColor.rgbBlue/255.0f;
  274. if (l <= 0.5) m2 = l * (1+s);
  275. else m2 = l + s - l*s;
  276. m1 = 2 * l - m2;
  277. if (s == 0) {
  278. r=g=b=(BYTE)(l*255.0f);
  279. } else {
  280. r = (BYTE)(HueToRGB(m1,m2,h+120) * 255.0f);
  281. g = (BYTE)(HueToRGB(m1,m2,h) * 255.0f);
  282. b = (BYTE)(HueToRGB(m1,m2,h-120) * 255.0f);
  283. }
  284. RGBQUAD rgb = {b,g,r,0};
  285. return rgb;
  286. }
  287. ////////////////////////////////////////////////////////////////////////////////
  288. RGBQUAD CxImage::YUVtoRGB(RGBQUAD lYUVColor)
  289. {
  290. int U,V,R,G,B;
  291. float Y = lYUVColor.rgbRed;
  292. U = lYUVColor.rgbGreen - 128;
  293. V = lYUVColor.rgbBlue - 128;
  294. // R = (int)(1.164 * Y + 2.018 * U);
  295. // G = (int)(1.164 * Y - 0.813 * V - 0.391 * U);
  296. // B = (int)(1.164 * Y + 1.596 * V);
  297. R = (int)( Y + 1.403f * V);
  298. G = (int)( Y - 0.344f * U - 0.714f * V);
  299. B = (int)( Y + 1.770f * U);
  300. R= min(255,max(0,R));
  301. G= min(255,max(0,G));
  302. B= min(255,max(0,B));
  303. RGBQUAD rgb={(BYTE)B,(BYTE)G,(BYTE)R,0};
  304. return rgb;
  305. }
  306. ////////////////////////////////////////////////////////////////////////////////
  307. RGBQUAD CxImage::RGBtoYUV(RGBQUAD lRGBColor)
  308. {
  309. int Y,U,V,R,G,B;
  310. R = lRGBColor.rgbRed;
  311. G = lRGBColor.rgbGreen;
  312. B = lRGBColor.rgbBlue;
  313. // Y = (int)( 0.257 * R + 0.504 * G + 0.098 * B);
  314. // U = (int)( 0.439 * R - 0.368 * G - 0.071 * B + 128);
  315. // V = (int)(-0.148 * R - 0.291 * G + 0.439 * B + 128);
  316. Y = (int)(0.299f * R + 0.587f * G + 0.114f * B);
  317. U = (int)((B-Y) * 0.565f + 128);
  318. V = (int)((R-Y) * 0.713f + 128);
  319. Y= min(255,max(0,Y));
  320. U= min(255,max(0,U));
  321. V= min(255,max(0,V));
  322. RGBQUAD yuv={(BYTE)V,(BYTE)U,(BYTE)Y,0};
  323. return yuv;
  324. }
  325. ////////////////////////////////////////////////////////////////////////////////
  326. RGBQUAD CxImage::YIQtoRGB(RGBQUAD lYIQColor)
  327. {
  328. int I,Q,R,G,B;
  329. float Y = lYIQColor.rgbRed;
  330. I = lYIQColor.rgbGreen - 128;
  331. Q = lYIQColor.rgbBlue - 128;
  332. R = (int)( Y + 0.956f * I + 0.621f * Q);
  333. G = (int)( Y - 0.273f * I - 0.647f * Q);
  334. B = (int)( Y - 1.104f * I + 1.701f * Q);
  335. R= min(255,max(0,R));
  336. G= min(255,max(0,G));
  337. B= min(255,max(0,B));
  338. RGBQUAD rgb={(BYTE)B,(BYTE)G,(BYTE)R,0};
  339. return rgb;
  340. }
  341. ////////////////////////////////////////////////////////////////////////////////
  342. RGBQUAD CxImage::RGBtoYIQ(RGBQUAD lRGBColor)
  343. {
  344. int Y,I,Q,R,G,B;
  345. R = lRGBColor.rgbRed;
  346. G = lRGBColor.rgbGreen;
  347. B = lRGBColor.rgbBlue;
  348. Y = (int)( 0.2992f * R + 0.5868f * G + 0.1140f * B);
  349. I = (int)( 0.5960f * R - 0.2742f * G - 0.3219f * B + 128);
  350. Q = (int)( 0.2109f * R - 0.5229f * G + 0.3120f * B + 128);
  351. Y= min(255,max(0,Y));
  352. I= min(255,max(0,I));
  353. Q= min(255,max(0,Q));
  354. RGBQUAD yiq={(BYTE)Q,(BYTE)I,(BYTE)Y,0};
  355. return yiq;
  356. }
  357. ////////////////////////////////////////////////////////////////////////////////
  358. RGBQUAD CxImage::XYZtoRGB(RGBQUAD lXYZColor)
  359. {
  360. int X,Y,Z,R,G,B;
  361. X = lXYZColor.rgbRed;
  362. Y = lXYZColor.rgbGreen;
  363. Z = lXYZColor.rgbBlue;
  364. double k=1.088751;
  365. R = (int)(  3.240479f * X - 1.537150f * Y - 0.498535f * Z * k);
  366. G = (int)( -0.969256f * X + 1.875992f * Y + 0.041556f * Z * k);
  367. B = (int)(  0.055648f * X - 0.204043f * Y + 1.057311f * Z * k);
  368. R= min(255,max(0,R));
  369. G= min(255,max(0,G));
  370. B= min(255,max(0,B));
  371. RGBQUAD rgb={(BYTE)B,(BYTE)G,(BYTE)R,0};
  372. return rgb;
  373. }
  374. ////////////////////////////////////////////////////////////////////////////////
  375. RGBQUAD CxImage::RGBtoXYZ(RGBQUAD lRGBColor)
  376. {
  377. int X,Y,Z,R,G,B;
  378. R = lRGBColor.rgbRed;
  379. G = lRGBColor.rgbGreen;
  380. B = lRGBColor.rgbBlue;
  381. X = (int)( 0.412453f * R + 0.357580f * G + 0.180423f * B);
  382. Y = (int)( 0.212671f * R + 0.715160f * G + 0.072169f * B);
  383. Z = (int)((0.019334f * R + 0.119193f * G + 0.950227f * B)*0.918483657f);
  384. //X= min(255,max(0,X));
  385. //Y= min(255,max(0,Y));
  386. //Z= min(255,max(0,Z));
  387. RGBQUAD xyz={(BYTE)Z,(BYTE)Y,(BYTE)X,0};
  388. return xyz;
  389. }
  390. ////////////////////////////////////////////////////////////////////////////////
  391. void CxImage::HuePalette(float correction)
  392. {
  393. if (head.biClrUsed==0) return;
  394. for(DWORD j=0; j<head.biClrUsed; j++){
  395. BYTE i=(BYTE)(j*correction*(255/(head.biClrUsed-1)));
  396. RGBQUAD hsl={120,240,i,0};
  397. SetPaletteColor((BYTE)j,HSLtoRGB(hsl));
  398. }
  399. }
  400. ////////////////////////////////////////////////////////////////////////////////
  401. bool CxImage::Colorize(BYTE hue, BYTE sat)
  402. {
  403. if (!pDib) return false;
  404. RGBQUAD color;
  405. if (head.biClrUsed==0){
  406. long xmin,xmax,ymin,ymax;
  407. if (pSelection){
  408. xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
  409. ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
  410. } else {
  411. xmin = ymin = 0;
  412. xmax = head.biWidth; ymax=head.biHeight;
  413. }
  414. for(long y=ymin; y<ymax; y++){
  415. for(long x=xmin; x<xmax; x++){
  416. #if CXIMAGE_SUPPORT_SELECTION
  417. if (SelectionIsInside(x,y))
  418. #endif //CXIMAGE_SUPPORT_SELECTION
  419. {
  420. color = RGBtoHSL(GetPixelColor(x,y));
  421. color.rgbRed=hue;
  422. color.rgbGreen=sat;
  423. SetPixelColor(x,y,HSLtoRGB(color));
  424. }
  425. }
  426. }
  427. } else {
  428. for(DWORD j=0; j<head.biClrUsed; j++){
  429. color = RGBtoHSL(GetPaletteColor((BYTE)j));
  430. color.rgbRed=hue;
  431. color.rgbGreen=sat;
  432. SetPaletteColor((BYTE)j,HSLtoRGB(color));
  433. }
  434. }
  435. return true;
  436. }
  437. ////////////////////////////////////////////////////////////////////////////////
  438. bool CxImage::Light(long brightness, long contrast)
  439. {
  440. if (!pDib) return false;
  441. float c=(100 + contrast)/100.0f;
  442. brightness+=128;
  443. BYTE cTable[256]; //<nipper>
  444. for (int i=0;i<256;i++) {
  445. cTable[i] = (BYTE)max(0,min(255,(int)((i-128)*c + brightness)));
  446. }
  447. return Lut(cTable);
  448. }
  449. ////////////////////////////////////////////////////////////////////////////////
  450. float CxImage::Mean()
  451. {
  452. if (!pDib) return 0;
  453. CxImage tmp(*this,true);
  454. tmp.GrayScale();
  455. float sum=0;
  456. long xmin,xmax,ymin,ymax;
  457. if (pSelection){
  458. xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
  459. ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
  460. } else {
  461. xmin = ymin = 0;
  462. xmax = head.biWidth; ymax=head.biHeight;
  463. }
  464. if (xmin==xmax || ymin==ymax) return (float)0.0;
  465. BYTE *iSrc=tmp.info.pImage;
  466. for(long y=ymin; y<ymax; y++){
  467. info.nProgress = (long)(100*y/ymax); //<Anatoly Ivasyuk>
  468. for(long x=xmin; x<xmax; x++){
  469. sum+=iSrc[x];
  470. }
  471. iSrc+=tmp.info.dwEffWidth;
  472. }
  473. return sum/(xmax-xmin)/(ymax-ymin);
  474. }
  475. ////////////////////////////////////////////////////////////////////////////////
  476. bool CxImage::Filter(long* kernel, long Ksize, long Kfactor, long Koffset)
  477. {
  478. if (!pDib) return false;
  479. long k2 = Ksize/2;
  480. long kmax= Ksize-k2;
  481. long r,g,b,i;
  482. RGBQUAD c;
  483. CxImage tmp(*this,pSelection!=0,true,true);
  484. long xmin,xmax,ymin,ymax;
  485. if (pSelection){
  486. xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
  487. ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
  488. } else {
  489. xmin = ymin = 0;
  490. xmax = head.biWidth; ymax=head.biHeight;
  491. }
  492. if ((head.biBitCount==8) && IsGrayScale())
  493. {
  494. unsigned char* cPtr;
  495. unsigned char* cPtr2;      
  496. int iCount;
  497. int iY, iY2, iY1;
  498. cPtr = info.pImage;
  499. cPtr2 = (unsigned char *)tmp.info.pImage;
  500. if (Kfactor==0) Kfactor = 1;
  501. for(long y=ymin; y<ymax; y++){
  502. info.nProgress = (long)(100*y/head.biHeight);
  503. if (info.nEscape) break;
  504. for(long x=xmin; x<xmax; x++){
  505. iY1 = y*info.dwEffWidth+x;
  506. #if CXIMAGE_SUPPORT_SELECTION
  507. if (SelectionIsInside(x,y))
  508. #endif //CXIMAGE_SUPPORT_SELECTION
  509. {
  510. if (y-k2 > 0 && (y+kmax-1) < head.biHeight && x-k2 > 0 && (x+kmax-1) < head.biWidth)
  511. {
  512. b=0;
  513. iCount = 0;
  514. iY2 = ((y-k2)*info.dwEffWidth);
  515. for(long j=-k2;j<kmax;j++)
  516. {
  517. iY = iY2+x;
  518. for(long k=-k2;k<kmax;k++)
  519. {
  520. i=kernel[iCount];
  521. b += cPtr[iY+k] * i;
  522. iCount++;
  523. }
  524. iY2 += info.dwEffWidth;
  525. }
  526. cPtr2[iY1] = (BYTE)min(255, max(0,(int)(b/Kfactor + Koffset)));
  527. }
  528. else
  529. cPtr2[iY1] = cPtr[iY1];
  530. }
  531. }
  532. }
  533. }
  534. else
  535. {
  536. for(long y=ymin; y<ymax; y++){
  537. info.nProgress = (long)(100*y/head.biHeight);
  538. if (info.nEscape) break;
  539. for(long x=xmin; x<xmax; x++){
  540. #if CXIMAGE_SUPPORT_SELECTION
  541. if (SelectionIsInside(x,y))
  542. #endif //CXIMAGE_SUPPORT_SELECTION
  543. {
  544. r=b=g=0;
  545. for(long j=-k2;j<kmax;j++){
  546. for(long k=-k2;k<kmax;k++){
  547. c=GetPixelColor(x+j,y+k);
  548. i=kernel[(j+k2)+Ksize*(k+k2)];
  549. r += c.rgbRed * i;
  550. g += c.rgbGreen * i;
  551. b += c.rgbBlue * i;
  552. }
  553. }
  554. if (Kfactor==0){
  555. c.rgbRed   = (BYTE)min(255, max(0,(int)(r + Koffset)));
  556. c.rgbGreen = (BYTE)min(255, max(0,(int)(g + Koffset)));
  557. c.rgbBlue  = (BYTE)min(255, max(0,(int)(b + Koffset)));
  558. } else {
  559. c.rgbRed   = (BYTE)min(255, max(0,(int)(r/Kfactor + Koffset)));
  560. c.rgbGreen = (BYTE)min(255, max(0,(int)(g/Kfactor + Koffset)));
  561. c.rgbBlue  = (BYTE)min(255, max(0,(int)(b/Kfactor + Koffset)));
  562. }
  563. tmp.SetPixelColor(x,y,c);
  564. }
  565. }
  566. }
  567. }
  568. Transfer(tmp);
  569. return true;
  570. }
  571. ////////////////////////////////////////////////////////////////////////////////
  572. bool CxImage::Erode(long Ksize)
  573. {
  574. if (!pDib) return false;
  575. long k2 = Ksize/2;
  576. long kmax= Ksize-k2;
  577. BYTE r,g,b;
  578. RGBQUAD c;
  579. CxImage tmp(*this,pSelection!=0,true,true);
  580. long xmin,xmax,ymin,ymax;
  581. if (pSelection){
  582. xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
  583. ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
  584. } else {
  585. xmin = ymin = 0;
  586. xmax = head.biWidth; ymax=head.biHeight;
  587. }
  588. for(long y=ymin; y<ymax; y++){
  589. info.nProgress = (long)(100*y/head.biHeight);
  590. if (info.nEscape) break;
  591. for(long x=xmin; x<xmax; x++){
  592. #if CXIMAGE_SUPPORT_SELECTION
  593. if (SelectionIsInside(x,y))
  594. #endif //CXIMAGE_SUPPORT_SELECTION
  595. {
  596. r=b=g=255;
  597. for(long j=-k2;j<kmax;j++){
  598. for(long k=-k2;k<kmax;k++){
  599. c=GetPixelColor(x+j,y+k);
  600. if (c.rgbRed < r) r=c.rgbRed;
  601. if (c.rgbGreen < g) g=c.rgbGreen;
  602. if (c.rgbBlue < b) b=c.rgbBlue;
  603. }
  604. }
  605. c.rgbRed   = r;
  606. c.rgbGreen = g;
  607. c.rgbBlue  = b;
  608. tmp.SetPixelColor(x,y,c);
  609. }
  610. }
  611. }
  612. Transfer(tmp);
  613. return true;
  614. }
  615. ////////////////////////////////////////////////////////////////////////////////
  616. bool CxImage::Dilate(long Ksize)
  617. {
  618. if (!pDib) return false;
  619. long k2 = Ksize/2;
  620. long kmax= Ksize-k2;
  621. BYTE r,g,b;
  622. RGBQUAD c;
  623. CxImage tmp(*this,pSelection!=0,true,true);
  624. long xmin,xmax,ymin,ymax;
  625. if (pSelection){
  626. xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
  627. ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
  628. } else {
  629. xmin = ymin = 0;
  630. xmax = head.biWidth; ymax=head.biHeight;
  631. }
  632. for(long y=ymin; y<ymax; y++){
  633. info.nProgress = (long)(100*y/head.biHeight);
  634. if (info.nEscape) break;
  635. for(long x=xmin; x<xmax; x++){
  636. #if CXIMAGE_SUPPORT_SELECTION
  637. if (SelectionIsInside(x,y))
  638. #endif //CXIMAGE_SUPPORT_SELECTION
  639. {
  640. r=b=g=0;
  641. for(long j=-k2;j<kmax;j++){
  642. for(long k=-k2;k<kmax;k++){
  643. c=GetPixelColor(x+j,y+k);
  644. if (c.rgbRed > r) r=c.rgbRed;
  645. if (c.rgbGreen > g) g=c.rgbGreen;
  646. if (c.rgbBlue > b) b=c.rgbBlue;
  647. }
  648. }
  649. c.rgbRed   = r;
  650. c.rgbGreen = g;
  651. c.rgbBlue  = b;
  652. tmp.SetPixelColor(x,y,c);
  653. }
  654. }
  655. }
  656. Transfer(tmp);
  657. return true;
  658. }
  659. ////////////////////////////////////////////////////////////////////////////////
  660. // thanks to Mwolski <mpwolski(at)hotmail(dot)com>
  661. void CxImage::Mix(CxImage & imgsrc2, ImageOpType op, long lXOffset, long lYOffset)
  662. {
  663.     long lWide = min(GetWidth(),imgsrc2.GetWidth()-lXOffset);
  664.     long lHeight = min(GetHeight(),imgsrc2.GetHeight()-lYOffset);
  665.     RGBQUAD rgbBackgrnd = GetTransColor();
  666.     RGBQUAD rgb1, rgb2, rgbDest;
  667.     for(long lY=0;lY<lHeight;lY++)
  668.     {
  669. info.nProgress = (long)(100*lY/head.biHeight);
  670. if (info.nEscape) break;
  671.         for(long lX=0;lX<lWide;lX++)
  672.         {
  673. #if CXIMAGE_SUPPORT_SELECTION
  674. if (SelectionIsInside(lX,lY) && imgsrc2.SelectionIsInside(lX+lXOffset,lY+lYOffset))
  675. #endif //CXIMAGE_SUPPORT_SELECTION
  676. {
  677. rgb1 = GetPixelColor(lX,lY);
  678. rgb2 = imgsrc2.GetPixelColor(lX+lXOffset,lY+lYOffset);
  679. switch(op)
  680. {
  681. case OpAdd:
  682. rgbDest.rgbBlue = (BYTE)max(0,min(255,rgb1.rgbBlue+rgb2.rgbBlue));
  683. rgbDest.rgbGreen = (BYTE)max(0,min(255,rgb1.rgbGreen+rgb2.rgbGreen));
  684. rgbDest.rgbRed = (BYTE)max(0,min(255,rgb1.rgbRed+rgb2.rgbRed));
  685. break;
  686. case OpSub:
  687. rgbDest.rgbBlue = (BYTE)max(0,min(255,rgb1.rgbBlue-rgb2.rgbBlue));
  688. rgbDest.rgbGreen = (BYTE)max(0,min(255,rgb1.rgbGreen-rgb2.rgbGreen));
  689. rgbDest.rgbRed = (BYTE)max(0,min(255,rgb1.rgbRed-rgb2.rgbRed));
  690. break;
  691. case OpAnd:
  692. rgbDest.rgbBlue = (BYTE)(rgb1.rgbBlue&rgb2.rgbBlue);
  693. rgbDest.rgbGreen = (BYTE)(rgb1.rgbGreen&rgb2.rgbGreen);
  694. rgbDest.rgbRed = (BYTE)(rgb1.rgbRed&rgb2.rgbRed);
  695. break;
  696. case OpXor:
  697. rgbDest.rgbBlue = (BYTE)(rgb1.rgbBlue^rgb2.rgbBlue);
  698. rgbDest.rgbGreen = (BYTE)(rgb1.rgbGreen^rgb2.rgbGreen);
  699. rgbDest.rgbRed = (BYTE)(rgb1.rgbRed^rgb2.rgbRed);
  700. break;
  701. case OpOr:
  702. rgbDest.rgbBlue = (BYTE)(rgb1.rgbBlue|rgb2.rgbBlue);
  703. rgbDest.rgbGreen = (BYTE)(rgb1.rgbGreen|rgb2.rgbGreen);
  704. rgbDest.rgbRed = (BYTE)(rgb1.rgbRed|rgb2.rgbRed);
  705. break;
  706. case OpMask:
  707. if(rgb2.rgbBlue==0 && rgb2.rgbGreen==0 && rgb2.rgbRed==0)
  708. rgbDest = rgbBackgrnd;
  709. else
  710. rgbDest = rgb1;
  711. break;
  712. case OpSrcCopy:
  713. if(memcmp(&rgb1,&rgbBackgrnd,sizeof(RGBQUAD))==0)
  714. rgbDest = rgb2;
  715. else // copy straight over
  716. rgbDest = rgb1;
  717. break;
  718. case OpDstCopy:
  719. if(memcmp(&rgb2,&rgbBackgrnd,sizeof(RGBQUAD))==0)
  720. rgbDest = rgb1;
  721. else // copy straight over
  722. rgbDest = rgb2;
  723. break;
  724. case OpSrcBlend:
  725. if(memcmp(&rgb1,&rgbBackgrnd,sizeof(RGBQUAD))==0)
  726. rgbDest = rgb2;
  727. else
  728. {
  729. long lBDiff = abs(rgb1.rgbBlue - rgbBackgrnd.rgbBlue);
  730. long lGDiff = abs(rgb1.rgbGreen - rgbBackgrnd.rgbGreen);
  731. long lRDiff = abs(rgb1.rgbRed - rgbBackgrnd.rgbRed);
  732. double lAverage = (lBDiff+lGDiff+lRDiff)/3;
  733. double lThresh = 16;
  734. double dLarge = lAverage/lThresh;
  735. double dSmall = (lThresh-lAverage)/lThresh;
  736. double dSmallAmt = dSmall*((double)rgb2.rgbBlue);
  737. if( lAverage < lThresh+1){
  738. rgbDest.rgbBlue = (BYTE)max(0,min(255,(int)(dLarge*((double)rgb1.rgbBlue) +
  739. dSmallAmt)));
  740. rgbDest.rgbGreen = (BYTE)max(0,min(255,(int)(dLarge*((double)rgb1.rgbGreen) +
  741. dSmallAmt)));
  742. rgbDest.rgbRed = (BYTE)max(0,min(255,(int)(dLarge*((double)rgb1.rgbRed) +
  743. dSmallAmt)));
  744. }
  745. else
  746. rgbDest = rgb1;
  747. }
  748. break;
  749. default:
  750. return;
  751. }
  752. SetPixelColor(lX,lY,rgbDest);
  753. }
  754. }
  755. }
  756. }
  757. ////////////////////////////////////////////////////////////////////////////////
  758. // thanks to Kenneth Ballard
  759. void CxImage::MixFrom(CxImage & imagesrc2, long lXOffset, long lYOffset)
  760. {
  761.     RGBQUAD rgbBackgrnd = imagesrc2.GetTransColor();
  762.     RGBQUAD rgb1;
  763.     long width = imagesrc2.GetWidth();
  764.     long height = imagesrc2.GetHeight();
  765.     int x, y;
  766.     for(x = 0; x < width; x++)
  767.     {
  768.         for(y = 0; y < height; y++)
  769.         {
  770.             rgb1 = imagesrc2.GetPixelColor(x, y);
  771.             if(memcmp(&rgb1, &rgbBackgrnd, sizeof(RGBQUAD)) != 0)
  772.                 SetPixelColor(x + lXOffset, y + lYOffset, rgb1);
  773.         }
  774.     }
  775. }
  776. ////////////////////////////////////////////////////////////////////////////////
  777. bool CxImage::ShiftRGB(long r, long g, long b)
  778. {
  779. if (!pDib) return false;
  780. RGBQUAD color;
  781. if (head.biClrUsed==0){
  782. long xmin,xmax,ymin,ymax;
  783. if (pSelection){
  784. xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
  785. ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
  786. } else {
  787. xmin = ymin = 0;
  788. xmax = head.biWidth; ymax=head.biHeight;
  789. }
  790. for(long y=ymin; y<ymax; y++){
  791. for(long x=xmin; x<xmax; x++){
  792. #if CXIMAGE_SUPPORT_SELECTION
  793. if (SelectionIsInside(x,y))
  794. #endif //CXIMAGE_SUPPORT_SELECTION
  795. {
  796. color = GetPixelColor(x,y);
  797. color.rgbRed = (BYTE)max(0,min(255,(int)(color.rgbRed + r)));
  798. color.rgbGreen = (BYTE)max(0,min(255,(int)(color.rgbGreen + g)));
  799. color.rgbBlue = (BYTE)max(0,min(255,(int)(color.rgbBlue + b)));
  800. SetPixelColor(x,y,color);
  801. }
  802. }
  803. }
  804. } else {
  805. for(DWORD j=0; j<head.biClrUsed; j++){
  806. color = GetPaletteColor((BYTE)j);
  807. color.rgbRed = (BYTE)max(0,min(255,(int)(color.rgbRed + r)));
  808. color.rgbGreen = (BYTE)max(0,min(255,(int)(color.rgbGreen + g)));
  809. color.rgbBlue = (BYTE)max(0,min(255,(int)(color.rgbBlue + b)));
  810. SetPaletteColor((BYTE)j,color);
  811. }
  812. }
  813. return true;
  814. }
  815. ////////////////////////////////////////////////////////////////////////////////
  816. bool CxImage::Gamma(float gamma)
  817. {
  818. if (!pDib) return false;
  819. double dinvgamma = 1/gamma;
  820. double dMax = pow(255.0, dinvgamma) / 255.0;
  821. BYTE cTable[256]; //<nipper>
  822. for (int i=0;i<256;i++) {
  823. cTable[i] = (BYTE)max(0,min(255,(int)( pow((double)i, dinvgamma) / dMax)));
  824. }
  825. return Lut(cTable);
  826. }
  827. ////////////////////////////////////////////////////////////////////////////////
  828. #if CXIMAGE_SUPPORT_WINCE == 0
  829. bool CxImage::Median(long Ksize)
  830. {
  831. if (!pDib) return false;
  832. long k2 = Ksize/2;
  833. long kmax= Ksize-k2;
  834. long i,j,k;
  835. RGBQUAD* kernel = (RGBQUAD*)malloc(Ksize*Ksize*sizeof(RGBQUAD));
  836. CxImage tmp(*this,pSelection!=0,true,true);
  837. long xmin,xmax,ymin,ymax;
  838. if (pSelection){
  839. xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
  840. ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
  841. } else {
  842. xmin = ymin = 0;
  843. xmax = head.biWidth; ymax=head.biHeight;
  844. }
  845. for(long y=ymin; y<ymax; y++){
  846. info.nProgress = (long)(100*y/head.biHeight);
  847. if (info.nEscape) break;
  848. for(long x=xmin; x<xmax; x++){
  849. #if CXIMAGE_SUPPORT_SELECTION
  850. if (SelectionIsInside(x,y))
  851. #endif //CXIMAGE_SUPPORT_SELECTION
  852. {
  853. for(j=-k2, i=0;j<kmax;j++)
  854. for(k=-k2;k<kmax;k++, i++)
  855. kernel[i]=GetPixelColor(x+j,y+k);
  856. qsort(kernel, i, sizeof(RGBQUAD), CompareColors);
  857. tmp.SetPixelColor(x,y,kernel[i/2]);
  858. }
  859. }
  860. }
  861. free(kernel);
  862. Transfer(tmp);
  863. return true;
  864. }
  865. #endif //CXIMAGE_SUPPORT_WINCE
  866. ////////////////////////////////////////////////////////////////////////////////
  867. bool CxImage::Noise(long level)
  868. {
  869. if (!pDib) return false;
  870. RGBQUAD color;
  871. long xmin,xmax,ymin,ymax,n;
  872. if (pSelection){
  873. xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
  874. ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
  875. } else {
  876. xmin = ymin = 0;
  877. xmax = head.biWidth; ymax=head.biHeight;
  878. }
  879. for(long y=ymin; y<ymax; y++){
  880. info.nProgress = (long)(100*y/ymax); //<Anatoly Ivasyuk>
  881. for(long x=xmin; x<xmax; x++){
  882. #if CXIMAGE_SUPPORT_SELECTION
  883. if (SelectionIsInside(x,y))
  884. #endif //CXIMAGE_SUPPORT_SELECTION
  885. {
  886. color = GetPixelColor(x,y);
  887. n=(long)((rand()/(float)RAND_MAX - 0.5)*level);
  888. color.rgbRed = (BYTE)max(0,min(255,(int)(color.rgbRed + n)));
  889. n=(long)((rand()/(float)RAND_MAX - 0.5)*level);
  890. color.rgbGreen = (BYTE)max(0,min(255,(int)(color.rgbGreen + n)));
  891. n=(long)((rand()/(float)RAND_MAX - 0.5)*level);
  892. color.rgbBlue = (BYTE)max(0,min(255,(int)(color.rgbBlue + n)));
  893. SetPixelColor(x,y,color);
  894. }
  895. }
  896. }
  897. return true;
  898. }
  899. ////////////////////////////////////////////////////////////////////////////////
  900. #ifndef __BORLANDC__
  901. ////////////////////////////////////////////////////////////////////////////////
  902. bool CxImage::FFT2(CxImage* srcReal, CxImage* srcImag, CxImage* dstReal, CxImage* dstImag,
  903.    long direction, bool bForceFFT, bool bMagnitude)
  904. {
  905. //check if there is something to convert
  906. if (srcReal==NULL && srcImag==NULL) return false;
  907. long w,h;
  908. //get width and height
  909. if (srcReal) {
  910. w=srcReal->GetWidth();
  911. h=srcReal->GetHeight();
  912. } else {
  913. w=srcImag->GetWidth();
  914. h=srcImag->GetHeight();
  915. }
  916. bool bXpow2 = IsPowerof2(w);
  917. bool bYpow2 = IsPowerof2(h);
  918. //if bForceFFT, width AND height must be powers of 2
  919. if (bForceFFT && !(bXpow2 && bYpow2)) {
  920. long i;
  921. i=0;
  922. while((1<<i)<w) i++;
  923. w=1<<i;
  924. bXpow2=true;
  925. i=0;
  926. while((1<<i)<h) i++;
  927. h=1<<i;
  928. bYpow2=true;
  929. }
  930. // I/O images for FFT
  931. CxImage *tmpReal,*tmpImag;
  932. // select output
  933. tmpReal = (dstReal) ? dstReal : srcReal;
  934. tmpImag = (dstImag) ? dstImag : srcImag;
  935. // src!=dst -> copy the image
  936. if (srcReal && dstReal) tmpReal->Copy(*srcReal,true,false,false);
  937. if (srcImag && dstImag) tmpImag->Copy(*srcImag,true,false,false);
  938. // dst&&src are empty -> create new one, else turn to GrayScale
  939. if (srcReal==0 && dstReal==0){
  940. tmpReal = new CxImage(w,h,8);
  941. tmpReal->Clear(0);
  942. tmpReal->SetGrayPalette();
  943. } else {
  944. if (!tmpReal->IsGrayScale()) tmpReal->GrayScale();
  945. }
  946. if (srcImag==0 && dstImag==0){
  947. tmpImag = new CxImage(w,h,8);
  948. tmpImag->Clear(0);
  949. tmpImag->SetGrayPalette();
  950. } else {
  951. if (!tmpImag->IsGrayScale()) tmpImag->GrayScale();
  952. }
  953. if (!(tmpReal->IsValid() && tmpImag->IsValid())){
  954. if (srcReal==0 && dstReal==0) delete tmpReal;
  955. if (srcImag==0 && dstImag==0) delete tmpImag;
  956. return false;
  957. }
  958. //resample for FFT, if necessary 
  959. tmpReal->Resample(w,h,0);
  960. tmpImag->Resample(w,h,0);
  961. //ok, here we have 2 (w x h), grayscale images ready for a FFT
  962. double* real;
  963. double* imag;
  964. long j,k,m;
  965. _complex **grid;
  966. //double mean = tmpReal->Mean();
  967. /* Allocate memory for the grid */
  968. grid = (_complex **)malloc(w * sizeof(_complex));
  969. for (k=0;k<w;k++) {
  970. grid[k] = (_complex *)malloc(h * sizeof(_complex));
  971. }
  972. for (j=0;j<h;j++) {
  973. for (k=0;k<w;k++) {
  974. grid[k][j].x = tmpReal->GetPixelIndex(k,j)-128;
  975. grid[k][j].y = tmpImag->GetPixelIndex(k,j)-128;
  976. }
  977. }
  978. //DFT buffers
  979. double *real2,*imag2;
  980. real2 = (double*)malloc(max(w,h) * sizeof(double));
  981. imag2 = (double*)malloc(max(w,h) * sizeof(double));
  982. /* Transform the rows */
  983. real = (double *)malloc(w * sizeof(double));
  984. imag = (double *)malloc(w * sizeof(double));
  985. m=0;
  986. while((1<<m)<w) m++;
  987. for (j=0;j<h;j++) {
  988. for (k=0;k<w;k++) {
  989. real[k] = grid[k][j].x;
  990. imag[k] = grid[k][j].y;
  991. }
  992. if (bXpow2) FFT(direction,m,real,imag);
  993. else DFT(direction,w,real,imag,real2,imag2);
  994. for (k=0;k<w;k++) {
  995. grid[k][j].x = real[k];
  996. grid[k][j].y = imag[k];
  997. }
  998. }
  999. free(real);
  1000. free(imag);
  1001. /* Transform the columns */
  1002. real = (double *)malloc(h * sizeof(double));
  1003. imag = (double *)malloc(h * sizeof(double));
  1004. m=0;
  1005. while((1<<m)<h) m++;
  1006. for (k=0;k<w;k++) {
  1007. for (j=0;j<h;j++) {
  1008. real[j] = grid[k][j].x;
  1009. imag[j] = grid[k][j].y;
  1010. }
  1011. if (bYpow2) FFT(direction,m,real,imag);
  1012. else DFT(direction,h,real,imag,real2,imag2);
  1013. for (j=0;j<h;j++) {
  1014. grid[k][j].x = real[j];
  1015. grid[k][j].y = imag[j];
  1016. }
  1017. }
  1018. free(real);
  1019. free(imag);
  1020. free(real2);
  1021. free(imag2);
  1022. /* converting from double to byte, there is a HUGE loss in the dynamics
  1023.   "nn" tries to keep an acceptable SNR, but 8bit=48dB: don't ask more */
  1024. double nn=pow((double)2,(double)log((double)max(w,h))/(double)log((double)2)-4);
  1025. //reversed gain for reversed transform
  1026. if (direction==-1) nn=1/nn;
  1027. //bMagnitude : just to see it on the screen
  1028. if (bMagnitude) nn*=4;
  1029. for (j=0;j<h;j++) {
  1030. for (k=0;k<w;k++) {
  1031. if (bMagnitude){
  1032. tmpReal->SetPixelIndex(k,j,(BYTE)max(0,min(255,(nn*(3+log(_cabs(grid[k][j])))))));
  1033. if (grid[k][j].x==0){
  1034. tmpImag->SetPixelIndex(k,j,(BYTE)max(0,min(255,(128+(atan(grid[k][j].y/0.0000000001)*nn)))));
  1035. } else {
  1036. tmpImag->SetPixelIndex(k,j,(BYTE)max(0,min(255,(128+(atan(grid[k][j].y/grid[k][j].x)*nn)))));
  1037. }
  1038. } else {
  1039. tmpReal->SetPixelIndex(k,j,(BYTE)max(0,min(255,(128 + grid[k][j].x*nn))));
  1040. tmpImag->SetPixelIndex(k,j,(BYTE)max(0,min(255,(128 + grid[k][j].y*nn))));
  1041. }
  1042. }
  1043. }
  1044. for (k=0;k<w;k++) free (grid[k]);
  1045. free (grid);
  1046. if (srcReal==0 && dstReal==0) delete tmpReal;
  1047. if (srcImag==0 && dstImag==0) delete tmpImag;
  1048. return true;
  1049. }
  1050. ////////////////////////////////////////////////////////////////////////////////
  1051. #endif //__BORLANDC__
  1052. ////////////////////////////////////////////////////////////////////////////////
  1053. bool CxImage::IsPowerof2(long x)
  1054. {
  1055. long i=0;
  1056. while ((1<<i)<x) i++;
  1057. if (x==(1<<i)) return true;
  1058. return false;
  1059. }
  1060. ////////////////////////////////////////////////////////////////////////////////
  1061. /*
  1062.    This computes an in-place complex-to-complex FFT 
  1063.    x and y are the real and imaginary arrays of n=2^m points.
  1064.    o(n)=n*log2(n)
  1065.    dir =  1 gives forward transform
  1066.    dir = -1 gives reverse transform 
  1067.    Written by Paul Bourke, July 1998
  1068.    FFT algorithm by Cooley and Tukey, 1965 
  1069. */
  1070. bool CxImage::FFT(int dir,int m,double *x,double *y)
  1071. {
  1072. long nn,i,i1,j,k,i2,l,l1,l2;
  1073. double c1,c2,tx,ty,t1,t2,u1,u2,z;
  1074. /* Calculate the number of points */
  1075. nn = 1<<m;
  1076. /* Do the bit reversal */
  1077. i2 = nn >> 1;
  1078. j = 0;
  1079. for (i=0;i<nn-1;i++) {
  1080. if (i < j) {
  1081. tx = x[i];
  1082. ty = y[i];
  1083. x[i] = x[j];
  1084. y[i] = y[j];
  1085. x[j] = tx;
  1086. y[j] = ty;
  1087. }
  1088. k = i2;
  1089. while (k <= j) {
  1090. j -= k;
  1091. k >>= 1;
  1092. }
  1093. j += k;
  1094. }
  1095. /* Compute the FFT */
  1096. c1 = -1.0;
  1097. c2 = 0.0;
  1098. l2 = 1;
  1099. for (l=0;l<m;l++) {
  1100. l1 = l2;
  1101. l2 <<= 1;
  1102. u1 = 1.0;
  1103. u2 = 0.0;
  1104. for (j=0;j<l1;j++) {
  1105. for (i=j;i<nn;i+=l2) {
  1106. i1 = i + l1;
  1107. t1 = u1 * x[i1] - u2 * y[i1];
  1108. t2 = u1 * y[i1] + u2 * x[i1];
  1109. x[i1] = x[i] - t1;
  1110. y[i1] = y[i] - t2;
  1111. x[i] += t1;
  1112. y[i] += t2;
  1113. }
  1114. z =  u1 * c1 - u2 * c2;
  1115. u2 = u1 * c2 + u2 * c1;
  1116. u1 = z;
  1117. }
  1118. c2 = sqrt((1.0 - c1) / 2.0);
  1119. if (dir == 1)
  1120. c2 = -c2;
  1121. c1 = sqrt((1.0 + c1) / 2.0);
  1122. }
  1123. /* Scaling for forward transform */
  1124. if (dir == 1) {
  1125. for (i=0;i<nn;i++) {
  1126. x[i] /= (double)nn;
  1127. y[i] /= (double)nn;
  1128. }
  1129. }
  1130.    return true;
  1131. }
  1132. ////////////////////////////////////////////////////////////////////////////////
  1133. /*
  1134.    Direct fourier transform o(n)=n^2
  1135.    Written by Paul Bourke, July 1998 
  1136. */
  1137. bool CxImage::DFT(int dir,long m,double *x1,double *y1,double *x2,double *y2)
  1138. {
  1139.    long i,k;
  1140.    double arg;
  1141.    double cosarg,sinarg;
  1142.    
  1143.    for (i=0;i<m;i++) {
  1144.       x2[i] = 0;
  1145.       y2[i] = 0;
  1146.       arg = - dir * 2.0 * 3.14159265358f * i / (double)m;
  1147.       for (k=0;k<m;k++) {
  1148.          cosarg = cos(k * arg);
  1149.          sinarg = sin(k * arg);
  1150.          x2[i] += (x1[k] * cosarg - y1[k] * sinarg);
  1151.          y2[i] += (x1[k] * sinarg + y1[k] * cosarg);
  1152.       }
  1153.    }
  1154.    
  1155.    /* Copy the data back */
  1156.    if (dir == 1) {
  1157.       for (i=0;i<m;i++) {
  1158.          x1[i] = x2[i] / m;
  1159.          y1[i] = y2[i] / m;
  1160.       }
  1161.    } else {
  1162.       for (i=0;i<m;i++) {
  1163.          x1[i] = x2[i];
  1164.          y1[i] = y2[i];
  1165.       }
  1166.    }
  1167.    
  1168.    return true;
  1169. }
  1170. ////////////////////////////////////////////////////////////////////////////////
  1171. bool CxImage::Combine(CxImage* r,CxImage* g,CxImage* b,CxImage* a, long colorspace)
  1172. {
  1173. if (r==0 || g==0 || b==0) return false;
  1174. long w = r->GetWidth();
  1175. long h = r->GetHeight();
  1176. Create(w,h,24);
  1177. g->Resample(w,h);
  1178. b->Resample(w,h);
  1179. if (a) {
  1180. a->Resample(w,h);
  1181. #if CXIMAGE_SUPPORT_ALPHA
  1182. AlphaCreate();
  1183. #endif //CXIMAGE_SUPPORT_ALPHA
  1184. }
  1185. RGBQUAD c;
  1186. for (long y=0;y<h;y++){
  1187. info.nProgress = (long)(100*y/h); //<Anatoly Ivasyuk>
  1188. for (long x=0;x<w;x++){
  1189. c.rgbRed=r->GetPixelIndex(x,y);
  1190. c.rgbGreen=g->GetPixelIndex(x,y);
  1191. c.rgbBlue=b->GetPixelIndex(x,y);
  1192. switch (colorspace){
  1193. case 1:
  1194. SetPixelColor(x,y,HSLtoRGB(c));
  1195. break;
  1196. case 2:
  1197. SetPixelColor(x,y,YUVtoRGB(c));
  1198. break;
  1199. case 3:
  1200. SetPixelColor(x,y,YIQtoRGB(c));
  1201. break;
  1202. case 4:
  1203. SetPixelColor(x,y,XYZtoRGB(c));
  1204. break;
  1205. default:
  1206. SetPixelColor(x,y,c);
  1207. }
  1208. #if CXIMAGE_SUPPORT_ALPHA
  1209. if (a) AlphaSet(x,y,a->GetPixelIndex(x,y));
  1210. #endif //CXIMAGE_SUPPORT_ALPHA
  1211. }
  1212. }
  1213. return true;
  1214. }
  1215. ////////////////////////////////////////////////////////////////////////////////
  1216. bool CxImage::Repair(float radius, long niterations, long colorspace)
  1217. {
  1218. if (!IsValid()) return false;
  1219. long w = GetWidth();
  1220. long h = GetHeight();
  1221. CxImage r,g,b;
  1222. r.Create(w,h,8);
  1223. g.Create(w,h,8);
  1224. b.Create(w,h,8);
  1225. switch (colorspace){
  1226. case 1:
  1227. SplitHSL(&r,&g,&b);
  1228. break;
  1229. case 2:
  1230. SplitYUV(&r,&g,&b);
  1231. break;
  1232. case 3:
  1233. SplitYIQ(&r,&g,&b);
  1234. break;
  1235. case 4:
  1236. SplitXYZ(&r,&g,&b);
  1237. break;
  1238. default:
  1239. SplitRGB(&r,&g,&b);
  1240. }
  1241. for (int i=0; i<niterations; i++){
  1242. RepairChannel(&r,radius);
  1243. RepairChannel(&g,radius);
  1244. RepairChannel(&b,radius);
  1245. }
  1246. CxImage* a=NULL;
  1247. #if CXIMAGE_SUPPORT_ALPHA
  1248. if (AlphaIsValid()){
  1249. a = new CxImage();
  1250. AlphaSplit(a);
  1251. }
  1252. #endif
  1253. Combine(&r,&g,&b,a,colorspace);
  1254. delete a;
  1255. return true;
  1256. }
  1257. ////////////////////////////////////////////////////////////////////////////////
  1258. bool CxImage::RepairChannel(CxImage *ch, float radius)
  1259. {
  1260. if (ch==NULL) return false;
  1261. CxImage tmp(*ch);
  1262. long w = ch->GetWidth()-1;
  1263. long h = ch->GetHeight()-1;
  1264. double correction,ix,iy,ixx,ixy,iyy,den,num;
  1265. int x,y,xy0,xp1,xm1,yp1,ym1;
  1266. for(x=1; x<w; x++){
  1267. for(y=1; y<h; y++){
  1268. xy0 = ch->GetPixelIndex(x,y);
  1269. xm1 = ch->GetPixelIndex(x-1,y);
  1270. xp1 = ch->GetPixelIndex(x+1,y);
  1271. ym1 = ch->GetPixelIndex(x,y-1);
  1272. yp1 = ch->GetPixelIndex(x,y+1);
  1273. ix= (xp1-xm1)/2.0;
  1274. iy= (yp1-ym1)/2.0;
  1275. ixx= xp1 - 2.0 * xy0 + xm1;
  1276. iyy= yp1 - 2.0 * xy0 + ym1;
  1277. ixy=(ch->GetPixelIndex(x+1,y+1)+ch->GetPixelIndex(x-1,y-1)-
  1278.  ch->GetPixelIndex(x-1,y+1)-ch->GetPixelIndex(x+1,y-1))/4.0;
  1279. num= (1.0+iy*iy)*ixx - ix*iy*ixy + (1.0+ix*ix)*iyy;
  1280. den= 1.0+ix*ix+iy*iy;
  1281. correction = num/den;
  1282. tmp.SetPixelIndex(x,y,(BYTE)min(255,max(0,(xy0 + radius * correction))));
  1283. }
  1284. }
  1285. for (x=0;x<=w;x++){
  1286. tmp.SetPixelIndex(x,0,ch->GetPixelIndex(x,0));
  1287. tmp.SetPixelIndex(x,h,ch->GetPixelIndex(x,h));
  1288. }
  1289. for (y=0;y<=h;y++){
  1290. tmp.SetPixelIndex(0,y,ch->GetPixelIndex(0,y));
  1291. tmp.SetPixelIndex(w,y,ch->GetPixelIndex(w,y));
  1292. }
  1293. ch->Transfer(tmp);
  1294. return true;
  1295. }
  1296. ////////////////////////////////////////////////////////////////////////////////
  1297. bool CxImage::Contour()
  1298. {
  1299. if (!pDib) return false;
  1300. long Ksize = 3;
  1301. long k2 = Ksize/2;
  1302. long kmax= Ksize-k2;
  1303. long i,j,k;
  1304. BYTE maxr,maxg,maxb;
  1305. RGBQUAD pix1,pix2;
  1306. CxImage tmp(*this,pSelection!=0,true,true);
  1307. long xmin,xmax,ymin,ymax;
  1308. if (pSelection){
  1309. xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
  1310. ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
  1311. } else {
  1312. xmin = ymin = 0;
  1313. xmax = head.biWidth; ymax=head.biHeight;
  1314. }
  1315. for(long y=ymin; y<ymax; y++){
  1316. info.nProgress = (long)(100*y/head.biHeight);
  1317. if (info.nEscape) break;
  1318. for(long x=xmin; x<xmax; x++){
  1319. #if CXIMAGE_SUPPORT_SELECTION
  1320. if (SelectionIsInside(x,y))
  1321. #endif //CXIMAGE_SUPPORT_SELECTION
  1322. {
  1323. pix1 = GetPixelColor(x,y);
  1324. maxr=maxg=maxb=0;
  1325. for(j=-k2, i=0;j<kmax;j++){
  1326. for(k=-k2;k<kmax;k++, i++){
  1327. pix2=GetPixelColor(x+j,y+k);
  1328. if ((pix2.rgbBlue-pix1.rgbBlue)>maxb) maxb = pix2.rgbBlue;
  1329. if ((pix2.rgbGreen-pix1.rgbGreen)>maxg) maxg = pix2.rgbGreen;
  1330. if ((pix2.rgbRed-pix1.rgbRed)>maxr) maxr = pix2.rgbRed;
  1331. }
  1332. }
  1333. pix1.rgbBlue=(BYTE)(255-maxb);
  1334. pix1.rgbGreen=(BYTE)(255-maxg);
  1335. pix1.rgbRed=(BYTE)(255-maxr);
  1336. tmp.SetPixelColor(x,y,pix1);
  1337. }
  1338. }
  1339. }
  1340. Transfer(tmp);
  1341. return true;
  1342. }
  1343. ////////////////////////////////////////////////////////////////////////////////
  1344. bool CxImage::Jitter(long radius)
  1345. {
  1346. if (!pDib) return false;
  1347. long nx,ny;
  1348. CxImage tmp(*this,pSelection!=0,true,true);
  1349. long xmin,xmax,ymin,ymax;
  1350. if (pSelection){
  1351. xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
  1352. ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
  1353. } else {
  1354. xmin = ymin = 0;
  1355. xmax = head.biWidth; ymax=head.biHeight;
  1356. }
  1357. for(long y=ymin; y<ymax; y++){
  1358. info.nProgress = (long)(100*y/head.biHeight);
  1359. if (info.nEscape) break;
  1360. for(long x=xmin; x<xmax; x++){
  1361. #if CXIMAGE_SUPPORT_SELECTION
  1362. if (SelectionIsInside(x,y))
  1363. #endif //CXIMAGE_SUPPORT_SELECTION
  1364. {
  1365. nx=x+(long)((rand()/(float)RAND_MAX - 0.5)*(radius*2));
  1366. ny=y+(long)((rand()/(float)RAND_MAX - 0.5)*(radius*2));
  1367. if (!IsInside(nx,ny)) {
  1368. nx=x;
  1369. ny=y;
  1370. }
  1371. if (head.biClrUsed==0){
  1372. tmp.SetPixelColor(x,y,GetPixelColor(nx,ny));
  1373. } else {
  1374. tmp.SetPixelIndex(x,y,GetPixelIndex(nx,ny));
  1375. }
  1376. #if CXIMAGE_SUPPORT_ALPHA
  1377. tmp.AlphaSet(x,y,AlphaGet(nx,ny));
  1378. #endif //CXIMAGE_SUPPORT_ALPHA
  1379. }
  1380. }
  1381. }
  1382. Transfer(tmp);
  1383. return true;
  1384. }
  1385. ////////////////////////////////////////////////////////////////////////////////
  1386. /* <nipper> generates a 1-D convolution matrix to be used for each pass of 
  1387. * a two-pass gaussian blur.  Returns the length of the matrix.
  1388. */
  1389. int CxImage::gen_convolve_matrix (float radius, float **cmatrix_p)
  1390. {
  1391. int matrix_length;
  1392. int matrix_midpoint;
  1393. float* cmatrix;
  1394. int i,j;
  1395. float std_dev;
  1396. float sum;
  1397. /* we want to generate a matrix that goes out a certain radius
  1398. * from the center, so we have to go out ceil(rad-0.5) pixels,
  1399. * inlcuding the center pixel.  Of course, that's only in one direction,
  1400. * so we have to go the same amount in the other direction, but not count
  1401. * the center pixel again.  So we double the previous result and subtract
  1402. * one.
  1403. * The radius parameter that is passed to this function is used as
  1404. * the standard deviation, and the radius of effect is the
  1405. * standard deviation * 2.  It's a little confusing.
  1406. */
  1407. radius = (float)fabs(radius) + 1.0f;
  1408. std_dev = radius;
  1409. radius = std_dev * 2;
  1410. /* go out 'radius' in each direction */
  1411. matrix_length = int (2 * ceil(radius-0.5) + 1);
  1412. if (matrix_length <= 0) matrix_length = 1;
  1413. matrix_midpoint = matrix_length/2 + 1;
  1414. *cmatrix_p = new float[matrix_length];
  1415. cmatrix = *cmatrix_p;
  1416. /*  Now we fill the matrix by doing a numeric integration approximation
  1417. * from -2*std_dev to 2*std_dev, sampling 50 points per pixel.
  1418. * We do the bottom half, mirror it to the top half, then compute the
  1419. * center point.  Otherwise asymmetric quantization errors will occur.
  1420. *  The formula to integrate is e^-(x^2/2s^2).
  1421. */
  1422. /* first we do the top (right) half of matrix */
  1423. for (i = matrix_length/2 + 1; i < matrix_length; i++)
  1424.     {
  1425. float base_x = i - (float)floor((float)(matrix_length/2)) - 0.5f;
  1426. sum = 0;
  1427. for (j = 1; j <= 50; j++)
  1428. {
  1429. if ( base_x+0.02*j <= radius ) 
  1430. sum += (float)exp (-(base_x+0.02*j)*(base_x+0.02*j) / 
  1431. (2*std_dev*std_dev));
  1432. }
  1433. cmatrix[i] = sum/50;
  1434.     }
  1435. /* mirror the thing to the bottom half */
  1436. for (i=0; i<=matrix_length/2; i++) {
  1437. cmatrix[i] = cmatrix[matrix_length-1-i];
  1438. }
  1439. /* find center val -- calculate an odd number of quanta to make it symmetric,
  1440. * even if the center point is weighted slightly higher than others. */
  1441. sum = 0;
  1442. for (j=0; j<=50; j++)
  1443.     {
  1444. sum += (float)exp (-(0.5+0.02*j)*(0.5+0.02*j) /
  1445. (2*std_dev*std_dev));
  1446.     }
  1447. cmatrix[matrix_length/2] = sum/51;
  1448. /* normalize the distribution by scaling the total sum to one */
  1449. sum=0;
  1450. for (i=0; i<matrix_length; i++) sum += cmatrix[i];
  1451. for (i=0; i<matrix_length; i++) cmatrix[i] = cmatrix[i] / sum;
  1452. return matrix_length;
  1453. }
  1454. ////////////////////////////////////////////////////////////////////////////////
  1455. /* ----------------------- gen_lookup_table ----------------------- */
  1456. /* <nipper> generates a lookup table for every possible product of 0-255 and
  1457. each value in the convolution matrix.  The returned array is
  1458. indexed first by matrix position, then by input multiplicand (?)
  1459. value.
  1460. */
  1461. float* CxImage::gen_lookup_table (float *cmatrix, int cmatrix_length)
  1462. {
  1463. float* lookup_table = new float[cmatrix_length * 256];
  1464. float* lookup_table_p = lookup_table;
  1465. float* cmatrix_p      = cmatrix;
  1466. for (int i=0; i<cmatrix_length; i++)
  1467.     {
  1468. for (int j=0; j<256; j++)
  1469. {
  1470. *(lookup_table_p++) = *cmatrix_p * (float)j;
  1471. }
  1472. cmatrix_p++;
  1473.     }
  1474. return lookup_table;
  1475. }
  1476. ////////////////////////////////////////////////////////////////////////////////
  1477. /* <nipper> this function is written as if it is blurring a column at a time,
  1478. even though it can operate on rows, too.  There is no difference
  1479. in the processing of the lines, at least to the blur_line function. */
  1480. void CxImage::blur_line (float *ctable, float *cmatrix, int cmatrix_length, BYTE* cur_col, BYTE* dest_col, int y, long bytes)
  1481. {
  1482. float scale;
  1483. float sum;
  1484. int i=0, j=0;
  1485. int row;
  1486. int cmatrix_middle = cmatrix_length/2;
  1487. float *cmatrix_p;
  1488. BYTE  *cur_col_p;
  1489. BYTE  *cur_col_p1;
  1490. BYTE  *dest_col_p;
  1491. float *ctable_p;
  1492. /* this first block is the same as the non-optimized version --
  1493. * it is only used for very small pictures, so speed isn't a
  1494. * big concern.
  1495. */
  1496. if (cmatrix_length > y)
  1497.     {
  1498. for (row = 0; row < y ; row++)
  1499. {
  1500. scale=0;
  1501. /* find the scale factor */
  1502. for (j = 0; j < y ; j++)
  1503. {
  1504. /* if the index is in bounds, add it to the scale counter */
  1505. if ((j + cmatrix_length/2 - row >= 0) &&
  1506. (j + cmatrix_length/2 - row < cmatrix_length))
  1507. scale += cmatrix[j + cmatrix_length/2 - row];
  1508. }
  1509. for (i = 0; i<bytes; i++)
  1510. {
  1511. sum = 0;
  1512. for (j = 0; j < y; j++)
  1513. {
  1514. if ((j >= row - cmatrix_length/2) &&
  1515. (j <= row + cmatrix_length/2))
  1516. sum += cur_col[j*bytes + i] * cmatrix[j];
  1517. }
  1518. dest_col[row*bytes + i] = (BYTE)(0.5f + sum / scale);
  1519. }
  1520. }
  1521.     }
  1522. else
  1523.     {
  1524. /* for the edge condition, we only use available info and scale to one */
  1525. for (row = 0; row < cmatrix_middle; row++)
  1526. {
  1527. /* find scale factor */
  1528. scale=0;
  1529. for (j = cmatrix_middle - row; j<cmatrix_length; j++)
  1530. scale += cmatrix[j];
  1531. for (i = 0; i<bytes; i++)
  1532. {
  1533. sum = 0;
  1534. for (j = cmatrix_middle - row; j<cmatrix_length; j++)
  1535. {
  1536. sum += cur_col[(row + j-cmatrix_middle)*bytes + i] * cmatrix[j];
  1537. }
  1538. dest_col[row*bytes + i] = (BYTE)(0.5f + sum / scale);
  1539. }
  1540. }
  1541. /* go through each pixel in each col */
  1542. dest_col_p = dest_col + row*bytes;
  1543. for (; row < y-cmatrix_middle; row++)
  1544. {
  1545. cur_col_p = (row - cmatrix_middle) * bytes + cur_col;
  1546. for (i = 0; i<bytes; i++)
  1547. {
  1548. sum = 0;
  1549. cmatrix_p = cmatrix;
  1550. cur_col_p1 = cur_col_p;
  1551. ctable_p = ctable;
  1552. for (j = cmatrix_length; j>0; j--)
  1553. {
  1554. sum += *(ctable_p + *cur_col_p1);
  1555. cur_col_p1 += bytes;
  1556. ctable_p += 256;
  1557. }
  1558. cur_col_p++;
  1559. *(dest_col_p++) = (BYTE)(0.5f + sum);
  1560. }
  1561. }
  1562. /* for the edge condition , we only use available info, and scale to one */
  1563. for (; row < y; row++)
  1564. {
  1565. /* find scale factor */
  1566. scale=0;
  1567. for (j = 0; j< y-row + cmatrix_middle; j++)
  1568. scale += cmatrix[j];
  1569. for (i = 0; i<bytes; i++)
  1570. {
  1571. sum = 0;
  1572. for (j = 0; j<y-row + cmatrix_middle; j++)
  1573. {
  1574. sum += cur_col[(row + j-cmatrix_middle)*bytes + i] * cmatrix[j];
  1575. }
  1576. dest_col[row*bytes + i] = (BYTE) (0.5f + sum / scale);
  1577. }
  1578. }
  1579.     }
  1580. }
  1581. ////////////////////////////////////////////////////////////////////////////////
  1582. /* <nipper> */
  1583. bool CxImage::UnsharpMask(float radius /*= 5.0*/, float amount /*= 0.5*/, int threshold /*= 0*/)
  1584. {
  1585. if (!pDib) return false;
  1586. if (!(head.biBitCount == 24 || IsGrayScale()))
  1587. return false;
  1588. CxImage tmp(*this);
  1589. CImageIterator itSrc(this);
  1590. CImageIterator itDst(&tmp);
  1591. // generate convolution matrix and make sure it's smaller than each dimension
  1592. float *cmatrix = NULL;
  1593. int cmatrix_length = gen_convolve_matrix(radius, &cmatrix);
  1594. // generate lookup table
  1595. float *ctable = gen_lookup_table(cmatrix, cmatrix_length);
  1596. double dbScaler = 33.3/head.biHeight;
  1597. // blur the rows
  1598.     for (int y=0;y<head.biHeight;y++)
  1599. {
  1600. if (info.nEscape) break;
  1601. info.nProgress = (long)(y*dbScaler);
  1602. blur_line(ctable, cmatrix, cmatrix_length, itSrc.GetRow(y), itDst.GetRow(y), head.biWidth, 3);
  1603. }
  1604. // blur the cols
  1605.     BYTE* cur_col = new BYTE[head.biHeight*3];
  1606.     BYTE* dest_col = new BYTE[head.biHeight*3];
  1607. dbScaler = 33.3/head.biWidth;
  1608. int x;
  1609. for (x=0;x<head.biWidth;x++)
  1610. {
  1611. if (info.nEscape) break;
  1612. info.nProgress = (long)(33.3+x*dbScaler);
  1613. itSrc.GetCol(cur_col, x);
  1614. itDst.GetCol(dest_col, x);
  1615. blur_line(ctable, cmatrix, cmatrix_length, cur_col, dest_col, head.biHeight, 3);
  1616. itSrc.SetCol(cur_col, x);
  1617. itDst.SetCol(dest_col, x);
  1618. }
  1619. delete [] cur_col;
  1620. delete [] dest_col;
  1621. delete [] cmatrix;
  1622. delete [] ctable;
  1623. // these are used for the merging step 
  1624. int diff;
  1625. int value;
  1626. dbScaler = 33.3/head.biHeight;
  1627. // merge the source and destination (which currently contains
  1628. // the blurred version) images
  1629.     for (LONG y=0;y<head.biHeight;y++)
  1630. {
  1631. if (info.nEscape) break;
  1632. info.nProgress = (long)(66.6+y*dbScaler);
  1633. value = 0;
  1634. // get source row
  1635. BYTE* cur_row = itSrc.GetRow(y);
  1636. // get dest row
  1637. BYTE* dest_row = itDst.GetRow(y);
  1638. // combine the two
  1639. for (int u = 0; u < x; u++)
  1640. {
  1641. for (int v = 0; v < 3; v++)
  1642. {
  1643. diff = (cur_row[u*3+v] - dest_row[u*3+v]);
  1644. // do tresholding
  1645. if (abs (2 * diff) < threshold)
  1646. diff = 0;
  1647. value = int(cur_row[u*3+v] + amount * diff);
  1648. if (value < 0) dest_row[u*3+v] =0;
  1649. else if (value > 255) dest_row[u*3+v] = 255;
  1650. else  dest_row[u*3+v] = value;
  1651. }
  1652. }
  1653. }
  1654. Transfer(tmp);
  1655. return TRUE;
  1656. }
  1657. ////////////////////////////////////////////////////////////////////////////////
  1658. bool CxImage::Lut(BYTE* pLut)
  1659. {
  1660. if (!pDib || !pLut) return false;
  1661. RGBQUAD color;
  1662. double dbScaler;
  1663. if (head.biClrUsed==0){
  1664. long xmin,xmax,ymin,ymax;
  1665. if (pSelection){
  1666. xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
  1667. ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
  1668. } else {
  1669. // faster loop for full image
  1670. BYTE *iSrc=info.pImage;
  1671. for(unsigned long i=0; i < head.biSizeImage ; i++){
  1672. *iSrc++ = pLut[*iSrc];
  1673. }
  1674. return true;
  1675. }
  1676. dbScaler = 100.0/ymax;
  1677. for(long y=ymin; y<ymax; y++){
  1678. info.nProgress = (long)(y*dbScaler); //<Anatoly Ivasyuk>
  1679. for(long x=xmin; x<xmax; x++){
  1680. #if CXIMAGE_SUPPORT_SELECTION
  1681. if (SelectionIsInside(x,y))
  1682. #endif //CXIMAGE_SUPPORT_SELECTION
  1683. {
  1684. color = GetPixelColor(x,y);
  1685. color.rgbRed = pLut[color.rgbRed];
  1686. color.rgbGreen = pLut[color.rgbGreen];
  1687. color.rgbBlue = pLut[color.rgbBlue];
  1688. SetPixelColor(x,y,color);
  1689. }
  1690. }
  1691. }
  1692. #if CXIMAGE_SUPPORT_SELECTION
  1693. } else if (pSelection && (head.biBitCount==8) && IsGrayScale()){
  1694. long xmin,xmax,ymin,ymax;
  1695. xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
  1696. ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
  1697. dbScaler = 100.0/ymax;
  1698. for(long y=ymin; y<ymax; y++){
  1699. info.nProgress = (long)(y*dbScaler);
  1700. for(long x=xmin; x<xmax; x++){
  1701. if (SelectionIsInside(x,y))
  1702. {
  1703. SetPixelIndex(x,y,pLut[GetPixelIndex(x,y)]);
  1704. }
  1705. }
  1706. }
  1707. #endif //CXIMAGE_SUPPORT_SELECTION
  1708. } else {
  1709. for(DWORD j=0; j<head.biClrUsed; j++){
  1710. color = GetPaletteColor((BYTE)j);
  1711. color.rgbRed = pLut[color.rgbRed];
  1712. color.rgbGreen = pLut[color.rgbGreen];
  1713. color.rgbBlue = pLut[color.rgbBlue];
  1714. SetPaletteColor((BYTE)j,color);
  1715. }
  1716. }
  1717. return true;
  1718. }
  1719. ////////////////////////////////////////////////////////////////////////////////
  1720. bool CxImage::Lut(BYTE* pLutR, BYTE* pLutG, BYTE* pLutB, BYTE* pLutA)
  1721. {
  1722. if (!pDib || !pLutR || !pLutG || !pLutB) return false;
  1723. RGBQUAD color;
  1724. double dbScaler;
  1725. if (head.biClrUsed==0){
  1726. long xmin,xmax,ymin,ymax;
  1727. if (pSelection){
  1728. xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
  1729. ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
  1730. } else {
  1731. xmin = ymin = 0;
  1732. xmax = head.biWidth; ymax=head.biHeight;
  1733. }
  1734. dbScaler = 100.0/ymax;
  1735. for(long y=ymin; y<ymax; y++){
  1736. info.nProgress = (long)(y*dbScaler);
  1737. for(long x=xmin; x<xmax; x++){
  1738. #if CXIMAGE_SUPPORT_SELECTION
  1739. if (SelectionIsInside(x,y))
  1740. #endif //CXIMAGE_SUPPORT_SELECTION
  1741. {
  1742. color = GetPixelColor(x,y);
  1743. color.rgbRed =   pLutR[color.rgbRed];
  1744. color.rgbGreen = pLutG[color.rgbGreen];
  1745. color.rgbBlue =  pLutB[color.rgbBlue];
  1746. if (pLutA) color.rgbReserved=pLutA[color.rgbReserved];
  1747. SetPixelColor(x,y,color,true);
  1748. }
  1749. }
  1750. }
  1751. } else {
  1752. for(DWORD j=0; j<head.biClrUsed; j++){
  1753. color = GetPaletteColor((BYTE)j);
  1754. color.rgbRed =   pLutR[color.rgbRed];
  1755. color.rgbGreen = pLutG[color.rgbGreen];
  1756. color.rgbBlue =  pLutB[color.rgbBlue];
  1757. SetPaletteColor((BYTE)j,color);
  1758. }
  1759. }
  1760. return true;
  1761. }
  1762. ////////////////////////////////////////////////////////////////////////////////
  1763. /*bool CxImage::FloodFill(int x, int y, RGBQUAD FillColor)
  1764. {
  1765. //<JDL>
  1766. if (!pDib) return false;
  1767.     FloodFill2(x,y,GetPixelColor(x,y),FillColor);
  1768. return true;
  1769. }
  1770. ////////////////////////////////////////////////////////////////////////////////
  1771. void CxImage::FloodFill2(int x, int y, RGBQUAD old_color, RGBQUAD new_color)
  1772. {
  1773. // Fill in the actual pixels. 
  1774. // Function steps recursively until it finds borders (color that is not old_color)
  1775. if (!IsInside(x,y)) return;
  1776. RGBQUAD r = GetPixelColor(x,y);
  1777. COLORREF cr = RGB(r.rgbRed,r.rgbGreen,r.rgbBlue);
  1778. if(cr == RGB(old_color.rgbRed,old_color.rgbGreen,old_color.rgbBlue)
  1779. && cr != RGB(new_color.rgbRed,new_color.rgbGreen,new_color.rgbBlue) ) {
  1780. // the above if statement, after && is there to prevent
  1781. // stack overflows.  The program will continue to find 
  1782. // colors if you flood-fill an entire region (entire picture)
  1783. SetPixelColor(x,y,new_color);
  1784. FloodFill2((x+1),y,old_color,new_color);
  1785. FloodFill2((x-1),y,old_color,new_color);
  1786. FloodFill2(x,(y+1),old_color,new_color);
  1787. FloodFill2(x,(y-1),old_color,new_color);
  1788. }
  1789. }*/
  1790. ///////////////////////////////////////////////////////////////////////////////
  1791. #endif //CXIMAGE_SUPPORT_DSP