TrueType to C Array - c

I am writing a low level C app, and I'm planning on using an array to store my fonts.
The problem is, the font I'd like to use is in TrueType format. How shall I:
Convert TTF to a large sized, B&W, bitmap
font without any kind of AA (not strictly programming
related);
Parse the B&W bitmap font into a C byte array.
What format should I use for the bitmap? Should be simple enough that me, a beginner programmer with little over a year of experience can write a parser to store it in said array.
I don't want to use external libraries, and I'd like to keep C Std. Lib. usage to a minimum. It's for a college project and I want to write everything myself.

It's not the most professional or cleanest, but here's what I'd do in your situation:
Choose a monospaced font and a size where each character is an integral number of pixels.
Open GIMP (or your favorite image editing program) and make an image that's font_width pixels wide and font_height*96 pixels tall.
Make a text element anchored at the upper-left corner containing <space> <newline> ! <newline> " <newline> # <newline> ... (i.e. all the ASCII glyphs).
Save it as an uncompressed image format that's easy to process, like PNM.
Load it into an array of type uint8_t [96][font_height][font_width].

Use Win32 GDI APIs to write bitmaps: create a bitmap, print a letter, use GetPixel to read it. Serialize to .c file.

Try Freetype. It can provide you with data you can use for your bitmap creation.

Related

How to embed a texture for use with OpenGL into a C array?

I don't want the error texture of my game engine to rely on an external file, because that file might be deleted, which defeats the purpose of having an error texture. How can I convert an image file into a C-sytle char array that I can read using glTexImage2D?
First, convert your source image file to the 'raw' format (while "remembering" the dimensions of your image in a pair of variables). For this you can use the ImageMagick's convert tool.
The sample for greyscale images conversion is given by this answer. For RGB images either write your own image-to-binary-file converter (since you probably know how to load RGB images for other textures, or use the STB or similar library to load it and then save the image data to a binary file), or (if no automation of this conversion step is needed) just use some GUI tool to save the RAW image.
Then use a tool named bin2h (binary file to C header) which takes a file as an input (your 'raw' file) and outputs a C header with a byte array.
One of such tools can be found at Bin2H#github
Then use the declared array (and a pair of variables with texture dimensions) as an argument to glTexImage2D.
If you have no aversions against 'gimp', gimp has a .h or .c exporter. In that way you have direct access to the dimensions and buffer of your image. Although it is not compressed, so be aware if you want to use a large Image.

Unicode with MAX7219

I'm trying to implement Asian symbols with my max7219 and using the 8x8 led displays.
I've had a look online and I've found libraries for the max7219 but it is only in ASCII. I was wondering if there was an easy way of implementing using a UNICODE library - assuming there is one.
I'd like to easily copy and paste say " な " this character into my code and print it onto the LED displays. So far, all attempts have not been working. My other option is to use binary/hex to manually draw up the symbols but I would really prefer to make it easy for the user to copy and paste any character and it prints onto the LEDs. Or will I have to create my own Arduino Library?
Any help is greatly appreciated!
Many thanks.
The problem with Unicode is that it's just so damn big (the first kana is U+3041), and most Arduinos have not nearly enough flash to store all the characters required.
My recommendation is to use an 8-bit encoding that maps to all the characters you need. I suggest starting with the character set used by the HD44780UA00 and replacing the characters where they make sense. Since some other libraries already use this set it won't be a huge leap to use them with your display.
You can't copy and paste a CHARACTER in a 8x8 matrix.
You have to find an 8x8 matrix of your font (katakana, kanji, etc.), and build an array that contains all the characters in a bit-by-bit format.
To all who have commented that MAX7219 is a 7-segment display, I want to say that MAX7219 is an IC which contains a buffer of 8x8 bit. You can use it to drive an 8x8 LED matrix, or an 8-in-line 7-segments (plus a decimal point) display, or anything else.

C - drawing in a bitmap

I have to calculate the flight path of a projectile and draw the result in a bitmap file. So far I'm pretty clueless how to do that.
Would it be a good idea to safe the values of the flight path in a struct and transfer it to the bitmap file?
Do you have any other suggestions how it could be done in a better way?
The simplest way to produce an image file without much hassle with only standard C library tools is most likely writing a bmp file. For start, check the Wikipedia article on this file format, it gives a quite complete description of it.
If you don't want to go too deep in that, save for example a 640x480 or so empty 24 bit ("truecolor") .bmp image, and rip out it's header for your use. Depending on the program you use to save your image, you might end up with varying header size, however since the data is not compressed, it is fairly easy isolate the header. For a 640x480 image the data will be exactly 921600 bytes long, anything preceding it is the header.
From here the colors are (usually) in RGB order, bottom to top row, left to right. Experimenting a little should give you the proper results.
If you only have the standard C libraries to work with, it is unlikely there is anything much simpler to implement. Of course this case you will have to write a pixel matrix (so no much assistance for solving the actual problem you want to image), but that's true for any image format (maybe except if you rather aim for creating an SVG for a twist, it is neither too hard, just XML).

How to resize an image using libpng?

I am looking for an advice how to resize an image using libpng in C. I have already written a function, which makes png image from the structure. I want to make my image bigger: for example 1 pixel is extended to 9 pixels (3x3). Is there any function which would be able to do that?
No, libpng is an I/O library that deals with reading and writing the PNG image format.
Resizing an image has nothing to do with any on-disk file format, it's a pure image operation that generally happens in-memory.
You should look at some general-purpose image-processing library, or just do it yourself. The particular resize you mention is easy enough, just read a single pixel and write it 9 times to the output. Then use libpng's functions to save the new image.

How to convert an image to WORD (uint16) array?

I have some images (.bmp, .png, .jpg) in my directory. I want to convert this image to WORD array in order to display this image in LCD in 565 formats (16 bit pixel). How to convert an image to WORD array? Please help. Is is there any utility to just convert the image to binary? or Please provide some code in Windows C/CPP to convert it to binary?
This will depend a whole lot on the exact format of the input image.
Just converting "to words" isn't really expressing what you want to do, which is probably more like "convert bitmap images to an array of RGB565 pixels in row-major format".
You should look at image-processing libraries that allow you to load bitmap images, and read out the value of each pixel.
You can probably just convert directly to RGB565, shouldn't be too hard from any other bitmap format.
Note that there are both indexed and "true color" bitmap formats, and you sound like you need to handle both. If you'er lucky, the library for each format will abstract this away and have e.g. auint32 read_pixel_as_rgb888() function.
Also note that many bitmap image formats focus a lot on compression, which is why just reading in the bits of the file is not nearly enough, you need to de-compress the data according to the format. This is quite complex, which is why pre-written libraries are the only sane choice.
For PNG, look at libpng, for JPEG look for libjpeg. On second thought, these libraries might be a bit too low-level, and maybe you should look at something like SDL_Image instead.
You could try to use CImg to open image files - http://cimg.sourceforge.net/

Resources