Home | History | Annotate | Download | only in pdf417
      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 2013 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/BC_ResultPoint.h"
     25 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h"
     26 #include "BC_PDF417BoundingBox.h"
     27 CBC_BoundingBox::CBC_BoundingBox(CBC_CommonBitMatrix* image,
     28                                  CBC_ResultPoint* topLeft,
     29                                  CBC_ResultPoint* bottomLeft,
     30                                  CBC_ResultPoint* topRight,
     31                                  CBC_ResultPoint* bottomRight,
     32                                  int32_t& e) {
     33   if ((topLeft == NULL && topRight == NULL) ||
     34       (bottomLeft == NULL && bottomRight == NULL) ||
     35       (topLeft != NULL && bottomLeft == NULL) ||
     36       (topRight != NULL && bottomRight == NULL)) {
     37     e = BCExceptionNotFoundInstance;
     38   }
     39   init(image, topLeft, bottomLeft, topRight, bottomRight);
     40 }
     41 CBC_BoundingBox::CBC_BoundingBox(CBC_BoundingBox* boundingBox) {
     42   init(boundingBox->m_image, boundingBox->m_topLeft, boundingBox->m_bottomLeft,
     43        boundingBox->m_topRight, boundingBox->m_bottomRight);
     44 }
     45 CBC_BoundingBox::~CBC_BoundingBox() {
     46   if (m_topLeft) {
     47     delete m_topLeft;
     48   }
     49   if (m_bottomLeft) {
     50     delete m_bottomLeft;
     51   }
     52   if (m_topRight) {
     53     delete m_topRight;
     54   }
     55   if (m_bottomRight) {
     56     delete m_bottomRight;
     57   }
     58 }
     59 CBC_BoundingBox* CBC_BoundingBox::merge(CBC_BoundingBox* leftBox,
     60                                         CBC_BoundingBox* rightBox,
     61                                         int32_t& e) {
     62   CBC_BoundingBox* boundingBox = NULL;
     63   if (leftBox == NULL) {
     64     boundingBox = new CBC_BoundingBox(rightBox);
     65     return boundingBox;
     66   }
     67   if (rightBox == NULL) {
     68     boundingBox = new CBC_BoundingBox(leftBox);
     69     return boundingBox;
     70   }
     71   boundingBox = new CBC_BoundingBox(leftBox->m_image, leftBox->m_topLeft,
     72                                     leftBox->m_bottomLeft, rightBox->m_topRight,
     73                                     rightBox->m_bottomRight, e);
     74   BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
     75   return boundingBox;
     76 }
     77 CBC_BoundingBox* CBC_BoundingBox::addMissingRows(int32_t missingStartRows,
     78                                                  int32_t missingEndRows,
     79                                                  FX_BOOL isLeft,
     80                                                  int32_t& e) {
     81   CBC_ResultPoint* newTopLeft = m_topLeft;
     82   CBC_ResultPoint* newBottomLeft = m_bottomLeft;
     83   CBC_ResultPoint* newTopRight = m_topRight;
     84   CBC_ResultPoint* newBottomRight = m_bottomRight;
     85   CBC_ResultPoint* newTop = NULL;
     86   CBC_ResultPoint* newBottom = NULL;
     87   if (missingStartRows > 0) {
     88     CBC_ResultPoint* top = isLeft ? m_topLeft : m_topRight;
     89     int32_t newMinY = (int32_t)top->GetY() - missingStartRows;
     90     if (newMinY < 0) {
     91       newMinY = 0;
     92     }
     93     newTop = new CBC_ResultPoint((FX_FLOAT)top->GetX(), (FX_FLOAT)newMinY);
     94     if (isLeft) {
     95       newTopLeft = newTop;
     96     } else {
     97       newTopRight = newTop;
     98     }
     99   }
    100   if (missingEndRows > 0) {
    101     CBC_ResultPoint* bottom = isLeft ? m_bottomLeft : m_bottomRight;
    102     int32_t newMaxY = (int32_t)bottom->GetY() + missingEndRows;
    103     if (newMaxY >= m_image->GetHeight()) {
    104       newMaxY = m_image->GetHeight() - 1;
    105     }
    106     newBottom =
    107         new CBC_ResultPoint((FX_FLOAT)bottom->GetX(), (FX_FLOAT)newMaxY);
    108     if (isLeft) {
    109       newBottomLeft = newBottom;
    110     } else {
    111       newBottomRight = newBottom;
    112     }
    113   }
    114   calculateMinMaxValues();
    115   CBC_BoundingBox* boundingBox = new CBC_BoundingBox(
    116       m_image, newTopLeft, newBottomLeft, newTopRight, newBottomRight, e);
    117   delete newTop;
    118   delete newBottom;
    119   BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    120   return boundingBox;
    121 }
    122 void CBC_BoundingBox::setTopRight(CBC_ResultPoint topRight) {
    123   if (m_topRight) {
    124     delete m_topRight;
    125   }
    126   m_topRight = new CBC_ResultPoint(topRight.GetX(), topRight.GetY());
    127   calculateMinMaxValues();
    128 }
    129 void CBC_BoundingBox::setBottomRight(CBC_ResultPoint bottomRight) {
    130   if (m_bottomRight) {
    131     delete m_bottomRight;
    132   }
    133   m_bottomRight = new CBC_ResultPoint(bottomRight.GetX(), bottomRight.GetY());
    134   calculateMinMaxValues();
    135 }
    136 int32_t CBC_BoundingBox::getMinX() {
    137   return m_minX;
    138 }
    139 int32_t CBC_BoundingBox::getMaxX() {
    140   return m_maxX;
    141 }
    142 int32_t CBC_BoundingBox::getMinY() {
    143   return m_minY;
    144 }
    145 int32_t CBC_BoundingBox::getMaxY() {
    146   return m_maxY;
    147 }
    148 CBC_ResultPoint* CBC_BoundingBox::getTopLeft() {
    149   return m_topLeft;
    150 }
    151 CBC_ResultPoint* CBC_BoundingBox::getTopRight() {
    152   return m_topRight;
    153 }
    154 CBC_ResultPoint* CBC_BoundingBox::getBottomLeft() {
    155   return m_bottomLeft;
    156 }
    157 CBC_ResultPoint* CBC_BoundingBox::getBottomRight() {
    158   return m_bottomRight;
    159 }
    160 void CBC_BoundingBox::init(CBC_CommonBitMatrix* image,
    161                            CBC_ResultPoint* topLeft,
    162                            CBC_ResultPoint* bottomLeft,
    163                            CBC_ResultPoint* topRight,
    164                            CBC_ResultPoint* bottomRight) {
    165   m_topLeft = NULL;
    166   m_bottomLeft = NULL;
    167   m_topRight = NULL;
    168   m_bottomRight = NULL;
    169   m_image = image;
    170   if (topLeft) {
    171     m_topLeft = new CBC_ResultPoint(topLeft->GetX(), topLeft->GetY());
    172   }
    173   if (bottomLeft) {
    174     m_bottomLeft = new CBC_ResultPoint(bottomLeft->GetX(), bottomLeft->GetY());
    175   }
    176   if (topRight) {
    177     m_topRight = new CBC_ResultPoint(topRight->GetX(), topRight->GetY());
    178   }
    179   if (bottomRight) {
    180     m_bottomRight =
    181         new CBC_ResultPoint(bottomRight->GetX(), bottomRight->GetY());
    182   }
    183   calculateMinMaxValues();
    184 }
    185 void CBC_BoundingBox::calculateMinMaxValues() {
    186   if (m_topLeft == NULL) {
    187     m_topLeft = new CBC_ResultPoint(0, m_topRight->GetY());
    188     m_bottomLeft = new CBC_ResultPoint(0, m_bottomRight->GetY());
    189   } else if (m_topRight == NULL) {
    190     m_topRight = new CBC_ResultPoint((FX_FLOAT)m_image->GetWidth() - 1,
    191                                      (FX_FLOAT)m_topLeft->GetY());
    192     m_bottomRight = new CBC_ResultPoint((FX_FLOAT)m_image->GetWidth() - 1,
    193                                         (FX_FLOAT)m_bottomLeft->GetY());
    194   }
    195   m_minX = (int32_t)(m_topLeft->GetX() < m_bottomLeft->GetX()
    196                          ? m_topLeft->GetX()
    197                          : m_bottomLeft->GetX());
    198   m_maxX = (int32_t)(m_topRight->GetX() > m_bottomRight->GetX()
    199                          ? m_topRight->GetX()
    200                          : m_bottomRight->GetX());
    201   m_minY =
    202       (int32_t)(m_topLeft->GetY() < m_topRight->GetY() ? m_topLeft->GetY()
    203                                                        : m_topRight->GetY());
    204   m_maxY = (int32_t)(m_bottomLeft->GetY() > m_bottomRight->GetY()
    205                          ? m_bottomLeft->GetY()
    206                          : m_bottomRight->GetY());
    207 }
    208