[SOLVED] troubles with near-neighbor upscaling bitmap fonts

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

[SOLVED] troubles with near-neighbor upscaling bitmap fonts

Post 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 2275 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...
Last edited by austanss on Sat Nov 28, 2020 8:53 am, edited 1 time in total.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: troubles with bicubic upscaling bitmap fonts

Post 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?
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: troubles with bicubic upscaling bitmap fonts

Post 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
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: troubles with bicubic upscaling bitmap fonts

Post 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!
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: troubles with bicubic upscaling bitmap fonts

Post 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.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: troubles with bicubic upscaling bitmap fonts

Post 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
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

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

Post 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: .
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: troubles with bicubic upscaling bitmap fonts

Post 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.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
bloodline
Member
Member
Posts: 264
Joined: Tue Sep 15, 2020 8:07 am
Location: London, UK

Re: troubles with bicubic upscaling bitmap fonts

Post 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.
CuriOS: A single address space GUI based operating system built upon a fairly pure Microkernel/Nanokernel. Download latest bootable x86 Disk Image: https://github.com/h5n1xp/CuriOS/blob/main/disk.img.zip
Discord:https://discord.gg/zn2vV2Su
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

Re: troubles with bicubic upscaling bitmap fonts

Post 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.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: troubles with bicubic upscaling bitmap fonts

Post 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
Post Reply