home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 11 / 11.iso / m / m248 / 4.ddi / PALUCD.C_ / PALUCD.C
Encoding:
C/C++ Source or Header  |  1993-02-01  |  7.4 KB  |  298 lines

  1. /*
  2.   User code function that changes the Authorware palette at runtime.
  3.  
  4.   Copyright 1987-1992 Macromedia, Inc.
  5.  
  6.   Revision History:
  7.      7/25/91  -  Initial version
  8.     11/29/92  -  Add loading palettes from DIB files.
  9.  
  10.   This file defines two user code functions:
  11.  
  12.   1. error := SetPalette(WindowHandle, <palettefile>)
  13.   
  14.      Sets the Authorware palette to the one in <palettefile>.  The file
  15.      must be a RIFF palette file (.PAL) as created by the Microsoft
  16.      PalEdit application, or a DIB or BMP file.  In the case of DIB or
  17.      BMP files, the palette is extracted and the image information 
  18.      is ignored.
  19.  
  20.      The first argument is the handle of the Authorware presentation 
  21.      window contained in the WindowHandle system variable.
  22.  
  23.      Returns:
  24.         0 if the palette was loaded
  25.         1 if the window handle was invalid
  26.         2 if insufficient memory to load the palette
  27.         3 if the file was not found
  28.         4 if the file was an unsupported type or an I/O error occurred
  29.  
  30.   2. error := ResetPalette(WindowHandle)
  31.  
  32.      Resets the Authorware palette to the default (as loaded in File Setup).
  33.  
  34.      The argument is the handle of the Authorware presentation 
  35.      window contained in the WindowHandle system variable.
  36.  
  37.      Returns:
  38.         0 if the palette was reset
  39.         1 if the window handle was invalid
  40.  
  41.  
  42.   Notes:
  43.      Setting and Reseting the palette is done by sending custom messages 
  44.      to the Authorware presentation window.  These messages are:
  45.  
  46.         APWC_SETPAL    -  set the palette
  47.         APWC_RESETPAL  -  reset the palette
  48. */
  49.  
  50. #define WINVER 0x0300
  51. #include <windows.h>
  52. #include <mmsystem.h>
  53. #include "apwapi.h"
  54.  
  55.  
  56. //   Error codes returned by these functions.
  57.  
  58. #define PAL_OK         0     // palette loaded/reset ok
  59. #define PAL_BADWIND    1     // bad window handle
  60. #define PAL_NOMEM      2     // not enough memory
  61. #define PAL_NOFILE     3     // specified file not found
  62. #define PAL_BADTYPE    4     // unsupported file type, bad file, or I/O error
  63.  
  64.  
  65. typedef struct {       // RIFF PAL file header
  66.   FOURCC   riffcc;
  67.   DWORD    riffsize;
  68.   FOURCC   palform;
  69.   FOURCC   datacc;
  70.   DWORD    datasize;
  71. } RIFFHEADER;
  72.  
  73. #define FOURCC_PAL     mmioFOURCC('P', 'A', 'L', ' ')
  74. #define FOURCC_DATA    mmioFOURCC('d', 'a', 't', 'a')
  75.  
  76. typedef struct {             // LOGPALETTE with 256 entries
  77.   LOGPALETTE     lp;         // includes one entry
  78.   PALETTEENTRY   extra[255]; // space for 255 more
  79. } LOGPAL256;
  80.  
  81.  
  82.  
  83. static int LoadRiffPalette( HANDLE fh, HPALETTE FAR *lphPal );
  84. static int LoadDibPalette( HANDLE fh, HPALETTE FAR *lphPal );
  85. static unsigned DibNumColors( LPBITMAPINFOHEADER lpbi );
  86.  
  87.  
  88.  
  89. short   FAR PASCAL LibMain( HANDLE hModule, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine )
  90. /*
  91.   DLL startup routine.  Nothing to do.
  92. */
  93. {
  94.   return 1;
  95. }
  96.  
  97.  
  98.  
  99. short FAR PASCAL WEP( short bSystemExit )
  100. /*
  101.   DLL cleanup routine. Nothing to do.
  102. */
  103. {
  104.   return 1;
  105. }
  106.  
  107.  
  108.  
  109. short FAR PASCAL SetPalette( HWND hWnd, LPSTR szFileName )
  110. /*
  111.   Changes the Authorware palette to the one found in the specified file.
  112.   Returns an error code as described above.
  113.  
  114.   Parameters:
  115.      hWnd        Authorware presentation window handle
  116.      szFileName  Name of a RIFF palette, DIB or BMP file.
  117. */
  118. {
  119.   short    rv;
  120.   HPALETTE hPal;
  121.   OFSTRUCT of;
  122.   HANDLE   fh;
  123.  
  124.   if (hWnd == NULL || !IsWindow(hWnd))
  125.      rv = PAL_BADWIND;
  126.  
  127.   // Open the file
  128.   else if ((fh = OpenFile(szFileName, &of, OF_READ)) == -1)
  129.      rv = PAL_NOFILE;
  130.  
  131.   else
  132.   {
  133.      // First try to load as a RIFF palette.
  134.      rv = LoadRiffPalette(fh, &hPal);
  135.  
  136.      // If it's not a RIFF palette, see if it's a DIB
  137.      if (rv == PAL_BADTYPE)
  138.         rv = LoadDibPalette(fh, &hPal);
  139.  
  140.      // Give palette to APW
  141.      if (rv == PAL_OK)
  142.           if (!APWSetPalette(hWnd, hPal, SP_DEFAULT))
  143.            rv = PAL_NOMEM;
  144.  
  145.      _lclose(fh);
  146.   }
  147.  
  148.   return rv;
  149. }
  150.  
  151.  
  152.  
  153. BOOL FAR PASCAL ResetPalette( HWND hWnd )
  154. /*
  155.   Restores the default course palette as loaded in File Setup.
  156.   Returns an error code as described above.
  157. */
  158. {
  159.   if (hWnd == NULL || !IsWindow(hWnd))
  160.      return PAL_BADWIND;
  161.  
  162.   APWResetPalette(hWnd);
  163.   return PAL_OK;
  164. }
  165.  
  166.  
  167.  
  168. static int LoadRiffPalette( HANDLE fh, HPALETTE FAR *lphPal )
  169. /*
  170.   This function extracts the palette from a RIFF PAL file.
  171.  
  172.   Returns:
  173.      PAL_OK      - palette handle in *lphPal
  174.      PAL_NOMEM   - insufficient memory
  175.      PAL_BADTYPE - not a RIFF PAL
  176.  
  177.   In RIFF notation, a RIFF palette file is defined as:
  178.  
  179.      RIFF ( 'PAL' data(<palette:LOGPALETTE>) )
  180. */
  181. {
  182.   LOGPAL256   logPal;
  183.   RIFFHEADER  riff;
  184.  
  185.   *lphPal = NULL;
  186.  
  187.   // read header
  188.   if (_lread(fh, (LPSTR)&riff, sizeof(riff)) != sizeof(riff))
  189.      return PAL_BADTYPE;
  190.   if (riff.riffcc != FOURCC_RIFF || riff.palform != FOURCC_PAL
  191.         || riff.datacc != FOURCC_DATA)
  192.      return PAL_BADTYPE;
  193.  
  194.  
  195.   // read as much data as is present, but at most 256 entries
  196.   _lread(fh, (LPSTR)&logPal, sizeof(LOGPALETTE) + 255*sizeof(PALETTEENTRY));
  197.  
  198.   // create the palette and we're done
  199.   *lphPal = CreatePalette(&logPal.lp);
  200.   if (*lphPal == NULL)
  201.      return PAL_NOMEM;
  202.   else
  203.      return PAL_OK;
  204. }
  205.  
  206.  
  207.  
  208. static int LoadDibPalette( HANDLE fh, HPALETTE FAR *lphPal )
  209. /*
  210.   This function extracts the palette from a DIB file.
  211.  
  212.   Returns:
  213.      PAL_OK      - palette handle in *lphPal
  214.      PAL_NOMEM   - insufficient memory
  215.      PAL_BADTYPE - not a dib file or no palette
  216. */
  217. {
  218.   BITMAPFILEHEADER  fhead;
  219.   BITMAPINFOHEADER  bmi;
  220.   LOGPAL256         logPal;
  221.   unsigned          palEntries;
  222.   unsigned          palSize;
  223.   unsigned          i;
  224.   BYTE              blue;
  225.  
  226.   *lphPal = NULL;
  227.  
  228.   // read the file header & check file type
  229.   if (_llseek(fh, 0, 0) == -1)        
  230.      return PAL_BADTYPE;
  231.   if (_lread(fh, (LPSTR)&fhead, sizeof(fhead)) < sizeof(fhead))
  232.      return PAL_BADTYPE;
  233.   if (fhead.bfType != 0x4d42)         
  234.      return PAL_BADTYPE;
  235.  
  236.   // read the bitmap header & make sure it's a Windows DIB (not OS/2)
  237.   if (_lread(fh, (LPSTR)&bmi, sizeof(bmi)) < sizeof(bmi))
  238.      return PAL_BADTYPE;
  239.   if (bmi.biSize != sizeof(bmi))
  240.      return PAL_BADTYPE;
  241.  
  242.   // read the palette
  243.   palEntries = DibNumColors(&bmi);
  244.   if (palEntries == 0)
  245.      return PAL_BADTYPE;
  246.   palSize = palEntries * sizeof(RGBQUAD);
  247.   if (_lread(fh, (LPSTR)(logPal.lp.palPalEntry), palSize) < palSize)
  248.      return PAL_BADTYPE;
  249.  
  250.   logPal.lp.palVersion = 0x300;
  251.   logPal.lp.palNumEntries = palEntries;
  252.  
  253.   // we just wrote the DIB palette, consisting of RGBQUADs into a
  254.   // log palette, consisting of PALETTEENTRYs so convert from quads
  255.   // to pal entries by exchanging red & blue.
  256.  
  257.   for (i = 0; i < palEntries; i++)
  258.   {
  259.      blue = logPal.lp.palPalEntry[i].peRed;
  260.      logPal.lp.palPalEntry[i].peRed = logPal.lp.palPalEntry[i].peBlue;
  261.      logPal.lp.palPalEntry[i].peBlue = blue;
  262.   }
  263.  
  264.   // create the palette and we're done
  265.   *lphPal = CreatePalette(&logPal.lp);
  266.   if (*lphPal == NULL)
  267.      return PAL_NOMEM;
  268.   else
  269.      return PAL_OK;
  270. }
  271.  
  272.  
  273.  
  274. static unsigned DibNumColors( LPBITMAPINFOHEADER lpbi )
  275. /*
  276.    Returns the # of entries in the DIB's color table.
  277. */
  278. {
  279.   if (lpbi->biClrUsed != 0)
  280.      return (int)(lpbi->biClrUsed);
  281.  
  282.   else
  283.   {
  284.      switch (lpbi->biBitCount)
  285.      {
  286.      case 1:
  287.         return 2;
  288.      case 4:
  289.         return 16;
  290.      case 8:
  291.         return 256;
  292.      default:
  293.         /* A 24 bit DIB has no color table */
  294.         return 0;
  295.      }
  296.   }
  297. }
  298.