summaryrefslogtreecommitdiff
path: root/render/pixmap.c
blob: 57402d81f5d0b880dc0973b32c746342b1b6c102 (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
#include <fitz.h>

fz_error *
fz_newpixmap(fz_pixmap **pixp, int x, int y, int w, int h, int n, int a)
{
	fz_pixmap *pix;

	pix = *pixp = fz_malloc(sizeof (fz_pixmap));
	if (!pix)
		return fz_outofmem;

	pix->x = x;
	pix->y = y;
	pix->w = w;
	pix->h = h;
	pix->n = n;
	pix->a = a;
	pix->cs = nil;
	pix->stride = (pix->n + pix->a) * pix->w;

	pix->samples = fz_malloc(sizeof(short) * pix->stride * pix->h);
	if (!pix->samples) {
		fz_free(pix);
		return fz_outofmem;
	}

	memset(pix->samples, 0, sizeof(short) * pix->stride * pix->h);

	return nil;
}

void
fz_freepixmap(fz_pixmap *pix)
{
	fz_free(pix->samples);
	fz_free(pix);
}

void
fz_clearpixmap(fz_pixmap *pix)
{
	memset(pix->samples, 0, sizeof(short) * pix->stride * pix->h);
}

void
fz_debugpixmap(fz_pixmap *pix)
{
	int x, y;
	FILE *f = fopen("out.ppm", "w");
	fprintf(f, "P6\n%d %d\n255\n", pix->w, pix->h);
	for (y = 0; y < pix->h; y++)
		for (x = 0; x < pix->w; x++)
		{
			putc(255 - pix->samples[x + y * pix->stride], f);
			putc(255 - pix->samples[x + y * pix->stride], f);
			putc(255 - pix->samples[x + y * pix->stride], f);
		}
	fclose(f);
}