假设我已经分配了一个二维数组int位图[WIDTH] [HEIGHT]。我希望让Linux系统绘制一行文本,对这个位图缓冲区说“Hello World。”#34;就像Linux在屏幕上打印普通文本一样,这样我就可以提取出位图模式文本并将其与我的其他位图图像结合使用。那么,你可以为此目的分享一个适用于Linux的C ++代码片段吗?非常感谢。
PS:我可以找到一些代码来完成这项工作,但它们是在Windows上,而不是在Linux上。 PS2:如果你碰巧没有代码,你可以教我如何编写代码,但是请期待很多关于Linux C ++编程的天真问题。
答案 0 :(得分:2)
您需要选择C或C ++图形库。 Qt和SDL是两个不错的选择。
=============================================== ==========================
如果你想要的只是绘制图像,另一个选择是libGD(以及前面提到的Qt和SDL)。
Jere就是一个例子:
https://cs.marlboro.edu/code/c/GD_example/GD_example.c
include <stdio.h>
#include <gd.h>
// Dimensions of image in pixels
#define IMAGE_WIDTH 300
#define IMAGE_HEIGHT 300
// The data to display is in a DATA_SIZE x DATA_SIZE array,
// which will have its (left,top)=(x,y) corner at
// (DATA_TOP, DATA_LEFT) pixels in from the (0,0)=(left,top) pixel.
#define DATA_SIZE 8
#define DATA_LEFT 30
#define DATA_TOP 30
// Position of some blue lines drawn in the image.
#define BORDER 10
#define LEFT BORDER
#define RIGHT IMAGE_WIDTH - BORDER
#define TOP BORDER
#define BOTTOM IMAGE_HEIGHT - BORDER
// See the bottom of this code for a discussion of some output possibilities.
char* filename = "GD_example.png";
// Some values that'll go into the image as shades of gray.
// Range is 0 to 255 (i.e. 8 bits, which is the standard range of intensity).
// To do this sort of thing with floating point data or other data,
// you'd first scale your numbers to be in this 0 to 255 range.
int data[DATA_SIZE][DATA_SIZE] ={
{ 2, 10, 10, 10, 10, 10, 10, 2},
{ 10, 20, 30, 40, 40, 30, 20, 10},
{ 10, 30, 100, 100, 100, 100, 30, 10},
{ 10, 40, 100, 200, 200, 100, 40, 10},
{ 10, 30, 100, 200, 200, 100, 30, 10},
{ 10, 20, 100, 100, 100, 100, 20, 10},
{ 10, 10, 20, 30, 40, 30, 10, 10},
{ 2, 10, 10, 10, 10, 10, 10, 2}
};
int main(){
FILE* outfile; // defined in stdio
gdImagePtr image; // a GD image object
int white, blue, gray[255]; // some GD colors
int i, x, y; // array subscripts
printf("=== GD example ===\n");
printf("Creating %i by %i image.\n", IMAGE_WIDTH, IMAGE_HEIGHT);
image = gdImageCreate(IMAGE_WIDTH, IMAGE_HEIGHT);
// Or image = gdImageCreateTrueColor(IMAGE_WIDTH, IMAGE_HEIGHT);
// followed by colors like white=gdTrueColor(255,255,255) that don't
// need to refer to any one image's color table.
white = gdImageColorAllocate(image, 255,255,255); // 1st is background
blue = gdImageColorAllocate(image, 0,0,255); // (red,green,blue)
for (i=0; i<255; i++){
gray[i] = gdImageColorAllocate(image, i,i,i);
}
printf("Drawing some blue lines.\n");
gdImageLine(image, LEFT,TOP, RIGHT,TOP, blue); // draw lines in image
gdImageLine(image, RIGHT,TOP, RIGHT,BOTTOM, blue); // +-----------------+
gdImageLine(image, RIGHT,BOTTOM, LEFT,BOTTOM, blue); // |0,0 WIDTH,0|
gdImageLine(image, LEFT,BOTTOM, LEFT,TOP, blue); // |0,HEIGHT |
// +-----------------+
printf("Filling in some gray pixels.\n");
for (x=0; x<DATA_SIZE; x++){ // fill some grayscale
for (y=0; y<DATA_SIZE; y++){ // colors from data.
gdImageSetPixel(image, x+DATA_LEFT, y+DATA_TOP, gray[data[x][y]]);
}
}
// Finally, write the image out to a file.
printf("Creating output file '%s'.\n", filename);
outfile = fopen(filename, "wb");
gdImagePng(image, outfile);
fclose(outfile);
}
答案 1 :(得分:0)
我后来使用OpenImageIO来完成此任务。 C ++代码如下:
#include <OpenImageIO/imageio.h>
#include <OpenImageIO/imagebuf.h>
#include <OpenImageIO/imagebufalgo.h>
OIIO_NAMESPACE_USING
const int WIDTH = 256, HEIGHT = 32, CHANNELS = 3;
unsigned char buffer[WIDTH * HEIGHT * CHANNELS];
int main() {
ImageBuf ib(ImageSpec(WIDTH, HEIGHT, CHANNELS), buffer);
ImageBufAlgo::render_text(ib, 0 /* x */, 10 /* y */,
"Hello, world!" /* text to draw */);
for (int j = 0; j < HEIGHT; j++)
for (int i = 0; i < WIDTH; i++)
cout << buffer[(i+j*WIDTH)*CHANNELS+0] << "," //R
<< buffer[(i+j*WIDTH)*CHANNELS+1] << "," //G
<< buffer[(i+j*WIDTH)*CHANNELS+2] << endl; //B
}
位图模式可以显示为以下屏幕截图(使用ib.write("output.tif");
):
感谢Larry Gritz帮助我。
PS:在ubuntu 14.04和g ++ 4.8.4上测试。使用的链接选项是-lboost_system -lOpenImageIO
。
通常,理解问题比理解问题更难 回答它。