Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding 2bpp to Import and Export in Font Editor #359

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 62 additions & 15 deletions pages/pagedisplaytool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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++;
}

Expand All @@ -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;
}
}
}

Expand Down Expand Up @@ -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++;
}

Expand All @@ -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?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am curious if I can grab the pallet colors from the display editor at this point? I spent an embarrassing amount of time trying to figure it out and I'm not sure if we want to use the pallet colors in case the user is in indexed 2 or 16 format. Advice would be much appreciated!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on the antialiasing!

If you think it helps to grab and use the palette-colors here I can add some functions to enable that. I just pushed some code that updates the palette when activating anti-aliasing.

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);
}
}
Expand Down
110 changes: 60 additions & 50 deletions pages/pagedisplaytool.ui
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<enum>QTabWidget::Triangular</enum>
</property>
<property name="currentIndex">
<number>0</number>
<number>2</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
Expand Down Expand Up @@ -707,13 +707,9 @@
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="1" colspan="2">
<widget class="QRadioButton" name="fontEditNumOnlyButton">
<property name="text">
<string>Numbers Only</string>
</property>
</widget>
</item>
<property name="horizontalSpacing">
<number>6</number>
</property>
<item row="0" column="0">
<widget class="QRadioButton" name="fontEditAllButton">
<property name="text">
Expand All @@ -724,33 +720,22 @@
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QCheckBox" name="fontEditBorderBox">
<property name="toolTip">
<string>Draw border around characters to see their bounding box</string>
<item row="2" column="2" colspan="2">
<widget class="QDoubleSpinBox" name="fontEditScaleBox">
<property name="prefix">
<string>Scale: </string>
</property>
<property name="text">
<string>Border</string>
<property name="decimals">
<number>2</number>
</property>
<property name="checked">
<bool>true</bool>
<property name="maximum">
<double>10.000000000000000</double>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QCheckBox" name="fontEditBoldBox">
<property name="text">
<string>Bold</string>
<property name="singleStep">
<double>0.010000000000000</double>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QFontComboBox" name="fontEditFontBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<property name="value">
<double>1.000000000000000</double>
</property>
</widget>
</item>
Expand All @@ -767,6 +752,26 @@
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QCheckBox" name="fontEditBorderBox">
<property name="toolTip">
<string>Draw border around characters to see their bounding box</string>
</property>
<property name="text">
<string>Border</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="5" column="2" colspan="2">
<widget class="QPushButton" name="fontEditImportButton">
<property name="text">
<string>Import Font</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="fontEditHBox">
<property name="prefix">
Expand All @@ -780,22 +785,10 @@
</property>
</widget>
</item>
<item row="2" column="2" colspan="2">
<widget class="QDoubleSpinBox" name="fontEditScaleBox">
<property name="prefix">
<string>Scale: </string>
</property>
<property name="decimals">
<number>2</number>
</property>
<property name="maximum">
<double>10.000000000000000</double>
</property>
<property name="singleStep">
<double>0.010000000000000</double>
</property>
<property name="value">
<double>1.000000000000000</double>
<item row="1" column="2">
<widget class="QCheckBox" name="fontEditBoldBox">
<property name="text">
<string>Bold</string>
</property>
</widget>
</item>
Expand All @@ -822,10 +815,27 @@
</property>
</widget>
</item>
<item row="5" column="2" colspan="2">
<widget class="QPushButton" name="fontEditImportButton">
<item row="0" column="1" colspan="2">
<widget class="QRadioButton" name="fontEditNumOnlyButton">
<property name="text">
<string>Import Font</string>
<string>Numbers Only</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QFontComboBox" name="fontEditFontBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="fontEditAABox">
<property name="text">
<string>Anti Alias (2bpp)</string>
</property>
</widget>
</item>
Expand Down
Loading