| OLD | NEW |
| (Empty) |
| 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 2009 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 "core/include/fxcodec/fx_codec.h" | |
| 24 #include "xfa/src/fxbarcode/BC_BufferedImageLuminanceSource.h" | |
| 25 #include "xfa/src/fxbarcode/BC_LuminanceSource.h" | |
| 26 #include "xfa/src/fxbarcode/utils.h" | |
| 27 | |
| 28 class CBC_Pause : public IFX_Pause { | |
| 29 public: | |
| 30 virtual FX_BOOL NeedToPauseNow() { return TRUE; } | |
| 31 }; | |
| 32 static CFX_DIBitmap* CreateDIBSource(IFX_FileRead* fileread) { | |
| 33 CFX_DIBitmap* bitmap = NULL; | |
| 34 CCodec_ModuleMgr* pCodecMgr = NULL; | |
| 35 ICodec_ProgressiveDecoder* pImageCodec = NULL; | |
| 36 pCodecMgr = new CCodec_ModuleMgr(); | |
| 37 pImageCodec = pCodecMgr->CreateProgressiveDecoder(); | |
| 38 FXCODEC_STATUS status = FXCODEC_STATUS_DECODE_FINISH; | |
| 39 status = pImageCodec->LoadImageInfo(fileread, FXCODEC_IMAGE_UNKNOWN, nullptr); | |
| 40 if (status != FXCODEC_STATUS_FRAME_READY) { | |
| 41 return NULL; | |
| 42 } | |
| 43 bitmap = new CFX_DIBitmap; | |
| 44 bitmap->Create(pImageCodec->GetWidth(), pImageCodec->GetHeight(), FXDIB_Argb); | |
| 45 bitmap->Clear(FXARGB_MAKE(0xFF, 0xFF, 0xFF, 0xFF)); | |
| 46 CBC_Pause pause; | |
| 47 int32_t frames; | |
| 48 status = pImageCodec->GetFrames(frames, &pause); | |
| 49 while (status == FXCODEC_STATUS_FRAME_TOBECONTINUE) { | |
| 50 status = pImageCodec->GetFrames(frames, &pause); | |
| 51 } | |
| 52 if (status != FXCODEC_STATUS_DECODE_READY) { | |
| 53 goto except; | |
| 54 } | |
| 55 status = pImageCodec->StartDecode(bitmap, 0, 0, bitmap->GetWidth(), | |
| 56 bitmap->GetHeight(), 0, FALSE); | |
| 57 if (status == FXCODEC_STATUS_ERR_PARAMS) { | |
| 58 goto except; | |
| 59 } | |
| 60 if (status != FXCODEC_STATUS_DECODE_TOBECONTINUE) { | |
| 61 goto except; | |
| 62 } | |
| 63 while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) { | |
| 64 status = pImageCodec->ContinueDecode(&pause); | |
| 65 } | |
| 66 if (status != FXCODEC_STATUS_DECODE_FINISH) { | |
| 67 goto except; | |
| 68 } | |
| 69 if (pImageCodec) { | |
| 70 delete pImageCodec; | |
| 71 pImageCodec = NULL; | |
| 72 } | |
| 73 delete pCodecMgr; | |
| 74 pCodecMgr = NULL; | |
| 75 return bitmap; | |
| 76 except: | |
| 77 if (pImageCodec) { | |
| 78 delete pImageCodec; | |
| 79 pImageCodec = NULL; | |
| 80 } | |
| 81 delete pCodecMgr; | |
| 82 pCodecMgr = NULL; | |
| 83 if (bitmap) { | |
| 84 delete bitmap; | |
| 85 } | |
| 86 return NULL; | |
| 87 } | |
| 88 CBC_BufferedImageLuminanceSource::CBC_BufferedImageLuminanceSource( | |
| 89 const CFX_WideString& filename) | |
| 90 : CBC_LuminanceSource(0, 0), m_filename(filename) { | |
| 91 m_height = 0; | |
| 92 m_width = 0; | |
| 93 m_bytesPerLine = 0; | |
| 94 m_top = 0; | |
| 95 m_left = 0; | |
| 96 } | |
| 97 void CBC_BufferedImageLuminanceSource::Init(int32_t& e) { | |
| 98 IFX_FileRead* fileread = FX_CreateFileRead(m_filename); | |
| 99 m_pBitmap = CreateDIBSource(fileread); | |
| 100 if (m_pBitmap == NULL) { | |
| 101 e = BCExceptionLoadFile; | |
| 102 return; | |
| 103 } | |
| 104 m_pBitmap->ConvertFormat(FXDIB_Argb); | |
| 105 m_height = m_pBitmap->GetHeight(); | |
| 106 m_width = m_pBitmap->GetWidth(); | |
| 107 m_rgbData.SetSize(m_height * m_width); | |
| 108 m_bytesPerLine = m_width * 4; | |
| 109 m_top = 0; | |
| 110 m_left = 0; | |
| 111 } | |
| 112 CBC_BufferedImageLuminanceSource::CBC_BufferedImageLuminanceSource( | |
| 113 CFX_DIBitmap* pBitmap) | |
| 114 : CBC_LuminanceSource(0, 0) { | |
| 115 m_pBitmap = pBitmap->Clone(); | |
| 116 m_pBitmap->ConvertFormat(FXDIB_Argb); | |
| 117 m_height = m_pBitmap->GetHeight(); | |
| 118 m_width = m_pBitmap->GetWidth(); | |
| 119 m_rgbData.SetSize(m_height * m_width); | |
| 120 m_bytesPerLine = m_width * 4; | |
| 121 m_top = 0; | |
| 122 m_left = 0; | |
| 123 } | |
| 124 CBC_BufferedImageLuminanceSource::~CBC_BufferedImageLuminanceSource() { | |
| 125 delete m_pBitmap; | |
| 126 m_pBitmap = NULL; | |
| 127 } | |
| 128 CFX_ByteArray* CBC_BufferedImageLuminanceSource::GetRow(int32_t y, | |
| 129 CFX_ByteArray& row, | |
| 130 int32_t& e) { | |
| 131 if (y < 0 || y >= m_height) { | |
| 132 e = BCExceptionRequestedRowIsOutSizeTheImage; | |
| 133 return NULL; | |
| 134 } | |
| 135 int32_t width = m_width; | |
| 136 if (row.GetSize() == 0 || row.GetSize() < width) { | |
| 137 row.SetSize(width); | |
| 138 } | |
| 139 if (m_rgbData.GetSize() == 0 || m_rgbData.GetSize() < width) { | |
| 140 m_rgbData.SetSize(width); | |
| 141 } | |
| 142 int32_t* rowLine = (int32_t*)m_pBitmap->GetScanline(y); | |
| 143 int32_t x; | |
| 144 for (x = 0; x < width; x++) { | |
| 145 int32_t pixel = rowLine[x]; | |
| 146 int32_t luminance = (306 * ((pixel >> 16) & 0xFF) + | |
| 147 601 * ((pixel >> 8) & 0xFF) + 117 * (pixel & 0xFF)) >> | |
| 148 10; | |
| 149 row[x] = (uint8_t)luminance; | |
| 150 } | |
| 151 return &row; | |
| 152 } | |
| 153 CFX_ByteArray* CBC_BufferedImageLuminanceSource::GetMatrix() { | |
| 154 CFX_ByteArray* matirx = new CFX_ByteArray(); | |
| 155 matirx->SetSize(m_bytesPerLine * m_height); | |
| 156 int32_t* rgb = (int32_t*)m_pBitmap->GetBuffer(); | |
| 157 int32_t y; | |
| 158 for (y = 0; y < m_height; y++) { | |
| 159 int32_t offset = y * m_width; | |
| 160 int32_t x; | |
| 161 for (x = 0; x < m_width; x++) { | |
| 162 int32_t pixel = rgb[offset + x]; | |
| 163 int32_t luminance = | |
| 164 (306 * ((pixel >> 16) & 0xFF) + 601 * ((pixel >> 8) & 0xFF) + | |
| 165 117 * (pixel & 0xFF)) >> | |
| 166 10; | |
| 167 (*matirx)[offset + x] = (uint8_t)luminance; | |
| 168 } | |
| 169 } | |
| 170 return matirx; | |
| 171 } | |
| OLD | NEW |