home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / imagedit / rwpal.c < prev    next >
Text File  |  1996-06-12  |  5KB  |  172 lines

  1.     /****************************************************************************/
  2.     /*                                                                          */
  3.     /*                 Copyright (C) 1987-1996 Microsoft Corp.                */
  4.     /*                           All Rights Reserved                            */
  5.     /*                                                                          */
  6.     /****************************************************************************/
  7.     /****************************** Module Header *******************************
  8.     * Module Name: rwpal.c
  9.     *
  10.     * Routines for reading and writing color palette files.
  11.     *
  12.     * History:
  13.     *
  14.     ****************************************************************************/
  15.     
  16.     #include "imagedit.h"
  17.     
  18.     #include <stdio.h>
  19.     #include <fcntl.h>
  20.     #include <io.h>
  21.     #include <sys\types.h>                      // For fstat() types.
  22.     #include <sys\stat.h>                       // For fstat() function.
  23.     
  24.     
  25.     /*
  26.      * The color palette is saved in a .PAL file.  This file consists
  27.      * of a header followed by the colors.
  28.      *
  29.      * The header has the following format:
  30.      *
  31.      * struct {
  32.      *     CHAR tag;                // Always 'C'.
  33.      *     WORD colors;             // Number of colors.  Always COLORSMAX.
  34.      *     CHAR reserved[47];       // Reserved bytes.
  35.      * }
  36.      *
  37.      * Immediately following this is RGB quads for each of the colors in
  38.      * the palette.
  39.      */
  40.     
  41.     
  42.     /*
  43.      * Size in bytes of the header of a color palette file.
  44.      */
  45.     #define CBCOLORHDR          (sizeof(CHAR) + sizeof(WORD) + 47)
  46.     
  47.     /*
  48.      * Size in bytes of the color information in the color file.
  49.      */
  50.     #define CBCOLORINFO         (sizeof(DWORD) * COLORSMAX)
  51.     
  52.     /*
  53.      * Size in bytes of a color palette file.  This includes the
  54.      * size of the header and room for all the colors.
  55.      */
  56.     #define CBCOLORFILE         (CBCOLORHDR + CBCOLORINFO)
  57.     
  58.     
  59.     
  60.     /************************************************************************
  61.     * LoadColorFile
  62.     *
  63.     *
  64.     *
  65.     * Arguments:
  66.     *
  67.     * History:
  68.     *
  69.     ************************************************************************/
  70.     
  71.     VOID LoadColorFile(VOID)
  72.     {
  73.         HFILE hf;
  74.         OFSTRUCT OfStruct;
  75.         struct stat FileStatus;
  76.         DWORD argb[COLORSMAX];
  77.         UINT cbRead;
  78.         INT i;
  79.         CHAR tag;
  80.         CHAR szFileName[CCHMAXPATH];
  81.     
  82.         *szFileName = '\0';
  83.         if (!OpenDlg(szFileName, FT_PALETTE))
  84.             return;
  85.     
  86.           if ((hf = (HFILE)OpenFile(szFileName, (LPOFSTRUCT)&OfStruct, OF_READ))
  87.                 == (HFILE)-1) {
  88.             Message(MSG_CANTOPEN, szFileName);
  89.             return;
  90.         }
  91.     
  92.         fstat((INT)_open_osfhandle((long)(hf), (int)(O_RDONLY)), &FileStatus);
  93.     
  94.         if (FileStatus.st_size != CBCOLORFILE) {
  95.             Message(MSG_BADPALFILE, szFileName);
  96.             goto Error1;
  97.         }
  98.     
  99.         if ((cbRead = _lread((HFILE)hf, &tag, 1)) == -1 || cbRead != 1) {
  100.             Message(MSG_READERROR, szFileName);
  101.             goto Error1;
  102.         }
  103.     
  104.         if (tag != 'C') {
  105.             Message(MSG_BADPALFILE, szFileName);
  106.             goto Error1;
  107.         }
  108.     
  109.         SetFilePointer((HANDLE)hf, CBCOLORHDR, NULL, (DWORD)0);
  110.         if ((cbRead = _lread((HFILE)hf, (LPSTR)argb, CBCOLORINFO)) == -1 ||
  111.                 cbRead != CBCOLORINFO) {
  112.             Message(MSG_READERROR, szFileName);
  113.             goto Error1;
  114.         }
  115.     
  116.         for (i = 0; i < COLORSMAX; i++)
  117.             gargbColor[i] = argb[i];
  118.     
  119.         SetColorPalette(16, giType, TRUE);
  120.     
  121.     Error1:
  122.         _lclose((HFILE)hf);
  123.     }
  124.     
  125.     
  126.     
  127.     /************************************************************************
  128.     * SaveColorFile
  129.     *
  130.     *
  131.     *
  132.     * Arguments:
  133.     *
  134.     * History:
  135.     *
  136.     ************************************************************************/
  137.     
  138.     VOID SaveColorFile(VOID)
  139.     {
  140.         INT i;
  141.         HFILE hf;
  142.         OFSTRUCT OfStruct;
  143.         CHAR reserved[47];
  144.         WORD wColors = COLORSMAX;
  145.         CHAR tag = 'C';
  146.         CHAR szFileName[CCHMAXPATH];
  147.     
  148.         *szFileName = '\0';
  149.         if (!SaveAsDlg(szFileName, FT_PALETTE))
  150.             return;
  151.     
  152.         if ((hf = (HFILE)OpenFile(szFileName, &OfStruct,
  153.                 OF_CREATE | OF_WRITE)) == (HFILE)-1) {
  154.             Message(MSG_CANTCREATE, szFileName);
  155.             return;
  156.         }
  157.     
  158.         for (i = 0; i < sizeof(reserved); i++)
  159.             reserved[i] = 0;
  160.     
  161.         if (_lwrite((HFILE)hf, (LPSTR)&tag, sizeof(tag)) != sizeof(tag) ||
  162.                 _lwrite((HFILE)hf, (LPSTR)&wColors, sizeof(wColors)) !=
  163.                 sizeof(wColors) ||
  164.                 _lwrite((HFILE)hf, (LPSTR)reserved, sizeof(reserved)) !=
  165.                 sizeof(reserved) ||
  166.                 _lwrite((HFILE)hf, (LPSTR)gargbColor, CBCOLORINFO) != CBCOLORINFO) {
  167.             Message(MSG_WRITEERROR, szFileName);
  168.         }
  169.     
  170.         _lclose((HFILE)hf);
  171.     }
  172.