With C++ and Qt how do I display a 16-bit raw file as an image?

后端 未结 3 1620
独厮守ぢ
独厮守ぢ 2020-12-20 01:01

I am looking to display heightmaps from the video game Battlefield 2 as images in my application.
I am new to C++ and Qt and it might be straight forward but what I am h

3条回答
  •  Happy的楠姐
    2020-12-20 01:22

    Just a guess. Maybe try something like this?

    #include 
    ...
    QByteArray data=file.readAll();
    
    // create an empty image of the right size.  We'll use 32-bit RGB for simplicity
    QImage img(1025,1025, QImage::Format_RGB32);
    
    // Access the image at low level.  From the manual, a 32-bit RGB image is just a
    // vector of QRgb (which is really just some integer typedef)
    QRgb *pixels=reinterpret_cast(img.bits());
    
    // Now copy our image data in.  We'll assume 16-bit LE format for the input data.
    // Since we only have 8 bits of grayscale color resolution in a 32-bit RGB, we'll
    // just chop off the most significant 8 bits (the second byte of each pair) and 
    // make a pixel out of that.  If this doesn't work, our assumption might be off --
    // perhaps assume 16-bit BE format.  In that case we'd want the first byte of each
    // pair.
    for (size_t i=0;2*i

    edit: oops, QImage::Format_RGB32 instead of QImage::Format_RGB

提交回复
热议问题