Commit 4f99cc0d authored by Mahmoud Khalil's avatar Mahmoud Khalil
Browse files

Refactors ImageXObject to AnnotStampImageHelper

parent 95a73871
Pipeline #369611 failed with stage
in 3 minutes and 59 seconds
......@@ -375,6 +375,7 @@ set(poppler_SRCS
fofi/FoFiType1C.cc
fofi/FoFiIdentifier.cc
poppler/Annot.cc
poppler/AnnotStampImageHelper.cc
poppler/Array.cc
poppler/CachedFile.cc
poppler/Catalog.cc
......@@ -394,7 +395,6 @@ set(poppler_SRCS
poppler/GfxState.cc
poppler/GlobalParams.cc
poppler/Hints.cc
poppler/ImageXObject.cc
poppler/JArithmeticDecoder.cc
poppler/JBIG2Stream.cc
poppler/JSInfo.cc
......@@ -582,6 +582,7 @@ install(TARGETS poppler RUNTIME DESTINATION bin LIBRARY DESTINATION ${CMAKE_INST
if(ENABLE_UNSTABLE_API_ABI_HEADERS)
install(FILES
poppler/Annot.h
poppler/AnnotStampImageHelper.h
poppler/Array.h
poppler/CachedFile.h
poppler/Catalog.h
......@@ -602,7 +603,6 @@ if(ENABLE_UNSTABLE_API_ABI_HEADERS)
poppler/GfxState_helpers.h
poppler/GlobalParams.h
poppler/Hints.h
poppler/ImageXObject.h
poppler/JArithmeticDecoder.h
poppler/JBIG2Stream.h
poppler/JSInfo.h
......
......@@ -5363,7 +5363,11 @@ AnnotStamp::AnnotStamp(PDFDoc *docA, Object &&dictObject, const Object *obj) : A
initialize(docA, annotObj.getDict());
}
AnnotStamp::~AnnotStamp() = default;
AnnotStamp::~AnnotStamp()
{
if (stampImageHelper != nullptr)
delete stampImageHelper;
}
void AnnotStamp::initialize(PDFDoc *docA, Dict *dict)
{
......@@ -5374,13 +5378,14 @@ void AnnotStamp::initialize(PDFDoc *docA, Dict *dict)
icon = std::make_unique<GooString>("Draft");
}
customImageRef = Ref::INVALID();
stampImageHelper = nullptr;
updatedAppearanceStream = Ref::INVALID();
}
void AnnotStamp::generateStampAppearance()
{
std::string imgStrName = "X" + std::to_string(customImageRef.num);
Ref imgRef = stampImageHelper->getRef();
std::string imgStrName = "X" + std::to_string(imgRef.num);
GooString imgRefName(imgStrName);
AnnotAppearanceBuilder appearBuilder;
......@@ -5392,7 +5397,7 @@ void AnnotStamp::generateStampAppearance()
appearBuilder.append("Q\n");
Dict *xobjectDict = new Dict(doc->getXRef());
xobjectDict->add(imgRefName.c_str(), Object(customImageRef));
xobjectDict->add(imgRefName.c_str(), Object(imgRef));
Dict *resDict = new Dict(doc->getXRef());
resDict->add("XObject", Object(xobjectDict));
......@@ -5408,7 +5413,7 @@ void AnnotStamp::draw(Gfx *gfx, bool printing)
annotLocker();
if (appearance.isNull()) {
if (customImageRef == Ref::INVALID())
if (stampImageHelper == nullptr)
return;
generateStampAppearance();
......@@ -5435,15 +5440,15 @@ void AnnotStamp::setIcon(GooString *new_icon)
invalidateAppearance();
}
void AnnotStamp::setCustomImage(ImageXObject *img)
void AnnotStamp::setCustomImage(AnnotStampImageHelper *stampImageHelperA)
{
if (!img)
if (!stampImageHelperA)
return;
annotLocker();
clearCustomImage();
customImageRef = img->getRef();
stampImageHelper = stampImageHelperA;
generateStampAppearance();
if (updatedAppearanceStream == Ref::INVALID()) {
......@@ -5460,8 +5465,9 @@ void AnnotStamp::setCustomImage(ImageXObject *img)
void AnnotStamp::clearCustomImage()
{
if (customImageRef != Ref::INVALID()) {
doc->getXRef()->removeIndirectObject(customImageRef);
if (stampImageHelper != nullptr) {
stampImageHelper->removeAnnotStampImageObject();
delete stampImageHelper;
}
invalidateAppearance();
}
......
......@@ -52,8 +52,8 @@
#include <mutex>
#include <vector>
#include "AnnotStampImageHelper.h"
#include "Object.h"
#include "ImageXObject.h"
#include "poppler_private_export.h"
class XRef;
......@@ -1198,7 +1198,7 @@ public:
void setIcon(GooString *new_icon);
void setCustomImage(ImageXObject *img);
void setCustomImage(AnnotStampImageHelper *img);
void clearCustomImage();
......@@ -1210,7 +1210,7 @@ private:
void generateStampAppearance();
std::unique_ptr<GooString> icon; // Name (Default Draft)
Ref customImageRef;
AnnotStampImageHelper *stampImageHelper;
Ref updatedAppearanceStream;
};
......
//========================================================================
//
// ImageXObject.h
// AnnotStampImageHelper.h
//
// Copyright (C) 2021 Mahmoud Ahmed Khalil <mahmoudkhalil11@gmail.com>
//
......@@ -8,7 +8,7 @@
//
//========================================================================
#include "ImageXObject.h"
#include "AnnotStampImageHelper.h"
#include "goo/gmem.h"
#include "goo/gstrtod.h"
......@@ -18,26 +18,26 @@
#include <iostream>
#define imgLocker() std::unique_lock<std::recursive_mutex> locker(mutex)
ImageXObject::ImageXObject(PDFDoc *docA, int widthA, int heightA, ColorSpace colorSpace, int bitsPerComponent, char *data, int dataLength)
AnnotStampImageHelper::AnnotStampImageHelper(PDFDoc *docA, int widthA, int heightA, ColorSpace colorSpace, int bitsPerComponent, char *data, int dataLength)
{
initialize(docA, widthA, heightA, colorSpace, bitsPerComponent, data, dataLength);
}
ImageXObject::ImageXObject(PDFDoc *docA, int widthA, int heightA, ColorSpace colorSpace, int bitsPerComponent, char *data, int dataLength, Ref sMaskRef)
AnnotStampImageHelper::AnnotStampImageHelper(PDFDoc *docA, int widthA, int heightA, ColorSpace colorSpace, int bitsPerComponent, char *data, int dataLength, Ref softMaskRef)
{
initialize(docA, widthA, heightA, colorSpace, bitsPerComponent, data, dataLength);
sMaskRef = softMaskRef;
Dict *dict = imgObj.streamGetDict();
dict->add("SMask", Object(sMaskRef));
}
void ImageXObject::initialize(PDFDoc *docA, int widthA, int heightA, ColorSpace colorSpace, int bitsPerComponent, char *data, int dataLength)
void AnnotStampImageHelper::initialize(PDFDoc *docA, int widthA, int heightA, ColorSpace colorSpace, int bitsPerComponent, char *data, int dataLength)
{
doc = docA;
width = widthA;
height = heightA;
sMaskRef = Ref::INVALID();
Dict *dict = new Dict(docA->getXRef());
dict->add("Type", Object(objName, "XObject"));
......@@ -68,7 +68,10 @@ void ImageXObject::initialize(PDFDoc *docA, int widthA, int heightA, ColorSpace
ref = doc->getXRef()->addIndirectObject(&imgObj);
}
void ImageXObject::setPage(int pageA)
void AnnotStampImageHelper::removeAnnotStampImageObject()
{
page = pageA;
if (sMaskRef != Ref::INVALID())
doc->getXRef()->removeIndirectObject(sMaskRef);
doc->getXRef()->removeIndirectObject(ref);
}
//========================================================================
//
// ImageXObject.h
// AnnotStampImageHelper.h
//
// Copyright (C) 2021 Mahmoud Ahmed Khalil <mkhalil.hamdallah@gmail.com>
// Copyright (C) 2021 Mahmoud Ahmed Khalil <mahmoudkhalil11@gmail.com>
//
// Licensed under GPLv2 or later
//
//========================================================================
#ifndef IMAGEXOBJECT_H
#define IMAGEXOBJECT_H
#include <mutex>
#ifndef ANNOTSTAMPIMAGEHELPER_H
#define ANNOTSTAMPIMAGEHELPER_H
#include "Object.h"
......@@ -24,21 +22,24 @@ enum ColorSpace
DeviceCMYK
};
class POPPLER_PRIVATE_EXPORT ImageXObject
/**
* This class is used only to load Image XObjects into stamp annotations.
*/
class POPPLER_PRIVATE_EXPORT AnnotStampImageHelper
{
public:
ImageXObject(PDFDoc *docA, int widthA, int heightA, ColorSpace colorSpace, int bitsPerComponent, char *data, int dataLength);
ImageXObject(PDFDoc *docA, int widthA, int heightA, ColorSpace colorSpace, int bitsPerComponent, char *data, int dataLength, Ref sMaskRef);
~ImageXObject() { }
void setPage(int pageA);
AnnotStampImageHelper(PDFDoc *docA, int widthA, int heightA, ColorSpace colorSpace, int bitsPerComponent, char *data, int dataLength);
AnnotStampImageHelper(PDFDoc *docA, int widthA, int heightA, ColorSpace colorSpace, int bitsPerComponent, char *data, int dataLength, Ref softMaskRef);
~AnnotStampImageHelper() { }
Ref getRef() { return ref; }
int getWidth() const { return width; }
int getHeight() const { return height; }
Object &getImageXObjectObject() { return imgObj; }
Object &getAnnotStampImageHelperObject() { return imgObj; }
void removeAnnotStampImageObject();
private:
void initialize(PDFDoc *docA, int widthA, int heightA, ColorSpace colorSpace, int bitsPerComponent, char *data, int dataLength);
......@@ -47,13 +48,10 @@ private:
Object imgObj;
Ref ref;
int page;
Ref sMaskRef;
int width;
int height;
mutable std::recursive_mutex mutex;
};
#endif
......@@ -30,7 +30,7 @@
#include "poppler-annotation.h"
#include <Object.h>
#include <ImageXObject.h>
#include <AnnotStampImageHelper.h>
class Annot;
class AnnotPath;
......@@ -91,7 +91,7 @@ public:
::Page *pdfPage;
DocumentData *parentDoc;
ImageXObject *convertQImageToImageXObject(const QImage &qimg);
AnnotStampImageHelper *convertQImageToAnnotStampImageHelper(const QImage &qimg);
/* The following helpers only work if pdfPage is set */
void flushBaseAnnotationProperties();
......
......@@ -131,7 +131,7 @@ QDomElement AnnotationUtils::findChildElement(const QDomNode &parentNode, const
// BEGIN Annotation implementation
AnnotationPrivate::AnnotationPrivate() : flags(0), revisionScope(Annotation::Root), revisionType(Annotation::None), pdfAnnot(nullptr), pdfPage(nullptr), parentDoc(nullptr) { }
ImageXObject *AnnotationPrivate::convertQImageToImageXObject(const QImage &qimg)
AnnotStampImageHelper *AnnotationPrivate::convertQImageToAnnotStampImageHelper(const QImage &qimg)
{
qDebug() << "Format: " << qimg.format();
......@@ -214,18 +214,18 @@ ImageXObject *AnnotationPrivate::convertQImageToImageXObject(const QImage &qimg)
getRawDataFromQImage(convertedQImage, convertedQImage.depth(), data, sMaskData);
ImageXObject *imgXObject;
AnnotStampImageHelper *annotImg;
if (sMaskData->count() > 0) {
ImageXObject sMaskImgXObject(parentDoc->doc, width, height, ColorSpace::DeviceGray, 8, sMaskData->data(), sMaskData->count());
imgXObject = new ImageXObject(parentDoc->doc, width, height, colorSpace, bitsPerComponent, data->data(), data->count(), sMaskImgXObject.getRef());
AnnotStampImageHelper sMask(parentDoc->doc, width, height, ColorSpace::DeviceGray, 8, sMaskData->data(), sMaskData->count());
annotImg = new AnnotStampImageHelper(parentDoc->doc, width, height, colorSpace, bitsPerComponent, data->data(), data->count(), sMask.getRef());
} else {
imgXObject = new ImageXObject(parentDoc->doc, width, height, colorSpace, bitsPerComponent, data->data(), data->count());
annotImg = new AnnotStampImageHelper(parentDoc->doc, width, height, colorSpace, bitsPerComponent, data->data(), data->count());
}
delete data;
return imgXObject;
return annotImg;
}
void getRawDataFromQImage(const QImage &qimg, int bitsPerPixel, QByteArray *data, QByteArray *sMaskData)
......@@ -3382,9 +3382,8 @@ void StampAnnotation::setStampCustomImage(const QImage &image)
}
AnnotStamp *stampann = static_cast<AnnotStamp *>(d->pdfAnnot);
ImageXObject *annotCustomImageXObject = d->convertQImageToImageXObject(image);
stampann->setCustomImage(annotCustomImageXObject);
delete annotCustomImageXObject;
AnnotStampImageHelper *annotCustomImage = d->convertQImageToAnnotStampImageHelper(image);
stampann->setCustomImage(annotCustomImage);
}
void StampAnnotation::clearStampCustomImage()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment