Annotation of mstools/samples/sdktools/imagedit/rwpal.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.