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>