home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / VDLib / source / win32 / DIBSection.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  3.4 KB  |  135 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Application helper library
  3. //    Copyright (C) 1998-2007 Avery Lee
  4. //
  5. //    This program is free software; you can redistribute it and/or modify
  6. //    it under the terms of the GNU General Public License as published by
  7. //    the Free Software Foundation; either version 2 of the License, or
  8. //    (at your option) any later version.
  9. //
  10. //    This program is distributed in the hope that it will be useful,
  11. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //    GNU General Public License for more details.
  14. //
  15. //    You should have received a copy of the GNU General Public License
  16. //    along with this program; if not, write to the Free Software
  17. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include "stdafx.h"
  20. #include <windows.h>
  21. #include <vd2/VDLib/win32/DIBSection.h>
  22. #include <vd2/VDLib/win32/FileMapping.h>
  23. #include <vd2/Kasumi/pixmap.h>
  24.  
  25. VDDIBSectionW32::VDDIBSectionW32()
  26.     : mpBits(NULL)
  27.     , mhbm(NULL)
  28.     , mhdc(NULL)
  29.     , mhgo(NULL)
  30.     , mbForceUnmap(false)
  31. {
  32. }
  33.  
  34. VDDIBSectionW32::~VDDIBSectionW32() {
  35.     Shutdown();
  36. }
  37.  
  38. bool VDDIBSectionW32::Init(int w, int h, int depth, const VDFileMappingW32 *mapping, uint32 mapOffset) {
  39.     BITMAPINFO bi = {0};
  40.     bi.bmiHeader.biSize                = sizeof(BITMAPINFOHEADER);
  41.     bi.bmiHeader.biWidth            = w;
  42.     bi.bmiHeader.biHeight            = h;
  43.     bi.bmiHeader.biPlanes            = 1;
  44.     bi.bmiHeader.biCompression        = BI_RGB;
  45.     bi.bmiHeader.biBitCount            = depth;
  46.     bi.bmiHeader.biSizeImage        = ((w * depth + 31) >> 5) * 4 * abs(h);
  47.     bi.bmiHeader.biXPelsPerMeter    = 0;
  48.     bi.bmiHeader.biYPelsPerMeter    = 0;
  49.     bi.bmiHeader.biClrUsed            = 0;
  50.     bi.bmiHeader.biClrImportant        = 0;
  51.  
  52.     return Init(&bi, mapping, mapOffset);
  53. }
  54.  
  55. bool VDDIBSectionW32::Init(const tagBITMAPINFO *bi, const VDFileMappingW32 *mapping, uint32 mapOffset) {
  56.     Shutdown();
  57.  
  58.     HDC hdc = GetDC(NULL);
  59.     mhdc = CreateCompatibleDC(hdc);
  60.     if (mhdc) {
  61.         HANDLE hMap = NULL;
  62.         if (mapping)
  63.             hMap = mapping->GetHandle();
  64.  
  65.         mhbm = CreateDIBSection(hdc, bi, DIB_RGB_COLORS, &mpBits, hMap, mapOffset);
  66.         if (mhbm) {
  67.             mbForceUnmap = (hMap != NULL);
  68.  
  69.             mhgo = SelectObject(mhdc, mhbm);
  70.             if (mhgo) {
  71.                 ReleaseDC(NULL, hdc);
  72.  
  73.                 mWidth        = bi->bmiHeader.biWidth;
  74.                 mHeight        = abs(bi->bmiHeader.biHeight);
  75.                 mDepth        = bi->bmiHeader.biBitCount;
  76.                 mPitch        = ((mWidth * mDepth + 31) >> 5) * 4;
  77.                 mpScan0        = mpBits;
  78.                 if (bi->bmiHeader.biHeight >= 0) {
  79.                     mpScan0 = (char *)mpScan0 + mPitch*(mHeight - 1);
  80.                     mPitch = -mPitch;
  81.                 }
  82.                 return true;
  83.             }
  84.         }
  85.     }
  86.     ReleaseDC(NULL, hdc);
  87.  
  88.     Shutdown();
  89.     return false;
  90. }
  91.  
  92. void VDDIBSectionW32::Shutdown() {
  93.     if (mhdc) {
  94.         if (mhbm) {
  95.             if (mhgo) {
  96.                 SelectObject(mhdc, mhgo);
  97.                 mhgo = NULL;
  98.             }
  99.  
  100.             DeleteObject(mhbm);
  101.             mhbm = NULL;
  102.  
  103.             if (mbForceUnmap) {
  104.                 UnmapViewOfFile(mpBits);
  105.  
  106.                 mbForceUnmap = false;
  107.             }
  108.  
  109.             mpBits = NULL;
  110.         }
  111.  
  112.         DeleteDC(mhdc);
  113.         mhdc = NULL;
  114.     }
  115. }
  116.  
  117. VDPixmap VDDIBSectionW32::GetPixmap() const {
  118.     VDPixmap px;
  119.  
  120.     px.data        = mpScan0;
  121.     px.pitch    = mPitch;
  122.     px.palette    = NULL;
  123.     px.w        = mWidth;
  124.     px.h        = mHeight;
  125.     px.format    = mDepth == 16 ? nsVDPixmap::kPixFormat_XRGB1555
  126.                 : mDepth == 24 ? nsVDPixmap::kPixFormat_RGB888
  127.                 : mDepth == 32 ? nsVDPixmap::kPixFormat_XRGB8888
  128.                 : 0;
  129.     px.data2    = NULL;
  130.     px.pitch2    = 0;
  131.     px.data3    = NULL;
  132.     px.pitch3    = 0;
  133.     return px;
  134. }
  135.