"Fossies" - the Fresh Open Source Software Archive

Member "src/imageloaderqt.cpp" (28 Sep 2008, 7049 Bytes) of package /windows/misc/PosteRazor-1.9.5-Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "imageloaderqt.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2     PosteRazor - Make your own poster!
    3     Copyright (C) 2005-2008 by Alessandro Portale
    4     http://posterazor.sourceforge.net/
    5 
    6     This file is part of PosteRazor
    7 
    8     PosteRazor is free software; you can redistribute it and/or modify
    9     it under the terms of the GNU General Public License as published by
   10     the Free Software Foundation, either version 3 of the License, or
   11     (at your option) any later version.
   12 
   13     PosteRazor is distributed in the hope that it will be useful,
   14     but WITHOUT ANY WARRANTY; without even the implied warranty of
   15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16     GNU General Public License for more details.
   17 
   18     You should have received a copy of the GNU General Public License
   19     along with PosteRazor; if not, write to the Free Software
   20     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
   21 */
   22 
   23 #include "imageloaderqt.h"
   24 #include <QImageReader>
   25 #include <math.h>
   26 
   27 ImageLoaderQt::ImageLoaderQt(QObject *parent)
   28     : QObject(parent)
   29 {
   30 }
   31 
   32 bool ImageLoaderQt::loadInputImage(const QString &imageFileName, QString &errorMessage)
   33 {
   34     Q_UNUSED(errorMessage);
   35     bool result = m_image.load(imageFileName);
   36     if (result)
   37         m_imageFileName = imageFileName;
   38     return result;
   39 }
   40 
   41 bool ImageLoaderQt::isImageLoaded() const
   42 {
   43     return !m_image.isNull();
   44 }
   45 
   46 bool ImageLoaderQt::isJpeg() const
   47 {
   48     QImageReader reader(m_imageFileName);
   49     return reader.format() == "jpeg";
   50 }
   51 
   52 QString ImageLoaderQt::fileName() const
   53 {
   54     return m_imageFileName;
   55 }
   56 
   57 QSize ImageLoaderQt::sizePixels() const
   58 {
   59     return m_image.size();
   60 }
   61 
   62 double ImageLoaderQt::horizontalDotsPerUnitOfLength(Types::UnitsOfLength unit) const
   63 {
   64     return m_image.logicalDpiX() / Types::convertBetweenUnitsOfLength(1, Types::UnitOfLengthInch, unit);
   65 }
   66 
   67 double ImageLoaderQt::verticalDotsPerUnitOfLength(Types::UnitsOfLength unit) const
   68 {
   69     return m_image.logicalDpiY() / Types::convertBetweenUnitsOfLength(1, Types::UnitOfLengthInch, unit);
   70 }
   71 
   72 QSizeF ImageLoaderQt::size(Types::UnitsOfLength unit) const
   73 {
   74     const QSize sizePixels = this->sizePixels();
   75     return QSizeF(sizePixels.width() / horizontalDotsPerUnitOfLength(unit), sizePixels.height() / verticalDotsPerUnitOfLength(unit));
   76 }
   77 
   78 const QImage ImageLoaderQt::imageAsRGB(const QSize &size) const
   79 {
   80     return m_image.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
   81 }
   82 
   83 int ImageLoaderQt::bitsPerPixel() const
   84 {
   85     return colorDataType() == Types::ColorTypeRGB?24:m_image.depth();
   86 }
   87 
   88 Types::ColorTypes ImageLoaderQt::colorDataType() const
   89 {
   90     Types::ColorTypes result = Types::ColorTypeRGB;
   91     switch (m_image.format())
   92     {
   93     case QImage::Format_Mono:
   94         result = Types::ColorTypeMonochrome;
   95         break;
   96     case QImage::Format_Indexed8:
   97         result = m_image.isGrayscale()?Types::ColorTypeGreyscale:Types::ColorTypePalette;
   98         break;
   99     case QImage::Format_ARGB32:
  100         result = Types::ColorTypeRGBA;
  101         break;
  102     default:
  103         result = Types::ColorTypeRGB;
  104     }
  105     return result;
  106 }
  107 
  108 const QByteArray ImageLoaderQt::bits() const
  109 {
  110     const int imageWidth = m_image.width();
  111     const int imageHeight = m_image.height();
  112     const unsigned int bitsPerLine = imageWidth * bitsPerPixel();
  113     const unsigned int bytesPerLine = (unsigned int)ceil(bitsPerLine/8.0);
  114     const unsigned int imageBytesCount = bytesPerLine * imageHeight;
  115 
  116     QByteArray result(imageBytesCount, 0);
  117     char *destination = result.data();
  118 
  119     const bool has32Bpp = bitsPerPixel() == 32;
  120     if ((bitsPerPixel() == 24 || has32Bpp) && QSysInfo::ByteOrder == QSysInfo::LittleEndian) {
  121         for (int scanline = 0; scanline < imageHeight; scanline++) {
  122             const uchar *sourceScanLine = m_image.scanLine(scanline);
  123             for (int column = 0; column < imageWidth; column++) {
  124                 if (has32Bpp)
  125                     *destination++ = sourceScanLine[3];
  126                 *destination++ = sourceScanLine[2];
  127                 *destination++ = sourceScanLine[1];
  128                 *destination++ = sourceScanLine[0];
  129                 sourceScanLine += 4;
  130             }
  131         }
  132     } else {
  133         for (int scanline = 0; scanline < imageHeight; scanline++) {
  134             const uchar *sourceScanLine = m_image.scanLine(scanline);
  135             memcpy(destination, sourceScanLine, bytesPerLine);
  136             destination += bytesPerLine;
  137         }
  138     }
  139 
  140     return result;
  141 }
  142 
  143 const QVector<QRgb> ImageLoaderQt::colorTable() const
  144 {
  145     return m_image.colorTable();
  146 }
  147 
  148 const QVector<QPair<QStringList, QString> > &ImageLoaderQt::imageFormats() const
  149 {
  150     static QVector<QPair<QStringList, QString> > formats;
  151     if (formats.empty()) {
  152         const struct {
  153             const QString extensions;
  154             const QString description;
  155         } extensionList[] = {
  156             {QLatin1String("bmp"),      QLatin1String("Windows, OS/2 Bitmap")},
  157             {QLatin1String("gif"),      QLatin1String("Graphic Interchange Format")},
  158             {QLatin1String("ico"),      QLatin1String("Windows Icon")},
  159             {QLatin1String("jpg jpeg"), QLatin1String("Joint Photographic Experts Group")},
  160             {QLatin1String("mng"),      QLatin1String("Multiple-image Network Graphics")},
  161             {QLatin1String("pbm"),      QLatin1String("Portable Bitmap")},
  162             {QLatin1String("pgm"),      QLatin1String("Portable Graymap")},
  163             {QLatin1String("png"),      QLatin1String("Portable Network Graphics")},
  164             {QLatin1String("ppm"),      QLatin1String("Portable Pixelmap")},
  165             {QLatin1String("tif tiff"), QLatin1String("Tagged Image File Format")},
  166             {QLatin1String("svg"),      QLatin1String("Scalable Vector Graphics")},
  167             {QLatin1String("xbm"),      QLatin1String("X11 Bitmap")},
  168             {QLatin1String("xpm"),      QLatin1String("X11 Pixmap")}
  169         };
  170         const int extensionListCount = (int)sizeof(extensionList)/sizeof(extensionList[0]);
  171         const QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
  172         for (int i = 0; i < extensionListCount; i++) {
  173             QStringList extensions = extensionList[i].extensions.split(' ');
  174             foreach (const QString &extension, extensions)
  175                 if (supportedFormats.contains(extension.toAscii())) {
  176                     formats.append(QPair<QStringList, QString> (extensions, extensionList[i].description));
  177                     break;
  178                 }
  179         }
  180     }
  181     return formats;
  182 }
  183 
  184 QString ImageLoaderQt::libraryName() const
  185 {
  186     return QLatin1String("Qt");
  187 }
  188 
  189 QString ImageLoaderQt::libraryAboutText() const
  190 {
  191     // Returning an empty string means that this image IO library does not want to be mentioned.
  192     // Assuming that the host application provides a state-of-the-art "About Qt" message box.
  193     return QString();
  194 }