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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
/* sane - Scanner Access Now Easy.
Copyright (C) Marian Eichholz 2001
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.
*/
/* ======================================================================
Userspace scan tool for the Microtek 3600 scanner
grayscale scan routine
(C) Marian Eichholz 2001
====================================================================== */
#include "sm3600-scantool.h"
/* **********************************************************************
DoScanGray()
********************************************************************** */
#define LINE_THRESHOLD 0x800
static unsigned char uchRegs075[]={
/*R_SPOS*/ 0xFC, /*R_SPOSH*/ 0x00, /*0x03*/ 0x20,
/*R_SWID*/ 0xB0, /*R_SWIDH*/ 0x04, /*R_STPS*/ 0x06,
/*R_STPSH*/ 0x00, /*0x08*/ 0x00, /*0x09*/ 0x3F,
/*R_LEN*/ 0x28, /*R_LENH*/ 0x07, /*0x0C*/ 0x6D,
/*0x0D*/ 0x70, /*0x0E*/ 0x69, /*0x0F*/ 0xD0,
/*0x10*/ 0x00, /*0x11*/ 0x00, /*0x12*/ 0x40,
/*0x13*/ 0x15, /*0x14*/ 0x80, /*0x15*/ 0x2A,
/*0x16*/ 0xC0, /*0x17*/ 0x40, /*0x18*/ 0xC0,
/*0x19*/ 0x40, /*0x1A*/ 0xFF, /*0x1B*/ 0x01,
/*0x1C*/ 0x88, /*0x1D*/ 0x40, /*0x1E*/ 0x4C,
/*0x1F*/ 0x50, /*0x20*/ 0x00, /*0x21*/ 0x0C,
/*0x22*/ 0x21, /*0x23*/ 0xF0, /*0x24*/ 0x40,
/*0x25*/ 0x00, /*0x26*/ 0x0A, /*0x27*/ 0xF0,
/*0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 0x42, /*0x17*/ 0x15, /*0x18*/ 0x42,
/*0x19*/ 0x15, /*0x1A*/ 0x07, /*0x1B*/ 0x00,
/*0x1C*/ 0x08, /*0x1D*/ 0x12, /*0x1E*/ 0x4C,
/*0x1F*/ 0x50, /*0x20*/ 0x00, /*0x21*/ 0x0C,
/*0x22*/ 0x21, /*0x23*/ 0xF0, /*0x24*/ 0x40,
/*0x25*/ 0x00, /*0x26*/ 0x0A, /*0x27*/ 0xF0,
/*0x28*/ 0x00, /*0x29*/ 0x00, /*0x2A*/ 0x4E,
/*0x2B*/ 0xF0, /*0x2C*/ 0x00, /*0x2D*/ 0x00,
/*0x2E*/ 0x4E, /*R_CCAL*/ 0x80, /*R_CCAL2*/ 0x80,
/*R_CCAL3*/ 0x80, /*0x32*/ 0xC9, /*0x33*/ 0x20,
/*0x34*/ 0x03, /*0x35*/ 0x29, /*0x36*/ 0x00,
/*0x37*/ 0x00, /*0x38*/ 0x00, /*0x39*/ 0x00,
/*0x3A*/ 0x00, /*0x3B*/ 0x00, /*0x3C*/ 0xFF,
/*0x3D*/ 0x0F, /*0x3E*/ 0x00, /*0x3F*/ 0x00,
/*0x40*/ 0x01, /*0x41*/ 0x00, /*R_CSTAT*/ 0x80,
/*0x43*/ 0x03, /*R_LMP*/ 0x01, /*0x45*/ 0x00,
/*R_CTL*/ 0x39, /*0x47*/ 0x42, /*0x48*/ 0x15,
/*0x49*/ 0x96, /*0x4A*/ 0x8C };
/* ======================================================================
ReadNextGrayLine()
====================================================================== */
static TState ReadNextGrayLine(PTInstance this)
{
int iWrite;
int iDot;
unsigned char chBits;
int iRead; /* read position in raw line */
int nInterpolator;
iWrite=0;
while (iWrite<this->state.cxMax) /* max 1 time in reality */
{
while (iWrite<this->state.cxMax &&
this->state.iBulkReadPos<this->state.cchBulk)
this->state.ppchLines[0][iWrite++] += /* add! */
this->state.pchBuf[this->state.iBulkReadPos++]<<4;
if (iWrite<this->state.cxMax) /* we need an additional chunk */
{
if (this->state.bLastBulk)
return SANE_STATUS_EOF;
this->state.cchBulk=BulkReadBuffer(this,this->state.pchBuf,
USB_CHUNK_SIZE);
dprintf(DEBUG_SCAN,"bulk read: %d byte(s), line #%d\n",
this->state.cchBulk, this->state.iLine);
if (this->bWriteRaw)
fwrite(this->state.pchBuf,1,this->state.cchBulk,this->fhScan);
INST_ASSERT();
if (this->state.cchBulk!=USB_CHUNK_SIZE)
this->state.bLastBulk=true;
this->state.iBulkReadPos=0;
}
} /* while raw line buffer acquiring */
this->state.iLine++;
iDot=0; chBits=0; /* init pixelbuffer */
for (nInterpolator=50, iWrite=0, iRead=0;
iRead<this->state.cxMax;
iRead++)
{
nInterpolator+=this->state.nFixAspect;
if (nInterpolator<100) continue; /* res. reduction */
nInterpolator-=100;
if (iWrite>=this->state.cchLineOut) continue;
/* dprintf(DEBUG_SCAN," i=%d",iTo); */
if (this->mode==gray)
this->state.pchLineOut[iWrite++]=
this->state.ppchLines[0][iRead]>>4;
else
{
unsigned char chBit; /* 1=white */
if (this->mode==line)
chBit=(this->state.ppchLines[0][iRead]<LINE_THRESHOLD);
else
{
short nError=this->state.ppchLines[0][iRead];
/* printf("(%d)",nError); */
if (nError>=0xFF0)
{
nError-=0xFF0;
chBit=0;
}
else
chBit=1;
/* since I sketched the Floyd-Steinberg
algorithm from heart, I have no idea, if
there is room for improvement in the
coefficients. If You know, please drop
me a line (eichholz@computer.org, 1.4.2001) */
#define FASTDITHER
#ifdef FASTDITHER
this->state.ppchLines[0][iRead+1]+=(nError>>2); /* 8/16 */
this->state.ppchLines[1][iRead+1]+=(nError>>1);
this->state.ppchLines[1][iRead] +=(nError>>2); /* 8/16 */
#else
this->state.ppchLines[0][iRead+1]+=(nError*5)>>4;
this->state.ppchLines[1][iRead+1]+=(nError*8)>>4;
this->state.ppchLines[1][iRead] +=(nError*3)>>4;
#endif
}
chBits=(chBits<<1)|chBit;
iDot++;
if (iDot==8 && iWrite<this->state.cchLineOut)
{
this->state.pchLineOut[iWrite++]=chBits;
iDot=0; chBits=0;
}
} /* gray pixel postprocessing */
} /* line postprocessing */
if (iDot && iWrite<this->state.cchLineOut)
this->state.pchLineOut[iWrite++]=chBits;
/* cycle the history lines and clear the preread buffer*/
{
short *p=this->state.ppchLines[0];
this->state.ppchLines[0]=this->state.ppchLines[1];
this->state.ppchLines[1]=p;
memset(this->state.ppchLines[1],0,(this->state.cxMax+1)*sizeof(short));
}
return SANE_STATUS_GOOD;
}
/* ======================================================================
StartScanGray()
====================================================================== */
__SM3600EXPORT__
TState StartScanGray(TInstance *this)
{
unsigned char *puchRegs;
int i;
if (this->state.bScanning)
return SetError(this,SANE_STATUS_DEVICE_BUSY,"scan active");
memset(&(this->state),0,sizeof(TScanState));
this->state.ReadProc =ReadNextGrayLine;
puchRegs=NULL;
switch (this->param.res)
{
case 75: puchRegs=uchRegs075; break;
case 100: puchRegs=uchRegs100; break;
case 200: puchRegs=uchRegs200; break;
case 300: puchRegs=uchRegs300; break;
case 600: puchRegs=uchRegs600; break;
}
GetAreaSize(this);
this->state.cyTotalPath = this->param.y/2;
DoJog(this,this->state.cyTotalPath);
INST_ASSERT();
this->state.cyTotalPath += this->param.cy/2; /* for jogging back */
/*
regular scan is asynchronously, that is,
the scanning is issued, and the driver does bulk reads,
until there are no data left.
*/
RegWriteArray(this,R_ALL, NUM_SCANREGS, puchRegs); INST_ASSERT();
RegWrite(this,R_SPOS, 2,
this->param.x/2+this->calibration.xMargin); INST_ASSERT();
RegWrite(this,R_SLEN, 2, this->state.cyWindow); INST_ASSERT();
RegWrite(this,R_SWID, 2, this->state.cxWindow); INST_ASSERT();
RegWrite(this,R_STPS, 2, 0); INST_ASSERT();
/* upload gamma table */
RegWrite(this,0x41,1,0x01); /* gamma, gray */
RegWrite(this,0x40,1,0x20); /* FIFO at 0x08000 */
UploadGammaTable(this,0,this->agammaY); INST_ASSERT();
UploadGainCorrection(this, 0x2000);
INST_ASSERT();
/* for halftone dithering we need one history line */
this->state.pchBuf=malloc(USB_CHUNK_SIZE);
this->state.cBacklog=2;
this->state.ppchLines=calloc(this->state.cBacklog,sizeof(short *));
if (!this->state.pchBuf || !this->state.ppchLines)
return FreeState(this,SetError(this,
SANE_STATUS_NO_MEM,"no buffers available"));
for (i=0; i<this->state.cBacklog; i++)
{
this->state.ppchLines[i]=calloc(this->state.cxMax+1,
sizeof(short)); /* 1 dummy at right edge */
if (!this->state.ppchLines[i])
return FreeState(this,SetError(this,
SANE_STATUS_NO_MEM,"no buffers available"));
}
/* calculate and prepare intermediate line transfer buffer */
this->state.cchLineOut=(this->mode==gray)
? this->state.cxPixel
: (this->state.cxPixel+7)/8;
this->state.pchLineOut = malloc(this->state.cchLineOut);
if (!this->state.pchLineOut)
return FreeState(this,SetError(this,
SANE_STATUS_NO_MEM,
"no buffers available"));
/* start the unit, when all buffers are available */
RegWrite(this,R_CTL, 1, 0x39); INST_ASSERT();
RegWrite(this,R_CTL, 1, 0x79); INST_ASSERT();
RegWrite(this,R_CTL, 1, 0xF9); INST_ASSERT();
this->state.bScanning = true;
return SANE_STATUS_GOOD;
}
|