Page 1 of 1

[SOLVED] troubles with near-neighbor upscaling bitmap fonts

Posted: Fri Nov 27, 2020 10:27 pm
by austanss
I have this code:

Code: Select all

void Terminal::put_entry_at(char c, uint32_t color, size_t xpos, size_t ypos)
{
	uint64_t font_selector = FONT[c];

	uint8_t bits[64];

	for (uint8_t i = 1; i <= 64; i++)
	{
		bits[i] = get_bit(font_selector, i);
	}

//	for (int i = 63; i >= 0; i--)
//	{
//		if ((i + 1) % 8 == 0)
//			serial_msg('\n');
//		serial_msg(new_bits[i] + 48); // 48, ASCII code '0'
//	}

	for (uint32_t y = 0, yy = (ypos * 16); y < 8; y++, yy += 2)
	{
		for (uint32_t x = 0, xx = (xpos * 16); x < 8; x++, xx += 2)
		{
			if (bits[(8 * y) + x])
				plot_pixel(pos(xx, 		yy), 		color);
				plot_pixel(pos(xx + 1, 	yy), 		color);
				plot_pixel(pos(xx, 		yy + 1), 	color);
				plot_pixel(pos(xx + 1, 	yy + 1), 	color);
		}
	}
}
I originally believed this would simply upscale bicubic 2x for each pixel, but I'm getting visually buggy results.

This is the code for the message I am attempting to display:

Code: Select all

	terminal.put_entry_at('H', 0x00FFFF, 0,	 0);
	terminal.put_entry_at('e', 0x00FFFF, 1,	 0);
	terminal.put_entry_at('l', 0x00FFFF, 2,	 0);
	terminal.put_entry_at('l', 0x00FFFF, 3,	 0);
	terminal.put_entry_at('o', 0x00FFFF, 4,	 0);
	terminal.put_entry_at(',', 0x00FFFF, 5,	 0);
	terminal.put_entry_at(' ', 0x00FFFF, 6,	 0);
	terminal.put_entry_at('w', 0x00FFFF, 7,	 0);
	terminal.put_entry_at('o', 0x00FFFF, 8,	 0);
	terminal.put_entry_at('r', 0x00FFFF, 9,	 0);
	terminal.put_entry_at('l', 0x00FFFF, 10, 0);
	terminal.put_entry_at('d', 0x00FFFF, 11, 0);
	terminal.put_entry_at('!', 0x00FFFF, 12, 0);
This is the result:
2020-11-27_23-26.png
2020-11-27_23-26.png (1.22 KiB) Viewed 2305 times
Anyone know why this is? I've been stuck on this for a while and have made some progress, but the background coloring is killing me. It's not supposed to be there. I can't do without the upscaling because the text is too small and I have terrible vision...

Re: troubles with bicubic upscaling bitmap fonts

Posted: Fri Nov 27, 2020 10:40 pm
by Octocontrabass
rizxt wrote:bicubic
This is nearest-neighbor, not bicubic.
rizxt wrote:

Code: Select all

			if (bits[(8 * y) + x])
With no braces following your if statement, only the first pixel is skipped when the pixel should be blank. The other three are always drawn.
rizxt wrote:I can't do without the upscaling because the text is too small and I have terrible vision...
Is there no way to tell your VM to upscale its output?

Re: troubles with bicubic upscaling bitmap fonts

Posted: Sat Nov 28, 2020 7:25 am
by bzt
Hi

Yes, @Octocontrabass is right.
rizxt wrote:I can't do without the upscaling because the text is too small and I have terrible vision...
Scaling bitmap fonts is a very difficult thing if you want them to look good. You should give a try to Scalable Screen Font. It is an stb-style, one header only renderer, that adds less than 32k code to your kernel. Normally it only depends on 4 libc functions (realloc, free, memset, memcpy), which you should already have in your kernel, but with a define you can also make it totally dependency-free (with limited features).

The normal ssfn_render() does not only upscale the bitmap font, but it also smooths the edges and uses anti-aliasing for sub-pixel rendering to make them look good and more readable.

See the difference in the image below in the two "scaling bitmap fonts" lines. Compare the two "g"s in the word "scaling" for example. Can you see how different they look?
Image

As a bonus, the renderer also supports vector fonts (like TrueType) and colorful pixmap fonts (for emojis) in addition to bitmap fonts with the same API.

Cheers,
bzt

Re: troubles with bicubic upscaling bitmap fonts

Posted: Sat Nov 28, 2020 8:52 am
by austanss
Octocontrabass wrote:
rizxt wrote:bicubic
This is nearest-neighbor, not bicubic.
rizxt wrote:

Code: Select all

			if (bits[(8 * y) + x])
With no braces following your if statement, only the first pixel is skipped when the pixel should be blank. The other three are always drawn.
rizxt wrote:I can't do without the upscaling because the text is too small and I have terrible vision...
Is there no way to tell your VM to upscale its output?
Took me a little while to figure out what your words meant, but thanks for pointing that out! I don't know how I failed to recognize this! Thank you, it works now!

Re: troubles with bicubic upscaling bitmap fonts

Posted: Sat Nov 28, 2020 8:55 am
by austanss
bzt wrote:Hi

Yes, @Octocontrabass is right.
rizxt wrote:I can't do without the upscaling because the text is too small and I have terrible vision...
Scaling bitmap fonts is a very difficult thing if you want them to look good. You should give a try to Scalable Screen Font. It is an stb-style, one header only renderer, that adds less than 32k code to your kernel. Normally it only depends on 4 libc functions (realloc, free, memset, memcpy), which you should already have in your kernel, but with a define you can also make it totally dependency-free (with limited features).

