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