rwrgb.txt
上传用户:zbjingming
上传日期:2010-01-02
资源大小:2436k
文件大小:7k
- From cyy Wed Aug 11 10:03:15 1999
- Return-Path: <cyy>
- Received: by ox (SMI-8.6/SMI-SVR4)
- id KAA00559; Wed, 11 Aug 1999 10:03:15 +0900
- Date: Wed, 11 Aug 1999 10:03:15 +0900
- From: cyy (Chen Yanyun)
- Message-Id: <199908110103.KAA00559@ox>
- To: zhx
- Content-Length: 6251
- X-Lines: 258
- Status: RO
- /*==============================================================*
- | function: write_bmp |
- *==============================================================*/
- void write_bmp(char *filename)
- {
- int viewx,viewy;
- int x,y,i,j;
- int Xlen, Ylen;
- GLint port[4];
- unsigned char pcrgb;
- BITMAPFILEHEADER head;BITMAPINFOHEADER info;
- FILE *fp;
- unsigned short *rbuf;
- unsigned short *gbuf;
- unsigned short *bbuf;
- unsigned char *pbuf;
-
- glGetIntegerv(GL_VIEWPORT,port);
- viewx = port[2]-port[0];
- viewy = port[3]-port[1];
- Xlen = viewx;
- Ylen = viewy;
-
- pbuf = (unsigned char *) malloc(viewx*viewy*3);
- rbuf = (unsigned short *)malloc(Xlen*sizeof(short));
- gbuf = (unsigned short *)malloc(Xlen*sizeof(short));
- bbuf = (unsigned short *)malloc(Xlen*sizeof(short));
- if ( (pbuf == NULL)||(rbuf == NULL)||(gbuf == NULL)||(bbuf == NULL) )
- {
- printf("Malloc bbuf mamory failure n");
- exit(0);
- }
- glReadPixels(0,0,viewx,viewy,GL_RGB,GL_UNSIGNED_BYTE,pbuf);
- fp=fopen(filename,"wb");
- if(fp==NULL) {printf("Can't Create New Filen");exit(-1);}
- head.bfType=0x4d42;
- head.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+
- Xlen*Ylen*3;
- head.bfReserved1=0;head.bfReserved2=0;
- head.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
- info.biSize=sizeof(BITMAPINFOHEADER);
- info.biWidth=Xlen;
- info.biHeight=Ylen;
- info.biPlanes=1;
- info.biBitCount=24;
- info.biCompression=0;
- info.biSizeImage=0;
- info.biXPelsPerMeter=0;
- info.biYPelsPerMeter=0;
- info.biClrUsed=0;
- info.biClrImportant=0;
- fwrite(&head,sizeof(BITMAPFILEHEADER),1,fp);
- fwrite(&info,sizeof(BITMAPINFOHEADER),1,fp);
- i=0;
- for( y=0; y<Ylen; y++)
- {
- for(x=0; x<Xlen; x++)
- {
- rbuf[x] = (short)(pbuf[(i+x)*3]);
- gbuf[x] = (short)(pbuf[(i+x)*3+1]);
- bbuf[x] = (short)(pbuf[(i+x)*3+2]);
- }
- i += Xlen;
- for(j=0;j<Xlen;j++)
- { pcrgb=(unsigned char)bbuf[j];
- fwrite(&pcrgb,1,1,fp);
- pcrgb=(unsigned char)gbuf[j];
- fwrite(&pcrgb,1,1,fp);
- pcrgb=(unsigned char)rbuf[j];
- fwrite(&pcrgb,1,1,fp);
- }
- j=Xlen;
- if(j%4!=0) j=4-j%4;
- else j=0;
- pcrgb=0;
- while(j>0)
- { fwrite(&pcrgb,1,1,fp);
- j--;
- }/* this is because windows bmp's characteristic */
- }
- fclose(fp);
- free(pbuf);
- free(rbuf);
- free(gbuf);
- free(bbuf);
- }/*____write_bmp____*/
- ///////////////////////////////////////////////////////////////////
- /*==============================================================*
- | function: OpenTexture |
- *==============================================================*/
- void OpenTexture( char *fn )
- {
- #if platform==windowsnt
- BITMAPFILEHEADER head;BITMAPINFOHEADER info;
- unsigned char *ImageData,ddd;FILE *fp;
- int index;
- int i,j,rslp, rslx, rsly;
- fp=fopen(fn,"rb");
- if(fp==NULL) {printf("Can't Create New Filen");exit(-1);}
-
- fread(&head,sizeof(BITMAPFILEHEADER),1,fp);
- fread(&info,sizeof(BITMAPINFOHEADER),1,fp);
- rslx=info.biWidth;
- rsly=info.biHeight;
- if(rslx%4!=0) rslp=rslx+4-rslx%4;
- else rslp=rslx;
- ImageData=(unsigned char *) malloc(rslp*rsly*3);
- if(ImageData==NULL){
- printf("Malloc ImageData mamory failure n");
- exit(0);
- }
-
- fread(ImageData,1,rslp*rsly*3,fp);
-
- rslp*=3;
- for(i=0;i<rsly;i++){
- index=i*rslp;
- for(j=0;j<rslx;j++){
- ddd=ImageData[index];
- ImageData[index]=ImageData[index+2];
- ImageData[index+2]=ddd;
- index+=3;
- }
- }
- fclose(fp);
- #else
- IMAGE *image;
- unsigned short srcx, srcy, srcz,
- x, y;
- unsigned int i, rslp, srcp, srcp2, rslx, rsly;
- unsigned short *rbuf, *gbuf, *bbuf;
- float daltarx, daltary;
- char *SImageData, *ImageData;
- /*
- * load a image
- */
- if((image = iopen(fn, "r", RLE(1), 3, srcx, srcy, 3)) == NULL)
- { printf("n Can not open %sn", fn); return; }
-
- srcx = image->xsize;
- srcy = image->ysize;
- srcz = image->zsize;
- if( srcz < 3 )
- { printf("n Can not read %sn", fn); return; }
- SImageData = (unsigned char *)malloc(srcx*srcy*3);
- if(SImageData==NULL){
- printf("Malloc SImageData mamory failure n");
- exit(0);
- }
- rbuf = (unsigned short*)malloc(srcx*sizeof(short));
- if(rbuf==NULL){
- printf("Malloc rbuf mamory failure n");
- exit(0);
- }
- gbuf = (unsigned short*)malloc(srcx*sizeof(short));
- if(gbuf==NULL){
- printf("Malloc gbuf mamory failure n");
- exit(0);
- }
- bbuf = (unsigned short*)malloc(srcx*sizeof(short));
- if(bbuf==NULL){
- printf("Malloc bbuf mamory failure n");
- exit(0);
- }
-
- i = 0;
- for( y=0; y<srcy; y++)
- {
- getrow(image, rbuf,y,0);
- getrow(image, gbuf,y,1);
- getrow(image, bbuf,y,2);
- for(x=0; x<srcx; x++)
- {
- SImageData[i] = (unsigned char)rbuf[x]; i++;
- SImageData[i] = (unsigned char)gbuf[x]; i++;
- SImageData[i] = (unsigned char)bbuf[x]; i++;
- }
- }
- iclose(image);
- /*
- * Texture size
- */
- rslx = rsly = 1;
- while( (rslx<<1) <= srcx ) rslx <<= 1;
- while( (rsly<<1) <= srcy ) rsly <<= 1;
- ImageData = (unsigned char *)malloc( rslx * rsly * 3 );
- if(ImageData==NULL){
- printf("Malloc ImageData mamory failure n");
- exit(0);
- }
- /*
- * Calc. daltar
- */
- daltarx = (float)srcx/(float)rslx;
- daltary = (float)srcy/(float)rsly;
- /*
- * map source picture to texture-image
- */
- rslp = srcp = 0;
- for( y = 0; y < rsly; y++ )
- {
- srcp2 = srcx * 3 * (int)( daltary*y );
- rslp = y * rslx * 3;
- for( x = 0; x < rslx; x ++ )
- {
- ImageData[ rslp ] = SImageData[ srcp ];
- ImageData[ rslp + 1 ] = SImageData[ srcp + 1 ];
- ImageData[ rslp + 2 ] = SImageData[ srcp + 2 ];
- rslp += 3;
- srcp = srcp2 + 3 * (int)((x+1) * daltarx);
- }
- }
- /*
- * free memory
- */
- free( rbuf );
- free( gbuf );
- free( bbuf );
- free( SImageData );
- #endif
-
- /*
- * Enable 2D texture
- */
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexImage2D ( GL_TEXTURE_2D, 0, 3, rslx, rsly,
- 0,GL_RGB, GL_UNSIGNED_BYTE, ImageData );
- glEnable(GL_TEXTURE_2D);
-
- free( ImageData );
- }/*____Open Texture____*/
- ////////////////////////////////////////////////////////////////