Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. raw data to bitmap

raw data to bitmap

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicsperformance
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Snillet2k
    wrote on last edited by
    #1

    Hello. I have posted a question about this before but i wrote it bad. So i try again. I'm working with a program that storage BMP and WAV files in one file. I'm almost finished with it but I want to make it show BMP files directly from the packed file without extracting it. I have tried many times but I can't make function to put BITMAPINFOHEADER and BITMAPINFO correct data. /////////////////////////////////////////////////////////////////////////////// LPTSTR szFileName = "C:\\Min Projekt\\MittPixelTest\\3_16.bmp"; CFile test; test.Open(szFileName, CFile::modeRead); DWORD m_nMaxSize = test.GetLength(); void *pBuf = (BYTE *) malloc(sizeof(BYTE) * m_nMaxSize); test.ReadHuge((LPVOID) pBuf, m_nMaxSize); LoadBMPImage(pBuf, m_nMaxSize, myBmp, &myPalette); /////////////////////////////////////////////////////////////////////////////// BOOL CMyArchiveDlg::LoadBMPImage(void* vBits, DWORD len, CBitmap &bitmap, CPalette *pPal) { BITMAPFILEHEADER bmfHeader; // Read file header if (!memcpy((LPSTR)&bmfHeader, vBits, sizeof(bmfHeader))) return FALSE; // File type should be 'BM' if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B')) return FALSE; // Get length of the remainder of the file and allocate memory DWORD nPackedDIBLen = len - sizeof(BITMAPFILEHEADER); HGLOBAL hDIB = ::GlobalAlloc(GMEM_FIXED, nPackedDIBLen); if (hDIB == 0) return FALSE; // Read the remainder of the bitmap file. if (!memcpy((LPSTR)hDIB, vBits, nPackedDIBLen)) { ::GlobalFree(hDIB); return FALSE; } BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)hDIB ; // Gets wrong data BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ; // If bmiHeader.biClrUsed is zero we have to infer the number // of colors from the number of bits used to specify it. int nColors = bmiHeader.biClrUsed ? bmiHeader.biClrUsed : 1 << bmiHeader.biBitCount; // Color value is to high!! LPVOID lpDIBBits; if( bmInfo.bmiHeader.biBitCount > 8 ) lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors + bmInfo.bmiHeader.biClrUsed) + ((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0)); else lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors); // Create the logical palette if( pPal != NULL ) { // Create the palette if( nColors <= 256 ) { UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors); LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize]; pLP->palVersion = 0x300; pLP->palNumEntries = nColors; for( int i=0; i < nColors; i++) {

    J 1 Reply Last reply
    0
    • S Snillet2k

      Hello. I have posted a question about this before but i wrote it bad. So i try again. I'm working with a program that storage BMP and WAV files in one file. I'm almost finished with it but I want to make it show BMP files directly from the packed file without extracting it. I have tried many times but I can't make function to put BITMAPINFOHEADER and BITMAPINFO correct data. /////////////////////////////////////////////////////////////////////////////// LPTSTR szFileName = "C:\\Min Projekt\\MittPixelTest\\3_16.bmp"; CFile test; test.Open(szFileName, CFile::modeRead); DWORD m_nMaxSize = test.GetLength(); void *pBuf = (BYTE *) malloc(sizeof(BYTE) * m_nMaxSize); test.ReadHuge((LPVOID) pBuf, m_nMaxSize); LoadBMPImage(pBuf, m_nMaxSize, myBmp, &myPalette); /////////////////////////////////////////////////////////////////////////////// BOOL CMyArchiveDlg::LoadBMPImage(void* vBits, DWORD len, CBitmap &bitmap, CPalette *pPal) { BITMAPFILEHEADER bmfHeader; // Read file header if (!memcpy((LPSTR)&bmfHeader, vBits, sizeof(bmfHeader))) return FALSE; // File type should be 'BM' if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B')) return FALSE; // Get length of the remainder of the file and allocate memory DWORD nPackedDIBLen = len - sizeof(BITMAPFILEHEADER); HGLOBAL hDIB = ::GlobalAlloc(GMEM_FIXED, nPackedDIBLen); if (hDIB == 0) return FALSE; // Read the remainder of the bitmap file. if (!memcpy((LPSTR)hDIB, vBits, nPackedDIBLen)) { ::GlobalFree(hDIB); return FALSE; } BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)hDIB ; // Gets wrong data BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ; // If bmiHeader.biClrUsed is zero we have to infer the number // of colors from the number of bits used to specify it. int nColors = bmiHeader.biClrUsed ? bmiHeader.biClrUsed : 1 << bmiHeader.biBitCount; // Color value is to high!! LPVOID lpDIBBits; if( bmInfo.bmiHeader.biBitCount > 8 ) lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors + bmInfo.bmiHeader.biClrUsed) + ((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0)); else lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors); // Create the logical palette if( pPal != NULL ) { // Create the palette if( nColors <= 256 ) { UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors); LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize]; pLP->palVersion = 0x300; pLP->palNumEntries = nColors; for( int i=0; i < nColors; i++) {

      J Offline
      J Offline
      John R Shaw
      wrote on last edited by
      #2

      First: if (!memcpy((LPSTR)hDIB, vBits, nPackedDIBLen)) well normaly I would have called GlobalLock() to get the pointer, but hDIB is realy a pointer so this should work ( LPSTR should be LPVOID ). The problem with the line is that vBits points to the start of the buffer, where the BITMAPFILEHEADER is stored and you are trying to copy the information following it: if (!memcpy((LPSTR)hDIB, (LPBYTE)vBits + sizeof(BITMAPFILEHEADER), nPackedDIBLen)) That should fix the following: BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)hDIB ; // Gets wrong data BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ; (interesting use of references!) Try takeing a look at the CDibData article; I wrote the class so that it could take a handle to a DIB (see CDibData::Attach()) just like the clipboard. INTP

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups