summaryrefslogtreecommitdiff
path: root/print_wcp.c
blob: 5c0bbd0d3db84cb61aeefb7df9d233337507aeb5 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * $NCDOr: print_wcp.c,v 1.1 1996/07/18 23:26:38 keithp Exp keithp $
 *
 * Copyright 1996 Network Computing Devices
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of NCD. not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  NCD. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * NCD. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NCD.
 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author:  Keith Packard, Network Computing Devices
 */

#include "scope.h"
#include "x11.h"
#include "wcpscope.h"

void
WcpQueryVersion (
    FD fd,
    register const unsigned char *buf)
{
  PrintField (buf, 0, 1, REQUEST, REQUESTHEADER) /* WcpRequest */ ;
  PrintField (buf, 1, 1, WCPREQUEST, WCPREQUESTHEADER) /* WcpSwitch */ ;
  if (Verbose < 1)
    return;
  if (Verbose > 1)
    PrintField(SBf, 0, 4, CARD32, "sequence number");

  printfield(buf, 2, 2, CONST2(2), "request length");
}

void
WcpQueryVersionReply (
    FD fd,
    const unsigned char *buf)
{
  short   n;
  long    m;
  long   k;

  PrintField(RBf, 0, 1, REPLY, REPLYHEADER) /* WcpRequest reply */ ;
  PrintField(RBf, 1, 1, WCPREPLY, WCPREPLYHEADER) /* WcpQueryVersion reply */;
  if (Verbose < 1)
    return;
  printfield(buf, 2, 2, CARD16, "sequence number");
  printfield(buf, 4, 4, DVALUE4(0), "reply length");
  PrintField(buf, 8, 2, CARD16, "major-version");
  PrintField(buf, 10, 2, CARD16, "minor-version");
}
    
#define NextByte(dst)  if (!len--) { error = "Out of data"; break; } dst = *data++;

static void
WcpAnalyzeImage1RLL (
    const char *buf,
    int	    len,
    int	    width,
    int	    height)
{
    unsigned char   byte;
    unsigned char   *data = (unsigned char *) buf;
    int		    x, y;
    char	    *error = 0;
    int		    bytewidth;
    int		    i;
    int		    w;
    
    y = 0;
    bytewidth = (width +  7) >> 3;
    while (!error && height--)
    {
	x = 0;
	if (Verbose > 2)
	    fprintf (stdout, "%s %9d %9d:", Leader, y, (char *) data - buf);
	NextByte (byte);
	if (Verbose > 2)
	    fprintf (stdout, " %2x", byte);
	switch (byte) {
	case 0xff:
	    for (i = 0; i < bytewidth; i++)
	    {
		NextByte(byte);
	    }
	    break;
	default:
	    w = width;
	    while (w)
	    {
		NextByte(byte);
		if (Verbose > 2)
		    fprintf (stdout, " %3d", byte);
		if (byte > w)
		{
		    error = "bad run";
		    break;
		}
		x += byte;
		w -= byte;
	    }
	    break;
	}
	if (Verbose > 2)
	    fprintf (stdout, "\n");
	y++;
    }
    if (error)
	fprintf (stdout, "%s%20s: %d, %d %d\n", Leader, error, 
		 x, y - 1, (char *) data - buf);
}

static void
WcpAnalyzeImageNRLL (
    const char *buf,
    int	    len,
    int	    width,
    int	    height,
    int	    bytes)
{
}

#define MAX_LRU_SHORT	0xe
#define LRU_LONG	0xf
#define MAX_LRU_RUN	(MAX_LRU_SHORT + 0xff)

#define LRU_CACHE	0xf
#define LRU_MISS	0xf

typedef unsigned long	PIXEL;

static void
init_cache (
    PIXEL   cache[LRU_CACHE],
    int	    BPP)
{
    int	    e;
    
    /*
     * initialize cache values
     */
    for (e = 0; e < 8; e++)
    {
	cache[e] = e;
	cache[LRU_CACHE-e-1] = ((1 << BPP) - 1) - e;
    }
}

static void
push (
    PIXEL cache[LRU_CACHE],
    PIXEL pixel)
{
    int    e;

    e = LRU_CACHE - 1;
    while (--e >= 0)
	cache[e+1] = cache[e];
    cache[0] = pixel;
}

static PIXEL
use (
    PIXEL  cache[LRU_CACHE],
    int    e)
{
    PIXEL   tmp;

    tmp = cache[e];
    if (e)
    {
	while (--e >= 0)
	    cache[e+1] = cache[e];
	cache[0] = tmp;
    }
    return tmp;
}

static int
match (
    PIXEL	cache[LRU_CACHE],
    PIXEL	pixel)
{
    int	    e;

    for (e = 0; e < LRU_CACHE; e++)
        if (cache[e] == pixel)
	{
	    use (cache, e);
            return e;
	}
    push (cache, pixel);
    return LRU_MISS;
}

static void
WcpAnalyzeImageNLRU (
    const char *buf,
    int	    len,
    int	    width,
    int	    height,
    int	    bytes)
{
    unsigned char   *data = (unsigned char *) buf;
    unsigned char   byte;
    int		    x, y;
    int		    w;
    int		    i;
    int		    bytewidth;
    int		    cache;
    unsigned long   run;
    PIXEL	    lru[LRU_CACHE];
    PIXEL	    pix;
    char	    *error = 0;

    y = 0;
    bytewidth = width * bytes;
    while (!error && height--)
    {
	x = 0;
	if (Verbose > 2)
	    fprintf (stdout, "%s %9d %9d:", Leader, y, (char *) data - buf);
	NextByte (byte);
	if (Verbose > 2)
	    fprintf (stdout, " %2x", byte);
	switch (byte) {
	case 0xff:
	    for (i = 0; i < bytewidth; i++)
	    {
		NextByte (byte);
	    }
	    break;
	default:
	    init_cache (lru, bytes * 8);
	    w = width;
	    while (w)
	    {
		NextByte (byte);
		run = byte >> 4;
		cache = byte & 0xf;
		if (run == LRU_LONG)
		{
		    NextByte (byte);
		    run = (int) byte + MAX_LRU_SHORT;
		}
		run++;
		if (cache == LRU_MISS)
		{
		    pix = 0;
		    for (i = 0; i < bytes; i++)
		    {
			pix = pix << 8;
			NextByte (byte);
			pix |= byte;
		    }
		    push (lru, pix);
		}
		else
		{
		    pix = use (lru, cache);
		}
		w -= run;
		if (w < 0)
		{
		    error = "bad run";
		    break;
		}
		x += run;
		fprintf (stdout, " %3d:%0*x", run, bytes * 2, pix);
	    }
	    break;
	}
	if (Verbose > 2)
	    fprintf (stdout, "\n");
	y++;
    }
    if (error)
	fprintf (stdout, "%s%20s: %d, %d %d\n", Leader, error, 
		 x, y - 1, (char *) data - buf);
}

static void
WcpAnalyzeImage (
    const char *buf,
    int	    len,
    int	    depth,
    int	    encoding,
    int	    width,
    int	    height)
{
    int	    bytes;
    
    bytes = depth / 8;
    switch (depth) {
    case 1:
	switch (encoding) {
	case 1:
	case 2:
	case 3:
	case 4:
	    WcpAnalyzeImage1RLL (buf, len, width, height);
	    return;
	default:
	    break;
	}
	break;
    case 4:
	switch (encoding) {
	case 1:
	case 2:
	case 3:
	case 4:
	default:
	    break;
	}
	break;
    case 8:
    case 16:
    case 24:
	switch (encoding) {
	case 1:
	case 2:
	case 3:
	    WcpAnalyzeImageNRLL (buf, len, width, height, bytes);
	    return;
	case 4:
	    WcpAnalyzeImageNLRU (buf, len, width, height, bytes);
	    return;
	default:
	    break;
	}
	break;
    }
    fprintf (stdout, "%s%20s: ", Leader, "invalid");
    fprintf (stdout, "encoding %d depth %d\n", encoding, depth);
}

void
WcpPutImage (
    FD fd,
    const unsigned char *buf)
{
  int n;
  PrintField (buf, 0, 1, REQUEST, REQUESTHEADER) /* WcpRequest */ ;
  PrintField (buf, 1, 1, WCPREQUEST, WCPREQUESTHEADER) /* WcpSwitch */ ;
  if (Verbose < 1)
    return;
  if (Verbose > 1)
    PrintField(SBf, 0, 4, CARD32, "sequence number");

  n = (IShort(&buf[2]) - 7) * 4;
  printfield(buf, 2, 2, DVALUE2(7 + (n + p) / 4), "request length");
  PrintField(buf, 4, 4, DRAWABLE, "drawable");
  PrintField(buf, 8, 4, GCONTEXT, "gc");
  if (Verbose > 2)
    PrintValueRec (ILong(&buf[8]),
		   GC_function|
		   GC_plane_mask|
		   GC_foreground|
		   GC_background,
		   GC_BITMASK);
  PrintField(buf, 12, 4, CARD32, "lut");
  PrintField(buf, 16, 2, INT16, "dst-x");
  PrintField(buf, 18, 2, INT16, "dst-y");
  PrintField(buf, 20, 2, CARD16, "width");
  PrintField(buf, 22, 2, CARD16, "height");
  PrintField(buf, 24, 1, CARD8, "depth");
  PrintField(buf, 25, 1, CARD8, "encoding");
  PrintField(buf, 26, 1, CARD8, "left-pad");
  WcpAnalyzeImage ((const char *) &buf[28], (long) n, buf[24], buf[25],
		   IShort(&buf[20]) + buf[26],
		   IShort(&buf[22]));
  PrintBytes(&buf[28], (long)n, "data");
}

