Files
OpenRCT2-Web/src/openrct2-ui/drawing/BitmapReader.cpp
T
Hielke Morsink 0cf256ac9e Ready copyright notice for clang-format
Clang-format sees the text behind `#pragma region` as code and formats it. Instead of stating the copyright and date there, it's now in the comment block right below it. The text "Copyright" is left in the `#pragma region` line, as clang-format sees it as a single identifier.

I took the opportunity to normalize the dates, and add the copyright notice to the source files where it was missing them (except for third-party and the generated resources.h file).
2018-06-15 14:07:34 +02:00

106 lines
3.3 KiB
C++

/*****************************************************************************
* 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.
*****************************************************************************/
#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;
}
// 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.
static Image ReadBitmap(std::istream &istream, IMAGE_FORMAT format)
{
auto buffer = ReadToVector(istream);
auto sdlStream = SDL_RWFromConstMem(buffer.data(), (int)buffer.size());
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);
}