| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Copyright (C) 2016 Petroules Corporation. |
| 5 | ** Contact: https://www.qt.io/licensing/ |
| 6 | ** |
| 7 | ** This file is part of the JP2 plugins in the Qt ImageFormats module. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU Lesser General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 22 | ** packaging of this file. Please review the following information to |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 25 | ** |
| 26 | ** GNU General Public License Usage |
| 27 | ** Alternatively, this file may be used under the terms of the GNU |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General |
| 29 | ** Public license version 3 or any later version approved by the KDE Free |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 32 | ** included in the packaging of this file. Please review the following |
| 33 | ** information to ensure the GNU General Public License requirements will |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 36 | ** |
| 37 | ** $QT_END_LICENSE$ |
| 38 | ** |
| 39 | ****************************************************************************/ |
| 40 | |
| 41 | #include "qjp2handler_p.h" |
| 42 | |
| 43 | #include "qimage.h" |
| 44 | #include "qvariant.h" |
| 45 | #include "qcolor.h" |
| 46 | #include "qimagereader.h" |
| 47 | |
| 48 | #include <jasper/jasper.h> |
| 49 | #include <math.h> // for pow |
| 50 | |
| 51 | QT_BEGIN_NAMESPACE |
| 52 | |
| 53 | class QJp2HandlerPrivate |
| 54 | { |
| 55 | Q_DECLARE_PUBLIC(QJp2Handler) |
| 56 | Q_DISABLE_COPY(QJp2HandlerPrivate) |
| 57 | public: |
| 58 | int writeQuality; |
| 59 | QByteArray subType; |
| 60 | QJp2Handler *q_ptr; |
| 61 | QJp2HandlerPrivate(QJp2Handler *q_ptr); |
| 62 | }; |
| 63 | |
| 64 | enum SubFormat { Jp2Format, J2kFormat }; |
| 65 | |
| 66 | /* |
| 67 | \class Jpeg2000JasperReader |
| 68 | \brief Jpeg2000JasperReader implements reading and writing of JPEG 2000 |
| 69 | image files. |
| 70 | |
| 71 | \internal |
| 72 | |
| 73 | This class is designed to be used together with the an QImageIO IOHandler, |
| 74 | and it should probably not be necessary to instantiate it directly. |
| 75 | |
| 76 | Internally it used the Jasper library for coding the image data. |
| 77 | */ |
| 78 | class Jpeg2000JasperReader |
| 79 | { |
| 80 | public: |
| 81 | Jpeg2000JasperReader(QIODevice *iod, const SubFormat format = Jp2Format); |
| 82 | |
| 83 | ~Jpeg2000JasperReader(); |
| 84 | |
| 85 | bool read(QImage *pImage); |
| 86 | bool write(const QImage &image, int quality); |
| 87 | private: |
| 88 | typedef void (Jpeg2000JasperReader::*ScanlineFunc)(jas_seqent_t** const, uchar*); |
| 89 | typedef void (Jpeg2000JasperReader::*ScanlineFuncWrite)(jas_matrix_t**, uchar*); |
| 90 | |
| 91 | void copyJasperQt(ScanlineFunc scanlinecopier); |
| 92 | void copyJasperQtGeneric(); |
| 93 | void copyScanlineJasperQtRGB(jas_seqent_t ** const jasperRow, uchar *qtScanLine); |
| 94 | void copyScanlineJasperQtRGBA(jas_seqent_t ** const jasperRow, uchar *qtScanLine); |
| 95 | void copyScanlineJasperQtGray(jas_seqent_t ** const jasperRow, uchar *qtScanLine); |
| 96 | void copyScanlineJasperQtGrayA(jas_seqent_t ** const jasperRow, uchar *qtScanLine); |
| 97 | |
| 98 | void copyQtJasper(const ScanlineFuncWrite scanlinecopier); |
| 99 | void copyScanlineQtJasperRGB(jas_matrix_t ** jasperRow, uchar *qtScanLine); |
| 100 | void copyScanlineQtJasperRGBA(jas_matrix_t ** jasperRow, uchar *qtScanLine); |
| 101 | void copyScanlineQtJasperColormapRGB(jas_matrix_t ** jasperRow, uchar *qtScanLine); |
| 102 | void copyScanlineQtJasperColormapRGBA(jas_matrix_t ** jasperRow, uchar *qtScanLine); |
| 103 | void copyScanlineQtJasperColormapGrayscale(jas_matrix_t ** jasperRow, uchar *qtScanLine); |
| 104 | void copyScanlineQtJasperColormapGrayscaleA(jas_matrix_t ** jasperRow, uchar *qtScanLine); |
| 105 | |
| 106 | bool attemptColorspaceChange(int wantedColorSpace); |
| 107 | bool createJasperMatrix(jas_matrix_t **&matrix); |
| 108 | bool freeJasperMatrix(jas_matrix_t **matrix); |
| 109 | void printColorSpaceError(); |
| 110 | jas_image_cmptparm_t createComponentMetadata(const int width, const int height); |
| 111 | jas_image_t *newRGBAImage(const int width, const int height, bool alpha); |
| 112 | jas_image_t *newGrayscaleImage(const int width, const int height, bool alpha); |
| 113 | bool decodeColorSpace(int clrspc, QString &family, QString &specific); |
| 114 | void printMetadata(jas_image_t *image); |
| 115 | |
| 116 | bool jasperOk; |
| 117 | |
| 118 | QIODevice *ioDevice; |
| 119 | QImage qtImage; |
| 120 | SubFormat format; |
| 121 | |
| 122 | // Qt image properties |
| 123 | int qtWidth; |
| 124 | int qtHeight; |
| 125 | int qtDepth; |
| 126 | int qtNumComponents; |
| 127 | |
| 128 | jas_image_t *jasper_image; |
| 129 | // jasper image properties |
| 130 | int jasNumComponents; |
| 131 | int jasComponentPrecicion[4]; |
| 132 | int computedComponentWidth ; |
| 133 | int computedComponentHeight; |
| 134 | int computedComponentHorizontalSubsampling; |
| 135 | int computedComponentVerticalSubsampling; |
| 136 | int jasperColorspaceFamily; |
| 137 | // maps color to component (ex: colorComponentMapping[RED] |
| 138 | // gives the component that contains the red color) |
| 139 | int colorComponentMapping[4]; |
| 140 | bool hasAlpha; |
| 141 | }; |
| 142 | |
| 143 | QJp2HandlerPrivate::QJp2HandlerPrivate(QJp2Handler *q_ptr) |
| 144 | : writeQuality(100), subType("jp2"), q_ptr(q_ptr) |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | /*! |
| 149 | \class QJp2Handler |
| 150 | \brief The QJp2Handler class provides support for reading and writing |
| 151 | JPEG 2000 image files with the Qt plugin system. |
| 152 | Currently, it only supports dynamically-loaded plugins. |
| 153 | |
| 154 | JPEG files comes in two subtypes: the JPEG 2000 file format (\c .jp2) and |
| 155 | the JPEG 2000 code stream format (\c .j2k, \c .jpc, or \c .j2c). |
| 156 | QJp2Handler can read and write both types. |
| 157 | |
| 158 | To select a subtype, use the setOption() function with the |
| 159 | QImageIOHandler::SubType option and a parameter value of "jp2" or "j2k". |
| 160 | |
| 161 | To set the image quality when writing, you can use setOption() with the |
| 162 | QImageIOHandler::Quality option, or QImageWriter::setQuality() if your are |
| 163 | using a QImageWriter object to write the image. The image quality is |
| 164 | specified as an int in the range 0 to 100. 0 means maximum compression and |
| 165 | 99 means minimum compression. 100 selects lossless encoding, and this is the |
| 166 | default value. |
| 167 | |
| 168 | The JPEG handler is only available as a plugin, |
| 169 | and this enables you to use normal QImage and QPixmap functions to read and |
| 170 | write images. For example: |
| 171 | |
| 172 | \code |
| 173 | myLabel->setPixmap(QPixmap("myimage.jp2")); |
| 174 | |
| 175 | QPixmap myPixmap; |
| 176 | myPixmap.save("myimage.jp2", "JP2"); |
| 177 | \endcode |
| 178 | */ |
| 179 | |
| 180 | /*! |
| 181 | Constructs an instance of QJp2Handler. |
| 182 | */ |
| 183 | QJp2Handler::QJp2Handler() |
| 184 | : d_ptr(new QJp2HandlerPrivate(this)) |
| 185 | { |
| 186 | } |
| 187 | |
| 188 | /*! |
| 189 | Destructor for QJp2Handler. |
| 190 | */ |
| 191 | QJp2Handler::~QJp2Handler() |
| 192 | { |
| 193 | |
| 194 | } |
| 195 | |
| 196 | /*! |
| 197 | Verifies if some values (magic bytes) are set as expected in the |
| 198 | header of the file. If the magic bytes were found, we assume that we |
| 199 | can read the file. The function will assume that the \a iod is |
| 200 | pointing to the beginning of the JPEG 2000 header. (i.e. it will for |
| 201 | instance not seek to the beginning of a file before reading). |
| 202 | |
| 203 | If \a subType is not 0, it will contain "jp2" or "j2k" upon |
| 204 | successful return. |
| 205 | */ |
| 206 | bool QJp2Handler::canRead(QIODevice *iod, QByteArray *subType) |
| 207 | { |
| 208 | bool bCanRead = false; |
| 209 | if (iod) { |
| 210 | const QByteArray header = iod->peek(12); |
| 211 | if (header.startsWith(QByteArrayLiteral("\000\000\000\fjP \r\n\207\n"))) { |
| 212 | // Jp2 is the JPEG 2000 file format |
| 213 | bCanRead = true; |
| 214 | if (subType) |
| 215 | *subType = QByteArray("jp2"); |
| 216 | } else if (header.startsWith(QByteArrayLiteral("\377\117\377\121\000"))) { |
| 217 | // J2c is the JPEG 2000 code stream |
| 218 | bCanRead = true; |
| 219 | if (subType) |
| 220 | *subType = QByteArray("j2k"); |
| 221 | } |
| 222 | } |
| 223 | return bCanRead; |
| 224 | } |
| 225 | |
| 226 | /*! \reimp |
| 227 | */ |
| 228 | bool QJp2Handler::canRead() const |
| 229 | { |
| 230 | QByteArray subType; |
| 231 | if (canRead(device(), &subType)) { |
| 232 | setFormat(subType); |
| 233 | return true; |
| 234 | } |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | /*! \reimp |
| 239 | */ |
| 240 | bool QJp2Handler::read(QImage *image) |
| 241 | { |
| 242 | Jpeg2000JasperReader reader(device()); |
| 243 | return reader.read(image); |
| 244 | } |
| 245 | |
| 246 | /*! \reimp |
| 247 | */ |
| 248 | bool QJp2Handler::write(const QImage &image) |
| 249 | { |
| 250 | Q_D(const QJp2Handler); |
| 251 | SubFormat subFormat; |
| 252 | if (d->subType == QByteArray("jp2")) |
| 253 | subFormat = Jp2Format; |
| 254 | else |
| 255 | subFormat = J2kFormat; |
| 256 | |
| 257 | Jpeg2000JasperReader writer(device(), subFormat); |
| 258 | return writer.write(image, d->writeQuality); |
| 259 | } |
| 260 | |
| 261 | /*! |
| 262 | Get the value associated with \a option. |
| 263 | \sa setOption() |
| 264 | */ |
| 265 | QVariant QJp2Handler::option(ImageOption option) const |
| 266 | { |
| 267 | Q_D(const QJp2Handler); |
| 268 | if (option == Quality) { |
| 269 | return QVariant(d->writeQuality); |
| 270 | } else if (option == SubType) { |
| 271 | return QVariant(d->subType); |
| 272 | } |
| 273 | return QVariant(); |
| 274 | } |
| 275 | |
| 276 | /*! |
| 277 | The JPEG 2000 handler supports two options. |
| 278 | Set \a option to QImageIOHandler::Quality to balance between quality and the |
| 279 | compression level, where \a value should be an integer in the interval |
| 280 | [0-100]. 0 is maximum compression (low quality) and 100 is lossless |
| 281 | compression (high quality). |
| 282 | |
| 283 | Set \a option to QImageIOHandler::Subtype to choose the subtype, |
| 284 | where \a value should be a string equal to either "jp2" or "j2k" |
| 285 | \sa option() |
| 286 | */ |
| 287 | void QJp2Handler::setOption(ImageOption option, const QVariant &value) |
| 288 | { |
| 289 | Q_D(QJp2Handler); |
| 290 | if (option == Quality) { |
| 291 | bool ok; |
| 292 | const int quality = value.toInt(&ok); |
| 293 | if (ok) |
| 294 | d->writeQuality = quality; |
| 295 | } else if (option == SubType) { |
| 296 | const QByteArray subTypeCandidate = value.toByteArray(); |
| 297 | // Test for default Jpeg2000 file format (jp2), or stream format (j2k). |
| 298 | if (subTypeCandidate == QByteArrayLiteral("jp2") || |
| 299 | subTypeCandidate == QByteArrayLiteral("j2k")) |
| 300 | d->subType = subTypeCandidate; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /*! |
| 305 | This function will return true if \a option is set to either |
| 306 | QImageIOHandler::Quality or QImageIOHandler::Subtype. |
| 307 | */ |
| 308 | bool QJp2Handler::supportsOption(ImageOption option) const |
| 309 | { |
| 310 | return (option == Quality || option == SubType); |
| 311 | } |
| 312 | |
| 313 | /*! |
| 314 | Automatic resource handling for a jas_image_t*. |
| 315 | */ |
| 316 | class ScopedJasperImage |
| 317 | { |
| 318 | public: |
| 319 | // Take reference to the pointer here, because the pointer |
| 320 | // may change when we change color spaces. |
| 321 | ScopedJasperImage(jas_image_t *&image):image(image) { } |
| 322 | ~ScopedJasperImage() { jas_image_destroy(image); } |
| 323 | private: |
| 324 | jas_image_t *ℑ |
| 325 | }; |
| 326 | |
| 327 | /*! \internal |
| 328 | Construct a Jpeg2000JasperReader using the provided \a imageIO. |
| 329 | Note that currently the jasper library is initialized in this constructor, |
| 330 | (and freed in the destructor) which means that: |
| 331 | - Only one instance of this class may exist at one time |
| 332 | - No thread safety |
| 333 | */ |
| 334 | Jpeg2000JasperReader::Jpeg2000JasperReader(QIODevice *iod, SubFormat format) |
| 335 | : jasperOk(true), ioDevice(iod), format(format), hasAlpha(false) |
| 336 | { |
| 337 | #if JAS_VERSION_MAJOR < 3 |
| 338 | if (jas_init()) { |
| 339 | jasperOk = false; |
| 340 | qDebug("Jasper Library initialization failed"); |
| 341 | } |
| 342 | #else |
| 343 | jas_conf_clear(); |
| 344 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 345 | jas_conf_set_max_mem_usage(QImageReader::allocationLimit() * 1024 * 1024); |
| 346 | #else |
| 347 | // 128MB seems to be enough. |
| 348 | jas_conf_set_max_mem_usage(128 * 1024 * 1024); |
| 349 | #endif |
| 350 | if (jas_init_library()) { |
| 351 | jasperOk = false; |
| 352 | qDebug("Jasper library initialization failed"); |
| 353 | } |
| 354 | if (jas_init_thread()) { |
| 355 | jas_cleanup_library(); |
| 356 | jasperOk = false; |
| 357 | qDebug("Jasper thread initialization failed"); |
| 358 | } |
| 359 | #endif |
| 360 | } |
| 361 | |
| 362 | Jpeg2000JasperReader::~Jpeg2000JasperReader() |
| 363 | { |
| 364 | #if JAS_VERSION_MAJOR < 3 |
| 365 | if (jasperOk) |
| 366 | jas_cleanup(); |
| 367 | #else |
| 368 | if (jasperOk) { |
| 369 | if (jas_cleanup_thread()) { |
| 370 | qDebug("Jasper thread cleanup failed"); |
| 371 | } |
| 372 | if (jas_cleanup_library()) { |
| 373 | qDebug("Jasper library cleanup failed"); |
| 374 | } |
| 375 | } |
| 376 | #endif |
| 377 | } |
| 378 | |
| 379 | /*! \internal |
| 380 | Opens the file data and attempts to decode it using the Jasper library. |
| 381 | Returns true if successful, false on failure |
| 382 | */ |
| 383 | bool Jpeg2000JasperReader::read(QImage *pImage) |
| 384 | { |
| 385 | if (!jasperOk) |
| 386 | return false; |
| 387 | |
| 388 | /* |
| 389 | Reading proceeds approximately as follows: |
| 390 | 1. Open stream and decode using Jasper |
| 391 | 2. Get image metadata |
| 392 | 3. Change colorspace if necessary |
| 393 | 4. Create a QImage of the appropriate type (32-bit for RGB, |
| 394 | 8-bit for grayscale) |
| 395 | 5. Copy image data from Jasper to the QImage |
| 396 | |
| 397 | When copying the image data from the Jasper data structures to the |
| 398 | QImage, a generic copy function (copyJasperQt) iterates through the |
| 399 | scanlines and calls the provided (via the scanlineCopier argument) |
| 400 | scanline copy function for each scanline. The scanline copy function |
| 401 | selected according to image metadata such as color space and the |
| 402 | presence of an alpha channel. |
| 403 | */ |
| 404 | QByteArray fileContents = ioDevice->readAll(); |
| 405 | jas_stream_t *imageData = jas_stream_memopen(fileContents.data(), |
| 406 | fileContents.size()); |
| 407 | jasper_image = jas_image_decode(imageData, jas_image_getfmt(imageData), 0); |
| 408 | jas_stream_close(imageData); |
| 409 | if (!jasper_image) { |
| 410 | qDebug("Jasper library can't decode Jpeg2000 image data"); |
| 411 | return false; |
| 412 | } |
| 413 | ScopedJasperImage scopedImage(jasper_image); |
| 414 | //printMetadata(jasper_image); |
| 415 | |
| 416 | qtWidth = jas_image_width(jasper_image); |
| 417 | qtHeight = jas_image_height(jasper_image); |
| 418 | jasNumComponents = jas_image_numcmpts(jasper_image); |
| 419 | jasperColorspaceFamily = jas_clrspc_fam(jas_image_clrspc(jasper_image)); |
| 420 | |
| 421 | bool needColorspaceChange = false; |
| 422 | if (jasperColorspaceFamily != JAS_CLRSPC_FAM_RGB && |
| 423 | jasperColorspaceFamily != JAS_CLRSPC_FAM_GRAY) |
| 424 | needColorspaceChange = true; |
| 425 | |
| 426 | // Get per-component data |
| 427 | int c; |
| 428 | for (c = 0; c < jasNumComponents; ++c) { |
| 429 | jasComponentPrecicion[c] = jas_image_cmptprec(jasper_image, c); |
| 430 | |
| 431 | // Test for precision |
| 432 | if (jasComponentPrecicion[c] > 8 || jasComponentPrecicion[c] < 8) |
| 433 | needColorspaceChange = true; |
| 434 | |
| 435 | // Test for subsampling |
| 436 | if (jas_image_cmpthstep(jasper_image, c) != 1 || |
| 437 | jas_image_cmptvstep(jasper_image, c) != 1) |
| 438 | needColorspaceChange = true; |
| 439 | |
| 440 | // Test for signed components |
| 441 | if (jas_image_cmptsgnd(jasper_image, c) != 0) |
| 442 | needColorspaceChange = true; |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | If we encounter a different color space than RGB |
| 447 | (such as XYZ or YCbCr) we change that to RGB. |
| 448 | Also, if any component has "funny" metadata (such as precicion != 8 bits |
| 449 | or subsampling != 1) we also do a colorspace |
| 450 | change in order to convert it to something we can load. |
| 451 | */ |
| 452 | |
| 453 | bool decodeOk = true; |
| 454 | if (needColorspaceChange) |
| 455 | decodeOk = attemptColorspaceChange(JAS_CLRSPC_SRGB); |
| 456 | |
| 457 | if (!decodeOk) { |
| 458 | printColorSpaceError(); |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | // Image metadata may have changed, get from Jasper. |
| 463 | qtWidth = jas_image_width(jasper_image); |
| 464 | qtHeight = jas_image_height(jasper_image); |
| 465 | jasNumComponents = jas_image_numcmpts(jasper_image); |
| 466 | jasperColorspaceFamily = jas_clrspc_fam(jas_image_clrspc(jasper_image)); |
| 467 | for (c = 0; c < jasNumComponents; ++c) { |
| 468 | jasComponentPrecicion[c] = jas_image_cmptprec(jasper_image, c); |
| 469 | } |
| 470 | |
| 471 | if (jasperColorspaceFamily != JAS_CLRSPC_FAM_RGB && |
| 472 | jasperColorspaceFamily != JAS_CLRSPC_FAM_GRAY) { |
| 473 | qDebug("The Qt JPEG 2000 reader was unable to convert colorspace to RGB or grayscale"); |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | // If a component has a subsampling factor != 1, we can't trust |
| 478 | // jas_image_height/width, so we need to figure it out ourselves |
| 479 | bool oddComponentSubsampling = false; |
| 480 | for (c = 0; c < jasNumComponents; ++c) { |
| 481 | if (jas_image_cmpthstep(jasper_image, c) != 1 || |
| 482 | jas_image_cmptvstep(jasper_image, c) != 1) { |
| 483 | oddComponentSubsampling = true; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | if (oddComponentSubsampling) { |
| 488 | // Check if all components have the same vertical/horizontal dim and |
| 489 | // subsampling |
| 490 | computedComponentWidth = jas_image_cmptwidth(jasper_image, 0); |
| 491 | computedComponentHeight = jas_image_cmptheight(jasper_image, 0); |
| 492 | computedComponentHorizontalSubsampling = jas_image_cmpthstep(jasper_image, 0); |
| 493 | computedComponentVerticalSubsampling = jas_image_cmptvstep(jasper_image, 0); |
| 494 | |
| 495 | for (c = 1; c < jasNumComponents; ++c) { |
| 496 | if (computedComponentWidth != jas_image_cmptwidth(jasper_image, c) || |
| 497 | computedComponentWidth != jas_image_cmptwidth(jasper_image, c) || |
| 498 | computedComponentHorizontalSubsampling != jas_image_cmpthstep(jasper_image, c) || |
| 499 | computedComponentVerticalSubsampling != jas_image_cmptvstep(jasper_image, c)) { |
| 500 | qDebug("The Qt JPEG 2000 reader does not support images where " |
| 501 | "component geometry differs from image geometry"); |
| 502 | return false; |
| 503 | } |
| 504 | } |
| 505 | qtWidth = computedComponentWidth * computedComponentHorizontalSubsampling; |
| 506 | qtHeight = computedComponentHeight * computedComponentVerticalSubsampling; |
| 507 | } |
| 508 | |
| 509 | // Sanity check each component |
| 510 | for (c = 0; c < jasNumComponents; ++c) { |
| 511 | // Test for precision |
| 512 | if (jasComponentPrecicion[c]>8 || jasComponentPrecicion[c]<8) { |
| 513 | qDebug("The Qt JPEG 2000 reader does not support components with " |
| 514 | "precision != 8"); |
| 515 | decodeOk = false; |
| 516 | } |
| 517 | #if 0 |
| 518 | // Test the subsampling factor (space between pixels on the image grid) |
| 519 | if (oddComponentSubsampling) { |
| 520 | qDebug("The Qt JPEG 2000 reader does not support components with " |
| 521 | "a subsampling factor != 1 (yet)"); |
| 522 | decodeOk = false; |
| 523 | } |
| 524 | #endif |
| 525 | // Test for signed components |
| 526 | if (jas_image_cmptsgnd(jasper_image, c) != 0) { |
| 527 | qDebug("Qt JPEG 2000 reader does not support signed components"); |
| 528 | decodeOk = false; |
| 529 | } |
| 530 | |
| 531 | // Test for component/image geomoetry mismach. |
| 532 | // If oddComponentSubsampling, then this is already taken care of above. |
| 533 | if (!oddComponentSubsampling) |
| 534 | if (jas_image_cmpttlx(jasper_image,c) != 0 || |
| 535 | jas_image_cmpttly(jasper_image,c) != 0 || |
| 536 | jas_image_cmptbrx(jasper_image,c) != jas_image_brx(jasper_image) || |
| 537 | jas_image_cmptbry(jasper_image,c) != jas_image_bry(jasper_image) || |
| 538 | jas_image_cmptwidth (jasper_image, c) != jas_image_width (jasper_image) || |
| 539 | jas_image_cmptheight(jasper_image, c) != jas_image_height(jasper_image )) { |
| 540 | qDebug("The Qt JPEG 2000 reader does not support images where " |
| 541 | "component geometry differs from image geometry"); |
| 542 | printMetadata(jasper_image); |
| 543 | decodeOk = false; |
| 544 | } |
| 545 | } |
| 546 | if (!decodeOk) |
| 547 | return false; |
| 548 | |
| 549 | // At this point, the colorspace should be either RGB or grayscale, |
| 550 | // and each component should have eight bits of precision and |
| 551 | // no unsupported geometry. |
| 552 | //printMetadata(jasper_image); |
| 553 | |
| 554 | // Get color components |
| 555 | jasperColorspaceFamily = jas_clrspc_fam(jas_image_clrspc(jasper_image)); |
| 556 | if (jasperColorspaceFamily == JAS_CLRSPC_FAM_RGB) { |
| 557 | if (jasNumComponents > 4) |
| 558 | qDebug("JPEG 2000 reader expected 3 or 4 components, got %d", |
| 559 | jasNumComponents); |
| 560 | |
| 561 | // Set up mapping from R,G,B -> component num. |
| 562 | colorComponentMapping[0] = jas_image_getcmptbytype(jasper_image, |
| 563 | JAS_IMAGE_CT_RGB_R); |
| 564 | colorComponentMapping[1] = jas_image_getcmptbytype(jasper_image, |
| 565 | JAS_IMAGE_CT_RGB_G); |
| 566 | colorComponentMapping[2] = jas_image_getcmptbytype(jasper_image, |
| 567 | JAS_IMAGE_CT_RGB_B); |
| 568 | qtNumComponents = 3; |
| 569 | } else if (jasperColorspaceFamily == JAS_CLRSPC_FAM_GRAY) { |
| 570 | if (jasNumComponents > 2) |
| 571 | qDebug("JPEG 2000 reader expected 1 or 2 components, got %d", |
| 572 | jasNumComponents); |
| 573 | colorComponentMapping[0] = jas_image_getcmptbytype(jasper_image, |
| 574 | JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y)); |
| 575 | qtNumComponents = 1; |
| 576 | } else { |
| 577 | printColorSpaceError(); |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | // Get alpha component if one exists. Due to the lack of test images, |
| 582 | // loading images with alpha channels is a bit untested. It works |
| 583 | // with images saved with this implementation though. |
| 584 | const int posibleAlphaComponent1 = 3; |
| 585 | const int posibleAlphaComponent2 = 48; |
| 586 | |
| 587 | if (jasNumComponents == qtNumComponents + 1) { |
| 588 | colorComponentMapping[qtNumComponents] = jas_image_getcmptbytype(jasper_image, posibleAlphaComponent1); |
| 589 | if (colorComponentMapping[qtNumComponents] < 0) { |
| 590 | colorComponentMapping[qtNumComponents] = jas_image_getcmptbytype(jasper_image, posibleAlphaComponent2); |
| 591 | } |
| 592 | if (colorComponentMapping[qtNumComponents] > 0) { |
| 593 | hasAlpha = true; |
| 594 | qtNumComponents++; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | // Check for missing components |
| 599 | for (c = 0; c < qtNumComponents; ++c) { |
| 600 | if (colorComponentMapping[c] < 0) { |
| 601 | qDebug("JPEG 2000 reader missing a color component"); |
| 602 | return false; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | // Create a QImage of the correct type |
| 607 | if (jasperColorspaceFamily == JAS_CLRSPC_FAM_RGB) { |
| 608 | qtImage = QImage(qtWidth, qtHeight, hasAlpha |
| 609 | ? QImage::Format_ARGB32 |
| 610 | : QImage::Format_RGB32); |
| 611 | } else if (jasperColorspaceFamily == JAS_CLRSPC_FAM_GRAY) { |
| 612 | if (hasAlpha) { |
| 613 | qtImage = QImage(qtWidth, qtHeight, QImage::Format_ARGB32); |
| 614 | } else { |
| 615 | qtImage = QImage(qtWidth, qtHeight, QImage::Format_Grayscale8); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | // Copy data |
| 620 | if (oddComponentSubsampling) { |
| 621 | // This is a hack really, copying of data with component subsampling |
| 622 | // != 1 doesn't fit in with the rest of the scanline copying framework. |
| 623 | copyJasperQtGeneric(); |
| 624 | } else if (jasperColorspaceFamily == JAS_CLRSPC_FAM_RGB) { |
| 625 | if (hasAlpha) |
| 626 | copyJasperQt(&Jpeg2000JasperReader::copyScanlineJasperQtRGBA); |
| 627 | else |
| 628 | copyJasperQt(&Jpeg2000JasperReader::copyScanlineJasperQtRGB); |
| 629 | } else if (jasperColorspaceFamily == JAS_CLRSPC_FAM_GRAY) { |
| 630 | if (hasAlpha) |
| 631 | copyJasperQt(&Jpeg2000JasperReader::copyScanlineJasperQtGrayA); |
| 632 | else |
| 633 | copyJasperQt(&Jpeg2000JasperReader::copyScanlineJasperQtGray); |
| 634 | } |
| 635 | if (decodeOk) |
| 636 | *pImage = qtImage; |
| 637 | |
| 638 | return decodeOk; |
| 639 | } |
| 640 | |
| 641 | /*! |
| 642 | \internal |
| 643 | */ |
| 644 | void Jpeg2000JasperReader::copyJasperQtGeneric() |
| 645 | { |
| 646 | // Create scanline data poinetrs |
| 647 | jas_matrix_t **jasperMatrix; |
| 648 | jas_seqent_t **jasperRow; |
| 649 | createJasperMatrix(jasperMatrix); |
| 650 | jasperRow = (jas_seqent_t**)malloc(jasNumComponents * sizeof(jas_seqent_t *)); |
| 651 | Q_CHECK_PTR(jasperRow); |
| 652 | |
| 653 | int imageY = 0; |
| 654 | for (int componentY = 0; componentY < computedComponentHeight; ++componentY) { |
| 655 | for (int c = 0; c < jasNumComponents; ++c) { |
| 656 | jas_image_readcmpt(jasper_image, colorComponentMapping[c], 0, |
| 657 | componentY, computedComponentWidth, 1, |
| 658 | jasperMatrix[c]); |
| 659 | jasperRow[c] = jas_matrix_getref(jasperMatrix[c], 0, 0); |
| 660 | } |
| 661 | for (int verticalSubsample = 0; |
| 662 | verticalSubsample < computedComponentVerticalSubsampling; |
| 663 | ++verticalSubsample) { |
| 664 | uchar *scanLineUchar = qtImage.scanLine(imageY); |
| 665 | QRgb *scanLineQRgb = reinterpret_cast<QRgb *>(scanLineUchar); |
| 666 | for (int componentX = 0; componentX < computedComponentWidth; |
| 667 | ++componentX) { |
| 668 | for (int horizontalSubsample = 0; |
| 669 | horizontalSubsample < |
| 670 | computedComponentHorizontalSubsampling; |
| 671 | ++horizontalSubsample) { |
| 672 | if (jasperColorspaceFamily == JAS_CLRSPC_FAM_RGB) { |
| 673 | if (hasAlpha) { |
| 674 | *scanLineQRgb++ = (jasperRow[3][componentX] << 24) | |
| 675 | (jasperRow[0][componentX] << 16) | |
| 676 | (jasperRow[1][componentX] << 8) | |
| 677 | jasperRow[2][componentX]; |
| 678 | } else { |
| 679 | *scanLineQRgb++ = (jasperRow[0][componentX] << 16) | |
| 680 | (jasperRow[1][componentX] << 8) | |
| 681 | jasperRow[2][componentX]; |
| 682 | } |
| 683 | } else if (jasperColorspaceFamily == JAS_CLRSPC_FAM_GRAY) { |
| 684 | if (hasAlpha) { |
| 685 | *scanLineQRgb++ = (jasperRow[1][componentX] << 24) | |
| 686 | (jasperRow[0][componentX] << 16) | |
| 687 | (jasperRow[0][componentX] << 8) | |
| 688 | jasperRow[0][componentX]; |
| 689 | } else { |
| 690 | *scanLineUchar++ = jasperRow[0][componentX]; |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | ++imageY; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | /*! |
| 701 | \internal |
| 702 | Copies data from Jasper to QImage. The scanlineCopier parameter specifies |
| 703 | which function to use for handling each scan line. |
| 704 | */ |
| 705 | void Jpeg2000JasperReader::copyJasperQt(const ScanlineFunc scanlineCopier) |
| 706 | { |
| 707 | // Create scanline data poinetrs |
| 708 | jas_matrix_t **jasperMatrix; |
| 709 | jas_seqent_t **jasperRow; |
| 710 | |
| 711 | createJasperMatrix(jasperMatrix); |
| 712 | jasperRow = (jas_seqent_t**)malloc(jasNumComponents * sizeof(jas_seqent_t *)); |
| 713 | Q_CHECK_PTR(jasperRow); |
| 714 | |
| 715 | for (int scanline = 0; scanline < qtHeight; ++scanline) { |
| 716 | for (int c = 0; c < jasNumComponents; ++c) { |
| 717 | jas_image_readcmpt(jasper_image, colorComponentMapping[c], 0, |
| 718 | scanline, qtWidth, 1, jasperMatrix[c]); |
| 719 | jasperRow[c] = jas_matrix_getref(jasperMatrix[c], 0, 0); |
| 720 | } |
| 721 | (this->*scanlineCopier)(jasperRow, qtImage.scanLine(scanline)); |
| 722 | } |
| 723 | |
| 724 | freeJasperMatrix(jasperMatrix); |
| 725 | free(jasperRow); |
| 726 | } |
| 727 | |
| 728 | /*! |
| 729 | \internal |
| 730 | Copies RGB data from Jasper to a 32-bit QImage. |
| 731 | */ |
| 732 | void Jpeg2000JasperReader::copyScanlineJasperQtRGB( |
| 733 | jas_seqent_t ** const jasperRow, uchar *qtScanLine) |
| 734 | { |
| 735 | QRgb *scanLine = reinterpret_cast<QRgb *>(qtScanLine); |
| 736 | for (int c = 0; c < qtWidth; ++c) { |
| 737 | *scanLine++ = (0xFF << 24) | |
| 738 | (jasperRow[0][c] << 16) | |
| 739 | (jasperRow[1][c] << 8) | |
| 740 | jasperRow[2][c]; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | /*! |
| 745 | \internal |
| 746 | Copies RGBA data from Jasper to a 32-bit QImage. |
| 747 | */ |
| 748 | void Jpeg2000JasperReader::copyScanlineJasperQtRGBA(jas_seqent_t ** const jasperRow, uchar *qtScanLine) |
| 749 | { |
| 750 | QRgb *scanLine = reinterpret_cast<QRgb *>(qtScanLine); |
| 751 | for (int c = 0; c < qtWidth; ++c) { |
| 752 | *scanLine++ = (jasperRow[3][c] << 24) | |
| 753 | (jasperRow[0][c] << 16) | |
| 754 | (jasperRow[1][c] << 8) | |
| 755 | jasperRow[2][c]; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /*! |
| 760 | \internal |
| 761 | Copies data from a grayscale image to an 8-bit QImage. |
| 762 | */ |
| 763 | void Jpeg2000JasperReader::copyScanlineJasperQtGray(jas_seqent_t ** const jasperRow, uchar *qtScanLine) |
| 764 | { |
| 765 | for (int c = 0; c < qtWidth; ++c) { |
| 766 | // *qtScanLine++ = (jasperRow[0][c] >> (jasComponentPrecicion[0] - 8)); |
| 767 | *qtScanLine++ = jasperRow[0][c]; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /*! |
| 772 | \internal |
| 773 | Copies data from a grayscale image to a 32-bit QImage. |
| 774 | Note that in this case we use an 32-bit image for grayscale data, since the |
| 775 | alpha value is per-pixel, not per-color (per-color alpha is supported by |
| 776 | 8-bit QImage). |
| 777 | */ |
| 778 | void Jpeg2000JasperReader::copyScanlineJasperQtGrayA(jas_seqent_t ** const jasperRow, uchar *qtScanLine) |
| 779 | { |
| 780 | QRgb *scanLine = reinterpret_cast<QRgb *>(qtScanLine); |
| 781 | for (int c = 0; c < qtWidth; ++c) { |
| 782 | *scanLine++ = (jasperRow[1][c] << 24) | |
| 783 | (jasperRow[0][c] << 16) | |
| 784 | (jasperRow[0][c] << 8) | |
| 785 | jasperRow[0][c]; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /*! |
| 790 | Opens the file data and attempts to decode it using the Jasper library. |
| 791 | Returns true on success, false on failure. |
| 792 | |
| 793 | 32-bit and color mapped color images are encoded as RGB images, |
| 794 | color mapped grayscale images are encoded as grayscale images |
| 795 | */ |
| 796 | bool Jpeg2000JasperReader::write(const QImage &image, int quality) |
| 797 | { |
| 798 | if (!jasperOk) |
| 799 | return false; |
| 800 | |
| 801 | qtImage = image; |
| 802 | |
| 803 | qtHeight = qtImage.height(); |
| 804 | qtWidth = qtImage.width(); |
| 805 | qtDepth = qtImage.depth(); |
| 806 | |
| 807 | if (qtDepth == 32) { // RGB(A) |
| 808 | jasper_image = newRGBAImage(qtWidth, qtHeight, qtImage.hasAlphaChannel()); |
| 809 | if (!jasper_image) |
| 810 | return false; |
| 811 | |
| 812 | if (qtImage.hasAlphaChannel()) |
| 813 | copyQtJasper(&Jpeg2000JasperReader::copyScanlineQtJasperRGBA); |
| 814 | else |
| 815 | copyQtJasper(&Jpeg2000JasperReader::copyScanlineQtJasperRGB); |
| 816 | } else if (qtDepth == 8) { |
| 817 | // Color mapped grayscale |
| 818 | if (qtImage.allGray()) { |
| 819 | jasper_image = newGrayscaleImage(qtWidth, qtHeight, qtImage.hasAlphaChannel()); |
| 820 | if (!jasper_image) |
| 821 | return false; |
| 822 | |
| 823 | if (qtImage.hasAlphaChannel()) |
| 824 | copyQtJasper(&Jpeg2000JasperReader::copyScanlineQtJasperColormapGrayscaleA); |
| 825 | else |
| 826 | copyQtJasper(&Jpeg2000JasperReader::copyScanlineQtJasperColormapGrayscale); |
| 827 | } else { |
| 828 | // Color mapped color |
| 829 | jasper_image = newRGBAImage(qtWidth, qtHeight, qtImage.hasAlphaChannel()); |
| 830 | if (!jasper_image) |
| 831 | return false; |
| 832 | |
| 833 | if (qtImage.hasAlphaChannel()) |
| 834 | copyQtJasper(&Jpeg2000JasperReader::copyScanlineQtJasperColormapRGBA); |
| 835 | else |
| 836 | copyQtJasper(&Jpeg2000JasperReader::copyScanlineQtJasperColormapRGB); |
| 837 | } |
| 838 | } else { |
| 839 | qDebug("Unable to handle color depth %d", qtDepth); |
| 840 | return false; |
| 841 | } |
| 842 | |
| 843 | int fmtid; |
| 844 | if (format == Jp2Format) |
| 845 | fmtid = jas_image_strtofmt(const_cast<char*>("jp2")); |
| 846 | else /* if (format == J2cFormat) */ |
| 847 | // JasPer refers to the code stream format as jpc |
| 848 | fmtid = jas_image_strtofmt(const_cast<char*>("jpc")); |
| 849 | |
| 850 | const int minQuality = 0; |
| 851 | const int maxQuality = 100; |
| 852 | |
| 853 | if (quality == -1) |
| 854 | quality = 100; |
| 855 | if (quality <= minQuality) |
| 856 | quality = minQuality; |
| 857 | if (quality > maxQuality) |
| 858 | quality = maxQuality; |
| 859 | |
| 860 | // Qt specifies quality as an integer in the range 0..100. Jasper specifies |
| 861 | // compression rate as an real in the range 0..1, where 1 corresponds to no |
| 862 | // compression. Computing the rate from quality is difficult, large images |
| 863 | // get better image quality than small images at the same rate. If the rate |
| 864 | // is too low, Jasper will generate a completely black image. |
| 865 | // minirate is the smallest safe rate value. |
| 866 | const double minRate = 0.001; |
| 867 | |
| 868 | // maxRate specifies maximum target rate, which give the minimum amount |
| 869 | // of compression. Tests show that maxRates higer than 0.3 give no |
| 870 | // additional image quality for most images. Large images could use an even |
| 871 | // smaller maxRate value. |
| 872 | const double maxRate = 0.3; |
| 873 | |
| 874 | // Set jasperRate to a value in the range minRate..maxRate. Distribute the |
| 875 | // quality steps more densely at the lower end if the rate scale. |
| 876 | const double jasperRate = minRate + pow((double(quality) / double(maxQuality)), 2) * maxRate; |
| 877 | |
| 878 | // The Jasper format string contains two options: |
| 879 | // rate: rate=x |
| 880 | // lossy/lossless compression : mode=real/mode=int |
| 881 | QString jasperFormatString; |
| 882 | |
| 883 | // If quality is not maxQuality, we set lossy encoding. |
| 884 | // (lossless is default) |
| 885 | if (quality != maxQuality) { |
| 886 | jasperFormatString += QLatin1String("mode=real"); |
| 887 | jasperFormatString += QString(QLatin1String(" rate=%1")).arg(jasperRate); |
| 888 | } |
| 889 | |
| 890 | // Open an empty jasper stream that grows automatically |
| 891 | jas_stream_t * memory_stream = jas_stream_memopen(0, 0); |
| 892 | |
| 893 | // Jasper wants a non-const string. |
| 894 | char *str = qstrdup(jasperFormatString.toLatin1().constData()); |
| 895 | jas_image_encode(jasper_image, memory_stream, fmtid, str); |
| 896 | delete[] str; |
| 897 | jas_stream_flush(memory_stream); |
| 898 | |
| 899 | // jas_stream_t::obj_ is a void* which points to the stream implementation, |
| 900 | // e.g a file stream or a memory stream. But in our case we know that it is |
| 901 | // a memory stream since we created the object, so we just reiterpret_cast |
| 902 | // here.. |
| 903 | char *buffer = reinterpret_cast<char *>(reinterpret_cast<jas_stream_memobj_t*>(memory_stream->obj_)->buf_); |
| 904 | qint64 length = jas_stream_length(memory_stream); |
| 905 | ioDevice->write(buffer, length); |
| 906 | |
| 907 | jas_stream_close(memory_stream); |
| 908 | jas_image_destroy(jasper_image); |
| 909 | |
| 910 | return true; |
| 911 | } |
| 912 | |
| 913 | /*! |
| 914 | \internal |
| 915 | Copies data from qtImage to JasPer. The scanlineCopier parameter specifies |
| 916 | which function to use for handling each scan line. |
| 917 | */ |
| 918 | void Jpeg2000JasperReader::copyQtJasper(const ScanlineFuncWrite scanlinecopier) |
| 919 | { |
| 920 | // Create jasper matrix for holding one scanline |
| 921 | jas_matrix_t **jasperMatrix; |
| 922 | createJasperMatrix(jasperMatrix); |
| 923 | |
| 924 | for (int scanline = 0; scanline < qtHeight; ++scanline) { |
| 925 | (this->*scanlinecopier)(jasperMatrix, qtImage.scanLine(scanline)); |
| 926 | |
| 927 | // Write a scanline of data to jasper_image |
| 928 | for (int c = 0; c < jasNumComponents; ++c) |
| 929 | jas_image_writecmpt(jasper_image, c, 0, scanline, qtWidth, 1, |
| 930 | jasperMatrix[c]); |
| 931 | } |
| 932 | freeJasperMatrix(jasperMatrix); |
| 933 | } |
| 934 | |
| 935 | /*! |
| 936 | \internal |
| 937 | */ |
| 938 | void Jpeg2000JasperReader::copyScanlineQtJasperRGB(jas_matrix_t ** jasperRow, |
| 939 | uchar *qtScanLine) |
| 940 | { |
| 941 | QRgb *scanLineBuffer = reinterpret_cast<QRgb *>(qtScanLine); |
| 942 | for (int col = 0; col < qtWidth; ++col) { |
| 943 | jas_matrix_set(jasperRow[0], 0, col, (*scanLineBuffer & 0xFF0000) >> 16); |
| 944 | jas_matrix_set(jasperRow[1], 0, col, (*scanLineBuffer & 0x00FF00) >> 8); |
| 945 | jas_matrix_set(jasperRow[2], 0, col, *scanLineBuffer & 0x0000FF); |
| 946 | ++scanLineBuffer; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | /*! |
| 951 | \internal |
| 952 | */ |
| 953 | void Jpeg2000JasperReader::copyScanlineQtJasperRGBA(jas_matrix_t ** jasperRow, |
| 954 | uchar *qtScanLine) |
| 955 | { |
| 956 | QRgb *scanLineBuffer = reinterpret_cast<QRgb *>(qtScanLine); |
| 957 | for (int col = 0; col < qtWidth; ++col) { |
| 958 | jas_matrix_set(jasperRow[3], 0, col, (*scanLineBuffer & 0xFF000000) >> 24); |
| 959 | jas_matrix_set(jasperRow[0], 0, col, (*scanLineBuffer & 0x00FF0000) >> 16); |
| 960 | jas_matrix_set(jasperRow[1], 0, col, (*scanLineBuffer & 0x0000FF00) >> 8); |
| 961 | jas_matrix_set(jasperRow[2], 0, col, *scanLineBuffer & 0x000000FF); |
| 962 | ++scanLineBuffer; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | /*! |
| 967 | \internal |
| 968 | */ |
| 969 | void Jpeg2000JasperReader::copyScanlineQtJasperColormapRGB(jas_matrix_t ** jasperRow, |
| 970 | uchar *qtScanLine) |
| 971 | { |
| 972 | for (int col = 0; col < qtWidth; ++col) { |
| 973 | QRgb color = qtImage.color(*qtScanLine); |
| 974 | jas_matrix_set(jasperRow[0], 0, col, qRed(color)); |
| 975 | jas_matrix_set(jasperRow[1], 0, col, qGreen(color)); |
| 976 | jas_matrix_set(jasperRow[2], 0, col, qBlue(color)); |
| 977 | ++qtScanLine; |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | /*! |
| 982 | \internal |
| 983 | */ |
| 984 | void Jpeg2000JasperReader::copyScanlineQtJasperColormapRGBA(jas_matrix_t ** jasperRow, |
| 985 | uchar *qtScanLine) |
| 986 | { |
| 987 | for (int col = 0; col < qtWidth; ++col) { |
| 988 | QRgb color = qtImage.color(*qtScanLine); |
| 989 | jas_matrix_set(jasperRow[0], 0, col, qRed(color)); |
| 990 | jas_matrix_set(jasperRow[1], 0, col, qGreen(color)); |
| 991 | jas_matrix_set(jasperRow[2], 0, col, qBlue(color)); |
| 992 | jas_matrix_set(jasperRow[3], 0, col, qAlpha(color)); |
| 993 | ++qtScanLine; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | /*! |
| 998 | \internal |
| 999 | */ |
| 1000 | void Jpeg2000JasperReader::copyScanlineQtJasperColormapGrayscale(jas_matrix_t ** jasperRow, |
| 1001 | uchar *qtScanLine) |
| 1002 | { |
| 1003 | for (int col = 0; col < qtWidth; ++col) { |
| 1004 | QRgb color = qtImage.color(*qtScanLine); |
| 1005 | jas_matrix_set(jasperRow[0], 0, col, qGray(color)); |
| 1006 | ++qtScanLine; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | /*! |
| 1011 | \internal |
| 1012 | */ |
| 1013 | void Jpeg2000JasperReader::copyScanlineQtJasperColormapGrayscaleA(jas_matrix_t ** jasperRow, |
| 1014 | uchar *qtScanLine) |
| 1015 | { |
| 1016 | for (int col = 0; col < qtWidth; ++col) { |
| 1017 | QRgb color = qtImage.color(*qtScanLine); |
| 1018 | jas_matrix_set(jasperRow[0], 0, col, qGray(color)); |
| 1019 | jas_matrix_set(jasperRow[1], 0, col, qAlpha(color)); |
| 1020 | ++qtScanLine; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | /*! |
| 1025 | \internal |
| 1026 | Attempts to change the color space for the image to wantedColorSpace using |
| 1027 | the JasPer library |
| 1028 | */ |
| 1029 | bool Jpeg2000JasperReader::attemptColorspaceChange(int wantedColorSpace) |
| 1030 | { |
| 1031 | //qDebug("Attemting color space change"); |
| 1032 | jas_cmprof_t *outprof; |
| 1033 | if (!(outprof = jas_cmprof_createfromclrspc(wantedColorSpace))) |
| 1034 | return false; |
| 1035 | |
| 1036 | jas_image_t *newimage; |
| 1037 | if (!(newimage = jas_image_chclrspc(jasper_image, outprof, |
| 1038 | JAS_CMXFORM_INTENT_PER))) { |
| 1039 | jas_cmprof_destroy(outprof); |
| 1040 | return false; |
| 1041 | } |
| 1042 | jas_image_destroy(jasper_image); |
| 1043 | jas_cmprof_destroy(outprof); |
| 1044 | jasper_image = newimage; |
| 1045 | return true; |
| 1046 | } |
| 1047 | |
| 1048 | /*! |
| 1049 | \internal |
| 1050 | Set up a component with parameters suitable for storing a QImage. |
| 1051 | */ |
| 1052 | jas_image_cmptparm_t Jpeg2000JasperReader::createComponentMetadata( |
| 1053 | const int width, const int height) |
| 1054 | { |
| 1055 | jas_image_cmptparm_t param; |
| 1056 | param.tlx = 0; |
| 1057 | param.tly = 0; |
| 1058 | param.hstep = 1; |
| 1059 | param.vstep = 1; |
| 1060 | param.width = width; |
| 1061 | param.height = height; |
| 1062 | param.prec = 8; |
| 1063 | param.sgnd = 0; |
| 1064 | return param; |
| 1065 | } |
| 1066 | |
| 1067 | /*! |
| 1068 | \internal |
| 1069 | Create a new RGB JasPer image with a possible alpha channel. |
| 1070 | */ |
| 1071 | jas_image_t* Jpeg2000JasperReader::newRGBAImage(const int width, |
| 1072 | const int height, bool alpha) |
| 1073 | { |
| 1074 | jasNumComponents = alpha ? 4 : 3; |
| 1075 | jas_image_cmptparm_t *params = new jas_image_cmptparm_t[jasNumComponents]; |
| 1076 | jas_image_cmptparm_t param = createComponentMetadata(width, height); |
| 1077 | for (int c=0; c < jasNumComponents; c++) |
| 1078 | params[c] = param; |
| 1079 | jas_image_t *newImage = jas_image_create(jasNumComponents, params, |
| 1080 | JAS_CLRSPC_SRGB); |
| 1081 | |
| 1082 | if (!newImage) { |
| 1083 | delete[] params; |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | jas_image_setcmpttype(newImage, 0, JAS_IMAGE_CT_RGB_R); |
| 1088 | jas_image_setcmpttype(newImage, 1, JAS_IMAGE_CT_RGB_G); |
| 1089 | jas_image_setcmpttype(newImage, 2, JAS_IMAGE_CT_RGB_B); |
| 1090 | |
| 1091 | /* |
| 1092 | It is unclear how one stores opacity(alpha) components with JasPer, |
| 1093 | the following seems to have no effect. The opacity component gets |
| 1094 | type id 3 or 48 depending jp2 or j2c format no matter what one puts |
| 1095 | in here. |
| 1096 | |
| 1097 | The symbols are defined as follows: |
| 1098 | #define JAS_IMAGE_CT_RGB_R 0 |
| 1099 | #define JAS_IMAGE_CT_RGB_G 1 |
| 1100 | #define JAS_IMAGE_CT_RGB_B 2 |
| 1101 | #define JAS_IMAGE_CT_OPACITY 0x7FFF |
| 1102 | */ |
| 1103 | if (alpha) |
| 1104 | jas_image_setcmpttype(newImage, 3, JAS_IMAGE_CT_OPACITY); |
| 1105 | delete[] params; |
| 1106 | return newImage; |
| 1107 | } |
| 1108 | |
| 1109 | /*! |
| 1110 | \internal |
| 1111 | Create a new RGB JasPer image with a possible alpha channel. |
| 1112 | */ |
| 1113 | jas_image_t *Jpeg2000JasperReader::newGrayscaleImage(const int width, |
| 1114 | const int height, |
| 1115 | bool alpha) |
| 1116 | { |
| 1117 | jasNumComponents = alpha ? 2 : 1; |
| 1118 | jas_image_cmptparm_t param = createComponentMetadata(width, height); |
| 1119 | jas_image_t *newImage = jas_image_create(1, ¶m, JAS_CLRSPC_SGRAY); |
| 1120 | if (!newImage) |
| 1121 | return 0; |
| 1122 | |
| 1123 | jas_image_setcmpttype(newImage, 0, JAS_IMAGE_CT_GRAY_Y); |
| 1124 | |
| 1125 | // See corresponding comment for newRGBAImage. |
| 1126 | if (alpha) |
| 1127 | jas_image_setcmpttype(newImage, 1, JAS_IMAGE_CT_OPACITY); |
| 1128 | return newImage; |
| 1129 | } |
| 1130 | |
| 1131 | /*! |
| 1132 | \internal |
| 1133 | Allocate data structures that hold image data during transfer from the |
| 1134 | JasPer data structures to QImage. |
| 1135 | */ |
| 1136 | bool Jpeg2000JasperReader::createJasperMatrix(jas_matrix_t **&matrix) |
| 1137 | { |
| 1138 | matrix = (jas_matrix_t**)malloc(jasNumComponents * sizeof(jas_matrix_t *)); |
| 1139 | for (int c = 0; c < jasNumComponents; ++c) |
| 1140 | matrix[c] = jas_matrix_create(1, qtWidth); |
| 1141 | return true; |
| 1142 | } |
| 1143 | |
| 1144 | /*! |
| 1145 | \internal |
| 1146 | Free data structures that hold image data during transfer from the |
| 1147 | JasPer data structures to QImage. |
| 1148 | */ |
| 1149 | bool Jpeg2000JasperReader::freeJasperMatrix(jas_matrix_t **matrix) |
| 1150 | { |
| 1151 | for (int c = 0; c < jasNumComponents; ++c) |
| 1152 | jas_matrix_destroy(matrix[c]); |
| 1153 | free(matrix); |
| 1154 | return false; |
| 1155 | } |
| 1156 | |
| 1157 | /*! |
| 1158 | \internal |
| 1159 | */ |
| 1160 | void Jpeg2000JasperReader::printColorSpaceError() |
| 1161 | { |
| 1162 | QString colorspaceFamily, colorspaceSpecific; |
| 1163 | decodeColorSpace(jas_image_clrspc(jasper_image), colorspaceFamily, |
| 1164 | colorspaceSpecific); |
| 1165 | qDebug("Jpeg2000 decoder is not able to handle color space %s - %s", |
| 1166 | qPrintable(colorspaceFamily), qPrintable(colorspaceSpecific)); |
| 1167 | } |
| 1168 | /*! |
| 1169 | \internal |
| 1170 | */ |
| 1171 | bool Jpeg2000JasperReader::decodeColorSpace(int clrspc, QString &family, |
| 1172 | QString &specific) |
| 1173 | { |
| 1174 | int fam = jas_clrspc_fam(clrspc); |
| 1175 | int mbr = jas_clrspc_mbr(clrspc); |
| 1176 | |
| 1177 | switch (fam) { |
| 1178 | case 0: family = QLatin1String("JAS_CLRSPC_FAM_UNKNOWN"); break; |
| 1179 | case 1: family = QLatin1String("JAS_CLRSPC_FAM_XYZ"); break; |
| 1180 | case 2: family = QLatin1String("JAS_CLRSPC_FAM_LAB"); break; |
| 1181 | case 3: family = QLatin1String("JAS_CLRSPC_FAM_GRAY"); break; |
| 1182 | case 4: family = QLatin1String("JAS_CLRSPC_FAM_RGB"); break; |
| 1183 | case 5: family = QLatin1String("JAS_CLRSPC_FAM_YCBCR"); break; |
| 1184 | default: family = QLatin1String("Unknown"); return false; |
| 1185 | } |
| 1186 | |
| 1187 | switch (mbr) { |
| 1188 | case 0: |
| 1189 | switch (fam) { |
| 1190 | case 1: specific = QLatin1String("JAS_CLRSPC_CIEXYZ"); break; |
| 1191 | case 2: specific = QLatin1String("JAS_CLRSPC_CIELAB"); break; |
| 1192 | case 3: specific = QLatin1String("JAS_CLRSPC_SGRAY"); break; |
| 1193 | case 4: specific = QLatin1String("JAS_CLRSPC_SRGB"); break; |
| 1194 | case 5: specific = QLatin1String("JAS_CLRSPC_SYCBCR"); break; |
| 1195 | default: specific = QLatin1String("Unknown"); return false; |
| 1196 | } |
| 1197 | break; |
| 1198 | case 1: |
| 1199 | switch (fam) { |
| 1200 | case 3: specific = QLatin1String("JAS_CLRSPC_GENGRAY"); break; |
| 1201 | case 4: specific = QLatin1String("JAS_CLRSPC_GENRGB"); break; |
| 1202 | case 5: specific = QLatin1String("JAS_CLRSPC_GENYCBCR"); break; |
| 1203 | default: specific = QLatin1String("Unknown"); return false; |
| 1204 | } |
| 1205 | break; |
| 1206 | default: |
| 1207 | return false; |
| 1208 | } |
| 1209 | return true; |
| 1210 | } |
| 1211 | /*! |
| 1212 | \internal |
| 1213 | */ |
| 1214 | void Jpeg2000JasperReader::printMetadata(jas_image_t *image) |
| 1215 | { |
| 1216 | #ifndef QT_NO_DEBUG |
| 1217 | // jas_image_cmptparm_t param |
| 1218 | qDebug("Image width: %ld", long(jas_image_width(image))); |
| 1219 | qDebug("Image height: %ld", long(jas_image_height(image))); |
| 1220 | qDebug("Coordinates on reference grid: (%ld,%ld) (%ld,%ld)", |
| 1221 | long(jas_image_tlx(image)), long(jas_image_tly(image)), |
| 1222 | long(jas_image_brx(image)), long(jas_image_bry(image))); |
| 1223 | qDebug("Number of image components: %d", jas_image_numcmpts(image)); |
| 1224 | |
| 1225 | QString colorspaceFamily; |
| 1226 | QString colorspaceSpecific; |
| 1227 | decodeColorSpace(jas_image_clrspc(image), colorspaceFamily, colorspaceSpecific); |
| 1228 | qDebug("Color model (space): %d, %s - %s", jas_image_clrspc(image), |
| 1229 | qPrintable(colorspaceFamily), qPrintable(colorspaceSpecific)); |
| 1230 | |
| 1231 | qDebug("Component metadata:"); |
| 1232 | |
| 1233 | for (int c = 0; c < jas_image_numcmpts(image); ++c) { |
| 1234 | qDebug("Component %d:", c); |
| 1235 | qDebug(" Component type: %ld", long(jas_image_cmpttype(image, c))); |
| 1236 | qDebug(" Width: %ld", long(jas_image_cmptwidth(image, c))); |
| 1237 | qDebug(" Height: %ld", long(jas_image_cmptheight(image, c))); |
| 1238 | qDebug(" Signedness: %d", jas_image_cmptsgnd(image, c)); |
| 1239 | qDebug(" Precision: %d", jas_image_cmptprec(image, c)); |
| 1240 | qDebug(" Horizontal subsampling factor: %ld",long(jas_image_cmpthstep(image, c))); |
| 1241 | qDebug(" Vertical subsampling factor: %ld", long(jas_image_cmptvstep(image, c))); |
| 1242 | qDebug(" Coordinates on reference grid: (%ld,%ld) (%ld,%ld)", |
| 1243 | long(jas_image_cmpttlx(image, c)), long(jas_image_cmpttly(image, c)), |
| 1244 | long(jas_image_cmptbrx(image, c)), long(jas_image_cmptbry(image, c))); |
| 1245 | } |
| 1246 | #else |
| 1247 | Q_UNUSED(image); |
| 1248 | #endif |
| 1249 | } |
| 1250 | |
| 1251 | QT_END_NAMESPACE |