The normal ssfn_render() does not only upscale the bitmap font, but it also smooths the edges and uses anti-aliasing for sub-pixel rendering to make them look good and more readable.

See the difference in the image below in the two "scaling bitmap fonts" lines. Compare the two "g"s in the word "scaling" for example. Can you see how different they look?
Image

As a bonus, the renderer also supports vector fonts (like TrueType) and colorful pixmap fonts (for emojis) in addition to bitmap fonts with the same API.

Cheers,
bzt
I'm not interested in using external code unless I modify a large portion of the code. The only code I've borrowed is the bootloader, and even then I made major changes to it's functionality.

Re: troubles with bicubic upscaling bitmap fonts

Posted: Sat Nov 28, 2020 2:26 pm
by bzt
rizxt wrote:I'm not interested in using external code
As you feel, however if you have trouble with a simple upscale, then maybe you shouldn't try to reinvent the wheel. Your implementation that uses "plot_pixel" and "pos" calls in a loop is extremely ineffective.
rizxt wrote:unless I modify a large portion of the code.
"No need to fix if it's not broken". SSFN is Open Source and licensed under MIT, you are free to modify it as you please. But it was designed with care to be easily integrated, needs no library linkage, uses no FPU and has extremely minimal, libc-only dependencies. Just saying.
rizxt wrote:The only code I've borrowed is the bootloader, and even then I made major changes to it's functionality.
If you prefer your own code over an external one, then why did you borrow the bootloader, may I ask? Writing a bootloader is one of the easiest and most straightforward tasks in OSDev. All the rest is a lot more complicated. Don't get me wrong, I'm just curious why did you borrowed it then?

Cheers,
bzt

Re: [SOLVED] troubles with near-neighbor upscaling bitmap fo

Posted: Sat Nov 28, 2020 2:53 pm
by nexos
bzt wrote:Writing a bootloader is one of the easiest and most straightforward tasks in OSDev.
In that case, easy = distracting.
Writing a boot loader takes time. And when you start working on your kernel, then when a bug occurs, the bug may be in the bootloader. I enjoy making bootloaders, so i do custom make it, but it is distracting.
I generally don't reinvent the wheel if it is something small and annoying. For example, I would never use LwIP. OTOH, I will (initially) use a premade libc in my OS. First I have to stop recoding my kernel :lol: .

Re: troubles with bicubic upscaling bitmap fonts

Posted: Sat Nov 28, 2020 3:01 pm
by austanss
bzt wrote:[
rizxt wrote:The only code I've borrowed is the bootloader, and even then I made major changes to it's functionality.
If you prefer your own code over an external one, then why did you borrow the bootloader, may I ask? Writing a bootloader is one of the easiest and most straightforward tasks in OSDev. All the rest is a lot more complicated. Don't get me wrong, I'm just curious why did you borrowed it then?
Why did I borrow it? I had just started out in UEFI and I was unfamilliar with many of the functions that come with boot services. There is hardly any documentation. The UEFI spec doesn't describe how you might use some function, or how it is useful. The code I borrowed was pretty self-documenting, and I found it very easy to work with. Some people say you could have just gone with a prebuilt bootloader instead of copying source code. I disagree. I didn't want to carbon-copy a bootloader. I found GRUB's MultiBoot and configurations extremely confusing. I wanted a dedicated bootloader, similar to Windows'. Thus, I went with a modified OSS bootloader.

I also didn't use GRUB because it was getting difficult to maintain the proper options in the Multiboot header, and the binary size was overwhelming compared to my kernel.

Yes, bootloaders are pretty simple. And quite fun to work with.

Also, the bootloader I copied had many bugs that made it completely dysfunctional. I fixed these.

I prefer to dive into the deep end before I know how to swim, because then there is a sense of urgency, and you learn faster.

Also, bzt, I dislike how you advertised your font renderer as a replacement, to fix a simple logic error. It's like buying a new car when your battery dies.

Re: troubles with bicubic upscaling bitmap fonts

Posted: Sat Nov 28, 2020 5:11 pm
by bloodline
rizxt wrote:
I prefer to dive into the deep end before I know how to swim, because then there is a sense of urgency, and you learn faster.
Or you drown.
Also, bzt, I dislike how you advertised your font renderer as a replacement, to fix a simple logic error. It's like buying a new car when your battery dies.
bzt was simply offering you solutions. It’s better to have options.

Re: troubles with bicubic upscaling bitmap fonts

Posted: Sat Nov 28, 2020 5:24 pm
by austanss
bloodline wrote:
rizxt wrote: I prefer to dive into the deep end before I know how to swim, because then there is a sense of urgency, and you learn faster.
Or you drown.
Fair point. Although I've never experienced any form of drowning or similar when deep-diving into new concepts.

Re: troubles with bicubic upscaling bitmap fonts

Posted: Sun Nov 29, 2020 12:43 pm
by bzt
rizxt wrote:I prefer to dive into the deep end before I know how to swim, because then there is a sense of urgency, and you learn faster.
OSDev requires lots of knowledge, both theory and experience, it is not something you can jump into hoping you won't sink and it's going to work somehow. It won't.
A friendly advice, make sure you have read required knowledge and beginner mistakes.
rizxt wrote:Also, bzt, I dislike how you advertised your font renderer as a replacement, to fix a simple logic error. It's like buying a new car when your battery dies.
I've tried to explain this to you several times: it is not just a simple logic error you have. I've recommended a new car because it's not only the battery that died, but also the gear and the engine is failing, and even the tank is leaking.

I've just recommended an easy solution that not only solves the one problem you see now, but also solves the problems that you don't see yet and that you haven't thought of. Nobody wanted to force anything on you.

Cheers,
bzt