Commit 1393b256 authored by Volker Krause's avatar Volker Krause
Browse files

Add flag to convert PDF images to grayscale during loading already

This avoids having to do it during binarization for barcode decoding, at
which points its a bit more expensive, and it reduces the amount of memory
we need in the process.
parent ec24147a
......@@ -71,7 +71,7 @@ std::vector<GenericExtractor::Result> GenericPdfExtractor::extract(PdfDocument *
for (int j = 0; j < page.imageCount(); ++j) {
auto img = page.image(j);
img.setLoadingHints(PdfImage::AbortOnColorHint); // we only care about b/w-ish images for barcode detection
img.setLoadingHints(PdfImage::AbortOnColorHint | PdfImage::ConvertToGrayscaleHint); // we only care about b/w-ish images for barcode detection
if (img.hasObjectId() && m_imageIds.find(img.objectId()) != m_imageIds.end()) {
continue;
}
......
......@@ -93,7 +93,7 @@ static inline bool isColor(GfxRGB rgb)
QImage PdfImagePrivate::load(Stream* str, GfxImageColorMap* colorMap)
{
auto img = QImage(m_sourceWidth, m_sourceHeight, m_format);
auto img = QImage(m_sourceWidth, m_sourceHeight, (m_loadingHints & PdfImage::ConvertToGrayscaleHint) ? QImage::Format_Grayscale8 : m_format);
const auto bytesPerPixel = colorMap->getNumPixelComps();
std::unique_ptr<ImageStream> imgStream(new ImageStream(str, m_sourceWidth, bytesPerPixel, colorMap->getBits()));
imgStream->reset();
......@@ -109,9 +109,13 @@ QImage PdfImagePrivate::load(Stream* str, GfxImageColorMap* colorMap)
if ((m_loadingHints & PdfImage::AbortOnColorHint) && isColor(rgb)) {
return {};
}
*imgData++ = colToByte(rgb.r);
*imgData++ = colToByte(rgb.g);
*imgData++ = colToByte(rgb.b);
if ((m_loadingHints & PdfImage::ConvertToGrayscaleHint)) {
*imgData++ = colToByte(rgb.g); // technically not correct but good enough
} else {
*imgData++ = colToByte(rgb.r);
*imgData++ = colToByte(rgb.g);
*imgData++ = colToByte(rgb.b);
}
}
}
break;
......
......@@ -62,6 +62,7 @@ public:
enum LoadingHint {
NoHint = 0, ///< Load image data as-is. The default.
AbortOnColorHint = 1, ///< Abort loading when encountering a non black/white pixel, as a shortcut for barcode detection.
ConvertToGrayscaleHint = 2, ///< Convert to QImage::Format_Grayscale8 during loading. More efficient than converting later if all you need is grayscale.
};
Q_DECLARE_FLAGS(LoadingHints, LoadingHint)
......
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