summaryrefslogtreecommitdiffstats
path: root/private/sdktools/imagedit/rwpal.c
blob: c28636781ae3e096dbc94468e72a2792db93d75d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/****************************************************************************/
/*                                                                          */
/*                         Microsoft Confidential                           */
/*                                                                          */
/*                 Copyright (c) Microsoft Corp.  1987, 1991                */
/*                           All Rights Reserved                            */
/*                                                                          */
/****************************************************************************/
/****************************** Module Header *******************************
* Module Name: rwpal.c
*
* Routines for reading and writing color palette files.
*
* History:
*
****************************************************************************/

#include "imagedit.h"

#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <sys\types.h>                      // For fstat() types.
#include <sys\stat.h>                       // For fstat() function.


/*
 * The color palette is saved in a .PAL file.  This file consists
 * of a header followed by the colors.
 *
 * The header has the following format:
 *
 * struct {
 *     CHAR tag;                // Always 'C'.
 *     WORD colors;             // Number of colors.  Always COLORSMAX.
 *     CHAR reserved[47];       // Reserved bytes.
 * }
 *
 * Immediately following this is RGB quads for each of the colors in
 * the palette.
 */


/*
 * Size in bytes of the header of a color palette file.
 */
#define CBCOLORHDR          (sizeof(CHAR) + sizeof(WORD) + 47)

/*
 * Size in bytes of the color information in the color file.
 */
#define CBCOLORINFO         (sizeof(DWORD) * COLORSMAX)

/*
 * Size in bytes of a color palette file.  This includes the
 * size of the header and room for all the colors.
 */
#define CBCOLORFILE         (CBCOLORHDR + CBCOLORINFO)



/************************************************************************
* LoadColorFile
*
*
*
* Arguments:
*
* History:
*
************************************************************************/

VOID LoadColorFile(VOID)
{
    HFILE hf;
    OFSTRUCT OfStruct;
    struct _stat FileStatus;
    DWORD argb[COLORSMAX];
    UINT cbRead;
    INT i;
    CHAR tag;
    CHAR szFileName[CCHMAXPATH];

    *szFileName = '\0';
    if (!OpenDlg(szFileName, FT_PALETTE))
        return;

    if ((hf = MOpenFile(szFileName, (LPOFSTRUCT)&OfStruct, OF_READ))
            == (HFILE)-1) {
        Message(MSG_CANTOPEN, szFileName);
        return;
    }

    _fstat(HFILE2INT(hf, O_RDONLY), &FileStatus);

    if (FileStatus.st_size != CBCOLORFILE) {
        Message(MSG_BADPALFILE, szFileName);
        goto Error1;
    }

    if ((cbRead = M_lread(hf, &tag, 1)) == -1 || cbRead != 1) {
        Message(MSG_READERROR, szFileName);
        goto Error1;
    }

    if (tag != 'C') {
        Message(MSG_BADPALFILE, szFileName);
        goto Error1;
    }

    M_llseek(hf, CBCOLORHDR, 0);
    if ((cbRead = M_lread(hf, (LPSTR)argb, CBCOLORINFO)) == -1 ||
            cbRead != CBCOLORINFO) {
        Message(MSG_READERROR, szFileName);
        goto Error1;
    }

    for (i = 0; i < COLORSMAX; i++)
        gargbColor[i] = argb[i];

    SetColorPalette(16, giType, TRUE);

Error1:
    M_lclose(hf);
}



/************************************************************************
* SaveColorFile
*
*
*
* Arguments:
*
* History:
*
************************************************************************/

VOID SaveColorFile(VOID)
{
    INT i;
    HFILE hf;
    OFSTRUCT OfStruct;
    CHAR reserved[47];
    WORD wColors = COLORSMAX;
    CHAR tag = 'C';
    CHAR szFileName[CCHMAXPATH];

    *szFileName = '\0';
    if (!SaveAsDlg(szFileName, FT_PALETTE))
        return;

    if ((hf = MOpenFile(szFileName, &OfStruct,
            OF_CREATE | OF_WRITE)) == (HFILE)-1) {
        Message(MSG_CANTCREATE, szFileName);
        return;
    }

    for (i = 0; i < sizeof(reserved); i++)
        reserved[i] = 0;

    if (M_lwrite(hf, (LPSTR)&tag, sizeof(tag)) != sizeof(tag) ||
            M_lwrite(hf, (LPSTR)&wColors, sizeof(wColors)) !=
            sizeof(wColors) ||
            M_lwrite(hf, (LPSTR)reserved, sizeof(reserved)) !=
            sizeof(reserved) ||
            M_lwrite(hf, (LPSTR)gargbColor, CBCOLORINFO) != CBCOLORINFO) {
        Message(MSG_WRITEERROR, szFileName);
    }

    M_lclose(hf);
}