简体   繁体   中英

ttc Font in Bitmap in C++

Is there an easy way to insert a font into a bitmap image? Currently I use the following class to edit my bitmap:

class BitmapImg{
public:
 BitmapImg();
 ~BitmapImg();
 void setPixel(int x, int y, int redn, int greenn, int bluen);
 void getPixel(int x, int y, int& redn, int& greenn, int& bluen);
private:
 unsigned short int red[1080][1080]; //1080X1080 Pixels
 unsigned short int green[1080][1080];
 unsigned short int blue[1080][1080];
};

But now I have started to import letters via xbm-file into XCode, which then use various loops to change my RGB values in the array. But this solution is very complicated.

I also have difficulty getting the single pixel-bits from the image. Currently I use this loop to change my pixels in the bitmap image:

BitmapImg Picture;
// ctor makes is completely blue -> no problem with the whit color below
int counter = 0;
for (int y=0;y<=199;y++)
{
    for (int x = 0; x<=199 ;x++)
    {
        for (int n = 0; n<16;n++)
        {
            bool bit =(A_bits[counter]>>n) & 1U;
                       
            if(bit)
              Picture.setPixel(counter%200,counter%200,255,255,255);
            counter ++;
            std::cout << counter<< std::endl; //for debugging
        }
    }
}

Header of xvm-File:

 #define A_width 200
 #define A_height 200
 static unsigned short A_bits[] = { 0x0000, 0x0000,....} 

The xbm-file describes an "A" but I only get a one pixel-wide line diagonally from the top left.

Furthermore I have problems to get the single bits out. Currently I use this loop to change my pixels in the bitmap image:

Basically what you are trying to do here is copy the pixels from one image to another. XBM is just a very basic mono format, so just a question of setting the pixel to the desired colour (foreground) or leaving it as is (background).

This is very nearly what you have. Note that this draws it in the top-left (0,0) of the image, and assumes the destination image is large enough. You should add bounds checking to safely clip the draw.

void draw_A(BitmapImg &picture, unsigned short r, unsigned short g, unsigned short b)
{
    for (int y = 0; y < A_height; ++y)
    {
        for (int x = 0; x < A_width; ++x)
        {
            unsigned bytes_per_row = (A_width + 7) / 8; // divide rounding up
            unsigned row_byte = x / 8; // 8 bits per byte
            unsigned row_bit = x % 8;
            unsigned char src_byte = A_bits[y * bytes_per_row + row_byte]; // row by row, left to right, top to bottom
            bool src_bit = ((src_byte >> row_bit) & 0x1) != 0; // least signifcant bit (1) is first
            if (src_bit)
            {
                picture.setPixel(x, y, r, g, b);
            }
        }
    }
}

 unsigned short int red[1080][1080]; //1080X1080 Pixels unsigned short int green[1080][1080]; unsigned short int blue[1080][1080];

Note that this is a pretty uncommon way to store image data, normally each pixel is kept together in a single array, also 2D arrays don't work with dynamic sizing (you can't do p = new unsigned short[width][height]; p[50][40] = 10; ).

For example 8bpp 24bit RGB might be stored as unsigned char pixels[width * height * 3]; pixels[50 * 3 + 40 * width * 3 + 1] = 10; /*set green at (50,40)*/ unsigned char pixels[width * height * 3]; pixels[50 * 3 + 40 * width * 3 + 1] = 10; /*set green at (50,40)*/ unsigned char pixels[width * height * 3]; pixels[50 * 3 + 40 * width * 3 + 1] = 10; /*set green at (50,40)*/ . You will see this when dealing with image data in many libraries and file formats. Although be aware some image formats especially start from the bottom, not the top.

But now I have started to import letters via xbm-file into XCode, which then use various loops to change my RGB values in the array. But this solution is very complicated.

Do many things directly with images however does tend to get pretty complicated, especially once start considering transparency/blending, transforms like scaling, rotating, all the different image formats to deal with (indexed colour, 16,24,32,etc. bit integer pixels, formats that are bottom to top rather than top to bottom, etc.).

There are many graphics libraries around, both hardware accelerated and software, platform specific (eg all the Windows GDI, DirectWrite, Direct2D, Direct3D, etc. stuff) or portable. Many of these libraries will support input and output images in that style, using different formats for the specific pixels, and plenty of solutions around to convert one format to another if needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM