• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*.............................................................................
2  * Project : SANE library for Plustek flatbed scanners.
3  *.............................................................................
4  */
5 
6 /** @file plustek-usbmap.c
7  *  @brief Creating and manipulating lookup tables.
8  *
9  * Based on sources acquired from Plustek Inc.<br>
10  * Copyright (C) 2001-2007 Gerhard Jaeger <gerhard@gjaeger.de>
11  *
12  * History:
13  * - 0.40 - starting version of the USB support
14  * - 0.41 - fixed brightness problem for lineart mode
15  * - 0.42 - removed preset of linear gamma tables
16  * - 0.43 - no changes
17  * - 0.44 - map inversion for negatatives now only upon user request
18  * - 0.45 - no changes
19  * - 0.46 - no changes
20  * - 0.47 - cleanup work
21  * - 0.48 - added support for binary from color scans
22  * - 0.49 - changed usb_MapDownload
23  * - 0.50 - cleanup
24  * - 0.51 - no changes
25  * - 0.52 - no changes
26  * .
27  * <hr>
28  * This file is part of the SANE package.
29  *
30  * This program is free software; you can redistribute it and/or
31  * modify it under the terms of the GNU General Public License as
32  * published by the Free Software Foundation; either version 2 of the
33  * License, or (at your option) any later version.
34  *
35  * This program is distributed in the hope that it will be useful, but
36  * WITHOUT ANY WARRANTY; without even the implied warranty of
37  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
38  * General Public License for more details.
39  *
40  * You should have received a copy of the GNU General Public License
41  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
42  *
43  * As a special exception, the authors of SANE give permission for
44  * additional uses of the libraries contained in this release of SANE.
45  *
46  * The exception is that, if you link a SANE library with other files
47  * to produce an executable, this does not by itself cause the
48  * resulting executable to be covered by the GNU General Public
49  * License.  Your use of that executable is in no way restricted on
50  * account of linking the SANE library code into it.
51  *
52  * This exception does not, however, invalidate any other reasons why
53  * the executable file might be covered by the GNU General Public
54  * License.
55  *
56  * If you submit changes to SANE to the maintainers to be included in
57  * a subsequent release, you agree by submitting the changes that
58  * those changes may be distributed with this exception intact.
59  *
60  * If you write modifications of your own for SANE, it is your choice
61  * whether to permit this exception to apply to your modifications.
62  * If you do not wish that, delete this exception notice.
63  * <hr>
64  */
65 
66 #define _MAP_SIZE 4096
67 
68 static SANE_Byte a_bMap[_MAP_SIZE * 3];
69 
70 /** adjust according to brightness and contrast
71  */
usb_MapAdjust(Plustek_Device * dev)72 static void usb_MapAdjust( Plustek_Device *dev )
73 {
74 	int    i, tabLen;
75 	double b, c, tmp;
76 
77 	tabLen = _MAP_SIZE;
78 
79 	/* adjust brightness (b) and contrast (c) using the function:
80 	 *
81 	 * s(x,y) = (s(x,y) + b) * c
82 	 * b = [-127, 127]
83 	 * c = [0,2]
84 	 */
85 	b = ((double)dev->scanning.sParam.brightness * 192.0)/100.0;
86 	c = ((double)dev->scanning.sParam.contrast   + 100.0)/100.0;
87 
88 	DBG( _DBG_INFO, "* brightness = %i -> %i\n",
89 	                dev->scanning.sParam.brightness, (u_char)b);
90 	DBG( _DBG_INFO, "* contrast   = %i -> %.3f\n",
91 	                dev->scanning.sParam.contrast, c);
92 
93 	if( dev->scanning.sParam.brightness == 0 && dev->scanning.sParam.contrast )
94 		return;
95 
96 	for( i = 0; i < tabLen; i++ ) {
97 
98 		tmp = ((double)(a_bMap[i] + b)) * c;
99 		if( tmp < 0 )   tmp = 0;
100 		if( tmp > 255 ) tmp = 255;
101 		a_bMap[i] = (u_char)tmp;
102 
103 		tmp = ((double)(a_bMap[tabLen+i] + b)) * c;
104 		if( tmp < 0 )   tmp = 0;
105 		if( tmp > 255 ) tmp = 255;
106 		a_bMap[tabLen+i] = (u_char)tmp;
107 
108 		tmp = ((double)(a_bMap[tabLen*2+i] + b)) * c;
109 		if( tmp < 0 )   tmp = 0;
110 		if( tmp > 255 ) tmp = 255;
111 		a_bMap[tabLen*2+i] = (u_char)tmp;
112 	}
113 }
114 
115 /**
116  */
usb_MapDownload(Plustek_Device * dev)117 static SANE_Bool usb_MapDownload( Plustek_Device *dev )
118 {
119 	ScanDef  *scanning = &dev->scanning;
120 	DCapsDef *sc       = &dev->usbDev.Caps;
121 
122 	int       color;
123 	int       i, threshold;
124 	SANE_Byte value;
125 	SANE_Bool fInverse = 0;
126 
127 	DBG( _DBG_INFO, "usb_MapDownload()\n" );
128 
129 	/* the maps are have been already set */
130 
131 	/* do the brightness and contrast adjustment ... */
132 	if( scanning->sParam.bDataType != SCANDATATYPE_BW )
133 		usb_MapAdjust( dev );
134 
135 	if( !usbio_WriteReg( dev->fd, 7, 0))
136 		return SANE_FALSE;
137 
138 	/* we download all the time all three color maps, as we run
139 	 * into trouble elsewhere on CanoScan models using gray mode
140 	 */
141 	for( color = 0; color < 3; color++) {
142 
143 		/* select color */
144 		value = (color << 2)+2;
145 
146 		/* set gamma color selector */
147 		usbio_WriteReg( dev->fd, 0x03, value );
148 		usbio_WriteReg( dev->fd, 0x04, 0 );
149 		usbio_WriteReg( dev->fd, 0x05, 0 );
150 
151 		/* write the gamma table entry to merlin */
152 		if((scanning->sParam.bDataType == SCANDATATYPE_BW) ||
153 		   (scanning->fGrayFromColor > 7 )) {
154 
155 			threshold = (int)((double)scanning->sParam.brightness *
156 			                                (_MAP_SIZE/200.0)) + (_MAP_SIZE/2);
157 			threshold = _MAP_SIZE - threshold;
158 			if(threshold < 0)
159 				threshold = 0;
160 			if(threshold > (int)_MAP_SIZE)
161 				threshold = _MAP_SIZE;
162 
163 			DBG(_DBG_INFO, "* Threshold is at %u brightness=%i\n",
164 			               threshold, scanning->sParam.brightness );
165 
166 			for(i = 0; i < threshold; i++)
167 				a_bMap[color*_MAP_SIZE + i] = 0;
168 
169 			for(i = threshold; i < _MAP_SIZE; i++)
170 				a_bMap[color*_MAP_SIZE + i] = 255;
171 
172 			fInverse = 1;
173 
174 		} else {
175 			fInverse = 0;
176 		}
177 
178 		if( /*scanning->dwFlag & SCANFLAG_Pseudo48 && */
179 			(scanning->sParam.bSource == SOURCE_Negative) &&
180 			(sc->workaroundFlag &_WAF_INV_NEGATIVE_MAP)) {
181 			fInverse ^= 1;
182 		}
183 
184 		if( fInverse ) {
185 
186 			u_char  map[_MAP_SIZE];
187 			u_char *pMap = a_bMap+color*_MAP_SIZE;
188 
189 			DBG( _DBG_INFO, "* Inverting Map\n" );
190 
191 			for( i = 0; i < _MAP_SIZE; i++, pMap++ )
192 				map[i] = ~*pMap;
193 
194 			sanei_lm983x_write( dev->fd,  0x06, map, _MAP_SIZE, SANE_FALSE );
195 
196 		} else {
197 			DBG( _DBG_INFO, "* downloading map %u...\n", color );
198 			sanei_lm983x_write( dev->fd, 0x06, a_bMap+color*_MAP_SIZE,
199 			                    _MAP_SIZE, SANE_FALSE );
200 		}
201 
202 	} /* for each color */
203 
204 	DBG( _DBG_INFO, "usb_MapDownload() done.\n" );
205 	return SANE_TRUE;
206 }
207 
208 /* END PLUSTEK-USBMAP.C .....................................................*/
209