summaryrefslogtreecommitdiff
path: root/platform/x11/x11_image.c
blob: 329a58b87b0a4426aaa720ec1113d566eabeba14 (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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/*
 * Blit RGBA images to X with X(Shm)Images
 */

#ifndef _XOPEN_SOURCE
# define _XOPEN_SOURCE 1
#endif

#ifndef _XOPEN_SOURCE
# define _XOPEN_SOURCE 1
#endif

#define noSHOWINFO

#include "mupdf/fitz.h"

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>

extern int ffs(int);

static int is_big_endian(void)
{
	static const int one = 1;
	return *(char*)&one == 0;
}

typedef void (*ximage_convert_func_t)
(
	const unsigned char *src,
	int srcstride,
	unsigned char *dst,
	int dststride,
	int w,
	int h
	);

#define POOLSIZE 4
#define WIDTH 256
#define HEIGHT 256

enum {
	ARGB8888,
	BGRA8888,
	RGBA8888,
	ABGR8888,
	RGB888,
	BGR888,
	RGB565,
	RGB565_BR,
	RGB555,
	RGB555_BR,
	BGR233,
	UNKNOWN
};

#ifdef SHOWINFO
static char *modename[] = {
	"ARGB8888",
	"BGRA8888",
	"RGBA8888",
	"ABGR8888",
	"RGB888",
	"BGR888",
	"RGB565",
	"RGB565_BR",
	"RGB555",
	"RGB555_BR",
	"BGR233",
	"UNKNOWN"
};
#endif

extern ximage_convert_func_t ximage_convert_funcs[];

static struct
{
	Display *display;
	int screen;
	XVisualInfo visual;
	Colormap colormap;

	int bitsperpixel;
	int mode;

	XColor rgbcube[256];

	ximage_convert_func_t convert_func;

	int useshm;
	int shmcode;
	XImage *pool[POOLSIZE];
	/* MUST exist during the lifetime of the shared ximage according to the
	xc/doc/hardcopy/Xext/mit-shm.PS.gz */
	XShmSegmentInfo shminfo[POOLSIZE];
	int lastused;
} info;

static XImage *
createximage(Display *dpy, Visual *vis, XShmSegmentInfo *xsi, int depth, int w, int h)
{
	XImage *img;
	Status status;

	if (!XShmQueryExtension(dpy))
		goto fallback;
	if (!info.useshm)
		goto fallback;

	img = XShmCreateImage(dpy, vis, depth, ZPixmap, NULL, xsi, w, h);
	if (!img)
	{
		fprintf(stderr, "warn: could not XShmCreateImage\n");
		goto fallback;
	}

	xsi->shmid = shmget(IPC_PRIVATE,
		img->bytes_per_line * img->height,
		IPC_CREAT | 0777);
	if (xsi->shmid < 0)
	{
		XDestroyImage(img);
		fprintf(stderr, "warn: could not shmget\n");
		goto fallback;
	}

	img->data = xsi->shmaddr = shmat(xsi->shmid, NULL, 0);
	if (img->data == (char*)-1)
	{
		XDestroyImage(img);
		fprintf(stderr, "warn: could not shmat\n");
		goto fallback;
	}

	xsi->readOnly = False;
	status = XShmAttach(dpy, xsi);
	if (!status)
	{
		shmdt(xsi->shmaddr);
		XDestroyImage(img);
		fprintf(stderr, "warn: could not XShmAttach\n");
		goto fallback;
	}

	XSync(dpy, False);

	shmctl(xsi->shmid, IPC_RMID, NULL);

	return img;

fallback:
	info.useshm = 0;

	img = XCreateImage(dpy, vis, depth, ZPixmap, 0, NULL, w, h, 32, 0);
	if (!img)
	{
		fprintf(stderr, "fail: could not XCreateImage");
		abort();
	}

	img->data = malloc(h * img->bytes_per_line);
	if (!img->data)
	{
		fprintf(stderr, "fail: could not malloc");
		abort();
	}

	return img;
}

static void
make_colormap(void)
{
	if (info.visual.class == PseudoColor && info.visual.depth == 8)
	{
		int i, r, g, b;
		i = 0;
		for (b = 0; b < 4; b++) {
			for (g = 0; g < 8; g++) {
				for (r = 0; r < 8; r++) {
					info.rgbcube[i].pixel = i;
					info.rgbcube[i].red = (r * 36) << 8;
					info.rgbcube[i].green = (g * 36) << 8;
					info.rgbcube[i].blue = (b * 85) << 8;
					info.rgbcube[i].flags =
					DoRed | DoGreen | DoBlue;
					i++;
				}
			}
		}
		info.colormap = XCreateColormap(info.display,
			RootWindow(info.display, info.screen),
			info.visual.visual,
			AllocAll);
		XStoreColors(info.display, info.colormap, info.rgbcube, 256);
		return;
	}
	else if (info.visual.class == TrueColor)
	{
		info.colormap = 0;
		return;
	}
	fprintf(stderr, "Cannot handle visual class %d with depth: %d\n",
		info.visual.class, info.visual.depth);
	return;
}

static void
select_mode(void)
{

	int byteorder;
	int byterev;
	unsigned long rm, gm, bm;
	unsigned long rs, gs, bs;

	byteorder = ImageByteOrder(info.display);
	if (is_big_endian())
		byterev = byteorder != MSBFirst;
	else
		byterev = byteorder != LSBFirst;

	rm = info.visual.red_mask;
	gm = info.visual.green_mask;
	bm = info.visual.blue_mask;

	rs = ffs(rm) - 1;
	gs = ffs(gm) - 1;
	bs = ffs(bm) - 1;

#ifdef SHOWINFO
	printf("ximage: mode %d/%d %08lx %08lx %08lx (%ld,%ld,%ld) %s%s\n",
		info.visual.depth,
		info.bitsperpixel,
		rm, gm, bm, rs, gs, bs,
		byteorder == MSBFirst ? "msb" : "lsb",
		byterev ? " <swap>":"");
#endif

	info.mode = UNKNOWN;
	if (info.bitsperpixel == 8) {
		/* Either PseudoColor with BGR233 colormap, or TrueColor */
		info.mode = BGR233;
	}
	else if (info.bitsperpixel == 16) {
		if (rm == 0xF800 && gm == 0x07E0 && bm == 0x001F)
			info.mode = !byterev ? RGB565 : RGB565_BR;
		if (rm == 0x7C00 && gm == 0x03E0 && bm == 0x001F)
			info.mode = !byterev ? RGB555 : RGB555_BR;
	}
	else if (info.bitsperpixel == 24) {
		if (rs == 0 && gs == 8 && bs == 16)
			info.mode = byteorder == MSBFirst ? RGB888 : BGR888;
		if (rs == 16 && gs == 8 && bs == 0)
			info.mode = byteorder == MSBFirst ? BGR888 : RGB888;
	}
	else if (info.bitsperpixel == 32) {
		if (rs == 0 && gs == 8 && bs == 16)
			info.mode = byteorder == MSBFirst ? ABGR8888 : RGBA8888;
		if (rs == 8 && gs == 16 && bs == 24)
			info.mode = byteorder == MSBFirst ? BGRA8888 : ARGB8888;
		if (rs == 16 && gs == 8 && bs == 0)
			info.mode = byteorder == MSBFirst ? ARGB8888 : BGRA8888;
		if (rs == 24 && gs == 16 && bs == 8)
			info.mode = byteorder == MSBFirst ? RGBA8888 : ABGR8888;
	}

#ifdef SHOWINFO
	printf("ximage: RGBA8888 to %s\n", modename[info.mode]);
#endif

	/* select conversion function */
	info.convert_func = ximage_convert_funcs[info.mode];
}

static int
create_pool(void)
{
	int i;

	info.lastused = 0;

	for (i = 0; i < POOLSIZE; i++) {
		info.pool[i] = NULL;
	}

	for (i = 0; i < POOLSIZE; i++) {
		info.pool[i] = createximage(info.display,
			info.visual.visual, &info.shminfo[i], info.visual.depth,
			WIDTH, HEIGHT);
		if (!info.pool[i]) {
			return 0;
		}
	}

	return 1;
}

static XImage *
next_pool_image(void)
{
	if (info.lastused + 1 >= POOLSIZE) {
		if (info.useshm)
			XSync(info.display, False);
		else
			XFlush(info.display);
		info.lastused = 0;
	}
	return info.pool[info.lastused ++];
}

static int
ximage_error_handler(Display *display, XErrorEvent *event)
{
	/* Turn off shared memory images if we get an error from the MIT-SHM extension */
	if (event->request_code == info.shmcode)
	{
		char buf[80];
		XGetErrorText(display, event->error_code, buf, sizeof buf);
		fprintf(stderr, "ximage: disabling shared memory extension: %s\n", buf);
		info.useshm = 0;
		return 0;
	}

	XSetErrorHandler(NULL);
	return (XSetErrorHandler(ximage_error_handler))(display, event);
}

int
ximage_init(Display *display, int screen, Visual *visual)
{
	XVisualInfo template;
	XVisualInfo *visuals;
	int nvisuals;
	XPixmapFormatValues *formats;
	int nformats;
	int ok;
	int i;
	int major;
	int event;
	int error;

	info.display = display;
	info.screen = screen;
	info.colormap = 0;

	/* Get XVisualInfo for this visual */
	template.visualid = XVisualIDFromVisual(visual);
	visuals = XGetVisualInfo(display, VisualIDMask, &template, &nvisuals);
	if (nvisuals != 1) {
		fprintf(stderr, "Visual not found!\n");
		XFree(visuals);
		return 0;
	}
	memcpy(&info.visual, visuals, sizeof (XVisualInfo));
	XFree(visuals);

	/* Get appropriate PixmapFormat for this visual */
	formats = XListPixmapFormats(info.display, &nformats);
	for (i = 0; i < nformats; i++) {
		if (formats[i].depth == info.visual.depth) {
			info.bitsperpixel = formats[i].bits_per_pixel;
			break;
		}
	}
	XFree(formats);
	if (i == nformats) {
		fprintf(stderr, "PixmapFormat not found!\n");
		return 0;
	}

	/* extract mode */
	select_mode();

	/* prepare colormap */
	make_colormap();

	/* identify code for MIT-SHM extension */
	if (XQueryExtension(display, "MIT-SHM", &major, &event, &error) &&
		XShmQueryExtension(display))
		info.shmcode = major;

	/* intercept errors looking for SHM code */
	XSetErrorHandler(ximage_error_handler);

	/* prepare pool of XImages */
	info.useshm = 1;
	ok = create_pool();
	if (!ok)
		return 0;

#ifdef SHOWINFO
	printf("ximage: %sPutImage\n", info.useshm ? "XShm" : "X");
#endif

	return 1;
}

int
ximage_get_depth(void)
{
	return info.visual.depth;
}

Visual *
ximage_get_visual(void)
{
	return info.visual.visual;
}

Colormap
ximage_get_colormap(void)
{
	return info.colormap;
}

void
ximage_blit(Drawable d, GC gc,
	int dstx, int dsty,
	unsigned char *srcdata,
	int srcx, int srcy,
	int srcw, int srch,
	int srcstride)
{
	XImage *image;
	int ax, ay;
	int w, h;
	unsigned char *srcptr;

	for (ay = 0; ay < srch; ay += HEIGHT)
	{
		h = fz_mini(srch - ay, HEIGHT);
		for (ax = 0; ax < srcw; ax += WIDTH)
		{
			w = fz_mini(srcw - ax, WIDTH);

			image = next_pool_image();

			srcptr = srcdata +
			(ay + srcy) * srcstride +
			(ax + srcx) * 4;

			info.convert_func(srcptr, srcstride,
				(unsigned char *) image->data,
				image->bytes_per_line, w, h);

			if (info.useshm)
			{
				XShmPutImage(info.display, d, gc, image,
					0, 0, dstx + ax, dsty + ay,
					w, h, False);
			}
			else
			{
				XPutImage(info.display, d, gc, image,
					0, 0,
					dstx + ax,
					dsty + ay,
					w, h);
			}
		}
	}
}

/*
 * Primitive conversion functions
 */

#ifndef restrict
#ifndef _C99
#ifdef __GNUC__
#define restrict __restrict__
#else
#define restrict
#endif
#endif
#endif

#define PARAMS \
	const unsigned char * restrict src, \
	int srcstride, \
	unsigned char * restrict dst, \
	int dststride, \
	int w, \
	int h

/*
 * Convert byte:RGBA8888 to various formats
 */

static void
ximage_convert_argb8888(PARAMS)
{
	int x, y;
	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x ++) {
			dst[x * 4 + 0] = src[x * 4 + 3]; /* a */
			dst[x * 4 + 1] = src[x * 4 + 0]; /* r */
			dst[x * 4 + 2] = src[x * 4 + 1]; /* g */
			dst[x * 4 + 3] = src[x * 4 + 2]; /* b */
		}
		dst += dststride;
		src += srcstride;
	}
}

