summaryrefslogtreecommitdiff
path: root/utils/studiomdl/bmpread.cpp
diff options
context:
space:
mode:
authorFluorescentCIAAfricanAmerican <[email protected]>2020-04-22 12:56:21 -0400
committerFluorescentCIAAfricanAmerican <[email protected]>2020-04-22 12:56:21 -0400
commit3bf9df6b2785fa6d951086978a3e66f49427166a (patch)
tree2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /utils/studiomdl/bmpread.cpp
downloadarchived-source-engine-2018-hl2-src-master.tar.xz
archived-source-engine-2018-hl2-src-master.zip
Diffstat (limited to 'utils/studiomdl/bmpread.cpp')
-rw-r--r--utils/studiomdl/bmpread.cpp98
1 files changed, 98 insertions, 0 deletions
diff --git a/utils/studiomdl/bmpread.cpp b/utils/studiomdl/bmpread.cpp
new file mode 100644
index 0000000..24663fe
--- /dev/null
+++ b/utils/studiomdl/bmpread.cpp
@@ -0,0 +1,98 @@
+//========= Copyright Valve Corporation, All rights reserved. ============//
+//
+// Purpose:
+//
+// $NoKeywords: $
+//
+//=============================================================================//
+
+
+#include <windows.h>
+#include <STDIO.H>
+
+
+int
+ReadBmpFile(
+ char* szFile,
+ unsigned char** ppbPalette,
+ unsigned char** ppbBits,
+ int *pwidth,
+ int *pheight)
+ {
+ int rc = 0;
+ FILE *pfile = NULL;
+ BITMAPFILEHEADER bmfh;
+ BITMAPINFOHEADER bmih;
+ RGBQUAD rgrgbPalette[256];
+ ULONG cbPalBytes;
+ ULONG cbBmpBits;
+ BYTE* pbBmpBits;
+
+ // Bogus parameter check
+ if (!(ppbPalette != NULL && ppbBits != NULL))
+ { rc = -1000; goto GetOut; }
+
+ // File exists?
+ if ((pfile = fopen(szFile, "rb")) == NULL)
+ { rc = -1; goto GetOut; }
+
+ // Read file header
+ if (fread(&bmfh, sizeof bmfh, 1/*count*/, pfile) != 1)
+ { rc = -2; goto GetOut; }
+
+ // Bogus file header check
+ if (!(bmfh.bfReserved1 == 0 && bmfh.bfReserved2 == 0))
+ { rc = -2000; goto GetOut; }
+
+ // Read info header
+ if (fread(&bmih, sizeof bmih, 1/*count*/, pfile) != 1)
+ { rc = -3; goto GetOut; }
+
+ // Bogus info header check
+ if (!(bmih.biSize == sizeof bmih && bmih.biPlanes == 1))
+ { rc = -3000; goto GetOut; }
+
+ // Bogus bit depth? Only 8-bit supported.
+ if (bmih.biBitCount != 8)
+ { rc = -4; goto GetOut; }
+
+ // Bogus compression? Only non-compressed supported.
+ if (bmih.biCompression != BI_RGB)
+ { rc = -5; goto GetOut; }
+
+ // Figure out how many entires are actually in the table
+ if (bmih.biClrUsed == 0)
+ {
+ cbPalBytes = (1 << bmih.biBitCount) * sizeof( RGBQUAD );
+ }
+ else
+ {
+ cbPalBytes = bmih.biClrUsed * sizeof( RGBQUAD );
+ }
+
+ // Read palette (256 entries)
+ if (fread(rgrgbPalette, cbPalBytes, 1/*count*/, pfile) != 1)
+ { rc = -6; goto GetOut; }
+
+ // Read bitmap bits (remainder of file)
+ cbBmpBits = bmfh.bfSize - ftell(pfile);
+ pbBmpBits = (BYTE *)malloc(cbBmpBits);
+ if (fread(pbBmpBits, cbBmpBits, 1/*count*/, pfile) != 1)
+ { rc = -7; goto GetOut; }
+
+ // Set output parameters
+ *ppbPalette = (BYTE *)malloc(sizeof rgrgbPalette);
+ memcpy(*ppbPalette, rgrgbPalette, cbPalBytes);
+ *ppbBits = pbBmpBits;
+
+
+ *pwidth = bmih.biWidth;
+ *pheight = bmih.biHeight;
+
+ printf("w %d h %d s %d\n",bmih.biWidth, bmih.biHeight, cbBmpBits );
+
+GetOut:
+ if (pfile) fclose(pfile);
+ return rc;
+ }
+