Home | History | Annotate | Download | only in qrcode
      1 // Copyright 2014 PDFium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 // Original code is licensed as follows:
      7 /*
      8  * Copyright 2007 ZXing authors
      9  *
     10  * Licensed under the Apache License, Version 2.0 (the "License");
     11  * you may not use this file except in compliance with the License.
     12  * You may obtain a copy of the License at
     13  *
     14  *      http://www.apache.org/licenses/LICENSE-2.0
     15  *
     16  * Unless required by applicable law or agreed to in writing, software
     17  * distributed under the License is distributed on an "AS IS" BASIS,
     18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     19  * See the License for the specific language governing permissions and
     20  * limitations under the License.
     21  */
     22 
     23 #include "xfa/src/fxbarcode/barcode.h"
     24 #include "xfa/src/fxbarcode/common/BC_CommonPerspectiveTransform.h"
     25 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h"
     26 #include "BC_QRGridSampler.h"
     27 CBC_QRGridSampler CBC_QRGridSampler::m_gridSampler;
     28 CBC_QRGridSampler::CBC_QRGridSampler() {}
     29 CBC_QRGridSampler::~CBC_QRGridSampler() {}
     30 CBC_QRGridSampler& CBC_QRGridSampler::GetInstance() {
     31   return m_gridSampler;
     32 }
     33 void CBC_QRGridSampler::CheckAndNudgePoints(CBC_CommonBitMatrix* image,
     34                                             CFX_FloatArray* points,
     35                                             int32_t& e) {
     36   int32_t width = image->GetWidth();
     37   int32_t height = image->GetHeight();
     38   FX_BOOL nudged = TRUE;
     39   int32_t offset;
     40   for (offset = 0; offset < points->GetSize() && nudged; offset += 2) {
     41     int32_t x = (int32_t)(*points)[offset];
     42     int32_t y = (int32_t)(*points)[offset + 1];
     43     if (x < -1 || x > width || y < -1 || y > height) {
     44       e = BCExceptionRead;
     45       BC_EXCEPTION_CHECK_ReturnVoid(e);
     46     }
     47     nudged = FALSE;
     48     if (x == -1) {
     49       (*points)[offset] = 0.0f;
     50       nudged = TRUE;
     51     } else if (x == width) {
     52       (*points)[offset] = (FX_FLOAT)(width - 1);
     53       nudged = TRUE;
     54     }
     55     if (y == -1) {
     56       (*points)[offset + 1] = 0.0f;
     57       nudged = TRUE;
     58     } else if (y == height) {
     59       (*points)[offset + 1] = (FX_FLOAT)(height - 1);
     60       nudged = TRUE;
     61     }
     62   }
     63   nudged = TRUE;
     64   for (offset = (*points).GetSize() - 2; offset >= 0 && nudged; offset -= 2) {
     65     int32_t x = (int32_t)(*points)[offset];
     66     int32_t y = (int32_t)(*points)[offset + 1];
     67     if (x < -1 || x > width || y < -1 || y > height) {
     68       e = BCExceptionRead;
     69       BC_EXCEPTION_CHECK_ReturnVoid(e);
     70     }
     71     nudged = FALSE;
     72     if (x == -1) {
     73       (*points)[offset] = 0.0f;
     74       nudged = TRUE;
     75     } else if (x == width) {
     76       (*points)[offset] = (FX_FLOAT)(width - 1);
     77       nudged = TRUE;
     78     }
     79     if (y == -1) {
     80       (*points)[offset + 1] = 0.0f;
     81       nudged = TRUE;
     82     } else if (y == height) {
     83       (*points)[offset + 1] = (FX_FLOAT)(height - 1);
     84       nudged = TRUE;
     85     }
     86   }
     87 }
     88 CBC_CommonBitMatrix* CBC_QRGridSampler::SampleGrid(CBC_CommonBitMatrix* image,
     89                                                    int32_t dimensionX,
     90                                                    int32_t dimensionY,
     91                                                    FX_FLOAT p1ToX,
     92                                                    FX_FLOAT p1ToY,
     93                                                    FX_FLOAT p2ToX,
     94                                                    FX_FLOAT p2ToY,
     95                                                    FX_FLOAT p3ToX,
     96                                                    FX_FLOAT p3ToY,
     97                                                    FX_FLOAT p4ToX,
     98                                                    FX_FLOAT p4ToY,
     99                                                    FX_FLOAT p1FromX,
    100                                                    FX_FLOAT p1FromY,
    101                                                    FX_FLOAT p2FromX,
    102                                                    FX_FLOAT p2FromY,
    103                                                    FX_FLOAT p3FromX,
    104                                                    FX_FLOAT p3FromY,
    105                                                    FX_FLOAT p4FromX,
    106                                                    FX_FLOAT p4FromY,
    107                                                    int32_t& e) {
    108   CBC_AutoPtr<CBC_CommonPerspectiveTransform> transform(
    109       CBC_CommonPerspectiveTransform::QuadrilateralToQuadrilateral(
    110           p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX,
    111           p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY));
    112   CBC_CommonBitMatrix* tempBitM = new CBC_CommonBitMatrix();
    113   tempBitM->Init(dimensionX, dimensionY);
    114   CBC_AutoPtr<CBC_CommonBitMatrix> bits(tempBitM);
    115   CFX_FloatArray points;
    116   points.SetSize(dimensionX << 1);
    117   for (int32_t y = 0; y < dimensionY; y++) {
    118     int32_t max = points.GetSize();
    119     FX_FLOAT iValue = (FX_FLOAT)(y + 0.5f);
    120     int32_t x;
    121     for (x = 0; x < max; x += 2) {
    122       points[x] = (FX_FLOAT)((x >> 1) + 0.5f);
    123       points[x + 1] = iValue;
    124     }
    125     transform->TransformPoints(&points);
    126     CheckAndNudgePoints(image, &points, e);
    127     BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    128     for (x = 0; x < max; x += 2) {
    129       if (image->Get((int32_t)points[x], (int32_t)points[x + 1])) {
    130         bits->Set(x >> 1, y);
    131       }
    132     }
    133   }
    134   return bits.release();
    135 }
    136