d1744d5eb2fde40fe12c544b96fc58347c96a3f0
[rocksndiamonds.git] / src / new.c
1
2 /* new.c */
3
4 #include "image.h"
5 #include "misc.h"
6
7 /* this table is useful for quick conversions between depth and ncolors
8  */
9
10 unsigned long DepthToColorsTable[] =
11 {
12   /*  0 */ 1,
13   /*  1 */ 2,
14   /*  2 */ 4,
15   /*  3 */ 8,
16   /*  4 */ 16,
17   /*  5 */ 32,
18   /*  6 */ 64,
19   /*  7 */ 128,
20   /*  8 */ 256,
21   /*  9 */ 512,
22   /* 10 */ 1024,
23   /* 11 */ 2048,
24   /* 12 */ 4096,
25   /* 13 */ 8192,
26   /* 14 */ 16384,
27   /* 15 */ 32768,
28   /* 16 */ 65536,
29   /* 17 */ 131072,
30   /* 18 */ 262144,
31   /* 19 */ 524288,
32   /* 20 */ 1048576,
33   /* 21 */ 2097152,
34   /* 22 */ 4194304,
35   /* 23 */ 8388608,
36   /* 24 */ 16777216
37 };
38
39 void newRGBMapData(RGBMap *rgb, unsigned int size)
40 {
41   rgb->used = 0;
42   rgb->size = size;
43   rgb->compressed = FALSE;
44   rgb->red = (Intensity *)checked_malloc(sizeof(Intensity) * size);
45   rgb->green = (Intensity *)checked_malloc(sizeof(Intensity) * size);
46   rgb->blue = (Intensity *)checked_malloc(sizeof(Intensity) * size);
47 }
48
49 void freeRGBMapData(RGBMap *rgb)
50 {
51   free((byte *)rgb->red);
52   free((byte *)rgb->green);
53   free((byte *)rgb->blue);
54 }
55
56 Image *newBitImage(unsigned int width, unsigned int height)
57 {
58   Image        *image;
59   unsigned int  linelen;
60
61   image = (Image *)checked_malloc(sizeof(Image));
62   image->type = IBITMAP;
63   newRGBMapData(&(image->rgb), (unsigned int)2);
64   *(image->rgb.red)= *(image->rgb.green) = *(image->rgb.blue)= 65535;
65   *(image->rgb.red + 1)= *(image->rgb.green + 1) = *(image->rgb.blue + 1)= 0;
66   image->rgb.used = 2;
67   image->width = width;
68   image->height = height;
69   image->depth = 1;
70   linelen = ((width + 7) / 8);
71   image->data = (unsigned char *)checked_calloc(linelen * height);
72   return(image);
73 }
74
75 Image *newRGBImage(unsigned int width, unsigned int height, unsigned int depth)
76 {
77   Image        *image;
78   unsigned int  pixlen, numcolors;
79
80   if (depth == 0)       /* special case for `zero' depth image, which is */
81     depth = 1;          /* sometimes interpreted as `one color' */
82   pixlen = ((depth+7) / 8);
83   numcolors = depthToColors(depth);
84   image = (Image *)checked_malloc(sizeof(Image));
85   image->type = IRGB;
86   newRGBMapData(&(image->rgb), numcolors);
87   image->width = width;
88   image->height = height;
89   image->depth = depth;
90   image->pixlen = pixlen;
91   image->data = (unsigned char *)checked_malloc(width * height * pixlen);
92   return(image);
93 }
94
95 void freeImageData(Image *image)
96 {
97   freeRGBMapData(&(image->rgb));
98   free(image->data);
99 }
100
101 void freeImage(Image *image)
102 {
103   freeImageData(image);
104   free((byte *)image);
105 }