summaryrefslogtreecommitdiffstats
path: root/pixmap.c
blob: 8cd21a62ff5235a8504f591479b7b58fdb180101 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 *  Pixmap manipulation
 *
 *  (C) Copyright 2001-2003 Geert Uytterhoeven
 *
 *  This file is subject to the terms and conditions of the GNU General Public
 *  License. See the file COPYING in the main directory of this archive for
 *  more details.
 */

#include <stdlib.h>

#include "types.h"
#include "clut.h"
#include "image.h"
#include "pixmap.h"
#include "visual.h"
#include "visops.h"
#include "util.h"


static void image_bw_to_pixmap(const struct image *image, pixel_t *pixmap);
static void image_lut256_to_pixmap(const struct image *image, pixel_t *pixmap);
static void image_rgb888_to_pixmap(const struct image *image, pixel_t *pixmap);


    /*
     *  Convert an image to a pixmap
     */

pixel_t *create_pixmap(const struct image *image)
{
    pixel_t *pixmap;

    pixmap = malloc(image->width*image->height*sizeof(pixel_t));
    if (!pixmap)
	Fatal("Not enough memory\n");
    switch (image->type) {
	case IMAGE_BW:
	    image_bw_to_pixmap(image, pixmap);
	    break;

	case IMAGE_GREY256:
	case IMAGE_CLUT256:
	    image_lut256_to_pixmap(image, pixmap);
	    break;

	case IMAGE_RGB888:
	    image_rgb888_to_pixmap(image, pixmap);
	    break;

	default:
	    Fatal("Unknown image type %d\n", image->type);
	    break;
    }
    return pixmap;
}


    /*
     *  Convert a black-and-white image to a pixmap
     */

static const unsigned char bitmask[8] = {
    0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
};

static void image_bw_to_pixmap(const struct image *image, pixel_t *pixmap)
{
    pixel_t black, white;
    const unsigned char *src;
    pixel_t *dst;
    int i, j, k;

    black = match_color(&clut_mono[0]);
    white = match_color(&clut_mono[1]);

    src = image->data;
    dst = pixmap;
    for (i = 0; i < image->height; i++) {
	for (j = 0, k = 0; j < image->width; j++, k = (k+1) & 7) {
	    *dst++ = (*src & bitmask[k]) ? white : black;
	    if (k == 7)
		src++;
	}
	if (k)
	    src++;
    }
}


    /*
     *  Convert a GREY256/CLUT256 image to a pixmap
     */

static pixel_t lut[256];

static void image_lut256_to_pixmap(const struct image *image, pixel_t *pixmap)
{
    rgba_t color;
    const unsigned char *src;
    pixel_t *dst;
    int i;

    color.a = 65535;
    if (image->type == IMAGE_GREY256) {
	for (i = 0; i < 256; i++) {
	    color.r = color.g = color.b = EXPAND_TO_16BIT(i, 255);
	    lut[i] = match_color(&color);
	}
    } else {
	src = image->clut;
	for (i = 0; i < image->clut_len; i++) {
	    color.r = EXPAND_TO_16BIT(*src++, 255);
	    color.g = EXPAND_TO_16BIT(*src++, 255);
	    color.b = EXPAND_TO_16BIT(*src++, 255);
	    lut[i] = match_color(&color);
	}
    }

    src = image->data;
    dst = pixmap;
    for (i = 0; i < image->width*image->height; i++)
	*dst++ = lut[*src++];
}


    /*
     *  Convert an RGB888 image to a pixmap
     */

static void image_rgb888_to_pixmap(const struct image *image, pixel_t *pixmap)
{
    const unsigned char *src;
    pixel_t *dst;
    rgba_t color;
    int i;

    src = image->data;
    dst = pixmap;
    color.a = 65535;
    for (i = 0; i < image->width*image->height; i++) {
	color.r = EXPAND_TO_16BIT(*src++, 255);
	color.g = EXPAND_TO_16BIT(*src++, 255);
	color.b = EXPAND_TO_16BIT(*src++, 255);
	*dst++ = match_color(&color);
    }
}