| 1 | /* |
| 2 | * LegacyClonk |
| 3 | * |
| 4 | * Copyright (c) 2017-2019, The LegacyClonk Team and contributors |
| 5 | * |
| 6 | * Distributed under the terms of the ISC license; see accompanying file |
| 7 | * "COPYING" for details. |
| 8 | * |
| 9 | * "Clonk" is a registered trademark of Matthes Bender, used with permission. |
| 10 | * See accompanying file "TRADEMARK" for details. |
| 11 | * |
| 12 | * To redistribute this file separately, substitute the full license texts |
| 13 | * for the above references. |
| 14 | */ |
| 15 | |
| 16 | #include <Standard.h> |
| 17 | #include <StdBitmap.h> |
| 18 | |
| 19 | StdBitmap::StdBitmap(const std::uint32_t width, const std::uint32_t height, const bool useAlpha) |
| 20 | : width(width), height(height), useAlpha(useAlpha), |
| 21 | bytes(new uint8_t[width * height * (useAlpha ? 4 : 3)]) {} |
| 22 | |
| 23 | const void *StdBitmap::GetBytes() const |
| 24 | { |
| 25 | return bytes.get(); |
| 26 | } |
| 27 | |
| 28 | void *StdBitmap::GetBytes() |
| 29 | { |
| 30 | return const_cast<void *>(const_cast<const StdBitmap &>(*this).GetBytes()); |
| 31 | } |
| 32 | |
| 33 | const void *StdBitmap::GetPixelAddr(const std::uint32_t x, const std::uint32_t y) const |
| 34 | { |
| 35 | return useAlpha ? GetPixelAddr32(x, y) : GetPixelAddr24(x, y); |
| 36 | } |
| 37 | |
| 38 | void *StdBitmap::GetPixelAddr(const std::uint32_t x, const std::uint32_t y) |
| 39 | { |
| 40 | return const_cast<void *>(const_cast<const StdBitmap &>(*this).GetPixelAddr(x, y)); |
| 41 | } |
| 42 | |
| 43 | const void *StdBitmap::GetPixelAddr24(const std::uint32_t x, const std::uint32_t y) const |
| 44 | { |
| 45 | return &bytes[(y * width + x) * 3]; |
| 46 | } |
| 47 | |
| 48 | void *StdBitmap::GetPixelAddr24(const std::uint32_t x, const std::uint32_t y) |
| 49 | { |
| 50 | return const_cast<void *>(const_cast<const StdBitmap &>(*this).GetPixelAddr24(x, y)); |
| 51 | } |
| 52 | |
| 53 | const void *StdBitmap::GetPixelAddr32(const std::uint32_t x, const std::uint32_t y) const |
| 54 | { |
| 55 | return &bytes[(y * width + x) * 4]; |
| 56 | } |
| 57 | |
| 58 | void *StdBitmap::GetPixelAddr32(const std::uint32_t x, const std::uint32_t y) |
| 59 | { |
| 60 | return const_cast<void *>(const_cast<const StdBitmap &>(*this).GetPixelAddr32(x, y)); |
| 61 | } |
| 62 | |
| 63 | std::uint32_t StdBitmap::GetPixel(const std::uint32_t x, const std::uint32_t y) const |
| 64 | { |
| 65 | return useAlpha ? GetPixel32(x, y) : GetPixel24(x, y); |
| 66 | } |
| 67 | |
| 68 | std::uint32_t StdBitmap::GetPixel24(const std::uint32_t x, const std::uint32_t y) const |
| 69 | { |
| 70 | const auto pixel = static_cast<const std::uint8_t *>(GetPixelAddr24(x, y)); |
| 71 | return RGB(r: pixel[0], g: pixel[1], b: pixel[2]); |
| 72 | } |
| 73 | |
| 74 | std::uint32_t StdBitmap::GetPixel32(const std::uint32_t x, const std::uint32_t y) const |
| 75 | { |
| 76 | return *static_cast<const std::uint32_t *>(GetPixelAddr32(x, y)); |
| 77 | } |
| 78 | |
| 79 | void StdBitmap::SetPixel(const std::uint32_t x, const std::uint32_t y, const std::uint32_t value) |
| 80 | { |
| 81 | return useAlpha ? SetPixel32(x, y, value) : SetPixel24(x, y, value); |
| 82 | } |
| 83 | |
| 84 | void StdBitmap::SetPixel24(const std::uint32_t x, const std::uint32_t y, const std::uint32_t value) |
| 85 | { |
| 86 | const auto pixel = static_cast<std::uint8_t *>(GetPixelAddr24(x, y)); |
| 87 | pixel[0] = GetRValue(value); |
| 88 | pixel[1] = GetGValue(value); |
| 89 | pixel[2] = GetBValue(value); |
| 90 | } |
| 91 | |
| 92 | void StdBitmap::SetPixel32(const std::uint32_t x, const std::uint32_t y, const std::uint32_t value) |
| 93 | { |
| 94 | *static_cast<std::uint32_t *>(GetPixelAddr32(x, y)) = value; |
| 95 | } |
| 96 | |