summaryrefslogtreecommitdiff
path: root/xfa/src/fxbarcode/BC_BufferedImageLuminanceSource.cpp
blob: 01c4f6c1fdb9ea1babf84224ed992c24bcd89de5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
// Original code is licensed as follows:
/*
 * Copyright 2009 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "barcode.h"
#include "BC_LuminanceSource.h"
#include "BC_BufferedImageLuminanceSource.h"
class CBC_Pause : public IFX_Pause {
 public:
  virtual FX_BOOL NeedToPauseNow() { return TRUE; }
};
static CFX_DIBitmap* CreateDIBSource(IFX_FileRead* fileread) {
  CFX_DIBitmap* bitmap = NULL;
  CCodec_ModuleMgr* pCodecMgr = NULL;
  ICodec_ProgressiveDecoder* pImageCodec = NULL;
  pCodecMgr = new CCodec_ModuleMgr();
  pImageCodec = pCodecMgr->CreateProgressiveDecoder();
  FXCODEC_STATUS status = FXCODEC_STATUS_DECODE_FINISH;
  status = pImageCodec->LoadImageInfo(fileread, FXCODEC_IMAGE_UNKNOWN, nullptr);
  if (status != FXCODEC_STATUS_FRAME_READY) {
    return NULL;
  }
  bitmap = new CFX_DIBitmap;
  bitmap->Create(pImageCodec->GetWidth(), pImageCodec->GetHeight(), FXDIB_Argb);
  bitmap->Clear(FXARGB_MAKE(0xFF, 0xFF, 0xFF, 0xFF));
  CBC_Pause pause;
  int32_t frames;
  status = pImageCodec->GetFrames(frames, &pause);
  while (status == FXCODEC_STATUS_FRAME_TOBECONTINUE) {
    status = pImageCodec->GetFrames(frames, &pause);
  }
  if (status != FXCODEC_STATUS_DECODE_READY) {
    goto except;
  }
  status = pImageCodec->StartDecode(bitmap, 0, 0, bitmap->GetWidth(),
                                    bitmap->GetHeight(), 0, FALSE);
  if (status == FXCODEC_STATUS_ERR_PARAMS) {
    goto except;
  }
  if (status != FXCODEC_STATUS_DECODE_TOBECONTINUE) {
    goto except;
  }
  while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) {
    status = pImageCodec->ContinueDecode(&pause);
  }
  if (status != FXCODEC_STATUS_DECODE_FINISH) {
    goto except;
  }
  if (pImageCodec) {
    delete pImageCodec;
    pImageCodec = NULL;
  }
  delete pCodecMgr;
  pCodecMgr = NULL;
  return bitmap;
except:
  if (pImageCodec) {
    delete pImageCodec;
    pImageCodec = NULL;
  }
  delete pCodecMgr;
  pCodecMgr = NULL;
  if (bitmap) {
    delete bitmap;
  }
  return NULL;
}
CBC_BufferedImageLuminanceSource::CBC_BufferedImageLuminanceSource(
    const CFX_WideString& filename)
    : CBC_LuminanceSource(0, 0), m_filename(filename) {
  m_height = 0;
  m_width = 0;
  m_bytesPerLine = 0;
  m_top = 0;
  m_left = 0;
}
void CBC_BufferedImageLuminanceSource::Init(int32_t& e) {
  IFX_FileRead* fileread = FX_CreateFileRead(m_filename);
  m_pBitmap = CreateDIBSource(fileread);
  if (m_pBitmap == NULL) {
    e = BCExceptionLoadFile;
    return;
  }
  m_pBitmap->ConvertFormat(FXDIB_Argb);
  m_height = m_pBitmap->GetHeight();
  m_width = m_pBitmap->GetWidth();
  m_rgbData.SetSize(m_height * m_width);
  m_bytesPerLine = m_width * 4;
  m_top = 0;
  m_left = 0;
}
CBC_BufferedImageLuminanceSource::CBC_BufferedImageLuminanceSource(
    CFX_DIBitmap* pBitmap)
    : CBC_LuminanceSource(0, 0) {
  m_pBitmap = pBitmap->Clone();
  m_pBitmap->ConvertFormat(FXDIB_Argb);
  m_height = m_pBitmap->GetHeight();
  m_width = m_pBitmap->GetWidth();
  m_rgbData.SetSize(m_height * m_width);
  m_bytesPerLine = m_width * 4;
  m_top = 0;
  m_left = 0;
}
CBC_BufferedImageLuminanceSource::~CBC_BufferedImageLuminanceSource() {
  delete m_pBitmap;
  m_pBitmap = NULL;
}
CFX_ByteArray* CBC_BufferedImageLuminanceSource::GetRow(int32_t y,
                                                        CFX_ByteArray& row,
                                                        int32_t& e) {
  if (y < 0 || y >= m_height) {
    e = BCExceptionRequestedRowIsOutSizeTheImage;
    return NULL;
  }
  int32_t width = m_width;
  if (row.GetSize() == 0 || row.GetSize() < width) {
    row.SetSize(width);
  }
  if (m_rgbData.GetSize() == 0 || m_rgbData.GetSize() < width) {
    m_rgbData.SetSize(width);
  }
  int32_t* rowLine = (int32_t*)m_pBitmap->GetScanline(y);
  int32_t x;
  for (x = 0; x < width; x++) {
    int32_t pixel = rowLine[x];
    int32_t luminance = (306 * ((pixel >> 16) & 0xFF) +
                         601 * ((pixel >> 8) & 0xFF) + 117 * (pixel & 0xFF)) >>
                        10;
    row[x] = (uint8_t)luminance;
  }
  return &row;
}
CFX_ByteArray* CBC_BufferedImageLuminanceSource::GetMatrix() {
  CFX_ByteArray* matirx = new CFX_ByteArray();
  matirx->SetSize(m_bytesPerLine * m_height);
  int32_t* rgb = (int32_t*)m_pBitmap->GetBuffer();
  int32_t y;
  for (y = 0; y < m_height; y++) {
    int32_t offset = y * m_width;
    int32_t x;
    for (x = 0; x < m_width; x++) {
      int32_t pixel = rgb[offset + x];
      int32_t luminance =
          (306 * ((pixel >> 16) & 0xFF) + 601 * ((pixel >> 8) & 0xFF) +
           117 * (pixel & 0xFF)) >>
          10;
      (*matirx)[offset + x] = (uint8_t)luminance;
    }
  }
  return matirx;
}
FX_BOOL CBC_BufferedImageLuminanceSource::IsCropSupported() {
  return TRUE;
}
FX_BOOL CBC_BufferedImageLuminanceSource::IsRotateSupported() {
  return TRUE;
}
CBC_LuminanceSource* CBC_BufferedImageLuminanceSource::Crop(int32_t left,
                                                            int32_t top,
                                                            int32_t width,
                                                            int32_t height) {
  return NULL;
}
CBC_LuminanceSource* CBC_BufferedImageLuminanceSource::RotateCounterClockwise(
    int32_t& e) {
  if (!IsRotateSupported())
    e = BCExceptionRotateNotSupported;
  return NULL;
}