about summary refs log tree commit diff
path: root/urt/rle_hdr.c
blob: 1611324c8ed978b3eca89f56aaac5fab4ea98e2d (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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * This software is copyrighted as noted below.  It may be freely copied,
 * modified, and redistributed, provided that the copyright notice is 
 * preserved on all copies.
 * 
 * There is no warranty or other guarantee of fitness for this software,
 * it is provided solely "as is".  Bug reports or fixes may be sent
 * to the author, who may or may not act on them as he desires.
 *
 * You may not include this software in a program or other software product
 * without supplying the source, or without informing the end-user that the 
 * source is available for no extra charge.
 *
 * If you modify this software, you should include a notice giving the
 * name of the person performing the modification, the date of modification,
 * and the reason for such modification.
 */
/* 
 * rle_hdr.c - Functions to manipulate rle_hdr structures.
 * 
 * Author:	Spencer W. Thomas
 * 		EECS Dept.
 * 		University of Michigan
 * Date:	Mon May 20 1991
 * Copyright (c) 1991, University of Michigan
 */

#include <string.h>

#include "rle_config.h"
#include "rle.h"

/*****************************************************************
 * TAG( rle_names )
 *
 * Load program and file names into header.
 * Inputs:
 * 	the_hdr:	Header to modify.
 * 	pgmname:	The program name.
 * 	fname:		The file name.
 * 	img_num:	Number of the image within the file.
 * Outputs:
 * 	the_hdr:	Modified header.
 * Algorithm:
 * 	If values previously filled in (by testing is_init field),
 * 	free them.  Make copies of file name and program name,
 * 	modifying file name for standard i/o.  Set is_init field.
 */
void
rle_names( the_hdr, pgmname, fname, img_num )
rle_hdr *the_hdr;
CONST_DECL char *pgmname;
CONST_DECL char *fname;
int img_num;
{
#if 0
    /* Can't do this because people do hdr1 = hdr2, which copies
       the pointers. */

    /* If filled in, free previous values. */
    if ( the_hdr->is_init == RLE_INIT_MAGIC &&
	 the_hdr->cmd != NULL && the_hdr->file_name != NULL )
    {
	if ( pgmname != the_hdr->cmd )
	    free( the_hdr->cmd );
	if ( fname != the_hdr->file_name )
	    free( the_hdr->file_name );
    }
#endif

    /* Mark as filled in. */
    the_hdr->is_init = RLE_INIT_MAGIC;

    /* Default file name for stdin/stdout. */
    if ( fname == NULL || strcmp( fname, "-" ) == 0 || *fname == '\0' )
	fname = "Standard I/O";
    if ( pgmname == NULL )
	pgmname = rle_dflt_hdr.cmd;

    /* Fill in with copies of the strings. */
    if ( the_hdr->cmd != pgmname )
    {
	char *tmp = (char *)malloc( strlen( pgmname ) + 1 );
	RLE_CHECK_ALLOC( pgmname, tmp, 0 );
	strcpy( tmp, pgmname );
	the_hdr->cmd = tmp;
    }

    if ( the_hdr->file_name != fname )
    {
	char *tmp = (char *)malloc( strlen( fname ) + 1 );
	RLE_CHECK_ALLOC( pgmname, tmp, 0 );
	strcpy( tmp, fname );
	the_hdr->file_name = tmp;
    }

    the_hdr->img_num = img_num;
}


/* Used by rle_hdr_cp and rle_hdr_init to avoid recursion loops. */
static int no_recurse = 0;

/*****************************************************************
 * TAG( rle_hdr_cp )
 * 
 * Make a "safe" copy of a rle_hdr structure.
 * Inputs:
 * 	from_hdr:	Header to be copied.
 * Outputs:
 * 	to_hdr:		Copy of from_hdr, with all memory referred to
 * 			by pointers copied.  Also returned as function
 * 			value.  If NULL, a static header is used.
 * Assumptions:
 * 	It is safe to call rle_hdr_init on to_hdr.
 * Algorithm:
 * 	Initialize to_hdr, copy from_hdr to it, then copy the memory
 * 	referred to by all non-null pointers.
 */
rle_hdr *
rle_hdr_cp( from_hdr, to_hdr )
rle_hdr *from_hdr, *to_hdr;
{
    static rle_hdr dflt_hdr;
    CONST_DECL char *cmd, *file;
    int num;

    /* Save command, file name, and image number if already initialized. */
    if ( to_hdr &&  to_hdr->is_init == RLE_INIT_MAGIC )
    {
	cmd = to_hdr->cmd;
	file = to_hdr->file_name;
	num = to_hdr->img_num;
    }
    else
    {
	cmd = file = NULL;
	num = 0;
    }

    if ( !no_recurse )
    {
	no_recurse++;
	rle_hdr_init( to_hdr );
	no_recurse--;
    }

    if ( to_hdr == NULL )
	to_hdr = &dflt_hdr;

    *to_hdr = *from_hdr;

    if ( to_hdr->bg_color )
    {
	int size = to_hdr->ncolors * sizeof(int);
	to_hdr->bg_color = (int *)malloc( size );
	RLE_CHECK_ALLOC( to_hdr->cmd, to_hdr->bg_color, "background color" );
	memcpy( to_hdr->bg_color, from_hdr->bg_color, size );
    }

    if ( to_hdr->cmap )
    {
	int size = to_hdr->ncmap * (1 << to_hdr->cmaplen) * sizeof(rle_map);
	to_hdr->cmap = (rle_map *)malloc( size );
	RLE_CHECK_ALLOC( to_hdr->cmd, to_hdr->cmap, "color map" );
	memcpy( to_hdr->cmap, from_hdr->cmap, size );
    }

    /* Only copy array of pointers, as the original comment memory
     * never gets overwritten.
     */
    if ( to_hdr->comments )
    {
	int size = 0;
	CONST_DECL char **cp;
	for ( cp=to_hdr->comments; *cp; cp++ )
	    size++;		/* Count the comments. */
	/* Check if there are really any comments. */
	if ( size )
	{
	    size++;		/* Copy the NULL pointer, too. */
	    size *= sizeof(char *);
	    to_hdr->comments = (CONST_DECL char **)malloc( size );
	    RLE_CHECK_ALLOC( to_hdr->cmd, to_hdr->comments, "comments" );
	    memcpy( to_hdr->comments, from_hdr->comments, size );
	}
	else
	    to_hdr->comments = NULL;	/* Blow off empty comment list. */
    }

    /* Restore the names to their original values. */
    to_hdr->cmd = cmd;
    to_hdr->file_name = file;

    /* Lines above mean nothing much happens if cmd and file are != NULL. */
    rle_names( to_hdr, to_hdr->cmd, to_hdr->file_name, num );

    return to_hdr;
}

/*****************************************************************
 * TAG( rle_hdr_clear )
 * 
 * Clear out the allocated memory pieces of a header.
 *
 * This routine is intended to be used internally by the library, to
 * clear a header before putting new data into it.  It clears all the
 * fields that would be set by reading in a new image header.
 * Therefore, it does not clear the program and file names.
 * 
 * Inputs:
 * 	the_hdr:	To be cleared.
 * Outputs:
 * 	the_hdr:	After clearing.
 * Assumptions:
 * 	If is_init field is RLE_INIT_MAGIC, the header has been
 * 	properly initialized.  This will fail every 2^(-32) times, on
 * 	average.
 * Algorithm:
 * 	Free memory and set to zero all pointers, except program and
 * 	file name.
 */
void
rle_hdr_clear( the_hdr )
rle_hdr *the_hdr;
{
    /* Try to free memory.  Assume if is_init is properly set that this
     * header has been previously initialized, therefore it is safe to
     * free memory.
     */
    if ( the_hdr && the_hdr->is_init == RLE_INIT_MAGIC )
    {
	if ( the_hdr->bg_color )
	    free( the_hdr->bg_color );
	the_hdr->bg_color = 0;
	if ( the_hdr->cmap )
	    free( the_hdr->cmap );
	the_hdr->cmap = 0;
	/* Unfortunately, we don't know how to free the comment memory. */
	if ( the_hdr->comments )
	    free( the_hdr->comments );
	the_hdr->comments = 0;
    }
}



/*****************************************************************
 * TAG( rle_hdr_init )
 * 
 * Initialize a rle_hdr structure.
 * Inputs:
 * 	the_hdr:	Header to be initialized.
 * Outputs:
 * 	the_hdr:	Initialized header.
 * Assumptions:
 * 	If the_hdr->is_init is RLE_INIT_MAGIC, the header has been
 * 	previously initialized.
 * 	If the_hdr is a copy of another rle_hdr structure, the copy
 * 	was made with rle_hdr_cp.
 * Algorithm:
 *  Fill in fields of rle_dflt_hdr that could not be set by the loader
 *	If the_hdr is rle_dflt_hdr, do nothing else
 *  Else:
 *	  If the_hdr is NULL, return a copy of rle_dflt_hdr in static storage
 * 	  If the_hdr->is_init is RLE_INIT_MAGIC, free all memory
 * 	     pointed to by non-null pointers.
 *    If this is a recursive call to rle_hdr_init, clear *the_hdr and
 *      return the_hdr.
 *    Else make a copy of rle_dflt_hdr and return its address.  Make the
 *      copy in static storage if the_hdr is NULL, and in the_hdr otherwise.
 */
rle_hdr *
rle_hdr_init( the_hdr )
rle_hdr *the_hdr;
{
    rle_hdr *ret_hdr;

    rle_dflt_hdr.rle_file = stdout;
    /* The rest of rle_dflt_hdr is set by the loader's data initialization */

    if ( the_hdr == &rle_dflt_hdr )
	return the_hdr;

    rle_hdr_clear( the_hdr );

    /* Only call rle_hdr_cp if not called from there. */
    if ( !no_recurse )
    {
	no_recurse++;
	ret_hdr = rle_hdr_cp( &rle_dflt_hdr, the_hdr );
	no_recurse--;
    }
    else
	ret_hdr = the_hdr;

    return ret_hdr;
}