00001
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #include "reader.h"
00062
00063
00064
00065
00067 #define NO_SAMPLE NUL
00068
00069 #define NO_CODE 0
00070
00071 #define OK_CODE 1
00072
00073 #define NO_DECODE 0
00074
00075 #define OK_DECODE 1
00076
00078 #define MAX_DECODE 32
00079
00080 #define StartCode '*'
00081
00082 #define StopCode '*'
00083
00085 #define QUIET_MULTIPLIER 10
00086
00087
00088
00089
00090 enum{STANDARD, SPECIAL};
00091
00092
00093
00094 unsigned short Decode39(struct BC_STRUCT *p);
00095 static unsigned short FindStart(struct BC_STRUCT *p, unsigned short reference);
00096 static unsigned short Decode(struct BC_STRUCT *p, unsigned short reference);
00097 static unsigned short DecodeChar(struct BC_STRUCT *p, unsigned short reference);
00098
00099
00106 unsigned short
00107 Decode39(struct BC_STRUCT *p)
00108 {
00109
00110 if(p->sample_ctr < 27){
00111 return 0;
00112 }
00113
00114
00115 p->sample_ptr = &p->sample_buffer[0];
00116 if(FindStart(p, STANDARD) != NO_CODE){
00117 if(Decode(p, STANDARD) != NO_DECODE){
00118 return p->decode_count;
00119 }
00120 }
00121
00122
00123 p->sample_ptr = &p->sample_buffer[0];
00124 if(FindStart(p, SPECIAL) != NO_CODE){
00125 if(Decode(p, SPECIAL) != NO_DECODE){
00126 return p->decode_count;
00127 }
00128 }
00129
00130
00131 return 0;
00132 }
00133
00134
00135
00136
00137
00138 static
00139 unsigned short
00140 Decode(struct BC_STRUCT *p, unsigned short reference)
00141 {
00142 unsigned char DecodeByte;
00143
00144
00145 p->decode_count = 0;
00146
00147
00148
00149 while((*p->sample_ptr++ != NO_SAMPLE) &&
00150 ((DecodeByte = DecodeChar(p, reference)) != NO_CODE)){
00151
00152
00153 if(DecodeByte == StopCode){
00154
00155
00156 if(p->decode_count >= MAX_DECODE){
00157 return NO_DECODE;
00158 }
00159
00160
00161 if(p->decode_count == 0){
00162 return NO_DECODE;
00163 }
00164
00165
00166 p->decode_buffer[p->decode_count] = NUL;
00167
00168
00169 if(*p->sample_ptr == 0){
00170 return 1;
00171 }
00172
00173
00174 else if(*(p->sample_ptr) >
00175 *(p->sample_ptr - 1) * QUIET_MULTIPLIER){
00176 return OK_DECODE;
00177 }
00178
00179
00180 else{
00181 return NO_DECODE;
00182 }
00183 }
00184
00185
00186 else{
00187 p->decode_buffer[p->decode_count++] = DecodeByte;
00188 }
00189 }
00190
00191
00192 return NO_DECODE;
00193 }
00194
00195
00196
00197
00198 static
00199 unsigned short
00200 DecodeChar(struct BC_STRUCT *p, reference)
00201 {
00202 static const unsigned char BarTable[4][25] = {
00203 {0,0,0,'7',0,'4','0',0,0,'2','9',0,'6',0,0,0,0,'1','8',0,'5',0,0,0,'3'},
00204 {0,0,0,'G',0,'D','J',0,0,'B','I',0,'F',0,0,0,0,'A','H',0,'E',0,0,0,'C'},
00205 {0,0,0,'Q',0,'N','T',0,0,'L','S',0,'P',0,0,0,0,'K','R',0,'O',0,0,0,'M'},
00206 {0,0,0,'-',0,'X','*',0,0,'V',' ',0,'Z',0,0,0,0,'U','.',0,'Y',0,0,0,'W'}
00207 };
00208
00209 unsigned short *TempPtr;
00210 unsigned int BarThreshold, SpaceThreshold;
00211 unsigned char Bars;
00212 unsigned char Spaces;
00213 unsigned short c;
00214
00215
00216 TempPtr = p->sample_ptr;
00217 BarThreshold = 0;
00218 SpaceThreshold = 0;
00219
00220
00221 for(c = 0; c < 9; c++){
00222
00223
00224 if((*TempPtr) == NO_SAMPLE){
00225 return NO_CODE;
00226 }
00227
00228
00229 if(reference == STANDARD){
00230 BarThreshold += *TempPtr++;
00231 }
00232
00233
00234 else if(reference == SPECIAL){
00235
00236
00237 if(c & 1){
00238 SpaceThreshold += *TempPtr++;
00239 }
00240
00241
00242 else{
00243 BarThreshold += *TempPtr++;
00244 }
00245 }
00246
00247
00248 else{
00249 return NO_CODE;
00250 }
00251 }
00252
00253 if(reference == STANDARD){
00254 BarThreshold /= 8;
00255 SpaceThreshold = BarThreshold;
00256 }
00257
00258 else if(reference == SPECIAL){
00259 BarThreshold /= 6;
00260 SpaceThreshold /= 5;
00261 }
00262
00263
00264 else{
00265 return NO_CODE;
00266 }
00267
00268
00269 Bars = 0;
00270 Spaces = 0;
00271 for(c = 0; c < 4; c++){
00272
00273 if((long)*p->sample_ptr > BarThreshold){
00274 Bars |= 1;
00275 }
00276 p->sample_ptr++;
00277 Bars <<= 1;
00278
00279 if((long)*p->sample_ptr > SpaceThreshold){
00280 Spaces |= 1;
00281 }
00282 p->sample_ptr++;
00283 Spaces <<= 1;
00284 }
00285
00286 if((long)*p->sample_ptr++ > BarThreshold){
00287 Bars |= 1;
00288 }
00289 Spaces >>= 1;
00290
00291
00292 if(Bars > 24){
00293 return NO_CODE;
00294 }
00295
00296 switch(Spaces){
00297 case 0x4:
00298 return BarTable[0][Bars];
00299
00300 case 0x2:
00301 return BarTable[1][Bars];
00302
00303 case 0x1:
00304 return BarTable[2][Bars];
00305
00306 case 0x8:
00307 return BarTable[3][Bars];
00308
00309 case 0xe:
00310 if(Bars == 0){
00311 return '$';
00312 }
00313 break;
00314
00315 case 0xd:
00316 if(Bars == 0){
00317 return '/';
00318 }
00319 break;
00320
00321 case 0xb:
00322 if(Bars == 0){
00323 return '+';
00324 }
00325 break;
00326
00327 case 0x7:
00328 if(Bars == 0){
00329 return '%';
00330 }
00331 break;
00332
00333 default:
00334 return NO_CODE;
00335 }
00336 return NO_CODE;
00337 }
00338
00339
00340
00341
00342
00343 static
00344 unsigned short
00345 FindStart(struct BC_STRUCT *p, unsigned short reference)
00346 {
00347 unsigned short AttemptCount;
00348 unsigned short TempCount;
00349 unsigned short *TempPtr;
00350
00351
00352 AttemptCount = 5;
00353 TempCount = p->sample_ctr;
00354 TempPtr = p->sample_ptr;
00355
00356
00357 while(AttemptCount != 0 && TempCount >= 27){
00358
00359
00360 if(DecodeChar(p, reference) == StartCode){
00361
00362
00363 if(TempPtr == &p->sample_buffer[0]){
00364 return OK_CODE;
00365 }
00366
00367
00368 else if(*(TempPtr - 1) > *(TempPtr) * QUIET_MULTIPLIER){
00369 return OK_CODE;
00370 }
00371 }
00372
00373
00374 AttemptCount--;
00375 TempCount--;
00376 TempPtr++;
00377 p->sample_ptr = TempPtr;
00378 }
00379
00380
00381 return NO_CODE;
00382 }