vnc-enc-hextile-template.h 4.7 KB
Newer Older
B
bellard 已提交
1 2 3
#define CONCAT_I(a, b) a ## b
#define CONCAT(a, b) CONCAT_I(a, b)
#define pixel_t CONCAT(uint, CONCAT(BPP, _t))
B
bellard 已提交
4
#ifdef GENERIC
5
#define NAME CONCAT(generic_, BPP)
B
bellard 已提交
6 7 8 9 10 11
#else
#define NAME BPP
#endif

static void CONCAT(send_hextile_tile_, NAME)(VncState *vs,
                                             int x, int y, int w, int h,
12 13
                                             void *last_bg_,
                                             void *last_fg_,
B
bellard 已提交
14
                                             int *has_bg, int *has_fg)
B
bellard 已提交
15
{
S
Stefano Stabellini 已提交
16 17
    VncDisplay *vd = vs->vd;
    uint8_t *row = vd->server->data + y * ds_get_linesize(vs->ds) + x * ds_get_bytes_per_pixel(vs->ds);
B
bellard 已提交
18 19
    pixel_t *irow = (pixel_t *)row;
    int j, i;
20 21
    pixel_t *last_bg = (pixel_t *)last_bg_;
    pixel_t *last_fg = (pixel_t *)last_fg_;
B
bellard 已提交
22 23 24 25 26 27
    pixel_t bg = 0;
    pixel_t fg = 0;
    int n_colors = 0;
    int bg_count = 0;
    int fg_count = 0;
    int flags = 0;
28
    uint8_t data[(vs->clientds.pf.bytes_per_pixel + 2) * 16 * 16];
B
bellard 已提交
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
    int n_data = 0;
    int n_subtiles = 0;

    for (j = 0; j < h; j++) {
	for (i = 0; i < w; i++) {
	    switch (n_colors) {
	    case 0:
		bg = irow[i];
		n_colors = 1;
		break;
	    case 1:
		if (irow[i] != bg) {
		    fg = irow[i];
		    n_colors = 2;
		}
		break;
	    case 2:
		if (irow[i] != bg && irow[i] != fg) {
		    n_colors = 3;
		} else {
		    if (irow[i] == bg)
			bg_count++;
		    else if (irow[i] == fg)
			fg_count++;
		}
		break;
	    default:
		break;
	    }
	}
	if (n_colors > 2)
	    break;
61
	irow += ds_get_linesize(vs->ds) / sizeof(pixel_t);
B
bellard 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75
    }

    if (n_colors > 1 && fg_count > bg_count) {
	pixel_t tmp = fg;
	fg = bg;
	bg = tmp;
    }

    if (!*has_bg || *last_bg != bg) {
	flags |= 0x02;
	*has_bg = 1;
	*last_bg = bg;
    }

76
    if (n_colors < 3 && (!*has_fg || *last_fg != fg)) {
B
bellard 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89
	flags |= 0x04;
	*has_fg = 1;
	*last_fg = fg;
    }

    switch (n_colors) {
    case 1:
	n_data = 0;
	break;
    case 2:
	flags |= 0x08;

	irow = (pixel_t *)row;
90

B
bellard 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	for (j = 0; j < h; j++) {
	    int min_x = -1;
	    for (i = 0; i < w; i++) {
		if (irow[i] == fg) {
		    if (min_x == -1)
			min_x = i;
		} else if (min_x != -1) {
		    hextile_enc_cord(data + n_data, min_x, j, i - min_x, 1);
		    n_data += 2;
		    n_subtiles++;
		    min_x = -1;
		}
	    }
	    if (min_x != -1) {
		hextile_enc_cord(data + n_data, min_x, j, i - min_x, 1);
		n_data += 2;
		n_subtiles++;
	    }
109
	    irow += ds_get_linesize(vs->ds) / sizeof(pixel_t);
B
bellard 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122
	}
	break;
    case 3:
	flags |= 0x18;

	irow = (pixel_t *)row;

	if (!*has_bg || *last_bg != bg)
	    flags |= 0x02;

	for (j = 0; j < h; j++) {
	    int has_color = 0;
	    int min_x = -1;
T
ths 已提交
123
	    pixel_t color = 0; /* shut up gcc */
B
bellard 已提交
124 125 126 127 128 129 130 131 132 133

	    for (i = 0; i < w; i++) {
		if (!has_color) {
		    if (irow[i] == bg)
			continue;
		    color = irow[i];
		    min_x = i;
		    has_color = 1;
		} else if (irow[i] != color) {
		    has_color = 0;
B
bellard 已提交
134 135
#ifdef GENERIC
                    vnc_convert_pixel(vs, data + n_data, color);
136
                    n_data += vs->clientds.pf.bytes_per_pixel;
B
bellard 已提交
137
#else
B
bellard 已提交
138
		    memcpy(data + n_data, &color, sizeof(color));
B
bellard 已提交
139 140 141 142
                    n_data += sizeof(pixel_t);
#endif
		    hextile_enc_cord(data + n_data, min_x, j, i - min_x, 1);
		    n_data += 2;
B
bellard 已提交
143 144 145 146 147 148 149 150 151 152 153
		    n_subtiles++;

		    min_x = -1;
		    if (irow[i] != bg) {
			color = irow[i];
			min_x = i;
			has_color = 1;
		    }
		}
	    }
	    if (has_color) {
B
bellard 已提交
154 155
#ifdef GENERIC
                vnc_convert_pixel(vs, data + n_data, color);
156
                n_data += vs->clientds.pf.bytes_per_pixel;
B
bellard 已提交
157 158 159 160 161 162
#else
                memcpy(data + n_data, &color, sizeof(color));
                n_data += sizeof(pixel_t);
#endif
		hextile_enc_cord(data + n_data, min_x, j, i - min_x, 1);
		n_data += 2;
B
bellard 已提交
163 164
		n_subtiles++;
	    }
165
	    irow += ds_get_linesize(vs->ds) / sizeof(pixel_t);
B
bellard 已提交
166 167
	}

168 169
	/* A SubrectsColoured subtile invalidates the foreground color */
	*has_fg = 0;
B
bellard 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	if (n_data > (w * h * sizeof(pixel_t))) {
	    n_colors = 4;
	    flags = 0x01;
	    *has_bg = 0;

	    /* we really don't have to invalidate either the bg or fg
	       but we've lost the old values.  oh well. */
	}
    default:
	break;
    }

    if (n_colors > 3) {
	flags = 0x01;
	*has_fg = 0;
	*has_bg = 0;
	n_colors = 4;
    }

    vnc_write_u8(vs, flags);
    if (n_colors < 4) {
	if (flags & 0x02)
G
Gerd Hoffmann 已提交
192
	    vs->write_pixels(vs, &vd->server->pf, last_bg, sizeof(pixel_t));
B
bellard 已提交
193
	if (flags & 0x04)
G
Gerd Hoffmann 已提交
194
	    vs->write_pixels(vs, &vd->server->pf, last_fg, sizeof(pixel_t));
B
bellard 已提交
195 196 197 198 199 200
	if (n_subtiles) {
	    vnc_write_u8(vs, n_subtiles);
	    vnc_write(vs, data, n_data);
	}
    } else {
	for (j = 0; j < h; j++) {
G
Gerd Hoffmann 已提交
201 202
	    vs->write_pixels(vs, &vd->server->pf, row,
                             w * ds_get_bytes_per_pixel(vs->ds));
203
	    row += ds_get_linesize(vs->ds);
B
bellard 已提交
204 205 206 207
	}
    }
}

B
bellard 已提交
208
#undef NAME
B
bellard 已提交
209 210 211
#undef pixel_t
#undef CONCAT_I
#undef CONCAT