summaryrefslogtreecommitdiff
path: root/backend/epsonds-jpeg.c
blob: 62e8bb57c91de1315c89692d754aa5d7c06b1f51 (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
/*
 * epsonds-jpeg.c - Epson ESC/I-2 driver, JPEG support.
 *
 * Copyright (C) 2015 Tower Technologies
 * Author: Alessandro Zummo <a.zummo@towertech.it>
 *
 * This file is part of the SANE package.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 2.
 */

#define DEBUG_DECLARE_ONLY

#include "sane/config.h"

#include <math.h>

#include "epsonds.h"
#include "epsonds-jpeg.h"
#include "epsonds-ops.h"

#define min(A,B) (((A)<(B)) ? (A) : (B))

typedef struct
{
	struct jpeg_source_mgr pub;

	epsonds_scanner *s;
	JOCTET *buffer;

	SANE_Byte *linebuffer;
	SANE_Int linebuffer_size;
	SANE_Int linebuffer_index;
}
epsonds_src_mgr;

METHODDEF(void)
jpeg_init_source(j_decompress_ptr __sane_unused__ cinfo)
{
}

METHODDEF(void)
jpeg_term_source(j_decompress_ptr __sane_unused__ cinfo)
{
}

METHODDEF(boolean)
jpeg_fill_input_buffer(j_decompress_ptr cinfo)
{
	epsonds_src_mgr *src = (epsonds_src_mgr *)cinfo->src;
	int avail, size;

	/* read from the scanner or the ring buffer */

	avail = eds_ring_avail(src->s->current);
	if (avail == 0) {
		return FALSE;
	}

	/* read from scanner if no data? */
	size = min(1024, avail);

	eds_ring_read(src->s->current, src->buffer, size);

	src->pub.next_input_byte = src->buffer;
	src->pub.bytes_in_buffer = size;

	return TRUE;
}

METHODDEF (void)
jpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
{
	epsonds_src_mgr *src = (epsonds_src_mgr *)cinfo->src;

	if (num_bytes > 0) {

		while (num_bytes > (long)src->pub.bytes_in_buffer) {
			num_bytes -= (long)src->pub.bytes_in_buffer;
			jpeg_fill_input_buffer(cinfo);
		}

		src->pub.next_input_byte += (size_t) num_bytes;
		src->pub.bytes_in_buffer -= (size_t) num_bytes;
	}
}

SANE_Status
eds_jpeg_start(epsonds_scanner *s)
{
	epsonds_src_mgr *src;

	s->jpeg_cinfo.err = jpeg_std_error(&s->jpeg_err);

	jpeg_create_decompress(&s->jpeg_cinfo);

	s->jpeg_cinfo.src = (struct jpeg_source_mgr *)(*s->jpeg_cinfo.mem->alloc_small)((j_common_ptr)&s->jpeg_cinfo,
						JPOOL_PERMANENT, sizeof(epsonds_src_mgr));

	memset(s->jpeg_cinfo.src, 0x00, sizeof(epsonds_src_mgr));

	src = (epsonds_src_mgr *)s->jpeg_cinfo.src;
	src->s = s;

	src->buffer = (JOCTET *)(*s->jpeg_cinfo.mem->alloc_small)((j_common_ptr)&s->jpeg_cinfo,
							JPOOL_PERMANENT,
							1024 * sizeof(JOCTET));

	src->pub.init_source = jpeg_init_source;
	src->pub.fill_input_buffer = jpeg_fill_input_buffer;
	src->pub.skip_input_data = jpeg_skip_input_data;
	src->pub.resync_to_restart = jpeg_resync_to_restart;
	src->pub.term_source = jpeg_term_source;
	src->pub.bytes_in_buffer = 0;
	src->pub.next_input_byte = NULL;

	s->jpeg_header_seen = 0;

	return SANE_STATUS_GOOD;
}

SANE_Status
eds_jpeg_read_header(epsonds_scanner *s)
{
	epsonds_src_mgr *src = (epsonds_src_mgr *)s->jpeg_cinfo.src;

	if (jpeg_read_header(&s->jpeg_cinfo, TRUE)) {

		s->jdst = sanei_jpeg_jinit_write_ppm(&s->jpeg_cinfo);

		if (jpeg_start_decompress(&s->jpeg_cinfo)) {

			int size;

			DBG(3, "%s: w: %d, h: %d, components: %d\n",
				__func__,
				s->jpeg_cinfo.output_width, s->jpeg_cinfo.output_height,
				s->jpeg_cinfo.output_components);

			size = s->jpeg_cinfo.output_width * s->jpeg_cinfo.output_components * 1;

			src->linebuffer = (*s->jpeg_cinfo.mem->alloc_large)((j_common_ptr)&s->jpeg_cinfo,
				JPOOL_PERMANENT, size);

			src->linebuffer_size = 0;
			src->linebuffer_index = 0;

			s->jpeg_header_seen = 1;

			return SANE_STATUS_GOOD;

		} else {
			DBG(0, "%s: decompression failed\n", __func__);
			return SANE_STATUS_IO_ERROR;
		}
	} else {
		DBG(0, "%s: cannot read JPEG header\n", __func__);
		return SANE_STATUS_IO_ERROR;
	}
}

void
eds_jpeg_finish(epsonds_scanner *s)
{
	jpeg_destroy_decompress(&s->jpeg_cinfo);
}

void
eds_jpeg_read(SANE_Handle handle, SANE_Byte *data,
	   SANE_Int max_length, SANE_Int *length)
{
	epsonds_scanner *s = handle;

	struct jpeg_decompress_struct cinfo = s->jpeg_cinfo;
	epsonds_src_mgr *src = (epsonds_src_mgr *)s->jpeg_cinfo.src;

	int l;

	*length = 0;

	/* copy from line buffer if available */
	if (src->linebuffer_size && src->linebuffer_index < src->linebuffer_size) {

		*length = src->linebuffer_size - src->linebuffer_index;

		if (*length > max_length)
			*length = max_length;

		memcpy(data, src->linebuffer + src->linebuffer_index, *length);
		src->linebuffer_index += *length;

		return;
	}

	if (cinfo.output_scanline >= cinfo.output_height) {
		*length = 0;
		return;
	}

	/* scanlines of decompressed data will be in s->jdst->buffer
	 * only one line at time is supported
	 */

	l = jpeg_read_scanlines(&cinfo, s->jdst->buffer, 1);
	if (l == 0) {
		return;
	}

	/* from s->jdst->buffer to linebuffer
	 * linebuffer holds width * bytesperpixel
	 */

	(*s->jdst->put_pixel_rows)(&cinfo, s->jdst, 1, (char *)src->linebuffer);

	*length = cinfo.output_width * cinfo.output_components * 1;
	src->linebuffer_size = *length;
	src->linebuffer_index = 0;

	if (*length > max_length)
		*length = max_length;

	memcpy(data, src->linebuffer + src->linebuffer_index, *length);
	src->linebuffer_index += *length;
}