本页内容

C

图像解码器集成

概述

对于采用自定义格式且Qt Quick Ultralite无法识别的图像资源,图像解码器提供了一个易于实现的API,用于解码此类图像。Qt Quick Ultralite引擎会根据图像格式自动选择合适的解码器。

图像可直接从 SD 卡加载,无需预处理,甚至无需在项目中进行声明。无论资源存储在 Flash 还是 SD 卡中,只需实现一个 API 即可。

当需要将资源保留为特定于您使用场景的格式时,自定义图像解码器便派上用场。解码过程完全由实现者控制,可充分利用设备上可用的任何硬件加速功能。

要加载任意图像格式,必须在初始化阶段向应用程序注册图像解码器实现。每当需要显示来自资源库或磁盘(file://)的图像时Qt Quick ,Ultralite 会检查该图像是否为内置资产格式。 如果不支持,则会按注册顺序依次使用所有已注册的图像解码器来解码该图像。系统将采用第一个报告能够解码该图像的解码器。如果资源中包含可解码的图像,则必须在调用 Qul::initPlatform() 之前完成解码器的注册。

为Qt Quick Ultralite实现图像解码器

自定义图像解码器必须实现 `Qul::PlatformInterface::ImageDecoder ` 类。有关支持硬件加速的 JPEG 解码器的示例实现,请参见 `imagedecoder` 示例。

以下代码展示了该类的示例实现:

class MyDecoder : public Qul::PlatformInterface::ImageDecoder
{
public:
    bool imageInformation(RequestDataCallback &callback,
                          int16_t *width,
                          int16_t *height,
                          Qul::PixelFormat *actualPixelFormat,
                          Qul::PixelFormat optimalOpaquePixelFormat,
                          Qul::PixelFormat optimalAlphaPixelFormat);
    int decodeImage(RequestDataCallback &callback,
                    unsigned char *outbuffer,
                    uint32_t outbufferSize,
                    Qul::PixelFormat pixelFormat,
                    uint32_t requiredBytesPerLine);
}
bool MyDecoder::imageInformation(RequestDataCallback &callback,
                                 int16_t *width,
                                 int16_t *height,
                                 Qul::PixelFormat *actualPixelFormat,
                                 Qul::PixelFormat optimalOpaquePixelFormat,
                                 Qul::PixelFormat optimalAlphaPixelFormat)
{
    unsigned char buffer[1024];
    uint32_t offset 0; // Start from beginning of the file

    callback.read(buffer, offset, 8)

    // Check if this decoder is able to decode the image
    if (buffer[0] != 0x89 || buffer[1] != 0x50 … ) { // PNG format header: 89 50 4E 47 0D 0A 1A 0A
        return false; // Return early when the format is unknown
    }

    // Read more data from the image to determine the size and pixel format.
    callback.read(buffer, offset, sizeof(buffer))
    *width = getImageWidth(buffer);
    *height = getImageHeight(buffer);
    *actualPixelFormat = getOptimalImageFormat(buffer, optimalOpaquePixelFormat, optimalAlphaPixelFormat);

    return true;
}
int MyDecoder::decodeImage(RequestDataCallback &callback,
                           unsigned char *outbuffer,
                           uint32_t outbufferSize,
                           Qul::PixelFormat pixelFormat,
                           uint32_t requiredBytesPerLine)
{
    unsigned char *allocatedBuffer = NULL;

    allocatedBuffer = callback.rawData();
    if (!allocatedBuffer) {
        // The data is not in addressable memory and might need to be fetched
        uint32_t bufferSize = callback.totalAvailableDataSize();
        allocatedBuffer = (unsigned char *) Qul::Platform::qul_malloc(bufferSize);
        callback.readData(allocatedBuffer, 0, bufferSize);
    }

    hardware_decode(allocatedBuffer, bufferSize, outbuffer, outbufferSize);
    Qul::Platform::qul_free(allocatedBuffer);

    return 0;
}

为了让Qt Quick Ultralite识别已实现的图像解码器,必须将其注册到应用程序中。

static MyDecoder decoder;
Qul::Application::addImageDecoder(&decoder);

在某些 Qt 许可证下可用。
了解更多。