From: Ivailo Monev Date: Sun, 6 Mar 2022 20:08:44 +0000 (+0200) Subject: skanlite: save images in Katie format X-Git-Tag: 4.22.0~345 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=9bd9504a3f06e9425071e5444f9ab17c92b25c46;p=kde%2Fkde-extraapps.git skanlite: save images in Katie format and since KSaneWidget::toQImage() can handle KSaneIface::KSaneWidget::FormatRGB_16_C and KSaneIface::KSaneWidget::FormatGrayScale16 save all images via it getting rid of the png dependency Signed-off-by: Ivailo Monev --- diff --git a/appveyor.yml b/appveyor.yml index 286001bd..509e4daa 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -24,7 +24,7 @@ build_script: libavcodec-dev libavformat-dev libavutil-dev libblkid-dev libcups2-dev \ libdjvulibre-dev libepub-dev libexiv2-dev libfreetype6-dev \ libgcrypt20-dev libgettextpo-dev libgmp-dev libmms-dev \ - libjpeg-dev libpng-dev libparted-dev libqalculate-dev \ + libjpeg-dev libparted-dev libqalculate-dev \ libsane-dev libspectre-dev libspeechd-dev libswscale-dev libtag1-dev \ uuid-dev libudev-dev libvncserver-dev freerdp2-dev \ libboost-dev libffmpegthumbnailer-dev libbz2-dev libtorrent-rasterbar-dev \ diff --git a/skanlite/CMakeLists.txt b/skanlite/CMakeLists.txt index 7536353e..88c0605a 100644 --- a/skanlite/CMakeLists.txt +++ b/skanlite/CMakeLists.txt @@ -7,8 +7,6 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS}) endif() -find_package(PNG REQUIRED) - set(SKANLITE_VERSION_MAJOR "1") set(SKANLITE_VERSION_MINOR "1") set(SKANLITE_VERSION_STRING "${SKANLITE_VERSION_MAJOR}.${SKANLITE_VERSION_MINOR}") diff --git a/skanlite/src/CMakeLists.txt b/skanlite/src/CMakeLists.txt index 280fc0b8..5bb58a89 100644 --- a/skanlite/src/CMakeLists.txt +++ b/skanlite/src/CMakeLists.txt @@ -7,7 +7,6 @@ set(skanlite_SRCS main.cpp skanlite.cpp ImageViewer.cpp - KSaneImageSaver.cpp SaveLocation.cpp settings.ui SaveLocation.ui @@ -15,7 +14,15 @@ set(skanlite_SRCS add_executable(skanlite ${skanlite_SRCS}) -target_link_libraries(skanlite ${KDE4_KIO_LIBS} ${PNG_LIBRARY} ksane) +target_link_libraries(skanlite + ${KDE4_KIO_LIBS} + ksane +) -install(TARGETS skanlite ${INSTALL_TARGETS_DEFAULT_ARGS}) -install( PROGRAMS skanlite.desktop DESTINATION ${KDE4_XDG_APPS_INSTALL_DIR} ) +install( + TARGETS skanlite ${INSTALL_TARGETS_DEFAULT_ARGS} +) +install( + PROGRAMS skanlite.desktop + DESTINATION ${KDE4_XDG_APPS_INSTALL_DIR} +) diff --git a/skanlite/src/KSaneImageSaver.cpp b/skanlite/src/KSaneImageSaver.cpp deleted file mode 100644 index c8729322..00000000 --- a/skanlite/src/KSaneImageSaver.cpp +++ /dev/null @@ -1,236 +0,0 @@ -/* ============================================================ -* Date : 2010-07-02 -* Description : Image saver class for libksane image data. -* -* Copyright (C) 2010-2012 by Kåre Särs -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License as -* published by the Free Software Foundation; either version 2 of -* the License or (at your option) version 3 or any later version -* accepted by the membership of KDE e.V. (or its successor approved -* by the membership of KDE e.V.), which shall act as a proxy -* defined in Section 14 of version 3 of the license. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License. -* along with this program. If not, see -* -* ============================================================ */ - -#include "KSaneImageSaver.h" - -#include - -#include -#include - - -struct KSaneImageSaver::Private -{ - enum ImageType { - ImageTypePNG, - ImageTypeTIFF - }; - - bool m_savedOk; - QMutex m_runMutex; - KSaneImageSaver *q; - - QString m_name; - QByteArray m_data; - int m_width; - int m_height; - int m_format; - ImageType m_type; - - bool savePng(); - bool saveTiff(); -}; - -// ------------------------------------------------------------------------ -KSaneImageSaver::KSaneImageSaver(QObject *parent) : QThread(parent), d(new Private) -{ - d->q = this; -} - -// ------------------------------------------------------------------------ -KSaneImageSaver::~KSaneImageSaver() -{ - delete d; -} - -bool KSaneImageSaver::savePng(const QString &name, const QByteArray &data, int width, int height, int format) -{ - if (!d->m_runMutex.tryLock()) return false; - - d->m_name = name; - d->m_data = data; - d->m_width = width; - d->m_height = height; - d->m_format = format; - d->m_type = Private::ImageTypePNG; - - start(); - return true; -} - -bool KSaneImageSaver::savePngSync(const QString &name, const QByteArray &data, int width, int height, int format) -{ - if (!savePng(name, data, width, height, format)) { - kDebug() << "fail"; - return false; - } - wait(); - return d->m_savedOk; -} - -bool KSaneImageSaver::saveTiff(const QString &name, const QByteArray &data, int width, int height, int format) -{ - if (!d->m_runMutex.tryLock()) return false; - - d->m_name = name; - d->m_data = data; - d->m_width = width; - d->m_height = height; - d->m_format = format; - d->m_type = Private::ImageTypeTIFF; - - kDebug() << "saving Tiff images is not yet supported"; - d->m_runMutex.unlock(); - return false; -} - -bool KSaneImageSaver::saveTiffSync(const QString &name, const QByteArray &data, int width, int height, int format) -{ - if (!saveTiff(name, data, width, height, format)) { - return false; - } - wait(); - return d->m_savedOk; -} - -void KSaneImageSaver::run() -{ - if (d->m_type == Private::ImageTypeTIFF) { - d->m_savedOk = d->saveTiff(); - emit imageSaved(d->m_savedOk); - } - else { - d->m_savedOk = d->savePng(); - emit imageSaved(d->m_savedOk); - } - d->m_runMutex.unlock(); -} - -bool KSaneImageSaver::Private::saveTiff() -{ - return false; -} - -bool KSaneImageSaver::Private::savePng() -{ - FILE *file; - png_structp png_ptr; - png_infop info_ptr; - png_color_8 sig_bit; - png_bytep row_ptr; - int bytesPerPixel; - - // open the file - file = fopen(qPrintable(m_name), "wb"); - if (!file) return false; - - // create the png struct - png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (!png_ptr) { - fclose(file); - return false; - } - - // create the image information srtuct - info_ptr = png_create_info_struct(png_ptr); - if (!png_ptr) { - fclose(file); - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); - return false; - } - - // initialize IO - png_init_io(png_ptr, file); - - // set the image attributes - switch ((KSaneIface::KSaneWidget::ImageFormat)m_format) - { - case KSaneIface::KSaneWidget::FormatGrayScale16: - png_set_IHDR(png_ptr, info_ptr, m_width, m_height, 16, PNG_COLOR_TYPE_GRAY, - PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); - sig_bit.gray = 16; - bytesPerPixel = 2; - break; - case KSaneIface::KSaneWidget::FormatRGB_16_C: - png_set_IHDR(png_ptr, info_ptr, m_width, m_height, 16, PNG_COLOR_TYPE_RGB, - PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); - sig_bit.red = 16; - sig_bit.green = 16; - sig_bit.blue = 16; - bytesPerPixel = 6; - break; - default: - fclose(file); - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); - return false; - } - - png_set_sBIT(png_ptr, info_ptr, &sig_bit); - - /* Optionally write comments into the image */ -// text_ptr[0].key = "Title"; -// text_ptr[0].text = "Mona Lisa"; -// text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE; -// text_ptr[1].key = "Author"; -// text_ptr[1].text = "Leonardo DaVinci"; -// text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE; -// text_ptr[2].key = "Description"; -// text_ptr[2].text = ""; -// text_ptr[2].compression = PNG_TEXT_COMPRESSION_zTXt; -// png_set_text(png_ptr, info_ptr, text_ptr, 2); - - /* Write the file header information. */ - png_write_info(png_ptr, info_ptr); - - //png_set_shift(png_ptr, &sig_bit); - //png_set_packing(png_ptr); - - png_set_compression_level(png_ptr, 9); - - // This is not nice :( swap bytes in the 16 bit color.... - char tmp; - // Make sure we have a buffer size that is divisible by 2 - int dataSize = m_data.size(); - if ((dataSize % 2) > 0) dataSize--; - for (int i=0; i -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License as -* published by the Free Software Foundation; either version 2 of -* the License or (at your option) version 3 or any later version -* accepted by the membership of KDE e.V. (or its successor approved -* by the membership of KDE e.V.), which shall act as a proxy -* defined in Section 14 of version 3 of the license. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License. -* along with this program. If not, see -* -* ============================================================ */ - -#ifndef KSaneImageSaver_h -#define KSaneImageSaver_h - -#include - -#include - -class KSaneImageSaver : public QThread -{ - Q_OBJECT - public: - KSaneImageSaver(QObject *parent = 0); - ~KSaneImageSaver(); - - bool savePng(const QString &name, const QByteArray &data, int width, int height, int format); - - bool savePngSync(const QString &name, const QByteArray &data, int width, int height, int format); - - bool saveTiff(const QString &name, const QByteArray &data, int width, int height, int format); - - bool saveTiffSync(const QString &name, const QByteArray &data, int width, int height, int format); - - Q_SIGNALS: - void imageSaved(bool success); - - protected: - void run(); - - private: - struct Private; - Private * const d; - -}; - -#endif - - diff --git a/skanlite/src/skanlite.cpp b/skanlite/src/skanlite.cpp index d0605994..1a7f33d2 100644 --- a/skanlite/src/skanlite.cpp +++ b/skanlite/src/skanlite.cpp @@ -23,14 +23,12 @@ #include "skanlite.h" #include "moc_skanlite.cpp" - -#include "KSaneImageSaver.h" #include "SaveLocation.h" - #include #include #include +#include #include #include @@ -49,6 +47,12 @@ #include +#if QT_VERSION >= 0x041200 +static const QByteArray imageFormat = QImageWriter::defaultImageFormat(); +#else +static const QByteArray imageFormat = "png"; +#endif + Skanlite::Skanlite(const QString &device, QWidget *parent) : KDialog(parent) { @@ -98,16 +102,6 @@ Skanlite::Skanlite(const QString &device, QWidget *parent) // add the supported image types m_filterList = KImageIO::mimeTypes(KImageIO::Writing); - // Put first class citizens at first place - m_filterList.removeAll("image/jpeg"); - m_filterList.removeAll("image/tiff"); - m_filterList.removeAll("image/png"); - m_filterList.insert(0, "image/png"); - m_filterList.insert(1, "image/jpeg"); - m_filterList.insert(2, "image/tiff"); - - m_filter16BitList << "image/png"; - //m_filter16BitList << "image/tiff"; QStringList type; foreach (const QString &mime , m_filterList) { @@ -224,7 +218,7 @@ void Skanlite::readSettings(void) if (m_settingsUi.saveModeCB->currentIndex() != SaveModeAskFirst) m_firstImage = false; m_settingsUi.saveDirLEdit->setText(saving.readEntry("Location", QDir::homePath())); m_settingsUi.imgPrefix->setText(saving.readEntry("NamePrefix", i18nc("prefix for auto naming", "Image-"))); - m_settingsUi.imgFormat->setCurrentItem(saving.readEntry("ImgFormat", "png")); + m_settingsUi.imgFormat->setCurrentItem(saving.readEntry("ImgFormat", imageFormat)); m_settingsUi.imgQuality->setValue(saving.readEntry("ImgQuality", 90)); m_settingsUi.setQuality->setChecked(saving.readEntry("SetQuality", false)); m_settingsUi.showB4Save->setChecked(saving.readEntry("ShowBeforeSave", true)); @@ -328,17 +322,6 @@ void Skanlite::saveImage() QString prefix = m_saveLocation->u_imgPrefix->text(); QString type = m_saveLocation->u_imgFormat->currentText().toLower(); int fileNumber = m_saveLocation->u_numStartFrom->value(); - QStringList filterList = m_filterList; - if ((m_format==KSaneIface::KSaneWidget::FormatRGB_16_C) || - (m_format==KSaneIface::KSaneWidget::FormatGrayScale16)) - { - filterList = m_filter16BitList; - if (type != "png") { - type = "png"; - KMessageBox::information(this, i18n("The image will be saved in the PNG format, as Skanlite only supports saving 16 bit color images in the PNG format.")); - } - } - // find next available file name for name suggestion KUrl fileUrl; @@ -365,8 +348,8 @@ void Skanlite::saveImage() if (m_settingsUi.saveModeCB->currentIndex() == SaveModeManual) { // ask for a filename if requested. m_saveDialog->setSelection(fileUrl.url()); - m_saveDialog->setMimeFilter(filterList, "image/"+type); - +#warning FIXME: bogus default MIME type for some image formats + m_saveDialog->setMimeFilter(m_filterList, "image/"+type); do { if (m_saveDialog->exec() != KFileDialog::Accepted) return; @@ -412,28 +395,15 @@ void Skanlite::saveImage() } // Save - if ((m_format==KSaneIface::KSaneWidget::FormatRGB_16_C) || - (m_format==KSaneIface::KSaneWidget::FormatGrayScale16)) - { - KSaneImageSaver saver; - if (saver.savePngSync(fname, m_data, m_width, m_height, m_format)) { - m_showImgDialog->close(); // closing the window if it is closed should not be a problem. - } - else { - perrorMessageBox(i18n("Failed to save image")); - } + // create the image if needed. + if (m_img.width() < 1) { + m_img = m_ksanew->toQImage(m_data, m_width, m_height, m_bytesPerLine, (KSaneIface::KSaneWidget::ImageFormat)m_format); } - else { - // create the image if needed. - if (m_img.width() < 1) { - m_img = m_ksanew->toQImage(m_data, m_width, m_height, m_bytesPerLine, (KSaneIface::KSaneWidget::ImageFormat)m_format); - } - if (m_img.save(fname, 0, quality)) { - m_showImgDialog->close(); // calling close() on a closed window does nothing. - } - else { - perrorMessageBox(i18n("Failed to save image")); - } + if (m_img.save(fname, 0, quality)) { + m_showImgDialog->close(); // calling close() on a closed window does nothing. + } + else { + perrorMessageBox(i18n("Failed to save image")); } if (!fileUrl.isLocalFile()) { diff --git a/skanlite/src/skanlite.h b/skanlite/src/skanlite.h index 7d43ddfb..4b3243f9 100644 --- a/skanlite/src/skanlite.h +++ b/skanlite/src/skanlite.h @@ -90,7 +90,6 @@ class Skanlite : public KDialog ImageViewer m_imageViewer; QStringList m_filterList; - QStringList m_filter16BitList; QStringList m_typeList; bool m_firstImage; };