With C++ and QT, how do I display a 16-bit raw file as an image? -
i looking display heightmaps video game battlefield 2 images in application. new c++ , qt , might straight forward having trouble displaying grayscale 16-bit 1025x1025 2101250 bytes image. there no header file. need access displayed pixels (does not have pixel perfect precision) can point pixel , value.
what have tried
i have loaded binary data qbytearray qfile , have attempted use qimage::fromdata function make image making lot of mistakes , spending lot of time not getting far. hope posting here give me clue(s) need progress. here code:
void learningbinaryreader::setupreader() { qdebug("attempting open file.."); qfile file("heightmapprimary.raw"); if (!file.open(qfile::readonly)) { qdebug("could not open file"); return; } else { qdebug() << file.filename() << " opened"; } qbytearray data = file.readall(); file.flush(); file.close(); qdebug() << data.count() << "bytes loaded."; }
from here @ loss of do. have read of qt documentation, being new need guide in right direction understand problem , solution.
please note pretty beginner don't discount easy solutions might not have thought of. using qt framework.
just guess. maybe try this?
#include <qcolor> ... qbytearray data=file.readall(); // create empty image of right size. we'll use 32-bit rgb simplicity qimage img(1025,1025, qimage::format_rgb32); // access image @ low level. manual, 32-bit rgb image // vector of qrgb (which integer typedef) qrgb *pixels=reinterpret_cast<qrgb*>(img.bits()); // copy our image data in. we'll assume 16-bit le format input data. // since have 8 bits of grayscale color resolution in 32-bit rgb, we'll // chop off significant 8 bits (the second byte of each pair) , // make pixel out of that. if doesn't work, our assumption might off -- // perhaps assume 16-bit format. in case we'd want first byte of each // pair. (size_t i=0;2*i<data.size();++i) { uchar pixel_msb=data[2*i+1]; // or maybe try =data[2*i+0] pixels[i]=qrgb(pixel_msb, pixel_msb, pixel_msb); } // (do resulting 'img')
edit: oops, qimage::format_rgb32
instead of qimage::format_rgb
Comments
Post a Comment