From 55aaae9f0d7a65bb89577ce975213a5fdc05593d Mon Sep 17 00:00:00 2001
From: Michele Calgaro <michele.calgaro@yahoo.it>
Date: Sat, 14 Nov 2026 16:17:41 +0900
Subject: kuickshow: clean up code to load images.

Every KuickImage has 2 more bool flags: `myFirstLoad` and `myImlibOriented`. The first is set on creation, when reading an image from file for the first time, the second is set if imlib2 autoorient EXIF jpg images.
On first load, an image is oriented based on user requests (autorotation true/false) keeping into account whether imlib2 has already oriented the image or not. Also user modifications are applied if needed.

Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
---
 kuickshow/src/imagewindow.cpp | 114 ++++++++++++++++++----------
 kuickshow/src/imagewindow.h   |   7 +-
 kuickshow/src/imlibwidget.cpp | 169 +++++++++++-------------------------------
 kuickshow/src/imlibwidget.h   |  18 ++---
 kuickshow/src/kuickimage.cpp  |   3 +
 kuickshow/src/kuickimage.h    |   9 ++-
 6 files changed, 135 insertions(+), 185 deletions(-)

diff --git a/kuickshow/src/imagewindow.cpp b/kuickshow/src/imagewindow.cpp
index b0faef43..f1b4d7a2 100644
--- a/kuickshow/src/imagewindow.cpp
+++ b/kuickshow/src/imagewindow.cpp
@@ -398,7 +398,7 @@ void ImageWindow::scrollImage( int x, int y, bool restrict )
 //     XClearWindow(); // repaint
 //     XMapWindow(), XSync();
 //
-bool ImageWindow::showNextImage( const KURL& url )
+bool ImageWindow::showNextImage( const KURL& url, bool forceReload )
 {
     KuickFile *file = FileCache::self()->getFile( url );
     switch ( file->waitForDownload( this ) ) {
@@ -414,12 +414,12 @@ bool ImageWindow::showNextImage( const KURL& url )
 	    	break; // go on...
     }
 
-    return showNextImage( file );
+    return showNextImage( file, forceReload );
 }
 
