implemented conversions between internal pixel formats

This commit is contained in:
itsmattkc
2019-08-12 18:24:52 +10:00
parent eca8d98497
commit 3eee9893f8
6 changed files with 236 additions and 1 deletions
+181
View File
@@ -21,9 +21,13 @@
#include "pixelservice.h"
#include <QCoreApplication>
#include <QDebug>
#include <QFloat16>
const int kRGBAChannels = 4;
PixelService olive::pixel_service;
PixelService::PixelService()
{
}
@@ -87,3 +91,180 @@ int PixelService::BytesPerChannel(const olive::PixelFormat &format)
qFatal("Invalid pixel format requested");
}
}
FramePtr PixelService::ConvertPixelFormat(FramePtr frame, const olive::PixelFormat &dest_format)
{
if (frame->format() == dest_format) {
return frame;
}
FramePtr converted = std::make_shared<Frame>();
// Copy parameters
converted->set_width(frame->width());
converted->set_height(frame->height());
converted->set_timestamp(frame->timestamp());
converted->set_format(dest_format);
converted->allocate();
int pix_count = frame->width() * frame->height() * kRGBAChannels;
bool valid = true;
switch (static_cast<olive::PixelFormat>(frame->format())) {
case olive::PIX_FMT_RGBA8:
{
uint8_t* source = frame->data();
switch (dest_format) {
case olive::PIX_FMT_RGBA16: // 8-bit Integer -> 16-bit Integer
{
uint16_t* destination = reinterpret_cast<uint16_t*>(converted->data());
for (int i=0;i<pix_count;i++) {
destination[i] = static_cast<uint16_t>(source[i] * 257);
}
break;
}
case olive::PIX_FMT_RGBA16F: // 8-bit Integer -> 16-bit Float
{
qfloat16* destination = reinterpret_cast<qfloat16*>(converted->data());
for (int i=0;i<pix_count;i++) {
destination[i] = source[i] / 255.0f;
}
break;
}
case olive::PIX_FMT_RGBA32F: // 8-bit Integer -> 32-bit Float
{
float* destination = reinterpret_cast<float*>(converted->data());
for (int i=0;i<pix_count;i++) {
destination[i] = source[i] / 255.0f;
}
break;
}
case olive::PIX_FMT_INVALID:
case olive::PIX_FMT_RGBA8:
case olive::PIX_FMT_COUNT:
valid = false;
}
break;
}
case olive::PIX_FMT_RGBA16:
{
uint16_t* source = reinterpret_cast<uint16_t*>(frame->data());
switch (dest_format) {
case olive::PIX_FMT_RGBA8: // 16-bit Integer -> 8-bit Integer
{
uint8_t* destination = converted->data();
for (int i=0;i<pix_count;i++) {
destination[i] = static_cast<uint8_t>(source[i] / 257);
}
break;
}
case olive::PIX_FMT_RGBA16F: // 16-bit Integer -> 16-bit Integer
{
qfloat16* destination = reinterpret_cast<qfloat16*>(converted->data());
for (int i=0;i<pix_count;i++) {
destination[i] = source[i] / 65535.0f;
}
break;
}
case olive::PIX_FMT_RGBA32F: // 16-bit Integer -> 16-bit Integer
{
float* destination = reinterpret_cast<float*>(converted->data());
for (int i=0;i<pix_count;i++) {
destination[i] = source[i] / 65535.0f;
}
break;
}
case olive::PIX_FMT_INVALID:
case olive::PIX_FMT_RGBA16:
case olive::PIX_FMT_COUNT:
valid = false;
}
break;
}
case olive::PIX_FMT_RGBA16F:
{
qfloat16* source = reinterpret_cast<qfloat16*>(frame->data());
switch (dest_format) {
case olive::PIX_FMT_RGBA8: // 16-bit Float -> 8-bit Integer
{
uint8_t* destination = converted->data();
for (int i=0;i<pix_count;i++) {
destination[i] = static_cast<uint8_t>(source[i] * 255.0f);
}
break;
}
case olive::PIX_FMT_RGBA16: // 16-bit Float -> 16-bit Integer
{
uint16_t* destination = reinterpret_cast<uint16_t*>(converted->data());
for (int i=0;i<pix_count;i++) {
destination[i] = static_cast<uint16_t>(source[i] * 65535.0f);
}
break;
}
case olive::PIX_FMT_RGBA32F: // 16-bit Float -> 32-bit Float
{
float* destination = reinterpret_cast<float*>(converted->data());
for (int i=0;i<pix_count;i++) {
destination[i] = source[i];
}
break;
}
case olive::PIX_FMT_INVALID:
case olive::PIX_FMT_RGBA16F:
case olive::PIX_FMT_COUNT:
valid = false;
}
break;
}
case olive::PIX_FMT_RGBA32F:
{
float* source = reinterpret_cast<float*>(frame->data());
switch (dest_format) {
case olive::PIX_FMT_RGBA8: // 32-bit Float -> 8-bit Integer
{
uint8_t* destination = converted->data();
for (int i=0;i<pix_count;i++) {
destination[i] = static_cast<uint8_t>(source[i] * 255.0f);
}
break;
}
case olive::PIX_FMT_RGBA16: // 32-bit Float -> 16-bit Integer
{
uint16_t* destination = reinterpret_cast<uint16_t*>(converted->data());
for (int i=0;i<pix_count;i++) {
destination[i] = static_cast<uint16_t>(source[i] * 255.0f);
}
break;
}
case olive::PIX_FMT_RGBA16F: // 32-bit Float -> 16-bit Float
{
qfloat16* destination = reinterpret_cast<qfloat16*>(converted->data());
for (int i=0;i<pix_count;i++) {
destination[i] = source[i];
}
break;
}
case olive::PIX_FMT_INVALID:
case olive::PIX_FMT_RGBA32F:
case olive::PIX_FMT_COUNT:
valid = false;
}
break;
}
case olive::PIX_FMT_INVALID:
case olive::PIX_FMT_COUNT:
valid = false;
}
if (valid) {
return converted;
}
qWarning() << tr("Invalid parameters called for pixel format conversion");
return nullptr;
}