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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
/* @file u12-pp_tpa.c
* @brief Here we find some adjustments according to the scan source.
*
* based on sources acquired from Plustek Inc.
* Copyright (C) 2003-2004 Gerhard Jaeger <gerhard@gjaeger.de>
*
* History:
* - 0.01 - initial version
* - 0.02 - cleanup
* .
* <hr>
* 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, see <https://www.gnu.org/licenses/>.
*
* 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.
* <hr>
*/
/** this function does some reshading, when scanning negatives on an ASIC 98003
* based scanner
*/
static void u12tpa_Reshading( U12_Device *dev )
{
SANE_Byte bHi[3], bHiLeft[3], bHiRight[3];
u_long i, dwR, dwG, dwB, dwSum;
u_long dwIndex, dwIndexRight, dwIndexLeft;
DataPointer RedPtr, GreenPtr, BluePtr;
TimerDef timer;
DBG( _DBG_INFO, "u12tpa_Reshading()\n" );
bHi[0] = bHi[1] = bHi[2] = 0;
dev->scan.negScan[1].exposureTime = 144;
dev->scan.negScan[1].xStepTime = 18;
dev->scan.negScan[2].exposureTime = 144;
dev->scan.negScan[2].xStepTime = 36;
dev->scan.negScan[3].exposureTime = 144;
dev->scan.negScan[3].xStepTime = 72;
dev->scan.negScan[4].exposureTime = 144;
dev->scan.negScan[4].xStepTime = 144;
dev->shade.wExposure = dev->scan.negScan[dev->scan.dpiIdx].exposureTime;
dev->shade.wXStep = dev->scan.negScan[dev->scan.dpiIdx].xStepTime;
u12io_StartTimer( &timer, _SECOND );
u12io_ResetFifoLen();
while(!(u12io_GetScanState( dev ) & _SCANSTATE_STOP) &&
(!u12io_CheckTimer(&timer)));
u12io_DataToRegister( dev, REG_XSTEPTIME,
(SANE_Byte)(dev->regs.RD_LineControl >> 4));
_DODELAY( 12 );
u12motor_PositionYProc( dev, _NEG_SHADING_OFFS );
u12io_DataToRegister( dev, REG_XSTEPTIME, dev->regs.RD_XStepTime );
dev->regs.RD_ScanControl = _SCAN_BYTEMODE;
u12hw_SelectLampSource( dev );
u12io_DataToRegister( dev, REG_LINECONTROL, _LOBYTE(dev->shade.wExposure));
u12io_DataToRegister( dev, REG_XSTEPTIME, _LOBYTE(dev->shade.wXStep));
dev->regs.RD_LineControl = _LOBYTE(dev->shade.wExposure);
dev->regs.RD_ExtLineControl = _HIBYTE(dev->shade.wExposure);
dev->regs.RD_XStepTime = (SANE_Byte)(dev->shade.wExposure);
dev->regs.RD_ModeControl = _ModeScan;
dev->regs.RD_Motor0Control = _FORWARD_MOTOR;
dev->regs.RD_Origin = (u_short)dev->scan.negBegin;
dev->regs.RD_Pixels = _NEG_PAGEWIDTH600;
memset( dev->scanStates, 0, _SCANSTATE_BYTES );
/* put 9 scan states to make sure there are 8 lines available at least */
for( i = 0; i <= 12; i++)
dev->scanStates[i] = 0x8f;
u12io_PutOnAllRegisters( dev );
_DODELAY( 70 );
/* prepare the buffers... */
memset( dev->bufs.TpaBuf.pb, 0, _SIZE_TPA_DATA_BUF );
RedPtr.pb = dev->bufs.b1.pShadingMap;
GreenPtr.pb = RedPtr.pb + _NEG_PAGEWIDTH600;
BluePtr.pb = GreenPtr.pb + _NEG_PAGEWIDTH600;
for( dwSum = 8; dwSum--; ) {
u12io_ReadOneShadingLine( dev, dev->bufs.b1.pShadingMap, _NEG_PAGEWIDTH600 );
for( i = 0; i < _NEG_PAGEWIDTH600; i++) {
dev->bufs.TpaBuf.pusrgb[i].Red += RedPtr.pb[i];
dev->bufs.TpaBuf.pusrgb[i].Green += GreenPtr.pb[i];
dev->bufs.TpaBuf.pusrgb[i].Blue += BluePtr.pb[i];
}
}
for( i = 0; i < (_NEG_PAGEWIDTH600 * 3UL); i++ )
dev->bufs.TpaBuf.pb[i] = dev->bufs.TpaBuf.pw[i] >> 3;
RedPtr.pb = dev->bufs.TpaBuf.pb;
/* Convert RGB to gray scale (Brightness), and average 16 pixels */
for( bHiRight[1] = 0, i = dwIndexRight = 0;
i < _NEG_PAGEWIDTH600 / 2; i += 16 ) {
bHiRight [0] =
(SANE_Byte)(((((u_long) RedPtr.pbrgb [i].Red +
(u_long) RedPtr.pbrgb[i + 1].Red +
(u_long) RedPtr.pbrgb[i + 2].Red +
(u_long) RedPtr.pbrgb[i + 3].Red +
(u_long) RedPtr.pbrgb[i + 4].Red +
(u_long) RedPtr.pbrgb[i + 5].Red +
(u_long) RedPtr.pbrgb[i + 6].Red +
(u_long) RedPtr.pbrgb[i + 7].Red +
(u_long) RedPtr.pbrgb[i + 8].Red +
(u_long) RedPtr.pbrgb[i + 9].Red +
(u_long) RedPtr.pbrgb[i + 10].Red +
(u_long) RedPtr.pbrgb[i + 11].Red +
(u_long) RedPtr.pbrgb[i + 12].Red +
(u_long) RedPtr.pbrgb[i + 13].Red +
(u_long) RedPtr.pbrgb[i + 14].Red +
(u_long) RedPtr.pbrgb[i + 15].Red) >> 4) * 30UL +
(((u_long) RedPtr.pbrgb[i].Green +
(u_long) RedPtr.pbrgb[i + 1].Green +
(u_long) RedPtr.pbrgb[i + 2].Green +
(u_long) RedPtr.pbrgb[i + 3].Green +
(u_long) RedPtr.pbrgb[i + 4].Green +
(u_long) RedPtr.pbrgb[i + 5].Green +
(u_long) RedPtr.pbrgb[i + 6].Green +
(u_long) RedPtr.pbrgb[i + 7].Green +
(u_long) RedPtr.pbrgb[i + 8].Green +
(u_long) RedPtr.pbrgb[i + 9].Green +
(u_long) RedPtr.pbrgb[i + 10].Green +
(u_long) RedPtr.pbrgb[i + 11].Green +
(u_long) RedPtr.pbrgb[i + 12].Green +
(u_long) RedPtr.pbrgb[i + 13].Green +
(u_long) RedPtr.pbrgb[i + 14].Green +
(u_long) RedPtr.pbrgb[i + 15].Green) >> 4) * 59UL +
(((u_long) RedPtr.pbrgb[i].Blue +
(u_long) RedPtr.pbrgb[i + 1].Blue +
(u_long) RedPtr.pbrgb[i + 2].Blue +
(u_long) RedPtr.pbrgb[i + 3].Blue +
(u_long) RedPtr.pbrgb[i + 4].Blue +
(u_long) RedPtr.pbrgb[i + 5].Blue +
(u_long) RedPtr.pbrgb[i + 6].Blue +
(u_long) RedPtr.pbrgb[i + 7].Blue +
(u_long) RedPtr.pbrgb[i + 8].Blue +
(u_long) RedPtr.pbrgb[i + 9].Blue +
(u_long) RedPtr.pbrgb[i + 10].Blue +
(u_long) RedPtr.pbrgb[i + 11].Blue +
(u_long) RedPtr.pbrgb[i + 12].Blue +
(u_long) RedPtr.pbrgb[i + 13].Blue +
(u_long) RedPtr.pbrgb[i + 14].Blue +
(u_long) RedPtr.pbrgb[i + 15].Blue) >> 4) * 11UL) / 100UL);
if( bHiRight[1] < bHiRight[0] ) {
bHiRight[1] = bHiRight[0];
dwIndexRight = i;
}
}
/* Convert RGB to gray scale (Brightness), and average 16 pixels */
for( bHiLeft[1] = 0, i = dwIndexLeft = _NEG_PAGEWIDTH / 2;
i < _NEG_PAGEWIDTH600; i += 16 ) {
bHiLeft [0] =
(SANE_Byte)(((((u_long) RedPtr.pbrgb[i].Red +
(u_long) RedPtr.pbrgb[i + 1].Red +
(u_long) RedPtr.pbrgb[i + 2].Red +
(u_long) RedPtr.pbrgb[i + 3].Red +
(u_long) RedPtr.pbrgb[i + 4].Red +
(u_long) RedPtr.pbrgb[i + 5].Red +
(u_long) RedPtr.pbrgb[i + 6].Red +
(u_long) RedPtr.pbrgb[i + 7].Red +
(u_long) RedPtr.pbrgb[i + 8].Red +
(u_long) RedPtr.pbrgb[i + 9].Red +
(u_long) RedPtr.pbrgb[i + 10].Red +
(u_long) RedPtr.pbrgb[i + 11].Red +
(u_long) RedPtr.pbrgb[i + 12].Red +
(u_long) RedPtr.pbrgb[i + 13].Red +
(u_long) RedPtr.pbrgb[i + 14].Red +
(u_long) RedPtr.pbrgb[i + 15].Red) >> 4) * 30UL +
(((u_long) RedPtr.pbrgb[i].Green +
(u_long) RedPtr.pbrgb[i + 1].Green +
(u_long) RedPtr.pbrgb[i + 2].Green +
(u_long) RedPtr.pbrgb[i + 3].Green +
(u_long) RedPtr.pbrgb[i + 4].Green +
(u_long) RedPtr.pbrgb[i + 5].Green +
(u_long) RedPtr.pbrgb[i + 6].Green +
(u_long) RedPtr.pbrgb[i + 7].Green +
(u_long) RedPtr.pbrgb[i + 8].Green +
(u_long) RedPtr.pbrgb[i + 9].Green +
(u_long) RedPtr.pbrgb[i + 10].Green +
(u_long) RedPtr.pbrgb[i + 11].Green +
(u_long) RedPtr.pbrgb[i + 12].Green +
(u_long) RedPtr.pbrgb[i + 13].Green +
(u_long) RedPtr.pbrgb[i + 14].Green +
(u_long) RedPtr.pbrgb[i + 15].Green) >> 4) * 59UL +
(((u_long) RedPtr.pbrgb[i].Blue +
(u_long) RedPtr.pbrgb[i + 1].Blue +
(u_long) RedPtr.pbrgb[i + 2].Blue +
(u_long) RedPtr.pbrgb[i + 3].Blue +
(u_long) RedPtr.pbrgb[i + 4].Blue +
(u_long) RedPtr.pbrgb[i + 5].Blue +
(u_long) RedPtr.pbrgb[i + 6].Blue +
(u_long) RedPtr.pbrgb[i + 7].Blue +
(u_long) RedPtr.pbrgb[i + 8].Blue +
(u_long) RedPtr.pbrgb[i + 9].Blue +
(u_long) RedPtr.pbrgb[i + 10].Blue +
(u_long) RedPtr.pbrgb[i + 11].Blue +
(u_long) RedPtr.pbrgb[i + 12].Blue +
(u_long) RedPtr.pbrgb[i + 13].Blue +
(u_long) RedPtr.pbrgb[i + 14].Blue +
(u_long) RedPtr.pbrgb[i + 15].Blue) >> 4) * 11UL) / 100UL);
if( bHiLeft[1] < bHiLeft[0] ) {
bHiLeft[1] = bHiLeft[0];
dwIndexLeft = i;
}
}
if((bHiLeft[1] < 200) && (bHiRight[1] < 200)) {
if( bHiLeft[1] < bHiRight[1] )
dwIndex = dwIndexRight;
else
dwIndex = dwIndexLeft;
} else {
if( bHiLeft[1] > 200 )
dwIndex = dwIndexRight;
else
dwIndex = dwIndexLeft;
}
/* Get the hilight */
RedPtr.pusrgb = dev->bufs.b2.pSumRGB + dwIndex +
dev->regs.RD_Origin + _SHADING_BEGINX;
for( dwR = dwG = dwB = 0, i = 16; i--; RedPtr.pusrgb++ ) {
dwR += RedPtr.pusrgb->Red;
dwG += RedPtr.pusrgb->Green;
dwB += RedPtr.pusrgb->Blue;
}
dwR >>= 8;
dwG >>= 8;
dwB >>= 8;
if( dwR > dwG && dwR > dwB )
dev->shade.bGainHigh = (SANE_Byte)dwR; /* >> 4 for average, >> 4 to 8-bit */
else {
if( dwG > dwR && dwG > dwB )
dev->shade.bGainHigh = (SANE_Byte)dwG;
else
dev->shade.bGainHigh = (SANE_Byte)dwB;
}
dev->shade.bGainHigh = (SANE_Byte)(dev->shade.bGainHigh - 0x18);
dev->shade.bGainLow = (SANE_Byte)(dev->shade.bGainHigh - 0x10);
/* Reshading to get the new gain */
dev->shade.Hilight.Colors.Red = 0;
dev->shade.Hilight.Colors.Green = 0;
dev->shade.Hilight.Colors.Blue = 0;
dev->shade.Gain.Colors.Red++;
dev->shade.Gain.Colors.Green++;
dev->shade.Gain.Colors.Blue++;
dev->shade.fStop = SANE_FALSE;
RedPtr.pb = dev->bufs.b1.pShadingMap + dwIndex;
GreenPtr.pb = RedPtr.pb + _NEG_PAGEWIDTH600;
BluePtr.pb = GreenPtr.pb + _NEG_PAGEWIDTH600;
for( i = 16; i-- && !dev->shade.fStop;) {
dev->shade.fStop = SANE_TRUE;
u12shading_FillToDAC( dev, &dev->RegDACGain, &dev->shade.Gain );
u12io_DataToRegister( dev, REG_MODECONTROL, _ModeIdle );
dev->regs.RD_ScanControl = _SCAN_BYTEMODE;
u12hw_SelectLampSource( dev );
dev->regs.RD_ModeControl = _ModeScan;
dev->regs.RD_StepControl = _MOTOR0_SCANSTATE;
dev->regs.RD_Motor0Control = _FORWARD_MOTOR;
memset( dev->scanStates, 0, _SCANSTATE_BYTES );
dev->scanStates[1] = 0x77;
u12io_PutOnAllRegisters( dev );
_DODELAY( 50 );
if(u12io_ReadOneShadingLine( dev,
dev->bufs.b1.pShadingMap,_NEG_PAGEWIDTH600)) {
bHi[0] = u12shading_SumGains( RedPtr.pb, 32 );
bHi[1] = u12shading_SumGains( GreenPtr.pb, 32 );
bHi[2] = u12shading_SumGains( BluePtr.pb, 32 );
if( !bHi[0] || !bHi[1] || !bHi[2]) {
dev->shade.fStop = SANE_FALSE;
} else {
u12shading_AdjustGain( dev, _CHANNEL_RED, bHi[0] );
u12shading_AdjustGain( dev, _CHANNEL_GREEN, bHi[1] );
u12shading_AdjustGain( dev, _CHANNEL_BLUE, bHi[2] );
}
} else {
dev->shade.fStop = SANE_FALSE;
}
}
u12shading_FillToDAC( dev, &dev->RegDACGain, &dev->shade.Gain );
/* Set RGB Gain */
if( dwR && dwG && dwB ) {
if(dev->CCDID == _CCD_3797 || dev->DACType == _DA_ESIC ) {
dev->shade.pCcdDac->GainResize.Colors.Red =
(u_short)((u_long)bHi[0] * 100UL / dwR);
dev->shade.pCcdDac->GainResize.Colors.Green =
(u_short)((u_long)bHi[1] * 100UL / dwG);
dev->shade.pCcdDac->GainResize.Colors.Blue =
(u_short)((u_long)bHi[2] * 100UL / dwB);
} else {
dev->shade.pCcdDac->GainResize.Colors.Red =
(u_short)((u_long)bHi[0] * 90UL / dwR);
dev->shade.pCcdDac->GainResize.Colors.Green =
(u_short)((u_long)bHi[1] * 77UL / dwG);
dev->shade.pCcdDac->GainResize.Colors.Blue =
(u_short)((u_long)bHi[2] * 73UL / dwB);
}
dev->shade.DarkOffset.Colors.Red +=
(u_short)((dwR > bHi[0]) ? dwR - bHi[0] : 0);
dev->shade.DarkOffset.Colors.Green +=
(u_short)((dwG > bHi[1]) ? dwG - bHi[1] : 0);
dev->shade.DarkOffset.Colors.Blue +=
(u_short)((dwB > bHi[2]) ? dwB - bHi[2] : 0);
if( dev->DACType != _DA_ESIC && dev->CCDID != _CCD_3799 ) {
dev->shade.DarkOffset.Colors.Red =
(u_short)(dev->shade.DarkOffset.Colors.Red *
dev->shade.pCcdDac->GainResize.Colors.Red / 100UL);
dev->shade.DarkOffset.Colors.Green =
(u_short)(dev->shade.DarkOffset.Colors.Green *
dev->shade.pCcdDac->GainResize.Colors.Green / 100UL);
dev->shade.DarkOffset.Colors.Blue =
(u_short)(dev->shade.DarkOffset.Colors.Blue *
dev->shade.pCcdDac->GainResize.Colors.Blue / 100UL);
}
}
/* AdjustDark () */
dev->regs.RD_Origin = _SHADING_BEGINX;
dev->regs.RD_Pixels = 5400;
}
/** perform some adjustments according to the source (normal, transparency etc)
*/
static void u12tpa_FindCenterPointer( U12_Device *dev )
{
u_long i;
u_long width;
u_long left;
u_long right;
RGBUShortDef *pwSum = dev->bufs.b2.pSumRGB;
if( dev->DataInf.dwScanFlag & _SCANDEF_Negative )
width = _NEG_PAGEWIDTH600;
else
width = _NEG_PAGEWIDTH600 - 94;
/* 2.54 cm tolerance */
left = _DATA_ORIGIN_X + _NEG_ORG_OFFSETX * 2 - 600;
right = _DATA_ORIGIN_X + _NEG_ORG_OFFSETX * 2 +
_NEG_PAGEWIDTH600 + 600;
for( i = 5400UL - left, pwSum = dev->bufs.b2.pSumRGB; i--; left++)
if( pwSum[left].Red > _NEG_EDGE_VALUE &&
pwSum[left].Green > _NEG_EDGE_VALUE &&
pwSum[left].Blue > _NEG_EDGE_VALUE)
break;
for( i = 5400UL - left, pwSum = dev->bufs.b2.pSumRGB; i--; right--)
if( pwSum[right].Red > _NEG_EDGE_VALUE &&
pwSum[right].Green > _NEG_EDGE_VALUE &&
pwSum[right].Blue > _NEG_EDGE_VALUE)
break;
if((right <= left) || ((right - left) < width)) {
if( dev->DataInf.dwScanFlag & _SCANDEF_Negative )
dev->scan.negBegin = _DATA_ORIGIN_X + _NEG_ORG_OFFSETX * 2;
else
dev->scan.posBegin = _DATA_ORIGIN_X + _POS_ORG_OFFSETX * 2;
} else {
if( dev->DataInf.dwScanFlag & _SCANDEF_Negative )
dev->scan.negBegin = (right + left) / 2UL - _NEG_PAGEWIDTH;
else
dev->scan.posBegin = (right + left) / 2UL - _POS_PAGEWIDTH;
}
}
/* END U12_TPA.C ............................................................*/
|