Home | History | Annotate | Download | only in fxbarcode
      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 
      7 #include <algorithm>
      8 
      9 #include "third_party/base/numerics/safe_math.h"
     10 #include "xfa/src/fxbarcode/barcode.h"
     11 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h"
     12 #include "xfa/src/fxbarcode/BC_Writer.h"
     13 #include "xfa/src/fxbarcode/BC_TwoDimWriter.h"
     14 
     15 CBC_TwoDimWriter::CBC_TwoDimWriter() {
     16   m_iCorrectLevel = 1;
     17   m_bFixedSize = TRUE;
     18   m_output = NULL;
     19 }
     20 CBC_TwoDimWriter::~CBC_TwoDimWriter() {
     21   if (m_output != NULL) {
     22     delete m_output;
     23     m_output = NULL;
     24   }
     25 }
     26 void CBC_TwoDimWriter::RenderDeviceResult(CFX_RenderDevice* device,
     27                                           const CFX_Matrix* matrix) {
     28   CFX_GraphStateData stateData;
     29   CFX_PathData path;
     30   path.AppendRect(0, 0, (FX_FLOAT)m_Width, (FX_FLOAT)m_Height);
     31   device->DrawPath(&path, matrix, &stateData, m_backgroundColor,
     32                    m_backgroundColor, FXFILL_ALTERNATE);
     33   int32_t leftPos = 0;
     34   int32_t topPos = 0;
     35   if (m_bFixedSize) {
     36     leftPos = (m_Width - m_output->GetWidth()) / 2;
     37     topPos = (m_Height - m_output->GetHeight()) / 2;
     38   }
     39   CFX_Matrix matri = *matrix;
     40   if (m_Width < m_output->GetWidth() && m_Height < m_output->GetHeight()) {
     41     CFX_Matrix matriScale(
     42         (FX_FLOAT)m_Width / (FX_FLOAT)m_output->GetWidth(), 0.0, 0.0,
     43         (FX_FLOAT)m_Height / (FX_FLOAT)m_output->GetHeight(), 0.0, 0.0);
     44     matriScale.Concat(*matrix);
     45     matri = matriScale;
     46   }
     47   for (int32_t x = 0; x < m_output->GetWidth(); x++) {
     48     for (int32_t y = 0; y < m_output->GetHeight(); y++) {
     49       CFX_PathData rect;
     50       rect.AppendRect((FX_FLOAT)leftPos + x, (FX_FLOAT)topPos + y,
     51                       (FX_FLOAT)(leftPos + x + 1), (FX_FLOAT)(topPos + y + 1));
     52       CFX_GraphStateData stateData;
     53       if (m_output->Get(x, y)) {
     54         device->DrawPath(&rect, &matri, &stateData, m_barColor, 0,
     55                          FXFILL_WINDING);
     56       }
     57     }
     58   }
     59 }
     60 void CBC_TwoDimWriter::RenderBitmapResult(CFX_DIBitmap*& pOutBitmap,
     61                                           int32_t& e) {
     62   if (m_bFixedSize) {
     63     pOutBitmap = CreateDIBitmap(m_Width, m_Height);
     64   } else {
     65     pOutBitmap = CreateDIBitmap(m_output->GetWidth(), m_output->GetHeight());
     66   }
     67   if (!pOutBitmap) {
     68     e = BCExceptionFailToCreateBitmap;
     69     return;
     70   }
     71   pOutBitmap->Clear(m_backgroundColor);
     72   int32_t leftPos = 0;
     73   int32_t topPos = 0;
     74   if (m_bFixedSize) {
     75     leftPos = (m_Width - m_output->GetWidth()) / 2;
     76     topPos = (m_Height - m_output->GetHeight()) / 2;
     77   }
     78   for (int32_t x = 0; x < m_output->GetWidth(); x++) {
     79     for (int32_t y = 0; y < m_output->GetHeight(); y++) {
     80       if (m_output->Get(x, y)) {
     81         pOutBitmap->SetPixel(leftPos + x, topPos + y, m_barColor);
     82       }
     83     }
     84   }
     85   if (!m_bFixedSize) {
     86     CFX_DIBitmap* pStretchBitmap = pOutBitmap->StretchTo(m_Width, m_Height);
     87     if (pOutBitmap) {
     88       delete pOutBitmap;
     89     }
     90     pOutBitmap = pStretchBitmap;
     91   }
     92 }
     93 void CBC_TwoDimWriter::RenderResult(uint8_t* code,
     94                                     int32_t codeWidth,
     95                                     int32_t codeHeight,
     96                                     int32_t& e) {
     97   int32_t inputWidth = codeWidth;
     98   int32_t inputHeight = codeHeight;
     99   int32_t tempWidth = inputWidth + 2;
    100   int32_t tempHeight = inputHeight + 2;
    101   FX_FLOAT moduleHSize = std::min(m_ModuleWidth, m_ModuleHeight);
    102   moduleHSize = std::min(moduleHSize, 8.0f);
    103   moduleHSize = std::max(moduleHSize, 1.0f);
    104   pdfium::base::CheckedNumeric<int32_t> scaledWidth = tempWidth;
    105   pdfium::base::CheckedNumeric<int32_t> scaledHeight = tempHeight;
    106   scaledWidth *= moduleHSize;
    107   scaledHeight *= moduleHSize;
    108 
    109   int32_t outputWidth = scaledWidth.ValueOrDie();
    110   int32_t outputHeight = scaledHeight.ValueOrDie();
    111   if (m_bFixedSize) {
    112     if (m_Width < outputWidth || m_Height < outputHeight) {
    113       e = BCExceptionBitmapSizeError;
    114       return;
    115     }
    116   } else {
    117     if (m_Width > outputWidth || m_Height > outputHeight) {
    118       outputWidth = (int32_t)(outputWidth *
    119                               ceil((FX_FLOAT)m_Width / (FX_FLOAT)outputWidth));
    120       outputHeight = (int32_t)(
    121           outputHeight * ceil((FX_FLOAT)m_Height / (FX_FLOAT)outputHeight));
    122     }
    123   }
    124   int32_t multiX = (int32_t)ceil((FX_FLOAT)outputWidth / (FX_FLOAT)tempWidth);
    125   int32_t multiY = (int32_t)ceil((FX_FLOAT)outputHeight / (FX_FLOAT)tempHeight);
    126   if (m_bFixedSize) {
    127     multiX = std::min(multiX, multiY);
    128     multiY = multiX;
    129   }
    130   int32_t leftPadding = (outputWidth - (inputWidth * multiX)) / 2;
    131   int32_t topPadding = (outputHeight - (inputHeight * multiY)) / 2;
    132   if (leftPadding < 0) {
    133     leftPadding = 0;
    134   }
    135   if (topPadding < 0) {
    136     topPadding = 0;
    137   }
    138   m_output = new CBC_CommonBitMatrix;
    139   m_output->Init(outputWidth, outputHeight);
    140   for (int32_t inputY = 0, outputY = topPadding;
    141        (inputY < inputHeight) && (outputY < outputHeight - multiY);
    142        inputY++, outputY += multiY) {
    143     for (int32_t inputX = 0, outputX = leftPadding;
    144          (inputX < inputWidth) && (outputX < outputWidth - multiX);
    145          inputX++, outputX += multiX) {
    146       if (code[inputX + inputY * inputWidth] == 1) {
    147         m_output->SetRegion(outputX, outputY, multiX, multiY, e);
    148         BC_EXCEPTION_CHECK_ReturnVoid(e);
    149       }
    150     }
    151   }
    152 }
    153