//========= Copyright Valve Corporation, All rights reserved. ============// // // Purpose: // //===========================================================================// #include #include #include #include #include #include "bitmap/float_bm.h" #include #include "bitmap/imageformat.h" #include "bitmap/tgaloader.h" #include "tier1/strtools.h" #include "filesystem.h" #define SQ(x) ((x)*(x)) // linear interpolate between 2 control points (L,R) inline float LinInterp(float frac, float L, float R) { return (((R-L) * frac) + L); } // bilinear interpolate between 4 control points (UL,UR,LL,LR) inline float BiLinInterp(float Xfrac, float Yfrac, float UL, float UR, float LL, float LR) { float iu = LinInterp(Xfrac, UL, UR); float il = LinInterp(Xfrac, LL, LR); return( LinInterp(Yfrac, iu, il) ); } FloatBitMap_t::FloatBitMap_t(int width, int height) { RGBAData=0; AllocateRGB(width,height); } FloatBitMap_t::FloatBitMap_t(FloatBitMap_t const *orig) { RGBAData=0; AllocateRGB(orig->Width,orig->Height); memcpy(RGBAData,orig->RGBAData,Width*Height*sizeof(float)*4); } static char GetChar(FileHandle_t &f) { char a; g_pFullFileSystem->Read(&a,1,f); return a; } static int GetInt(FileHandle_t &f) { char buf[100]; char *bout=buf; for(;;) { char c=GetChar(f); if ((c<'0') || (c>'9')) break; *(bout++)=c; } *(bout++)=0; return atoi(buf); } #define PFM_MAX_XSIZE 2048 bool FloatBitMap_t::LoadFromPFM(char const *fname) { FileHandle_t f = g_pFullFileSystem->Open(fname, "rb"); if (f) { if( ( GetChar(f) == 'P' ) && (GetChar(f) == 'F' ) && ( GetChar(f) == '\n' )) { Width=GetInt(f); Height=GetInt(f); // eat crap until the next newline while( GetChar(f) != '\n') { } // printf("file %s w=%d h=%d\n",fname,Width,Height); AllocateRGB(Width,Height); for( int y = Height-1; y >= 0; y-- ) { float linebuffer[PFM_MAX_XSIZE*3]; g_pFullFileSystem->Read(linebuffer,3*Width*sizeof(float),f); for(int x=0;xClose( f ); // close file after reading } return (RGBAData!=0); } bool FloatBitMap_t::WritePFM(char const *fname) { FileHandle_t f = g_pFullFileSystem->Open(fname, "wb"); if ( f ) { g_pFullFileSystem->FPrintf(f,"PF\n%d %d\n-1.000000\n",Width,Height); for( int y = Height-1; y >= 0; y-- ) { float linebuffer[PFM_MAX_XSIZE*3]; for(int x=0;xWrite(linebuffer,3*Width*sizeof(float),f); } g_pFullFileSystem->Close(f); return true; } return false; } float FloatBitMap_t::InterpolatedPixel(float x, float y, int comp) const { int Top= floor(y); float Yfrac= y - Top; int Bot= min(Height-1,Top+1); int Left= floor(x); float Xfrac= x - Left; int Right= min(Width-1,Left+1); return BiLinInterp(Xfrac, Yfrac, Pixel(Left, Top, comp), Pixel(Right, Top, comp), Pixel(Left, Bot, comp), Pixel(Right, Bot, comp)); } //----------------------------------------------------------------- // resize (with bilinear filter) truecolor bitmap in place void FloatBitMap_t::ReSize(int NewWidth, int NewHeight) { float XRatio= (float)Width / (float)NewWidth; float YRatio= (float)Height / (float)NewHeight; float SourceX, SourceY, Xfrac, Yfrac; int Top, Bot, Left, Right; float *newrgba=new float[NewWidth * NewHeight * 4]; SourceY= 0; for(int y=0;y=Height) Bot= Height-1; SourceX= 0; for(int x=0;x=Width) Right= Width-1; for(int c=0;c<4;c++) { newrgba[4*(y*NewWidth+x)+c] = BiLinInterp(Xfrac, Yfrac, Pixel(Left, Top, c), Pixel(Right, Top, c), Pixel(Left, Bot, c), Pixel(Right, Bot, c)); } SourceX+= XRatio; } SourceY+= YRatio; } delete[] RGBAData; RGBAData=newrgba; Width=NewWidth; Height=NewHeight; } struct TGAHeader_t { unsigned char id_length, colormap_type, image_type; unsigned char colormap_index0,colormap_index1, colormap_length0,colormap_length1; unsigned char colormap_size; unsigned char x_origin0,x_origin1, y_origin0,y_origin1, width0, width1,height0,height1; unsigned char pixel_size, attributes; }; bool FloatBitMap_t::WriteTGAFile(char const *filename) const { FileHandle_t f = g_pFullFileSystem->Open(filename, "wb"); if (f) { TGAHeader_t myheader; memset(&myheader,0,sizeof(myheader)); myheader.image_type=2; myheader.pixel_size=32; myheader.width0= Width & 0xff; myheader.width1= (Width>>8); myheader.height0= Height & 0xff; myheader.height1= (Height>>8); myheader.attributes=0x20; g_pFullFileSystem->Write(&myheader,sizeof(myheader),f); // now, write the pixels for(int y=0;yWrite(&pix8.Blue,1,f); g_pFullFileSystem->Write(&pix8.Green,1,f); g_pFullFileSystem->Write(&pix8.Red,1,f); g_pFullFileSystem->Write(&pix8.Alpha,1,f); } } g_pFullFileSystem->Close( f ); // close file after reading return true; } return false; } FloatBitMap_t::FloatBitMap_t(char const *tgafilename) { RGBAData=0; // load from a tga or pfm if (Q_stristr(tgafilename, ".pfm")) { LoadFromPFM(tgafilename); return; } int width1, height1; ImageFormat imageFormat1; float gamma1; if( !TGALoader::GetInfo( tgafilename, &width1, &height1, &imageFormat1, &gamma1 ) ) { printf( "error loading %s\n", tgafilename); exit( -1 ); } AllocateRGB(width1,height1); uint8 *pImage1Tmp = new uint8 [ImageLoader::GetMemRequired( width1, height1, 1, imageFormat1, false )]; if( !TGALoader::Load( pImage1Tmp, tgafilename, width1, height1, imageFormat1, 2.2f, false ) ) { printf( "error loading %s\n", tgafilename); exit( -1 ); } uint8 *pImage1 = new uint8 [ImageLoader::GetMemRequired( width1, height1, 1, IMAGE_FORMAT_ABGR8888, false )]; ImageLoader::ConvertImageFormat( pImage1Tmp, imageFormat1, pImage1, IMAGE_FORMAT_ABGR8888, width1, height1, 0, 0 ); for(int y=0;yPixel(x,y,c)=((Pixel(x*2,y*2,c)+Pixel(x*2+1,y*2,c)+ Pixel(x*2,y*2+1,c)+Pixel(x*2+1,y*2+1,c))/4); } return newbm; } FloatBitMap_t *FloatBitMap_t::QuarterSizeBlocky(void) const { // generate a new bitmap half on each axis FloatBitMap_t *newbm=new FloatBitMap_t(Width/2,Height/2); for(int y=0;yPixel(x,y,c)=Pixel(x*2,y*2,c); } return newbm; } Vector FloatBitMap_t::AverageColor(void) { Vector ret(0,0,0); for(int y=0;y static inline void SWAP(T & a, T & b) { T temp=a; a=b; b=temp; } void FloatBitMap_t::RaiseToPower(float power) { for(int y=0;yPixel(x,y,c)=dx1; } } for(int x=1;xPixel(x+xofs,y+yofs,c)=diff; if (Flags & SPFLAGS_MAXGRADIENT) { float dx1=Pixel(x+xofs,y+yofs,c)-Pixel(x+dx[i]+xofs,y+dy[i]+yofs,c); if (fabs(dx1)>fabs(diff)) deltas[i]->Pixel(x+xofs,y+yofs,c)=dx1; } } } // now, calculate modifiability for(int x=0;xxofs+1) && (x<=xofs+b.Width-2) && (y>yofs+1) && (y<=yofs+b.Height-2)) modify=1; Alpha(x,y)=modify; } // // now, force a fex pixels in center to be constant // int midx=xofs+b.Width/2; // int midy=yofs+b.Height/2; // for(x=midx-10;xPixel(x,y,c)=dx1; gsum+=fabs(dx1); } } // now, reduce gradient changes // float gavg=gsum/(Width*Height); for(int x=0;xPixel(x,y,c); // if (norml<0.0) // norml=-pow(-norml,1.2); // else // norml=pow(norml,1.2); deltas[i]->Pixel(x,y,c)=norml; } } // now, calculate modifiability for(int x=0;x0) && (xPixel(x+1,y,c); float desiredy=DiffMapY.Pixel(x,y,c)+cursrc->Pixel(x,y+1,c); float desired=0.5*(desiredy+desiredx); curdst->Pixel(x,y,c)=FLerp(cursrc->Pixel(x,y,c),desired,0.5); error+=SQ(desired-cursrc->Pixel(x,y,c)); } SWAP(cursrc,curdst); } // paste result for(int x=0;xPixel(x,y,c); } void FloatBitMap_t::GetAlphaBounds(int &minx, int &miny, int &maxx,int &maxy) { for(minx=0;minx=0;maxx--) { int y; for(y=0;y=0;maxy--) { int x; for(x=minx;x<=maxx;x++) if (Alpha(x,maxy)) break; if (x25) && (maxy-miny)>25) { // perform at low resolution FloatBitMap_t *lowdeltas[NDELTAS]; for(int i=0;iQuarterSize(); FloatBitMap_t *tmp=QuarterSize(); tmp->Poisson(lowdeltas,n_iters*4,flags); // now, propagate results from tmp to us for(int x=0;xWidth;x++) for(int y=0;yHeight;y++) for(int xi=0;xi<2;xi++) for(int yi=0;yi<2;yi++) if (Alpha(x*2+xi,y*2+yi)) { for(int c=0;c<3;c++) Pixel(x*2+xi,y*2+yi,c)= FLerp(Pixel(x*2+xi,y*2+yi,c),tmp->Pixel(x,y,c),Alpha(x*2+xi,y*2+yi)); } char fname[80]; sprintf(fname,"sub%dx%d.tga",tmp->Width,tmp->Height); tmp->WriteTGAFile(fname); sprintf(fname,"submrg%dx%d.tga",tmp->Width,tmp->Height); WriteTGAFile(fname); delete tmp; for(int i=0;iPixel(x,y,c)+cursrc->Pixel(x+dx[i],y+dy[i],c); desired*=(1.0/NDELTAS); // desired=FLerp(Pixel(x,y,c),desired,Alpha(x,y)); curdst->Pixel(x,y,c)=FLerp(cursrc->Pixel(x,y,c),desired,0.5); error+=SQ(desired-cursrc->Pixel(x,y,c)); } } SWAP(cursrc,curdst); } } } // paste result for(int x=0;xPixel(x,y,c); } } } }