Source code of Windows XP (NT5)
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.

108 lines
2.9 KiB

  1. /*************************************************************************/
  2. /* Copyright (C) 2000 Microsoft Corporation */
  3. /* File: savebmp.cpp */
  4. /* Description: Save a CaptureBitmapData to BMP file format. */
  5. /* Note: This is not a general BMP encode function. */
  6. /* Special assumptions made about bitmap format: */
  7. /* RGB (3 bytes per pixel without colormap) */
  8. /* */
  9. /* Author: Phillip Lu */
  10. /*************************************************************************/
  11. #include "stdafx.h"
  12. #include <stdio.h>
  13. #include "capture.h"
  14. HRESULT WriteBitmapDataToBMPFile(char *filename, CaptureBitmapData *bmpdata)
  15. {
  16. BITMAPFILEHEADER bfh;
  17. BITMAPINFOHEADER bmih;
  18. UINT numColors = 0; // Number of colors in bmiColors
  19. FILE *outfile = NULL;
  20. HRESULT hr = S_OK;
  21. BYTE *bufline = NULL;
  22. int bitmapStride;
  23. int nBytesWritten;
  24. // Setup BITMAPINFOHEADER
  25. ZeroMemory(&bmih, sizeof(bmih));
  26. bmih.biSize = sizeof(bmih);
  27. bmih.biWidth = bmpdata->Width;
  28. bmih.biHeight = bmpdata->Height;
  29. bmih.biPlanes = 1;
  30. bmih.biCompression = BI_RGB;
  31. bmih.biBitCount = 24;
  32. // Compute the bitmap stride
  33. bitmapStride = (bmpdata->Width * bmih.biBitCount + 7) / 8;
  34. bitmapStride = (bitmapStride + 3) & (~3);
  35. // Now fill in the BITMAPFILEHEADER
  36. bfh.bfType = 0x4d42;
  37. bfh.bfReserved1 = 0;
  38. bfh.bfReserved2 = 0;
  39. bfh.bfOffBits = sizeof(bfh) + sizeof(bmih) + numColors * sizeof(RGBQUAD);
  40. bfh.bfSize = bfh.bfOffBits + bitmapStride * bmpdata->Height;
  41. // allocate a buffer to hold one line of bitmap data
  42. bufline = new BYTE[bitmapStride];
  43. if (NULL == bufline)
  44. {
  45. hr = E_OUTOFMEMORY;
  46. return hr;
  47. }
  48. ZeroMemory(bufline, bitmapStride);
  49. if ((outfile = fopen(filename, "wb")) == NULL)
  50. {
  51. delete[] bufline;
  52. hr = E_FAIL;
  53. return hr;
  54. }
  55. // Write the BITMAPFILEHEADER
  56. fwrite((void *)&bfh, sizeof(bfh), 1, outfile);
  57. // Write the BITMAPINFOHEADER
  58. fwrite((void *)&bmih, sizeof(bmih), 1, outfile);
  59. // Write bitmap data
  60. for (int iLine = bmpdata->Height-1; iLine >= 0; iLine--)
  61. {
  62. BYTE *bmpSrc = bmpdata->Scan0 + iLine*bmpdata->Stride;
  63. BYTE *bmpDst = bufline;
  64. for (int iPixel=0; iPixel<bmpdata->Width; iPixel++)
  65. {
  66. // in BMP file a pixel is in BGR order, so we have reverse it
  67. bmpDst[2] = *bmpSrc++;
  68. bmpDst[1] = *bmpSrc++;
  69. bmpDst[0] = *bmpSrc++;
  70. bmpDst += 3;
  71. }
  72. fwrite(bufline, bitmapStride, 1, outfile);
  73. }
  74. if (bufline != NULL)
  75. {
  76. delete[] bufline;
  77. }
  78. if (outfile != NULL)
  79. {
  80. fclose(outfile);
  81. }
  82. return hr;
  83. }