Files
OpenRCT2-Web/src/openrct2-ui/drawing/BitmapReader.cpp
T

105 lines
3.3 KiB
C++
Raw Normal View History

2018-05-08 23:39:03 +01:00
/*****************************************************************************
2018-06-15 14:07:34 +02:00
* Copyright (c) 2014-2018 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-05-08 23:39:03 +01:00
#include <algorithm>
#include <stdexcept>
#include <cstring>
#include <SDL2/SDL.h>
#include <openrct2/core/Imaging.h>
#include "BitmapReader.h"
static std::vector<uint8> ReadToVector(std::istream &stream)
{
std::vector<uint8> result;
if (!stream.eof() && !stream.fail())
{
stream.seekg(0, std::ios_base::end);
auto size = stream.tellg();
result.resize(size);
stream.seekg(0, std::ios_base::beg);
stream.read((char *)result.data(), size);
}
return result;
}
2018-05-08 23:42:13 +01:00
// TODO Bitmaps aren't very complicated to read so we should probably just write our
// own implementation in libopenrct2 and spare the AOT implementation registration.
2018-05-08 23:39:03 +01:00
static Image ReadBitmap(std::istream &istream, IMAGE_FORMAT format)
{
auto buffer = ReadToVector(istream);
2018-05-09 19:27:36 +01:00
auto sdlStream = SDL_RWFromConstMem(buffer.data(), (int)buffer.size());
2018-05-08 23:39:03 +01:00
auto bitmap = SDL_LoadBMP_RW(sdlStream, 1);
if (bitmap != nullptr)
{
auto numChannels = bitmap->format->BytesPerPixel;
if (numChannels < 3 || bitmap->format->BitsPerPixel < 24)
{
SDL_FreeSurface(bitmap);
throw std::runtime_error("Only 24-bit bitmaps are supported.");
}
// Copy pixels over, then discard the surface
if (SDL_LockSurface(bitmap) == 0)
{
Image image;
image.Width = bitmap->w;
image.Height = bitmap->h;
image.Depth = 32;
image.Pixels.resize(bitmap->w * bitmap->h * 4);
image.Stride = bitmap->w * 4;
// Clear image with 0xFF
std::fill(image.Pixels.begin(), image.Pixels.end(), 0xFF);
// Copy pixels over
auto src = (const uint8 *)bitmap->pixels;
auto dst = image.Pixels.data();
if (numChannels == 4)
{
for (sint32 y = 0; y < bitmap->h; y++)
{
std::memcpy(dst, src, bitmap->w);
src += bitmap->pitch;
dst += bitmap->w;
}
}
else
{
for (sint32 y = 0; y < bitmap->h; y++)
{
for (sint32 x = 0; x < bitmap->w; x++)
{
std::memcpy(dst, src, 3);
src += 3;
dst += 4;
}
src += bitmap->pitch - (bitmap->w * 3);
}
}
SDL_UnlockSurface(bitmap);
SDL_FreeSurface(bitmap);
return image;
}
else
{
SDL_FreeSurface(bitmap);
throw std::runtime_error("Unable to lock surface.");
}
}
else
{
throw std::runtime_error(SDL_GetError());
}
}
void RegisterBitmapReader()
{
Imaging::SetReader(IMAGE_FORMAT::BITMAP, ReadBitmap);
}