File: rdbitmap.c

package info (click to toggle)
wily 0.13.41-7.3
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,560 kB
  • ctags: 3,426
  • sloc: ansic: 25,364; perl: 580; makefile: 448; sh: 415; python: 30; exp: 17
file content (62 lines) | stat: -rw-r--r-- 1,496 bytes parent folder | download | duplicates (18)
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
/* Copyright (c) 1992 AT&T - All rights reserved. */
#include <libc.h>
#include <libg.h>
#include "libgint.h"

void
rdbitmap(Bitmap *b, int miny, int maxy, unsigned char *data)
{
	XImage *gim, *eim;
	int x, y, w, h, pix, l, offset, px;
	int inld, outld;
	char *tdata;

	/*
	 * The XGetImage returned image may be wrong in a number of ways:
	 * wrong bit order, byte order, bit pad, scanline pad,
	 * and constant shift.
	 * So use a SLOW loop, for now
	 */
	w = Dx(b->r);
	h = maxy - miny;
	outld = b->ldepth;
	inld = (b->ldepth == 0) ? 0 : screen.ldepth;
	gim = XGetImage(_dpy, (Drawable)b->id, 0, miny - b->r.min.y,
			w, h, ~0, ZPixmap);
	px = 1<<(3-outld);	/* pixels per byte */
	/* set l to number of bytes of data per scan line */
	if(b->r.min.x >= 0)
		offset = b->r.min.x % px;
	else
		offset = px - b->r.min.x % px;
	l = (-b->r.min.x+px-1)/px;
	if(b->r.max.x >= 0)
		l += (b->r.max.x+px-1)/px;
	else
		l -= b->r.max.x/px;
	l *= h;
	if(l <= 0)
		return;
	tdata = (char *)malloc(l);
	if (tdata == (char *) 0)
		berror("rdbitmap malloc");
	eim = XCreateImage(_dpy, 0, 1 << inld, ZPixmap, 0, tdata,
			w+offset, h, 8, 0);
	eim->bitmap_pad = 8;
	eim->bitmap_bit_order = MSBFirst;
	eim->byte_order = MSBFirst;

	for(y = 0; y < h; y++)
		for(x = 0; x < w; x++) {
			pix = XGetPixel(gim, x, y);
			XPutPixel(eim, x+offset, y, pix);
		}

	if (inld == outld)
		memcpy((char *)data, tdata, l);
	else
		_ldconvert(tdata, inld, (char*)data, outld, w, h);

	XDestroyImage(gim);
	XDestroyImage(eim);
}