The below code consist of 3 parts (threads in this case).
In part 1: U have a hardware which act as a camera and generates the RAW data of each really fast roughly 3000*5000 pixel. Each pixel is Raw value of 16bit in grayscale please don’t be concern for how this camera works just assume it generates the data.
(Thread1)
void camera(){
char* framebuf = NULL;
uint8_t* images = NULL;
images = new uint8_t[3000*5000*100];
framebuf = new char[3000*5000];
for(int i = 0, i<3000*5000; i++){
framebuf+i = "1AD2"; // sudo code not actual implementation
}
// copy these lines to image in memory
memcpy(images + (recv_frames * ImageByteSize), framebuf, ImageByteSize);
// not emit the index of read and location to different thread
emit transferCurrentFrame(images, index);
}
The camera function is called 100 times.
Part2: In this you get this emited signal in different thread which read the whole image and convert it to 8 bit value (as i am under the impression that the graphicsview can only handle 8 bit data).
(Thread2)
Display::Display(QObject *parent)
: QThread{parent}, image(3000, 5000, QImage::Format_Grayscale16)
{}
void ProcessRecievedFrame(uint8_t* images, int index){
const uint32_t* rawPixels = reinterpret_cast<const uint32_t*>(images);
for (int y = 0; y < 5000; y++) {
for (int x = 0; x < 3000; x++) {
int bufIndex = (y * width + x) + (index* width * frameHeight);
ushort pixelValue = rawPixels[bufIndex];
int scaledValue = static_cast<int>(static_cast<double>(pixelValue) >> 8);
image.setPixelColor(x, currentLine + y, QColor(scaledValue, scaledValue, scaledValue));
}
}
emit frameProcessed(image);
}
I am not sure whether i need to scale my 16 bit value or not or can I use them directly but this is computational heavy and causes lag in loading when dealing with so much data.
Part 3: This is the main thread which runs GUI and grphicsView. This thread takes the signal from Thread2. The pgraphics view dimension is huge and therefore I have to keep the aspectratio to fit the image.
(Thread3)
GUI::GUI(QWidget *parent)
: QMainWindow(parent), ui(new Ui::GUI)
{
ui->setupUi(this);
m_try = new Try(this);
//initilise the graphics view
scene = new QGraphicsScene();
scene->setSceneRect(0, 0, 3000, 5000*100);
scene->setBackgroundBrush(Qt::black);
ui->graphicsView->setScene(scene);
ui->graphicsView->rotate(270);
ui->graphicsView->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
}
void GUI::resetCurrentLine(const QImage &image) {
int frameHeight = 5000;
QPixmap pixmap = QPixmap::fromImage(image);
QRectF updateRect(0, currentLinegui, 3000, frameHeight);
scene->invalidate(updateRect, QGraphicsScene::ForegroundLayer);
QGraphicsPixmapItem *pixmapItem = scene->addPixmap(pixmap.copy(0, currentLinegui, 3000, frameHeight));
pixmapItem->setPos(0, currentLinegui);
currentLinegui += frameHeight;
ui->graphicsView->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
if (currentLinegui > 5000){
qDebug() << "doing reset" << "breset" << currentLinegui;
currentLinegui = 0;
scene->clear();
scene->setBackgroundBrush(Qt::black);
ui->graphicsView->setScene(scene);
ui->graphicsView->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
}
}
Note: After showing 5000 lines I refresh my graphicsview to black
Right now everything is working as per my expectation but only problem is the time delay. The Image isn’t shown immediately. Approx. each image is shown after 5 seconds after acquisition and this time adds up to next image. So even though the acquisition of 3 image takes 3 seconds but the last image is shown after 15 seconds.
My assumption is scaling every bit to 8bit ans using setPixelColor
cause this problem as my i7 cpu usage goes to 100% let me know if I am wrong
I assume this is enough information but let me know if you still need more clarification on this problem?