1/*
2 * LegacyClonk
3 *
4 * Copyright (c) 1998-2000, Matthes Bender (RedWolf Design)
5 * Copyright (c) 2017-2020, The LegacyClonk Team and contributors
6 *
7 * Distributed under the terms of the ISC license; see accompanying file
8 * "COPYING" for details.
9 *
10 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11 * See accompanying file "TRADEMARK" for details.
12 *
13 * To redistribute this file separately, substitute the full license texts
14 * for the above references.
15 */
16
17/* A structure for handling 256-color bitmap files */
18
19#include <Standard.h>
20#include <Bitmap256.h>
21
22#include <cstring>
23#include <algorithm>
24
25CBitmapInfo::CBitmapInfo()
26{
27 Default();
28}
29
30void CBitmapInfo::Default()
31{
32 Head = {};
33 Info = {};
34}
35
36int CBitmapInfo::FileBitsOffset()
37{
38 return Head.bfOffBits - sizeof(CBitmapInfo);
39}
40
41CBitmap256Info::CBitmap256Info()
42{
43 Default();
44}
45
46int CBitmap256Info::FileBitsOffset()
47{
48 return Head.bfOffBits - sizeof(CBitmap256Info);
49}
50
51void CBitmap256Info::Set(int iWdt, int iHgt, uint8_t *bypPalette)
52{
53 Default();
54 // Set header
55 Head.bfType = 0x4d42; // bmp magic "BM"
56 Head.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD) + DWordAligned(val: iWdt) * iHgt;
57 Head.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
58 // Set bitmap info
59 Info.biSize = sizeof(BITMAPINFOHEADER);
60 Info.biWidth = iWdt;
61 Info.biHeight = iHgt;
62 Info.biPlanes = 1;
63 Info.biBitCount = 8;
64 Info.biCompression = 0;
65 Info.biSizeImage = iWdt * iHgt;
66 Info.biClrUsed = Info.biClrImportant = 256;
67 // Set palette
68 for (int cnt = 0; cnt < 256; cnt++)
69 {
70 Colors[cnt].rgbRed = bypPalette[cnt * 3 + 0];
71 Colors[cnt].rgbGreen = bypPalette[cnt * 3 + 1];
72 Colors[cnt].rgbBlue = bypPalette[cnt * 3 + 2];
73 }
74}
75
76void CBitmap256Info::Default()
77{
78 CBitmapInfo::Default();
79 std::fill_n(first: Colors, n: std::size(Colors), value: RGBQUAD{});
80}
81