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
|
#include <fitz.h>
fz_error *
fz_newpixmap(fz_pixmap **pixp, int x, int y, int w, int h, int n)
{
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->samples = fz_malloc(pix->w * pix->h * pix->n * sizeof(fz_sample));
if (!pix->samples) {
fz_free(pix);
return fz_outofmem;
}
return nil;
}
fz_error *
fz_newpixmapwithrect(fz_pixmap **pixp, fz_irect r, int n)
{
return fz_newpixmap(pixp,
r.min.x, r.min.y,
r.max.x - r.min.x,
r.max.y - r.min.y, n);
}
fz_error *
fz_newpixmapcopy(fz_pixmap **pixp, fz_pixmap *old)
{
fz_error *error;
error = fz_newpixmap(pixp, old->x, old->y, old->w, old->h, old->n);
if (error)
return error;
memcpy((*pixp)->samples, old->samples, old->w * old->h * old->n);
return nil;
}
void
fz_droppixmap(fz_pixmap *pix)
{
fz_free(pix->samples);
fz_free(pix);
}
void
fz_clearpixmap(fz_pixmap *pix)
{
memset(pix->samples, 0, pix->w * pix->h * pix->n * sizeof(fz_sample));
}
void
fz_gammapixmap(fz_pixmap *pix, float gamma)
{
unsigned char table[255];
int n = pix->w * pix->h * pix->n;
unsigned char *p = pix->samples;
int i;
for (i = 0; i < 256; i++)
table[i] = CLAMP(pow(i / 255.0, gamma) * 255.0, 0, 255);
while (n--)
*p = table[*p]; p++;
}
void
fz_debugpixmap(fz_pixmap *pix)
{
if (pix->n == 4)
{
int x, y;
FILE *ppm = fopen("out.ppm", "w");
FILE *pgm = fopen("out.pgm", "w");
fprintf(ppm, "P6\n%d %d\n255\n", pix->w, pix->h);
fprintf(pgm, "P5\n%d %d\n255\n", pix->w, pix->h);
for (y = 0; y < pix->h; y++)
for (x = 0; x < pix->w; x++)
{
int a = pix->samples[x * pix->n + y * pix->w * pix->n + 0];
int r = pix->samples[x * pix->n + y * pix->w * pix->n + 1];
int g = pix->samples[x * pix->n + y * pix->w * pix->n + 2];
int b = pix->samples[x * pix->n + y * pix->w * pix->n + 3];
putc(a, pgm);
putc(r, ppm);
putc(g, ppm);
putc(b, ppm);
}
fclose(ppm);
fclose(pgm);
}
else if (pix->n == 2)
{
int x, y;
FILE *pgm = fopen("out.pgm", "w");
fprintf(pgm, "P5\n%d %d\n255\n", pix->w, pix->h);
for (y = 0; y < pix->h; y++)
for (x = 0; x < pix->w; x++)
{
putc(pix->samples[y * pix->w * 2 + x * 2 + 1], pgm);
}
fclose(pgm);
}
else if (pix->n == 1)
{
FILE *pgm = fopen("out.pgm", "w");
fprintf(pgm, "P5\n%d %d\n255\n", pix->w, pix->h);
fwrite(pix->samples, 1, pix->w * pix->h, pgm);
fclose(pgm);
}
}
|