PICTURE.CPP
Upload User: nthssl
Upload Date: 2022-04-05
Package Size: 25357k
Code Size: 9k
Category:

OpenCV

Development Platform:

Visual C++

  1. #include "stdafx.h"
  2. #include <memory.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. #include "jpeg.h"
  7. int sizes[]={ 1,2,4,8,16,32,64,128,256 };
  8. int nsizes=8;
  9. int byteRGB::Intensity(void)
  10. {
  11.     return (int)(r*299/1000 + g*587/1000 + b*114/1000);
  12. }
  13. void byteRGB::ToYIQ(void)
  14. {
  15. unsigned char y,i,q;
  16. y=(299*r+587*g+114*b)/1000;
  17. i=(596*r-275*g-321*b)/1192+127;
  18. q=(212*r-523*g+311*b)/1046+127;
  19. y=((int)y-127)*2/3+127;
  20. i=((int)i-127)*2/3+127;
  21. q=((int)q-127)*2/3+127;
  22. r=y;
  23. g=i;
  24. b=q;
  25. }
  26. void byteRGB::FromYIQ(void)
  27. {
  28. unsigned char y,i,q;
  29. int u,I,Q;
  30. I=((int)g-127)*166/127;
  31. Q=((int)b-127)*148/127;
  32. u=(int)r+(956*I+621*Q)/1000;
  33. u=(u-127)*3/2+127;
  34. y=u<0?0:u>255?255:u;
  35. u=(int)r-(272*I+647*Q)/1000;
  36. u=(u-127)*3/2+127;
  37. i=u<0?0:u>255?255:u;
  38. u=(int)r+(1703*Q-1106*I)/1000;
  39. u=(u-127)*3/2+127;
  40. q=u<0?0:u>255?255:u;
  41. r=y;
  42. g=i;
  43. b=q;
  44. }
  45. void picture::ErasePicture(char c)
  46. {
  47.      int a;
  48.      for( a=0;a<sy;a++ )
  49.           memset(buf[a],c,sx*bytespixel);
  50. }
  51. int picture::CreatePicture24(int xd,int yd)
  52. {
  53.     int a;
  54.     sx=xd; sy=yd; bytespixel=3;
  55.     if ((buf=new unsigned char *[sy])!=0)
  56.        {
  57. buf[0]=new unsigned char[sx*sy*bytespixel];
  58. if (!buf[0])
  59. {
  60. delete buf;
  61. buf=0;
  62. return 0;
  63. }
  64.         for( a=1;a<sy;a++ )
  65. buf[a]=&buf[0][a*sx*bytespixel];
  66.         return 1;
  67.        }
  68.     return 0;
  69. }
  70. int picture::CreatePicture8(int xd,int yd,int palsize)
  71. {
  72.     int a;
  73.     sx=xd; sy=yd; bytespixel=1;
  74.     if ((buf=new unsigned char *[sy])!=0)
  75.        {
  76. buf[0]=new unsigned char[sx*sy];
  77. if (!buf[0])
  78. {
  79. delete buf;
  80. buf=0;
  81. return 0;
  82. }
  83.         for( a=1;a<sy;a++ )
  84. buf[a]=&buf[0][a*sx];
  85. if (palsize)
  86. {
  87. pal=new byteRGB[palsize];
  88. npal=palsize;
  89. }
  90.         return 1;
  91.        }
  92.     return 0;
  93. }
  94. void picture::FreePicture(void)
  95. {
  96. if (buf)
  97. {
  98. delete buf[0];
  99. delete buf;
  100. }
  101. if (pal) 
  102. delete pal;
  103. if (icon)
  104. delete icon;
  105. buf=0;
  106. pal=0;
  107. npal=0;
  108. icon=0;
  109. }
  110. int picture::LoadTGA(char *name)
  111. {
  112.     FILE *fp;
  113.     int a;
  114. unsigned char flag;
  115. unsigned char TGA_INI[18];
  116.     sx=sy=0;
  117. FreePicture();
  118.     if ((fp=fopen(name,"rb"))!=0)
  119.        {
  120.         fread((char *)&TGA_INI[0],18,1,fp);
  121.   if (TGA_INI[16]!=24 || TGA_INI[2]!=2)
  122.   {
  123. fclose(fp);
  124. return 0;
  125.   }
  126. sx=TGA_INI[12]+256*TGA_INI[13];
  127.         sy=TGA_INI[14]+256*TGA_INI[15];
  128. CreatePicture24(sx,sy);
  129.         for( a=sy-1;a>=0;a-- )
  130.             {
  131.               fread((char *)buf[a],sx,3,fp);
  132.   int z=0,p=0;
  133.               for( ;z<sx;z++,p+=3 )
  134.                    {
  135.                     flag=buf[a][p+2];
  136.                     buf[a][p+2]=buf[a][p];
  137.                     buf[a][p]=flag;
  138.                    }
  139. }
  140.         fclose(fp);
  141. build_icon();
  142.         return 1;
  143.        }
  144.     return 0;
  145. }
  146. int picture::SaveTGA(char *name)
  147. {
  148.     FILE *fp;
  149.     int a,b;
  150.     unsigned char TGA_INI[18];
  151.     byteRGB *picline;
  152.     picline=new byteRGB[sx];
  153.     if (!picline)
  154.        return 0;
  155.     if ((fp=fopen(name,"wb"))!=0)
  156.        {
  157.         memset(&TGA_INI[0],0,18);
  158.         TGA_INI[12]=(unsigned char)(sx%256);
  159.         TGA_INI[13]=(unsigned char)(sx/256);
  160.         TGA_INI[14]=(unsigned char)(sy%256);
  161.         TGA_INI[15]=(unsigned char)(sy/256);
  162.         TGA_INI[2]=2;
  163.         TGA_INI[16]=0x18;
  164.         fwrite((char *)&TGA_INI[0],18,1,fp);
  165.         for( a=sy-1;a>=0;a-- )
  166.              {
  167.               for( b=0;b<sx;b++ )
  168.                    {
  169.                     picline[b].r=buf[a][b*bytespixel+2];
  170.                     picline[b].g=buf[a][b*bytespixel+1];
  171.                     picline[b].b=buf[a][b*bytespixel];
  172.                    }
  173.               if (fwrite((char *)picline,sx,3,fp)<3)
  174.                  {
  175.                   fclose(fp);
  176.                   delete picline;
  177.                   return 0;
  178.                  }
  179.              }
  180.         fclose(fp);
  181.        }
  182.     delete picline;
  183.     return 1;
  184. }
  185. void picture::build_icon()
  186. {
  187. if (buf)
  188. {
  189. int sizex,sizey,i;
  190. for( i=0;i<nsizes;i++ )
  191. if (sizes[i]>sx)
  192. break;
  193. if (i<nsizes) i--;
  194. sizex=sizes[i];
  195. for( i=0;i<nsizes;i++ )
  196. if (sizes[i]>sy)
  197. break;
  198. if (i<nsizes) i--;
  199. sizey=sizes[i];
  200. int x,y;
  201. icon=new unsigned char[sizex*sizey*bytespixel];
  202. unsigned char *p=icon;
  203. float dx,dy;
  204. dx=(float)sx/sizex;
  205. dy=(float)sy/sizey;
  206. iconsx=sizex;
  207. iconsy=sizey;
  208. for( y=0;y<sizey;y++ )
  209. for( x=0;x<sizex;x++ )
  210. if (bytespixel==3)
  211. {
  212. *(p++)=buf[(int)(y*dy)][(int)(x*dx)*bytespixel];
  213. *(p++)=buf[(int)(y*dy)][(int)(x*dx)*bytespixel+1];
  214. *(p++)=buf[(int)(y*dy)][(int)(x*dx)*bytespixel+2];
  215. }
  216. else *(p++)=buf[(int)(y*dy)][(int)(x*dx)];
  217. }
  218. }
  219. int picture::LoadBMP(char *name)
  220. {
  221. FILE *fp;
  222. int a,flag=0,wx;
  223. unsigned char *c;
  224. BITMAPFILEHEADER bmfh;
  225. struct { BITMAPINFOHEADER bmih;
  226. RGBQUAD bmcolors[256];
  227. } bmi;
  228. if ((fp=fopen(name,"rb"))!=0)
  229. {
  230. fread((char *)&bmfh,14,1,fp);          // sizeof(bmfh)==14
  231. fread((char *)&bmi,40,1,fp);           // sizeof(BITMAPINFOHEADER)==40
  232. if (bmi.bmih.biPlanes!=1||bmi.bmih.biCompression!=0||bmi.bmih.biBitCount!=8)
  233. {
  234. fclose(fp);
  235. return 0;
  236. }
  237. FreePicture();
  238. c=(unsigned char *)&bmi;
  239. sx=c[4]+256*c[5];
  240. sy=c[8]+256*c[9];
  241. bytespixel=1;
  242. fread((char *)&bmi.bmcolors[0],256,4,fp);
  243. CreatePicture8(sx,sy,256);
  244. for( a=0;a<256;a++ )
  245. {
  246. pal[a].r=bmi.bmcolors[a].rgbRed;
  247. pal[a].g=bmi.bmcolors[a].rgbGreen;
  248. pal[a].b=bmi.bmcolors[a].rgbBlue;
  249. }
  250. wx=(int)(bmi.bmih.biSizeImage/sy);
  251. for( a=sy-1;a>=0;a-- )
  252. fread( (char *)buf[a],wx,1,fp );
  253. fclose(fp);
  254. build_icon();
  255. return 1;
  256. }
  257. return 0;
  258. }
  259. int picture::LoadJPG(char *name)
  260. {
  261. FreePicture();
  262. JPEGDATA data;
  263. FILE *fp;
  264. fp=fopen(name,"rb");
  265. if (fp)
  266. {
  267. memset(&data,0,sizeof(JPEGDATA));
  268. data.input_file = fp;
  269. JpegInfo(&data);
  270. fclose(fp);
  271. if (data.components==3 || data.components==1)
  272. {
  273. bytespixel=data.components;
  274. if (bytespixel==3)
  275. CreatePicture24(data.width,data.height);
  276. else CreatePicture8(data.width,data.height,0);
  277. fp=fopen(name,"rb");
  278. data.ptr             = buf[0];
  279. data.input_file      = fp;
  280. data.hWnd            = 0;
  281. data.ProgressMsg     = 0;
  282. JpegRead(&data);
  283. fclose(fp);
  284. if (data.status==0)
  285. {
  286. build_icon();
  287. return 1;
  288. }
  289. else FreePicture();
  290. }
  291. }
  292. return 0;
  293. }
  294. void picture::ToYIQ()
  295. {
  296. if (bytespixel!=3)
  297. return ;
  298. int x,y;
  299. byteRGB *rgb;
  300. for( y=0;y<sy;y++ )
  301. {
  302. rgb=(byteRGB *)buf[y];
  303. for( x=0;x<sx;x++ )
  304. (rgb++)->ToYIQ();
  305. }
  306. }
  307. void picture::FromYIQ()
  308. {
  309. if (bytespixel!=3)
  310. return ;
  311. int x,y;
  312. byteRGB *rgb;
  313. for( y=0;y<sy;y++ )
  314. {
  315. rgb=(byteRGB *)buf[y];
  316. for( x=0;x<sx;x++ )
  317. (rgb++)->FromYIQ();
  318. }
  319. }
  320. void cor_inter(float xi[3], float yi[3], float c[3][3], float x, float y, byteRGB *rgb)
  321. {
  322. int a;
  323. float v[3][2],m[3][2],n[3];
  324. for( a=0;a<3;a++ )
  325. {
  326. v[a][0]=xi[a];
  327. v[a][1]=yi[a];
  328. }
  329. m[0][0]=x-v[0][0];
  330. m[0][1]=y-v[0][1];
  331. m[1][0]=v[2][0]-v[0][0];
  332. m[1][1]=v[2][1]-v[0][1];
  333. m[2][0]=v[1][0]-v[0][0];
  334. m[2][1]=v[1][1]-v[0][1];
  335. n[0]=m[1][0]*m[2][1]-m[1][1]*m[2][0];
  336. n[1]=m[0][0]*m[2][1]-m[0][1]*m[2][0];
  337. n[2]=m[0][0]*m[1][1]-m[0][1]*m[1][0];
  338. for( a=0;a<3;a++ )
  339. *((&rgb->r)+a)=(unsigned char)(((c[2][a]-c[0][a])*n[1]-(c[1][a]-c[0][a])*n[2])/n[0]+c[0][a]);
  340. }
  341. void picture::GetPixel(int x,int y,byteRGB *rgb)
  342. {
  343. if (buf==0)
  344. return;
  345. x=(x%sx);
  346. y=(y%sy);
  347. if(x<0)
  348. x=sx+x;
  349. if(y<0)
  350. y=sy+y;
  351. if (bytespixel==3)
  352. *rgb=*((byteRGB *)&buf[y][x*3]);
  353. else if (pal)
  354. *rgb=pal[buf[y][x]];
  355. else rgb->r=rgb->g=rgb->b=buf[y][x];
  356. }
  357. #define ROUND(x) ((x)>=0?((int)(x)):((int)(x)-1))
  358. void picture::GetPixel(float x, float y, byteRGB * rgb,int textinterp)
  359. {
  360. if (textinterp==0)
  361. {
  362. GetPixel(ROUND(x), ROUND(y), rgb);
  363. return;
  364. }
  365. float xi[3], yi[3];
  366. float cor[3][3];
  367. int ix=ROUND(x),iy=ROUND(y);
  368. byteRGB rgbt;
  369. if (buf==0)
  370. return;
  371. GetPixel(ix, iy, &rgbt);
  372. cor[2][0]=rgbt.r;
  373. cor[2][1]=rgbt.g;
  374. cor[2][2]=rgbt.b;
  375. GetPixel(ix, iy+1, &rgbt);
  376. cor[2][0]+=rgbt.r;
  377. cor[2][1]+=rgbt.g;
  378. cor[2][2]+=rgbt.b;
  379. GetPixel(ix+1, iy, &rgbt);
  380. cor[2][0]+=rgbt.r;
  381. cor[2][1]+=rgbt.g;
  382. cor[2][2]+=rgbt.b;
  383. GetPixel(ix+1, iy+1, &rgbt);
  384. cor[2][0]+=rgbt.r;
  385. cor[2][1]+=rgbt.g;
  386. cor[2][2]+=rgbt.b;
  387. cor[2][0]/=4;
  388. cor[2][1]/=4;
  389. cor[2][2]/=4;
  390. if((y-iy)>(ix+1-x))
  391. {
  392. xi[0]=(float)(ix+1);
  393. yi[0]=(float)(iy+1);
  394. }
  395. else
  396. {
  397. xi[0]=(float)(ix);
  398. yi[0]=(float)(iy);
  399. }
  400. if((x-ix)>(y-iy))
  401. {
  402. xi[1]=(float)(ix+1);
  403. yi[1]=(float)(iy);
  404. }
  405. else
  406. {
  407. xi[1]=(float)(ix);
  408. yi[1]=(float)(iy+1);
  409. }
  410. GetPixel((int)xi[0], (int)yi[0], &rgbt);
  411. cor[0][0]=rgbt.r;
  412. cor[0][1]=rgbt.g;
  413. cor[0][2]=rgbt.b;
  414. GetPixel((int)xi[1], (int)yi[1], &rgbt);
  415. cor[1][0]=rgbt.r;
  416. cor[1][1]=rgbt.g;
  417. cor[1][2]=rgbt.b;
  418. xi[2]=ix+0.5f;
  419. yi[2]=iy+0.5f;
  420. cor_inter(xi, yi, cor, x, y, rgb);
  421. }