Debian Codegraph @

converter/other/pamtosvg/bitmap.c

raw
1 /* bitmap.c: operations on bitmaps. */
2
3 #include <string.h>
4
5 #include "mallocvar.h"
6
7 #include "bitmap.h"
8
9 at_bitmap_type *
10 at_bitmap_new(unsigned short width,
11 unsigned short height,
12 unsigned int planes) {
13
14 at_bitmap_type * bitmap;
15
16 MALLOCVAR_NOFAIL(bitmap);
17
18 *bitmap = at_bitmap_init(NULL, width, height, planes);
19
20 return bitmap;
21 }
22
23
24
25 at_bitmap_type *
26 at_bitmap_copy(at_bitmap_type * src)
27 {
28 at_bitmap_type * dist;
29 unsigned short width, height, planes;
30
31 width = at_bitmap_get_width(src);
32 height = at_bitmap_get_height(src);
33 planes = at_bitmap_get_planes(src);
34
35 dist = at_bitmap_new(width, height, planes);
36 memcpy(dist->bitmap,
37 src->bitmap,
38 width * height * planes * sizeof(unsigned char));
39 return dist;
40 }
41
42
43
44 at_bitmap_type
45 at_bitmap_init(unsigned char * area,
46 unsigned short width,
47 unsigned short height,
48 unsigned int planes) {
49
50 at_bitmap_type bitmap;
51
52 if (area)
53 bitmap.bitmap = area;
54 else {
55 if (width * height == 0)
56 bitmap.bitmap = NULL;
57 else {
58 MALLOCARRAY(bitmap.bitmap, width * height * planes);
59 if (bitmap.bitmap == NULL)
60 pm_error("Unable to allocate %u x %u x %u bitmap array",
61 width, height, planes);
62 memset(bitmap.bitmap,
63 0, width * height * planes * sizeof(unsigned char));
64 }
65 }
66
67 bitmap.width = width;
68 bitmap.height = height;
69 bitmap.np = planes;
70
71 return bitmap;
72 }
73
74
75
76 void
77 at_bitmap_free (at_bitmap_type * bitmap)
78 {
79 free_bitmap (bitmap);
80 free(bitmap);
81 }
82
83
84
85 unsigned short
86 at_bitmap_get_width (at_bitmap_type * bitmap)
87 {
88 return bitmap->width;
89 }
90
91
92
93 unsigned short
94 at_bitmap_get_height (at_bitmap_type * bitmap)
95 {
96 return bitmap->height;
97 }
98
99
100
101 unsigned short
102 at_bitmap_get_planes (at_bitmap_type * bitmap)
103 {
104 return bitmap->np;
105 }
106
107
108
109 bitmap_type
110 new_bitmap (unsigned short width, unsigned short height)
111 {
112 return at_bitmap_init(NULL,width,height,1);
113 }
114
115
116
117 /* Free the storage that is allocated for a bitmap. On the other hand,
118 the bitmap might not have any storage allocated for it if it is zero
119 in either dimension; in that case, don't free it. */
120
121 void
122 free_bitmap (bitmap_type *b)
123 {
124 if (b->bitmap != NULL)
125 free (b->bitmap);
126 }
127
128
129