JBIG2 file viewer

This commit is contained in:
Jakub Melka 2019-10-29 19:34:22 +01:00
parent 489033c4ce
commit a60b13c3a0
8 changed files with 340 additions and 3 deletions

View File

@ -0,0 +1,36 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
QMAKE_CXXFLAGS += /std:c++latest /utf-8
INCLUDEPATH += $$PWD/../PDFForQtLib/Sources
DESTDIR = $$OUT_PWD/..
LIBS += -L$$OUT_PWD/..
LIBS += -lPDFForQtLib
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui

16
JBIG2_Viewer/main.cpp Normal file
View File

@ -0,0 +1,16 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCoreApplication::setOrganizationName("MelkaJ");
QCoreApplication::setApplicationName("JBIG2_image_viewer");
QCoreApplication::setApplicationVersion("1.0");
MainWindow w;
w.show();
return a.exec();
}

114
JBIG2_Viewer/mainwindow.cpp Normal file
View File

@ -0,0 +1,114 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "pdfexception.h"
#include "pdfjbig2decoder.h"
#include <QFile>
#include <QFileDialog>
#include <QImageReader>
#include <QHBoxLayout>
#include <QLabel>
#include <QPixmap>
#include <QSettings>
#include <QMessageBox>
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowState(Qt::WindowMaximized);
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
m_directory = settings.value("Directory").toString();
}
MainWindow::~MainWindow()
{
QSettings settings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName());
settings.setValue("Directory", m_directory);
delete ui;
}
void MainWindow::reportRenderError(pdf::RenderErrorType type, QString message)
{
Q_UNUSED(type);
QMessageBox::critical(this, tr("Error"), message);
}
void MainWindow::on_actionAddImage_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open image"), m_directory, QString("Bitmap image (*.bmp)"));
if (QFile::exists(fileName))
{
QImageReader reader(fileName);
QImage image = reader.read();
if (!image.isNull())
{
QFileInfo file(fileName);
m_directory = file.filePath();
addImage(file.fileName(), qMove(image));
}
}
}
void MainWindow::addImage(QString title, QImage image)
{
QGroupBox* groupBox = new QGroupBox(this);
QHBoxLayout* layout = new QHBoxLayout(groupBox);
groupBox->setTitle(title);
QLabel* label = new QLabel(groupBox);
layout->addWidget(label);
label->setPixmap(QPixmap::fromImage(image));
label->setFixedSize(image.size());
ui->imagesMainLayout->addWidget(groupBox);
}
void MainWindow::on_actionClear_triggered()
{
for (int i = 0; i < ui->imagesMainLayout->count(); ++i)
{
ui->imagesMainLayout->itemAt(i)->widget()->deleteLater();
}
}
void MainWindow::on_actionAdd_JBIG2_image_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open image"), m_directory, QString("JBIG2 image (*.jb2)"));
if (QFile::exists(fileName))
{
QFile file(fileName);
if (file.open(QFile::ReadOnly))
{
m_directory = QFileInfo(file).filePath();
QByteArray data = file.readAll();
file.close();
try
{
if (data.startsWith( "\x97\x4A\x42\x32\x0D\x0A\x1A\x0A"))
{
data = data.mid(13);
}
pdf::PDFJBIG2Decoder decoder(data, QByteArray(), this);
pdf::PDFImageData imageData = decoder.decode(pdf::PDFImageData::MaskingType::None);
if (imageData.isValid())
{
QImage image(imageData.getWidth(), imageData.getHeight(), QImage::Format_Mono);
const uchar* sourceData = reinterpret_cast<const uchar*>(imageData.getData().constData());
std::copy(sourceData, sourceData + imageData.getData().size(), image.bits());
addImage(file.fileName(), qMove(image));
}
}
catch (pdf::PDFException exception)
{
QMessageBox::critical(this, tr("Error"), exception.getMessage());
}
}
}
}

35
JBIG2_Viewer/mainwindow.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "pdfexception.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow, public pdf::PDFRenderErrorReporter
{
Q_OBJECT
public:
MainWindow(QWidget* parent = nullptr);
virtual ~MainWindow() override;
virtual void reportRenderError(pdf::RenderErrorType type, QString message) override;
private slots:
void on_actionAddImage_triggered();
void on_actionClear_triggered();
void on_actionAdd_JBIG2_image_triggered();
private:
void addImage(QString title, QImage image);
Ui::MainWindow* ui;
QString m_directory;
};
#endif // MAINWINDOW_H

110
JBIG2_Viewer/mainwindow.ui Normal file
View File