-bool ImageWindow::showNextImage( KuickFile *file )
+bool ImageWindow::showNextImage( KuickFile *file, bool forceReload )
 {
-    if ( !loadImage( file ) ) {
+    if ( !loadImage( file, forceReload ) ) {
    	    TQString tmp = i18n("Unable to load the image %1.\n"
                        "Perhaps the file format is unsupported or "
                        "your Imlib is not installed properly.").arg(file->url().prettyURL());
@@ -439,7 +439,7 @@ bool ImageWindow::showNextImage( KuickFile *file )
 
 void ImageWindow::reload()
 {
-    showNextImage( currentFile() );
+    showNextImage( currentFile(), true );
 }
 
 void ImageWindow::pauseSlideShow()
@@ -1019,19 +1019,77 @@ void ImageWindow::toggleFullscreen()
     showWindow();
 }
 
-void ImageWindow::loaded( KuickImage *kuim, bool decoded )
+static bool readExifOrientation( KuickImage *kuim, FlipRotation &orientation )
 {
-	if ( !kdata->isModsEnabled ) {
-		// ### BUG: should be "restorePreviousSize"
-		if ( decoded )
-			undoAutoRotate( kuim );
-		kuim->restoreOriginalSize();
+    KFileMetaInfo metadatas( kuim->file().localFile() );
+    if ( !metadatas.isValid() )
+        return false;
+
+    KFileMetaInfoItem metaitem = metadatas.item("Orientation");
+    if ( !metaitem.isValid() || metaitem.value().isNull() )
+        return false;
+
+    switch ( metaitem.value().toInt() )
+    {
+        default:
+	case 1: orientation = FlipRotation( ROT_0 );   break;
+        case 2: orientation = FlipRotation::hflip();   break;
+        case 3: orientation = FlipRotation( ROT_180 ); break;
+        case 4: orientation = FlipRotation::vflip();   break;
+        case 5: orientation = FlipRotation( ROT_90 )  * FlipRotation::hflip(); break;
+        case 6: orientation = FlipRotation( ROT_90 );  break;
+        case 7: orientation = FlipRotation( ROT_270 ) * FlipRotation::hflip(); break;
+        case 8: orientation = FlipRotation( ROT_270 ); break;
+    }
+    return true;
+}
+
+void ImageWindow::loaded( KuickImage *kuim )
+{
+    if ( !kuim->isFirstLoad() ) {
+	return;
+    }
+    kuim->setFirstLoad( false );
+
+    FlipRotation orientation;
+    if ( readExifOrientation( kuim, orientation ) ) {
+	// If the image was autorotated and the user has disabled it, unrotate the image
+	if ( kuim->isImlibOriented() && !kdata->autoRotation ) {
+	    kuim->flipRotate( orientation.inverse() );
 	}
-	else
-	{
-		autoRotate( kuim );
-		autoScale( kuim );
+	// If the image was not autorotated and the user has enabled it, rotate the image
+	if ( !kuim->isImlibOriented() && kdata->autoRotation ) {
+	    kuim->flipRotate( orientation );
 	}
+    }
+
+    if ( !kdata->isModsEnabled ) {
+	return;
+    }
+
+    // apply default image modifications
+    colMod.brightness = idata->brightness;
+    colMod.contrast = idata->contrast;
+    colMod.gamma = idata->gamma;
+    applyImageModifier( kuim );
+
+    FlipRotation op;
+    if ( kdata->flipVertically )
+	op = FlipRotation::vflip();
+    if ( kdata->flipHorizontally )
+	op *= FlipRotation::hflip();
+    if ( kdata->rotation != ROT_0 )
+	op *= kdata->rotation;
+    kuim->flipRotate( op );
+
+    if ( !kdata->downScale && !kdata->upScale ) {
+	// ### BUG: should be "restorePreviousSize"
+	kuim->restoreOriginalSize();
+    }
+    else
+    {
+	autoScale( kuim );
+    }
 }
 
 // upscale/downscale depending on configuration
@@ -1094,32 +1152,6 @@ void ImageWindow::autoScale( KuickImage *kuim )
         kuim->resize( newW, newH, idata->smoothScale ? KuickImage::SMOOTH : KuickImage::FAST );
 }
 
-// only called when kdata->isModsEnabled is true
-bool ImageWindow::autoRotate( KuickImage *kuim )
-{
-    bool exifApplied = kdata->autoRotation && ImlibWidget::autoRotate( kuim );
-
-    // The initial user preferences are applied after metadata handling;
-    // they do not replace the EXIF transform.
-    FlipRotation op;
-    if ( kdata->flipVertically )
-        op = FlipRotation::vflip();
-    if ( kdata->flipHorizontally )
-        op *= FlipRotation::hflip();
-    if ( kdata->rotation != ROT_0 )
-        op *= kdata->rotation;
-
-    bool loaderOrientationUndone = !kdata->autoRotation && undoAutoRotate( kuim );
-    if ( exifApplied || loaderOrientationUndone )
-    {
-        kuim->flipRotate( op );
-    }
-    else
-        kuim->flipRotateAbs( op );
-
-    return true;
-}
-
 int ImageWindow::desktopWidth( bool totalScreen ) const
 {
     if ( myIsFullscreen || totalScreen )
diff --git a/kuickshow/src/imagewindow.h b/kuickshow/src/imagewindow.h
index 21d507bd..b068adce 100644
--- a/kuickshow/src/imagewindow.h
+++ b/kuickshow/src/imagewindow.h
@@ -43,8 +43,8 @@ class ImageWindow : public ImlibWidget
 public:
   ImageWindow( ImData *_idata=0, TQWidget *parent=0, const char *name=0 );
 
-  bool 		showNextImage( KuickFile * file );
-  bool 		showNextImage( const KURL& url );
+  bool 		showNextImage( KuickFile * file, bool forceReload = false );
+  bool 		showNextImage( const KURL& url, bool forceReload = false );
   void 		scrollImage( int, int, bool restrict=true );
   void		setFullscreen( bool );
   bool 		isFullscreen() 	const { return myIsFullscreen; }
@@ -63,7 +63,6 @@ public:
    */
   void resizeOptimal( int w, int h );
   void autoScale( KuickImage *kuim );
-  virtual bool autoRotate( KuickImage *kuim );
 
   bool          saveImage( const KURL& dest, bool keepOriginalSize );
 
@@ -100,7 +99,7 @@ protected:
   void 		centerImage();
   void          addAlternativeShortcut( TDEAction *action, int key );
   virtual void	updateGeometry( int imWidth, int imHeight );
-  virtual void  loaded( KuickImage *, bool decoded );
+  virtual void  loaded( KuickImage * );
   virtual bool  canZoomTo( int newWidth, int newHeight );
   virtual void  rotated( KuickImage *kuim, int rotation );
 
diff --git a/kuickshow/src/imlibwidget.cpp b/kuickshow/src/imlibwidget.cpp
index 8efcf942..9ea192f0 100644
--- a/kuickshow/src/imlibwidget.cpp
+++ b/kuickshow/src/imlibwidget.cpp
@@ -125,41 +125,32 @@ KuickFile * ImlibWidget::currentFile() const
 // tries to load "file" and returns the according KuickImage *
 // or 0L if unsuccessful
 // Note that the given file MUST already be downloaded prior to calling this function
-KuickImage * ImlibWidget::loadImageInternal( KuickFile * file )
+KuickImage * ImlibWidget::loadImageInternal( KuickFile * file, bool forceReload )
 {
     assert( file->isAvailable() );
 
-    bool decoded = false;
-    KuickImage *kuim = imageCache->getKuimage( file, decoded );
+    KuickImage *kuim = imageCache->getKuimage( file, forceReload );
     if ( !kuim ) {// couldn't load file, maybe corrupt or wrong format
 	kdWarning() << "ImlibWidget: can't load image " << file->url().prettyURL() << endl;
 	return 0L;
     }
 
-    // apply default image modifications
-    colMod.brightness = idata->brightness;
-    colMod.contrast = idata->contrast;
-    colMod.gamma = idata->gamma;
-    applyImageModifier( kuim );
-
-    loaded( kuim, decoded ); // maybe upscale/downscale/rotate in subclasses
+    loaded( kuim ); // maybe upscale/downscale/rotate in subclasses
 
     return kuim;
 }
 
-bool ImlibWidget::loadImage( const KURL& url )
+bool ImlibWidget::loadImage( const KURL& url, bool forceReload )
 {
-    return loadImage( FileCache::self()->getFile( url ));
+    return loadImage( FileCache::self()->getFile( url ), forceReload );
 }
 
-bool ImlibWidget::loadImage( KuickFile * file )
+bool ImlibWidget::loadImage( KuickFile * file, bool forceReload )
 {
     if ( file->waitForDownload( this ) != KuickFile::OK)
     	return false;
     	
-    KuickImage *kuim = loadImageInternal( file );
-    // FIXME - check everywhere if we have a kuim or not!
-
+    KuickImage *kuim = loadImageInternal( file, forceReload );
     if ( kuim ) {
 	m_kuim = kuim;
 	autoUpdate( true ); // -> updateWidget() -> updateGeometry()
@@ -270,91 +261,6 @@ void ImlibWidget::showImageOriginalSize()
     showImage();
 }
 
-static FlipRotation exifOrientation( int orientation )
-{
-    switch ( orientation )
-    {
-        case 2: return FlipRotation::hflip();
-        case 3: return FlipRotation( ROT_180 );
-        case 4: return FlipRotation::vflip();
-        case 5: return FlipRotation( ROT_90 ) * FlipRotation::hflip();
-        case 6: return FlipRotation( ROT_90 );
-        case 7: return FlipRotation( ROT_270 ) * FlipRotation::hflip();
-        case 8: return FlipRotation( ROT_270 );
-        case 1:
-        default: return FlipRotation( ROT_0 );
-    }
-}
-
-static bool readExifOrientation( KuickImage *kuim, FlipRotation& orientation )
-{
-    KFileMetaInfo metadatas( kuim->file().localFile() );
-    if ( !metadatas.isValid() )
-        return false;
-
-    KFileMetaInfoItem metaitem = metadatas.item("Orientation");
-    if ( !metaitem.isValid()
-        || metaitem.value().isNull()
-        )
-        return false;
-
-
-    orientation = exifOrientation( metaitem.value().toInt() );
-    return true;
-}
-
-static bool imlibAutoOrientsExif( KuickImage *kuim, Imlib_Context context )
-{
-#if defined(IMLIB2_VERSION) && IMLIB2_VERSION >= 10705
-    imlib_context_push( context );
-    // Smooth resizing creates an image without a format; the retained
-    // original still identifies which loader handled the image.
-    imlib_context_set_image( kuim->originalImage() );
-    TQString format = imlib_image_format();
-    imlib_context_pop();
-
-    // Imlib2 1.7.5 added automatic EXIF orientation to its JPEG loader.
-    return format == "jpg" || format == "jpeg";
-#else
-    (void) kuim;
-    (void) context;
-    return false;
-#endif
-}
-
-bool ImlibWidget::autoRotate( KuickImage *kuim )
-{
-    FlipRotation orientation;
-    if ( !readExifOrientation( kuim, orientation ) )
-        return false;
-
-    if ( imlibAutoOrientsExif( kuim, context ) )
-    {
-        kuim->flipRotateAbs( FlipRotation( ROT_0 ) );
-        return true;
-    }
-
-    kuim->flipRotateAbs( orientation );
-    return true;
-}
-
-bool ImlibWidget::undoAutoRotate( KuickImage *kuim )
-{
-#if defined(IMLIB2_VERSION) && IMLIB2_VERSION >= 10705
-    FlipRotation orientation;
-    if ( readExifOrientation( kuim, orientation ) &&
-         imlibAutoOrientsExif( kuim, context ) )
-    {
-        kuim->flipRotateAbs( orientation.inverse() );
-        return true;
-    }
-#else
-    (void) kuim;
-#endif
-    return false;
-}
-
-
 void ImlibWidget::setRotation( Rotation rot )
 {
     if ( m_kuim )
@@ -364,7 +270,6 @@ void ImlibWidget::setRotation( Rotation rot )
     }
 }
 
-
 // slots connected to Accels and popupmenu
 void ImlibWidget::rotate90()
 {
@@ -661,10 +566,17 @@ Imlib_Image ImageCache::fromTQImage( const TQString& fileName ) const
     return im;
 }
 
+static bool imlibAutoOrientsExif( const TQString &format )
+{
+#if defined(IMLIB2_VERSION) && IMLIB2_VERSION >= 10705
+    // Imlib2 1.7.5 added automatic EXIF orientation to its JPEG loader.
+    return format == "jpg" || format == "jpeg";
+#endif
+    return false;
+}
 
-KuickImage * ImageCache::getKuimage( KuickFile * file, bool &decoded )
+KuickImage * ImageCache::getKuimage( KuickFile * file, bool forceReload )
 {
-    decoded = false;
     if ( !file )
 	return 0L;
 
@@ -675,26 +587,27 @@ KuickImage * ImageCache::getKuimage( KuickFile * file, bool &decoded )
     KuickImage *kuim = nullptr;
     int index = fileList.findIndex( file );
     if ( index != -1 ) {
-        kuim = kuickList.at( index );
-
-        // Drop the cached image if the file was modified on the disk
-        if ( kuim && kuim->fileChangedOnDisk() ) {
-            kuickList.remove( index );
-            fileList.remove( file );
-            kuim = nullptr;
-        }
-        // need to reorder the lists, otherwise we might delete the current
-        // image when a new one is cached and the current one is the last!
-        else if ( index != 0 ) {
-            kuim = kuickList.take( index );
-            kuickList.insert( 0, kuim );
-            fileList.remove( file );
-            fileList.prepend( file );
-        }
+	kuim = kuickList.at( index );
+
+	// Drop the cached image if the file was modified on the disk
+	// or a reload was being requested
+	if ( kuim && (forceReload || kuim->fileChangedOnDisk()) ) {
+	    kuickList.remove( index );
+	    fileList.remove( file );
+	    kuim = nullptr;
+	}
+	// need to reorder the lists, otherwise we might delete the current
+	// image when a new one is cached and the current one is the last!
+	else if ( index != 0 ) {
+	    kuim = kuickList.take( index );
+	    kuickList.insert( 0, kuim );
+	    fileList.remove( file );
+	    fileList.prepend( file );
+	}
 
-        if ( kuim ) {
-            return kuim;
-        }
+	if ( kuim ) {
+	    return kuim;
+	}
     }
 
     if ( !kuim ) {
@@ -702,10 +615,14 @@ KuickImage * ImageCache::getKuimage( KuickFile * file, bool &decoded )
 
 	imlib_context_push(context);
 	Imlib_Image im = imlib_load_image( TQFile::encodeName( file->localFile() ).data() );
+	TQString format;
+	if ( im ) {
+	    imlib_context_set_image( im );
+	    format = imlib_image_format();
+	}
 	imlib_context_pop();
 
-	if ( !im )
-	{
+	if ( !im ) {
 	    // Try loading using TQImage
 	    im = fromTQImage( file->localFile() );
 	}
@@ -714,12 +631,12 @@ KuickImage * ImageCache::getKuimage( KuickFile * file, bool &decoded )
 	    return 0L;
 
 	kuim = new KuickImage( file, im, context );
+	kuim->setImlibOriented( imlibAutoOrientsExif(format) );
 	connect( kuim, TQ_SIGNAL( startRendering() ),   TQ_SLOT( slotBusy() ));
 	connect( kuim, TQ_SIGNAL( stoppedRendering() ), TQ_SLOT( slotIdle() ));
 
 	kuickList.insert( 0, kuim );
 	fileList.prepend( file );
-	decoded = true;
     }
 
     if ( kuickList.count() > (uint) myMaxImages ) {
diff --git a/kuickshow/src/imlibwidget.h b/kuickshow/src/imlibwidget.h
index 13ffe961..bce3c4bc 100644
--- a/kuickshow/src/imlibwidget.h
+++ b/kuickshow/src/imlibwidget.h
@@ -43,7 +43,7 @@ public:
   void setMaxImages( int maxImages );
   int  maxImages() const { return myMaxImages; }
 
-  KuickImage* getKuimage( KuickFile * file, bool &decoded );
+  KuickImage* getKuimage( KuickFile * file, bool forceReload = false );
 
 private:
   Imlib_Image fromTQImage( const TQString& fileName ) const;
@@ -76,8 +76,8 @@ public:
 
   KURL          url()                   const;
   KuickFile *   currentFile()           const;
-  bool		loadImage( KuickFile * file );
-  bool		loadImage( const KURL& url );
+  bool		loadImage( KuickFile * file, bool forceReload = false );
+  bool		loadImage( const KURL& url, bool forceReload = false );
   bool 		cacheImage(const KURL& url );
   void   	zoomImage( float );
   void 		setBrightness( int );
@@ -93,13 +93,6 @@ public:
   bool 		isAutoRenderEnabled() 	const  { return isAutoRendering;  }
   const TQColor& backgroundColor() 	const;
   void 		setBackgroundColor( const TQColor& );
-
-  /**
-   * @return true if auto-rotation is not possible, e.g. because no metadata
-   * about orientation is available
-   */
-  virtual bool  autoRotate( KuickImage *kuim );
-
   virtual void  reparent( TQWidget* parent, WFlags f, const TQPoint& p, bool showIt = false );
 
 public slots:
@@ -113,15 +106,14 @@ public slots:
 
 
 protected:
-  KuickImage *	loadImageInternal( KuickFile * file );
-	bool		undoAutoRotate( KuickImage *kuim );
+  KuickImage *	loadImageInternal( KuickFile * file, bool forceReload = false );
   void 		showImage();
   void 		applyImageModifier( KuickImage *kuim );
   void 		resetImageModifier();
   void 		rotate( int );
   void 		updateWidget( bool geometryUpdate=true );
   virtual void 	updateGeometry( int width, int height );
-  virtual void  loaded( KuickImage *, bool decoded ) {}
+  virtual void  loaded( KuickImage * ) {}
   virtual bool  canZoomTo( int newWidth, int newHeight );
   virtual void  rotated( KuickImage *kuim, int rotation ) {}
 
diff --git a/kuickshow/src/kuickimage.cpp b/kuickshow/src/kuickimage.cpp
index 67136291..d1ee8df5 100644
--- a/kuickshow/src/kuickimage.cpp
+++ b/kuickshow/src/kuickimage.cpp
@@ -24,6 +24,9 @@ KuickImage::KuickImage( const KuickFile *file, Imlib_Image im, Imlib_Context con
     myOrigWidth  = myWidth;
     myOrigHeight = myHeight;
     myIsDirty    = true;
+    myFirstLoad	 = true;
+    myImlibOriented = false;
+
     myIm.im      = im;
     mySavedIm.im = nullptr;
 }
diff --git a/kuickshow/src/kuickimage.h b/kuickshow/src/kuickimage.h
index 163a5bb1..71d6afc5 100644
--- a/kuickshow/src/kuickimage.h
+++ b/kuickshow/src/kuickimage.h
@@ -51,8 +51,12 @@ public:
   const KuickFile& file()       const { return *myFile; }
   const KURL& url()             const { return myFile->url(); }
 
-  void		setDirty( bool d )    { myIsDirty = d;    }
+  void		setDirty( bool d )    { myIsDirty = d; }
   bool		isDirty()       const { return myIsDirty; }
+  void		setFirstLoad( bool fl ) { myFirstLoad = fl; }
+  bool		isFirstLoad()   const { return myFirstLoad; }
+  void		setImlibOriented( bool o ) { myImlibOriented = o; }
+  bool		isImlibOriented()   const { return myImlibOriented; }
   bool		fileChangedOnDisk() const;
   Rotation	absRotation()   const { return myIm.orient.rot; }
   bool		flipMode()      const { return myIm.orient.flip; }
@@ -83,6 +87,9 @@ private:
   Pixmap 	myPixmap;
   bool 		myIsDirty;
 
+  bool 		myFirstLoad;
+  bool 		myImlibOriented;
+
   // On-disk fingerprint of the file at the time this image was loaded,
   // used to detect stale cache entries when a file is replaced
   TQDateTime	myFileDateTime;
-- 
cgit v1.2.3

