Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include <windows.h>
  9. #include <STDIO.H>
  10. int
  11. ReadBmpFile(
  12. char* szFile,
  13. unsigned char** ppbPalette,
  14. unsigned char** ppbBits,
  15. int *pwidth,
  16. int *pheight)
  17. {
  18. int rc = 0;
  19. FILE *pfile = NULL;
  20. BITMAPFILEHEADER bmfh;
  21. BITMAPINFOHEADER bmih;
  22. RGBQUAD rgrgbPalette[256];
  23. ULONG cbPalBytes;
  24. ULONG cbBmpBits;
  25. BYTE* pbBmpBits;
  26. // Bogus parameter check
  27. if (!(ppbPalette != NULL && ppbBits != NULL))
  28. { rc = -1000; goto GetOut; }
  29. // File exists?
  30. if ((pfile = fopen(szFile, "rb")) == NULL)
  31. { rc = -1; goto GetOut; }
  32. // Read file header
  33. if (fread(&bmfh, sizeof bmfh, 1/*count*/, pfile) != 1)
  34. { rc = -2; goto GetOut; }
  35. // Bogus file header check
  36. if (!(bmfh.bfReserved1 == 0 && bmfh.bfReserved2 == 0))
  37. { rc = -2000; goto GetOut; }
  38. // Read info header
  39. if (fread(&bmih, sizeof bmih, 1/*count*/, pfile) != 1)
  40. { rc = -3; goto GetOut; }
  41. // Bogus info header check
  42. if (!(bmih.biSize == sizeof bmih && bmih.biPlanes == 1))
  43. { rc = -3000; goto GetOut; }
  44. // Bogus bit depth? Only 8-bit supported.
  45. if (bmih.biBitCount != 8)
  46. { rc = -4; goto GetOut; }
  47. // Bogus compression? Only non-compressed supported.
  48. if (bmih.biCompression != BI_RGB)
  49. { rc = -5; goto GetOut; }
  50. // Figure out how many entires are actually in the table
  51. if (bmih.biClrUsed == 0)
  52. {
  53. cbPalBytes = (1 << bmih.biBitCount) * sizeof( RGBQUAD );
  54. }
  55. else
  56. {
  57. cbPalBytes = bmih.biClrUsed * sizeof( RGBQUAD );
  58. }
  59. // Read palette (256 entries)
  60. if (fread(rgrgbPalette, cbPalBytes, 1/*count*/, pfile) != 1)
  61. { rc = -6; goto GetOut; }
  62. // Read bitmap bits (remainder of file)
  63. cbBmpBits = bmfh.bfSize - ftell(pfile);
  64. pbBmpBits = (BYTE *)malloc(cbBmpBits);
  65. if (fread(pbBmpBits, cbBmpBits, 1/*count*/, pfile) != 1)
  66. { rc = -7; goto GetOut; }
  67. // Set output parameters
  68. *ppbPalette = (BYTE *)malloc(sizeof rgrgbPalette);
  69. memcpy(*ppbPalette, rgrgbPalette, cbPalBytes);
  70. *ppbBits = pbBmpBits;
  71. *pwidth = bmih.biWidth;
  72. *pheight = bmih.biHeight;
  73. printf("w %d h %d s %d\n",bmih.biWidth, bmih.biHeight, cbBmpBits );
  74. GetOut:
  75. if (pfile) fclose(pfile);
  76. return rc;
  77. }