\usepackage[Export]{adjustbox}
\usepackage{environ}

\setcounter{topnumber}{2}
\setcounter{bottomnumber}{1}
\setcounter{totalnumber}{3}

\renewcommand{\topfraction}{0.8}
\renewcommand{\bottomfraction}{0.8}
\renewcommand{\textfraction}{0.2}
\renewcommand{\floatpagefraction}{0.8}

\setlength{\textfloatsep}{2em plus 1ex minus 1ex}
\setlength{\floatsep}{2em plus 1ex minus 1ex}
\setlength{\intextsep}{2em plus 1ex minus 1ex}

\makeatletter
\setlength{\@fptop}{0pt}
\setlength{\@fpbot}{0pt plus 1fil}
\makeatother

\makeatletter
\def\fps@figure{htbp}
\def\fps@table{htbp}
\makeatother

% Maximum width/height for figures and tables.
\newcommand{\maxfigwidth}{\textwidth}
\newcommand{\maxfigheight}{\textheight}
\newcommand{\maxtabwidth}{\textwidth}
\newcommand{\maxtabheight}{\textheight}

% Maintain aspect ratio of figures.
\let\origincgfx\includegraphics
\renewcommand{\includegraphics}[2][]{%
\origincgfx[%
  #1,keepaspectratio,max width=\maxfigwidth,max height=\maxfigheight%
]{#2}%
}

% Global placeholder variables.
\def\mdccap{CAPTION}
\def\mdclab{LABEL}

% Automatically adjust figure layout based on width.
\let\oldfigure\figure
\let\endoldfigure\endfigure
\RenewEnviron{figure}{
  % Save old caption/label commands.
  \let\oldcaption\caption
  \let\oldlabel\label
  \let\oldsetcaptionsubtype\setcaptionsubtype

  % Update caption/label commands to save the values to a global variable.
  \renewcommand{\caption}[2][]{\global\def\mdccap{##2}}
  \renewcommand{\label}[1]{\global\def\mdclab{##1}}
  \renewcommand{\setcaptionsubtype}{}

  % Render the body inside a box.
  \sbox{0}{\BODY}

  \let\setcaptionsubtype\oldsetcaptionsubtype
  % Restore the label command so that sub-figures will have their labels
  % set properly. TODO: this leads to multiply defined top level labels,
  % which doesn't seem to cause any issues, but try to avoid.
  \let\label\oldlabel

  % Save the linewidth to a constant (the macro will change depending on environment).
  \xdef\thelinew{\the\linewidth}

  \ifdim\wd0>\thelinew
    \begin{figure*}[tbp]
  \else
    \begin{oldfigure}[htbp]
  \fi

      \begin{adjustbox}{center}
      \begin{adjustbox}{max totalheight=\maxfigheight}
        \begin{minipage}{\maxfigwidth}
          \centering
          \BODY
        \end{minipage}
      \end{adjustbox}
      \end{adjustbox}

      \oldcaption{\mdccap}
      \oldlabel{\mdclab}

  \ifdim\wd0>\thelinew
    \end{figure*}
  \else
    \end{oldfigure}
  \fi

  % Restore caption command.
  \let\caption\oldcaption
}

% Automatically adjust table layout based on width.
\let\oldtable\table
\let\endoldtable\endtable
\RenewEnviron{table}{
  \let\oldcaption\caption
  \let\oldlabel\label

  \renewcommand{\caption}[2][]{\global\def\mdccap{##2}}
  \renewcommand{\label}[1]{\global\def\mdclab{##1}}

  \sbox{0}{\BODY}

  \xdef\thelinew{\the\linewidth}

  \ifdim\wd0>\thelinew
    \begin{table*}[tbp]
  \else
    \begin{oldtable}[htbp]
  \fi

      \begin{adjustbox}{center}
        \maxsizebox{\maxtabwidth}{\maxtabheight}{\BODY}
      \end{adjustbox}

      \oldcaption{\mdccap}
      \oldlabel{\mdclab}

  \ifdim\wd0>\thelinew
    \end{table*}
  \else
    \end{oldtable}
  \fi
}