void
WcpGetImage (
    FD fd,
    const unsigned char *buf)
{
  PrintField (buf, 0, 1, REQUEST, REQUESTHEADER) /* WcpRequest */ ;
  PrintField (buf, 1, 1, WCPREQUEST, WCPREQUESTHEADER) /* WcpSwitch */ ;
  if (Verbose < 1)
    return;
  if (Verbose > 1)
    PrintField(SBf, 0, 4, CARD32, "sequence number");

  printfield(buf, 2, 2, CONST2(2), "request length");
  PrintField(buf, 4, 4, DRAWABLE, "drawable");
  PrintField(buf, 8, 4, CARD32, "lut");
  PrintField(buf, 12, 2, INT16, "x");
  PrintField(buf, 14, 2, INT16, "y");
  PrintField(buf, 16, 2, CARD16, "width");
  PrintField(buf, 18, 2, CARD16, "height");
  PrintField(buf, 20, 1, CARD8, "encoding");
}

void
WcpGetImageReply (
    FD fd,
    const unsigned char *buf)
{
  long	  n;

  PrintField(RBf, 0, 1, REPLY, REPLYHEADER) /* WcpRequest reply */ ;
  PrintField(RBf, 1, 1, WCPREPLY, WCPREPLYHEADER) /* WcpGetImage reply */;
  if (Verbose < 1)
    return;
  printfield(buf, 2, 2, CARD16, "sequence number");
  printfield(buf, 4, 4, DVALUE4(n), "reply length");
  PrintField(buf, 8, 4, CARD32, "visual");
  n = ILong (&buf[4]) * 4;
  PrintBytes (&buf[32], n, "data");
}

void
WcpCreateColorCursor (
    FD fd,
    const unsigned char *buf)
{
  PrintField (buf, 0, 1, REQUEST, REQUESTHEADER) /* WcpRequest */ ;
  PrintField (buf, 1, 1, WCPREQUEST, WCPREQUESTHEADER) /* WcpSwitch */ ;
  if (Verbose < 1)
    return;
  if (Verbose > 1)
    PrintField(SBf, 0, 4, CARD32, "sequence number");

  printfield(buf, 2, 2, CONST2(2), "request length");
}

void
WcpCreateLut (
    FD fd,
    const unsigned char *buf)
{
  PrintField (buf, 0, 1, REQUEST, REQUESTHEADER) /* WcpRequest */ ;
  PrintField (buf, 1, 1, WCPREQUEST, WCPREQUESTHEADER) /* WcpSwitch */ ;
  if (Verbose < 1)
    return;
  if (Verbose > 1)
    PrintField(SBf, 0, 4, CARD32, "sequence number");

  printfield(buf, 2, 2, CONST2(2), "request length");
}

void
WcpFreeLut (
    FD fd,
    const unsigned char *buf)
{
  PrintField (buf, 0, 1, REQUEST, REQUESTHEADER) /* WcpRequest */ ;
  PrintField (buf, 1, 1, WCPREQUEST, WCPREQUESTHEADER) /* WcpSwitch */ ;
  if (Verbose < 1)
    return;
  if (Verbose > 1)
    PrintField(SBf, 0, 4, CARD32, "sequence number");

  printfield(buf, 2, 2, CONST2(2), "request length");
}

void
WcpCopyArea (
    FD fd,
    const unsigned char *buf)
{
  PrintField (buf, 0, 1, REQUEST, REQUESTHEADER) /* WcpRequest */ ;
  PrintField (buf, 1, 1, WCPREQUEST, WCPREQUESTHEADER) /* WcpSwitch */ ;
  if (Verbose < 1)
    return;
  if (Verbose > 1)
    PrintField(SBf, 0, 4, CARD32, "sequence number");

  printfield(buf, 2, 2, CONST2(2), "request length");
}