summaryrefslogtreecommitdiff
path: root/stream/filt_dcte.c
blob: 3ce0f21d94e16a41a3dd4fd6ffae00a07f8a7979 (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
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
250
251
252
253
254
255
#include "fitz-base.h"
#include "fitz-stream.h"

#include "filt_dctc.h"

typedef struct fz_dcte_s fz_dcte;

struct mydstmgr
{
	struct jpeg_destination_mgr super;
	fz_buffer *buf;
};

struct fz_dcte_s
{
	fz_filter super;

	struct jpeg_compress_struct cinfo;
	struct mydstmgr dst;
	struct myerrmgr err;
	int stage;

	int columns;
	int rows;
	int colors;
};

static void myinitdest(j_compress_ptr cinfo) { /* empty */ }
static boolean myemptybuf(j_compress_ptr cinfo) { return FALSE; }
static void mytermdest(j_compress_ptr cinfo) { /* empty */ }

fz_error *
fz_newdcte(fz_filter **fp, fz_obj *params)
{
	fz_error *err;
	fz_obj *obj;
	int i;

	FZ_NEWFILTER(fz_dcte, e, dcte);

	e->stage = 0;

	obj = fz_dictgets(params, "Columns");
	if (!obj) { fz_free(e); return fz_throw("missing Columns parameter"); }
	e->columns = fz_toint(obj);

	obj = fz_dictgets(params, "Rows");
	if (!obj) { fz_free(e); return fz_throw("missing Rows parameter"); }
	e->rows = fz_toint(obj);

	obj = fz_dictgets(params, "Colors");
	if (!obj) { fz_free(e); return fz_throw("missing Colors parameter"); }
	e->colors = fz_toint(obj);

	/* setup error callback first thing */
	myiniterr(&e->err);
	e->cinfo.err = (struct jpeg_error_mgr*) &e->err;

	if (setjmp(e->err.jb))
	{
		err = fz_throw("cannot encode jpeg: %s", e->err.msg);
		fz_free(e);
		return err;
	}

	jpeg_create_compress(&e->cinfo);

	/* prepare destination manager */
	e->cinfo.dest = (struct jpeg_destination_mgr *) &e->dst;
	e->dst.super.init_destination = myinitdest;
	e->dst.super.empty_output_buffer = myemptybuf;
	e->dst.super.term_destination = mytermdest;

	e->dst.super.next_output_byte = nil;
	e->dst.super.free_in_buffer = 0;

	/* prepare required encoding options */
	e->cinfo.image_width = e->columns;
	e->cinfo.image_height = e->rows;
	e->cinfo.input_components = e->colors;

	switch (e->colors) {
		case 1: e->cinfo.in_color_space = JCS_GRAYSCALE; break;
		case 3: e->cinfo.in_color_space = JCS_RGB; break;
		case 4: e->cinfo.in_color_space = JCS_CMYK; break;
		default: e->cinfo.in_color_space = JCS_UNKNOWN; break;
	}

	jpeg_set_defaults(&e->cinfo);

	/* FIXME check this */
	obj = fz_dictgets(params, "ColorTransform");
	if (obj) {
		int colortransform = fz_toint(obj);
		if (e->colors == 3 && !colortransform)
			jpeg_set_colorspace(&e->cinfo, JCS_RGB);
		if (e->colors == 4 && colortransform)
			jpeg_set_colorspace(&e->cinfo, JCS_YCCK);
		if (e->colors == 4 && !colortransform)
			jpeg_set_colorspace(&e->cinfo, JCS_CMYK);
	}

	obj = fz_dictgets(params, "HSamples");
	if (obj && fz_isarray(obj)) {
		fz_obj *o;
		for (i = 0; i < e->colors; i++) {
			o = fz_arrayget(obj, i);
			e->cinfo.comp_info[i].h_samp_factor = fz_toint(o);
		}
	}

	obj = fz_dictgets(params, "VSamples");
	if (obj && fz_isarray(obj)) {
		fz_obj *o;
		for (i = 0; i < e->colors; i++) {
			o = fz_arrayget(obj, i);
			e->cinfo.comp_info[i].v_samp_factor = fz_toint(o);
		}
	}

	/* TODO: quant-tables and huffman-tables */

	return fz_okay;
}

void
fz_dropdcte(fz_filter *filter)
{
	fz_dcte *e = (fz_dcte*)filter;

	if (setjmp(e->err.jb)) {
		fz_warn("jpeg error: jpeg_destroy_compress: %s", e->err.msg);
		return;
	}

	jpeg_destroy_compress(&e->cinfo);
}

/* Adobe says zigzag order. IJG > v6a says natural order. */
#if JPEG_LIB_VERSION >= 61
#define unzigzag(x) unzigzagorder[x]
/* zigzag array position of n'th element of natural array order */
static const unsigned char unzigzagorder[] =
{
	0, 1, 5, 6, 14, 15, 27, 28,
	2, 4, 7, 13, 16, 26, 29, 42,
	3, 8, 12, 17, 25, 30, 41, 43,
	9, 11, 18, 24, 31, 40, 44, 53,
	10, 19, 23, 32, 39, 45, 52, 54,
	20, 22, 33, 38, 46, 51, 55, 60,
	21, 34, 37, 47, 50, 56, 59, 61,
	35, 36, 48, 49, 57, 58, 62, 63
};
#else
#define unzigzag(x) (x)
#endif

fz_error *
fz_setquanttables(fz_dcte *e, unsigned int **qtables, int qfactor)
{
	int i, j;
	unsigned int table[64];

	if (setjmp(e->err.jb)) {
		return fz_throw("jpeg error: jpeg_add_quant_table: %s", e->err.msg);
	}

	/* TODO: check for duplicate tables */

	for (i = 0; i < e->colors; i++) {
		for (j = 0; j < 64; j++) {
			table[j] = unzigzag(qtables[i][j]);
		}
		jpeg_add_quant_table(&e->cinfo, i, table, qfactor, TRUE);
		e->cinfo.comp_info[i].quant_tbl_no = i;
	}

	return fz_okay;
}

fz_error *
fz_processdcte(fz_filter *filter, fz_buffer *in, fz_buffer *out)
{
	fz_dcte *e = (fz_dcte*)filter;
	JSAMPROW scanline[1];
	int stride;
	int i;

	e->dst.buf = out;
	e->dst.super.free_in_buffer = out->ep - out->wp;
	e->dst.super.next_output_byte = out->wp;

	if (setjmp(e->err.jb))
	{
		return fz_throw("cannot encode jpeg: %s", e->err.msg);
	}

	switch (e->stage)
	{
		case 0:
			/* must have enough space for markers, typically 600 bytes or so */
			if (out->wp + 1024 > out->ep)
				goto needoutput;

			jpeg_start_compress(&e->cinfo, TRUE);

			/* TODO: write Adobe ColorTransform marker */

			/* fall through */
			e->stage = 1;

		case 1:
			stride = e->columns * e->colors;

			while (e->cinfo.next_scanline < e->cinfo.image_height)
			{
				if (in->rp + stride > in->wp)
					goto needinput;

				scanline[0] = in->rp;

				i = jpeg_write_scanlines(&e->cinfo, scanline, 1);

				if (i == 0)
					goto needoutput;

				in->rp += stride;
			}

			/* fall through */
			e->stage = 2;

		case 2:
			/* must have enough space for end markers */
			if (out->wp + 100 > out->ep)
				goto needoutput;

			/* finish compress cannot suspend! */
			jpeg_finish_compress(&e->cinfo);

			e->stage = 3;
			out->eof = 1;
			out->wp = out->ep - e->dst.super.free_in_buffer;
			return fz_iodone;
	}

needinput:
	out->wp = out->ep - e->dst.super.free_in_buffer;
	return fz_ioneedin;

needoutput:
	out->wp = out->ep - e->dst.super.free_in_buffer;
	return fz_ioneedout;
}