rnd-19980904-1
[rocksndiamonds.git] / src / new.c
1
2 /* new.c:
3  *
4  * functions to allocate and deallocate structures and structure data
5  *
6  * jim frost 09.29.89
7  *
8  * Copyright 1989, 1991 Jim Frost.
9  * See included file "copyright.h" for complete copyright information.
10  */
11
12 #include "xli.h"
13
14 /* this table is useful for quick conversions between depth and ncolors
15  */
16
17 unsigned long DepthToColorsTable[] =
18 {
19   /*  0 */ 1,
20   /*  1 */ 2,
21   /*  2 */ 4,
22   /*  3 */ 8,
23   /*  4 */ 16,
24   /*  5 */ 32,
25   /*  6 */ 64,
26   /*  7 */ 128,
27   /*  8 */ 256,
28   /*  9 */ 512,
29   /* 10 */ 1024,
30   /* 11 */ 2048,
31   /* 12 */ 4096,
32   /* 13 */ 8192,
33   /* 14 */ 16384,
34   /* 15 */ 32768,
35   /* 16 */ 65536,
36   /* 17 */ 131072,
37   /* 18 */ 262144,
38   /* 19 */ 524288,
39   /* 20 */ 1048576,
40   /* 21 */ 2097152,
41   /* 22 */ 4194304,
42   /* 23 */ 8388608,
43   /* 24 */ 16777216
44 };
45
46 char *dupString(char *s)
47 {
48   char *d;
49
50   if (!s)
51     return(NULL);
52
53   d = (char *)lmalloc(strlen(s) + 1);
54   strcpy(d, s);
55   return(d);
56 }
57
58 void newRGBMapData(RGBMap *rgb, unsigned int size)
59 {
60   rgb->used = 0;
61   rgb->size = size;
62   rgb->compressed = FALSE;
63   rgb->red = (Intensity *)lmalloc(sizeof(Intensity) * size);
64   rgb->green = (Intensity *)lmalloc(sizeof(Intensity) * size);
65   rgb->blue = (Intensity *)lmalloc(sizeof(Intensity) * size);
66 }
67
68 void freeRGBMapData(RGBMap *rgb)
69 {
70   lfree((byte *)rgb->red);
71   lfree((byte *)rgb->green);
72   lfree((byte *)rgb->blue);
73 }
74
75 Image *newBitImage(unsigned int width, unsigned int height)
76 {
77   Image        *image;
78   unsigned int  linelen;
79
80   image = (Image *)lmalloc(sizeof(Image));
81   image->type = IBITMAP;
82   image->title = NULL;
83   newRGBMapData(&(image->rgb), (unsigned int)2);
84   *(image->rgb.red)= *(image->rgb.green) = *(image->rgb.blue)= 65535;
85   *(image->rgb.red + 1)= *(image->rgb.green + 1) = *(image->rgb.blue + 1)= 0;
86   image->rgb.used = 2;
87   image->width = width;
88   image->height = height;
89   image->depth = 1;
90   linelen = ((width + 7) / 8);
91   image->data = (unsigned char *)lcalloc(linelen * height);
92   return(image);
93 }
94
95 Image *newRGBImage(unsigned int width, unsigned int height, unsigned int depth)
96 {
97   Image        *image;
98   unsigned int  pixlen, numcolors;
99
100   if (depth == 0)       /* special case for `zero' depth image, which is */
101     depth = 1;          /* sometimes interpreted as `one color' */
102   pixlen = ((depth+7) / 8);
103   numcolors = depthToColors(depth);
104   image = (Image *)lmalloc(sizeof(Image));
105   image->type = IRGB;
106   image->title = NULL;
107   newRGBMapData(&(image->rgb), numcolors);
108   image->width = width;
109   image->height = height;
110   image->depth = depth;
111   image->pixlen = pixlen;
112   image->data = (unsigned char *)lmalloc(width * height * pixlen);
113   return(image);
114 }
115
116 void freeImageData(Image *image)
117 {
118   if (image->title)
119   {
120     lfree((byte *)image->title);
121     image->title= NULL;
122   }
123   freeRGBMapData(&(image->rgb));
124   lfree(image->data);
125 }
126
127 void freeImage(Image *image)
128 {
129   freeImageData(image);
130   lfree((byte *)image);
131 }
132
133 byte *lmalloc(unsigned int size)
134 {
135   byte *area;
136
137   if (size == 0)
138   {
139     size = 1;
140   }
141   if (!(area = (byte *)malloc(size)))
142   {
143     fprintf(stderr, "Out of memory!\n");
144     exit(1);
145   }
146
147   return(area);
148 }
149
150 byte *lcalloc(unsigned int size)
151 {
152   byte *area;
153
154   if (size == 0)
155   {
156     size = 1;
157   }
158   if (!(area = (byte *)calloc(1, size)))
159   {
160     fprintf(stderr, "Out of memory!\n");
161     exit(1);
162   }
163
164   return(area);
165 }
166
167 void lfree(byte *area)
168 {
169   free(area);
170 }