# Change rotations with sidewaysfigure and twoside option

I am trying to rotate a figure using the rotating package (with the sidewaysfigure environment) in a document using the twoside option. By default, the twoside option will set uneven pages to be on the right, and even pages on the left. Now, the rotating package promises to take this to account, and actually does rotate figures clockwise or counterclockwise depending on the page number. However, it does the exact opposite of what I want; figures that are on uneven pages (i.e. on the right of a printed document) have their captions on the right, on the far side of the binding. I would like the caption to be on the side of the binding, is this possible? I have included a MWE below.

\documentclass[twoside]{report}

\usepackage{graphicx}
\usepackage{rotating}

\begin{document}

\begin{sidewaysfigure}
\centering
\includegraphics{example-image-a}
\caption{Foo.}
\end{sidewaysfigure}

\end{document}


This produces the following:

As there does not seem to be a build in option for this, you could try the following:

\documentclass[twoside]{report}

\usepackage{graphicx}
\usepackage{rotating}

\makeatletter
\def\end@rotdblfloat{%
\end{minipage}\end{lrbox}%
\stepcounter{r@tfl@t}%
\rot@label{RF\ther@tfl@t}%
\rot@pageref{RF\ther@tfl@t}{\R@@page}%
\edef\@tempa{Adding sideways \@captype\space on page \R@@page\space}
\rot@mess@toks\expandafter{\@tempa}
\@tempdima\ht\rot@float@box
\wd\rot@float@box\z@
\ht\rot@float@box\z@
\dp\rot@float@box\z@
\vbox to \textheight{%
\setkeys{Grot}{units=360}%
\if@rot@twoside
\else
\let\R@@page\rot@LR
\fi
\ifthenelse{\isodd{\R@@page}}{%
\ifrot@messages
\if@rot@twoside
\rot@mess@toks\expandafter{\the\rot@mess@toks (right hand page)}%
\fi
\fi
\vfill
\@@line{%
\hskip\rotFPtop
\rotatebox{-90}{\box\rot@float@box}%
\hskip\rotFPbot
}%
}{%
\ifrot@messages
\if@rot@twoside
\rot@mess@toks\expandafter{\the\rot@mess@toks (left hand page)}%
\fi
\fi%
\@@line{%
\hskip\rotFPbot
\rotatebox{90}{\box\rot@float@box}%
\hskip\rotFPtop
}%
\vfill
}%
\rot@message{\the\rot@mess@toks}%
}%
\end@dblfloat
}
\makeatother

\begin{document}

\begin{sidewaysfigure}
\centering
\includegraphics{example-image-a}
\caption{Foo.}
\end{sidewaysfigure}

\end{document}


I have copied the relevant part of rotating.sty and changed the direction of the rotation by replacing \rotatebox{-90} by \rotatebox{90} and vice versa.