1141cc406Sopenharmony_ci/* sane - Scanner Access Now Easy.
2141cc406Sopenharmony_ci
3141cc406Sopenharmony_ci   Copyright (C) 2002 Sergey Vlasov <vsu@altlinux.ru>
4141cc406Sopenharmony_ci   Copyright (C) 2002-2007 Henning Geinitz <sane@geinitz.org>
5141cc406Sopenharmony_ci
6141cc406Sopenharmony_ci   This file is part of the SANE package.
7141cc406Sopenharmony_ci
8141cc406Sopenharmony_ci   This program is free software; you can redistribute it and/or
9141cc406Sopenharmony_ci   modify it under the terms of the GNU General Public License as
10141cc406Sopenharmony_ci   published by the Free Software Foundation; either version 2 of the
11141cc406Sopenharmony_ci   License, or (at your option) any later version.
12141cc406Sopenharmony_ci
13141cc406Sopenharmony_ci   This program is distributed in the hope that it will be useful, but
14141cc406Sopenharmony_ci   WITHOUT ANY WARRANTY; without even the implied warranty of
15141cc406Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16141cc406Sopenharmony_ci   General Public License for more details.
17141cc406Sopenharmony_ci
18141cc406Sopenharmony_ci   You should have received a copy of the GNU General Public License
19141cc406Sopenharmony_ci   along with this program.  If not, see <https://www.gnu.org/licenses/>.
20141cc406Sopenharmony_ci
21141cc406Sopenharmony_ci   As a special exception, the authors of SANE give permission for
22141cc406Sopenharmony_ci   additional uses of the libraries contained in this release of SANE.
23141cc406Sopenharmony_ci
24141cc406Sopenharmony_ci   The exception is that, if you link a SANE library with other files
25141cc406Sopenharmony_ci   to produce an executable, this does not by itself cause the
26141cc406Sopenharmony_ci   resulting executable to be covered by the GNU General Public
27141cc406Sopenharmony_ci   License.  Your use of that executable is in no way restricted on
28141cc406Sopenharmony_ci   account of linking the SANE library code into it.
29141cc406Sopenharmony_ci
30141cc406Sopenharmony_ci   This exception does not, however, invalidate any other reasons why
31141cc406Sopenharmony_ci   the executable file might be covered by the GNU General Public
32141cc406Sopenharmony_ci   License.
33141cc406Sopenharmony_ci
34141cc406Sopenharmony_ci   If you submit changes to SANE to the maintainers to be included in
35141cc406Sopenharmony_ci   a subsequent release, you agree by submitting the changes that
36141cc406Sopenharmony_ci   those changes may be distributed with this exception intact.
37141cc406Sopenharmony_ci
38141cc406Sopenharmony_ci   If you write modifications of your own for SANE, it is your choice
39141cc406Sopenharmony_ci   whether to permit this exception to apply to your modifications.
40141cc406Sopenharmony_ci   If you do not wish that, delete this exception notice.
41141cc406Sopenharmony_ci*/
42141cc406Sopenharmony_ci
43141cc406Sopenharmony_ci#include "gt68xx_mid.h"
44141cc406Sopenharmony_ci#include "gt68xx_low.c"
45141cc406Sopenharmony_ci
46141cc406Sopenharmony_ci/** @file
47141cc406Sopenharmony_ci * @brief Image data unpacking.
48141cc406Sopenharmony_ci */
49141cc406Sopenharmony_ci
50141cc406Sopenharmony_cistatic SANE_Status
51141cc406Sopenharmony_cigt68xx_delay_buffer_init (GT68xx_Delay_Buffer * delay,
52141cc406Sopenharmony_ci			  SANE_Int pixels_per_line, SANE_Int delay_count)
53141cc406Sopenharmony_ci{
54141cc406Sopenharmony_ci  SANE_Int bytes_per_line;
55141cc406Sopenharmony_ci  SANE_Int line_count, i;
56141cc406Sopenharmony_ci
57141cc406Sopenharmony_ci  if (pixels_per_line <= 0)
58141cc406Sopenharmony_ci    {
59141cc406Sopenharmony_ci      DBG (3, "gt68xx_delay_buffer_init: BUG: pixels_per_line=%d\n",
60141cc406Sopenharmony_ci	   pixels_per_line);
61141cc406Sopenharmony_ci      return SANE_STATUS_INVAL;
62141cc406Sopenharmony_ci    }
63141cc406Sopenharmony_ci
64141cc406Sopenharmony_ci  if (delay_count < 0)
65141cc406Sopenharmony_ci    {
66141cc406Sopenharmony_ci      DBG (3, "gt68xx_delay_buffer_init: BUG: delay_count=%d\n", delay_count);
67141cc406Sopenharmony_ci      return SANE_STATUS_INVAL;
68141cc406Sopenharmony_ci    }
69141cc406Sopenharmony_ci
70141cc406Sopenharmony_ci  bytes_per_line = pixels_per_line * sizeof (unsigned int);
71141cc406Sopenharmony_ci
72141cc406Sopenharmony_ci  delay->line_count = line_count = delay_count + 1;
73141cc406Sopenharmony_ci  delay->read_index = 0;
74141cc406Sopenharmony_ci  delay->write_index = delay_count;
75141cc406Sopenharmony_ci
76141cc406Sopenharmony_ci  delay->mem_block = (SANE_Byte *) malloc (bytes_per_line * line_count);
77141cc406Sopenharmony_ci  if (!delay->mem_block)
78141cc406Sopenharmony_ci    {
79141cc406Sopenharmony_ci      DBG (3, "gt68xx_delay_buffer_init: no memory for delay block\n");
80141cc406Sopenharmony_ci      return SANE_STATUS_NO_MEM;
81141cc406Sopenharmony_ci    }
82141cc406Sopenharmony_ci  /* make sure that we will see if one of the uninitialized lines get displayed */
83141cc406Sopenharmony_ci  for (i = 0; i < bytes_per_line * line_count; i++)
84141cc406Sopenharmony_ci    delay->mem_block[i] = i % 256;
85141cc406Sopenharmony_ci
86141cc406Sopenharmony_ci  delay->lines =
87141cc406Sopenharmony_ci    (unsigned int **) malloc (sizeof (unsigned int *) * line_count);
88141cc406Sopenharmony_ci  if (!delay->lines)
89141cc406Sopenharmony_ci    {
90141cc406Sopenharmony_ci      free (delay->mem_block);
91141cc406Sopenharmony_ci      DBG (3,
92141cc406Sopenharmony_ci	   "gt68xx_delay_buffer_init: no memory for delay line pointers\n");
93141cc406Sopenharmony_ci      return SANE_STATUS_NO_MEM;
94141cc406Sopenharmony_ci    }
95141cc406Sopenharmony_ci
96141cc406Sopenharmony_ci  for (i = 0; i < line_count; ++i)
97141cc406Sopenharmony_ci    delay->lines[i] =
98141cc406Sopenharmony_ci      (unsigned int *) (delay->mem_block + i * bytes_per_line);
99141cc406Sopenharmony_ci
100141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
101141cc406Sopenharmony_ci}
102141cc406Sopenharmony_ci
103141cc406Sopenharmony_cistatic SANE_Status
104141cc406Sopenharmony_cigt68xx_delay_buffer_done (GT68xx_Delay_Buffer * delay)
105141cc406Sopenharmony_ci{
106141cc406Sopenharmony_ci  if (delay->lines)
107141cc406Sopenharmony_ci    {
108141cc406Sopenharmony_ci      free (delay->lines);
109141cc406Sopenharmony_ci      delay->lines = NULL;
110141cc406Sopenharmony_ci    }
111141cc406Sopenharmony_ci
112141cc406Sopenharmony_ci  if (delay->mem_block)
113141cc406Sopenharmony_ci    {
114141cc406Sopenharmony_ci      free (delay->mem_block);
115141cc406Sopenharmony_ci      delay->mem_block = NULL;
116141cc406Sopenharmony_ci    }
117141cc406Sopenharmony_ci
118141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
119141cc406Sopenharmony_ci}
120141cc406Sopenharmony_ci
121141cc406Sopenharmony_ci#define DELAY_BUFFER_WRITE_PTR(delay) ( (delay)->lines[(delay)->write_index] )
122141cc406Sopenharmony_ci
123141cc406Sopenharmony_ci#define DELAY_BUFFER_SELECT_PTR(delay,dist) \
124141cc406Sopenharmony_ci ((delay)->lines[((delay)->read_index + (dist)) % (delay)->line_count])
125141cc406Sopenharmony_ci
126141cc406Sopenharmony_ci#define DELAY_BUFFER_READ_PTR(delay)  ( (delay)->lines[(delay)->read_index ] )
127141cc406Sopenharmony_ci
128141cc406Sopenharmony_ci#define DELAY_BUFFER_STEP(delay)                                               \
129141cc406Sopenharmony_ci  do                                                                           \
130141cc406Sopenharmony_ci    {                                                                          \
131141cc406Sopenharmony_ci      (delay)->read_index  = ((delay)->read_index  + 1) % (delay)->line_count; \
132141cc406Sopenharmony_ci      (delay)->write_index = ((delay)->write_index + 1) % (delay)->line_count; \
133141cc406Sopenharmony_ci    }                                                                          \
134141cc406Sopenharmony_ci   while (SANE_FALSE)
135141cc406Sopenharmony_ci
136141cc406Sopenharmony_ci
137141cc406Sopenharmony_cistatic inline void
138141cc406Sopenharmony_ciunpack_8_mono (SANE_Byte * src, unsigned int *dst, SANE_Int pixels_per_line)
139141cc406Sopenharmony_ci{
140141cc406Sopenharmony_ci  for (; pixels_per_line > 0; ++src, ++dst, --pixels_per_line)
141141cc406Sopenharmony_ci    {
142141cc406Sopenharmony_ci      *dst = (((unsigned int) *src) << 8) | *src;
143141cc406Sopenharmony_ci    }
144141cc406Sopenharmony_ci}
145141cc406Sopenharmony_ci
146141cc406Sopenharmony_cistatic inline void
147141cc406Sopenharmony_ciunpack_8_rgb (SANE_Byte * src, unsigned int *dst, SANE_Int pixels_per_line)
148141cc406Sopenharmony_ci{
149141cc406Sopenharmony_ci  for (; pixels_per_line > 0; src += 3, ++dst, --pixels_per_line)
150141cc406Sopenharmony_ci    {
151141cc406Sopenharmony_ci      *dst = (((unsigned int) *src) << 8) | *src;
152141cc406Sopenharmony_ci    }
153141cc406Sopenharmony_ci}
154141cc406Sopenharmony_ci
155141cc406Sopenharmony_ci/* 12-bit routines use the fact that pixels_per_line is aligned */
156141cc406Sopenharmony_ci
157141cc406Sopenharmony_cistatic inline void
158141cc406Sopenharmony_ciunpack_12_le_mono (SANE_Byte * src, unsigned int *dst,
159141cc406Sopenharmony_ci		   SANE_Int pixels_per_line)
160141cc406Sopenharmony_ci{
161141cc406Sopenharmony_ci  for (; pixels_per_line > 0; src += 3, dst += 2, pixels_per_line -= 2)
162141cc406Sopenharmony_ci    {
163141cc406Sopenharmony_ci      dst[0] = ((((unsigned int) (src[1] & 0x0f)) << 12)
164141cc406Sopenharmony_ci		| (((unsigned int) src[0]) << 4) | (src[1] & 0x0f));
165141cc406Sopenharmony_ci      dst[1] = ((((unsigned int) src[2]) << 8)
166141cc406Sopenharmony_ci		| (src[1] & 0xf0) | (((unsigned int) src[2]) >> 0x04));
167141cc406Sopenharmony_ci    }
168141cc406Sopenharmony_ci}
169141cc406Sopenharmony_ci
170141cc406Sopenharmony_cistatic inline void
171141cc406Sopenharmony_ciunpack_12_le_rgb (SANE_Byte * src,
172141cc406Sopenharmony_ci		  unsigned int *dst1,
173141cc406Sopenharmony_ci		  unsigned int *dst2,
174141cc406Sopenharmony_ci		  unsigned int *dst3, SANE_Int pixels_per_line)
175141cc406Sopenharmony_ci{
176141cc406Sopenharmony_ci  for (; pixels_per_line > 0; pixels_per_line -= 2)
177141cc406Sopenharmony_ci    {
178141cc406Sopenharmony_ci      *dst1++ = ((((unsigned int) (src[1] & 0x0f)) << 12)
179141cc406Sopenharmony_ci		 | (((unsigned int) src[0]) << 4) | (src[1] & 0x0f));
180141cc406Sopenharmony_ci      *dst2++ = ((((unsigned int) src[2]) << 8)
181141cc406Sopenharmony_ci		 | (src[1] & 0xf0) | (((unsigned int) src[2]) >> 0x04));
182141cc406Sopenharmony_ci      src += 3;
183141cc406Sopenharmony_ci
184141cc406Sopenharmony_ci      *dst3++ = ((((unsigned int) (src[1] & 0x0f)) << 12)
185141cc406Sopenharmony_ci		 | (((unsigned int) src[0]) << 4) | (src[1] & 0x0f));
186141cc406Sopenharmony_ci      *dst1++ = ((((unsigned int) src[2]) << 8)
187141cc406Sopenharmony_ci		 | (src[1] & 0xf0) | (((unsigned int) src[2]) >> 0x04));
188141cc406Sopenharmony_ci      src += 3;
189141cc406Sopenharmony_ci
190141cc406Sopenharmony_ci      *dst2++ = ((((unsigned int) (src[1] & 0x0f)) << 12)
191141cc406Sopenharmony_ci		 | (((unsigned int) src[0]) << 4) | (src[1] & 0x0f));
192141cc406Sopenharmony_ci      *dst3++ = ((((unsigned int) src[2]) << 8)
193141cc406Sopenharmony_ci		 | (src[1] & 0xf0) | (((unsigned int) src[2]) >> 0x04));
194141cc406Sopenharmony_ci      src += 3;
195141cc406Sopenharmony_ci    }
196141cc406Sopenharmony_ci}
197141cc406Sopenharmony_ci
198141cc406Sopenharmony_cistatic inline void
199141cc406Sopenharmony_ciunpack_16_le_mono (SANE_Byte * src, unsigned int *dst,
200141cc406Sopenharmony_ci		   SANE_Int pixels_per_line)
201141cc406Sopenharmony_ci{
202141cc406Sopenharmony_ci  for (; pixels_per_line > 0; src += 2, dst++, --pixels_per_line)
203141cc406Sopenharmony_ci    {
204141cc406Sopenharmony_ci      *dst = (((unsigned int) src[1]) << 8) | src[0];
205141cc406Sopenharmony_ci    }
206141cc406Sopenharmony_ci}
207141cc406Sopenharmony_ci
208141cc406Sopenharmony_cistatic inline void
209141cc406Sopenharmony_ciunpack_16_le_rgb (SANE_Byte * src, unsigned int *dst,
210141cc406Sopenharmony_ci		  SANE_Int pixels_per_line)
211141cc406Sopenharmony_ci{
212141cc406Sopenharmony_ci  for (; pixels_per_line > 0; src += 6, ++dst, --pixels_per_line)
213141cc406Sopenharmony_ci    {
214141cc406Sopenharmony_ci      *dst = (((unsigned int) src[1]) << 8) | src[0];
215141cc406Sopenharmony_ci    }
216141cc406Sopenharmony_ci}
217141cc406Sopenharmony_ci
218141cc406Sopenharmony_ci
219141cc406Sopenharmony_cistatic SANE_Status
220141cc406Sopenharmony_ciline_read_gray_8 (GT68xx_Line_Reader * reader,
221141cc406Sopenharmony_ci		  unsigned int **buffer_pointers_return)
222141cc406Sopenharmony_ci{
223141cc406Sopenharmony_ci  SANE_Status status;
224141cc406Sopenharmony_ci  size_t size;
225141cc406Sopenharmony_ci  unsigned int *buffer;
226141cc406Sopenharmony_ci
227141cc406Sopenharmony_ci  size = reader->params.scan_bpl;
228141cc406Sopenharmony_ci
229141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, reader->pixel_buffer, &size));
230141cc406Sopenharmony_ci
231141cc406Sopenharmony_ci  buffer = DELAY_BUFFER_READ_PTR (&reader->g_delay);
232141cc406Sopenharmony_ci  buffer_pointers_return[0] = buffer;
233141cc406Sopenharmony_ci  unpack_8_mono (reader->pixel_buffer, buffer, reader->pixels_per_line);
234141cc406Sopenharmony_ci
235141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
236141cc406Sopenharmony_ci}
237141cc406Sopenharmony_ci
238141cc406Sopenharmony_cistatic SANE_Status
239141cc406Sopenharmony_ciline_read_gray_double_8 (GT68xx_Line_Reader * reader,
240141cc406Sopenharmony_ci			 unsigned int **buffer_pointers_return)
241141cc406Sopenharmony_ci{
242141cc406Sopenharmony_ci  SANE_Status status;
243141cc406Sopenharmony_ci  size_t size;
244141cc406Sopenharmony_ci  unsigned int *buffer;
245141cc406Sopenharmony_ci  int i;
246141cc406Sopenharmony_ci
247141cc406Sopenharmony_ci  size = reader->params.scan_bpl;
248141cc406Sopenharmony_ci
249141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, reader->pixel_buffer, &size));
250141cc406Sopenharmony_ci  unpack_8_mono (reader->pixel_buffer,
251141cc406Sopenharmony_ci		 DELAY_BUFFER_WRITE_PTR (&reader->g_delay),
252141cc406Sopenharmony_ci		 reader->pixels_per_line);
253141cc406Sopenharmony_ci
254141cc406Sopenharmony_ci  buffer = DELAY_BUFFER_READ_PTR (&reader->g_delay);
255141cc406Sopenharmony_ci
256141cc406Sopenharmony_ci  for (i = reader->params.double_column; i < reader->pixels_per_line; i += 2)
257141cc406Sopenharmony_ci    buffer[i] = DELAY_BUFFER_WRITE_PTR (&reader->g_delay)[i];
258141cc406Sopenharmony_ci
259141cc406Sopenharmony_ci  buffer_pointers_return[0] = buffer;
260141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
261141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
262141cc406Sopenharmony_ci}
263141cc406Sopenharmony_ci
264141cc406Sopenharmony_cistatic SANE_Status
265141cc406Sopenharmony_ciline_read_gray_12 (GT68xx_Line_Reader * reader,
266141cc406Sopenharmony_ci		   unsigned int **buffer_pointers_return)
267141cc406Sopenharmony_ci{
268141cc406Sopenharmony_ci  SANE_Status status;
269141cc406Sopenharmony_ci  size_t size;
270141cc406Sopenharmony_ci  unsigned int *buffer;
271141cc406Sopenharmony_ci
272141cc406Sopenharmony_ci  size = reader->params.scan_bpl;
273141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, reader->pixel_buffer, &size));
274141cc406Sopenharmony_ci
275141cc406Sopenharmony_ci  buffer = DELAY_BUFFER_READ_PTR (&reader->g_delay);
276141cc406Sopenharmony_ci  buffer_pointers_return[0] = buffer;
277141cc406Sopenharmony_ci  unpack_12_le_mono (reader->pixel_buffer, buffer, reader->pixels_per_line);
278141cc406Sopenharmony_ci
279141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
280141cc406Sopenharmony_ci}
281141cc406Sopenharmony_ci
282141cc406Sopenharmony_cistatic SANE_Status
283141cc406Sopenharmony_ciline_read_gray_double_12 (GT68xx_Line_Reader * reader,
284141cc406Sopenharmony_ci			  unsigned int **buffer_pointers_return)
285141cc406Sopenharmony_ci{
286141cc406Sopenharmony_ci  SANE_Status status;
287141cc406Sopenharmony_ci  size_t size;
288141cc406Sopenharmony_ci  unsigned int *buffer;
289141cc406Sopenharmony_ci  int i;
290141cc406Sopenharmony_ci
291141cc406Sopenharmony_ci  size = reader->params.scan_bpl;
292141cc406Sopenharmony_ci
293141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, reader->pixel_buffer, &size));
294141cc406Sopenharmony_ci  unpack_12_le_mono (reader->pixel_buffer,
295141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->g_delay),
296141cc406Sopenharmony_ci		     reader->pixels_per_line);
297141cc406Sopenharmony_ci
298141cc406Sopenharmony_ci  buffer = DELAY_BUFFER_READ_PTR (&reader->g_delay);
299141cc406Sopenharmony_ci
300141cc406Sopenharmony_ci  for (i = reader->params.double_column; i < reader->pixels_per_line; i += 2)
301141cc406Sopenharmony_ci    buffer[i] = DELAY_BUFFER_WRITE_PTR (&reader->g_delay)[i];
302141cc406Sopenharmony_ci
303141cc406Sopenharmony_ci  buffer_pointers_return[0] = buffer;
304141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
305141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
306141cc406Sopenharmony_ci}
307141cc406Sopenharmony_ci
308141cc406Sopenharmony_cistatic SANE_Status
309141cc406Sopenharmony_ciline_read_gray_16 (GT68xx_Line_Reader * reader,
310141cc406Sopenharmony_ci		   unsigned int **buffer_pointers_return)
311141cc406Sopenharmony_ci{
312141cc406Sopenharmony_ci  SANE_Status status;
313141cc406Sopenharmony_ci  size_t size;
314141cc406Sopenharmony_ci  unsigned int *buffer;
315141cc406Sopenharmony_ci
316141cc406Sopenharmony_ci  size = reader->params.scan_bpl;
317141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, reader->pixel_buffer, &size));
318141cc406Sopenharmony_ci
319141cc406Sopenharmony_ci  buffer = DELAY_BUFFER_READ_PTR (&reader->g_delay);
320141cc406Sopenharmony_ci  buffer_pointers_return[0] = buffer;
321141cc406Sopenharmony_ci  unpack_16_le_mono (reader->pixel_buffer, buffer, reader->pixels_per_line);
322141cc406Sopenharmony_ci
323141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
324141cc406Sopenharmony_ci}
325141cc406Sopenharmony_ci
326141cc406Sopenharmony_cistatic SANE_Status
327141cc406Sopenharmony_ciline_read_gray_double_16 (GT68xx_Line_Reader * reader,
328141cc406Sopenharmony_ci			  unsigned int **buffer_pointers_return)
329141cc406Sopenharmony_ci{
330141cc406Sopenharmony_ci  SANE_Status status;
331141cc406Sopenharmony_ci  size_t size;
332141cc406Sopenharmony_ci  unsigned int *buffer;
333141cc406Sopenharmony_ci  int i;
334141cc406Sopenharmony_ci
335141cc406Sopenharmony_ci  size = reader->params.scan_bpl;
336141cc406Sopenharmony_ci
337141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, reader->pixel_buffer, &size));
338141cc406Sopenharmony_ci  unpack_16_le_mono (reader->pixel_buffer,
339141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->g_delay),
340141cc406Sopenharmony_ci		     reader->pixels_per_line);
341141cc406Sopenharmony_ci
342141cc406Sopenharmony_ci  buffer = DELAY_BUFFER_READ_PTR (&reader->g_delay);
343141cc406Sopenharmony_ci
344141cc406Sopenharmony_ci  for (i = reader->params.double_column; i < reader->pixels_per_line; i += 2)
345141cc406Sopenharmony_ci    buffer[i] = DELAY_BUFFER_WRITE_PTR (&reader->g_delay)[i];
346141cc406Sopenharmony_ci
347141cc406Sopenharmony_ci  buffer_pointers_return[0] = buffer;
348141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
349141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
350141cc406Sopenharmony_ci
351141cc406Sopenharmony_ci}
352141cc406Sopenharmony_ci
353141cc406Sopenharmony_cistatic SANE_Status
354141cc406Sopenharmony_ciline_read_rgb_8_line_mode (GT68xx_Line_Reader * reader,
355141cc406Sopenharmony_ci			   unsigned int **buffer_pointers_return)
356141cc406Sopenharmony_ci{
357141cc406Sopenharmony_ci  SANE_Status status;
358141cc406Sopenharmony_ci  size_t size;
359141cc406Sopenharmony_ci  SANE_Int pixels_per_line;
360141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
361141cc406Sopenharmony_ci
362141cc406Sopenharmony_ci  size = reader->params.scan_bpl * 3;
363141cc406Sopenharmony_ci
364141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
365141cc406Sopenharmony_ci
366141cc406Sopenharmony_ci  pixels_per_line = reader->pixels_per_line;
367141cc406Sopenharmony_ci  unpack_8_mono (pixel_buffer,
368141cc406Sopenharmony_ci		 DELAY_BUFFER_WRITE_PTR (&reader->r_delay), pixels_per_line);
369141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
370141cc406Sopenharmony_ci  unpack_8_mono (pixel_buffer,
371141cc406Sopenharmony_ci		 DELAY_BUFFER_WRITE_PTR (&reader->g_delay), pixels_per_line);
372141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
373141cc406Sopenharmony_ci  unpack_8_mono (pixel_buffer,
374141cc406Sopenharmony_ci		 DELAY_BUFFER_WRITE_PTR (&reader->b_delay), pixels_per_line);
375141cc406Sopenharmony_ci
376141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
377141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
378141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
379141cc406Sopenharmony_ci
380141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
381141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
382141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
383141cc406Sopenharmony_ci
384141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
385141cc406Sopenharmony_ci}
386141cc406Sopenharmony_ci
387141cc406Sopenharmony_cistatic SANE_Status
388141cc406Sopenharmony_ciline_read_rgb_double_8_line_mode (GT68xx_Line_Reader * reader,
389141cc406Sopenharmony_ci				  unsigned int **buffer_pointers_return)
390141cc406Sopenharmony_ci{
391141cc406Sopenharmony_ci  SANE_Status status;
392141cc406Sopenharmony_ci  size_t size;
393141cc406Sopenharmony_ci  SANE_Int pixels_per_line;
394141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
395141cc406Sopenharmony_ci  int i;
396141cc406Sopenharmony_ci
397141cc406Sopenharmony_ci  size = reader->params.scan_bpl * 3;
398141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
399141cc406Sopenharmony_ci
400141cc406Sopenharmony_ci  pixels_per_line = reader->pixels_per_line;
401141cc406Sopenharmony_ci  unpack_8_mono (pixel_buffer,
402141cc406Sopenharmony_ci		 DELAY_BUFFER_WRITE_PTR (&reader->r_delay), pixels_per_line);
403141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
404141cc406Sopenharmony_ci  unpack_8_mono (pixel_buffer,
405141cc406Sopenharmony_ci		 DELAY_BUFFER_WRITE_PTR (&reader->g_delay), pixels_per_line);
406141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
407141cc406Sopenharmony_ci  unpack_8_mono (pixel_buffer,
408141cc406Sopenharmony_ci		 DELAY_BUFFER_WRITE_PTR (&reader->b_delay), pixels_per_line);
409141cc406Sopenharmony_ci
410141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
411141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
412141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
413141cc406Sopenharmony_ci
414141cc406Sopenharmony_ci  for (i = reader->params.double_column; i < reader->pixels_per_line; i += 2)
415141cc406Sopenharmony_ci    {
416141cc406Sopenharmony_ci      DELAY_BUFFER_READ_PTR (&reader->r_delay)[i] =
417141cc406Sopenharmony_ci	DELAY_BUFFER_SELECT_PTR (&reader->r_delay,
418141cc406Sopenharmony_ci				 reader->params.ld_shift_double)[i];
419141cc406Sopenharmony_ci      DELAY_BUFFER_READ_PTR (&reader->g_delay)[i] =
420141cc406Sopenharmony_ci	DELAY_BUFFER_SELECT_PTR (&reader->g_delay,
421141cc406Sopenharmony_ci				 reader->params.ld_shift_double)[i];
422141cc406Sopenharmony_ci      DELAY_BUFFER_READ_PTR (&reader->b_delay)[i] =
423141cc406Sopenharmony_ci	DELAY_BUFFER_SELECT_PTR (&reader->b_delay,
424141cc406Sopenharmony_ci				 reader->params.ld_shift_double)[i];
425141cc406Sopenharmony_ci    }
426141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
427141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
428141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
429141cc406Sopenharmony_ci
430141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
431141cc406Sopenharmony_ci}
432141cc406Sopenharmony_ci
433141cc406Sopenharmony_cistatic SANE_Status
434141cc406Sopenharmony_ciline_read_bgr_8_line_mode (GT68xx_Line_Reader * reader,
435141cc406Sopenharmony_ci			   unsigned int **buffer_pointers_return)
436141cc406Sopenharmony_ci{
437141cc406Sopenharmony_ci  SANE_Status status;
438141cc406Sopenharmony_ci  size_t size;
439141cc406Sopenharmony_ci  SANE_Int pixels_per_line;
440141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
441141cc406Sopenharmony_ci
442141cc406Sopenharmony_ci  size = reader->params.scan_bpl * 3;
443141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
444141cc406Sopenharmony_ci
445141cc406Sopenharmony_ci  pixels_per_line = reader->pixels_per_line;
446141cc406Sopenharmony_ci  unpack_8_mono (pixel_buffer,
447141cc406Sopenharmony_ci		 DELAY_BUFFER_WRITE_PTR (&reader->b_delay), pixels_per_line);
448141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
449141cc406Sopenharmony_ci  unpack_8_mono (pixel_buffer,
450141cc406Sopenharmony_ci		 DELAY_BUFFER_WRITE_PTR (&reader->g_delay), pixels_per_line);
451141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
452141cc406Sopenharmony_ci  unpack_8_mono (pixel_buffer,
453141cc406Sopenharmony_ci		 DELAY_BUFFER_WRITE_PTR (&reader->r_delay), pixels_per_line);
454141cc406Sopenharmony_ci
455141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
456141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
457141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
458141cc406Sopenharmony_ci
459141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
460141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
461141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
462141cc406Sopenharmony_ci
463141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
464141cc406Sopenharmony_ci}
465141cc406Sopenharmony_ci
466141cc406Sopenharmony_cistatic SANE_Status
467141cc406Sopenharmony_ciline_read_rgb_12_line_mode (GT68xx_Line_Reader * reader,
468141cc406Sopenharmony_ci			    unsigned int **buffer_pointers_return)
469141cc406Sopenharmony_ci{
470141cc406Sopenharmony_ci  SANE_Status status;
471141cc406Sopenharmony_ci  size_t size;
472141cc406Sopenharmony_ci  SANE_Int pixels_per_line;
473141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
474141cc406Sopenharmony_ci
475141cc406Sopenharmony_ci  size = reader->params.scan_bpl * 3;
476141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
477141cc406Sopenharmony_ci
478141cc406Sopenharmony_ci  pixels_per_line = reader->pixels_per_line;
479141cc406Sopenharmony_ci  unpack_12_le_mono (pixel_buffer,
480141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->r_delay),
481141cc406Sopenharmony_ci		     pixels_per_line);
482141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
483141cc406Sopenharmony_ci  unpack_12_le_mono (pixel_buffer,
484141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->g_delay),
485141cc406Sopenharmony_ci		     pixels_per_line);
486141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
487141cc406Sopenharmony_ci  unpack_12_le_mono (pixel_buffer,
488141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->b_delay),
489141cc406Sopenharmony_ci		     pixels_per_line);
490141cc406Sopenharmony_ci
491141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
492141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
493141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
494141cc406Sopenharmony_ci
495141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
496141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
497141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
498141cc406Sopenharmony_ci
499141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
500141cc406Sopenharmony_ci}
501141cc406Sopenharmony_ci
502141cc406Sopenharmony_cistatic SANE_Status
503141cc406Sopenharmony_ciline_read_rgb_double_12_line_mode (GT68xx_Line_Reader * reader,
504141cc406Sopenharmony_ci				   unsigned int **buffer_pointers_return)
505141cc406Sopenharmony_ci{
506141cc406Sopenharmony_ci  SANE_Status status;
507141cc406Sopenharmony_ci  size_t size;
508141cc406Sopenharmony_ci  SANE_Int pixels_per_line;
509141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
510141cc406Sopenharmony_ci  int i;
511141cc406Sopenharmony_ci
512141cc406Sopenharmony_ci  size = reader->params.scan_bpl * 3;
513141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
514141cc406Sopenharmony_ci
515141cc406Sopenharmony_ci  pixels_per_line = reader->pixels_per_line;
516141cc406Sopenharmony_ci  unpack_12_le_mono (pixel_buffer,
517141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->r_delay),
518141cc406Sopenharmony_ci		     pixels_per_line);
519141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
520141cc406Sopenharmony_ci  unpack_12_le_mono (pixel_buffer,
521141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->g_delay),
522141cc406Sopenharmony_ci		     pixels_per_line);
523141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
524141cc406Sopenharmony_ci  unpack_12_le_mono (pixel_buffer,
525141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->b_delay),
526141cc406Sopenharmony_ci		     pixels_per_line);
527141cc406Sopenharmony_ci
528141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
529141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
530141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
531141cc406Sopenharmony_ci
532141cc406Sopenharmony_ci  for (i = reader->params.double_column; i < reader->pixels_per_line; i += 2)
533141cc406Sopenharmony_ci    {
534141cc406Sopenharmony_ci      DELAY_BUFFER_READ_PTR (&reader->r_delay)[i] =
535141cc406Sopenharmony_ci	DELAY_BUFFER_SELECT_PTR (&reader->r_delay,
536141cc406Sopenharmony_ci				 reader->params.ld_shift_double)[i];
537141cc406Sopenharmony_ci      DELAY_BUFFER_READ_PTR (&reader->g_delay)[i] =
538141cc406Sopenharmony_ci	DELAY_BUFFER_SELECT_PTR (&reader->g_delay,
539141cc406Sopenharmony_ci				 reader->params.ld_shift_double)[i];
540141cc406Sopenharmony_ci      DELAY_BUFFER_READ_PTR (&reader->b_delay)[i] =
541141cc406Sopenharmony_ci	DELAY_BUFFER_SELECT_PTR (&reader->b_delay,
542141cc406Sopenharmony_ci				 reader->params.ld_shift_double)[i];
543141cc406Sopenharmony_ci    }
544141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
545141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
546141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
547141cc406Sopenharmony_ci
548141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
549141cc406Sopenharmony_ci}
550141cc406Sopenharmony_ci
551141cc406Sopenharmony_cistatic SANE_Status
552141cc406Sopenharmony_ciline_read_rgb_16_line_mode (GT68xx_Line_Reader * reader,
553141cc406Sopenharmony_ci			    unsigned int **buffer_pointers_return)
554141cc406Sopenharmony_ci{
555141cc406Sopenharmony_ci  SANE_Status status;
556141cc406Sopenharmony_ci  size_t size;
557141cc406Sopenharmony_ci  SANE_Int pixels_per_line;
558141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
559141cc406Sopenharmony_ci
560141cc406Sopenharmony_ci  size = reader->params.scan_bpl * 3;
561141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
562141cc406Sopenharmony_ci
563141cc406Sopenharmony_ci  pixels_per_line = reader->pixels_per_line;
564141cc406Sopenharmony_ci  unpack_16_le_mono (pixel_buffer,
565141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->r_delay),
566141cc406Sopenharmony_ci		     pixels_per_line);
567141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
568141cc406Sopenharmony_ci  unpack_16_le_mono (pixel_buffer,
569141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->g_delay),
570141cc406Sopenharmony_ci		     pixels_per_line);
571141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
572141cc406Sopenharmony_ci  unpack_16_le_mono (pixel_buffer,
573141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->b_delay),
574141cc406Sopenharmony_ci		     pixels_per_line);
575141cc406Sopenharmony_ci
576141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
577141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
578141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
579141cc406Sopenharmony_ci
580141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
581141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
582141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
583141cc406Sopenharmony_ci
584141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
585141cc406Sopenharmony_ci}
586141cc406Sopenharmony_ci
587141cc406Sopenharmony_cistatic SANE_Status
588141cc406Sopenharmony_ciline_read_rgb_double_16_line_mode (GT68xx_Line_Reader * reader,
589141cc406Sopenharmony_ci				   unsigned int **buffer_pointers_return)
590141cc406Sopenharmony_ci{
591141cc406Sopenharmony_ci  SANE_Status status;
592141cc406Sopenharmony_ci  size_t size;
593141cc406Sopenharmony_ci  SANE_Int pixels_per_line;
594141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
595141cc406Sopenharmony_ci  int i;
596141cc406Sopenharmony_ci
597141cc406Sopenharmony_ci  size = reader->params.scan_bpl * 3;
598141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
599141cc406Sopenharmony_ci
600141cc406Sopenharmony_ci  pixels_per_line = reader->pixels_per_line;
601141cc406Sopenharmony_ci  unpack_16_le_mono (pixel_buffer,
602141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->r_delay),
603141cc406Sopenharmony_ci		     pixels_per_line);
604141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
605141cc406Sopenharmony_ci  unpack_16_le_mono (pixel_buffer,
606141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->g_delay),
607141cc406Sopenharmony_ci		     pixels_per_line);
608141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
609141cc406Sopenharmony_ci  unpack_16_le_mono (pixel_buffer,
610141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->b_delay),
611141cc406Sopenharmony_ci		     pixels_per_line);
612141cc406Sopenharmony_ci
613141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
614141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
615141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
616141cc406Sopenharmony_ci
617141cc406Sopenharmony_ci  for (i = reader->params.double_column; i < reader->pixels_per_line; i += 2)
618141cc406Sopenharmony_ci    {
619141cc406Sopenharmony_ci      DELAY_BUFFER_READ_PTR (&reader->r_delay)[i] =
620141cc406Sopenharmony_ci	DELAY_BUFFER_SELECT_PTR (&reader->r_delay,
621141cc406Sopenharmony_ci				 reader->params.ld_shift_double)[i];
622141cc406Sopenharmony_ci      DELAY_BUFFER_READ_PTR (&reader->g_delay)[i] =
623141cc406Sopenharmony_ci	DELAY_BUFFER_SELECT_PTR (&reader->g_delay,
624141cc406Sopenharmony_ci				 reader->params.ld_shift_double)[i];
625141cc406Sopenharmony_ci      DELAY_BUFFER_READ_PTR (&reader->b_delay)[i] =
626141cc406Sopenharmony_ci	DELAY_BUFFER_SELECT_PTR (&reader->b_delay,
627141cc406Sopenharmony_ci				 reader->params.ld_shift_double)[i];
628141cc406Sopenharmony_ci    }
629141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
630141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
631141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
632141cc406Sopenharmony_ci
633141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
634141cc406Sopenharmony_ci}
635141cc406Sopenharmony_ci
636141cc406Sopenharmony_cistatic SANE_Status
637141cc406Sopenharmony_ciline_read_bgr_12_line_mode (GT68xx_Line_Reader * reader,
638141cc406Sopenharmony_ci			    unsigned int **buffer_pointers_return)
639141cc406Sopenharmony_ci{
640141cc406Sopenharmony_ci  SANE_Status status;
641141cc406Sopenharmony_ci  size_t size;
642141cc406Sopenharmony_ci  SANE_Int pixels_per_line;
643141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
644141cc406Sopenharmony_ci
645141cc406Sopenharmony_ci  size = reader->params.scan_bpl * 3;
646141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
647141cc406Sopenharmony_ci
648141cc406Sopenharmony_ci  pixels_per_line = reader->pixels_per_line;
649141cc406Sopenharmony_ci  unpack_12_le_mono (pixel_buffer,
650141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->b_delay),
651141cc406Sopenharmony_ci		     pixels_per_line);
652141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
653141cc406Sopenharmony_ci  unpack_12_le_mono (pixel_buffer,
654141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->g_delay),
655141cc406Sopenharmony_ci		     pixels_per_line);
656141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
657141cc406Sopenharmony_ci  unpack_12_le_mono (pixel_buffer,
658141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->r_delay),
659141cc406Sopenharmony_ci		     pixels_per_line);
660141cc406Sopenharmony_ci
661141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
662141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
663141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
664141cc406Sopenharmony_ci
665141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
666141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
667141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
668141cc406Sopenharmony_ci
669141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
670141cc406Sopenharmony_ci}
671141cc406Sopenharmony_ci
672141cc406Sopenharmony_cistatic SANE_Status
673141cc406Sopenharmony_ciline_read_bgr_16_line_mode (GT68xx_Line_Reader * reader,
674141cc406Sopenharmony_ci			    unsigned int **buffer_pointers_return)
675141cc406Sopenharmony_ci{
676141cc406Sopenharmony_ci  SANE_Status status;
677141cc406Sopenharmony_ci  size_t size;
678141cc406Sopenharmony_ci  SANE_Int pixels_per_line;
679141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
680141cc406Sopenharmony_ci
681141cc406Sopenharmony_ci  size = reader->params.scan_bpl * 3;
682141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
683141cc406Sopenharmony_ci
684141cc406Sopenharmony_ci  pixels_per_line = reader->pixels_per_line;
685141cc406Sopenharmony_ci  unpack_16_le_mono (pixel_buffer,
686141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->b_delay),
687141cc406Sopenharmony_ci		     pixels_per_line);
688141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
689141cc406Sopenharmony_ci  unpack_16_le_mono (pixel_buffer,
690141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->g_delay),
691141cc406Sopenharmony_ci		     pixels_per_line);
692141cc406Sopenharmony_ci  pixel_buffer += reader->params.scan_bpl;
693141cc406Sopenharmony_ci  unpack_16_le_mono (pixel_buffer,
694141cc406Sopenharmony_ci		     DELAY_BUFFER_WRITE_PTR (&reader->r_delay),
695141cc406Sopenharmony_ci		     pixels_per_line);
696141cc406Sopenharmony_ci
697141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
698141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
699141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
700141cc406Sopenharmony_ci
701141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
702141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
703141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
704141cc406Sopenharmony_ci
705141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
706141cc406Sopenharmony_ci}
707141cc406Sopenharmony_ci
708141cc406Sopenharmony_cistatic SANE_Status
709141cc406Sopenharmony_ciline_read_rgb_8_pixel_mode (GT68xx_Line_Reader * reader,
710141cc406Sopenharmony_ci			    unsigned int **buffer_pointers_return)
711141cc406Sopenharmony_ci{
712141cc406Sopenharmony_ci  SANE_Status status;
713141cc406Sopenharmony_ci  size_t size;
714141cc406Sopenharmony_ci  SANE_Int pixels_per_line;
715141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
716141cc406Sopenharmony_ci
717141cc406Sopenharmony_ci  size = reader->params.scan_bpl;
718141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
719141cc406Sopenharmony_ci
720141cc406Sopenharmony_ci  pixels_per_line = reader->pixels_per_line;
721141cc406Sopenharmony_ci  unpack_8_rgb (pixel_buffer,
722141cc406Sopenharmony_ci		DELAY_BUFFER_WRITE_PTR (&reader->r_delay), pixels_per_line);
723141cc406Sopenharmony_ci  ++pixel_buffer;
724141cc406Sopenharmony_ci  unpack_8_rgb (pixel_buffer,
725141cc406Sopenharmony_ci		DELAY_BUFFER_WRITE_PTR (&reader->g_delay), pixels_per_line);
726141cc406Sopenharmony_ci  ++pixel_buffer;
727141cc406Sopenharmony_ci  unpack_8_rgb (pixel_buffer,
728141cc406Sopenharmony_ci		DELAY_BUFFER_WRITE_PTR (&reader->b_delay), pixels_per_line);
729141cc406Sopenharmony_ci
730141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
731141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
732141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
733141cc406Sopenharmony_ci
734141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
735141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
736141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
737141cc406Sopenharmony_ci
738141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
739141cc406Sopenharmony_ci}
740141cc406Sopenharmony_ci
741141cc406Sopenharmony_ci
742141cc406Sopenharmony_cistatic SANE_Status
743141cc406Sopenharmony_ciline_read_rgb_12_pixel_mode (GT68xx_Line_Reader * reader,
744141cc406Sopenharmony_ci			     unsigned int **buffer_pointers_return)
745141cc406Sopenharmony_ci{
746141cc406Sopenharmony_ci  SANE_Status status;
747141cc406Sopenharmony_ci  size_t size;
748141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
749141cc406Sopenharmony_ci
750141cc406Sopenharmony_ci  size = reader->params.scan_bpl;
751141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
752141cc406Sopenharmony_ci
753141cc406Sopenharmony_ci  unpack_12_le_rgb (pixel_buffer,
754141cc406Sopenharmony_ci		    DELAY_BUFFER_WRITE_PTR (&reader->r_delay),
755141cc406Sopenharmony_ci		    DELAY_BUFFER_WRITE_PTR (&reader->g_delay),
756141cc406Sopenharmony_ci		    DELAY_BUFFER_WRITE_PTR (&reader->b_delay),
757141cc406Sopenharmony_ci		    reader->pixels_per_line);
758141cc406Sopenharmony_ci
759141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
760141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
761141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
762141cc406Sopenharmony_ci
763141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
764141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
765141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
766141cc406Sopenharmony_ci
767141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
768141cc406Sopenharmony_ci}
769141cc406Sopenharmony_ci
770141cc406Sopenharmony_cistatic SANE_Status
771141cc406Sopenharmony_ciline_read_rgb_16_pixel_mode (GT68xx_Line_Reader * reader,
772141cc406Sopenharmony_ci			     unsigned int **buffer_pointers_return)
773141cc406Sopenharmony_ci{
774141cc406Sopenharmony_ci  SANE_Status status;
775141cc406Sopenharmony_ci  size_t size;
776141cc406Sopenharmony_ci  SANE_Int pixels_per_line;
777141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
778141cc406Sopenharmony_ci
779141cc406Sopenharmony_ci  size = reader->params.scan_bpl;
780141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
781141cc406Sopenharmony_ci
782141cc406Sopenharmony_ci  pixels_per_line = reader->pixels_per_line;
783141cc406Sopenharmony_ci  unpack_16_le_rgb (pixel_buffer,
784141cc406Sopenharmony_ci		    DELAY_BUFFER_WRITE_PTR (&reader->r_delay),
785141cc406Sopenharmony_ci		    pixels_per_line);
786141cc406Sopenharmony_ci  pixel_buffer += 2;
787141cc406Sopenharmony_ci  unpack_16_le_rgb (pixel_buffer,
788141cc406Sopenharmony_ci		    DELAY_BUFFER_WRITE_PTR (&reader->g_delay),
789141cc406Sopenharmony_ci		    pixels_per_line);
790141cc406Sopenharmony_ci  pixel_buffer += 2;
791141cc406Sopenharmony_ci  unpack_16_le_rgb (pixel_buffer,
792141cc406Sopenharmony_ci		    DELAY_BUFFER_WRITE_PTR (&reader->b_delay),
793141cc406Sopenharmony_ci		    pixels_per_line);
794141cc406Sopenharmony_ci
795141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
796141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
797141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
798141cc406Sopenharmony_ci
799141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
800141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
801141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
802141cc406Sopenharmony_ci
803141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
804141cc406Sopenharmony_ci}
805141cc406Sopenharmony_ci
806141cc406Sopenharmony_cistatic SANE_Status
807141cc406Sopenharmony_ciline_read_bgr_8_pixel_mode (GT68xx_Line_Reader * reader,
808141cc406Sopenharmony_ci			    unsigned int **buffer_pointers_return)
809141cc406Sopenharmony_ci{
810141cc406Sopenharmony_ci  SANE_Status status;
811141cc406Sopenharmony_ci  size_t size;
812141cc406Sopenharmony_ci  SANE_Int pixels_per_line;
813141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
814141cc406Sopenharmony_ci
815141cc406Sopenharmony_ci  size = reader->params.scan_bpl;
816141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
817141cc406Sopenharmony_ci
818141cc406Sopenharmony_ci  pixels_per_line = reader->pixels_per_line;
819141cc406Sopenharmony_ci  unpack_8_rgb (pixel_buffer,
820141cc406Sopenharmony_ci		DELAY_BUFFER_WRITE_PTR (&reader->b_delay), pixels_per_line);
821141cc406Sopenharmony_ci  ++pixel_buffer;
822141cc406Sopenharmony_ci  unpack_8_rgb (pixel_buffer,
823141cc406Sopenharmony_ci		DELAY_BUFFER_WRITE_PTR (&reader->g_delay), pixels_per_line);
824141cc406Sopenharmony_ci  ++pixel_buffer;
825141cc406Sopenharmony_ci  unpack_8_rgb (pixel_buffer,
826141cc406Sopenharmony_ci		DELAY_BUFFER_WRITE_PTR (&reader->r_delay), pixels_per_line);
827141cc406Sopenharmony_ci
828141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
829141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
830141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
831141cc406Sopenharmony_ci
832141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
833141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
834141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
835141cc406Sopenharmony_ci
836141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
837141cc406Sopenharmony_ci}
838141cc406Sopenharmony_ci
839141cc406Sopenharmony_ci
840141cc406Sopenharmony_cistatic SANE_Status
841141cc406Sopenharmony_ciline_read_bgr_12_pixel_mode (GT68xx_Line_Reader * reader,
842141cc406Sopenharmony_ci			     unsigned int **buffer_pointers_return)
843141cc406Sopenharmony_ci{
844141cc406Sopenharmony_ci  SANE_Status status;
845141cc406Sopenharmony_ci  size_t size;
846141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
847141cc406Sopenharmony_ci
848141cc406Sopenharmony_ci  size = reader->params.scan_bpl;
849141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
850141cc406Sopenharmony_ci
851141cc406Sopenharmony_ci  unpack_12_le_rgb (pixel_buffer,
852141cc406Sopenharmony_ci		    DELAY_BUFFER_WRITE_PTR (&reader->b_delay),
853141cc406Sopenharmony_ci		    DELAY_BUFFER_WRITE_PTR (&reader->g_delay),
854141cc406Sopenharmony_ci		    DELAY_BUFFER_WRITE_PTR (&reader->r_delay),
855141cc406Sopenharmony_ci		    reader->pixels_per_line);
856141cc406Sopenharmony_ci
857141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
858141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
859141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
860141cc406Sopenharmony_ci
861141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
862141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
863141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
864141cc406Sopenharmony_ci
865141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
866141cc406Sopenharmony_ci}
867141cc406Sopenharmony_ci
868141cc406Sopenharmony_cistatic SANE_Status
869141cc406Sopenharmony_ciline_read_bgr_16_pixel_mode (GT68xx_Line_Reader * reader,
870141cc406Sopenharmony_ci			     unsigned int **buffer_pointers_return)
871141cc406Sopenharmony_ci{
872141cc406Sopenharmony_ci  SANE_Status status;
873141cc406Sopenharmony_ci  size_t size;
874141cc406Sopenharmony_ci  SANE_Int pixels_per_line;
875141cc406Sopenharmony_ci  SANE_Byte *pixel_buffer = reader->pixel_buffer;
876141cc406Sopenharmony_ci
877141cc406Sopenharmony_ci  size = reader->params.scan_bpl;
878141cc406Sopenharmony_ci  RIE (gt68xx_device_read (reader->dev, pixel_buffer, &size));
879141cc406Sopenharmony_ci
880141cc406Sopenharmony_ci  pixels_per_line = reader->pixels_per_line;
881141cc406Sopenharmony_ci  unpack_16_le_rgb (pixel_buffer,
882141cc406Sopenharmony_ci		    DELAY_BUFFER_WRITE_PTR (&reader->b_delay),
883141cc406Sopenharmony_ci		    pixels_per_line);
884141cc406Sopenharmony_ci  pixel_buffer += 2;
885141cc406Sopenharmony_ci  unpack_16_le_rgb (pixel_buffer,
886141cc406Sopenharmony_ci		    DELAY_BUFFER_WRITE_PTR (&reader->g_delay),
887141cc406Sopenharmony_ci		    pixels_per_line);
888141cc406Sopenharmony_ci  pixel_buffer += 2;
889141cc406Sopenharmony_ci  unpack_16_le_rgb (pixel_buffer,
890141cc406Sopenharmony_ci		    DELAY_BUFFER_WRITE_PTR (&reader->r_delay),
891141cc406Sopenharmony_ci		    pixels_per_line);
892141cc406Sopenharmony_ci
893141cc406Sopenharmony_ci  buffer_pointers_return[0] = DELAY_BUFFER_READ_PTR (&reader->r_delay);
894141cc406Sopenharmony_ci  buffer_pointers_return[1] = DELAY_BUFFER_READ_PTR (&reader->g_delay);
895141cc406Sopenharmony_ci  buffer_pointers_return[2] = DELAY_BUFFER_READ_PTR (&reader->b_delay);
896141cc406Sopenharmony_ci
897141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->r_delay);
898141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->g_delay);
899141cc406Sopenharmony_ci  DELAY_BUFFER_STEP (&reader->b_delay);
900141cc406Sopenharmony_ci
901141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
902141cc406Sopenharmony_ci}
903141cc406Sopenharmony_ci
904141cc406Sopenharmony_cistatic SANE_Status
905141cc406Sopenharmony_cigt68xx_line_reader_init_delays (GT68xx_Line_Reader * reader)
906141cc406Sopenharmony_ci{
907141cc406Sopenharmony_ci  SANE_Status status;
908141cc406Sopenharmony_ci
909141cc406Sopenharmony_ci  if (reader->params.color)
910141cc406Sopenharmony_ci    {
911141cc406Sopenharmony_ci      status = gt68xx_delay_buffer_init (&reader->r_delay,
912141cc406Sopenharmony_ci					 reader->params.scan_xs,
913141cc406Sopenharmony_ci					 reader->params.ld_shift_r +
914141cc406Sopenharmony_ci					 reader->params.ld_shift_double);
915141cc406Sopenharmony_ci      if (status != SANE_STATUS_GOOD)
916141cc406Sopenharmony_ci	return status;
917141cc406Sopenharmony_ci
918141cc406Sopenharmony_ci      status = gt68xx_delay_buffer_init (&reader->g_delay,
919141cc406Sopenharmony_ci					 reader->params.scan_xs,
920141cc406Sopenharmony_ci					 reader->params.ld_shift_g +
921141cc406Sopenharmony_ci					 reader->params.ld_shift_double);
922141cc406Sopenharmony_ci      if (status != SANE_STATUS_GOOD)
923141cc406Sopenharmony_ci	{
924141cc406Sopenharmony_ci	  gt68xx_delay_buffer_done (&reader->r_delay);
925141cc406Sopenharmony_ci	  return status;
926141cc406Sopenharmony_ci	}
927141cc406Sopenharmony_ci
928141cc406Sopenharmony_ci      status = gt68xx_delay_buffer_init (&reader->b_delay,
929141cc406Sopenharmony_ci					 reader->params.scan_xs,
930141cc406Sopenharmony_ci					 reader->params.ld_shift_b +
931141cc406Sopenharmony_ci					 reader->params.ld_shift_double);
932141cc406Sopenharmony_ci      if (status != SANE_STATUS_GOOD)
933141cc406Sopenharmony_ci	{
934141cc406Sopenharmony_ci	  gt68xx_delay_buffer_done (&reader->g_delay);
935141cc406Sopenharmony_ci	  gt68xx_delay_buffer_done (&reader->r_delay);
936141cc406Sopenharmony_ci	  return status;
937141cc406Sopenharmony_ci	}
938141cc406Sopenharmony_ci    }
939141cc406Sopenharmony_ci  else
940141cc406Sopenharmony_ci    {
941141cc406Sopenharmony_ci      status = gt68xx_delay_buffer_init (&reader->g_delay,
942141cc406Sopenharmony_ci					 reader->params.scan_xs,
943141cc406Sopenharmony_ci					 reader->params.ld_shift_double);
944141cc406Sopenharmony_ci      if (status != SANE_STATUS_GOOD)
945141cc406Sopenharmony_ci	return status;
946141cc406Sopenharmony_ci    }
947141cc406Sopenharmony_ci  reader->delays_initialized = SANE_TRUE;
948141cc406Sopenharmony_ci
949141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
950141cc406Sopenharmony_ci}
951141cc406Sopenharmony_ci
952141cc406Sopenharmony_cistatic void
953141cc406Sopenharmony_cigt68xx_line_reader_free_delays (GT68xx_Line_Reader * reader)
954141cc406Sopenharmony_ci{
955141cc406Sopenharmony_ci  if (reader->delays_initialized)
956141cc406Sopenharmony_ci    {
957141cc406Sopenharmony_ci      if (reader->params.color)
958141cc406Sopenharmony_ci	{
959141cc406Sopenharmony_ci	  gt68xx_delay_buffer_done (&reader->b_delay);
960141cc406Sopenharmony_ci	  gt68xx_delay_buffer_done (&reader->g_delay);
961141cc406Sopenharmony_ci	  gt68xx_delay_buffer_done (&reader->r_delay);
962141cc406Sopenharmony_ci	}
963141cc406Sopenharmony_ci      else
964141cc406Sopenharmony_ci	{
965141cc406Sopenharmony_ci	  gt68xx_delay_buffer_done (&reader->g_delay);
966141cc406Sopenharmony_ci	}
967141cc406Sopenharmony_ci      reader->delays_initialized = SANE_FALSE;
968141cc406Sopenharmony_ci    }
969141cc406Sopenharmony_ci}
970141cc406Sopenharmony_ci
971141cc406Sopenharmony_ciSANE_Status
972141cc406Sopenharmony_cigt68xx_line_reader_new (GT68xx_Device * dev,
973141cc406Sopenharmony_ci			GT68xx_Scan_Parameters * params,
974141cc406Sopenharmony_ci			SANE_Bool final_scan,
975141cc406Sopenharmony_ci			GT68xx_Line_Reader ** reader_return)
976141cc406Sopenharmony_ci{
977141cc406Sopenharmony_ci  SANE_Status status;
978141cc406Sopenharmony_ci  GT68xx_Line_Reader *reader;
979141cc406Sopenharmony_ci  SANE_Int image_size;
980141cc406Sopenharmony_ci  SANE_Int scan_bpl_full;
981141cc406Sopenharmony_ci
982141cc406Sopenharmony_ci  DBG (6, "gt68xx_line_reader_new: enter\n");
983141cc406Sopenharmony_ci
984141cc406Sopenharmony_ci  *reader_return = NULL;
985141cc406Sopenharmony_ci
986141cc406Sopenharmony_ci  reader = (GT68xx_Line_Reader *) malloc (sizeof (GT68xx_Line_Reader));
987141cc406Sopenharmony_ci  if (!reader)
988141cc406Sopenharmony_ci    {
989141cc406Sopenharmony_ci      DBG (3, "gt68xx_line_reader_new: cannot allocate GT68xx_Line_Reader\n");
990141cc406Sopenharmony_ci      return SANE_STATUS_NO_MEM;
991141cc406Sopenharmony_ci    }
992141cc406Sopenharmony_ci  memset (reader, 0, sizeof (GT68xx_Line_Reader));
993141cc406Sopenharmony_ci
994141cc406Sopenharmony_ci  reader->dev = dev;
995141cc406Sopenharmony_ci  memcpy (&reader->params, params, sizeof (GT68xx_Scan_Parameters));
996141cc406Sopenharmony_ci  reader->pixel_buffer = 0;
997141cc406Sopenharmony_ci  reader->delays_initialized = SANE_FALSE;
998141cc406Sopenharmony_ci
999141cc406Sopenharmony_ci  reader->read = NULL;
1000141cc406Sopenharmony_ci
1001141cc406Sopenharmony_ci  status = gt68xx_line_reader_init_delays (reader);
1002141cc406Sopenharmony_ci  if (status != SANE_STATUS_GOOD)
1003141cc406Sopenharmony_ci    {
1004141cc406Sopenharmony_ci      DBG (3, "gt68xx_line_reader_new: cannot allocate line buffers: %s\n",
1005141cc406Sopenharmony_ci	   sane_strstatus (status));
1006141cc406Sopenharmony_ci      free (reader);
1007141cc406Sopenharmony_ci      reader = NULL;
1008141cc406Sopenharmony_ci      return status;
1009141cc406Sopenharmony_ci    }
1010141cc406Sopenharmony_ci
1011141cc406Sopenharmony_ci  reader->pixels_per_line = reader->params.pixel_xs;
1012141cc406Sopenharmony_ci
1013141cc406Sopenharmony_ci  if (!reader->params.color)
1014141cc406Sopenharmony_ci    {
1015141cc406Sopenharmony_ci      if (reader->params.depth == 8)
1016141cc406Sopenharmony_ci	{
1017141cc406Sopenharmony_ci	  if (reader->params.ld_shift_double > 0)
1018141cc406Sopenharmony_ci	    reader->read = line_read_gray_double_8;
1019141cc406Sopenharmony_ci	  else
1020141cc406Sopenharmony_ci	    reader->read = line_read_gray_8;
1021141cc406Sopenharmony_ci	}
1022141cc406Sopenharmony_ci      else if (reader->params.depth == 12)
1023141cc406Sopenharmony_ci	{
1024141cc406Sopenharmony_ci	  if (reader->params.ld_shift_double > 0)
1025141cc406Sopenharmony_ci	    reader->read = line_read_gray_double_12;
1026141cc406Sopenharmony_ci	  else
1027141cc406Sopenharmony_ci	    reader->read = line_read_gray_12;
1028141cc406Sopenharmony_ci	}
1029141cc406Sopenharmony_ci      else if (reader->params.depth == 16)
1030141cc406Sopenharmony_ci	{
1031141cc406Sopenharmony_ci	  if (reader->params.ld_shift_double > 0)
1032141cc406Sopenharmony_ci	    reader->read = line_read_gray_double_16;
1033141cc406Sopenharmony_ci	  else
1034141cc406Sopenharmony_ci	    reader->read = line_read_gray_16;
1035141cc406Sopenharmony_ci	}
1036141cc406Sopenharmony_ci    }
1037141cc406Sopenharmony_ci  else if (reader->params.line_mode)
1038141cc406Sopenharmony_ci    {
1039141cc406Sopenharmony_ci      if (reader->params.depth == 8)
1040141cc406Sopenharmony_ci	{
1041141cc406Sopenharmony_ci	  if (dev->model->line_mode_color_order == COLOR_ORDER_RGB)
1042141cc406Sopenharmony_ci	    {
1043141cc406Sopenharmony_ci	      if (reader->params.ld_shift_double > 0)
1044141cc406Sopenharmony_ci		reader->read = line_read_rgb_double_8_line_mode;
1045141cc406Sopenharmony_ci	      else
1046141cc406Sopenharmony_ci		reader->read = line_read_rgb_8_line_mode;
1047141cc406Sopenharmony_ci	    }
1048141cc406Sopenharmony_ci	  else if (dev->model->line_mode_color_order == COLOR_ORDER_BGR)
1049141cc406Sopenharmony_ci	    reader->read = line_read_bgr_8_line_mode;
1050141cc406Sopenharmony_ci	}
1051141cc406Sopenharmony_ci      else if (reader->params.depth == 12)
1052141cc406Sopenharmony_ci	{
1053141cc406Sopenharmony_ci	  if (dev->model->line_mode_color_order == COLOR_ORDER_RGB)
1054141cc406Sopenharmony_ci	    {
1055141cc406Sopenharmony_ci	      if (reader->params.ld_shift_double > 0)
1056141cc406Sopenharmony_ci		reader->read = line_read_rgb_double_12_line_mode;
1057141cc406Sopenharmony_ci	      else
1058141cc406Sopenharmony_ci		reader->read = line_read_rgb_12_line_mode;
1059141cc406Sopenharmony_ci	    }
1060141cc406Sopenharmony_ci	  else if (dev->model->line_mode_color_order == COLOR_ORDER_BGR)
1061141cc406Sopenharmony_ci	    reader->read = line_read_bgr_12_line_mode;
1062141cc406Sopenharmony_ci	}
1063141cc406Sopenharmony_ci      else if (reader->params.depth == 16)
1064141cc406Sopenharmony_ci	{
1065141cc406Sopenharmony_ci	  if (dev->model->line_mode_color_order == COLOR_ORDER_RGB)
1066141cc406Sopenharmony_ci	    {
1067141cc406Sopenharmony_ci	      if (reader->params.ld_shift_double > 0)
1068141cc406Sopenharmony_ci		reader->read = line_read_rgb_double_16_line_mode;
1069141cc406Sopenharmony_ci	      else
1070141cc406Sopenharmony_ci		reader->read = line_read_rgb_16_line_mode;
1071141cc406Sopenharmony_ci	    }
1072141cc406Sopenharmony_ci	  else if (dev->model->line_mode_color_order == COLOR_ORDER_BGR)
1073141cc406Sopenharmony_ci	    reader->read = line_read_bgr_16_line_mode;
1074141cc406Sopenharmony_ci	}
1075141cc406Sopenharmony_ci    }
1076141cc406Sopenharmony_ci  else
1077141cc406Sopenharmony_ci    {
1078141cc406Sopenharmony_ci      if (reader->params.depth == 8)
1079141cc406Sopenharmony_ci	{
1080141cc406Sopenharmony_ci	  if (dev->model->line_mode_color_order == COLOR_ORDER_RGB)
1081141cc406Sopenharmony_ci	    reader->read = line_read_rgb_8_pixel_mode;
1082141cc406Sopenharmony_ci	  else if (dev->model->line_mode_color_order == COLOR_ORDER_BGR)
1083141cc406Sopenharmony_ci	    reader->read = line_read_bgr_8_pixel_mode;
1084141cc406Sopenharmony_ci	}
1085141cc406Sopenharmony_ci      else if (reader->params.depth == 12)
1086141cc406Sopenharmony_ci	{
1087141cc406Sopenharmony_ci	  if (dev->model->line_mode_color_order == COLOR_ORDER_RGB)
1088141cc406Sopenharmony_ci	    reader->read = line_read_rgb_12_pixel_mode;
1089141cc406Sopenharmony_ci	  else if (dev->model->line_mode_color_order == COLOR_ORDER_BGR)
1090141cc406Sopenharmony_ci	    reader->read = line_read_bgr_12_pixel_mode;
1091141cc406Sopenharmony_ci	}
1092141cc406Sopenharmony_ci      else if (reader->params.depth == 16)
1093141cc406Sopenharmony_ci	{
1094141cc406Sopenharmony_ci	  if (dev->model->line_mode_color_order == COLOR_ORDER_RGB)
1095141cc406Sopenharmony_ci	    reader->read = line_read_rgb_16_pixel_mode;
1096141cc406Sopenharmony_ci	  else if (dev->model->line_mode_color_order == COLOR_ORDER_BGR)
1097141cc406Sopenharmony_ci	    reader->read = line_read_bgr_16_pixel_mode;
1098141cc406Sopenharmony_ci	}
1099141cc406Sopenharmony_ci    }
1100141cc406Sopenharmony_ci
1101141cc406Sopenharmony_ci  if (reader->read == NULL)
1102141cc406Sopenharmony_ci    {
1103141cc406Sopenharmony_ci      DBG (3, "gt68xx_line_reader_new: unsupported bit depth (%d)\n",
1104141cc406Sopenharmony_ci	   reader->params.depth);
1105141cc406Sopenharmony_ci      gt68xx_line_reader_free_delays (reader);
1106141cc406Sopenharmony_ci      free (reader);
1107141cc406Sopenharmony_ci      reader = NULL;
1108141cc406Sopenharmony_ci      return SANE_STATUS_UNSUPPORTED;
1109141cc406Sopenharmony_ci    }
1110141cc406Sopenharmony_ci
1111141cc406Sopenharmony_ci  scan_bpl_full = reader->params.scan_bpl;
1112141cc406Sopenharmony_ci
1113141cc406Sopenharmony_ci  if (reader->params.color && reader->params.line_mode)
1114141cc406Sopenharmony_ci    scan_bpl_full *= 3;
1115141cc406Sopenharmony_ci
1116141cc406Sopenharmony_ci  reader->pixel_buffer = malloc (scan_bpl_full);
1117141cc406Sopenharmony_ci  if (!reader->pixel_buffer)
1118141cc406Sopenharmony_ci    {
1119141cc406Sopenharmony_ci      DBG (3, "gt68xx_line_reader_new: cannot allocate pixel buffer\n");
1120141cc406Sopenharmony_ci      gt68xx_line_reader_free_delays (reader);
1121141cc406Sopenharmony_ci      free (reader);
1122141cc406Sopenharmony_ci      reader = NULL;
1123141cc406Sopenharmony_ci      return SANE_STATUS_NO_MEM;
1124141cc406Sopenharmony_ci    }
1125141cc406Sopenharmony_ci
1126141cc406Sopenharmony_ci  gt68xx_device_set_read_buffer_size (reader->dev,
1127141cc406Sopenharmony_ci				      scan_bpl_full /* * 200 */ );
1128141cc406Sopenharmony_ci
1129141cc406Sopenharmony_ci  image_size = reader->params.scan_bpl * reader->params.scan_ys;
1130141cc406Sopenharmony_ci  status = gt68xx_device_read_prepare (reader->dev, image_size, final_scan);
1131141cc406Sopenharmony_ci  if (status != SANE_STATUS_GOOD)
1132141cc406Sopenharmony_ci    {
1133141cc406Sopenharmony_ci      DBG (3,
1134141cc406Sopenharmony_ci	   "gt68xx_line_reader_new: gt68xx_device_read_prepare failed: %s\n",
1135141cc406Sopenharmony_ci	   sane_strstatus (status));
1136141cc406Sopenharmony_ci      free (reader->pixel_buffer);
1137141cc406Sopenharmony_ci      gt68xx_line_reader_free_delays (reader);
1138141cc406Sopenharmony_ci      free (reader);
1139141cc406Sopenharmony_ci      reader = NULL;
1140141cc406Sopenharmony_ci      return status;
1141141cc406Sopenharmony_ci    }
1142141cc406Sopenharmony_ci
1143141cc406Sopenharmony_ci  DBG (6, "gt68xx_line_reader_new: leave: ok\n");
1144141cc406Sopenharmony_ci  *reader_return = reader;
1145141cc406Sopenharmony_ci  return SANE_STATUS_GOOD;
1146141cc406Sopenharmony_ci}
1147141cc406Sopenharmony_ci
1148141cc406Sopenharmony_ciSANE_Status
1149141cc406Sopenharmony_cigt68xx_line_reader_free (GT68xx_Line_Reader * reader)
1150141cc406Sopenharmony_ci{
1151141cc406Sopenharmony_ci  SANE_Status status;
1152141cc406Sopenharmony_ci
1153141cc406Sopenharmony_ci  DBG (6, "gt68xx_line_reader_free: enter\n");
1154141cc406Sopenharmony_ci
1155141cc406Sopenharmony_ci  if (reader == NULL)
1156141cc406Sopenharmony_ci    {
1157141cc406Sopenharmony_ci      DBG (3, "gt68xx_line_reader_free: already freed\n");
1158141cc406Sopenharmony_ci      DBG (6, "gt68xx_line_reader_free: leave\n");
1159141cc406Sopenharmony_ci      return SANE_STATUS_INVAL;
1160141cc406Sopenharmony_ci    }
1161141cc406Sopenharmony_ci
1162141cc406Sopenharmony_ci  gt68xx_line_reader_free_delays (reader);
1163141cc406Sopenharmony_ci
1164141cc406Sopenharmony_ci  if (reader->pixel_buffer)
1165141cc406Sopenharmony_ci    {
1166141cc406Sopenharmony_ci      free (reader->pixel_buffer);
1167141cc406Sopenharmony_ci      reader->pixel_buffer = NULL;
1168141cc406Sopenharmony_ci    }
1169141cc406Sopenharmony_ci
1170141cc406Sopenharmony_ci  status = gt68xx_device_read_finish (reader->dev);
1171141cc406Sopenharmony_ci  if (status != SANE_STATUS_GOOD)
1172141cc406Sopenharmony_ci    {
1173141cc406Sopenharmony_ci      DBG (3,
1174141cc406Sopenharmony_ci	   "gt68xx_line_reader_free: gt68xx_device_read_finish failed: %s\n",
1175141cc406Sopenharmony_ci	   sane_strstatus (status));
1176141cc406Sopenharmony_ci    }
1177141cc406Sopenharmony_ci
1178141cc406Sopenharmony_ci  free (reader);
1179141cc406Sopenharmony_ci  reader = NULL;
1180141cc406Sopenharmony_ci
1181141cc406Sopenharmony_ci  DBG (6, "gt68xx_line_reader_free: leave\n");
1182141cc406Sopenharmony_ci  return status;
1183141cc406Sopenharmony_ci}
1184141cc406Sopenharmony_ci
1185141cc406Sopenharmony_ciSANE_Status
1186141cc406Sopenharmony_cigt68xx_line_reader_read (GT68xx_Line_Reader * reader,
1187141cc406Sopenharmony_ci			 unsigned int **buffer_pointers_return)
1188141cc406Sopenharmony_ci{
1189141cc406Sopenharmony_ci  SANE_Status status;
1190141cc406Sopenharmony_ci
1191141cc406Sopenharmony_ci  status = (*reader->read) (reader, buffer_pointers_return);
1192141cc406Sopenharmony_ci  return status;
1193141cc406Sopenharmony_ci}
1194141cc406Sopenharmony_ci
1195141cc406Sopenharmony_ci/* vim: set sw=2 cino=>2se-1sn-1s{s^-1st0(0u0 smarttab expandtab: */
1196