Message 1 of 3
Loading bitmaps in a thread-safe manner
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have a function which I use for generating thumbnails for the UI, but when I call it from multiple threads I get memory access errors in TheManager->Load() . I tried locking it with both Win32 critical sections and Qmutex, but with no success. I guess TheManager->Load() itself is not thread-safe. Is there another way?
Bitmap* Loader::loadHDR(const QString & path) {
Bitmap* bmap;
BitmapInfo bi;
QFile file(path);
if (file.exists() && file.fileName().endsWith(".exr") || file.fileName().endsWith(".hdr"))
{
bi.SetName(TSTR(path));
bmap = TheManager->Load(&bi);
if (!bmap)
{
return nullptr;
}
return bmap;
}
return nullptr;
}
QIcon Loader::getIcon(const QString & path, QSize size) { Bitmap* bmap = loadHDR(path); if (bmap) { BitmapInfo bi_icon; bi_icon.SetWidth(size.width()); bi_icon.SetHeight(size.height()); bi_icon.SetType(BMM_TRUE_32); Bitmap* icon = TheManager->Create(&bi_icon); icon->CopyImage(bmap, COPY_IMAGE_RESIZE_LO_QUALITY, BMM_Color_64(0, 0, 0, 0)); int type; uchar* buffer = static_cast<uchar*>(icon->GetStoragePtr(&type)); QImage image(buffer, icon->Width(), icon->Height(), QImage::Format::Format_RGB888); if (bmap) bmap->DeleteThis(); return QIcon(QPixmap::fromImage(image)); } return QIcon(); }
