home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Multimedia Jumpstart 1.1a / CD_ROM.BIN / develpmt / source / mergedib / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-25  |  3.9 KB  |  160 lines

  1. /* file.c
  2.  *
  3.  * File I/O and related functions.
  4.  */
  5.  
  6. #include "nocrap.h"
  7. #include <windows.h>
  8. #include <stdlib.h>
  9. #include "commdlg.h"
  10. #include "MergeDIB.h"
  11. #include "dib.h"
  12.  
  13.  
  14. /* FreeDIB(iDIB)
  15.  *
  16.  * Delete <gahdib[iDIB]> and <gahpal[iDIB]>.  If <iDIB> is MERGED_DIB,
  17.  * then also free the elements of <gaplogpal>.
  18.  */
  19. void FAR PASCAL
  20. FreeDIB(int iDIB)
  21. {
  22.     int        i;
  23.     
  24.     if (gahdib[iDIB] != NULL)
  25.     GlobalFree(gahdib[iDIB]), gahdib[iDIB] = NULL;
  26.  
  27.     if (gahpal[iDIB] != NULL)
  28.     DeleteObject(gahpal[iDIB]), gahpal[iDIB] = NULL;
  29.     
  30.     if (iDIB == MERGED_DIB)
  31.     {
  32.     for (i = PRIMARY_DIB; i <= MERGED_DIB; i++)
  33.     {
  34.         if (gaplogpal[i] != NULL)
  35.         {
  36.         LocalFree((HANDLE) gaplogpal[i]);
  37.         gaplogpal[i] = NULL;
  38.         }
  39.     }
  40.     }
  41. }
  42.  
  43.  
  44. /* fOK = FileOpen(iDIB, szFileName)
  45.  *
  46.  * If <szFileName> is NULL, do a File/Open command to open <gahdib[iDIB]>
  47.  * and <gahpal[iDIB]>.  Otherwise, open <szFileName>.
  48.  *
  49.  * Return TRUE unless the user cancelled or the operation failed.
  50.  */
  51. BOOL FAR PASCAL
  52. FileOpen(int iDIB, LPSTR szFileName)
  53. {
  54.     BOOL        fOK = TRUE;    // function succeeded?
  55.     char        achFileName[_MAX_PATH]; // user-specified file name
  56.     int        fh = -1;    // DOS file handle
  57.     HCURSOR        hcurPrev = NULL; // cursor before hourglass
  58.     BITMAPINFOHEADER bih;        // information about this DIB
  59.     BITMAPINFOHEADER bihOther;    // information about other DIB
  60.  
  61.     /* put file name into <achFileName> (get from user or <szFileName>) */
  62.     if (szFileName == NULL)
  63.     {
  64.     /* prompt the user for the name of the file to open */
  65.     if (!PromptForFileName(ghwndApp, ghInst, achFileName,
  66.         sizeof(achFileName), IDS_OPEN, OPENFILEFILTER,
  67.         IDS_DEFEXT, PFFN_OPENFILE | PFFN_UPPERCASE))
  68.         goto RETURN_ERROR;
  69.     }
  70.     else
  71.     lstrcpy(achFileName, szFileName);
  72.  
  73.     /* show hourglass cursor */
  74.     hcurPrev = SetCursor(LoadCursor(NULL, IDC_WAIT));
  75.  
  76.     /* opening a new primary DIB clears the secondary DIB */
  77.     if (iDIB == PRIMARY_DIB)
  78.     FreeDIB(SECONDARY_DIB);
  79.     
  80.     /* free the old DIB */
  81.     FreeDIB(iDIB);
  82.  
  83.     /* read the DIB into memory */
  84.     if ((gahdib[iDIB] = OpenDIB(achFileName)) == NULL)
  85.     goto ERROR_OPENING;
  86.  
  87.     /* create a palette for the DIB */
  88.     if ((gahpal[iDIB] = CreateDibPalette(gahdib[iDIB])) == NULL)
  89.     goto ERROR_OPENING;
  90.     
  91.     /* set the DIB to be DIB_PAL_COLORS */
  92.     SetDibUsage(gahdib[iDIB], gahpal[iDIB], DIB_PAL_COLORS);
  93.  
  94.     /* get the size etc. of the bitmap */
  95.     DibInfo(gahdib[iDIB], &bih);
  96.  
  97.     if ((bih.biPlanes != 1) || (bih.biBitCount != 8))
  98.     goto ERROR_BADDIBTYPE;
  99.  
  100.     if (gahdib[!iDIB] != NULL)
  101.     {
  102.     /* other DIB is loaded -- make sure this DIB is the same
  103.      * size as the other DIB
  104.      */
  105.     DibInfo(gahdib[!iDIB], &bihOther);
  106.     if ((bih.biWidth != bihOther.biWidth) ||
  107.         (bih.biHeight != bihOther.biHeight))
  108.     goto ERROR_BADDIBSIZE;
  109.         
  110.     /* calculate a new merged DIB */
  111.     FreeDIB(MERGED_DIB);
  112.     PostMessage(ghwndApp, WM_COMMAND, IDM_VIEWMERGEDDIB, 0L);
  113.     }
  114.     else
  115.     {
  116.     /* display the DIB */
  117.     PostMessage(ghwndApp, WM_COMMAND,
  118.         IDM_VIEWPRIMARYDIB + iDIB, 0L);
  119.     }
  120.  
  121.     goto RETURN_SUCCESS;
  122.  
  123. ERROR_OPENING:                // display generic error message
  124. #ifdef IGNOREERRORS
  125.     ErrorResBox(ghwndApp, ghInst, MB_ICONEXCLAMATION | MB_OK, 
  126.     IDS_APPNAME, IDS_ERROROPEN, (LPSTR) achFileName);
  127. #endif        
  128.     goto RETURN_ERROR;
  129.  
  130. ERROR_BADDIBTYPE:            // bad type of DIB
  131.  
  132. #ifdef IGNOREERRORS
  133.     ErrorResBox(ghwndApp, ghInst, MB_ICONEXCLAMATION | MB_OK, 
  134.     IDS_APPNAME, IDS_BADDIBTYPE);
  135. #endif        
  136.     goto RETURN_ERROR;
  137.  
  138. ERROR_BADDIBSIZE:            // bad size of DIB
  139.  
  140. #ifdef IGNOREERRORS
  141.     ErrorResBox(ghwndApp, ghInst, MB_ICONEXCLAMATION | MB_OK, 
  142.     IDS_APPNAME, IDS_BADDIBSIZE);
  143. #endif        
  144.     goto RETURN_ERROR;
  145.  
  146. RETURN_ERROR:                // do error exit without error message
  147.  
  148.     fOK = FALSE;
  149.  
  150. RETURN_SUCCESS:                // normal exit
  151.  
  152.     if (hcurPrev != NULL)
  153.     SetCursor(hcurPrev);
  154.     
  155.     if (!fOK)
  156.     FreeDIB(iDIB);
  157.  
  158.     return fOK;
  159. }
  160.