comparison ostc45_icon.cpp @ 5:115cfa4a3239 default tip

Added icon upload function for OSTC 4/5 For the upload the same process as the one for the firmware update is used => CRC functionality has been copied from the ostc_pack SW
author Ideenmodellierer
date Tue, 30 Dec 2025 21:41:02 +0100
parents
children
comparison
equal deleted inserted replaced
4:e30f00f760d3 5:115cfa4a3239
1 #include "ostc45_icon.h"
2
3
4 //OSTC45_Icon::OSTC45_Icon() {}
5
6
7 #pragma pack(push, 1)
8 struct BMPFileHeader
9 {
10 uint16_t bfType;
11 uint32_t bfSize;
12 uint16_t bfReserved1;
13 uint16_t bfReserved2;
14 uint32_t bfOffBits;
15 };
16
17 struct BMPInfoHeader
18 {
19 uint32_t biSize;
20 int32_t biWidth;
21 int32_t biHeight;
22 uint16_t biPlanes;
23 uint16_t biBitCount;
24 uint32_t biCompression;
25 uint32_t biSizeImage;
26 int32_t biXPelsPerMeter;
27 int32_t biYPelsPerMeter;
28 uint32_t biClrUsed;
29 uint32_t biClrImportant;
30 };
31 #pragma pack(pop)
32
33 BmpToArray::BmpToArray(const QString& filename)
34 {
35 loadBMP(filename);
36 }
37
38 void BmpToArray::loadBMP(const QString& filename)
39 {
40 QFile file(filename);
41 int dstY;
42 QByteArray rowBuffer;
43 bool topDown = false;
44 BMPFileHeader fileHeader;
45 BMPInfoHeader infoHeader;
46
47 if (!file.open(QIODevice::ReadOnly))
48 throw std::runtime_error("Cannot open BMP file");
49
50 if (file.read(reinterpret_cast<char*>(&fileHeader),
51 sizeof(BMPFileHeader)) != sizeof(BMPFileHeader))
52 throw std::runtime_error("Failed to read BMP file header");
53
54 if (file.read(reinterpret_cast<char*>(&infoHeader),
55 sizeof(BMPInfoHeader)) != sizeof(BMPInfoHeader))
56 throw std::runtime_error("Failed to read BMP info header");
57
58 if (fileHeader.bfType != 0x4D42)
59 throw std::runtime_error("Not a valid BMP file");
60
61 if (infoHeader.biBitCount != 8)
62 throw std::runtime_error("Only 8-bit BMP supported");
63 if (infoHeader.biCompression != 0)
64 throw std::runtime_error("Compressed BMP not supported");
65
66 if(infoHeader.biWidth > 800)
67 throw std::runtime_error("Only BMP with 800 or less horizontal pixels supported");
68
69 if(infoHeader.biHeight > 480)
70 throw std::runtime_error("Only BMP with 480 or less vertical pixels supported");
71
72 // Width / Height
73 width = infoHeader.biWidth;
74 height = infoHeader.biHeight;
75
76 if ((int32_t)height < 0) {
77 height = -((int32_t)height);
78 topDown = true;
79 }
80
81 // Palette
82 uint32_t colorCount = infoHeader.biClrUsed;
83 if (colorCount == 0) colorCount = 256;
84 if (colorCount > 256) colorCount = 256;
85
86 clut.resize(colorCount);
87
88 for (uint32_t i = 0; i < colorCount; ++i)
89 file.read(reinterpret_cast<char*>(&clut[i]), 4);
90
91 // CLUT in 32-Bit transform 0x00RRGGBB
92 clut32.resize(255, 0);
93
94 for (uint32_t i = 0; i < colorCount && i < 255; ++i)
95 {
96 clut32[i] =
97 (clut[i].r << 16) |
98 (clut[i].g << 8) |
99 (clut[i].b);
100 }
101
102 // Pixel-Data
103 if (!file.seek(fileHeader.bfOffBits))
104 throw std::runtime_error("Failed to seek to pixel data");
105
106 pixelData.resize(width * height);
107
108 size_t rowSize = (width + 3) & ~3; // 4-Byte alignment
109
110 rowBuffer.resize(rowSize);
111
112 for (int y = 0; y < height; ++y)
113 {
114 if (file.read(rowBuffer.data(), rowSize) != rowSize)
115 throw std::runtime_error("Failed to read BMP pixel row");
116
117 if (topDown)
118 {
119 dstY = height - 1 - y;
120 }
121 else
122 {
123 dstY = y;
124 }
125
126 for (int x = 0; x < width; ++x)
127 {
128 pixelData[x * height + dstY] =
129 static_cast<uint8_t>(rowBuffer[x]);
130 }
131 }
132
133
134 }
135 QByteArray BmpToArray::getTransferBytes() const
136 {
137 QByteArray out;
138
139 out.reserve(clut32.size() * 4 + pixelData.size());
140
141 // CLUT (32 Bit, Little Endian)
142 for (uint32_t color : clut32)
143 {
144 out.append(static_cast<char>( color & 0xFF));
145 out.append(static_cast<char>((color >> 8) & 0xFF));
146 out.append(static_cast<char>((color >> 16) & 0xFF));
147 out.append(static_cast<char>((color >> 24) & 0xFF));
148 }
149
150 // Pixel (8 Bit)
151 out.append(reinterpret_cast<const char*>(pixelData.data()),
152 pixelData.size());
153
154 return out;
155 }
156
157 void BmpToArray::getImageXY(uint32_t* x, uint32_t* y)
158 {
159 *x = width;
160 *y = height;
161 }
162
163