static void
ximage_convert_bgra8888(PARAMS)
{
	int x, y;
	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x++) {
			dst[x * 4 + 0] = src[x * 4 + 2];
			dst[x * 4 + 1] = src[x * 4 + 1];
			dst[x * 4 + 2] = src[x * 4 + 0];
			dst[x * 4 + 3] = src[x * 4 + 3];
		}
		dst += dststride;
		src += srcstride;
	}
}

static void
ximage_convert_abgr8888(PARAMS)
{
	int x, y;
	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x++) {
			dst[x * 4 + 0] = src[x * 4 + 3];
			dst[x * 4 + 1] = src[x * 4 + 2];
			dst[x * 4 + 2] = src[x * 4 + 1];
			dst[x * 4 + 3] = src[x * 4 + 0];
		}
		dst += dststride;
		src += srcstride;
	}
}

static void
ximage_convert_rgba8888(PARAMS)
{
	int x, y;
	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x++) {
			((unsigned *)dst)[x] = ((unsigned *)src)[x];
		}
		dst += dststride;
		src += srcstride;
	}
}

static void
ximage_convert_bgr888(PARAMS)
{
	int x, y;
	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x++) {
			dst[3*x + 0] = src[4*x + 2];
			dst[3*x + 1] = src[4*x + 1];
			dst[3*x + 2] = src[4*x + 0];
		}
		src += srcstride;
		dst += dststride;
	}
}

