Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
PICTURE.CPP
Package: openglsystem.rar [view]
Upload User: nthssl
Upload Date: 2022-04-05
Package Size: 25357k
Code Size: 9k
Category:
OpenCV
Development Platform:
Visual C++
- #include "stdafx.h"
- #include <memory.h>
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include "jpeg.h"
- int sizes[]={ 1,2,4,8,16,32,64,128,256 };
- int nsizes=8;
- int byteRGB::Intensity(void)
- {
- return (int)(r*299/1000 + g*587/1000 + b*114/1000);
- }
- void byteRGB::ToYIQ(void)
- {
- unsigned char y,i,q;
- y=(299*r+587*g+114*b)/1000;
- i=(596*r-275*g-321*b)/1192+127;
- q=(212*r-523*g+311*b)/1046+127;
- y=((int)y-127)*2/3+127;
- i=((int)i-127)*2/3+127;
- q=((int)q-127)*2/3+127;
- r=y;
- g=i;
- b=q;
- }
- void byteRGB::FromYIQ(void)
- {
- unsigned char y,i,q;
- int u,I,Q;
- I=((int)g-127)*166/127;
- Q=((int)b-127)*148/127;
- u=(int)r+(956*I+621*Q)/1000;
- u=(u-127)*3/2+127;
- y=u<0?0:u>255?255:u;
- u=(int)r-(272*I+647*Q)/1000;
- u=(u-127)*3/2+127;
- i=u<0?0:u>255?255:u;
- u=(int)r+(1703*Q-1106*I)/1000;
- u=(u-127)*3/2+127;
- q=u<0?0:u>255?255:u;
- r=y;
- g=i;
- b=q;
- }
- void picture::ErasePicture(char c)
- {
- int a;
- for( a=0;a<sy;a++ )
- memset(buf[a],c,sx*bytespixel);
- }
- int picture::CreatePicture24(int xd,int yd)
- {
- int a;
- sx=xd; sy=yd; bytespixel=3;
- if ((buf=new unsigned char *[sy])!=0)
- {
- buf[0]=new unsigned char[sx*sy*bytespixel];
- if (!buf[0])
- {
- delete buf;
- buf=0;
- return 0;
- }
- for( a=1;a<sy;a++ )
- buf[a]=&buf[0][a*sx*bytespixel];
- return 1;
- }
- return 0;
- }
- int picture::CreatePicture8(int xd,int yd,int palsize)
- {
- int a;
- sx=xd; sy=yd; bytespixel=1;
- if ((buf=new unsigned char *[sy])!=0)
- {
- buf[0]=new unsigned char[sx*sy];
- if (!buf[0])
- {
- delete buf;
- buf=0;
- return 0;
- }
- for( a=1;a<sy;a++ )
- buf[a]=&buf[0][a*sx];
- if (palsize)
- {
- pal=new byteRGB[palsize];
- npal=palsize;
- }
- return 1;
- }
- return 0;
- }
- void picture::FreePicture(void)
- {
- if (buf)
- {
- delete buf[0];
- delete buf;
- }
- if (pal)
- delete pal;
- if (icon)
- delete icon;
- buf=0;
- pal=0;
- npal=0;
- icon=0;
- }
- int picture::LoadTGA(char *name)
- {
- FILE *fp;
- int a;
- unsigned char flag;
- unsigned char TGA_INI[18];
- sx=sy=0;
- FreePicture();
- if ((fp=fopen(name,"rb"))!=0)
- {
- fread((char *)&TGA_INI[0],18,1,fp);
- if (TGA_INI[16]!=24 || TGA_INI[2]!=2)
- {
- fclose(fp);
- return 0;
- }
- sx=TGA_INI[12]+256*TGA_INI[13];
- sy=TGA_INI[14]+256*TGA_INI[15];
- CreatePicture24(sx,sy);
- for( a=sy-1;a>=0;a-- )
- {
- fread((char *)buf[a],sx,3,fp);
- int z=0,p=0;
- for( ;z<sx;z++,p+=3 )
- {
- flag=buf[a][p+2];
- buf[a][p+2]=buf[a][p];
- buf[a][p]=flag;
- }
- }
- fclose(fp);
- build_icon();
- return 1;
- }
- return 0;
- }
- int picture::SaveTGA(char *name)
- {
- FILE *fp;
- int a,b;
- unsigned char TGA_INI[18];
- byteRGB *picline;
- picline=new byteRGB[sx];
- if (!picline)
- return 0;
- if ((fp=fopen(name,"wb"))!=0)
- {
- memset(&TGA_INI[0],0,18);
- TGA_INI[12]=(unsigned char)(sx%256);
- TGA_INI[13]=(unsigned char)(sx/256);
- TGA_INI[14]=(unsigned char)(sy%256);
- TGA_INI[15]=(unsigned char)(sy/256);
- TGA_INI[2]=2;
- TGA_INI[16]=0x18;
- fwrite((char *)&TGA_INI[0],18,1,fp);
- for( a=sy-1;a>=0;a-- )
- {
- for( b=0;b<sx;b++ )
- {
- picline[b].r=buf[a][b*bytespixel+2];
- picline[b].g=buf[a][b*bytespixel+1];
- picline[b].b=buf[a][b*bytespixel];
- }
- if (fwrite((char *)picline,sx,3,fp)<3)
- {
- fclose(fp);
- delete picline;
- return 0;
- }
- }
- fclose(fp);
- }
- delete picline;
- return 1;
- }
- void picture::build_icon()
- {
- if (buf)
- {
- int sizex,sizey,i;
- for( i=0;i<nsizes;i++ )
- if (sizes[i]>sx)
- break;
- if (i<nsizes) i--;
- sizex=sizes[i];
- for( i=0;i<nsizes;i++ )
- if (sizes[i]>sy)
- break;
- if (i<nsizes) i--;
- sizey=sizes[i];
- int x,y;
- icon=new unsigned char[sizex*sizey*bytespixel];
- unsigned char *p=icon;
- float dx,dy;
- dx=(float)sx/sizex;
- dy=(float)sy/sizey;
- iconsx=sizex;
- iconsy=sizey;
- for( y=0;y<sizey;y++ )
- for( x=0;x<sizex;x++ )
- if (bytespixel==3)
- {
- *(p++)=buf[(int)(y*dy)][(int)(x*dx)*bytespixel];
- *(p++)=buf[(int)(y*dy)][(int)(x*dx)*bytespixel+1];
- *(p++)=buf[(int)(y*dy)][(int)(x*dx)*bytespixel+2];
- }
- else *(p++)=buf[(int)(y*dy)][(int)(x*dx)];
- }
- }
- int picture::LoadBMP(char *name)
- {
- FILE *fp;
- int a,flag=0,wx;
- unsigned char *c;
- BITMAPFILEHEADER bmfh;
- struct { BITMAPINFOHEADER bmih;
- RGBQUAD bmcolors[256];
- } bmi;
- if ((fp=fopen(name,"rb"))!=0)
- {
- fread((char *)&bmfh,14,1,fp); // sizeof(bmfh)==14
- fread((char *)&bmi,40,1,fp); // sizeof(BITMAPINFOHEADER)==40
- if (bmi.bmih.biPlanes!=1||bmi.bmih.biCompression!=0||bmi.bmih.biBitCount!=8)
- {
- fclose(fp);
- return 0;
- }
- FreePicture();
- c=(unsigned char *)&bmi;
- sx=c[4]+256*c[5];
- sy=c[8]+256*c[9];
- bytespixel=1;
- fread((char *)&bmi.bmcolors[0],256,4,fp);
- CreatePicture8(sx,sy,256);
- for( a=0;a<256;a++ )
- {
- pal[a].r=bmi.bmcolors[a].rgbRed;
- pal[a].g=bmi.bmcolors[a].rgbGreen;
- pal[a].b=bmi.bmcolors[a].rgbBlue;
- }
- wx=(int)(bmi.bmih.biSizeImage/sy);
- for( a=sy-1;a>=0;a-- )
- fread( (char *)buf[a],wx,1,fp );
- fclose(fp);
- build_icon();
- return 1;
- }
- return 0;
- }
- int picture::LoadJPG(char *name)
- {
- FreePicture();
- JPEGDATA data;
- FILE *fp;
- fp=fopen(name,"rb");
- if (fp)
- {
- memset(&data,0,sizeof(JPEGDATA));
- data.input_file = fp;
- JpegInfo(&data);
- fclose(fp);
- if (data.components==3 || data.components==1)
- {
- bytespixel=data.components;
- if (bytespixel==3)
- CreatePicture24(data.width,data.height);
- else CreatePicture8(data.width,data.height,0);
- fp=fopen(name,"rb");
- data.ptr = buf[0];
- data.input_file = fp;
- data.hWnd = 0;
- data.ProgressMsg = 0;
- JpegRead(&data);
- fclose(fp);
- if (data.status==0)
- {
- build_icon();
- return 1;
- }
- else FreePicture();
- }
- }
- return 0;
- }
- void picture::ToYIQ()
- {
- if (bytespixel!=3)
- return ;
- int x,y;
- byteRGB *rgb;
- for( y=0;y<sy;y++ )
- {
- rgb=(byteRGB *)buf[y];
- for( x=0;x<sx;x++ )
- (rgb++)->ToYIQ();
- }
- }
- void picture::FromYIQ()
- {
- if (bytespixel!=3)
- return ;
- int x,y;
- byteRGB *rgb;
- for( y=0;y<sy;y++ )
- {
- rgb=(byteRGB *)buf[y];
- for( x=0;x<sx;x++ )
- (rgb++)->FromYIQ();
- }
- }
- void cor_inter(float xi[3], float yi[3], float c[3][3], float x, float y, byteRGB *rgb)
- {
- int a;
- float v[3][2],m[3][2],n[3];
- for( a=0;a<3;a++ )
- {
- v[a][0]=xi[a];
- v[a][1]=yi[a];
- }
- m[0][0]=x-v[0][0];
- m[0][1]=y-v[0][1];
- m[1][0]=v[2][0]-v[0][0];
- m[1][1]=v[2][1]-v[0][1];
- m[2][0]=v[1][0]-v[0][0];
- m[2][1]=v[1][1]-v[0][1];
- n[0]=m[1][0]*m[2][1]-m[1][1]*m[2][0];
- n[1]=m[0][0]*m[2][1]-m[0][1]*m[2][0];
- n[2]=m[0][0]*m[1][1]-m[0][1]*m[1][0];
- for( a=0;a<3;a++ )
- *((&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]);
- }
- void picture::GetPixel(int x,int y,byteRGB *rgb)
- {
- if (buf==0)
- return;
- x=(x%sx);
- y=(y%sy);
- if(x<0)
- x=sx+x;
- if(y<0)
- y=sy+y;
- if (bytespixel==3)
- *rgb=*((byteRGB *)&buf[y][x*3]);
- else if (pal)
- *rgb=pal[buf[y][x]];
- else rgb->r=rgb->g=rgb->b=buf[y][x];
- }
- #define ROUND(x) ((x)>=0?((int)(x)):((int)(x)-1))
- void picture::GetPixel(float x, float y, byteRGB * rgb,int textinterp)
- {
- if (textinterp==0)
- {
- GetPixel(ROUND(x), ROUND(y), rgb);
- return;
- }
- float xi[3], yi[3];
- float cor[3][3];
- int ix=ROUND(x),iy=ROUND(y);
- byteRGB rgbt;
- if (buf==0)
- return;
- GetPixel(ix, iy, &rgbt);
- cor[2][0]=rgbt.r;
- cor[2][1]=rgbt.g;
- cor[2][2]=rgbt.b;
- GetPixel(ix, iy+1, &rgbt);
- cor[2][0]+=rgbt.r;
- cor[2][1]+=rgbt.g;
- cor[2][2]+=rgbt.b;
- GetPixel(ix+1, iy, &rgbt);
- cor[2][0]+=rgbt.r;
- cor[2][1]+=rgbt.g;
- cor[2][2]+=rgbt.b;
- GetPixel(ix+1, iy+1, &rgbt);
- cor[2][0]+=rgbt.r;
- cor[2][1]+=rgbt.g;
- cor[2][2]+=rgbt.b;
- cor[2][0]/=4;
- cor[2][1]/=4;
- cor[2][2]/=4;
- if((y-iy)>(ix+1-x))
- {
- xi[0]=(float)(ix+1);
- yi[0]=(float)(iy+1);
- }
- else
- {
- xi[0]=(float)(ix);
- yi[0]=(float)(iy);
- }
- if((x-ix)>(y-iy))
- {
- xi[1]=(float)(ix+1);
- yi[1]=(float)(iy);
- }
- else
- {
- xi[1]=(float)(ix);
- yi[1]=(float)(iy+1);
- }
- GetPixel((int)xi[0], (int)yi[0], &rgbt);
- cor[0][0]=rgbt.r;
- cor[0][1]=rgbt.g;
- cor[0][2]=rgbt.b;
- GetPixel((int)xi[1], (int)yi[1], &rgbt);
- cor[1][0]=rgbt.r;
- cor[1][1]=rgbt.g;
- cor[1][2]=rgbt.b;
- xi[2]=ix+0.5f;
- yi[2]=iy+0.5f;
- cor_inter(xi, yi, cor, x, y, rgb);
- }