home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / WBITMAP.ZIP / BMFILE.C < prev    next >
C/C++ Source or Header  |  1991-10-15  |  5KB  |  254 lines

  1. /*
  2. **    $id: ssvcid bmfile.c 1.0 10/15/91  9:49 am$
  3. **        This file contains the functions needed to support reading and writing
  4. **    bitmaps from and to Windows 3.0 bitmap files, as well as for reading in
  5. **    icon files.
  6. **
  7. **    (C) 1991 Larry Widing
  8. */
  9. #include    <windows.h>
  10. #include    <malloc.h>
  11. #include    <dos.h>
  12. #include    <stdlib.h>
  13. #include    "bitmaps.h"
  14. #include    "bmmanip.h"
  15.  
  16. /*
  17. ** HANDLE                                            handle of created DI bitmap
  18. ** ReadBitmapFile(const char *filename);    name of file to load
  19. **
  20. **    This function will read the passed file in, and create a device
  21. **    dependant bitmap, returning the handle to the created bitmap to the
  22. **    caller.
  23. **
  24. ** Modification History:
  25. ** 09/06/91  LCW  Created
  26. */
  27. HANDLE
  28. ReadBitmapFile(const char *filename)
  29. {
  30.     int                    file;
  31.     int                    rc;
  32.     int                    block;
  33.     long                    size;
  34.     HANDLE                hdata = (HANDLE)NULL;
  35.     char HUGE            *pdata;
  36.     char HUGE            *ptr;
  37.     BITMAPFILEHEADER    bfHdr;
  38.     OFSTRUCT                ofs;
  39.  
  40.     /*
  41.     **    1. Open file
  42.     */
  43.     file = OpenFile((LPSTR)filename, (LPOFSTRUCT)&ofs, OF_READ | OF_SHARE_DENY_WRITE);
  44.     if (file != -1)
  45.     {
  46.         /*
  47.         **    2. Read in BITMAPFILEHEADER and verify that this is a bitmap file
  48.         */
  49.         rc = _lread(file, (LPSTR)&bfHdr, sizeof(BITMAPFILEHEADER));
  50.         if (rc == sizeof(BITMAPFILEHEADER)
  51.             || bfHdr.bfType == ('B' + ('M' << 8)))
  52.         {
  53.             /*
  54.             **    3. Allocate storage for packed DIB
  55.             */
  56.             size = bfHdr.bfSize - sizeof(BITMAPFILEHEADER);
  57.             hdata = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD, size);
  58.             if (hdata != (HANDLE)NULL)
  59.             {
  60.                 rc = -1;
  61.                 pdata = (char HUGE *)GlobalLock(hdata);
  62.                 if (pdata != NULL)
  63.                 {
  64.                     /*
  65.                     **    4. Read in DIB header and bits into packed-DIB buffer
  66.                     */
  67.                     block = 16 * 1024;    /* size of chunks to read in */
  68.                     ptr = pdata;
  69.  
  70.                     while (size > 0)
  71.                     {
  72.                         if (size < (long)block)
  73.                         {
  74.                             block = (int)size;
  75.                         }
  76.  
  77.                         if (_lread(file, (LPSTR)ptr, block) != block)
  78.                         {
  79.                             ErrorBox("ReadBitmapFile(): Error reading BMP file");
  80.                             break;
  81.                         }
  82.  
  83.                         size -= (long)block;
  84. #if    defined(__TSC__)
  85.                         if ((FP_OFF(ptr) + block) == 0)
  86.                         {
  87.                             ptr = MK_FP(FP_SEG(ptr) + 8, 0);
  88.                         }
  89.                         else
  90.                         {
  91.                             ptr += block;
  92.                         }
  93. #else
  94.                         ptr += block;
  95. #endif
  96.                     }
  97.  
  98.                     if (size == 0)
  99.                     {
  100.                         rc = 0;
  101.                     }
  102.                     GlobalUnlock(hdata);
  103.                 }
  104.                 else
  105.                 {
  106.                     ErrorBox("ReadBitmapFile(): Error locking packed DIB memory");
  107.                 }
  108.  
  109.                 if (rc < 0)
  110.                 {
  111.                     GlobalFree(hdata);
  112.                     hdata = (HANDLE)NULL;
  113.                 }
  114.             }
  115.             else
  116.             {
  117.                 ErrorBox("ReadBitmapFile(): Unable to allocate memory for packed DIB");
  118.             }
  119.         }
  120.         else
  121.         {
  122.             ErrorBox("ReadBitmapFile(): Error reading BITMAPFILEHEADER");
  123.         }
  124.         _lclose(file);
  125.     }
  126.     else
  127.     {
  128.         ErrorBox("ReadBitmapFile(): Unable to open bitmap file");
  129.     }
  130.  
  131.     return hdata;
  132. }
  133.  
  134. /*
  135. ** int
  136. ** WriteBitmapFile(
  137. **        const char *filename,    name of file to load
  138. **        const HANDLE hbm);        handle to packed DIB
  139. **
  140. **    This function will write the passed packed Device Independant Bitmap
  141. **    to the specified file.
  142. **
  143. ** Modification History:
  144. ** 09/06/91  LCW  Created
  145. */
  146. int
  147. WriteBitmapFile(const char *filename, const HANDLE hbm)
  148. {
  149.     int                    file;
  150.     int                    rc = -1;
  151.     int                    block;
  152.     long                    size;
  153.     char HUGE            *ptr;
  154.     BITMAPFILEHEADER    bfHdr;
  155.     LPBITMAPINFO        bmi;
  156.     OFSTRUCT                ofs;
  157.  
  158.     /*
  159.     **    1. Open output file
  160.     */
  161.     file = OpenFile((LPSTR)filename, (LPOFSTRUCT)&ofs, OF_CREATE | OF_WRITE);
  162.     if (file != -1)
  163.     {
  164.         /*
  165.         **    2. Lock memory resource
  166.         */
  167.         bmi = (LPBITMAPINFO)GlobalLock(hbm);
  168.         if (bmi != NULL)
  169.         {
  170.             /*
  171.             **    3. Create BITMAPFILEHEADER and write to file
  172.             */
  173.             bfHdr.bfType = ('B' + ('M' << 8));
  174.             bfHdr.bfSize = sizeof(BITMAPFILEHEADER) + GlobalSize(hbm);
  175.             bfHdr.bfReserved1 = 0;
  176.             bfHdr.bfReserved2 = 0;
  177.             bfHdr.bfOffBits = sizeof(BITMAPFILEHEADER)
  178.                 + (DWORD)(DIBitmapBits(bmi) - (LPSTR)bmi);
  179.             if (_lwrite(file, (LPSTR)&bfHdr, sizeof(bfHdr)) == sizeof(bfHdr))
  180.             {
  181.                 /*
  182.                 **    4. Write out DIB header and packed bits to file
  183.                 */
  184.                 size = GlobalSize(hbm);
  185.                 ptr = (char HUGE *)bmi;
  186.                 block = 16 * 1024;    /* size of chunks to write out */
  187.  
  188.                 while (size > 0)
  189.                 {
  190.                     if (size < (long)block)
  191.                     {
  192.                         block = (int)size;
  193.                     }
  194.  
  195.                     if (_lwrite(file, (LPSTR)ptr, block) != block)
  196.                     {
  197.                         ErrorBox("WriteBitmapFiile(): Error writing DIB");
  198.                         break;
  199.                     }
  200.  
  201.                     size -= (long)block;
  202. #if    defined(__TSC__)
  203.                     if ((FP_OFF(ptr) + block) == 0)
  204.                     {
  205.                         ptr = MK_FP(FP_SEG(ptr) + 8, 0);
  206.                     }
  207.                     else
  208.                     {
  209.                         ptr += block;
  210.                     }
  211. #else
  212.                     ptr += block;
  213. #endif
  214.                 }
  215.  
  216.                 if (size == 0)
  217.                 {
  218.                     rc = 0;
  219.                 }
  220.             }
  221.             else
  222.             {
  223.                 ErrorBox("WriteBitmapFile(): Error writing BITMAPFILEHEADER");
  224.             }
  225.  
  226.             GlobalUnlock(hbm);
  227.         }
  228.         else
  229.         {
  230.             ErrorBox("WriteBitmapFile(): Error locking bitmap into memory");
  231.         }
  232.         _lclose(file);
  233.     }
  234.     else
  235.     {
  236.         ErrorBox("WriteBitmapFile(): Error opening output file");
  237.     }
  238.  
  239.     if (rc != 0)
  240.     {
  241.         unlink(filename);
  242.     }
  243.     
  244.     return rc;
  245. }
  246.  
  247. /*
  248. **    Modification History
  249. **    --------------------
  250. **    $lgb$
  251. ** 10/15/91     Larry Widing   Initial version for Win Tech Journal Article.
  252. **    $lge$
  253. */
  254.