static void
ximage_convert_rgb888(PARAMS)
{
	int x, y;
	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x++) {
			dst[3*x + 0] = src[4*x + 0];
			dst[3*x + 1] = src[4*x + 1];
			dst[3*x + 2] = src[4*x + 2];
		}
		src += srcstride;
		dst += dststride;
	}
}

static void
ximage_convert_rgb565(PARAMS)
{
	unsigned char r, g, b;
	int x, y;
	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x++) {
			r = src[4*x + 0];
			g = src[4*x + 1];
			b = src[4*x + 2];
			((unsigned short *)dst)[x] =
			((r & 0xF8) << 8) |
			((g & 0xFC) << 3) |
			(b >> 3);
		}
		src += srcstride;
		dst += dststride;
	}
}

static void
ximage_convert_rgb565_br(PARAMS)
{
	unsigned char r, g, b;
	int x, y;
	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x++) {
			r = src[4*x + 0];
			g = src[4*x + 1];
			b = src[4*x + 2];
			/* final word is:
			g4 g3 g2 b7 b6 b5 b4 b3 : r7 r6 r5 r4 r3 g7 g6 g5
			*/
			((unsigned short *)dst)[x] =
			(r & 0xF8) |
			((g & 0xE0) >> 5) |
			((g & 0x1C) << 11) |
			((b & 0xF8) << 5);
		}
		src += srcstride;
		dst += dststride;
	}
}

