跳转到内容

display_lib#

display_lib 库提供了在简单窗口中显示图像数据的函数。该库不能替代 GUI 工具包,仅用于无 GUI 应用程序的调试目的。特别是,该库未实现用于 Scaling 或转换图像的功能,且局限于显示不超过屏幕大小的图像数据。

要使用该库,应将包含文件 SisoDisplay.h 添加到源代码中。

#include <SisoDisplay.h>

此外, display_lib.lib 应添加到您的 Microsoft Visual Studio 项目中,或者 libdisplay_lib.so 添加到您的 Linux 项目中。如果您使用 CMake,包名称为 SisoDisplayLib,库存储在变量中 ${SISODISPLAYLIB_LIBRARIES} 中,包含目录存储在变量 ${SISODISPLAYLIB_INCLUDE_DIR}。请参阅 前提条件 了解有关项目以及如何使用 CMake 的更多详细信息。

创建显示窗口#

int CreateDisplay(
    unsigned int bitsPerPixel,
    unsigned int width,
    unsigned int height);

void CloseDisplay(
    int id);

void SetBufferWidth(
    int id,
    unsigned int width,
    unsigned int height);

要创建一个显示窗口,函数 CreateDisplay() 可以被调用。该函数在参数中期望每像素位数和窗口尺寸。如果调用成功,它将返回一个大于或等于零的值,该值是显示窗口的标识符。如果发生错误,则返回负值。

如果不再需要显示窗口,函数 CloseDisplay() 应当被调用。

显示窗口的图像缓冲区尺寸初始设置为与窗口相同的宽度和高度。如果要显示的图像非常大,可以将窗口尺寸设置为较小的尺寸,并调用函数 SetBufferWidth() 来为图像数据设置缓冲区大小。

信息

显示库仅限于处理最大为 2 GB 的图像。

以下示例展示了如何创建一个与 Applet 的图像尺寸和 Pixel Format 相匹配的显示窗口:

// get image dimensions and pixel format from applet
int result = FG_INVALID_PARAMETER;
int width = 0, height = 0, fgFormat = 0;
int widthId = Fg_getParameterIdByName(fg, "FG_WIDTH");
int heightId = Fg_getParameterIdByName(fg, "FG_HEIGHT");
int fgFormatId = Fg_getParameterIdByName(fg, "FG_FORMAT");
if (widthId > 0 && heightId > 0 && fgFormatId > 0) {
    result = Fg_getParameterWithType(fg, widthId, &width, dma);
    if (result == FG_OK) {
        result = Fg_getParameterWithType(fg, heightId, &height, dma);
    }
    if (result == FG_OK) {
        result = Fg_getParameterWithType(fg, fgFormatId, &fgFormat, dma);
    }
}

// get number of bits per pixel for the format
int bitsPerPixel = FG_INVALID_VALUE;
if (result == FG_OK) {
    bitsPerPixel = Fg_getBitsPerPixel(fgFormat);
}
if (bitsPerPixel <= 0) {
    result = FG_INVALID_VALUE;
}

// create a display
int display = FG_INVALID_HANDLE;
if (result == FG_OK) {
    display = CreateDisplay(bitsPerPixel, width, height);
}
if (display >= 0) {
    // use display ...

    CloseDisplay(display);
}

绘制到显示窗口#

void DrawBuffer(
    int id,
    const void * buffer,
    int frameNumber,
    const char * title);

要在显示窗口中绘制图像数据,可以调用 DrawBuffer() 函数。该函数期望在第一个参数中传入显示窗口 ID,在第二个参数中传入包含图像数据的缓冲区指针。其余两个参数可用于显示帧号以及显示窗口标题中的某些文本。

以下示例展示了如何在简单的采集循环中显示图像数据:

frameindex_t nextFrame = 1;
while (true) {
    // wait for new images
    frameindex_t newestFrame =
        Fg_getLastPicNumberBlockingEx(fg, nextFrame, dma,
                                      timeoutInSeconds, mem);
    if (newestFrame > 0) {
        // get pointer to current frame buffer
        void * ptr = Fg_getImagePtrEx(fg, newestFrame, dma, mem);

        // draw current frame buffer
        DrawBuffer(display, ptr, newestFrame & 0x7fffffff,
                   "A Sample Image Display");

        // calculate next frame
        nextFrame = (newestFrame < FRAMEINDEX_MAX) ? newestFrame + 1 : 1;
    } else {
        // handle error ...
    }
}