File: snapscan-310.c

package info (click to toggle)
sane 0.72-1.1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 3,008 kB
  • ctags: 5,180
  • sloc: ansic: 42,911; sh: 4,584; java: 1,421; makefile: 720
file content (374 lines) | stat: -rw-r--r-- 11,991 bytes parent folder | download | duplicates (2)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* sane - Scanner Access Now Easy.

   Copyright (C) 1997, 1998 Franck Schnefra, Michel Roelofs,
   Emmanuel Blot and Kevin Charter

   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; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA.

   As a special exception, the authors of SANE give permission for
   additional uses of the libraries contained in this release of SANE.

   The exception is that, if you link a SANE library with other files
   to produce an executable, this does not by itself cause the
   resulting executable to be covered by the GNU General Public
   License.  Your use of that executable is in no way restricted on
   account of linking the SANE library code into it.

   This exception does not, however, invalidate any other reasons why
   the executable file might be covered by the GNU General Public
   License.

   If you submit changes to SANE to the maintainers to be included in
   a subsequent release, you agree by submitting the changes that
   those changes may be distributed with this exception intact.

   If you write modifications of your own for SANE, it is your choice
   whether to permit this exception to apply to your modifications.
   If you do not wish that, delete this exception notice.

   This file implements a backend for the AGFA SnapScan flatbed
   scanner. */

/*
  This file implements the core of the SnapScan 310/600
  characteristics that defer from the SnapScan 300 model

  It is based on a RGB ring buffer that allows to synchronize
  the chrominance components.
*/

/* $Id: snapscan-310.c,v 1.1.3.1 1998/03/10 23:42:01 eblot Exp $
   SANE SnapScan 310/600 backend extension*/

#undef SNAPSCAN310_FORCE_600_DPI/* define for debugging 310 model */

SANE_Status rgb_buf_init (SnapScan_Scanner * pss);
void rgb_buf_clean (SnapScan_Scanner * pss);
SANE_Int rgb_buf_push_line (SnapScan_Scanner * pss,
			    const SANE_Byte * line,
			    const SANE_Int length);
void rgb_buf_get_line (SnapScan_Scanner * pss, SANE_Byte * line);
SANE_Bool rgb_buf_can_get_line (SnapScan_Scanner * pss);
void rgb_buf_set_diff (SnapScan_Scanner * pss,
		       SANE_Byte g_to_r,
		       SANE_Byte b_to_r);
SANE_Int transfer_data_diff (u_char * buf, SnapScan_Scanner * pss);

static inline SANE_Int rgb_buf_get_size (SnapScan_Scanner * pss);
static inline SANE_Int rgb_buf_get_line_pos (SnapScan_Scanner * pss);


/* Get the total number of lines of the RGB ring buffer */
static inline
  SANE_Int
rgb_buf_get_size (SnapScan_Scanner * pss)
{
  return
  (SANE_Int) pss->rgb_buf.g_offset +
  pss->rgb_buf.b_offset +
  pss->rgb_buf.r_offset + 1;
}

/* Get the first good line position from the RGB ring buffer */
static inline
  SANE_Int
rgb_buf_get_line_pos (SnapScan_Scanner * pss)
{
  return
  pss->rgb_buf.pixel_pos ?
  pss->rgb_buf.line_in - 1 :
  pss->rgb_buf.line_in;
}

/* Initialization of the RGB ring buffer */
SANE_Status
rgb_buf_init (SnapScan_Scanner * pss)
{
  static const char *me = "rgb_buf_init";

  size_t rgb_buf_bytes;
  pss->rgb_buf.line_in = 0;
  pss->rgb_buf.pixel_pos = 0;
  pss->rgb_buf.line_out = 0;

  /* get the ring buffer size */
  /* number of line = comp. total offset +
                      1 (size of last line) +
                      1 (running new line) */
  rgb_buf_bytes =
    pss->bytes_per_line *
    (rgb_buf_get_size (pss) + 2) *
    sizeof (SANE_Byte);

  /* allocate ring buffer memory */
  pss->rgb_buf.data = (SANE_Byte *) malloc (rgb_buf_bytes);

  if (!pss->rgb_buf.data)
    {
      DBG (DL_MAJOR_ERROR, "%s: rbuf_data cannot be allocated ! Out of memory ?\n", me);
      return SANE_STATUS_NO_MEM;
    }
  else
    {
      DBG (DL_VERBOSE,
	"%s: rgb ring buffer allocated: %d bytes, at %p, comp. offset %d\n",
	   me, (int) rgb_buf_bytes,
	   (void *) pss->rgb_buf.data, (int) rgb_buf_get_size (pss));
    }
  return SANE_STATUS_GOOD;
}

