|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #include "svl/graphics/bitmap.h"
-
- #include <string> /**< std::string */
- #include <memory> /**< std::make_unique */
-
- #if 0
- #include "SDL.h" /**< SDL_* functions */
- #include "SDL_image.h" /**< IMG_* functions */
- #endif // comment
-
- #include "svl/util/logger.h"
-
- namespace svl {
- namespace graphics {
-
- bool Bitmap::save(const std::string &/* filePath */)
- {
- #if 0
- bool ret = true;
- // Set up the pixel format color masks for RGBA byte arrays.
- // NOTE: Only little-endian and RGBA is supported.
- std::uint32_t bmask = 0x000000ff;
- std::uint32_t gmask = 0x0000ff00;
- std::uint32_t rmask = 0x00ff0000;
- std::uint32_t amask = 0xff000000;
-
- if (pixelFormatType() == PixelFormatType::Abgr8888)
- {
- std::uint32_t rmask = 0x000000ff;
- std::uint32_t bmask = 0x00ff0000;
- }
-
- int depth = 32;
- int pitch = 4 * mWidth;
- SDL_Surface *surf = SDL_CreateRGBSurfaceFrom((void *)mPixels, mWidth, mHeight, depth, pitch,
- rmask, gmask, bmask, amask);
-
- // Ends with .bmp
- if (filePath.rfind(".bmp") == (filePath.length() - 4))
- {
- ret = SDL_SaveBMP(surf, filePath.c_str()) == 0;
- }
- else if (filePath.rfind(".png") == (filePath.length() - 4))
- {
- ret = IMG_SavePNG(surf, filePath.c_str()) == 0;
- }
- else
- {
- SLOGE << "Only bmp and png are supportted!";
- ret = false;
- }
-
- SDL_FreeSurface(surf);
- return ret;
- #endif // comment
- return false;
- }
-
- bool Bitmap::flip(FlipMode mode)
- {
- if ((mode & FlipMode::Horizontal) == FlipMode::Horizontal)
- {
- std::uint32_t *p = reinterpret_cast<std::uint32_t *>(mPixels);
- int half = mWidth >> 1;
-
- for (int i = 0; i < half; i++)
- {
- for (int j = 0; j < mHeight; j++)
- {
- auto jt = j * mWidth;
- std::swap(p[i + jt], p[mWidth - 1 - i + jt]);
- }
- }
- }
-
- if ((mode & FlipMode::Vertical) == FlipMode::Vertical)
- {
- auto line = std::make_unique<unsigned char[]>(mPitch);
- int half = mHeight >> 1;
-
- for (int j = 0; j < half; j++)
- {
- auto l1 = j * mPitch;
- auto l2 = (mHeight - 1 - j) * mPitch;
- memmove(line.get(), mPixels + l1, mPitch);
- memmove(mPixels + l1, mPixels + l2, mPitch);
- memmove(mPixels + l2, line.get(), mPitch);
- }
- }
-
- return true;
- }
-
- } // namespace graphics
- } // namespace svl
-
|