1 /*
2 * mkpalette.cc
3 * Generate palette files from lump PLAYPAL.
4 * AYM 1998-12-29
5 */
6
7
8 /*
9 This file is part of Yadex.
10
11 Yadex incorporates code from DEU 5.21 that was put in the public domain in
12 1994 by Rapha�l Quinet and Brendon Wyber.
13
14 The rest of Yadex is Copyright � 1997-2003 Andr� Majorel and others.
15
16 This program is free software; you can redistribute it and/or modify it under
17 the terms of the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any later
19 version.
20
21 This program is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License along with
26 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
27 Place, Suite 330, Boston, MA 02111-1307, USA.
28 */
29
30
31 #include "yadex.h"
32 #include "mkpalette.h"
33 #include "gfx.h"
34 #include "rgb.h"
35 #include "wadfile.h"
36 #include "wads.h"
37
38
39 /*
40 * make_gimp_palette
41 * Generate a Gimp palette file for the <playpalnum>th
42 * palette in the PLAYPAL entry.
43 * Return 0 on success, non-zero on failure.
44 */
make_gimp_palette(int playpalnum,const char * filename)45 int make_gimp_palette (int playpalnum, const char *filename)
46 {
47 int rc = 0;
48 MDirPtr dir;
49 u8 *dpal = 0;
50 FILE *output_fp = 0;
51
52 const char *lump_name = "PLAYPAL";
53 dir = FindMasterDir (MasterDir, lump_name);
54 if (dir == 0)
55 {
56 warn ("%s: lump not found\n",lump_name);
57 return 1;
58 }
59
60 int playpal_count = dir->dir.size / (3 * DOOM_COLOURS);
61 if (playpalnum < 0 || playpalnum >= playpal_count)
62 {
63 warn ("playpalnum %d out of range (0-%d), using #0 instead\n",
64 playpalnum, playpal_count - 1);
65 playpalnum = 0;
66 }
67
68 output_fp = fopen (filename, "w");
69 if (output_fp == 0)
70 {
71 warn ("%s: %s\n", filename, strerror (errno));
72 return 1;
73 }
74 fprintf (output_fp,
75 "GIMP Palette\n"
76 "# Generated by Yadex %s\n", yadex_version);
77
78 dpal = (u8 *) GetFarMemory (3 * DOOM_COLOURS);
79 const Wad_file *wf = dir->wadfile;
80 wf->seek (dir->dir.start + (long) playpalnum * 3 * DOOM_COLOURS);
81 if (wf->error ())
82 {
83 err ("%s: seek error", lump_name);
84 rc = 1;
85 goto byebye;
86 }
87 wf->read_bytes (dpal, 3 * DOOM_COLOURS);
88 if (wf->error ())
89 {
90 err ("%s: read error", lump_name);
91 rc = 1;
92 goto byebye;
93 }
94 for (size_t n = 0; n < DOOM_COLOURS; n++)
95 fprintf (output_fp, "%3d %3d %3d Index = %d (%02Xh) RGB = %d, %d, %d\n",
96 dpal[3 * n],
97 dpal[3 * n + 1],
98 dpal[3 * n + 2],
99 n,
100 n,
101 dpal[3 * n],
102 dpal[3 * n + 1],
103 dpal[3 * n + 2]);
104
105 byebye:
106 if (dpal != 0)
107 FreeFarMemory (dpal);
108 if (output_fp != 0)
109 if (fclose (output_fp))
110 return 1;
111 return rc;
112 }
113
114
115 /*
116 * make_palette_ppm
117 * Generate a 256 x 128 raw PPM image showing all the
118 * colours in the palette.
119 * Return 0 on success, non-zero on failure.
120 */
make_palette_ppm(int playpalnum,const char * filename)121 int make_palette_ppm (int playpalnum, const char *filename)
122 {
123 int rc = 0;
124 MDirPtr dir;
125 u8 *dpal = 0;
126 FILE *output_fp = 0;
127
128 const char *lump_name = "PLAYPAL";
129 dir = FindMasterDir (MasterDir, lump_name);
130 if (dir == 0)
131 {
132 warn ("%s: lump not found\n", lump_name);
133 return 1;
134 }
135
136 int playpal_count = dir->dir.size / (3 * DOOM_COLOURS);
137 if (playpalnum < 0 || playpalnum >= playpal_count)
138 {
139 warn ("playpalnum %d out of range (0-%d), using #0 instead\n",
140 playpalnum, playpal_count - 1);
141 playpalnum = 0;
142 }
143
144 output_fp = fopen (filename, "wb");
145 if (output_fp == 0)
146 {
147 warn ("%s: %s\n", filename, strerror (errno));
148 return 1;
149 }
150
151 const int width = 128;
152 const int height = 128;
153 const int columns = 16;
154
155 fputs ("P6", output_fp);
156 fnewline (output_fp);
157 fprintf (output_fp, "# Generated by Yadex %s", yadex_version);
158 fnewline (output_fp);
159 fprintf (output_fp, "%d %d", width, height);
160 fnewline (output_fp);
161 fputs ("255\n", output_fp); // Always \n (must be a single character)
162
163 int rect_w = width / columns;
164 int rect_h = height / (DOOM_COLOURS / columns);
165
166 dpal = (u8 *) GetFarMemory (3 * DOOM_COLOURS);
167 const Wad_file *wf = dir->wadfile;
168 wf->seek (dir->dir.start + (long) playpalnum * 3 * DOOM_COLOURS);
169 if (wf->error ())
170 {
171 err ("%s: seek error", lump_name);
172 rc = 1;
173 goto byebye;
174 }
175 wf->read_bytes (dpal, 3 * DOOM_COLOURS);
176 if (wf->error ())
177 {
178 err ("%s: read error", lump_name);
179 rc = 1;
180 goto byebye;
181 }
182 for (size_t n = 0; n < DOOM_COLOURS; n += columns)
183 for (int subrow = 0; subrow < rect_h; subrow++)
184 for (int c = 0; c < columns; c++)
185 for (int subcol = 0; subcol < rect_w; subcol++)
186 {
187 if (subrow == 0 && subcol == 0)
188 {
189 putc (0, output_fp);
190 putc (0, output_fp);
191 putc (0, output_fp);
192 }
193 else
194 {
195 putc (dpal[3 * (n + c)], output_fp);
196 putc (dpal[3 * (n + c) + 1], output_fp);
197 putc (dpal[3 * (n + c) + 2], output_fp);
198 }
199 }
200
201 byebye:
202 if (dpal != 0)
203 FreeFarMemory (dpal);
204 if (output_fp != 0)
205 if (fclose (output_fp))
206 return 1;
207 return rc;
208 }
209
210
211 /*
212 * make_palette_ppm_2
213 * Make a wide PPM containing all the colours in the palette
214 */
215
make_palette_ppm_2(int playpalnum,const char * filename)216 int make_palette_ppm_2 (int playpalnum, const char *filename)
217 {
218 int rc = 0;
219 MDirPtr dir;
220 u8 *dpal = 0;
221 FILE *output_fp = 0;
222
223 const char *lump_name = "PLAYPAL";
224 dir = FindMasterDir (MasterDir, lump_name);
225 if (dir == 0)
226 {
227 warn ("%s: lump not found", lump_name);
228 return 1;
229 }
230
231 int playpal_count = dir->dir.size / (3 * DOOM_COLOURS);
232 if (playpalnum < 0 || playpalnum >= playpal_count)
233 {
234 warn ("playpalnum %d out of range (0-%d), using #0 instead",
235 playpalnum, playpal_count - 1);
236 playpalnum = 0;
237 }
238
239 output_fp = fopen (filename, "wb");
240 if (output_fp == 0)
241 {
242 warn ("%s: %s\n", filename, strerror (errno));
243 return 1;
244 }
245
246 const int width = DOOM_COLOURS;
247 const int height = DOOM_COLOURS;
248
249 fputs ("P6", output_fp);
250 fnewline (output_fp);
251 fprintf (output_fp, "# Generated by Yadex %s", yadex_version);
252 fnewline (output_fp);
253 fprintf (output_fp, "%d %d", width, height);
254 fnewline (output_fp);
255 fputs ("255\n", output_fp); // Always \n (must be a single character)
256
257 dpal = (u8 *) GetFarMemory (3 * DOOM_COLOURS);
258 const Wad_file *wf = dir->wadfile;
259 wf->seek (dir->dir.start + (long) playpalnum * 3 * DOOM_COLOURS);
260 if (wf->error ())
261 {
262 err ("%s: seek error", lump_name);
263 rc = 1;
264 goto byebye;
265 }
266 wf->read_bytes (dpal, 3 * DOOM_COLOURS);
267 if (wf->error ())
268 {
269 err ("%s: read error", lump_name);
270 rc = 1;
271 goto byebye;
272 }
273 for (int l = 0; l < height; l++)
274 for (int c = 0; c < width; c++)
275 {
276 putc (dpal[3 * ((c + l) % DOOM_COLOURS) ], output_fp);
277 putc (dpal[3 * ((c + l) % DOOM_COLOURS) + 1], output_fp);
278 putc (dpal[3 * ((c + l) % DOOM_COLOURS) + 2], output_fp);
279 }
280
281 byebye:
282 if (dpal != 0)
283 FreeFarMemory (dpal);
284 if (output_fp != 0)
285 if (fclose (output_fp))
286 return 1;
287 return rc;
288 }
289
290
291