/* Clean up of the RGB ring buffer */
void
rgb_buf_clean (SnapScan_Scanner * pss)
{
  static const char *me = "rgb_buf_clean";

  /* free previously allocated buffer memory */
  if (pss->rgb_buf.data)
    {
      free (pss->rgb_buf.data);
      DBG (DL_VERBOSE, "%s: release RGB ring buffer at %p\n",
	   me, (void *) pss->rgb_buf.data);
      pss->rgb_buf.data = NULL;
    }

  pss->rgb_buf.line_in = 0;
  pss->rgb_buf.pixel_pos = 0;
  pss->rgb_buf.line_out = 0;
  pss->rgb_buf.g_offset = 0;
  pss->rgb_buf.b_offset = 0;
  pss->rgb_buf.r_offset = 0;
}

/* Add/complete up to 2 lines to the 'top' of the rgb ring buffer */
SANE_Int
rgb_buf_push_line (SnapScan_Scanner * pss,
		   const SANE_Byte * line,
		   const SANE_Int length)
{
  /* static    const char *me = "rgb_buf_push_line"; */

  SANE_Int max_pixels;		/* maximum number of pixels that can be processed */
  SANE_Int line_pixels;		/* pixels that in the processed line */
  SANE_Int done_pixels;		/* pixels already processed */
  SANE_Bool done_flag;		/* flag set when a full line has been completed */
  SANE_Int index;		/* index to access a line into the ring buffer */

  max_pixels = MIN (length, pss->bytes_per_line);
  done_pixels = 0;
  done_flag = SANE_FALSE;

  while (max_pixels)
    {
      line_pixels = MIN (max_pixels,
			 pss->bytes_per_line - pss->rgb_buf.pixel_pos);

      /* if the line is empty */
      if (0 == pss->rgb_buf.pixel_pos)
	{
	  if (SANE_TRUE == done_flag)
	    {
	      /* prevent against processing more than one full line */
	      break;
	    }

	  ++(pss->rgb_buf.line_in);
	  done_flag = SANE_TRUE;
	}

      index = pss->rgb_buf.line_in;	/* next index */
      index %= rgb_buf_get_size (pss) + 1;	/* convert circular to linear index */
      index *= pss->bytes_per_line;	/* line index -> byte index */

      /* copy the pixel data into the ring buffer */
      memcpy ((void *) (pss->rgb_buf.data + index + pss->rgb_buf.pixel_pos),
	      (const void *) (line + done_pixels),
	      line_pixels);

      done_pixels += line_pixels;
      max_pixels -= line_pixels;
      pss->rgb_buf.pixel_pos += line_pixels;

      /* if a line has been fully completed, job is done */
      if (pss->rgb_buf.pixel_pos >= pss->bytes_per_line)
	{
	  pss->rgb_buf.pixel_pos = 0;
	  done_flag = SANE_TRUE;
	  break;
	}
    }

  return done_pixels;
}

/* Create a plain line from the RGB ring buffer */
void
rgb_buf_get_line (SnapScan_Scanner * pss, SANE_Byte * line)
{
  /* static const char *me = "rgb_buf_get_line"; */

  SANE_Int modulo;		/* to convert virtual to physical buffer index */
  SANE_Int comp_pixels;		/* number of pixel f a component to process    */
  SANE_Int linear_pixel;	/* pix pos in a interlaced component line      */
  SANE_Int max_offset;		/* total height (in line) of the ring buffer   */
  SANE_Int r_pos;		/* offset of the red component   */
  SANE_Int b_pos;		/* offset of the blue component  */
  SANE_Int g_pos;		/* offset of the green component */
  SANE_Int pixel;		/* pixel loop counter */
  SANE_Int line_pos;		/* first valid line */

  modulo = rgb_buf_get_size (pss) + 1;
  line_pos = rgb_buf_get_line_pos (pss);
  comp_pixels = pss->bytes_per_line / 3;
  linear_pixel = 0;
  max_offset = MAX (MAX (pss->rgb_buf.g_offset, pss->rgb_buf.b_offset), pss->rgb_buf.r_offset);
  r_pos = (line_pos - (max_offset - pss->rgb_buf.r_offset)) % modulo;
  b_pos = (line_pos - (max_offset - pss->rgb_buf.b_offset)) % modulo;
  g_pos = (line_pos - (max_offset - pss->rgb_buf.g_offset)) % modulo;

  r_pos *= pss->bytes_per_line;
  g_pos *= pss->bytes_per_line;
  b_pos *= pss->bytes_per_line;
  g_pos += comp_pixels;		/* planar to interlaced pixels */
  b_pos += 2 * comp_pixels;	/* planar to interlaced pixels */

  /* for each pixel in the line,
     collect the three components and set the pixel RGB value */
#ifdef SNAPSCAN310_FORCE_600_DPI
  if (pss->res != 600)
    {
#endif
      for (pixel = 0; pixel < comp_pixels; pixel++)
	{
	  line[linear_pixel++] = pss->rgb_buf.data[pixel + r_pos];
	  line[linear_pixel++] = pss->rgb_buf.data[pixel + g_pos];
	  line[linear_pixel++] = pss->rgb_buf.data[pixel + b_pos];
	}
#ifdef SNAPSCAN310_FORCE_600_DPI
    }
  else
    {
      /* 310 model seems to produce 300x600 dpi in 600 dpi mode,
       thus, we have to supersample by 2 times within the
       horizontal direction. Linear filtering, or bilinear
       filtering would do better results ... */
      comp_pixels >>= 1;
      for (pixel = 0; pixel < comp_pixels; pixel++)
	{
	  line[linear_pixel++] = pss->rgb_buf.data[pixel + r_pos];
	  line[linear_pixel++] = pss->rgb_buf.data[pixel + g_pos];
	  line[linear_pixel++] = pss->rgb_buf.data[pixel + b_pos];
	  line[linear_pixel++] = pss->rgb_buf.data[pixel + r_pos];
	  line[linear_pixel++] = pss->rgb_buf.data[pixel + g_pos];
	  line[linear_pixel++] = pss->rgb_buf.data[pixel + b_pos];
	}
    }
#endif

  /* increase the valid read line counter */
  ++(pss->rgb_buf.line_out);
}

