1 /* new.c:
2  *
3  * functions to allocate and deallocate structures and structure data
4  *
5  * jim frost 09.29.89
6  *
7  * Copyright 1989, 1991 Jim Frost.
8  * See included file "copyright.h" for complete copyright information.
9  */
10 
11 #include "copyright.h"
12 #include "image.h"
13 
14 #if defined(__APPLE__) || defined(CSRG_BASED)
15 #include <stdlib.h>
16 #else
17 #include <malloc.h>
18 #endif
19 
20 extern int _Xdebug;
21 extern void memoryExhausted(void);
22 
23 
24 /* this table is useful for quick conversions between depth and ncolors
25  */
26 
27 unsigned long DepthToColorsTable[] = {
28   /*  0 */ 1UL,
29   /*  1 */ 2UL,
30   /*  2 */ 4UL,
31   /*  3 */ 8UL,
32   /*  4 */ 16UL,
33   /*  5 */ 32UL,
34   /*  6 */ 64UL,
35   /*  7 */ 128UL,
36   /*  8 */ 256UL,
37   /*  9 */ 512UL,
38   /* 10 */ 1024UL,
39   /* 11 */ 2048UL,
40   /* 12 */ 4096UL,
41   /* 13 */ 8192UL,
42   /* 14 */ 16384UL,
43   /* 15 */ 32768UL,
44   /* 16 */ 65536UL,
45   /* 17 */ 131072UL,
46   /* 18 */ 262144UL,
47   /* 19 */ 524288UL,
48   /* 20 */ 1048576UL,
49   /* 21 */ 2097152UL,
50   /* 22 */ 4194304UL,
51   /* 23 */ 8388608UL,
52   /* 24 */ 16777216UL,
53   /* 25 */ 33554432UL,
54   /* 26 */ 67108864UL,
55   /* 27 */ 134217728UL,
56   /* 28 */ 268435456UL,
57   /* 29 */ 536870912UL,
58   /* 30 */ 1073741824UL,
59   /* 31 */ 2147483648UL,
60   /* 32 */ 2147483648UL /* bigger than unsigned int; this is good enough */
61 };
62 
colorsToDepth(ncolors)63 unsigned long colorsToDepth(ncolors)
64      unsigned long ncolors;
65 { unsigned long a;
66 
67   for (a= 0; (a < 32) && (DepthToColorsTable[a] < ncolors); a++)
68     /* EMPTY */
69     ;
70   return(a);
71 }
72 
dupString(s)73 char *dupString(s)
74      char *s;
75 { char *d;
76 
77   if (!s)
78     return(NULL);
79   d= (char *)lmalloc(strlen(s) + 1);
80   strcpy(d, s);
81   return(d);
82 }
83 
newRGBMapData(rgb,size)84 void newRGBMapData(rgb, size)
85      RGBMap       *rgb;
86      unsigned int  size;
87 {
88   rgb->used= 0;
89   rgb->size= size;
90   rgb->compressed= 0;
91   rgb->red= (Intensity *)lmalloc(sizeof(Intensity) * size);
92   rgb->green= (Intensity *)lmalloc(sizeof(Intensity) * size);
93   rgb->blue= (Intensity *)lmalloc(sizeof(Intensity) * size);
94 }
95 
freeRGBMapData(rgb)96 void freeRGBMapData(rgb)
97      RGBMap *rgb;
98 {
99   lfree((byte *)rgb->red);
100   lfree((byte *)rgb->green);
101   lfree((byte *)rgb->blue);
102 }
103 
newBitImage(width,height)104 Image *newBitImage(width, height)
105      unsigned int width, height;
106 { Image        *image;
107   unsigned int  linelen;
108 
109   image= (Image *)lmalloc(sizeof(Image));
110   image->type= IBITMAP;
111   image->title= NULL;
112   newRGBMapData(&(image->rgb), (unsigned int)2);
113   *(image->rgb.red)= *(image->rgb.green)= *(image->rgb.blue)= 65535;
114   *(image->rgb.red + 1)= *(image->rgb.green + 1)= *(image->rgb.blue + 1)= 0;
115   image->rgb.used= 2;
116   image->width= width;
117   image->height= height;
118   image->depth= 1;
119   linelen= (width / 8) + (width % 8 ? 1 : 0); /* thanx johnh@amcc.com */
120   image->data= (unsigned char *)lcalloc(linelen * height);
121   image->trans = -1;
122   return(image);
123 }
124 
newRGBImage(width,height,depth)125 Image *newRGBImage(width, height, depth)
126      unsigned int width, height, depth;
127 { Image        *image;
128   unsigned int  pixlen, numcolors;
129 
130   pixlen= (depth / 8) + (depth % 8 ? 1 : 0);
131   if (pixlen == 0) /* special case for `zero' depth image, which is */
132     pixlen= 1;     /* sometimes interpreted as `one color' */
133   numcolors = depthToColors(depth);
134   image= (Image *)lmalloc(sizeof(Image));
135   image->type= IRGB;
136   image->title= NULL;
137   newRGBMapData(&(image->rgb), numcolors);
138   image->width= width;
139   image->height= height;
140   image->depth= depth;
141   image->pixlen= pixlen;
142   image->data= (unsigned char *)lmalloc(width * height * pixlen);
143   image->trans = -1;
144   return(image);
145 }
146 
newTrueImage(width,height)147 Image *newTrueImage(width, height)
148      unsigned int width, height;
149 { Image        *image;
150 
151   image= (Image *)lmalloc(sizeof(Image));
152   image->type= ITRUE;
153   image->title= NULL;
154   image->rgb.used= image->rgb.size= 0;
155   image->width= width;
156   image->height= height;
157   image->depth= 24;
158   image->pixlen= 3;
159   image->data= (unsigned char *)lmalloc(width * height * 3);
160   image->trans = -1;
161   return(image);
162 }
163 
freeImageData(image)164 void freeImageData(image)
165      Image *image;
166 {
167   if (image->title) {
168     lfree((byte *)image->title);
169     image->title= NULL;
170   }
171   if (!TRUEP(image))
172     freeRGBMapData(&(image->rgb));
173   lfree(image->data);
174 }
175 
freeImage(image)176 void freeImage(image)
177      Image *image;
178 {
179   freeImageData(image);
180   lfree((byte *)image);
181 }
182 
lmalloc(size)183 byte *lmalloc(size)
184      unsigned int size;
185 { byte *area;
186 
187   if (size == 0) {
188     size= 1;
189     if (_Xdebug)
190       fprintf(stderr, "lcalloc given zero size!\n");
191   }
192   if (!(area= (byte *)malloc(size))) {
193     memoryExhausted();
194     /* NOTREACHED */
195   }
196   return(area);
197 }
198 
lcalloc(size)199 byte *lcalloc(size)
200      unsigned int size;
201 { byte *area;
202 
203   if (size == 0) {
204     size= 1;
205     if (_Xdebug)
206       fprintf(stderr, "lcalloc given zero size!\n");
207   }
208   if (!(area= (byte *)calloc(1, size))) {
209     memoryExhausted();
210     /* NOTREACHED */
211   }
212   return(area);
213 }
214 
lfree(area)215 void lfree(area)
216      byte *area;
217 {
218   free(area);
219 }
220