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