diff options
| author | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
|---|---|---|
| committer | FluorescentCIAAfricanAmerican <[email protected]> | 2020-04-22 12:56:21 -0400 |
| commit | 3bf9df6b2785fa6d951086978a3e66f49427166a (patch) | |
| tree | 2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /public/bitmap | |
| download | archived-source-engine-2018-hl2-src-master.tar.xz archived-source-engine-2018-hl2-src-master.zip | |
Diffstat (limited to 'public/bitmap')
| -rw-r--r-- | public/bitmap/bitmap.h | 142 | ||||
| -rw-r--r-- | public/bitmap/cubemap.h | 68 | ||||
| -rw-r--r-- | public/bitmap/float_bm.h | 363 | ||||
| -rw-r--r-- | public/bitmap/imageformat.h | 574 | ||||
| -rw-r--r-- | public/bitmap/psd.h | 105 | ||||
| -rw-r--r-- | public/bitmap/tgaloader.h | 45 | ||||
| -rw-r--r-- | public/bitmap/tgawriter.h | 40 |
7 files changed, 1337 insertions, 0 deletions
diff --git a/public/bitmap/bitmap.h b/public/bitmap/bitmap.h new file mode 100644 index 0000000..bc56ed2 --- /dev/null +++ b/public/bitmap/bitmap.h @@ -0,0 +1,142 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +// $Header: $ +// $NoKeywords: $ +//===========================================================================// + +#ifndef BITMAP_H +#define BITMAP_H + +#ifdef _WIN32 +#pragma once +#endif + + +#include "bitmap/imageformat.h" +#include "Color.h" +#include "dbg.h" + +class CUtlBuffer; + +//----------------------------------------------------------------------------- +// A Bitmap +//----------------------------------------------------------------------------- +struct Bitmap_t +{ + Bitmap_t() { Reset(); } + ~Bitmap_t() { Clear(); } + + // + // Accessors + // + inline int Width() const { return m_nWidth; } + inline int Height() const { return m_nHeight; } + inline ImageFormat Format() const { return m_ImageFormat; } + inline unsigned char *GetBits() const { return m_pBits; } + inline int Stride() const { return m_nStride; } + inline bool GetOwnsBuffer() const { return m_bOwnsBuffer; } + + /// Allocate the buffer. Discards existing data, freeing it if we own it + void Init( int nWidth, int nHeight, ImageFormat imageFormat, int nStride = 0 ); + + /// Set the bitmap to the specified buffer. Any existing data is discarded/freed + /// as appropriate. + void SetBuffer( int nWidth, int nHeight, ImageFormat imageFormat, unsigned char *pBits, bool bAssumeOwnership, int nStride = 0 ); + + /// Sets / releases ownershp of the buffer. This does not otherwise alter the + /// state of the bitmap. + void SetOwnsBuffer( bool bOwnsBuffer ) + { + Assert( m_pBits ); + m_bOwnsBuffer = bOwnsBuffer; + } + + /// Free up all memory and reset to default state + void Clear(); + + /// Return true if we have a valid size and buffer + bool IsValid() const; + + /// Get pointer to raw pixel data. + unsigned char *GetPixel( int x, int y ); + const unsigned char *GetPixel( int x, int y ) const; + + /// Get pixel value at specified coordinates + Color GetColor( int x, int y ) const; + + /// Set pixel value at specified coordinates + void SetColor( int x, int y, Color c ); + + /// Set this bitmap to be a logical copy of the specified + /// bitmap. No memory is allocated or copied, just copying + /// some pointers. We can also optionally transfer ownership + /// of the buffer. + void MakeLogicalCopyOf( Bitmap_t &src, bool bTransferBufferOwnership = false ); + + /// Set this bitmap to be a cropped rectangle from the given bitmap. + /// The source pointer can be NULL or point to this, which means to do + /// the crop in place. + void Crop( int x0, int y0, int nWidth, int nHeight, const Bitmap_t *pImgSource = NULL ); + + /// Blit a rectangle of pixel data into this image. + void SetPixelData( const Bitmap_t &src, int nSrcX1, int nSrcY1, int nCopySizeX, int nCopySizeY, int nDestX1, int nDestY1 ); + + /// Blit the entire source image into this image, at the specified offset. + /// the rectangle is clipped if necessary + void SetPixelData( const Bitmap_t &src, int nDestX1 = 0, int nDestY1 = 0 ); + +private: + void Reset(); + + /// Dimensions + int m_nWidth; + int m_nHeight; + + /// Size, in bytes, of one pixel + int m_nPixelSize; + + /// Image row stride, in bytes + int m_nStride; + + // Do we own this buffer? + bool m_bOwnsBuffer; + + /// Pixel format + ImageFormat m_ImageFormat; + + /// Bitmap data. Must be allocated with malloc/free. Don't use + /// new/delete + unsigned char *m_pBits; +}; + +inline void Bitmap_t::Reset() +{ + m_nWidth = 0; + m_nHeight = 0; + m_ImageFormat = IMAGE_FORMAT_UNKNOWN; + m_pBits = NULL; + m_nPixelSize = 0; + m_bOwnsBuffer = false; + m_nStride = 0; +} + +inline unsigned char *Bitmap_t::GetPixel( int x, int y ) +{ + if ( !m_pBits ) + return NULL; + + return m_pBits + (y*m_nStride) + x* m_nPixelSize; +} + +inline const unsigned char *Bitmap_t::GetPixel( int x, int y ) const +{ + if ( !m_pBits ) + return NULL; + + return m_pBits + (y*m_nStride) + x* m_nPixelSize; +} + + +#endif // BITMAP_H diff --git a/public/bitmap/cubemap.h b/public/bitmap/cubemap.h new file mode 100644 index 0000000..1bda06f --- /dev/null +++ b/public/bitmap/cubemap.h @@ -0,0 +1,68 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: a class for performing cube-mapped spherical sample lookups. +// +// $Workfile: $ +// $Date: $ +// $NoKeywords: $ +//===========================================================================// + +#ifndef CUBEMAP_H +#define CUBEMAP_H + +#ifdef _WIN32 +#pragma once +#endif + +#include "tier0/platform.h" +#include "tier1/utlmemory.h" +#include "mathlib/mathlib.h" + +template<class T, int RES> struct CCubeMap +{ + T m_Samples[6][RES][RES]; + +public: + FORCEINLINE void GetCoords( Vector const &vecNormalizedDirection, int &nX, int &nY, int &nFace ) + { + // find largest magnitude component + int nLargest = 0; + int nAxis0 = 1; + int nAxis1 = 2; + if ( fabs( vecNormalizedDirection[1] ) > fabs( vecNormalizedDirection[0] ) ) + { + nLargest = 1; + nAxis0 = 0; + nAxis1 = 2; + } + if ( fabs( vecNormalizedDirection[2] ) > fabs( vecNormalizedDirection[nLargest] ) ) + { + nLargest = 2; + nAxis0 = 0; + nAxis1 = 1; + } + float flZ = vecNormalizedDirection[nLargest]; + if ( flZ < 0 ) + { + flZ = - flZ; + nLargest += 3; + } + nFace = nLargest; + flZ = 1.0 / flZ; + nX = RemapValClamped( vecNormalizedDirection[nAxis0] * flZ, -1, 1, 0, RES - 1 ); + nY = RemapValClamped( vecNormalizedDirection[nAxis1] * flZ, -1, 1, 0, RES - 1 ); + } + + FORCEINLINE T & GetSample( Vector const &vecNormalizedDirection ) + { + int nX, nY, nFace; + GetCoords( vecNormalizedDirection, nX, nY, nFace ); + return m_Samples[nFace][nX][nY]; + } +}; + + + + + +#endif // CUBEMAP_H diff --git a/public/bitmap/float_bm.h b/public/bitmap/float_bm.h new file mode 100644 index 0000000..a80722d --- /dev/null +++ b/public/bitmap/float_bm.h @@ -0,0 +1,363 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +// $Header: $ +// $NoKeywords: $ +//=============================================================================// + +#ifndef FLOAT_BM_H +#define FLOAT_BM_H + +#ifdef _WIN32 +#pragma once +#endif + +#include <tier0/platform.h> +#include "tier0/dbg.h" +#include <mathlib/mathlib.h> + +struct PixRGBAF +{ + float Red; + float Green; + float Blue; + float Alpha; +}; + +struct PixRGBA8 +{ + unsigned char Red; + unsigned char Green; + unsigned char Blue; + unsigned char Alpha; +}; + +inline PixRGBAF PixRGBA8_to_F( PixRGBA8 const &x ) +{ + PixRGBAF f; + f.Red = x.Red / 255.f; + f.Green = x.Green / 255.f; + f.Blue = x.Blue / 255.f; + f.Alpha = x.Alpha / 255.f; + return f; +} + +inline PixRGBA8 PixRGBAF_to_8( PixRGBAF const &f ) +{ + PixRGBA8 x; + x.Red = max( 0.f, min( 255.f,255.f*f.Red ) ); + x.Green = max( 0.f, min( 255.f,255.f*f.Green ) ); + x.Blue = max( 0.f, min( 255.f,255.f*f.Blue ) ); + x.Alpha = max( 0.f, min( 255.f,255.f*f.Alpha ) ); + return x; +} + +#define SPFLAGS_MAXGRADIENT 1 + +// bit flag options for ComputeSelfShadowedBumpmapFromHeightInAlphaChannel: +#define SSBUMP_OPTION_NONDIRECTIONAL 1 // generate ambient occlusion only +#define SSBUMP_MOD2X_DETAIL_TEXTURE 2 // scale so that a flat unshadowed + // value is 0.5, and bake rgb luminance + // in. + + + +class FloatBitMap_t +{ +public: + int Width, Height; // bitmap dimensions + float *RGBAData; // actual data + + FloatBitMap_t(void) // empty one + { + Width=Height=0; + RGBAData=0; + } + + + + FloatBitMap_t(int width, int height); // make one and allocate space + FloatBitMap_t(char const *filename); // read one from a file (tga or pfm) + FloatBitMap_t(FloatBitMap_t const *orig); + // quantize one to 8 bits + bool WriteTGAFile(char const *filename) const; + + bool LoadFromPFM(char const *filename); // load from floating point pixmap (.pfm) file + bool WritePFM(char const *filename); // save to floating point pixmap (.pfm) file + + + void InitializeWithRandomPixelsFromAnotherFloatBM(FloatBitMap_t const &other); + + inline float & Pixel(int x, int y, int comp) const + { + Assert((x>=0) && (x<Width)); + Assert((y>=0) && (y<Height)); + return RGBAData[4*(x+Width*y)+comp]; + } + + inline float & PixelWrapped(int x, int y, int comp) const + { + // like Pixel except wraps around to other side + if (x < 0) + x+=Width; + else + if (x>= Width) + x -= Width; + + if ( y < 0 ) + y+=Height; + else + if ( y >= Height ) + y -= Height; + + return RGBAData[4*(x+Width*y)+comp]; + } + + inline float & PixelClamped(int x, int y, int comp) const + { + // like Pixel except wraps around to other side + x=clamp(x,0,Width-1); + y=clamp(y,0,Height-1); + return RGBAData[4*(x+Width*y)+comp]; + } + + + inline float & Alpha(int x, int y) const + { + Assert((x>=0) && (x<Width)); + Assert((y>=0) && (y<Height)); + return RGBAData[3+4*(x+Width*y)]; + } + + + // look up a pixel value with bilinear interpolation + float InterpolatedPixel(float x, float y, int comp) const; + + inline PixRGBAF PixelRGBAF(int x, int y) const + { + Assert((x>=0) && (x<Width)); + Assert((y>=0) && (y<Height)); + + PixRGBAF RetPix; + int RGBoffset= 4*(x+Width*y); + RetPix.Red= RGBAData[RGBoffset+0]; + RetPix.Green= RGBAData[RGBoffset+1]; + RetPix.Blue= RGBAData[RGBoffset+2]; + RetPix.Alpha= RGBAData[RGBoffset+3]; + + return RetPix; + } + + + inline void WritePixelRGBAF(int x, int y, PixRGBAF value) const + { + Assert((x>=0) && (x<Width)); + Assert((y>=0) && (y<Height)); + + int RGBoffset= 4*(x+Width*y); + RGBAData[RGBoffset+0]= value.Red; + RGBAData[RGBoffset+1]= value.Green; + RGBAData[RGBoffset+2]= value.Blue; + RGBAData[RGBoffset+3]= value.Alpha; + + } + + + inline void WritePixel(int x, int y, int comp, float value) + { + Assert((x>=0) && (x<Width)); + Assert((y>=0) && (y<Height)); + RGBAData[4*(x+Width*y)+comp]= value; + } + + // paste, performing boundary matching. Alpha channel can be used to make + // brush shape irregular + void SmartPaste(FloatBitMap_t const &brush, int xofs, int yofs, uint32 flags); + + // force to be tileable using poisson formula + void MakeTileable(void); + + void ReSize(int NewXSize, int NewYSize); + + // find the bounds of the area that has non-zero alpha. + void GetAlphaBounds(int &minx, int &miny, int &maxx,int &maxy); + + // Solve the poisson equation for an image. The alpha channel of the image controls which + // pixels are "modifiable", and can be used to set boundary conditions. Alpha=0 means the pixel + // is locked. deltas are in the order [(x,y)-(x,y-1),(x,y)-(x-1,y),(x,y)-(x+1,y),(x,y)-(x,y+1) + void Poisson(FloatBitMap_t *deltas[4], + int n_iters, + uint32 flags // SPF_xxx + ); + + FloatBitMap_t *QuarterSize(void) const; // get a new one downsampled + FloatBitMap_t *QuarterSizeBlocky(void) const; // get a new one downsampled + + FloatBitMap_t *QuarterSizeWithGaussian(void) const; // downsample 2x using a gaussian + + + void RaiseToPower(float pow); + void ScaleGradients(void); + void Logize(void); // pix=log(1+pix) + void UnLogize(void); // pix=exp(pix)-1 + + // compress to 8 bits converts the hdr texture to an 8 bit texture, encoding a scale factor + // in the alpha channel. upon return, the original pixel can be (approximately) recovered + // by the formula rgb*alpha*overbright. + // this function performs special numerical optimization on the texture to minimize the error + // when using bilinear filtering to read the texture. + void CompressTo8Bits(float overbright); + // decompress a bitmap converted by CompressTo8Bits + void Uncompress(float overbright); + + + Vector AverageColor(void); // average rgb value of all pixels + float BrightestColor(void); // highest vector magnitude + + void Clear(float r, float g, float b, float alpha); // set all pixels to speicifed values (0..1 nominal) + + void ScaleRGB(float scale_factor); // for all pixels, r,g,b*=scale_factor + + // given a bitmap with height stored in the alpha channel, generate vector positions and normals + void ComputeVertexPositionsAndNormals( float flHeightScale, Vector **ppPosOut, Vector **ppNormalOut ) const; + + // generate a normal map with height stored in alpha. uses hl2 tangent basis to support baked + // self shadowing. the bump scale maps the height of a pixel relative to the edges of the + // pixel. This function may take a while - many millions of rays may be traced. applications + // using this method need to link w/ raytrace.lib + FloatBitMap_t *ComputeSelfShadowedBumpmapFromHeightInAlphaChannel( + float bump_scale, int nrays_to_trace_per_pixel=100, + uint32 nOptionFlags = 0 // SSBUMP_OPTION_XXX + ) const; + + + // generate a conventional normal map from a source with height stored in alpha. + FloatBitMap_t *ComputeBumpmapFromHeightInAlphaChannel( float bump_scale ) const ; + + + // bilateral (edge preserving) smoothing filter. edge_threshold_value defines the difference in + // values over which filtering will not occur. Each channel is filtered independently. large + // radii will run slow, since the bilateral filter is neither separable, nor is it a + // convolution that can be done via fft. + void TileableBilateralFilter( int radius_in_pixels, float edge_threshold_value ); + + ~FloatBitMap_t(); + + void AllocateRGB(int w, int h) + { + if (RGBAData) delete[] RGBAData; + RGBAData=new float[w*h*4]; + Width=w; + Height=h; + } +}; + + +// a FloatCubeMap_t holds the floating point bitmaps for 6 faces of a cube map +class FloatCubeMap_t +{ +public: + FloatBitMap_t face_maps[6]; + + FloatCubeMap_t(int xfsize, int yfsize) + { + // make an empty one with face dimensions xfsize x yfsize + for(int f=0;f<6;f++) + face_maps[f].AllocateRGB(xfsize,yfsize); + } + + // load basenamebk,pfm, basenamedn.pfm, basenameft.pfm, ... + FloatCubeMap_t(char const *basename); + + // save basenamebk,pfm, basenamedn.pfm, basenameft.pfm, ... + void WritePFMs(char const *basename); + + Vector AverageColor(void) + { + Vector ret(0,0,0); + int nfaces=0; + for(int f=0;f<6;f++) + if (face_maps[f].RGBAData) + { + nfaces++; + ret+=face_maps[f].AverageColor(); + } + if (nfaces) + ret*=(1.0/nfaces); + return ret; + } + + float BrightestColor(void) + { + float ret=0.0; + int nfaces=0; + for(int f=0;f<6;f++) + if (face_maps[f].RGBAData) + { + nfaces++; + ret=max(ret,face_maps[f].BrightestColor()); + } + return ret; + } + + + // resample a cubemap to one of possibly a lower resolution, using a given phong exponent. + // dot-product weighting will be used for the filtering operation. + void Resample(FloatCubeMap_t &dest, float flPhongExponent); + + // returns the normalized direciton vector through a given pixel of a given face + Vector PixelDirection(int face, int x, int y); + + // returns the direction vector throught the center of a cubemap face + Vector FaceNormal( int nFaceNumber ); +}; + + +static inline float FLerp(float f1, float f2, float t) +{ + return f1+(f2-f1)*t; +} + + +// Image Pyramid class. +#define MAX_IMAGE_PYRAMID_LEVELS 16 // up to 64kx64k + +enum ImagePyramidMode_t +{ + PYRAMID_MODE_GAUSSIAN, +}; + +class FloatImagePyramid_t +{ +public: + int m_nLevels; + FloatBitMap_t *m_pLevels[MAX_IMAGE_PYRAMID_LEVELS]; // level 0 is highest res + + FloatImagePyramid_t(void) + { + m_nLevels=0; + memset(m_pLevels,0,sizeof(m_pLevels)); + } + + // build one. clones data from src for level 0. + FloatImagePyramid_t(FloatBitMap_t const &src, ImagePyramidMode_t mode); + + // read or write a Pixel from a given level. All coordinates are specified in the same domain as the base level. + float &Pixel(int x, int y, int component, int level) const; + + FloatBitMap_t *Level(int lvl) const + { + Assert(lvl<m_nLevels); + Assert(lvl<ARRAYSIZE(m_pLevels)); + return m_pLevels[lvl]; + } + // rebuild all levels above the specified level + void ReconstructLowerResolutionLevels(int starting_level); + + ~FloatImagePyramid_t(void); + + void WriteTGAs(char const *basename) const; // outputs name_00.tga, name_01.tga,... +}; + +#endif diff --git a/public/bitmap/imageformat.h b/public/bitmap/imageformat.h new file mode 100644 index 0000000..196397e --- /dev/null +++ b/public/bitmap/imageformat.h @@ -0,0 +1,574 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +//===========================================================================// + +#ifndef IMAGEFORMAT_H +#define IMAGEFORMAT_H + +#ifdef _WIN32 +#pragma once +#endif + +#include <stdio.h> + +enum NormalDecodeMode_t +{ + NORMAL_DECODE_NONE = 0 +}; + +// Forward declaration +#ifdef _WIN32 +typedef enum _D3DFORMAT D3DFORMAT; +#endif + +//----------------------------------------------------------------------------- +// The various image format types +//----------------------------------------------------------------------------- + +// don't bitch that inline functions aren't used!!!! +#pragma warning(disable : 4514) + +enum ImageFormat +{ + IMAGE_FORMAT_UNKNOWN = -1, + IMAGE_FORMAT_RGBA8888 = 0, + IMAGE_FORMAT_ABGR8888, + IMAGE_FORMAT_RGB888, + IMAGE_FORMAT_BGR888, + IMAGE_FORMAT_RGB565, + IMAGE_FORMAT_I8, + IMAGE_FORMAT_IA88, + IMAGE_FORMAT_P8, + IMAGE_FORMAT_A8, + IMAGE_FORMAT_RGB888_BLUESCREEN, + IMAGE_FORMAT_BGR888_BLUESCREEN, + IMAGE_FORMAT_ARGB8888, + IMAGE_FORMAT_BGRA8888, + IMAGE_FORMAT_DXT1, + IMAGE_FORMAT_DXT3, + IMAGE_FORMAT_DXT5, + IMAGE_FORMAT_BGRX8888, + IMAGE_FORMAT_BGR565, + IMAGE_FORMAT_BGRX5551, + IMAGE_FORMAT_BGRA4444, + IMAGE_FORMAT_DXT1_ONEBITALPHA, + IMAGE_FORMAT_BGRA5551, + IMAGE_FORMAT_UV88, + IMAGE_FORMAT_UVWQ8888, + IMAGE_FORMAT_RGBA16161616F, + IMAGE_FORMAT_RGBA16161616, + IMAGE_FORMAT_UVLX8888, + IMAGE_FORMAT_R32F, // Single-channel 32-bit floating point + IMAGE_FORMAT_RGB323232F, + IMAGE_FORMAT_RGBA32323232F, + + // Depth-stencil texture formats for shadow depth mapping + IMAGE_FORMAT_NV_DST16, // + IMAGE_FORMAT_NV_DST24, // + IMAGE_FORMAT_NV_INTZ, // Vendor-specific depth-stencil texture + IMAGE_FORMAT_NV_RAWZ, // formats for shadow depth mapping + IMAGE_FORMAT_ATI_DST16, // + IMAGE_FORMAT_ATI_DST24, // + IMAGE_FORMAT_NV_NULL, // Dummy format which takes no video memory + + // Compressed normal map formats + IMAGE_FORMAT_ATI2N, // One-surface ATI2N / DXN format + IMAGE_FORMAT_ATI1N, // Two-surface ATI1N format + +#if defined( _X360 ) + // Depth-stencil texture formats + IMAGE_FORMAT_X360_DST16, + IMAGE_FORMAT_X360_DST24, + IMAGE_FORMAT_X360_DST24F, + // supporting these specific formats as non-tiled for procedural cpu access + IMAGE_FORMAT_LINEAR_BGRX8888, + IMAGE_FORMAT_LINEAR_RGBA8888, + IMAGE_FORMAT_LINEAR_ABGR8888, + IMAGE_FORMAT_LINEAR_ARGB8888, + IMAGE_FORMAT_LINEAR_BGRA8888, + IMAGE_FORMAT_LINEAR_RGB888, + IMAGE_FORMAT_LINEAR_BGR888, + IMAGE_FORMAT_LINEAR_BGRX5551, + IMAGE_FORMAT_LINEAR_I8, + IMAGE_FORMAT_LINEAR_RGBA16161616, + + IMAGE_FORMAT_LE_BGRX8888, + IMAGE_FORMAT_LE_BGRA8888, +#endif + + IMAGE_FORMAT_DXT1_RUNTIME, + IMAGE_FORMAT_DXT5_RUNTIME, + + NUM_IMAGE_FORMATS +}; + +#if defined( POSIX ) || defined( DX_TO_GL_ABSTRACTION ) +typedef enum _D3DFORMAT + { + D3DFMT_INDEX16, + D3DFMT_D16, + D3DFMT_D24S8, + D3DFMT_A8R8G8B8, + D3DFMT_A4R4G4B4, + D3DFMT_X8R8G8B8, + D3DFMT_R5G6R5, + D3DFMT_X1R5G5B5, + D3DFMT_A1R5G5B5, + D3DFMT_L8, + D3DFMT_A8L8, + D3DFMT_A, + D3DFMT_DXT1, + D3DFMT_DXT3, + D3DFMT_DXT5, + D3DFMT_V8U8, + D3DFMT_Q8W8V8U8, + D3DFMT_X8L8V8U8, + D3DFMT_A16B16G16R16F, + D3DFMT_A16B16G16R16, + D3DFMT_R32F, + D3DFMT_A32B32G32R32F, + D3DFMT_R8G8B8, + D3DFMT_D24X4S4, + D3DFMT_A8, + D3DFMT_R5G6B5, + D3DFMT_D15S1, + D3DFMT_D24X8, + D3DFMT_VERTEXDATA, + D3DFMT_INDEX32, + + // adding fake D3D format names for the vendor specific ones (eases debugging/logging) + + // NV shadow depth tex + D3DFMT_NV_INTZ = 0x5a544e49, // MAKEFOURCC('I','N','T','Z') + D3DFMT_NV_RAWZ = 0x5a574152, // MAKEFOURCC('R','A','W','Z') + + // NV null tex + D3DFMT_NV_NULL = 0x4c4c554e, // MAKEFOURCC('N','U','L','L') + + // ATI shadow depth tex + D3DFMT_ATI_D16 = 0x36314644, // MAKEFOURCC('D','F','1','6') + D3DFMT_ATI_D24S8 = 0x34324644, // MAKEFOURCC('D','F','2','4') + + // ATI 1N and 2N compressed tex + D3DFMT_ATI_2N = 0x32495441, // MAKEFOURCC('A', 'T', 'I', '2') + D3DFMT_ATI_1N = 0x31495441, // MAKEFOURCC('A', 'T', 'I', '1') + + D3DFMT_UNKNOWN + } D3DFORMAT; +#endif + +//----------------------------------------------------------------------------- +// Color structures +//----------------------------------------------------------------------------- +struct BGRA8888_t; +struct BGRX8888_t; +struct RGBA8888_t; +struct RGB888_t; +struct BGR888_t; +struct BGR565_t; +struct BGRA5551_t; +struct BGRA4444_t; +struct RGBX5551_t; + +struct BGRA8888_t +{ + unsigned char b; // change the order of names to change the + unsigned char g; // order of the output ARGB or BGRA, etc... + unsigned char r; // Last one is MSB, 1st is LSB. + unsigned char a; + inline BGRA8888_t& operator=( const BGRA8888_t& in ) + { + *( unsigned int * )this = *( unsigned int * )∈ + return *this; + } +}; + +struct BGRX8888_t +{ + unsigned char b; // change the order of names to change the + unsigned char g; // order of the output ARGB or BGRA, etc... + unsigned char r; // Last one is MSB, 1st is LSB. + unsigned char x; + inline BGRX8888_t& operator=( const BGRX8888_t& in ) + { + *( unsigned int * )this = *( unsigned int * ) ∈ + return *this; + } +}; + +struct RGBA8888_t +{ + unsigned char r; // change the order of names to change the + unsigned char g; // order of the output ARGB or BGRA, etc... + unsigned char b; // Last one is MSB, 1st is LSB. + unsigned char a; + inline RGBA8888_t& operator=( const BGRA8888_t& in ); + inline RGBA8888_t& operator=( const RGB888_t& in ); + inline RGBA8888_t& operator=( const BGRX8888_t& in ); +}; + +struct RGB888_t +{ + unsigned char r; + unsigned char g; + unsigned char b; + inline RGB888_t& operator=( const BGRA8888_t& in ) + { + r = in.r; + g = in.g; + b = in.b; + return *this; + } + inline bool operator==( const RGB888_t& in ) const + { + return ( r == in.r ) && ( g == in.g ) && ( b == in.b ); + } + inline bool operator!=( const RGB888_t& in ) const + { + return ( r != in.r ) || ( g != in.g ) || ( b != in.b ); + } +}; + +struct BGR888_t +{ + unsigned char b; + unsigned char g; + unsigned char r; + inline BGR888_t& operator=( const BGRA8888_t& in ) + { + r = in.r; + g = in.g; + b = in.b; + return *this; + } +}; + +// 360 uses this structure for x86 dxt decoding +#if defined( _X360 ) +#pragma bitfield_order( push, lsb_to_msb ) +#endif +struct BGR565_t +{ + unsigned short b : 5; // order of names changes + unsigned short g : 6; // byte order of output to 32 bit + unsigned short r : 5; + inline BGR565_t& operator=( const BGRA8888_t& in ) + { + r = in.r >> 3; + g = in.g >> 2; + b = in.b >> 3; + return *this; + } + inline BGR565_t &Set( int red, int green, int blue ) + { + r = red >> 3; + g = green >> 2; + b = blue >> 3; + return *this; + } +}; +#if defined( _X360 ) +#pragma bitfield_order( pop ) +#endif + +struct BGRA5551_t +{ + unsigned short b : 5; // order of names changes + unsigned short g : 5; // byte order of output to 32 bit + unsigned short r : 5; + unsigned short a : 1; + inline BGRA5551_t& operator=( const BGRA8888_t& in ) + { + r = in.r >> 3; + g = in.g >> 3; + b = in.b >> 3; + a = in.a >> 7; + return *this; + } +}; + +struct BGRA4444_t +{ + unsigned short b : 4; // order of names changes + unsigned short g : 4; // byte order of output to 32 bit + unsigned short r : 4; + unsigned short a : 4; + inline BGRA4444_t& operator=( const BGRA8888_t& in ) + { + r = in.r >> 4; + g = in.g >> 4; + b = in.b >> 4; + a = in.a >> 4; + return *this; + } +}; + +struct RGBX5551_t +{ + unsigned short r : 5; + unsigned short g : 5; + unsigned short b : 5; + unsigned short x : 1; + inline RGBX5551_t& operator=( const BGRA8888_t& in ) + { + r = in.r >> 3; + g = in.g >> 3; + b = in.b >> 3; + return *this; + } +}; + + +//----------------------------------------------------------------------------- +// Conversion assignments +//----------------------------------------------------------------------------- +RGBA8888_t& RGBA8888_t::operator=( const BGRA8888_t& in ) +{ + r = in.r; + g = in.g; + b = in.b; + a = in.a; + return *this; +} + +RGBA8888_t& RGBA8888_t::operator=( const RGB888_t& in ) +{ + r = in.r; + g = in.g; + b = in.b; + a = 0xFF; + return *this; +} + +RGBA8888_t& RGBA8888_t::operator=( const BGRX8888_t& in ) +{ + r = in.r; + g = in.g; + b = in.b; + a = 0xFF; + return *this; +} + +//----------------------------------------------------------------------------- +// some important constants +//----------------------------------------------------------------------------- +#define ARTWORK_GAMMA ( 2.2f ) +#define IMAGE_MAX_DIM ( 2048 ) + + +//----------------------------------------------------------------------------- +// information about each image format +//----------------------------------------------------------------------------- +struct ImageFormatInfo_t +{ + const char* m_pName; + int m_NumBytes; + int m_NumRedBits; + int m_NumGreeBits; + int m_NumBlueBits; + int m_NumAlphaBits; + bool m_IsCompressed; +}; + + +//----------------------------------------------------------------------------- +// Various methods related to pixelmaps and color formats +//----------------------------------------------------------------------------- +namespace ImageLoader +{ + + bool GetInfo( const char *fileName, int *width, int *height, enum ImageFormat *imageFormat, float *sourceGamma ); + int GetMemRequired( int width, int height, int depth, ImageFormat imageFormat, bool mipmap ); + int GetMipMapLevelByteOffset( int width, int height, enum ImageFormat imageFormat, int skipMipLevels ); + void GetMipMapLevelDimensions( int *width, int *height, int skipMipLevels ); + int GetNumMipMapLevels( int width, int height, int depth = 1 ); + bool Load( unsigned char *imageData, const char *fileName, int width, int height, enum ImageFormat imageFormat, float targetGamma, bool mipmap ); + bool Load( unsigned char *imageData, FILE *fp, int width, int height, + enum ImageFormat imageFormat, float targetGamma, bool mipmap ); + + // convert from any image format to any other image format. + // return false if the conversion cannot be performed. + // Strides denote the number of bytes per each line, + // by default assumes width * # of bytes per pixel + bool ConvertImageFormat( const unsigned char *src, enum ImageFormat srcImageFormat, + unsigned char *dst, enum ImageFormat dstImageFormat, + int width, int height, int srcStride = 0, int dstStride = 0 ); + + // must be used in conjunction with ConvertImageFormat() to pre-swap and post-swap + void PreConvertSwapImageData( unsigned char *pImageData, int nImageSize, ImageFormat imageFormat, int width = 0, int stride = 0 ); + void PostConvertSwapImageData( unsigned char *pImageData, int nImageSize, ImageFormat imageFormat, int width = 0, int stride = 0 ); + void ByteSwapImageData( unsigned char *pImageData, int nImageSize, ImageFormat imageFormat, int width = 0, int stride = 0 ); + bool IsFormatValidForConversion( ImageFormat fmt ); + + //----------------------------------------------------------------------------- + // convert back and forth from D3D format to ImageFormat, regardless of + // whether it's supported or not + //----------------------------------------------------------------------------- + ImageFormat D3DFormatToImageFormat( D3DFORMAT format ); + D3DFORMAT ImageFormatToD3DFormat( ImageFormat format ); + + // Flags for ResampleRGBA8888 + enum + { + RESAMPLE_NORMALMAP = 0x1, + RESAMPLE_ALPHATEST = 0x2, + RESAMPLE_NICE_FILTER = 0x4, + RESAMPLE_CLAMPS = 0x8, + RESAMPLE_CLAMPT = 0x10, + RESAMPLE_CLAMPU = 0x20, + }; + + struct ResampleInfo_t + { + + ResampleInfo_t() : m_nFlags(0), m_flAlphaThreshhold(0.4f), m_flAlphaHiFreqThreshhold(0.4f), m_nSrcDepth(1), m_nDestDepth(1) + { + m_flColorScale[0] = 1.0f, m_flColorScale[1] = 1.0f, m_flColorScale[2] = 1.0f, m_flColorScale[3] = 1.0f; + m_flColorGoal[0] = 0.0f, m_flColorGoal[1] = 0.0f, m_flColorGoal[2] = 0.0f, m_flColorGoal[3] = 0.0f; + } + + unsigned char *m_pSrc; + unsigned char *m_pDest; + + int m_nSrcWidth; + int m_nSrcHeight; + int m_nSrcDepth; + + int m_nDestWidth; + int m_nDestHeight; + int m_nDestDepth; + + float m_flSrcGamma; + float m_flDestGamma; + + float m_flColorScale[4]; // Color scale factors RGBA + float m_flColorGoal[4]; // Color goal values RGBA DestColor = ColorGoal + scale * (SrcColor - ColorGoal) + + float m_flAlphaThreshhold; + float m_flAlphaHiFreqThreshhold; + + int m_nFlags; + }; + + bool ResampleRGBA8888( const ResampleInfo_t &info ); + bool ResampleRGBA16161616( const ResampleInfo_t &info ); + bool ResampleRGB323232F( const ResampleInfo_t &info ); + + void ConvertNormalMapRGBA8888ToDUDVMapUVLX8888( const unsigned char *src, int width, int height, + unsigned char *dst_ ); + void ConvertNormalMapRGBA8888ToDUDVMapUVWQ8888( const unsigned char *src, int width, int height, + unsigned char *dst_ ); + void ConvertNormalMapRGBA8888ToDUDVMapUV88( const unsigned char *src, int width, int height, + unsigned char *dst_ ); + + void ConvertIA88ImageToNormalMapRGBA8888( const unsigned char *src, int width, + int height, unsigned char *dst, + float bumpScale ); + + void NormalizeNormalMapRGBA8888( unsigned char *src, int numTexels ); + + + //----------------------------------------------------------------------------- + // Gamma correction + //----------------------------------------------------------------------------- + void GammaCorrectRGBA8888( unsigned char *src, unsigned char* dst, + int width, int height, int depth, float srcGamma, float dstGamma ); + + + //----------------------------------------------------------------------------- + // Makes a gamma table + //----------------------------------------------------------------------------- + void ConstructGammaTable( unsigned char* pTable, float srcGamma, float dstGamma ); + + + //----------------------------------------------------------------------------- + // Gamma corrects using a previously constructed gamma table + //----------------------------------------------------------------------------- + void GammaCorrectRGBA8888( unsigned char* pSrc, unsigned char* pDst, + int width, int height, int depth, unsigned char* pGammaTable ); + + + //----------------------------------------------------------------------------- + // Generates a number of mipmap levels + //----------------------------------------------------------------------------- + void GenerateMipmapLevels( unsigned char* pSrc, unsigned char* pDst, int width, + int height, int depth, ImageFormat imageFormat, float srcGamma, float dstGamma, + int numLevels = 0 ); + + // Low quality mipmap generation, but way faster. + void GenerateMipmapLevelsLQ( unsigned char* pSrc, unsigned char* pDst, int width, int height, + ImageFormat imageFormat, int numLevels ); + + //----------------------------------------------------------------------------- + // operations on square images (src and dst can be the same) + //----------------------------------------------------------------------------- + bool RotateImageLeft( const unsigned char *src, unsigned char *dst, + int widthHeight, ImageFormat imageFormat ); + bool RotateImage180( const unsigned char *src, unsigned char *dst, + int widthHeight, ImageFormat imageFormat ); + bool FlipImageVertically( void *pSrc, void *pDst, int nWidth, int nHeight, ImageFormat imageFormat, int nDstStride = 0 ); + bool FlipImageHorizontally( void *pSrc, void *pDst, int nWidth, int nHeight, ImageFormat imageFormat, int nDstStride = 0 ); + bool SwapAxes( unsigned char *src, + int widthHeight, ImageFormat imageFormat ); + + + //----------------------------------------------------------------------------- + // Returns info about each image format + //----------------------------------------------------------------------------- + ImageFormatInfo_t const& ImageFormatInfo( ImageFormat fmt ); + + + //----------------------------------------------------------------------------- + // Gets the name of the image format + //----------------------------------------------------------------------------- + inline char const* GetName( ImageFormat fmt ) + { + return ImageFormatInfo(fmt).m_pName; + } + + + //----------------------------------------------------------------------------- + // Gets the size of the image format in bytes + //----------------------------------------------------------------------------- + inline int SizeInBytes( ImageFormat fmt ) + { + return ImageFormatInfo(fmt).m_NumBytes; + } + + //----------------------------------------------------------------------------- + // Does the image format support transparency? + //----------------------------------------------------------------------------- + inline bool IsTransparent( ImageFormat fmt ) + { + return ImageFormatInfo(fmt).m_NumAlphaBits > 0; + } + + + //----------------------------------------------------------------------------- + // Is the image format compressed? + //----------------------------------------------------------------------------- + inline bool IsCompressed( ImageFormat fmt ) + { + return ImageFormatInfo(fmt).m_IsCompressed; + } + + //----------------------------------------------------------------------------- + // Is any channel > 8 bits? + //----------------------------------------------------------------------------- + inline bool HasChannelLargerThan8Bits( ImageFormat fmt ) + { + ImageFormatInfo_t info = ImageFormatInfo(fmt); + return ( info.m_NumRedBits > 8 || info.m_NumGreeBits > 8 || info.m_NumBlueBits > 8 || info.m_NumAlphaBits > 8 ); + } + + inline bool IsRuntimeCompressed( ImageFormat fmt ) + { + return ( fmt == IMAGE_FORMAT_DXT1_RUNTIME ) || ( fmt == IMAGE_FORMAT_DXT5_RUNTIME ); + } + +} // end namespace ImageLoader + + +#endif // IMAGEFORMAT_H diff --git a/public/bitmap/psd.h b/public/bitmap/psd.h new file mode 100644 index 0000000..aae3cbf --- /dev/null +++ b/public/bitmap/psd.h @@ -0,0 +1,105 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: Methods relating to saving + loading PSD files (photoshop) +// +// $NoKeywords: $ +//===========================================================================// + +#ifndef PSD_H +#define PSD_H + +#ifdef _WIN32 +#pragma once +#endif + +#include "bitmap/imageformat.h" //ImageFormat enum definition + +//----------------------------------------------------------------------------- +// Forward declarations +//----------------------------------------------------------------------------- +class CUtlBuffer; +struct Bitmap_t; + +class PSDImageResources +{ +public: + enum Resource { + eResFileInfo = 0x0404 + }; + + struct ResElement { + Resource m_eType; + // unsigned char m_pReserved[4]; + unsigned short m_numBytes; + unsigned char const *m_pvData; + }; + +public: + explicit PSDImageResources( unsigned int numBytes, unsigned char const *pvBuffer ) : m_numBytes( numBytes ), m_pvBuffer( pvBuffer ) {} + +public: + ResElement FindElement( Resource eType ) const; + +protected: + unsigned int m_numBytes; + unsigned char const * m_pvBuffer; +}; + +class PSDResFileInfo +{ +public: + enum ResFileInfo { + eTitle = 0x05, + eAuthor = 0x50, + eAuthorTitle = 0x55, + eDescription = 0x78, + eDescriptionWriter = 0x7A, + eKeywords = 0x19, + eCopyrightNotice = 0x74 + }; + + struct ResFileInfoElement { + ResFileInfo m_eType; + unsigned short m_numBytes; + unsigned char const *m_pvData; + }; + +public: + explicit PSDResFileInfo( PSDImageResources::ResElement res ) : m_res( res ) {} + +public: + ResFileInfoElement FindElement( ResFileInfo eType ) const; + +protected: + PSDImageResources::ResElement m_res; +}; + + +//----------------------------------------------------------------------------- +// Is a file a PSD file? +//----------------------------------------------------------------------------- +bool IsPSDFile( const char *pFileName, const char *pPathID ); +bool IsPSDFile( CUtlBuffer &buf ); + + +//----------------------------------------------------------------------------- +// Returns information about the PSD file +//----------------------------------------------------------------------------- +bool PSDGetInfo( const char *pFileName, const char *pPathID, int *pWidth, int *pHeight, ImageFormat *pImageFormat, float *pSourceGamma ); +bool PSDGetInfo( CUtlBuffer &buf, int *pWidth, int *pHeight, ImageFormat *pImageFormat, float *pSourceGamma ); + + +//----------------------------------------------------------------------------- +// Get PSD file image resources, pointers refer into the utlbuffer +//----------------------------------------------------------------------------- +PSDImageResources PSDGetImageResources( CUtlBuffer &buf ); + + +//----------------------------------------------------------------------------- +// Reads the PSD file into the specified buffer +//----------------------------------------------------------------------------- +bool PSDReadFileRGBA8888( CUtlBuffer &buf, Bitmap_t &bitmap ); +bool PSDReadFileRGBA8888( const char *pFileName, const char *pPathID, Bitmap_t &bitmap ); + + +#endif // PSD_H diff --git a/public/bitmap/tgaloader.h b/public/bitmap/tgaloader.h new file mode 100644 index 0000000..413eb17 --- /dev/null +++ b/public/bitmap/tgaloader.h @@ -0,0 +1,45 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +// $Workfile: $ +// $Date: $ +// $NoKeywords: $ +//===========================================================================// + +#ifndef TGALOADER_H +#define TGALOADER_H + +#ifdef _WIN32 +#pragma once +#endif + +#include "bitmap/imageformat.h" +#include "tier1/utlmemory.h" + + +//----------------------------------------------------------------------------- +// Forward declarations +//----------------------------------------------------------------------------- +class CUtlBuffer; + + +namespace TGALoader +{ + +int TGAHeaderSize(); + +bool GetInfo( const char *fileName, int *width, int *height, ImageFormat *imageFormat, float *sourceGamma ); +bool GetInfo( CUtlBuffer &buf, int *width, int *height, ImageFormat *imageFormat, float *sourceGamma ); + +bool Load( unsigned char *imageData, const char *fileName, int width, int height, + ImageFormat imageFormat, float targetGamma, bool mipmap ); +bool Load( unsigned char *imageData, CUtlBuffer &buf, int width, int height, + ImageFormat imageFormat, float targetGamma, bool mipmap ); + +bool LoadRGBA8888( const char *pFileName, CUtlMemory<unsigned char> &outputData, int &outWidth, int &outHeight ); +bool LoadRGBA8888( CUtlBuffer &buf, CUtlMemory<unsigned char> &outputData, int &outWidth, int &outHeight ); + +} // end namespace TGALoader + +#endif // TGALOADER_H diff --git a/public/bitmap/tgawriter.h b/public/bitmap/tgawriter.h new file mode 100644 index 0000000..51854ba --- /dev/null +++ b/public/bitmap/tgawriter.h @@ -0,0 +1,40 @@ +//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +// $NoKeywords: $ +//===========================================================================// + +#ifndef TGAWRITER_H +#define TGAWRITER_H + +#ifdef _WIN32 +#pragma once +#endif + + +#include "tier1/interface.h" +#include "bitmap/imageformat.h" //ImageFormat enum definition + +class CUtlBuffer; + + +namespace TGAWriter +{ + + bool WriteToBuffer( unsigned char *pImageData, CUtlBuffer &buffer, int width, int height, + ImageFormat srcFormat, ImageFormat dstFormat ); + + + // write out a simple tga file from a memory buffer. + bool WriteTGAFile( const char *fileName, int width, int height, enum ImageFormat srcFormat, uint8 const *srcData, int nStride ); + +// A pair of routines for writing to files without allocating any memory in the TGA writer +// Useful for very large files such as posters, which are rendered as sub-rects anyway + bool WriteDummyFileNoAlloc( const char *fileName, int width, int height, ImageFormat dstFormat ); + bool WriteRectNoAlloc( unsigned char *pImageData, const char *fileName, int nXOrigin, int nYOrigin, int width, int height, int nStride, ImageFormat srcFormat ); + + +} // end namespace TGAWriter + +#endif // TGAWRITER_H |