@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>JBIG2 Image Viewer</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QScrollArea" name="scrollArea">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>780</width>
<height>537</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Images</string>
</property>
<layout class="QHBoxLayout" name="imagesMainLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionAddImage"/>
<addaction name="actionAdd_JBIG2_image"/>
<addaction name="actionClear"/>
</widget>
<addaction name="menuFile"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionAddImage">
<property name="text">
<string>Add image</string>
</property>
<property name="shortcut">
<string>Ctrl+O</string>
</property>
</action>
<action name="actionClear">
<property name="text">
<string>Clear</string>
</property>
<property name="shortcut">
<string>Ctrl+W</string>
</property>
</action>
<action name="actionAdd_JBIG2_image">
<property name="text">
<string>Add JBIG2 image</string>
</property>
<property name="shortcut">
<string>Ctrl+J</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -18,9 +18,11 @@
TEMPLATE = subdirs
SUBDIRS += \
JBIG2_Viewer \
PdfForQtLib \
UnitTests \
PdfForQtViewer
UnitTests.depends = PdfForQtLib
PdfForQtViewer.depends = PdfForQtLib
JBIG2_Viewer.depends = PdfForQtLib

View File

@ -608,6 +608,11 @@ PDFJBIG2SegmentHeader PDFJBIG2SegmentHeader::read(PDFBitReader* reader)
return header;
}
PDFJBIG2Decoder::~PDFJBIG2Decoder()
{
}
PDFImageData PDFJBIG2Decoder::decode(PDFImageData::MaskingType maskingType)
{
for (const QByteArray* data : { &m_globalData, &m_data })
@ -1341,6 +1346,11 @@ PDFJBIG2Bitmap::PDFJBIG2Bitmap(int width, int height, uint8_t fill) :
m_data.resize(width * height, fill);
}
PDFJBIG2Bitmap::~PDFJBIG2Bitmap()
{
}
void PDFJBIG2Bitmap::paint(const PDFJBIG2Bitmap& bitmap, int offsetX, int offsetY, PDFJBIG2BitOperation operation, bool expandY, const uint8_t expandPixel)
{
if (!bitmap.isValid())
@ -1475,4 +1485,9 @@ uint32_t PDFJBIG2ArithmeticDecoderState::getQe(size_t context) const
return JBIG2_ARITHMETIC_DECODER_QE_VALUES[getQeRowIndex(context)].Qe;
}
PDFJBIG2Segment::~PDFJBIG2Segment()
{
}
} // namespace pdf

View File

@ -213,7 +213,7 @@ class PDFJBIG2Segment
{
public:
explicit inline PDFJBIG2Segment() = default;
virtual ~PDFJBIG2Segment() = default;
virtual ~PDFJBIG2Segment();
virtual const PDFJBIG2Bitmap* asBitmap() const { return nullptr; }
virtual PDFJBIG2Bitmap* asBitmap() { return nullptr; }
@ -243,12 +243,13 @@ private:
std::vector<PDFJBIG2HuffmanTableEntry> m_entries;
};
class PDFJBIG2Bitmap : public PDFJBIG2Segment
class PDFFORQTLIBSHARED_EXPORT PDFJBIG2Bitmap : public PDFJBIG2Segment
{
public:
explicit PDFJBIG2Bitmap();
explicit PDFJBIG2Bitmap(int width, int height);
explicit PDFJBIG2Bitmap(int width, int height, uint8_t fill);
virtual ~PDFJBIG2Bitmap() override;
virtual const PDFJBIG2Bitmap* asBitmap() const override { return this; }
virtual PDFJBIG2Bitmap* asBitmap() override { return this; }
@ -351,7 +352,7 @@ struct PDFJBIG2BitmapDecodingParameters
/// Decoder of JBIG2 data streams. Decodes the black/white monochrome image.
/// Handles also global segments. Decoder decodes data using the specification
/// ISO/IEC 14492:2001, T.88.
class PDFJBIG2Decoder
class PDFFORQTLIBSHARED_EXPORT PDFJBIG2Decoder
{
public:
explicit inline PDFJBIG2Decoder(QByteArray data, QByteArray globalData, PDFRenderErrorReporter* errorReporter) :
@ -368,6 +369,14 @@ public:
}
PDFJBIG2Decoder(const PDFJBIG2Decoder&) = delete;
PDFJBIG2Decoder(PDFJBIG2Decoder&&) = default;
PDFJBIG2Decoder& operator=(const PDFJBIG2Decoder&) = delete;
PDFJBIG2Decoder& operator=(PDFJBIG2Decoder&&) = default;
~PDFJBIG2Decoder();
/// Decodes image interpreting the data as JBIG2 data stream. If image cannot
/// be decoded, exception is thrown (or invalid PDFImageData is returned).
/// \param maskingType Image masking type