首页 > 解决方案 > C++位图中的ttc字体

问题描述

有没有一种简单的方法可以将字体插入位图图像?目前我使用以下类来编辑我的位图:

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];
};

但现在我已经开始通过 xbm 文件将字母导入 XCode,然后使用各种循环来更改数组中的 RGB 值。但是这个解决方案非常复杂。

我也很难从图像中获取单个像素位。目前我使用这个循环来改变我在位图图像中的像素:

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
        }
    }
}

xvm-File 的头文件:

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

xbm 文件描述了一个“A”,但我只从左上角对角线得到一个像素宽的线。

标签: c++fontsbitmap

解决方案


此外,我很难将单个位取出。目前我使用这个循环来改变我在位图图像中的像素:

基本上你在这里尝试做的是将像素从一个图像复制到另一个图像。XBM 只是一种非常基本的单声道格式,因此只是将像素设置为所需颜色(前景)或保持原样(背景)的问题。

这几乎是你所拥有的。请注意,这会将其绘制在图像的左上角 (0,0),并假设目标图像足够大。您应该添加边界检查以安全地剪辑平局。

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];

请注意,这是一种非常不常见的存储图像数据的方式,通常每个像素都保存在一个数组中,而且 2D 数组不适用于动态调整大小(你不能这样做p = new unsigned short[width][height]; p[50][40] = 10;)。

例如 8bpp 24 位 RGB 可能存储为unsigned char pixels[width * height * 3]; pixels[50 * 3 + 40 * width * 3 + 1] = 10; /*set green at (50,40)*/. 在处理许多库和文件格式的图像数据时,您会看到这一点。尽管请注意某些图像格式尤其是从底部开始,而不是从顶部开始。

但现在我已经开始通过 xbm 文件将字母导入 XCode,然后使用各种循环来更改数组中的 RGB 值。但是这个解决方案非常复杂。

直接用图像做很多事情往往会变得相当复杂,特别是一旦开始考虑透明度/混合,缩放,旋转等转换,所有不同的图像格式(索引颜色,16,24,32等位整数)像素,从下到上而不是从上到下的格式等)。

周围有许多图形库,包括硬件加速和软件、平台特定(例如所有 Windows GDI、DirectWrite、Direct2D、Direct3D 等)或可移植的。这些库中的许多将支持该样式的输入和输出图像,对特定像素使用不同的格式,如果需要,还有大量的解决方案可以将一种格式转换为另一种格式。


推荐阅读