2016-02-16 44 views
0

我试图将本帖(How do I convert a vec4 rgba value to a float?)中显示的代码移植到Qt(C++)。我粘贴了下面的整个代码。我的结果不完全确切。任何关于我在做什么的想法都是错误的?Qt(C++)在32位浮点数和QRgba之间转换

结果:

Input: -10.01 
Sign: 1 F: 10 Sign: 1 Exponent: 130 Mantissa: 1.25 
Red: 193 Green: 32 Blue: 0 Alpha: 0 
Result: -10.0151 

Input: -1 
Sign: 1 F: 1 Sign: 1 Exponent: 127 Mantissa: 1 
Red: 191 Green: 128 Blue: 0 Alpha: 0 
Result: -1.00195 

Input: 5.3 
Sign: 0 F: 5 Sign: 0 Exponent: 129 Mantissa: 1.25 
Red: 64 Green: 160 Blue: 0 Alpha: 0 
Result: 5.0116 

代码:

#include <QCoreApplication> 
#include <math.h> 
#include <QColor> 
#include <QDebug> 

float step(float edge, float x) { 
    if (x < edge) { 
     return 0.0; 
    } 
    else { 
     return 1.0; 
    } 
} 

QColor encode32(float f) { 

    //float e = 5.0; 
    qDebug() << "Input: " << f; 
    float F = abs(f); 
    float Sign = step(0.0,-f); 
    float Exponent = floor(log2(F)); 
    float Mantissa = F/exp2(Exponent); 
    if(Mantissa < 1) 
     Exponent -= 1; 
    Exponent += 127; 
    qDebug() << "Sign: " << Sign << " F: " << F << " Sign: " << Sign << " Exponent: " << Exponent << " Mantissa: " << Mantissa; 
    float red = 128.0 * Sign + floor(Exponent*exp2(-1.0)); 
    float green = 128.0 * fmod(Exponent,2.0) + fmod(floor(Mantissa*128.0),128.0); 
    float blue = floor(fmod(floor(Mantissa*exp2(23.0 -8.0)),exp2(8.0))); 
    float alpha = floor(exp2(23.0)*fmod(Mantissa,exp2(-15.0))); 
    qDebug() << "Red: " << red << " Green: " << green << " Blue: " << blue << " Alpha: " << alpha; 
    QColor rgba; 
    rgba.setRed(red); 
    rgba.setGreen(green); 
    rgba.setBlue(blue); 
    rgba.setAlpha(alpha); 
    return rgba; 

} 

float decode32 (QColor rgba) { 

    float red = rgba.redF() * 255.0; 
    float green = rgba.greenF() * 255.0; 
    float blue = rgba.blackF() * 255.0; 
    float alpha = rgba.alphaF() * 255.0; 

    float Sign = 1.0 - step(128.0,red)*2.0; 
    float Exponent = 2.0 * fmod(red,128.0) + step(128.0,green) - 127.0; 
    float Mantissa = fmod(green,128.0)*65536.0 + blue*256.0 + alpha + float(0x800000); 
    float Result = Sign * exp2(Exponent-23.0f) * Mantissa; 
    return Result; 
} 

int main(int argc, char *argv[]) 
{ 

    QCoreApplication a(argc, argv); 


    { 
     float result; 
     QColor rgba = encode32(-10.01); 
     result = decode32(rgba); 
     qDebug() << "Result: " << result << "\n"; 
    } 

    { 
     float result; 
     QColor rgba = encode32(-1.0); 
     result = decode32(rgba); 
     qDebug() << "Result: " << result << "\n"; 
    } 

    { 
     float result; 
     QColor rgba = encode32(5.3); 
     result = decode32(rgba); 
     qDebug() << "Result: " << result << "\n"; 
    } 

    return a.exec(); 

} 

回答

1

这是不可能的,至少不与方法。浮点数据有32位数据,但它必须表示NaN和无穷大等。所以,你实际上只有不到32位来存储你的rgba值,但它需要一个完整的32位(红,绿,蓝,阿尔法各8位)。这是不可能完成的,除非你将内存转换成一个破坏目的的int。

相关问题