From 5896196f560cff32a231e820536ae8b5c2b6bb63 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Sun, 12 May 2019 14:59:08 +0200 Subject: [PATCH] JPX 2000 image --- PdfForQtLib/sources/pdfcolorspaces.cpp | 21 +++++++++------------ PdfForQtLib/sources/pdfimage.cpp | 12 ++++++++++-- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/PdfForQtLib/sources/pdfcolorspaces.cpp b/PdfForQtLib/sources/pdfcolorspaces.cpp index 840691c..8368a5b 100644 --- a/PdfForQtLib/sources/pdfcolorspaces.cpp +++ b/PdfForQtLib/sources/pdfcolorspaces.cpp @@ -646,17 +646,18 @@ QImage PDFIndexedColorSpace::getImage(const PDFImageData& imageData) const image.fill(QColor(Qt::white)); unsigned int componentCount = imageData.getComponents(); - QDataStream stream(imageData.getData(), QIODevice::ReadOnly); - PDFBitReader reader(stream, imageData.getBitsPerComponent()); + QDataStream stream(const_cast(&imageData.getData()), QIODevice::ReadOnly); + PDFBitReader reader(&stream, imageData.getBitsPerComponent()); - /* if (componentCount != getColorComponentCount()) { - throw PDFParserException(PDFTranslationContext::tr("Invalid colors for color space. Color space has %1 colors. Provided color count is %4.").arg(getColorComponentCount()).arg(componentCount)); - }*/ + throw PDFParserException(PDFTranslationContext::tr("Invalid colors for indexed color space. Color space has %1 colors. Provided color count is %4.").arg(getColorComponentCount()).arg(componentCount)); + } + + Q_ASSERT(componentCount == 1); PDFColor color; - color.resize(componentCount); + color.resize(1); for (unsigned int i = 0, rowCount = imageData.getHeight(); i < rowCount; ++i) { @@ -665,12 +666,8 @@ QImage PDFIndexedColorSpace::getImage(const PDFImageData& imageData) const for (unsigned int j = 0; j < imageData.getWidth(); ++j) { - const unsigned char* currentData = rowData + (j * componentCount); - for (unsigned int k = 0; k < componentCount; ++k) - { - constexpr const double COEFFICIENT = 1.0 / 255.0; - color[k] = currentData[k] * COEFFICIENT; - } + PDFBitReader::Value index = reader.read(); + color[0] = index; QColor transformedColor = getColor(color); QRgb rgb = transformedColor.rgb(); diff --git a/PdfForQtLib/sources/pdfimage.cpp b/PdfForQtLib/sources/pdfimage.cpp index b03dd97..40099fc 100644 --- a/PdfForQtLib/sources/pdfimage.cpp +++ b/PdfForQtLib/sources/pdfimage.cpp @@ -244,6 +244,15 @@ PDFImage PDFImage::createImage(const PDFDocument* document, const PDFStream* str opj_dparameters_t decompressParameters; opj_set_default_decoder_parameters(&decompressParameters); + const bool isIndexed = dynamic_cast(image.m_colorSpace.data()); + if (isIndexed) + { + // What is this flag for? When we have indexed color space, we do not want to resolve index to color + // using the color map in the image. Instead of that, we just get indices and resolve them using + // our color space. + decompressParameters.flags |= OPJ_DPARAMETERS_IGNORE_PCLR_CMAP_CDEF_FLAG; + } + constexpr CODEC_FORMAT formats[] = { OPJ_CODEC_J2K, OPJ_CODEC_JP2, OPJ_CODEC_JPT, OPJ_CODEC_JPP, OPJ_CODEC_JPX }; for (CODEC_FORMAT format : formats) { @@ -327,7 +336,6 @@ PDFImage PDFImage::createImage(const PDFDocument* document, const PDFStream* str const OPJ_UINT32 prec = jpegImage->comps[0].prec; const OPJ_UINT32 sgnd = jpegImage->comps[0].sgnd; - const bool isIndexed = dynamic_cast(image.m_colorSpace.data()); int signumCorrection = (sgnd) ? (1 << (prec - 1)) : 0; int shiftLeft = (jpegImage->comps[0].prec < 8) ? 8 - jpegImage->comps[0].prec : 0; int shiftRight = (jpegImage->comps[0].prec > 8) ? jpegImage->comps[0].prec - 8 : 0; @@ -373,7 +381,7 @@ PDFImage PDFImage::createImage(const PDFDocument* document, const PDFStream* str int index = stride * row + col * components + componentIndex; Q_ASSERT(index < imageDataBuffer.size()); - imageDataBuffer[index] = transformValue(jpegImage->comps[componentIndex].data[w * row + h]); + imageDataBuffer[index] = transformValue(jpegImage->comps[componentIndex].data[w * row + col]); } } }