このページでは

C

画像デコーダの統合

概要

Qt Quick Ultraliteが認識しないカスタム形式の画像アセットについては、画像デコーダが実装しやすいAPIを提供し、これらの画像をデコードできるようにします。Qt Quick Ultraliteエンジンは、画像形式に応じて適切なデコーダを自動的に選択します。

画像は、前処理を行わずに、あるいはプロジェクト内で宣言することなく、SDカードから読み込むことができます。アセットがFlash内に保存されているかSDカード上に保存されているかに関わらず、実装すべきAPIは1つだけです。

カスタム画像デコーダは、ユースケースに固有の形式でアセットを保持する場合に役立ちます。デコード処理は実装者が完全に制御でき、デバイスで利用可能なハードウェアアクセラレーションを自由に活用できます。

任意の画像形式を読み込むには、初期化フェーズで画像デコーダの実装をアプリケーションに登録する必要があります。リソースまたはディスク(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ライセンスの下で利用可能です。
詳細はこちら。