int evaluate()

in Sharing/Src/External/zint-2.4.3/src/backend/qr.c [764:1016]


int evaluate(unsigned char *grid, int size, int pattern)
{ 
	int x, y, block;
	int result = 0;
	int result_b = 0;
	char state;
	int p;
	int dark_mods; 
	int percentage, k, k2;

#ifndef _MSC_VER
	char local[size * size];
#else
	char* local = (char *)_alloca((size * size) * sizeof(char));
#endif


#ifdef ZINTLOG 
	char str[15];
	write_log("");
	sprintf(str, "%d", pattern);
	write_log(str);
#endif

	// all eight bitmask variants have been encoded in the 8 bits of the bytes that make up the grid array. select them for evaluation according to the desired pattern.
	for(x = 0; x < size; x++) {
		for(y = 0; y < size; y++) {
			switch(pattern) {
				case 0: if (grid[(y * size) + x] & 0x01) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break;
				case 1: if (grid[(y * size) + x] & 0x02) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break;
				case 2: if (grid[(y * size) + x] & 0x04) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break;
				case 3: if (grid[(y * size) + x] & 0x08) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break;
				case 4: if (grid[(y * size) + x] & 0x10) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break;
				case 5: if (grid[(y * size) + x] & 0x20) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break;
				case 6: if (grid[(y * size) + x] & 0x40) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break;
				case 7: if (grid[(y * size) + x] & 0x80) { local[(y * size) + x] = '1'; } else { local[(y * size) + x] = '0'; } break;
			}
		}
	}

#ifdef ZINTLOG 	 
	//bitmask output
	for(y = 0; y < size; y++) { 
		strcpy (str, "");
		for(x = 0; x < size; x++) {  
			state =local[(y * size) + x];  
			append_log(state);
		} 
		write_log("");		
	}
	write_log("");
#endif

	/* Test 1: Adjacent modules in row/column in same colour */
	/* Vertical */ 
	for(x = 0; x < size; x++) {
		state = local[x];
		block = 0; 
		for(y = 0; y < size; y++) {
			if(local[(y * size) + x] == state) {
				block++;				
				if(block ==5)
					result += 3;
				
				if(block>5)				
					result +=1;
			} else {
				block=0;
			}
		} 
	}

	/* Horizontal */
	for(y = 0; y < size; y++) {
		state = local[y * size];
		block = 0;
		for(x = 0; x < size; x++) {
			if(local[(y * size) + x] == state) {
				block++;				
				if(block ==5)
					result += 3;
				
				if(block>5)				
					result +=1;
			} else {
				block=0;
			}
		} 
	}
	 
#ifdef ZINTLOG 
	/* output Test 1 */
	sprintf(str, "%d", result);
	result_b=result;
	write_log(str);
#endif
	
	/* Test 2 fd02131114 */
	for(x = 0; x < size-1; x++) {
		for(y = 0; y < (size - 7) -1; y++) {	
			// y + 1???		
			if((local[((y + 1) * size) + x] == '1') && 
				(local[((y + 1) * size) + x+1] == '1') &&
				(local[(((y + 1)+1) * size) + x] == '1') &&
				(local[(((y + 1)+1) * size) + x+1] == '1') 
				) { result += 3; }		 

			if((local[((y + 1) * size) + x] == '0') && 
				(local[((y + 1) * size) + x+1] == '0') &&
				(local[(((y + 1)+1) * size) + x] == '0') &&
				(local[(((y + 1)+1) * size) + x+1] == '0') 
				) { result += 3; }		
		}
	}

#ifdef ZINTLOG 
	/* output Test 2 */
	sprintf(str, "%d", result-result_b);
	result_b=result;
	write_log(str);
#endif

	/* Test 3: fd02131114 */ 
	/*pattern 10111010000 */ 
	/* Vertical */ 
	for(x = 0; x < size; x++) {
		for(y = 0; y < (size - 11); y++) {
			p = 0;
			if(local[(y * size) + x] == '1') { p += 1; }
			if(local[((y + 1) * size) + x] == '0') { p += 1; }
			if(local[((y + 2) * size) + x] == '1') { p += 1; }
			if(local[((y + 3) * size) + x] == '1') { p += 1; }
			if(local[((y + 4) * size) + x] == '1') { p += 1; }
			if(local[((y + 5) * size) + x] == '0') { p += 1; }
			if(local[((y + 6) * size) + x] == '1') { p += 1; }
			if(local[((y + 7) * size) + x] == '0') { p += 1; }
			if(local[((y + 8) * size) + x] == '0') { p += 1; }
			if(local[((y + 9) * size) + x] == '0') { p += 1; }
			if(local[((y + 10) * size) + x] == '0') { p += 1; } 		
			if(p == 11) {
				result += 40;
			}
		}
	}

	/* Horizontal */
	for(y = 0; y < size; y++) {
		for(x = 0; x < (size - 11); x++) {
			p = 0;
			if(local[(y * size) + x] == '1') { p += 1; }
			if(local[(y * size) + x + 1] == '0') { p += 1; }
			if(local[(y * size) + x + 2] == '1') { p += 1; }
			if(local[(y * size) + x + 3] == '1') { p += 1; }
			if(local[(y * size) + x + 4] == '1') { p += 1; }
			if(local[(y * size) + x + 5] == '0') { p += 1; }
			if(local[(y * size) + x + 6] == '1') { p += 1; }
			if(local[(y * size) + x + 7] == '0') { p += 1; }
			if(local[(y * size) + x + 8] == '0') { p += 1; }
			if(local[(y * size) + x + 9] == '0') { p += 1; }
			if(local[(y * size) + x + 10] == '0') { p += 1; }
			if(p == 11) {
				result += 40;
			}
		}
	}

	/*pattern 00001011101 */ 
	/* Vertical */ 
	for(x = 0; x < size; x++) {
		for(y = 0; y < (size - 11); y++) {
			p = 0;
			if(local[(y * size) + x] == '0') { p += 1; }
			if(local[((y + 1) * size) + x] == '0') { p += 1; }
			if(local[((y + 2) * size) + x] == '0') { p += 1; }
			if(local[((y + 3) * size) + x] == '0') { p += 1; }
			if(local[((y + 4) * size) + x] == '1') { p += 1; }
			if(local[((y + 5) * size) + x] == '0') { p += 1; }
			if(local[((y + 6) * size) + x] == '1') { p += 1; }
			if(local[((y + 7) * size) + x] == '1') { p += 1; }
			if(local[((y + 8) * size) + x] == '1') { p += 1; }
			if(local[((y + 9) * size) + x] == '0') { p += 1; }
			if(local[((y + 10) * size) + x] == '1') { p += 1; } 		
			if(p == 11) {
				result += 40;
			}
		}
	}

	/* Horizontal */
	for(y = 0; y < size; y++) {
		for(x = 0; x < (size - 11); x++) {
			p = 0;
			if(local[(y * size) + x] == '0') { p += 1; }
			if(local[(y * size) + x + 1] == '0') { p += 1; }
			if(local[(y * size) + x + 2] == '0') { p += 1; }
			if(local[(y * size) + x + 3] == '0') { p += 1; }
			if(local[(y * size) + x + 4] == '1') { p += 1; }
			if(local[(y * size) + x + 5] == '0') { p += 1; }
			if(local[(y * size) + x + 6] == '1') { p += 1; }
			if(local[(y * size) + x + 7] == '1') { p += 1; }
			if(local[(y * size) + x + 8] == '1') { p += 1; }
			if(local[(y * size) + x + 9] == '0') { p += 1; }
			if(local[(y * size) + x + 10] == '1') { p += 1; }
			if(p == 11) {
				result += 40;
			}
		}
	} 

#ifdef ZINTLOG 
	/* output Test 3 */
	sprintf(str, "%d", result-result_b);
	result_b=result;
	write_log(str);
#endif

	/* Test 4: Proportion of dark modules in entire symbol */
	dark_mods = 0; 
	for(x = 0; x < size; x++) {
		for(y = 0; y < size; y++) {
			if(local[(y * size) + x] == '1') {
				dark_mods++;
			} 
		}
	}
	percentage = 100 * (dark_mods / (size * size));   
	int m=0;
	for(x = 0; x < 100; x+=5) {
		if(x<percentage)
			m=x;
	}

		
	k=abs((m-50)/5);
	k2=abs((m+5-50)/5);

	int smallest=k;
	if(k2<smallest)
		smallest=k2;
	 
	result += 10 * smallest;

#ifdef ZINTLOG 
	/* output Test 4+summary */
	sprintf(str, "%d", result-result_b); 
	write_log(str); 
	write_log("==========");
	sprintf(str, "%d", result); 
	write_log(str);
#endif 

	return result;
}