From f349b283f124d0fad911a4124154bc841879dcf0 Mon Sep 17 00:00:00 2001 From: r3n33 Date: Tue, 12 Mar 2024 12:19:41 -0500 Subject: [PATCH] Adding 2bpp to Import and Export in Font Editor --- pages/pagedisplaytool.cpp | 77 ++++++++++++++++++++------ pages/pagedisplaytool.ui | 110 +++++++++++++++++++++----------------- 2 files changed, 122 insertions(+), 65 deletions(-) diff --git a/pages/pagedisplaytool.cpp b/pages/pagedisplaytool.cpp index 75ef9257d..3dd7ec262 100644 --- a/pages/pagedisplaytool.cpp +++ b/pages/pagedisplaytool.cpp @@ -250,7 +250,9 @@ QFont PageDisplayTool::getSelectedFontEditor() QFont f = ui->fontEditFontBox->currentFont(); f.setPointSizeF(500.0); f.setBold(ui->fontEditBoldBox->isChecked()); - f.setStyleStrategy(QFont::NoAntialias); + if (!ui->fontEditAABox->isChecked()) { + f.setStyleStrategy(QFont::NoAntialias); + } QFontMetrics fm(f); double fh = (500.0 * (double)ui->fontEditHBox->value()) / (double)fm.ascent(); @@ -336,10 +338,11 @@ void PageDisplayTool::on_fontEditExportButton_clicked() int w = ui->fontEditWBox->value(); int h = ui->fontEditHBox->value(); - int bytesPerChar = (w * h) / 8; int charNum = ui->fontEditNumOnlyButton->isChecked() ? 10 : 95; - if ((w * h) % 8 != 0) { + int bitsNeededPerChar = w * h * (ui->fontEditAABox->isChecked() ? 2 : 1); + int bytesPerChar = bitsNeededPerChar / 8; + if (bitsNeededPerChar % 8 != 0) { bytesPerChar++; } @@ -353,16 +356,48 @@ void PageDisplayTool::on_fontEditExportButton_clicked() fontArr[0] = w; fontArr[1] = h; fontArr[2] = charNum; - fontArr[3] = 1; // 1 bit per char + fontArr[3] = (ui->fontEditAABox->isChecked() ? 2 : 1); // 1 or 2 bits per pixel auto imgFont = ui->fontEditor->getEdit()->getImageBase(); - for (int ch = 0;ch < charNum; ch++) { - for (int i = 0;i < w * h; i++) { - QColor px = imgFont.pixel(ch * w + i % w, i / w); - char c = fontArr[4 + bytesPerChar * ch + i / 8]; - c |= (px != Qt::black) << (i % 8); - fontArr[4 + bytesPerChar * ch + (i / 8)] = c; + if (ui->fontEditAABox->isChecked()) { + // 2 bits per pixel + QColor colors[] = { QColor(0x00, 0x00, 0x00), QColor(0x55, 0x55, 0x55), QColor(0xAA, 0xAA, 0xAA), QColor(0xFF, 0xFF, 0xFF) }; + for (int ch = 0; ch < charNum; ch++) { + for (int i = 0; i < w * h; i += 4) { + char byteValue = 0; + for (int bitPos = 0; bitPos < 4; bitPos++) { + int pixelIndex = i + bitPos; + if (pixelIndex >= w * h) break; + QColor px = imgFont.pixel((ch * w + pixelIndex % w), pixelIndex / w); + int grayValue = qGray(px.rgb()); + + // Find the closest palette color + int minDistance = INT_MAX; + int palletIndex = 0; + for (int k = 0; k < 4; k++) { + int distance = abs(grayValue - qGray(colors[k].rgb())); + if (distance < minDistance) { + minDistance = distance; + palletIndex = k; + } + } + // Shift pixel value into position + byteValue |= palletIndex << (bitPos * 2); + } + // Store byte with 4 indexed pixels + fontArr[4 + bytesPerChar * ch + i / 4] = byteValue; + } + } + } else { + // 1 bit per pixel + for (int ch = 0; ch < charNum; ch++) { + for (int i = 0;i < w * h; i++) { + QColor px = imgFont.pixel(ch * w + i % w, i / w); + char c = fontArr[4 + bytesPerChar * ch + i / 8]; + c |= (px != Qt::black) << (i % 8); + fontArr[4 + bytesPerChar * ch + (i / 8)] = c; + } } } @@ -392,10 +427,10 @@ void PageDisplayTool::on_fontEditImportButton_clicked() int w = data.vbPopFrontUint8(); int h = data.vbPopFrontUint8(); int charNum = data.vbPopFrontUint8(); - data.vbPopFrontUint8(); + int bitsPerPixel = data.vbPopFrontUint8(); - int bytesPerChar = (w * h) / 8; - if ((w * h) % 8 != 0) { + int bytesPerChar = (w * h) / (bitsPerPixel == 2 ? 4 : 8); + if ((w * h) % (bitsPerPixel == 2 ? 4 : 8) != 0) { bytesPerChar++; } @@ -404,10 +439,22 @@ void PageDisplayTool::on_fontEditImportButton_clicked() QImage img(w * charNum, h, QImage::Format_ARGB32); img.fill(Qt::black); + //TODO: How do I get these colors from the pallet below the view? + QColor colors[] = { QColor(0x00, 0x00, 0x00), QColor(0x55, 0x55, 0x55), QColor(0xAA, 0xAA, 0xAA), QColor(0xFF, 0xFF, 0xFF) }; + for (int ch = 0;ch < charNum; ch++) { for (int i = 0;i < w * h; i++) { - char c = data[bytesPerChar * ch + i / 8]; - QColor px = (c >> (i % 8) & 0x01) ? Qt::white : Qt::black; + char c = data[bytesPerChar * ch + i / (bitsPerPixel == 2 ? 4 : 8)]; + QColor px; + if (bitsPerPixel == 2) { + // Extract 2 bits + int palletIndex = (c >> ((i % 4) * 2)) & 0x03; + px = colors[palletIndex]; + } else { + // 1 bitsPerPixel + int bitValue = (c >> (i % 8)) & 0x01; + px = bitValue ? Qt::white : Qt::black; + } img.setPixelColor(ch * w + i % w, i / w, px); } } diff --git a/pages/pagedisplaytool.ui b/pages/pagedisplaytool.ui index 8c5e9c59b..9b2dbc2cc 100644 --- a/pages/pagedisplaytool.ui +++ b/pages/pagedisplaytool.ui @@ -20,7 +20,7 @@ QTabWidget::Triangular - 0 + 2 @@ -707,13 +707,9 @@ - - - - Numbers Only - - - + + 6 + @@ -724,33 +720,22 @@ - - - - Draw border around characters to see their bounding box + + + + Scale: - - Border + + 2 - - true + + 10.000000000000000 - - - - - - Bold + + 0.010000000000000 - - - - - - - 0 - 0 - + + 1.000000000000000 @@ -767,6 +752,26 @@ + + + + Draw border around characters to see their bounding box + + + Border + + + true + + + + + + + Import Font + + + @@ -780,22 +785,10 @@ - - - - Scale: - - - 2 - - - 10.000000000000000 - - - 0.010000000000000 - - - 1.000000000000000 + + + + Bold @@ -822,10 +815,27 @@ - - + + - Import Font + Numbers Only + + + + + + + + 0 + 0 + + + + + + + + Anti Alias (2bpp)