104 posts
is this a good way to invert bitmap?
Edited by C_Worm on
I used the stbtt_truetype.h to render a bitmapfont.

However, like stb_image.h the bitmaps loaded are upside down.

In stb_image there's this function stbi_set_flip_vertically_on_load(true); which inverts the bitmap automatically.

I didn't find a corresponding function for stb_truetype so i came up with this little algorithm, but is this a good way to go or
is there a better way?

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33  bool success = 0; const u32 pxlw = 2048; const u32 pxlh = 2048; const u32 fontf_sz = pxlw*pxlh; unsigned char font_file[fontf_sz] = {}; unsigned char pixels[fontf_sz] = {}; stbtt_packedchar packed_char[124] = {}; stbtt_pack_context pack_c = {}; size_t bytes_read = 0; bytes_read = fread(font_file, 1, fontf_sz, fopen("C:/windows/fonts/arial.ttf", "rb")); // init pixel_bitmap success = stbtt_PackBegin(&pack_c, pixels, pxlw, pxlh, 0, 1, 0); // set detail level of characters in font which requires the pixel_bitmap to be bigger stbtt_PackSetOversampling(&pack_c, 8, 8); // Set what characters in the pixel_map you want STBTT_POINT_SIZE makes the charcter size in pixels stbtt_PackFontRange(&pack_c, font_file, 0, 32, 0, 124, packed_char); for(i32 y = 0; y < pxlh / 2; y++) { for(i32 x = 0; x < pxlw; x++) { i32 temp = pixels[x + (y * pxlw)]; pixels[x + (y * pxlw)] = pixels[x + ((pxlh - 1) * pxlw) - (y * pxlw)]; pixels[x + ((pxlh - 1) * pxlw) - (y * pxlw)] = temp; } } stbtt_PackEnd(&pack_c); 
Simon Anciaux
1274 posts
is this a good way to invert bitmap?
As far as I know stb_truetype doesn't provide a way to flip the atlas vertically. I have a similar thing in my code to invert the atlas.

Do you have a particular reason for setting the oversampling to 8 and 8 ? It seems overkill and I don't think it will improve the quality (it'll probably decrease it depending on how you render, but I haven't tested this in a long time) and it will take 64 times (8*8) the memory of a non over-sampled font. In my code I use 2 and 2 which seems fine.
104 posts
is this a good way to invert bitmap?
Okay, then i guess it won't be a "problem" to use this invert-algortihm.

This is my first time trying out this way of loading a font bitmap with stb_truetype, so im only playing around with the values,
hence the oversampling with (8, 8).

But i guess im just going to use (2, 2) or something, like u mentioned :)