ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

dib.c
Go to the documentation of this file.
00001 /*
00002  * PROJECT:     PAINT for ReactOS
00003  * LICENSE:     LGPL
00004  * FILE:        base/applications/paint/dib.c
00005  * PURPOSE:     Some DIB related functions
00006  * PROGRAMMERS: Benedikt Freisen
00007  */
00008 
00009 /* INCLUDES *********************************************************/
00010 
00011 #include "precomp.h"
00012 
00013 /* FUNCTIONS ********************************************************/
00014 
00015 HBITMAP
00016 CreateDIBWithProperties(int width, int height)
00017 {
00018     BITMAPINFO bmi;
00019     ZeroMemory(&bmi, sizeof(BITMAPINFO));
00020     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
00021     bmi.bmiHeader.biWidth = width;
00022     bmi.bmiHeader.biHeight = height;
00023     bmi.bmiHeader.biPlanes = 1;
00024     bmi.bmiHeader.biBitCount = 24;
00025     bmi.bmiHeader.biCompression = BI_RGB;
00026     return CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, NULL, NULL, 0);
00027 }
00028 
00029 int
00030 GetDIBWidth(HBITMAP hBitmap)
00031 {
00032     BITMAP bm;
00033     GetObject(hBitmap, sizeof(BITMAP), &bm);
00034     return bm.bmWidth;
00035 }
00036 
00037 int
00038 GetDIBHeight(HBITMAP hBitmap)
00039 {
00040     BITMAP bm;
00041     GetObject(hBitmap, sizeof(BITMAP), &bm);
00042     return bm.bmHeight;
00043 }
00044 
00045 void
00046 SaveDIBToFile(HBITMAP hBitmap, LPTSTR FileName, HDC hDC, LPSYSTEMTIME time, int *size, int hRes, int vRes)
00047 {
00048     BITMAP bm;
00049     HANDLE hFile;
00050     BITMAPFILEHEADER bf;
00051     BITMAPINFOHEADER bi;
00052     int imgDataSize;
00053     DWORD dwBytesWritten;
00054     char *buffer;
00055 
00056     GetObject(hBitmap, sizeof(BITMAP), &bm);
00057 
00058     ZeroMemory(&bf, sizeof(BITMAPFILEHEADER));
00059     ZeroMemory(&bi, sizeof(BITMAPINFOHEADER));
00060 
00061     imgDataSize = bm.bmWidthBytes * bm.bmHeight;
00062     bf.bfType = 0x4d42;         /* BM */
00063     bf.bfSize = imgDataSize + 52;
00064     bf.bfOffBits = 54;
00065     bi.biSize = sizeof(BITMAPINFOHEADER);
00066     bi.biWidth = bm.bmWidth;
00067     bi.biHeight = bm.bmHeight;
00068     bi.biPlanes = bm.bmPlanes;
00069     bi.biBitCount = bm.bmBitsPixel;
00070     bi.biCompression = BI_RGB;
00071     bi.biXPelsPerMeter = hRes;
00072     bi.biYPelsPerMeter = vRes;
00073 
00074     buffer = HeapAlloc(GetProcessHeap(), 0, imgDataSize);
00075     GetDIBits(hDC, hBitmap, 0, bm.bmHeight, buffer, (LPBITMAPINFO) & bi, DIB_RGB_COLORS);
00076 
00077     hFile = CreateFile(FileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
00078     if (hFile == INVALID_HANDLE_VALUE)
00079         return;
00080 
00081     WriteFile(hFile, &bf, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
00082     WriteFile(hFile, &bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
00083     WriteFile(hFile, buffer, imgDataSize, &dwBytesWritten, NULL);
00084 
00085     if (time)
00086     {
00087         FILETIME ft;
00088         GetFileTime(hFile, NULL, NULL, &ft);
00089         FileTimeToSystemTime(&ft, time);
00090     }
00091     if (size)
00092         *size = GetFileSize(hFile, NULL);
00093 
00094     CloseHandle(hFile);
00095     HeapFree(GetProcessHeap(), 0, buffer);
00096 }
00097 
00098 void
00099 LoadDIBFromFile(HBITMAP * hBitmap, LPTSTR name, LPSYSTEMTIME time, int *size, int *hRes, int *vRes)
00100 {
00101     BITMAPFILEHEADER bfh;
00102     BITMAPINFO *bi;
00103     PVOID pvBits;
00104     DWORD dwBytesRead;
00105     HANDLE hFile;
00106 
00107     if (!hBitmap)
00108         return;
00109 
00110     hFile =
00111         CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
00112     if (hFile == INVALID_HANDLE_VALUE)
00113         return;
00114 
00115     /* read header and check for 'BM' magic */
00116     ReadFile(hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwBytesRead, NULL);
00117     if (bfh.bfType != 0x4d42)
00118     {
00119         CloseHandle(hFile);
00120         return;
00121     }
00122 
00123     if (time)
00124     {
00125         FILETIME ft;
00126         GetFileTime(hFile, NULL, NULL, &ft);
00127         FileTimeToSystemTime(&ft, time);
00128     }
00129     if (size)
00130         *size = GetFileSize(hFile, NULL);
00131 
00132     bi = HeapAlloc(GetProcessHeap(), 0, bfh.bfOffBits - sizeof(BITMAPFILEHEADER));
00133     if (!bi)
00134         return;
00135 
00136     ReadFile(hFile, bi, bfh.bfOffBits - sizeof(BITMAPFILEHEADER), &dwBytesRead, NULL);
00137     *hBitmap = CreateDIBSection(NULL, bi, DIB_RGB_COLORS, &pvBits, NULL, 0);
00138     ReadFile(hFile, pvBits, bfh.bfSize - bfh.bfOffBits, &dwBytesRead, NULL);
00139 
00140     if (hRes)
00141         *hRes = (*bi).bmiHeader.biXPelsPerMeter;
00142     if (vRes)
00143         *vRes = (*bi).bmiHeader.biYPelsPerMeter;
00144 
00145     CloseHandle(hFile);
00146     HeapFree(GetProcessHeap(), 0, bi);
00147 }

Generated on Sun May 27 2012 04:17:04 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.