forked from unwind/gimpilbm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrayscale.c
107 lines (94 loc) · 1.85 KB
/
grayscale.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "grayscale.h"
/**** Grayscale ****/
void transGray(grayval *dest, gint width, const palidx *transGrayTab)
{
g_assert(dest != NULL);
g_assert(transGrayTab != NULL);
while(width--)
{
*dest = transGrayTab[*dest];
++dest;
}
}
void dumpGrayTrans(const palidx *t)
{
gint i;
g_assert(NULL != t);
printf("grayTrans colortable:\n");
/* Crashes if maxGrayshades % 8 > 0 */
for(i = 0; i < maxGrayshades; i += 8, t += 8)
printf(" %02x %02x %02x %02x %02x %02x %02x %02x\n", t[0], t[1], t[2], t[3], t[4], t[5], t[6], t[7]);
}
guint8 * allocGrayscale(void)
{
guint8 *gbuf = g_new(guint8, maxGrayshades * byteppRGB);
if(gbuf == NULL)
{
fputs("Out of memory.\n", stderr);
}
else
{
guint8 *gp;
gint i;
if(VERBOSE)
fputs("Composing grayscale colormap...\n", stdout);
for(gp = gbuf, i = 0; i < maxGrayshades; ++i)
{
*gp++ = i;
*gp++ = i;
*gp++ = i;
}
}
return gbuf;
}
/* Returns TRUE if the given colormap consists of only
* gray entries
*/
gboolean isGrayscale(const guint8 *cmap, gint ncols)
{
g_assert(cmap != NULL);
g_assert(ncols >= 0);
while(ncols--)
{
if(!((*cmap == cmap[1]) && (cmap[1] == cmap[2])))
return FALSE;
cmap += byteppRGB;
}
return TRUE;
}
/* Allocates a 256 byte array of the values 0..255 */
guint8 * allocGrayKeep(void)
{
guint8 *gt = g_new(guint8, maxGrayshades);
if(gt == NULL)
fputs("Out of memory.\n", stderr);
else
{
gint i;
for(i = maxGrayshades - 1; i >= 0; --i)
gt[i] = i;
}
return gt;
}
/* Returns a ncols wide array
* "cmap" has to be grayscale
*/
guint8 * allocGrayTrans(const guint8 *cmap, gint ncols)
{
guint8 *gt = g_new(guint8, maxGrayshades);
g_assert(cmap != NULL);
g_assert(ncols >= 0);
if(gt == NULL)
fputs("Out of memory.\n", stderr);
else
{
gint idx = 0;
while(ncols--)
{
gt[idx] = *cmap;
++idx;
cmap += byteppRGB;
}
}
return gt;
}