File: new.c

package info (click to toggle)
xli 1.17.0+20061110-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 1,540 kB
  • sloc: ansic: 25,840; makefile: 11
file content (262 lines) | stat: -rw-r--r-- 5,187 bytes parent folder | download | duplicates (6)
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
/* new.c:
 *
 * functions to allocate and deallocate structures and structure data
 *
 * jim frost 09.29.89
 *
 * Copyright 1989, 1991 Jim Frost.
 * See included file "copyright.h" for complete copyright information.
 */

#include "copyright.h"
#include "xli.h"

/* this table is useful for quick conversions between depth and ncolors */

unsigned long DepthToColorsTable[] =
{
  /*  0 */ 1,
  /*  1 */ 2,
  /*  2 */ 4,
  /*  3 */ 8,
  /*  4 */ 16,
  /*  5 */ 32,
  /*  6 */ 64,
  /*  7 */ 128,
  /*  8 */ 256,
  /*  9 */ 512,
  /* 10 */ 1024,
  /* 11 */ 2048,
  /* 12 */ 4096,
  /* 13 */ 8192,
  /* 14 */ 16384,
  /* 15 */ 32768,
  /* 16 */ 65536,
  /* 17 */ 131072,
  /* 18 */ 262144,
  /* 19 */ 524288,
  /* 20 */ 1048576,
  /* 21 */ 2097152,
  /* 22 */ 4194304,
  /* 23 */ 8388608,
  /* 24 */ 16777216,
  /* 25 */ 33554432,
  /* 26 */ 67108864,
  /* 27 */ 134217728,
  /* 28 */ 268435456,
  /* 29 */ 536870912,
  /* 30 */ 1073741824,
  /* 31 */ 2147483648U,

  /* bigger than unsigned int; this is good enough */
  /* 32 */ 2147483648U
};

unsigned long colorsToDepth(long unsigned int ncolors)
{
	unsigned long a;

	for (a = 0; (a < 32) && (DepthToColorsTable[a] < ncolors); a++)
		/* EMPTY */
		;
	return (a);
}

char *dupString(char *s)
{
	char *d;

	if (!s)
		return (NULL);
	d = (char *) lmalloc(strlen(s) + 1);
	strcpy(d, s);
	return (d);
}

void newRGBMapData(RGBMap *rgb, unsigned int size)
{
	CURRFUNC("newRGBMapData");
	rgb->used = 0;
	rgb->size = size;
	rgb->compressed = FALSE;
	rgb->red = (Intensity *) lmalloc(sizeof(Intensity) * size);
	rgb->green = (Intensity *) lmalloc(sizeof(Intensity) * size);
	rgb->blue = (Intensity *) lmalloc(sizeof(Intensity) * size);
}

void resizeRGBMapData(RGBMap *rgb, unsigned int size)
{
	CURRFUNC("resizeRGBMapData");
	rgb->red = (Intensity *) lrealloc((byte *) rgb->red,
		sizeof(Intensity) * size);
	rgb->green = (Intensity *) lrealloc((byte *) rgb->green,
		sizeof(Intensity) * size);
	rgb->blue = (Intensity *) lrealloc((byte *) rgb->blue,
		sizeof(Intensity) * size);
	rgb->size = size;
}

void freeRGBMapData(RGBMap *rgb)
{
	CURRFUNC("freeRGBMapData");
	lfree((byte *) rgb->red);
	lfree((byte *) rgb->green);
	lfree((byte *) rgb->blue);
}

static unsigned int ovmul(unsigned int a, unsigned int b)
{
	unsigned int r;

	r = a * b;
	if (r / a != b) {
		memoryExhausted();
	}

	return r;
}

static Image *newImage(unsigned width, unsigned height)
{
	Image *image;

	CURRFUNC("newImage");
	image = (Image *) lmalloc(sizeof(Image));
	image->title = 0;
	image->width = width;
	image->height = height;
	image->gamma = UNSET_GAMMA;
	image->flags = 0;

	return image;
}

Image *newBitImage(unsigned width, unsigned height)
{
	Image *image;
	unsigned int linelen;

	CURRFUNC("newBitImage");
	image = newImage(width, height);
	image->type = IBITMAP;
	newRGBMapData(&(image->rgb), (unsigned int) 2);
	*(image->rgb.red) = *(image->rgb.green) = *(image->rgb.blue) = 65535;
	*(image->rgb.red + 1) = *(image->rgb.green + 1) = *(image->rgb.blue + 1) = 0;
	image->rgb.used = 2;
	image->depth = 1;
	linelen = ((width + 7) / 8);
	image->data = (unsigned char *) lcalloc(ovmul(linelen, height));

	return image;
}

Image *newRGBImage(unsigned int width, unsigned int height, unsigned int depth)
{
	Image *image;
	unsigned int pixlen, numcolors;

	CURRFUNC("newRGBImage");

	/* special case for `zero' depth image, which is sometimes
	 * interpreted as `one color'
	 */
	if (depth == 0)
		depth = 1;
	pixlen = ((depth + 7) / 8);
	numcolors = depthToColors(depth);
	image = newImage(width, height);
	image->type = IRGB;
	newRGBMapData(&(image->rgb), numcolors);
	image->depth = depth;
	image->pixlen = pixlen;
	image->data =
		(unsigned char *) lmalloc(ovmul(ovmul(width, height), pixlen));

	return image;
}

Image *newTrueImage(unsigned int width, unsigned int height)
{
	Image *image;

	CURRFUNC("NewTrueImage");
	image = newImage(width, height);
	image->type = ITRUE;
	image->rgb.used = image->rgb.size = 0;
	image->depth = 24;
	image->pixlen = 3;
	image->data =
		(unsigned char *) lmalloc(ovmul(ovmul(width, height), 3));

	return image;
}

void freeImageData(Image *image)
{
	if (image->title) {
		lfree((byte *) image->title);
		image->title = NULL;
	}
	if (!TRUEP(image))
		freeRGBMapData(&(image->rgb));
	lfree(image->data);
}

void freeImage(Image *image)
{
	freeImageData(image);
	lfree((byte *) image);
}

byte *lmalloc(unsigned int size)
{
	byte *area;

	if (size == 0) {
		size = 1;
		if (globals._Xdebug)
			fprintf(stderr, "lmalloc given zero size!\n");
	}
	if (!(area = (byte *) malloc(size))) {
		memoryExhausted();
		/* NOTREACHED */
	}
	return (area);
}

byte *lcalloc(unsigned int size)
{
	byte *area;

	if (size == 0) {
		size = 1;
		if (globals._Xdebug)
			fprintf(stderr, "lcalloc given zero size!\n");
	}
	if (!(area = (byte *) calloc(1, size))) {
		memoryExhausted();
		/* NOTREACHED */
	}
	return (area);
}

byte *lrealloc(byte *old, unsigned int size)
{
	byte *area;

	if (size == 0) {
		size = 1;
		if (globals._Xdebug)
			fprintf(stderr, "lrealloc given zero size!\n");
	}
	if (!(area = (byte *) realloc(old, size))) {
		memoryExhausted();
		/* NOTREACHED */
	}
	return (area);
}

void lfree(byte *area)
{
	free(area);
}