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