Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7643 views
1
#include "mupdf/fitz.h"
2
3
fz_bitmap *
4
fz_new_bitmap(fz_context *ctx, int w, int h, int n, int xres, int yres)
5
{
6
fz_bitmap *bit;
7
8
bit = fz_malloc_struct(ctx, fz_bitmap);
9
bit->refs = 1;
10
bit->w = w;
11
bit->h = h;
12
bit->n = n;
13
bit->xres = xres;
14
bit->yres = yres;
15
/* Span is 32 bit aligned. We may want to make this 64 bit if we
16
* use SSE2 etc. */
17
bit->stride = ((n * w + 31) & ~31) >> 3;
18
19
bit->samples = fz_malloc_array(ctx, h, bit->stride);
20
21
return bit;
22
}
23
24
fz_bitmap *
25
fz_keep_bitmap(fz_context *ctx, fz_bitmap *bit)
26
{
27
if (bit)
28
bit->refs++;
29
return bit;
30
}
31
32
void
33
fz_drop_bitmap(fz_context *ctx, fz_bitmap *bit)
34
{
35
if (bit && --bit->refs == 0)
36
{
37
fz_free(ctx, bit->samples);
38
fz_free(ctx, bit);
39
}
40
}
41
42
void
43
fz_clear_bitmap(fz_context *ctx, fz_bitmap *bit)
44
{
45
memset(bit->samples, 0, bit->stride * bit->h);
46
}
47
48
/*
49
* Write bitmap to PBM file
50
*/
51
52
void
53
fz_write_pbm(fz_context *ctx, fz_bitmap *bitmap, char *filename)
54
{
55
FILE *fp;
56
unsigned char *p;
57
int h, bytestride;
58
59
fp = fopen(filename, "wb");
60
if (!fp)
61
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno));
62
63
assert(bitmap->n == 1);
64
65
fprintf(fp, "P4\n%d %d\n", bitmap->w, bitmap->h);
66
67
p = bitmap->samples;
68
69
h = bitmap->h;
70
bytestride = (bitmap->w + 7) >> 3;
71
while (h--)
72
{
73
fwrite(p, 1, bytestride, fp);
74
p += bitmap->stride;
75
}
76
77
fclose(fp);
78
}
79
80
fz_colorspace *fz_pixmap_colorspace(fz_context *ctx, fz_pixmap *pix)
81
{
82
if (!pix)
83
return NULL;
84
return pix->colorspace;
85
}
86
87
int fz_pixmap_components(fz_context *ctx, fz_pixmap *pix)
88
{
89
if (!pix)
90
return 0;
91
return pix->n;
92
}
93
94
unsigned char *fz_pixmap_samples(fz_context *ctx, fz_pixmap *pix)
95
{
96
if (!pix)
97
return NULL;
98
return pix->samples;
99
}
100
101
void fz_bitmap_details(fz_bitmap *bit, int *w, int *h, int *n, int *stride)
102
{
103
if (!bit)
104
{
105
if (w)
106
*w = 0;
107
if (h)
108
*h = 0;
109
if (n)
110
*n = 0;
111
if (stride)
112
*stride = 0;
113
return;
114
}
115
if (w)
116
*w = bit->w;
117
if (h)
118
*h = bit->h;
119
if (n)
120
*n = bit->n;
121
if (stride)
122
*stride = bit->stride;
123
}
124
125