void ut_load::testLoadSameFilterTwice() { QTemporaryFile testFile; testFile.open(); QImage greenTestImage("/usr/share/quillimagefilter-tests/images/854x480_blue.png"); // Verify whether image exists QVERIFY(!greenTestImage.isNull()); greenTestImage.save(testFile.fileName(), "jpg"); QuillImage target = QuillImage(); target.setFullImageSize(QSize(854, 480)); target.setArea(QRect(0, 0, 854, 480)); QuillImage target2 = QuillImage(); target2.setFullImageSize(QSize(854, 480)); target2.setArea(QRect(0, 0, 854, 480)); QuillImageFilter *filter = QuillImageFilterFactory::createImageFilter("org.maemo.load"); filter->setOption(QuillImageFilter::FileName, testFile.fileName()); // Passing JPEG through the filter QuillImage imageAfter = filter->apply(target); // Passing JPEG through the filter QuillImage imageAfter2 = filter->apply(target2); // Checking the red channel value which should be always bigger than that of greeen channel QVERIFY( qBlue(imageAfter.pixel(QPoint(0, 0))) > qGreen(imageAfter.pixel(QPoint(0, 0))) ); // Checking the red channel value which should be always bigger than that of greeen channel QVERIFY( qBlue(imageAfter2.pixel(QPoint(0, 0))) > qGreen(imageAfter2.pixel(QPoint(0, 0))) ); delete filter; }
// Such a strong downscaling that some tiles get resized into 0x0 void ut_scale::testScaleToMinimal() { QuillImageFilter *filter = QuillImageFilterFactory::createImageFilter("org.maemo.scale"); QVERIFY(filter->setOption(QuillImageFilter::SizeAfter, QSize(2, 1))); QImage image = Unittests::generatePaletteImage(); QuillImage tile1 = image.copy(0, 0, 2, 2); tile1.setFullImageSize(QSize(8, 2)); tile1.setArea(QRect(0, 0, 2, 2)); QuillImage tile2 = image.copy(2, 0, 2, 2); tile2.setFullImageSize(QSize(8, 2)); tile2.setArea(QRect(2, 0, 2, 2)); QuillImage tile3 = image.copy(4, 0, 2, 2); tile3.setFullImageSize(QSize(8, 2)); tile3.setArea(QRect(4, 0, 2, 2)); QuillImage tile4 = image.copy(6, 0, 2, 2); tile4.setFullImageSize(QSize(8, 2)); tile4.setArea(QRect(6, 0, 2, 2)); QuillImage resultTile1 = filter->apply(tile1); QCOMPARE(resultTile1.area(), QRect()); QCOMPARE(resultTile1.fullImageSize(), QSize(2, 1)); QCOMPARE(resultTile1, QuillImage()); QuillImage resultTile2 = filter->apply(tile2); QCOMPARE(resultTile2.area(), QRect(0, 0, 1, 1)); QCOMPARE(resultTile2.fullImageSize(), QSize(2, 1)); QCOMPARE((QImage)resultTile2, tile2.scaled(QSize(1, 1), Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); QuillImage resultTile3 = filter->apply(tile3); QCOMPARE(resultTile3.area(), QRect()); QCOMPARE(resultTile3.fullImageSize(), QSize(2, 1)); QCOMPARE(resultTile3, QuillImage()); QuillImage resultTile4 = filter->apply(tile4); QCOMPARE(resultTile4.area(), QRect(1, 0, 1, 1)); QCOMPARE(resultTile4.fullImageSize(), QSize(2, 1)); QCOMPARE((QImage)resultTile4, tile4.scaled(QSize(1, 1), Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); delete filter; }
void ut_load::testQImage() { QFile file("/usr/share/quillimagefilter-tests/images/16_color_palette.png"); QuillImageFilter *filter = new QuillImageFilter("com.meego.load"); QVERIFY(filter); QCOMPARE(filter->name(), QString("com.meego.load")); QVERIFY(filter->supportsOption(QuillImageFilter::FileName)); QVERIFY(filter->supportsOption(QuillImageFilter::FileFormat)); QVERIFY(filter->supportsOption(QuillImageFilter::DisableUndo)); QVERIFY(filter->supportsOption(QuillImageFilter::SizeAfter)); filter->setOption(QuillImageFilter::FileName, QVariant(file.fileName())); filter->setOption(QuillImageFilter::MimeType, QVariant("image/png")); QCOMPARE(filter->option(QuillImageFilter::FileName).toString(), file.fileName()); QCOMPARE(filter->option(QuillImageFilter::MimeType).toString(), QString("image/png")); QCOMPARE(filter->option(QuillImageFilter::FileFormat).toString(), QString("png")); QImage image(file.fileName()); QImage loadedImage = filter->apply(image); QVERIFY(Unittests::compareImage(loadedImage, QuillImage(image))); delete filter; }
// Test case: tiling approach with just a single tile void ut_tileloading::testSingleTile() { QTemporaryFile testFile; testFile.open(); Unittests::generatePaletteImage().save(testFile.fileName(), "png"); QImage originalImage = Unittests::generatePaletteImage(); Quill::setDefaultTileSize(QSize(10, 10)); QuillFile *file = new QuillFile(testFile.fileName()); file->setViewPort(QRect(0, 0, 8, 2)); file->setDisplayLevel(1); Quill::releaseAndWait(); Quill::releaseAndWait(); QCOMPARE(file->allImageLevels().count(), 2); QuillImage image = file->allImageLevels().at(1); QVERIFY(Unittests::compareImage(image, QuillImage(Unittests::generatePaletteImage()))); delete file; }
QuillImage QuillImageFilterGenerator::apply(const QuillImage &image) const { QuillImageFilterGeneratorImplementation *impl = dynamic_cast<QuillImageFilterGeneratorImplementation*>(handler()); if (impl) { priv->m_result = impl->generate(image); } return QuillImage(); }
QuillImage TiltShift::apply(const QuillImage& image) const { if (image.isNull()) { return image; } if (image.isFragment()) { m_gaussianFilter->setOption(QuillImageFilter::Radius, QVariant(GAUSSIAN_BLUR_RADIUS_TILE)); } else if (image.size() == QSize(170, 170)) { m_gaussianFilter->setOption(QuillImageFilter::Radius, QVariant(GAUSSIAN_BLUR_RADIUS_THUMBNAIL)); } else { m_gaussianFilter->setOption(QuillImageFilter::Radius, QVariant(GAUSSIAN_BLUR_RADIUS_PREVIEW)); } QuillImage blurredImage = m_gaussianFilter->apply(image); float** mask = maskImage(image); QImage resultImage(image.size(), QImage::Format_RGB32); for (int y=0; y<image.height(); y++) { const QRgb* originalPixelRow = (const QRgb*)image.constScanLine(y); const QRgb* blurredPixelRow = (const QRgb*)blurredImage.constScanLine(y); QRgb* resultPixelRow = (QRgb*)resultImage.scanLine(y); for (int x=0; x<image.width(); x++) { int red = qRed(originalPixelRow[x]) * mask[x][y] + qRed(blurredPixelRow[x]) * (1 - mask[x][y]); int blue = qBlue(originalPixelRow[x]) * mask[x][y] + qBlue(blurredPixelRow[x]) * (1 - mask[x][y]); int green = qGreen(originalPixelRow[x]) * mask[x][y] + qGreen(blurredPixelRow[x]) * (1 - mask[x][y]); QColor result(red, green, blue); result.setHsvF(result.hueF(), qMin(SATURATION_FACTOR * result.saturationF(), 1.0), result.valueF()); resultPixelRow[x] = result.rgb(); } } for (int i = 0; i < image.size().width(); i ++) { delete[] mask[i]; } delete[] mask; return QuillImage(image, resultImage); }
void ut_load::testExifRotation() { QTemporaryFile testFile; testFile.open(); QImage image("/usr/share/quillimagefilter-tests/images/16_color_palette.png"); image.save(testFile.fileName(), "jpeg"); QuillMetadata metadata; metadata.setEntry(QuillMetadata::Tag_Orientation, QVariant(6)); metadata.write(testFile.fileName()); QuillImageFilter *filter = QuillImageFilterFactory::createImageFilter("org.maemo.load"); filter->setOption(QuillImageFilter::FileName, QString(testFile.fileName())); QuillImage resultImage = filter->apply(QuillImage()); QCOMPARE(resultImage.size(), QSize(2, 8)); delete filter; }
void ut_load::testCropping() { QuillImageFilter *filter = QuillImageFilterFactory::createImageFilter("org.maemo.load"); QFile file("/usr/share/quillimagefilter-tests/images/16_color_palette.png"); filter->setOption(QuillImageFilter::FileName, QString(file.fileName())); QuillImage target = QuillImage(); target.setTargetSize(QSize(1, 1)); target.setFullImageSize(QSize(8, 2)); target.setArea(QRect(2, 0, 2, 2)); QuillImage result = filter->apply(target); QCOMPARE(result.size(), QSize(1, 1)); // Expect an average of the given area (png = smooth scaling). QCOMPARE(qRed(result.pixel(QPoint(0,0))), 96); QCOMPARE(qGreen(result.pixel(QPoint(0,0))), 193); QCOMPARE(qBlue(result.pixel(QPoint(0,0))), 0); delete filter; }
QuillImage SaveFilter::saveJpeg(const QuillImage &image) const { // For thumbnail saving, set save target size to the thumbnail size QSize targetSize = image.fullImageSize(); if (!image.isFragment()) targetSize = image.size(); if (priv->serialSaver == 0) priv->serialSaver = new SerialSaver(priv->fileName, targetSize, priv->rawExifData); bool result = priv->serialSaver->process(image); priv->tileCount--; setFileModificationDateTime(); if (result) return image; else return QuillImage(); }
QuillImage LoadFilter::apply(const QuillImage &tile) const { if (priv->fileFormatQt.isEmpty() && !priv->isInvalid) const_cast<LoadFilter *>(this)->detectFormat(); if (priv->isInvalid) return QuillImage(); bool isNew = !reader || (filterAddress != this) || !priv->bufferInitialized || !tile.isFragment() || (priv->fileFormatQt != reader->format()) || (!tile.targetSize().isEmpty() && (tile.targetSize() != tile.area().size())); filterAddress = const_cast<LoadFilter*>(this); priv->bufferInitialized = true; if (priv->iODevice) { delete reader; reader = new QImageReader(priv->iODevice, priv->fileFormatQt.toAscii()); if (!priv->fileFormatQt.isEmpty()) reader->setFormat(priv->fileFormatQt.toAscii()); } else { if (isNew) { delete reader; reader = 0; if (!const_cast<LoadFilter*>(this)->readFileToByteArray()) return QuillImage(); buffer.seek(0); priv->orientation = readOrientation(); reader = new QImageReader(&buffer, priv->fileFormatQt.toAscii()); if (!priv->fileFormatQt.isEmpty()) reader->setFormat(priv->fileFormatQt.toAscii()); } else { buffer.seek(0); } } QuillImage input = tile; if (input.fullImageSize().isEmpty()) input.setFullImageSize(reader->size()); if (input.area().isEmpty()) input.setArea(QRect(QPoint(0, 0), input.fullImageSize())); QImage newImage; QSize targetSize = input.targetSize(); QSize fullImageSize = input.fullImageSize(); QRect area = rotateArea(fullImageSize, input.area(), input); if ((priv->orientation == Exif_Orientation_Rotated90) || (priv->orientation == Exif_Orientation_Rotated270)) { targetSize.transpose(); fullImageSize.transpose(); } /* Resetting reader for ScaledSize and ClipRect. This resetting will make sure that the reader will have all the options set correctly if the same reader has been used previously. Does not do the calculation in all cases since it is time consuming. */ if (reader->scaledSize().isValid() || reader->clipRect().isValid()) { QSize readerSize = reader->size(); reader->setScaledSize(readerSize); reader->setClipRect(QRect(QPoint(0,0),readerSize)); } if (!input.isFragment()){ fullImage = QImage(); if (!targetSize.isEmpty()) { reader->setScaledSize(targetSize); } newImage = readFromReader(); } else if (!targetSize.isEmpty() && (targetSize != area.size())) { // Both cropping and scaling have been requested // Current implementation: ask plugin for the scaled image, then crop // This can be later optimized with a plugin that effectively // supports the ScaledClipRect option. reader->setScaledSize(QSize(targetSize.width() * fullImageSize.width() / area.width(), targetSize.height() * fullImageSize.height() / area.height())); newImage = readFromReader(); newImage = newImage.copy(input.area().left() * input.targetSize().width() / input.area().width(), input.area().top() * input.targetSize().height() / input.area().height(), input.targetSize().width(), input.targetSize().height()); newImage = QuillImage(input, newImage); } else if (reader->supportsOption(QImageIOHandler::ClipRect) && // Standard Qt now supports ClipRect with a too slow // implementation, so we use the ScaledClipRect information // to reject the standard Qt Jpeg plugin. // TODO: This needs to be removed if Qt becomes faster. !reader->supportsOption(QImageIOHandler::ScaledClipRect)) { fullImage = QImage(); //if it is real a tile, we then reset reader, otherwise we consider it as a full image if(!area.isNull()){ //set the scale size again here because preview sets the preview scale size already. reader->setScaledSize(area.size()); // clip rect support in image reader reader->setClipRect(area); } newImage = readFromReader(); } else { // no clip rect support: preserve whole full image as a tile cache, // uses a lot of extra memory if (isNew || fullImage.isNull()) fullImage = readFromReader(); newImage = fullImage.copy(input.area()); } QImage convertedImage; if (newImage.hasAlphaChannel()){ convertedImage = renderAlpha(newImage, priv->backgroundColor); } else{ convertedImage = newImage.convertToFormat(QImage::Format_RGB32); } QuillImage ret_value; if (!input.targetSize().isEmpty() || !input.fullImageSize().isEmpty() || !input.area().isEmpty()) { ret_value = QuillImage(input, convertedImage); } else{ // full image ret_value = QuillImage(convertedImage); } if (priv->iODevice) { delete reader; reader = 0; } return ret_value; }
QuillImageFilter *RedEyeDetection::generate(const QuillImage &image) const { // No reference image given; no operations possible. if (image == QuillImage()) return 0; // Scale the search rectangle from full-image coordinates to current // image. QRect rect = getStartingRectangle(image.size(), image.fullImageSize()); // Detect best possible starting point QPoint center = locateStartingPoint(image, rect); // No possible red-eye pixels found - aborting (this should // generally not happen unless you select the blue sky or // something) if (center == QPoint()) return 0; // Generate legal area around center int radius = priv->eyeRadius * qMin(image.size().width(), image.size().height()) / qMin(image.fullImageSize().width(), image.fullImageSize().height()); QRect area = QRect(center.x()-radius, center.y()-radius, radius*2+1, radius*2+1); area &= image.rect(); // Flood fill around selected center // If overflows, tighten the criteria and try again. QSet<QPoint> redEye; for (int phase = 0; phase < 5; phase++) { setThreshold(phase); redEye = expandRedEye(image, center, area); if (!redEye.isEmpty()) break; } // Still overflows - abandon if (redEye.isEmpty()) return 0; QPolygon polygon = generatePolygon(area, redEye); // For some reason, polygon was empty (this should never happen) if (polygon == QPolygon()) return 0; // If this was a preview, scale the plugin to big-picture coordinates if ((image.fullImageSize() != QSize()) && image.fullImageSize() != image.size()) polygon = scalePolygon(polygon, image.size(), image.fullImageSize()); // All is well. QuillImageFilter *filter = QuillImageFilterFactory::createImageFilter(QuillImageFilter::Name_RedEyeReduction); if (!filter) return 0; filter->setOption(QuillImageFilter::SelectionArea, polygon); return filter; }
QuillImage SaveFilter::apply(const QuillImage &image) const { if(image.isNull()) return QuillImage(); // Simple format detection (1. given format 2. suffix) QString format = priv->fileFormatQt.toLatin1(); if (format.isEmpty()) format = QFileInfo(priv->fileName).suffix().toLatin1(); // Jpeg always uses serial access saving, also for non-tiles, // to be able to insert EXIF data format = format.toLower(); if ((format == "jpeg") || (format == "jpg")) return saveJpeg(image); // For non-tiles, just save if (!image.isFragment()) { bool ok = saveFullImage(image); if (ok) { setFileModificationDateTime(); return image; } else return QuillImage(); } // First execute - prepare target if (priv->fullImageSize == QSize()) { priv->fullImageSize = image.fullImageSize(); priv->fullImage = QImage(priv->fullImageSize, QImage::Format_RGB32); } QPainter painter(&(priv->fullImage)); painter.setCompositionMode(QPainter::CompositionMode_Source); painter.drawImage(image.area().topLeft(), image); priv->tileCount--; // Target full image has everything necessary, so do the final save. if (priv->tileCount == 0) { bool ok = saveFullImage(priv->fullImage); if (ok) { setFileModificationDateTime(); // Return non-empty image to represent success return image; } else // Return empty image to represent failure return QuillImage(); } else // No actual save was made, return non-empty image to represent success return image; }