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?
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);