File: fpl.c

package info (click to toggle)
fenix 0.92a.dfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 8,492 kB
  • sloc: ansic: 42,357; sh: 3,474; perl: 527; makefile: 180; cpp: 19
file content (236 lines) | stat: -rw-r--r-- 4,979 bytes parent folder | download | duplicates (4)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 *  Fenix - Videogame compiler/interpreter
 *  Current release       : FENIX - PROJECT 1.0 - R 0.84
 *  Last stable release   :
 *  Project documentation : http://fenix.divsite.net
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 *  Copyright � 1999 Jos� Luis Cebri�n Pag�e
 *  Copyright � 2002 Fenix Team
 *
 */

/*
 * FILE        : fbm.c
 * DESCRIPTION : Fenix Bitmap handling functions
 *
 * HISTORY:      0.85 - first version
 */

#include <string.h>
#include <stdlib.h>

#ifdef USE_GETTEXT
#include <libintl.h>
#define _(String) gettext (String)
#else
#define _(String) String
#endif

#include "fxi.h"
#include "fpl.h"

const char * fpl_error = "";

/*
 *  FUNCTION : fpl_load_from
 *
 *  Load a FPL file from an already opened file
 *
 *  PARAMS :
 *		fp				Pointer to the file object
 *		fgc_depth		Color depth if the graphic is part of a FGC file or 0 if it isn't
 *						Part of the header are supposed to not be there if this
 *						parameter is greater than 0 (see fbm.h for details)
 *
 *  RETURN VALUE :
 *		0 if error (fpl_error will have a description in this case)
 *		1 on SUCCESS
 *
 */

int fpl_load_from (file * fp)
{
	FPL_HEADER			 header;
	static unsigned char color_palette[768];
	int n ;

	// Read the header and check the file type and version

	if (file_read (fp, &header, sizeof(FPL_HEADER)) != sizeof(FPL_HEADER)) {
		fpl_error = _("Read error") ;
		return 0 ;
	}

    if (strncmp(header.magic, FPL_MAGIC, strlen(FPL_MAGIC)) == 0) {
		fpl_error = _("Invalid FPL file") ;
		return 0 ;
    }

	ARRANGE_DWORD(&header.depth);
	ARRANGE_DWORD(&header.version);

	if (!FPL_VALID_DEPTH(header.depth)) {
		fpl_error = _("Invalid colour depth in the FPL file") ;
		return 0 ;
	}

	if ((header.version & 0xFF00) != 0x0100) {
		fpl_error = _("FPL belongs to a later incompatible version");
		return 0;
	}

	if (file_read(fp, color_palette, 768) != 768) {
		fpl_error = _("Truncated FPL file");
		return 0;
	}

	for (n = 0 ; n < 256 ; n++) {
		palette[n].r = color_palette[3*n    ];
		palette[n].g = color_palette[3*n + 1];
		palette[n].b = color_palette[3*n + 2];
	}

	palette_loaded = 1 ;
	palette_changed = 1 ;

	return 1 ;

}


/*
 *  FUNCTION : fpl_save_to
 *
 *  Save palette data
 *
 *  PARAMS :
 *		fp				Pointer to the file object
 *
 *  RETURN VALUE :
 *      1 if no error, 0 otherwise (fpl_error will have a description in this case)
 *
 */

int fpl_save_to (file * fp)
{
	static char			color_palette[768];
	int n ;

	// Write the color palette

	for (n = 0 ; n < 256 ; n++)
	{
		color_palette[3*n    ] = palette[n].r;
		color_palette[3*n + 1] = palette[n].g;
		color_palette[3*n + 2] = palette[n].b;
	}

	if (file_write (fp, &color_palette, 768) != 768)
	{
		fbm_error = _("Error writing FPL file");
		return 0;
	}

	return 1;
}

/*
 *  FUNCTION : fpl_save
 *
 *  Save a palette to a new FPL file
 *
 *  PARAMS :
 *		filename		Name of the file to be created
 *
 *  RETURN VALUE :
 *      1 if no error, 0 otherwise (fpl_error will have a description in this case)
 *
 */

int fpl_save (const char * filename)
{
	FPL_HEADER	file_header;
	file *		fp;
	int			return_value;

	// Create the file

	fp = file_open (filename, "wb9");
	if (fp == NULL)
	{
		fpl_error = _("Error creating FPL file");
		return 0;
	}

	// Write the FPL file header

	strcpy (file_header.magic, FPL_MAGIC);
	file_header.depth = 8;
	file_header.version = 0x0100;

	ARRANGE_DWORD(&file_header.depth);
	ARRANGE_DWORD(&file_header.version);

	if (file_write (fp, &file_header, sizeof(FPL_HEADER)) != sizeof(FPL_HEADER))
	{
		fpl_error = _("Error writing FPL file");
		file_close (fp);
		return 0;
	}

	// Use fbm_save_to to save the rest of the data

	return_value = fpl_save_to(fp);
	file_close(fp);
	return return_value;
}

/*
 *  FUNCTION : fpl_load
 *
 *  Load a palette from a FPL file
 *
 *  PARAMS :
 *		filename		Name of the file to read
 *
 *  RETURN VALUE :
 *      1 if no error, 0 otherwise (fpl_error will have a description in this case)
 *
 */

int fpl_load (const char * filename)
{
	file *		fp;
	int			return_value;

	// Open the file

	fp = file_open (filename, "rb9");
	if (fp == NULL)
	{
		fpl_error = _("Error opening FPL file");
		return 0;
	}

	// Use fbm_load_from to load the rest of the data

	return_value = fpl_load_from(fp);
	file_close(fp);
	return return_value;
}