Qt下的RGB24与QImage等的对等转换

8 篇文章 0 订阅
本文汇总了关于Qt环境下RGB24像素格式与QImage对象之间的转换方法,涉及关键步骤和实用技巧,帮助开发者解决相关问题。
摘要由CSDN通过智能技术生成

         收藏几个有用的网站内容,是关于RGB24的问题

unsigned int convertYUVtoRGB(int y, int u, int v) {
    int r,g,b;
 
    r = y + (int)(1.402f*v);
    g = y - (int)(0.344f*u +0.714f*v);
    b = y + (int)(1.772f*u);
 
    r = r>255? 255 : r<0 ? 0 : r;
    g = g>255? 255 : g<0 ? 0 : g;
    b = b>255? 255 : b<0 ? 0 : b;
    return 0xff000000 | (b<<16) | (g<<8) | r;
}
 
 
unsigned int * convertYUV420_NV21toRGB8888(unsigned char data[78080], int width, int height) {
    int size = width*height;
    int offset = size;
    unsigned int * pixels = new unsigned int[size];
    int u, v, y1, y2, y3, y4;
 
    // i percorre os Y and the final pixels
    // k percorre os pixles U e V
    for(int i=0, k=0; i < size; i+=2, k+=2) {
        y1 = data[i  ]&0xff;
        y2 = data[i+1]&0xff;
        y3 = data[width+i  ]&0xff;
        y4 = data[width+i+1]&0xff;
 
        u = data[offset+k  ]&0xff;
        v = data[offset+k+1]&0xff;
        u = u-128;
        v = v-128;
 
        pixels[i  ] = convertYUVtoRGB(y1, u, v);
        pixels[i+1] = convertYUVtoRGB(y2, u, v);
        pixels[width+i  ] = convertYUVtoRGB(y3, u, v);
        pixels[width+i+1] = convertYUVtoRGB(y4, u, v);
 
        if (i!=0 && (i+2)%width==0)
            i+=width;
    }
 
    return pixels;
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    unsigned char * buffer;
    unsigned int * image = NULL;
    QPixmap pixmap;
    QImage img(352, 288, QImage::Format_ARGB32_Premultiplied);
    img.fill(QColor(Qt::white).rgb());
 
    ofstream outfile ("debug.txt",ofstream::binary);
    ifstream is;
    is.open ("sample00.yuv", ios::binary );
    //    is.seekg (0, ios::end);
    //    length = is.tellg();
    //    is.seekg (0, ios::beg);
    buffer = new unsigned char[101376];
    is.read((char * )buffer,101376);
    is.close();
    for(int x=0; x<101376; x++)
    {
        outfile.write((char*)buffer + x, 1);
    }
    outfile.close();
 
    /*
     for (int x = 0; x < 10; ++x) {
       for (int y = 0; y < 10; ++y) {
         img.setPixel(x, y, qRgb(0, 0, 0));
       }
     }
     */
 
 
    for(int i = 0; i < 101376; i++)
        qDebug() << buffer[i]<< endl;
 
    image = convertYUV420_NV21toRGB8888(buffer,352,288);
    QByteArray byteImage((const char *)image);
    pixmap.loadFromData(byteImage);
 
 
    QLabel myLabel;
    myLabel.setPixmap(pixmap);
    myLabel.setGeometry(20,100,320,122);
    myLabel.show();
 
    return a.exec();
}

还有一段例子:

/* pDistImage = new QImage(w, h, 32) */
void RGB2Image(char *srcBuf, int w, int h, QImage *pDistImage)
{
 int i;
 int r, g, b;
 QRgb *point;
 uchar *bit;
 i = 0;
 bit = (uchar *)(srcBuf);
 for(int y = 0; y < h; y ++){
    for ( int x = 0; x < w; x ++ ){
     /* Please attion the Littile-Edian and Big-Edian,
      * The Order maybe R-G-B.
      */
        b = (int)bit[i];
        g = (int)bit[i+1];
        r = (int)bit[i+2];
        point = (QRgb *)pDistImage->scanLine(y) + x;
        *point = qRgb(r, g, b);
        i += 3;
     }
 }
 return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值