1
0

BMPWriter.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "BMPWriter.h"
  2. #include "api__bmp.h"
  3. #include <wchar.h>
  4. #include <bfc/platform/strcmp.h>
  5. // valid items include "quality" for jpeg files with value "0" to "100"
  6. // return value is 1 if the config item is supported, 0 if it is not.
  7. int BMPWriter::setConfig(const wchar_t * item, const wchar_t * value) {
  8. return 0; // no config yet
  9. }
  10. // valid items include "quality" for jpeg files with value "0" to "100", "lossless" returns "1" if it is "0" otherwise
  11. // return value is 1 if the config item is supported, 0 if it is not.
  12. int BMPWriter::getConfig(const wchar_t * item, wchar_t * value, int valuelen) {
  13. if(!_wcsicmp(item,L"lossless")) lstrcpynW(value,L"1",valuelen);
  14. else return 0;
  15. return 1;
  16. }
  17. // returns 1 if the bit depth is supported (eg 32 for ARGB32, 24 for RGB24)
  18. // ARGB32 MUST be supported
  19. int BMPWriter::bitDepthSupported(int depth) {
  20. if(depth == 32 || depth == 24) return 1;
  21. return 0;
  22. }
  23. // returns the image in our format, free the returned buffer with api_memmgr::sysFree()
  24. void * BMPWriter::convert(const void *pixels, int bitDepth, int w, int h, int *length) {
  25. if(bitDepth != 32 && bitDepth != 24) return 0;
  26. int pixDataSize = (w * h * (bitDepth/8));
  27. int headersSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  28. *length = pixDataSize + headersSize;
  29. BITMAPFILEHEADER fileHeader={0};
  30. fileHeader.bfType = 'MB';
  31. fileHeader.bfSize = *length;
  32. fileHeader.bfOffBits = headersSize;
  33. BITMAPINFOHEADER infoHeader={0};
  34. infoHeader.biSize = sizeof(BITMAPINFOHEADER);
  35. infoHeader.biWidth = w;
  36. infoHeader.biHeight = h;
  37. infoHeader.biPlanes = 1;
  38. infoHeader.biBitCount = bitDepth;
  39. infoHeader.biCompression = BI_RGB;
  40. infoHeader.biSizeImage = pixDataSize;
  41. infoHeader.biXPelsPerMeter = 2834; //72ppi
  42. infoHeader.biYPelsPerMeter = 2834; //72ppi
  43. infoHeader.biClrUsed = 0;
  44. infoHeader.biClrImportant = 0;
  45. /*
  46. The structure of bitmap files is like this:
  47. fileheader
  48. infoheader
  49. palette (optional)
  50. data
  51. */
  52. BYTE * bmp = (BYTE *)WASABI_API_MEMMGR->sysMalloc(*length);
  53. if(!bmp) return 0;
  54. memcpy(bmp,&fileHeader,sizeof(BITMAPFILEHEADER));
  55. memcpy(bmp + sizeof(BITMAPFILEHEADER),&infoHeader,sizeof(BITMAPINFOHEADER));
  56. //memcpy(bmp + headersSize,pixels,pixDataSize);
  57. {
  58. BYTE *pOut = bmp + headersSize;
  59. BYTE *pIn = ((BYTE*)pixels) + w*h*(bitDepth/8);
  60. int d = w*(bitDepth/8);
  61. for(int i=0; i<h; i++) {
  62. pIn-=d;
  63. memcpy(pOut,pIn,d);
  64. pOut+=d;
  65. }
  66. }
  67. return bmp;
  68. }
  69. #define CBCLASS BMPWriter
  70. START_DISPATCH;
  71. CB(GETIMAGETYPENAME, getImageTypeName);
  72. CB(GETEXTENSIONS, getExtensions);
  73. CB(SETCONFIG, setConfig);
  74. CB(GETCONFIG, getConfig);
  75. CB(BITDEPTHSUPPORTED, bitDepthSupported);
  76. CB(CONVERT, convert);
  77. END_DISPATCH;
  78. #undef CBCLASS