PDF4QT/PdfForQtLib/sources/pdfccittfaxdecoder.h

120 lines
4.5 KiB
C
Raw Normal View History

2019-10-06 17:36:43 +02:00
// Copyright (C) 2019 Jakub Melka
//
// This file is part of PdfForQt.
//
// PdfForQt is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// PdfForQt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with PDFForQt. If not, see <https://www.gnu.org/licenses/>.
#ifndef PDFCCITTFAXDECODER_H
#define PDFCCITTFAXDECODER_H
#include "pdfutils.h"
2019-10-12 18:10:25 +02:00
#include "pdfimage.h"
2019-10-06 17:36:43 +02:00
namespace pdf
{
struct PDFCCITTCode;
2019-10-12 18:10:25 +02:00
struct PDFCCITTFaxDecoderParameters
{
/// Type of encoding. Has this meaning:
/// K < 0 - pure two dimensional encoding (Group 4)
/// K = 0 - pure one dimensional encoding
/// K > 0 - mixed encoding; one dimensional encoded line can be followed by at most K - 1 two dimensional encoded lines
PDFInteger K = 0;
/// Pixel width of the image. Default value is 1728.
PDFInteger columns = 1728;
/// Pixel height of the image. This value can be zero or be absent, in this case,
/// end of block pattern must be present and end the stream.
PDFInteger rows = 0;
/// This parameter is ignored in this library. If positive, and \p hasEndOfLine is true,
/// and K is nonnegative, then if error occurs, end-of-line pattern is searched and
/// data are copied from previous line, or are set to white, if previous line is also damaged.
PDFInteger damagedRowsBeforeError = 0;
/// Flag indicating, that end of line patterns are required in the encoded data.
/// Stream filter must always accept end of line patterns, but require them only,
/// if this flag is set to true.
bool hasEndOfLine = false;
/// Flag indicating that lines are byte aligned, i.e. 0 bits are inserted before each line
/// to achieve byte alignment.
bool hasEncodedByteAlign = false;
2019-10-28 17:39:22 +01:00
/// Flag indicating, that filter expects the data be terminated by end of block bit pattern.
2019-10-12 18:10:25 +02:00
/// In this case, \p rows parameter is ignored. Otherwise, rows parameter is used, or image
/// is terminated by end of data stream, whichever occurs first. The end of block is marked
/// as end-of-facsimile block (EOFB), or return to control (RTC), according the K parameter.
bool hasEndOfBlock = true;
/// If this flag is true, then 1 means black pixel, 0 white pixel. Otherwise, if false,
/// then 0 means black pixel and 1 white pixel.
bool hasBlackIsOne = false;
2019-10-13 19:02:38 +02:00
2019-10-18 17:28:45 +02:00
/// Decode
std::vector<PDFReal> decode;
/// Masking type
2019-10-13 19:02:38 +02:00
PDFImageData::MaskingType maskingType = PDFImageData::MaskingType::None;
2019-10-12 18:10:25 +02:00
};
enum CCITT_2D_Code_Mode;
2019-10-06 17:36:43 +02:00
class PDFCCITTFaxDecoder
{
public:
2019-10-12 18:10:25 +02:00
explicit PDFCCITTFaxDecoder(const QByteArray* stream, const PDFCCITTFaxDecoderParameters& parameters);
PDFImageData decode();
2019-10-06 17:36:43 +02:00
private:
2019-10-12 18:10:25 +02:00
/// Skip zero bits at the start
void skipFill();
/// Skip end-of-line, if occured. Returns true, if EOL was skipped.
bool skipEOL();
/// Skip fill bits and then try to skip EOL. If EOL is found, then
/// true is returned, otherwise false is returned.
bool skipFillAndEOL() { skipFill(); return skipEOL(); }
/// Add pixels to the line.
/// \param line Line with changing element indices
/// \param a0_index Reference changing element index (index to the \p line array)
/// \param a1 Current changing element index (column index, not index to the \p line array)
/// \param isCurrentPixelBlack Are pixels black?
/// \param isA1LeftOfA0Allowed Allow a1 to be left of a0 (not a0_index, but line[a0_index], which is a0)
void addPixels(std::vector<int>& line, int& a0_index, int a1, bool isCurrentPixelBlack, bool isA1LeftOfA0Allowed);
/// Get 2D mode from the stream
CCITT_2D_Code_Mode get2DMode();
2019-10-06 17:36:43 +02:00
uint32_t getRunLength(bool white);
uint32_t getWhiteCode();
uint32_t getBlackCode();
uint32_t getCode(const PDFCCITTCode* codes, size_t codeCount);
PDFBitReader m_reader;
2019-10-12 18:10:25 +02:00
PDFCCITTFaxDecoderParameters m_parameters;
2019-10-06 17:36:43 +02:00
};
} // namespace pdf
#endif // PDFCCITTFAXDECODER_H