/* Check if a valid line can be generated from the RGB ring buffer */
SANE_Bool
rgb_buf_can_get_line (SnapScan_Scanner * pss)
{
  /* static const char *me = "rgb_buf_can_get_line"; */

  return
    (rgb_buf_get_line_pos (pss) - pss->rgb_buf.line_out) >
    (pss->rgb_buf.g_offset + pss->rgb_buf.b_offset + pss->rgb_buf.r_offset) ?
    SANE_TRUE : SANE_FALSE;
}

/* Set the red, green, and blue difference */
void
rgb_buf_set_diff (SnapScan_Scanner * pss, SANE_Byte g_to_r, SANE_Byte b_to_r)
{
  static char me[] = "rgb_buf_set_diff";

  signed char min_diff;
  signed char g = (g_to_r & 0x80) ? -(g_to_r & 0x7F) : g_to_r;
  signed char b = (b_to_r & 0x80) ? -(b_to_r & 0x7F) : b_to_r;

  min_diff = MIN (MIN (b, g), 0);

  pss->rgb_buf.g_offset = (u_char) (g - min_diff);
  pss->rgb_buf.b_offset = (u_char) (b - min_diff);
  pss->rgb_buf.r_offset = (u_char) (0 - min_diff);

  DBG (DL_VERBOSE, "%s: Chroma offsets Red:%u, Green:%u Blue:%u\n",
       me,
       pss->rgb_buf.r_offset,
       pss->rgb_buf.g_offset,
       pss->rgb_buf.b_offset);
}

SANE_Int
transfer_data_diff (u_char * buf, SnapScan_Scanner * pss)
{
  static char me[] = "transfer_data_diff";

  SANE_Int done_bytes;		/* bytes that have been processed               */
  SANE_Int line_bytes;		/* bytes processed in the last 'push_line' call */
  SANE_Int remaining_bytes;	/* bytes to be processed                        */
  SANE_Int transferred_bytes;	/* bytes generated by the ring buffer           */

  done_bytes = 0;
  line_bytes = 0;
  remaining_bytes = pss->read_bytes;
  transferred_bytes = 0;

  if (pss->read_bytes > 0)
    {
      /* lines = pss->read_bytes / pss->bytes_per_line; */

      transferred_bytes = 0;
      do
	{
	  line_bytes = rgb_buf_push_line (pss, pss->buf + done_bytes, remaining_bytes);
	  remaining_bytes -= line_bytes;
	  done_bytes += line_bytes;

	  if (rgb_buf_can_get_line (pss))
	    {
	      rgb_buf_get_line (pss, buf + transferred_bytes);
	      transferred_bytes += pss->bytes_per_line;
	    }
	}
      while (remaining_bytes);	/*while ( --lines );*/
    }

  DBG (DL_VERBOSE, "%s: transferred %lu lines (%lu bytes)\n",
       me, (u_long) pss->read_bytes / pss->bytes_per_line,
       (u_long) transferred_bytes);

  return transferred_bytes;
}

/* $Log: snapscan-310.c,v $
 * Revision 1.1.3.1  1998/03/10 23:42:01  eblot
 * Debugging: large windows, color preview
 *
 * Revision 1.1.2  1998/03/10 21:45:14  eblot
 * Workaround for the color preview mode
 *
 * Revision 1.1.1  1998/03/10 21:32:07  eblot
 * Workaround for the '4096 bytes' bug
 * */