1

Can anybody tell me how i can format my array so i can load it to a QImage? For now i have a 2d-char-array:

uchar dataArray[400][400];
time_t t;
srand(time(&t));
int x, y;
for(x=0; x< 400; x++)
{
    for(y=0; y<400; y++)
    {
        dataArray[x][y] = rand()%1001;
    }
}
QPainter MyPainter(this);
scene = new QGraphicsScene(this);
scene->addEllipse(200, 200, 20, 20);
ui.graphicsView->setScene(scene);
*image = new QImage(*dataArray, 400, 400, QImage::Format_Mono);
image->setColorCount(2);
image->setColor(1, qRgb(255, 0, 0));//red
image->setColor(0, Qt::transparent);
scene->addPixmap(QPixmap::fromImage(*image));

When the content of the array is 0 I want the one color and content > 0 the other color. So i want to load the array into a monochromatic QImage. Obviously this array does not work. How does the Array need to be formatted for my QImage to load properly? The doc just says following, but i do not really get what that means...

data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.

I want a QImage with Format_Mono like this Where "x" and "+" represent single pixels with different color (red and transparent):

xxxx+xxx++xxx
xxx++xx++xxxx
++x+x+xxxxxxx
+xxx+x+x+x+xx

For this i have an array (dataArray as in code above) with same pattern where x is above a specified value and + is under or equal (the value is 0 at the moment). How do i convert this array to an array wich can be used by QImage with Format_Mono so i can see the correct pattern?

2
  • Not enough data. What is the type of dataArray?
    – Marek R
    Sep 17, 2015 at 10:50
  • It is uchar. But i know the array will not work like i implemented it. Sep 17, 2015 at 10:56

2 Answers 2

0

Assuming that dataArray is declared as uchar dataArray[400][400];

QImage tmpImg(dataArray[0], 400, 400, QImage::Format_Grayscale8);
// this image shares memory with dataArray and this is not safe 
// for inexperienced developer

QImage result(tmpImg); // shallow copy
result.bits(); // enforce a deep copy of image
3
  • This does not work. The way I formatted my Array it should show about 1600 random pixels in the whole Image. But using it the way I imlemented the array it shows much more random Pixels (50%) all over the Image. I do not think my problem is how to pass the array to QImage but how i format the array in first place. Sep 17, 2015 at 11:06
  • so provide more code to show what did you do wrong.
    – Marek R
    Sep 17, 2015 at 11:27
  • I added more code, but the error must be in how i fill the array, because i simply do not know what format is required by QImage. Also i added some more text and hope you will understand my problem better now. Sep 17, 2015 at 11:35
0

Found the solution to convert my dataArray into a working imageArray:

Its pretty easy as soon as you figure out how it is done (and it should be obviously i do not know why i dind't get it in first place...). I just needed to convert every data-point to the new array bit-wise, also I had to figure out, that the axis of the imageArray have to be in [y][x]-order not the other way round (who the hell would do that in this order?!).

Many time wasted to figure that out...

uchar dataArray[400][400]; //first index is x-axis, second one is y-axis
uchar imageArray[400][52]; //first index is y-axis, second one is x-axis 
time_t t;
srand(time(&t));
int x, y;
for(y=0; y<400; y++)
{
    for(x=0; x<400; x++)
    {
        dataArray[x][y] = rand()%1001;
    }
}
for(y=0; y<400; y++)
{
    for(x=0; x<400; x++)
    {
        if(dataArray[x][y] > 0)
            imageArray[y][x/8] |= 1 << 7-x%8; //changing bit to 1
            //7- because msb is left but we start counting left starting with 0
        else
            imageArray[y][x/8] &= ~(1 << 7-x%8); //changing bit to 0
    }
}
scene = new QGraphicsScene(this);
ui.graphicsView->setScene(scene);
QImage *image = new QImage(*imageArray, 400, 400, QImage::Format_Mono);
1
  • "the axis of the imageArray have to be in [y][x]-order not the other way round (who the hell would do that in this order?!" ...Reading image data? Everyone, since progressive images are nearly* always read row-by-row / line-by-line. The trick in dealing with image data is to forget 'x' and 'y', because they just confuse matters, and instead think only in terms of 'h' and 'w'. * – (I'm sure someone would be able to come up with a counterexample, if I said always-always.)
    – FeRD
    Jan 21, 2020 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.