0

Is it possible to create bitmap programmatically in C? I want to pass it some text to draw, e.g.

createBitmapWithContents("Hello");

or

createBitmapWithContents("Hello \n other line");

And it should create bitmap which has "Hello" drawn in it (or draw second text respectively). Also, the text "Hello" might be a Unicode string. Not necessarily English characters.

Preferably I would like to do this without using some third party libraries.

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

2

You'll need to do two different things :

  • Generate an image in memory that represents your string
  • Store that image into a file

Both can be done without external libraries (using simple predefined patterns of characters and storing as simple format such as BMP).

But note that it would be a lot easier to do this using a high-level image drawing library such as OpenCV or ImageMagick.

The first thing to do is to is to define a data structure to store your image, something like this:

struct Image {
    int height, width;
    unsigned char* pixels;
};

Then you'll have to generate the functions to allocate the image, free the image and maybe something to copy an image inside another one.

In order to print your character on your image, you would have to create predefined patterns like this:

char patternA[] = {
    0x00, 0x00, 0xff, 0x00, 0x00
    0x00, 0xff, 0x00, 0xff, 0x00
    0x00, 0xff, 0x00, 0xff, 0x00
    0x00, 0xff, 0xff, 0xff, 0x00
    0x00, 0xff, 0x00, 0xff, 0x00
    0x00, 0xff, 0x00, 0xff, 0x00
    0x00, 0xff, 0x00, 0xff, 0x00
};
Image imageOfA;
imageOfA.width = 5;
imageOfA.height= 7;
imageOfA.pixels= patternA;

You can also read those patterns from image files or even better, from a font file (but without external libraries, you'll need to implement the file readers yourself).

Once you have your patterns of characters, you can combine those predefined images to create a new image corresponding to your input string.

Finally, you'll have to write your image structure into a file. For that, you can either use a low-level library (such as libjpeg or libpng) or you can implement it yourself using a simple file format (such as BMP).

The conclusion is that you really want to use a third party library to achieve what you want.

zakinster
  • 10,508
  • 1
  • 41
  • 52
0

Did you try googling this?

There's quite a few things you could do, for example you can you can run loops to create your own matrix of pixels. check this link here

Nick
  • 2,877
  • 2
  • 33
  • 62