📄 dmtxregion.c
字号:
} } // Need to move right, and previous step was right else { gapLength++; } prevStep = p0; p0.X += stepSize; } } matrixRegion->gapCount = gapCount; matrixRegion->highHit = highHitX; // Image coordinates dmtxMatrix3VMultiplyBy(&prevHit, sInv); dmtxMatrix3VMultiplyBy(&highHit, sInv); m = (highHit.Y - prevHit.Y)/(highHit.X - prevHit.X); matrixRegion->chain.by0 = (m * -prevHit.X) + prevHit.Y; matrixRegion->chain.by1 = (m * (100.0 - prevHit.X)) + prevHit.Y; if(matrixRegion->chain.by0 < 0 || matrixRegion->chain.by1 < 0) { return DMTX_FALSE; } MatrixRegionUpdateXfrms(matrixRegion); if(decode && decode->xfrmPlotPointCallback) { (*(decode->xfrmPlotPointCallback))(highHitX, sReg, 4, DMTX_DISPLAY_SQUARE); } return DMTX_TRUE;}/** * XXX * * @param * @return XXX */static intMatrixRegionAlignSide(DmtxMatrixRegion *matrixRegion, DmtxDecode *decode){ float t, m; DmtxVector2 p0, px0; DmtxColor3 color; DmtxMatrix3 s, sInv, sReg, sRegInv, m0, m1; DmtxVector2 prevHit, prevStep, highHit, highHitX; dmtxMatrix3LineSkewSide(m0, 100.0, 75.0, 100.0); dmtxMatrix3Scale(m1, 1.0, 1.25); dmtxMatrix3Multiply(s, m0, m1); dmtxMatrix3Multiply(sReg, matrixRegion->raw2fit, s); dmtxMatrix3LineSkewSideInv(m0, 100.0, 75.0, 100.0); dmtxMatrix3Scale(m1, 1.0, 0.8); dmtxMatrix3Multiply(sInv, m1, m0); dmtxMatrix3Multiply(sRegInv, sInv, matrixRegion->fit2raw);// if(decode && decode->buildMatrixCallback4)// (*(decode->buildMatrixCallback4))(sRegInv); p0.X = 100.0; p0.Y = 0.0; prevStep = prevHit = p0; highHit.X = highHit.Y = 100.0; // XXX add this for safety in case it's not found while(p0.Y < 100.0 && p0.X < 300.0) { // XXX 300.0 caps rise to prevent infinite loops// XXX infinite loop problem here... don't know why yet dmtxMatrix3VMultiply(&px0, &p0, sRegInv); dmtxColor3FromImage(&color, &(decode->image), px0.X, px0.Y); t = dmtxDistanceAlongRay3(&(matrixRegion->gradient.ray), &color); if(t >= matrixRegion->gradient.tMid) { prevStep = p0; p0.X += 0.1; } else { if(fabs(p0.X - prevStep.X) > DMTX_ALMOST_ZERO) { if(p0.Y - prevHit.Y < 3) { prevHit = p0;// if(decode && decode->xfrmPlotPointCallback)// (*(decode->xfrmPlotPointCallback))(px0, sReg, 5, DMTX_DISPLAY_POINT); } else { highHit = p0; highHitX = px0; // XXX note: capturing transformed point... not useful except for displaying } } prevStep = p0; p0.Y += 0.1; } } dmtxMatrix3VMultiplyBy(&prevHit, sInv); dmtxMatrix3VMultiplyBy(&highHit, sInv); m = (highHit.X - prevHit.X)/(highHit.Y - prevHit.Y); matrixRegion->chain.bx0 = (m * -prevHit.Y) + prevHit.X; matrixRegion->chain.bx1 = (m * (100.0 - prevHit.Y)) + prevHit.X; if(matrixRegion->chain.bx0 < 0 || matrixRegion->chain.bx1 < 0) { return DMTX_FALSE; } MatrixRegionUpdateXfrms(matrixRegion);// if(decode && decode->xfrmPlotPointCallback)// (*(decode->xfrmPlotPointCallback))(highHitX, sReg, 5, DMTX_DISPLAY_SQUARE); return DMTX_TRUE;}/** * XXX * * @param * @return XXX */static intMatrixRegionEstimateSize(DmtxMatrixRegion *matrixRegion, DmtxDecode *decode){ int matrixSize; DmtxVector2 p0; dmtxMatrix3VMultiply(&p0, &(matrixRegion->highHit), matrixRegion->raw2fit); matrixSize = 2 * (int)((matrixRegion->gapCount * 100)/p0.X + 0.5); // Round odd size estimates to next even number if(matrixSize & 0x01) matrixSize++; // Clamp size estimate to acceptable range if(matrixSize < 8) matrixSize = 8; else if(matrixSize > 144) matrixSize = 144; return matrixSize;}/** * XXX * * @param * @return XXX */static intMatrixRegionFindSize(DmtxMatrixRegion *matrixRegion, DmtxDecode *decode){ int i; int status = DMTX_FAILURE; int sizeIdx; int sizeEstimate; int adjust[] = { 0, -2, +2, -4, +4, -6, +6 }; int adjustAttempts = 7; int rowColAttempts[24]; sizeEstimate = MatrixRegionEstimateSize(matrixRegion, decode); memset(rowColAttempts, 0x00, sizeof(rowColAttempts)); assert(sizeof(symbolSize) / sizeof(int) == 24); for(i = 0; i < adjustAttempts; i++) { matrixRegion->dataRows = matrixRegion->dataCols = sizeEstimate - 2 + adjust[i]; if(matrixRegion->dataRows < 8 || matrixRegion->dataRows > 132) continue; status = PatternReadNonDataModules(matrixRegion, decode); if(status == DMTX_SUCCESS) break; } // If we try all adjustments and still can't get a good read then just give up if(status == DMTX_FAILURE) return status; // XXX bad read // Determine how many data words and error words will be stored in this size of grid // First loop determines matrix size and data/error counts for(sizeIdx = 0; sizeIdx < 9; sizeIdx++) { // XXX this will not work for structured append arrays if(symbolSize[sizeIdx] == matrixRegion->dataRows + 2) { matrixRegion->sizeIdx = sizeIdx; break; } } matrixRegion->arraySize = sizeof(unsigned char) * matrixRegion->dataRows * matrixRegion->dataCols; matrixRegion->array = (unsigned char *)malloc(matrixRegion->arraySize); memset(matrixRegion->array, 0x00, matrixRegion->arraySize); if(matrixRegion->array == NULL) { perror("Malloc failed"); exit(2); // XXX find better error handling here } matrixRegion->codeSize = sizeof(unsigned char) * dataWordLength[sizeIdx] + errorWordLength[sizeIdx]; matrixRegion->code = (unsigned char *)malloc(matrixRegion->codeSize); memset(matrixRegion->code, 0x00, matrixRegion->codeSize); if(matrixRegion->code == NULL) { perror("Malloc failed"); exit(2); // XXX find better error handling here } // XXX not sure if this is the right place or even the right approach. // Trying to allocate memory for the decoded data stream and will // initially assume that decoded data will not be larger than 2x encoded data matrixRegion->outputSize = sizeof(unsigned char) * matrixRegion->codeSize * 10; matrixRegion->output = (unsigned char *)malloc(matrixRegion->outputSize); memset(matrixRegion->output, 0x00, matrixRegion->outputSize); if(matrixRegion->output == NULL) { perror("Malloc failed"); exit(2); // XXX find better error handling here } return DMTX_SUCCESS; // XXX good read}/** * XXX * * @param * @return XXX */static DmtxColor3ReadModuleColor(DmtxMatrixRegion *matrixRegion, int row, int col, DmtxDecode *decode){ int i; double sampleX[] = { 1.5, 1.3, 1.5, 1.7, 1.5 }; double sampleY[] = { 1.5, 1.5, 1.3, 1.5, 1.7 }; DmtxVector2 p, p0; DmtxColor3 cPoint, cAverage; cAverage.R = cAverage.G = cAverage.B = 0.0; for(i = 0; i < 5; i++) { // The "+ 2" adjustment allows for the Finder and Calibration bars p.X = (100.0/(matrixRegion->dataCols + 2)) * (col + sampleX[i]); p.Y = (100.0/(matrixRegion->dataRows + 2)) * (row + sampleY[i]); dmtxMatrix3VMultiply(&p0, &p, matrixRegion->fit2raw); dmtxColor3FromImage(&cPoint, &(decode->image), p0.X, p0.Y); dmtxColor3AddTo(&cAverage, &cPoint); if(decode && decode->plotPointCallback) (*(decode->plotPointCallback))(p0, 1, 1, DMTX_DISPLAY_POINT); } dmtxColor3ScaleBy(&cAverage, 0.2); return cAverage;}/** * XXX * * @param * @return XXX */static intPatternReadNonDataModules(DmtxMatrixRegion *matrixRegion, DmtxDecode *decode){ int row, col; int errors = 0; float t, tRatio; DmtxColor3 color; for(row = matrixRegion->dataRows; row >= -1; row--) { for(col = matrixRegion->dataCols; col >= -1; col--) { if(row > -1 && row < matrixRegion->dataRows) { if(col > -1 && col < matrixRegion->dataCols) { continue; } } color = ReadModuleColor(matrixRegion, row, col, decode);// color = ReadModuleColor(matrixRegion, matrixRegion->dataRows - row - 1, col, decode); t = dmtxDistanceAlongRay3(&(matrixRegion->gradient.ray), &color); tRatio = (t - matrixRegion->gradient.tMin)/(matrixRegion->gradient.tMax - matrixRegion->gradient.tMin);// if(decode && decode->plotPointCallback)// (*(decode->plotPointCallback))(p0, 5, 1, DMTX_DISPLAY_POINT); if(decode && decode->plotModuleCallback) (*(decode->plotModuleCallback))(decode, matrixRegion, row, col, color); // Check that finder and calibration bar modules have correct colors if(col == -1 || row == -1) { if(tRatio < 0.5) {// fprintf(stdout, "Error: %g < 0.5\n", tRatio);// if(decode && decode->plotPointCallback)// (*(decode->plotPointCallback))(p0, 1, 1, DMTX_DISPLAY_SQUARE); errors++; } } else if(row == matrixRegion->dataRows || col == matrixRegion->dataCols) { if((row + col) % 2) { if(tRatio < 0.5) {// fprintf(stdout, "Error: %g < 0.5\n", tRatio);// if(decode && decode->plotPointCallback)// (*(decode->plotPointCallback))(p0, 1, 1, DMTX_DISPLAY_SQUARE); errors++; } } else { if(tRatio > 0.5) {// fprintf(stdout, "Error: %g > 0.5\n", tRatio);// if(decode && decode->plotPointCallback)// (*(decode->plotPointCallback))(p0, 1, 1, DMTX_DISPLAY_SQUARE); errors++; } } } if(errors > 4) return DMTX_FAILURE; // Too many errors detected } } return DMTX_SUCCESS;}/** * XXX * * @param * @return XXX */static intPopulateArrayFromImage(DmtxMatrixRegion *matrixRegion, DmtxDecode *decode){ int row, col; float t, tRatio; DmtxColor3 color; memset(matrixRegion->array, 0x00, matrixRegion->arraySize); for(row = 0; row < matrixRegion->dataRows; row++) { for(col = 0; col < matrixRegion->dataCols; col++) { // Swap row because the array's origin is top-left and everything else is bottom-left color = ReadModuleColor(matrixRegion, matrixRegion->dataRows - row - 1, col, decode); t = dmtxDistanceAlongRay3(&(matrixRegion->gradient.ray), &color); tRatio = (t - matrixRegion->gradient.tMin)/(matrixRegion->gradient.tMax - matrixRegion->gradient.tMin); // Value has been assigned, but not visited matrixRegion->array[row*matrixRegion->dataCols+col] |= (tRatio < 0.5) ? DMTX_MODULE_ON : DMTX_MODULE_OFF; matrixRegion->array[row*matrixRegion->dataCols+col] |= DMTX_MODULE_ASSIGNED; // Draw "ideal" barcode if(decode && decode->plotModuleCallback) (*(decode->plotModuleCallback))(decode, matrixRegion, matrixRegion->dataRows - row - 1, col, color); } } return DMTX_SUCCESS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -