Ticket #12424: 0015-MythUI-Handle-deviant-image-orientation-behaviour-by.patch

File 0015-MythUI-Handle-deviant-image-orientation-behaviour-by.patch, 7.0 KB (added by Roger Siddons <dizygotheca@…>, 5 years ago)
  • mythtv/libs/libmythmetadata/imagemetadata.cpp

    From 81f3beaf05972b7e4b2736b2282c2c89f1e941fa Mon Sep 17 00:00:00 2001
    From: Roger Siddons <dizygotheca@ntlworld.com>
    Date: Wed, 7 Oct 2015 01:25:58 +0100
    Subject: [PATCH 15/15] MythUI: Handle deviant image orientation behaviour by
     Qt 5.4.1
    
    When loading a QImage, Qt 5.4.1 automatically orientates the image.
    All other versions (Qt 4, 5.2, 5.4.0, 5.4.2) load the raw image without applying orientation.
    In Qt 5.5 the behaviour will be optional.
    See https://codereview.qt-project.org/#/c/110685/
    
    This causes Gallery images to be mis-orientated (as orientation is applied twice)
    
    This patch applies orientation compensation when running with Qt 5.4.1
    
    Tested on Qt 5.2.1, 5.4.1 & 5.4.2
    
    diff --git a/mythtv/libs/libmythmetadata/imagemetadata.cpp b/mythtv/libs/libmythmetadata/imagemetadata.cpp
    index 5db9119..5557d0a 100644
    a b int Orientation::Transform(int transform) 
    4040
    4141
    4242/*!
     43   \brief Initialises conversion matrix for Qt 5.4.1
     44   \return Matrix of orientation codes such that:
     45    Qt 5.4.1 orientation = matrix(file orientation, current orientation)
     46 */
     47Orientation::Matrix Orientation::InitOrientationMatrix()
     48{
     49    Orientation::Matrix matrix;
     50
     51    if (krunningQt541)
     52    {
     53        // Each row/string defines codes for a single file orientation
     54        // Each col/value defines applicable code for corresponding current orientation
     55        // As current orientation is applicable to raw camera image, these codes
     56        // define the current orientation relative to 1/Normal (as Qt 5.4.1 has already
     57        // applied the file orientation)
     58        QStringList vals = QStringList()
     59                << "0 1 2 3 4 5 6 7 8"
     60                << "0 1 2 3 4 5 6 7 8"
     61                << "0 2 1 4 3 8 7 6 5"
     62                << "0 3 4 1 2 7 8 5 6"
     63                << "0 4 3 2 1 6 5 8 7"
     64                << "0 5 6 7 8 1 2 3 4"
     65                << "0 8 7 6 5 2 1 4 3"
     66                << "0 7 8 5 6 3 4 1 2"
     67                << "0 6 5 8 7 4 3 2 1";
     68
     69        for (int row = 0; row < vals.size(); ++row)
     70        {
     71            QStringList rowVals = vals.at(row).split(' ');
     72            for (int col = 0; col < rowVals.size(); ++col)
     73                matrix[row][col] = rowVals.at(col).toInt();
     74        }
     75    }
     76    return matrix;
     77}
     78
     79const bool Orientation::krunningQt541 = (strcmp(qVersion(), "5.4.1") == 0);
     80const Orientation::Matrix Orientation::kQt541_orientation = InitOrientationMatrix();
     81
     82
     83/*!
     84 * \brief Determines orientation required for an image
     85 * \details Some Qt versions automatically apply file orientation when an image
     86 * is loaded. This compensates for that to ensure images are always orientated
     87   correctly.
     88 * \param compensate Whether to compensate for Qt auto-rotation
     89 * \return Exif orientation code to apply after the image has been loaded.
     90 */
     91int Orientation::GetCurrent(bool compensate)
     92{
     93    // Qt 5.4.1 automatically applies the file orientation when loading images
     94    // Ref: https://codereview.qt-project.org/#/c/111398/
     95    // Ref: https://codereview.qt-project.org/#/c/110685/
     96    // https://bugreports.qt.io/browse/QTBUG-37946
     97    if (compensate && krunningQt541)
     98    {
     99        // Deduce orientation relative to 1/Normal from file & current orientations
     100        int old = m_current;
     101        m_current = kQt541_orientation.value(m_file).value(m_current);
     102
     103        LOG(VB_FILE, LOG_DEBUG, LOC +
     104            QString("Adjusted orientation %1 to %2 for Qt 5.4.1")
     105            .arg(old).arg(m_current));
     106    }
     107    return m_current;
     108}
     109
     110
     111/*!
    43112 * \brief Adjust current orientation code to apply a transform to an image
    44113 * \details When displayed the image will be orientated iaw its orientation
    45114 * code. The transform is effected by applying the reverse transform to the
  • mythtv/libs/libmythmetadata/imagemetadata.h

    diff --git a/mythtv/libs/libmythmetadata/imagemetadata.h b/mythtv/libs/libmythmetadata/imagemetadata.h
    index 6c11c37..b5b49e9 100644
    a b public: 
    6464    //! Encode original & current orientation to a single Db field
    6565    int Composite() { return m_current * 10 + m_file; }
    6666    int Transform(int);
    67     int GetCurrent()   { return m_current; }
     67    int GetCurrent(bool compensate);
    6868    QString Description();
    6969
    7070    static int FromRotation(const QString &degrees);
    private: 
    7676
    7777    typedef QHash<int, QHash<int, int> > Matrix;
    7878
     79    //! True when using Qt 5.4.1 with its deviant orientation behaviour
     80    static const bool krunningQt541;
     81    //! Orientation conversions for proper display on Qt 5.4.1
     82    static const Matrix kQt541_orientation;
     83    static Matrix InitOrientationMatrix();
     84
    7985    //! The orientation to use: the file orientation with user transformations applied.
    8086    int m_current;
    8187    //! The orientation of the raw file image, as specified by the camera.
  • mythtv/libs/libmythmetadata/imagescanner.cpp

    diff --git a/mythtv/libs/libmythmetadata/imagescanner.cpp b/mythtv/libs/libmythmetadata/imagescanner.cpp
    index 7525224..f488c5f 100644
    a b void ImageScanThread<DBFS>::SyncFile(const QFileInfo &fileInfo, int devId, 
    474474                         im->m_comment, im->m_date, fileOrient);
    475475
    476476        // Reset file orientation, retaining existing setting
    477         int currentOrient = Orientation(dbIm->m_orientation).GetCurrent();
     477        int currentOrient = Orientation(dbIm->m_orientation).GetCurrent(false);
    478478        im->m_orientation = Orientation(currentOrient, fileOrient).Composite();
    479479
    480480        // Remove it from removed list
  • mythtv/libs/libmythmetadata/imagethumbs.cpp

    diff --git a/mythtv/libs/libmythmetadata/imagethumbs.cpp b/mythtv/libs/libmythmetadata/imagethumbs.cpp
    index 3e659c2..8d203c6 100644
    a b QString ThumbThread<DBFS>::CreateThumbnail(ImagePtrK im, int thumbPriority) 
    305305        return QString("Can't create thumbnail for type %1 (image %2)")
    306306                .arg(im->m_type).arg(imagePath);
    307307
    308     int orientBy = Orientation(im->m_orientation).GetCurrent();
     308    // Compensate for any Qt auto-orientation
     309    int orientBy = Orientation(im->m_orientation)
     310            .GetCurrent(im->m_type == kImageFile);
    309311
    310312    // Orientate now to optimise load/display time - no orientation
    311313    // is required when displaying thumbnails
  • mythtv/programs/mythfrontend/galleryslide.cpp

    diff --git a/mythtv/programs/mythfrontend/galleryslide.cpp b/mythtv/programs/mythfrontend/galleryslide.cpp
    index b9ef101..3626450 100644
    a b bool Slide::LoadSlide(ImagePtrK im, int direction, bool notifyCompletion) 
    421421    }
    422422    else
    423423    {
    424         // Load image and orientate
     424        // Load image, compensating for any Qt auto-orientation
    425425        SetFilename(im->m_url);
    426         SetOrientation(Orientation(m_data->m_orientation).GetCurrent());
     426        SetOrientation(Orientation(m_data->m_orientation).GetCurrent(true));
    427427    }
    428428
    429429    // Load in background