static void
ximage_convert_rgb555(PARAMS)
{
	unsigned char r, g, b;
	int x, y;
	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x++) {
			r = src[4*x + 0];
			g = src[4*x + 1];
			b = src[4*x + 2];
			((unsigned short *)dst)[x] =
			((r & 0xF8) << 7) |
			((g & 0xF8) << 2) |
			(b >> 3);
		}
		src += srcstride;
		dst += dststride;
	}
}

static void
ximage_convert_rgb555_br(PARAMS)
{
	unsigned char r, g, b;
	int x, y;
	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x++) {
			r = src[4*x + 0];
			g = src[4*x + 1];
			b = src[4*x + 2];
			/* final word is:
			g5 g4 g3 b7 b6 b5 b4 b3 : 0 r7 r6 r5 r4 r3 g7 g6
			*/
			((unsigned short *)dst)[x] =
			((r & 0xF8) >> 1) |
			((g & 0xC0) >> 6) |
			((g & 0x38) << 10) |
			((b & 0xF8) << 5);
		}
		src += srcstride;
		dst += dststride;
	}
}

static void
ximage_convert_bgr233(PARAMS)
{
	unsigned char r, g, b;
	int x,y;
	for(y = 0; y < h; y++) {
		for(x = 0; x < w; x++) {
			r = src[4*x + 0];
			g = src[4*x + 1];
			b = src[4*x + 2];
			/* format: b7 b6 g7 g6 g5 r7 r6 r5 */
			dst[x] = (b&0xC0) | ((g>>2)&0x38) | ((r>>5)&0x7);
		}
		src += srcstride;
		dst += dststride;
	}
}

