Advertisement
adilima

DPX Viewer using QLabel

Feb 20th, 2012
3,559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <QtGui>
  2. #include "dpx.h"
  3. #include <iostream>
  4.  
  5. #define UINT_10_TO_8(val) \
  6. (int)(((float)(val & 0x3FF) * (float)(0xFF)) / (float)(0x3FF))
  7.  
  8. #define DPX_PIXEL_RGB   50
  9.  
  10. QLabel* LoadDPX()
  11. {
  12.     QString strPath = QFileDialog::getOpenFileName(NULL, "Open DPX File",
  13.                         QDir::homePath(),
  14.                         "*.dpx",
  15.                         NULL, 0);
  16.     if (strPath.isEmpty())
  17.         return NULL;
  18.    
  19.     DPXHeader hdr;
  20.     DPXImageHeader imgHdr;
  21.     FILE* pf = fopen(strPath.toStdString().c_str(), "r");
  22.     if (!pf)
  23.         return NULL;
  24.     if (!fread(&hdr, sizeof(hdr), 1, pf))
  25.     {
  26.         std::cerr << "Unable to read DPXHeader\n";
  27.         fclose(pf);
  28.         return NULL;
  29.     }
  30.     if (!fread(&imgHdr, sizeof(imgHdr), 1, pf))
  31.     {
  32.         std::cerr << "Unable to read DPXImageHeader\n";
  33.         fclose(pf);
  34.         return NULL;
  35.     }
  36.    
  37.     // assume 10-bit image
  38.     if (imgHdr.img_element[0].bit_size != 10)
  39.     {
  40.         std::cerr << "Only supports 10-bit DPX\n";
  41.         fclose(pf);
  42.         return NULL;
  43.     }
  44.     // and has RGB interleaved format
  45.     if (imgHdr.img_element[0].descriptor != DPX_PIXEL_RGB)
  46.     {
  47.         std::cerr << "Has to be RGB Interleaved (desciptor == "
  48.             << (int)imgHdr.img_element[0].descriptor << ")\n";
  49.         fclose(pf);
  50.         return NULL;
  51.     }
  52.    
  53.     int nCount = imgHdr.img_width * imgHdr.img_height;
  54.     size_t read_size = nCount * sizeof(uint32_t);
  55.    
  56.     QImage* pImg = new QImage(imgHdr.img_width,
  57.                 imgHdr.img_height,
  58.                 QImage::Format_RGB32);
  59.    
  60.     // Jump to the right place
  61.     fseek(pf, hdr.image_offset, SEEK_SET);
  62.    
  63.     if (!fread(pImg->bits(), sizeof(uint32_t), read_size, pf))
  64.     {
  65.         std::cerr << "Unable to read image data\n";
  66.         delete pImg;
  67.         fclose(pf);
  68.         return NULL;
  69.     }
  70.    
  71.     // now, align the bits properly...
  72.     uint32_t* pData32 = (uint32_t*)pImg->bits();
  73.     uint32_t val32;
  74.     for (int i = 0; i < nCount; i++)
  75.     {
  76.         val32 = *pData32;
  77.         if (imgHdr.img_element[0].packing == 1)
  78.         {
  79.             // 10-bit DPX packed using METHOD A
  80.             // 2 bits padding in least significant bits
  81.             // get rid of this bits
  82.             val32 >>= 2;
  83.         }
  84.         *pData32++ = UINT_10_TO_8(val32) | (UINT_10_TO_8(val32>>10)<<8) |
  85.             (UINT_10_TO_8(val32>>20)<<16) | (0xFF<<24);
  86.     }
  87.    
  88.     QLabel* pResult = new QLabel;
  89.     pResult->setPixmap(QPixmap::fromImage(*pImg));
  90.     return pResult;
  91. }
  92.  
  93. int main(int argc, char** argv)
  94. {
  95.     QApplication app(argc, argv);
  96.     QLabel* label = LoadDPX();
  97.     if (!label)
  98.         return 0;
  99.     label->show();
  100.     return app.exec();
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement