You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

bitmap.cpp 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "svl/graphics/bitmap.h"
  2. #include <string> /**< std::string */
  3. #include <memory> /**< std::make_unique */
  4. #if 0
  5. #include "SDL.h" /**< SDL_* functions */
  6. #include "SDL_image.h" /**< IMG_* functions */
  7. #endif // comment
  8. #include "svl/util/logger.h"
  9. namespace svl {
  10. namespace graphics {
  11. bool Bitmap::save(const std::string &/* filePath */)
  12. {
  13. #if 0
  14. bool ret = true;
  15. // Set up the pixel format color masks for RGBA byte arrays.
  16. // NOTE: Only little-endian and RGBA is supported.
  17. std::uint32_t bmask = 0x000000ff;
  18. std::uint32_t gmask = 0x0000ff00;
  19. std::uint32_t rmask = 0x00ff0000;
  20. std::uint32_t amask = 0xff000000;
  21. if (pixelFormatType() == PixelFormatType::Abgr8888)
  22. {
  23. std::uint32_t rmask = 0x000000ff;
  24. std::uint32_t bmask = 0x00ff0000;
  25. }
  26. int depth = 32;
  27. int pitch = 4 * mWidth;
  28. SDL_Surface *surf = SDL_CreateRGBSurfaceFrom((void *)mPixels, mWidth, mHeight, depth, pitch,
  29. rmask, gmask, bmask, amask);
  30. // Ends with .bmp
  31. if (filePath.rfind(".bmp") == (filePath.length() - 4))
  32. {
  33. ret = SDL_SaveBMP(surf, filePath.c_str()) == 0;
  34. }
  35. else if (filePath.rfind(".png") == (filePath.length() - 4))
  36. {
  37. ret = IMG_SavePNG(surf, filePath.c_str()) == 0;
  38. }
  39. else
  40. {
  41. SLOGE << "Only bmp and png are supportted!";
  42. ret = false;
  43. }
  44. SDL_FreeSurface(surf);
  45. return ret;
  46. #endif // comment
  47. return false;
  48. }
  49. bool Bitmap::flip(FlipMode mode)
  50. {
  51. if ((mode & FlipMode::Horizontal) == FlipMode::Horizontal)
  52. {
  53. std::uint32_t *p = reinterpret_cast<std::uint32_t *>(mPixels);
  54. int half = mWidth >> 1;
  55. for (int i = 0; i < half; i++)
  56. {
  57. for (int j = 0; j < mHeight; j++)
  58. {
  59. auto jt = j * mWidth;
  60. std::swap(p[i + jt], p[mWidth - 1 - i + jt]);
  61. }
  62. }
  63. }
  64. if ((mode & FlipMode::Vertical) == FlipMode::Vertical)
  65. {
  66. auto line = std::make_unique<unsigned char[]>(mPitch);
  67. int half = mHeight >> 1;
  68. for (int j = 0; j < half; j++)
  69. {
  70. auto l1 = j * mPitch;
  71. auto l2 = (mHeight - 1 - j) * mPitch;
  72. memmove(line.get(), mPixels + l1, mPitch);
  73. memmove(mPixels + l1, mPixels + l2, mPitch);
  74. memmove(mPixels + l2, line.get(), mPitch);
  75. }
  76. }
  77. return true;
  78. }
  79. } // namespace graphics
  80. } // namespace svl