Containers::Array<unsigned char> TgaImageConverter::doExportToData(const ImageReference2D& image) const {
    #ifndef MAGNUM_TARGET_GLES
    if(image.format() != ImageFormat::BGR &&
       image.format() != ImageFormat::BGRA &&
       image.format() != ImageFormat::Red)
    #else
    if(image.format() != ImageFormat::RGB &&
       image.format() != ImageFormat::RGBA &&
       image.format() != ImageFormat::Red)
    #endif
    {
        Error() << "Trade::TgaImageConverter::convertToData(): unsupported image format" << image.format();
        return nullptr;
    }

    if(image.type() != ImageType::UnsignedByte) {
        Error() << "Trade::TgaImageConverter::convertToData(): unsupported image type" << image.type();
        return nullptr;
    }

    /* Initialize data buffer */
    const UnsignedByte pixelSize = image.pixelSize();
    auto data = Containers::Array<unsigned char>::zeroInitialized(sizeof(TgaHeader) + pixelSize*image.size().product());

    /* Fill header */
    auto header = reinterpret_cast<TgaHeader*>(data.begin());
    header->imageType = image.format() == ImageFormat::Red ? 3 : 2;
    header->bpp = pixelSize*8;
    header->width = Utility::Endianness::littleEndian(image.size().x());
    header->height = Utility::Endianness::littleEndian(image.size().y());

    /* Fill data */
    std::copy(image.data(), image.data()+pixelSize*image.size().product(), data.begin()+sizeof(TgaHeader));

    #ifdef MAGNUM_TARGET_GLES
    if(image->format() == ImageFormat::RGB) {
        auto pixels = reinterpret_cast<Math::Vector3<UnsignedByte>*>(data.begin()+sizeof(TgaHeader));
        std::transform(pixels, pixels + image.size().product(), pixels,
            [](Math::Vector3<UnsignedByte> pixel) { return swizzle<'b', 'g', 'r'>(pixel); });
    } else if(image.format() == ImageFormat::RGBA) {
        auto pixels = reinterpret_cast<Math::Vector4<UnsignedByte>*>(data.begin()+sizeof(TgaHeader));
        std::transform(pixels, pixels + image.size().product(), pixels,
            [](Math::Vector4<UnsignedByte> pixel) { return swizzle<'b', 'g', 'r', 'a'>(pixel); });
    }
    #endif

    return std::move(data);
}
Пример #2
0
void ImageDataTest::toReference() {
    unsigned char* data = new unsigned char[3];
    Trade::ImageData2D a(ColorFormat::Red, ColorType::UnsignedByte, {1, 3}, data);

    ImageReference2D b = a;
    CORRADE_COMPARE(b.format(), ColorFormat::Red);
    CORRADE_COMPARE(b.type(), ColorType::UnsignedByte);
    CORRADE_COMPARE(b.size(), Vector2i(1, 3));
    CORRADE_COMPARE(b.data(), data);
}
Пример #3
0
void ImageDataTest::toReference() {
    auto data = new unsigned char[3];
    const Trade::ImageData2D a(ColorFormat::Red, ColorType::UnsignedByte, {1, 3}, data);
    ImageReference2D b = a;

    CORRADE_COMPARE(b.format(), ColorFormat::Red);
    CORRADE_COMPARE(b.type(), ColorType::UnsignedByte);
    CORRADE_COMPARE(b.size(), Vector2i(1, 3));
    CORRADE_COMPARE(b.data(), data);

    CORRADE_VERIFY((std::is_convertible<const Trade::ImageData2D&, ImageReference2D>::value));
    {
        #ifdef CORRADE_GCC47_COMPATIBILITY
        CORRADE_EXPECT_FAIL("Rvalue references for *this are not supported in GCC < 4.8.1.");
        #endif
        CORRADE_VERIFY(!(std::is_convertible<const Trade::ImageData2D, ImageReference2D>::value));
        CORRADE_VERIFY(!(std::is_convertible<const Trade::ImageData2D&&, ImageReference2D>::value));
    }
}