I'm writing an image library from scratch I have written a console application that loads a bitmap image does some manipulation and writes the output to another bitmap. Now I would like to build a UI on top of my application. So I would like to do this in Qt Creator since I have some experience with Qt and I want it to work for multiple platforms.
Here is my code that loads a bitmap:
FILE *streamIn = fopen("path/to/file.bmp", "rb");
if (streamIn == (FILE *)0)
{
printf("Unable to open file\n");
}
unsigned char bmpHeader[54];
unsigned char bmpColorTable[1024];
for (int i = 0; i < 54; i++)
{
bmpHeader[i] = getc(streamIn);
}
int width = *(int *)&bmpHeader[18];
int height = *(int *)&bmpHeader[22];
int bitDepth = *(int *)&bmpHeader[28];
if (bitDepth <= 8)
{
fread(bmpColorTable, sizeof(unsigned char), 1024, streamIn);
}
unsigned char buf[height * width];
fread(buf, sizeof(unsigned char), (height * width), streamIn);
fclose(streamIn);
How do I get this into my UI? I already tried something like:
const QImage image(buf, width, height, QImage::Format_Grayscale8);
imageLabel->setPixmap(QPixmap::fromImage(image));
But that results in a tiny white image not the image I just read. Perhaps I can skip creating a QImage
and create a QPixmap
right away? What I tried now does not work so maybe someone more experienced can tell me how to get it done. When I loaded the initial image I would like to update the view when I do some manipulation so the user can see the changes.
I know it can be done much easier with QImageReader
but this is for learning purposes.