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
|
#include "mupdf/fitz.h"
/*
* Write pixmap to PNM file (without alpha channel)
*/
static void
pnm_write_header(fz_context *ctx, fz_band_writer *writer)
{
fz_output *out = writer->out;
int w = writer->w;
int h = writer->h;
int n = writer->n;
int alpha = writer->alpha;
n -= alpha;
if (n != 1 && n != 3)
fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap must be grayscale or rgb to write as pnm");
if (n == 1)
fz_write_printf(ctx, out, "P5\n");
if (n == 3)
fz_write_printf(ctx, out, "P6\n");
fz_write_printf(ctx, out, "%d %d\n", w, h);
fz_write_printf(ctx, out, "255\n");
}
static void
pnm_write_band(fz_context *ctx, fz_band_writer *writer, int stride, int band_start, int band_height, const unsigned char *p)
{
fz_output *out = writer->out;
int w = writer->w;
int h = writer->h;
int n = writer->n;
int alpha = writer->alpha;
char buffer[2*3*4*5*6]; /* Buffer must be a multiple of 2 and 3 at least. */
int len;
int end = band_start + band_height;
if (n-alpha != 1 && n-alpha != 3)
fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap must be grayscale or rgb to write as pnm");
if (!out)
return;
if (end > h)
end = h;
end -= band_start;
/* Tests show that writing single bytes out at a time
* is appallingly slow. We get a huge improvement
* by collating stuff into buffers first. */
while (end--)
{
len = w;
while (len)
{
int num_written = len;
switch (n)
{
case 1:
/* No collation required */
fz_write_data(ctx, out, p, num_written);
p += num_written;
break;
case 2:
{
char *o = buffer;
int count;
if (num_written > sizeof(buffer))
num_written = sizeof(buffer);
for (count = num_written; count; count--)
{
*o++ = *p;
p += 2;
}
fz_write_data(ctx, out, buffer, num_written);
break;
}
case 3:
fz_write_data(ctx, out, p, num_written*3);
p += num_written*3;
break;
case 4:
{
char *o = buffer;
int count;
if (num_written > sizeof(buffer)/3)
num_written = sizeof(buffer)/3;
for (count = num_written; count; count--)
{
*o++ = p[0];
*o++ = p[1];
*o++ = p[2];
p += n;
}
fz_write_data(ctx, out, buffer, num_written * 3);
break;
}
}
len -= num_written;
}
p += stride - w*n;
}
}
fz_band_writer *fz_new_pnm_band_writer(fz_context *ctx, fz_output *out)
{
fz_band_writer *writer = fz_new_band_writer(ctx, fz_band_writer, out);
writer->header = pnm_write_header;
writer->band = pnm_write_band;
return writer;
}
void
fz_write_pixmap_as_pnm(fz_context *ctx, fz_output *out, fz_pixmap *pixmap)
{
fz_band_writer *writer = fz_new_pnm_band_writer(ctx, out);
fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, 0, 0, 0);
fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
fz_drop_band_writer(ctx, writer);
}
void
fz_save_pixmap_as_pnm(fz_context *ctx, fz_pixmap *pixmap, const char *filename)
{
fz_band_writer *writer = NULL;
fz_output *out = fz_new_output_with_path(ctx, filename, 0);
fz_var(writer);
fz_try(ctx)
{
writer = fz_new_pnm_band_writer(ctx, out);
fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, 0, 0, 0);
fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
}
fz_always(ctx)
{
fz_drop_band_writer(ctx, writer);
fz_drop_output(ctx, out);
}
fz_catch(ctx)
fz_rethrow(ctx);
}
/*
* Write pixmap to PAM file (with or without alpha channel)
*/
static void
pam_write_header(fz_context *ctx, fz_band_writer *writer)
{
fz_output *out = writer->out;
int w = writer->w;
int h = writer->h;
int n = writer->n;
int alpha = writer->alpha;
fz_write_printf(ctx, out, "P7\n");
fz_write_printf(ctx, out, "WIDTH %d\n", w);
fz_write_printf(ctx, out, "HEIGHT %d\n", h);
fz_write_printf(ctx, out, "DEPTH %d\n", n);
fz_write_printf(ctx, out, "MAXVAL 255\n");
n -= alpha;
if (n == 0 && alpha) fz_write_printf(ctx, out, "TUPLTYPE GRAYSCALE\n");
else if (n == 1 && !alpha) fz_write_printf(ctx, out, "TUPLTYPE GRAYSCALE\n");
else if (n == 1 && alpha) fz_write_printf(ctx, out, "TUPLTYPE GRAYSCALE_ALPHA\n");
else if (n == 3 && !alpha) fz_write_printf(ctx, out, "TUPLTYPE RGB\n");
else if (n == 3 && alpha) fz_write_printf(ctx, out, "TUPLTYPE RGB_ALPHA\n");
else if (n == 4 && !alpha) fz_write_printf(ctx, out, "TUPLTYPE CMYK\n");
else if (n == 5) fz_write_printf(ctx, out, "TUPLTYPE CMYK_ALPHA\n");
fz_write_printf(ctx, out, "ENDHDR\n");
}
static void
pam_write_band(fz_context *ctx, fz_band_writer *writer, int stride, int band_start, int band_height, const unsigned char *sp)
{
fz_output *out = writer->out;
int w = writer->w;
int h = writer->h;
int n = writer->n;
int y;
int end = band_start + band_height;
if (!out)
return;
if (end > h)
end = h;
end -= band_start;
for (y = 0; y < end; y++)
{
fz_write_data(ctx, out, sp, w * n);
sp += stride;
}
}
fz_band_writer *fz_new_pam_band_writer(fz_context *ctx, fz_output *out)
{
fz_band_writer *writer = fz_new_band_writer(ctx, fz_band_writer, out);
writer->header = pam_write_header;
writer->band = pam_write_band;
return writer;
}
void
fz_write_pixmap_as_pam(fz_context *ctx, fz_output *out, fz_pixmap *pixmap)
{
fz_band_writer *writer = fz_new_pam_band_writer(ctx, out);
fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, 0, 0, 0);
fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
fz_drop_band_writer(ctx, writer);
}
void
fz_save_pixmap_as_pam(fz_context *ctx, fz_pixmap *pixmap, const char *filename)
{
fz_band_writer *writer = NULL;
fz_output *out = fz_new_output_with_path(ctx, filename, 0);
fz_var(writer);
fz_try(ctx)
{
writer = fz_new_pam_band_writer(ctx, out);
fz_write_header(ctx, writer, pixmap->w, pixmap->h, pixmap->n, pixmap->alpha, 0, 0, 0);
fz_write_band(ctx, writer, pixmap->stride, pixmap->h, pixmap->samples);
}
fz_always(ctx)
{
fz_drop_band_writer(ctx, writer);
fz_drop_output(ctx, out);
}
fz_catch(ctx)
fz_rethrow(ctx);
}
|