static void
ximage_convert_generic(PARAMS)
{
	unsigned long rm, gm, bm, rs, gs, bs, rx, bx, gx;
	unsigned long pixel;
	unsigned long r, g, b;
	int x, y;

	rm = info.visual.red_mask;
	gm = info.visual.green_mask;
	bm = info.visual.blue_mask;

	rs = ffs(rm) - 1;
	gs = ffs(gm) - 1;
	bs = ffs(bm) - 1;

	rx = ffs(~(rm >> rs)) - 1;
	gx = ffs(~(gm >> gs)) - 1;
	bx = ffs(~(bm >> bs)) - 1;

	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x++) {
			r = src[4*x + 0];
			g = src[4*x + 1];
			b = src[4*x + 2];

			/* adjust precision */
			if (rx < 8) r >>= (8 - rx); else if (rx > 8) r <<= (rx - 8);
			if (gx < 8) g >>= (8 - gx); else if (gx > 8) g <<= (gx - 8);
			if (bx < 8) b >>= (8 - bx); else if (bx > 8) b <<= (bx - 8);

			pixel = (r << rs) | (g << gs) | (b << bs);

			if (ImageByteOrder(info.display) == MSBFirst) {
				if (info.bitsperpixel > 16) {
					dst[4*x + 0] = (pixel >> 24) & 0xFF;
					dst[4*x + 1] = (pixel >> 16) & 0xFF;
					dst[4*x + 2] = (pixel >> 8) & 0xFF;
					dst[4*x + 3] = (pixel) & 0xFF;
				} else if (info.bitsperpixel > 8) {
					dst[2*x + 0] = (pixel >> 8) & 0xFF;
					dst[2*x + 1] = (pixel) & 0xFF;
				}
			} else {
				if (info.bitsperpixel > 16) {
					dst[4*x + 0] = (pixel) & 0xFF;
					dst[4*x + 1] = (pixel >> 8) & 0xFF;
					dst[4*x + 2] = (pixel >> 16) & 0xFF;
					dst[4*x + 3] = (pixel >> 24) & 0xFF;
				} else if (info.bitsperpixel > 8) {
					dst[2*x + 0] = (pixel) & 0xFF;
					dst[2*x + 1] = (pixel >> 8) & 0xFF;
				}
			}
		}
		src += srcstride;
		dst += dststride;
	}
}

ximage_convert_func_t ximage_convert_funcs[] = {
	ximage_convert_argb8888,
	ximage_convert_bgra8888,
	ximage_convert_rgba8888,
	ximage_convert_abgr8888,
	ximage_convert_rgb888,
	ximage_convert_bgr888,
	ximage_convert_rgb565,
	ximage_convert_rgb565_br,
	ximage_convert_rgb555,
	ximage_convert_rgb555_br,
	ximage_convert_bgr233,
	ximage_convert_generic
};