C++ 位图中的 ttc 字体

ttc Font in Bitmap in C++

本文关键字:ttc 字体 位图 C++      更新时间:2023-10-16

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

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 文件的标头:

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

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

此外,

我在取出单个位时遇到问题。目前,我使用此循环来更改位图图像中的像素:

基本上,您在这里要做的是将像素从一个图像复制到另一个图像。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等内容(或可移植的。其中许多库将支持该样式的输入和输出图像,对特定像素使用不同的格式,并在需要时将一种格式转换为另一种格式。