I'm trying to re-define the \includegraphics command to add surrounding code to hide warnings using the silence package.
So far I have the following MWE:
\documentclass{article}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{silence}
\usepackage{letltxmacro}
\WarningFilter[warning_to_hide]{latex}{File}
\makeatletter
\LetLtxMacro{\includegraphicsorig}{\includegraphics}
\def\includegraphicsnowarningstar{%
\@ifnextchar[%
{\includegraphicsnowarningstar@i}
{\includegraphicsnowarningstar@j}}
\def\includegraphicsnowarning{%
\@ifnextchar[%
{\includegraphicsnowarning@i}
{\includegraphicsnowarning@j}}
\def\includegraphicsnowarningstar@i[#1]{%
\@ifnextchar[%
{\includegraphicsnowarningstar@jjj[#1]}
{\includegraphicsnowarningstar@jj[#1]}}
\def\includegraphicsnowarning@i[#1]{%
\@ifnextchar[%
{\includegraphicsnowarning@jjj[#1]}
{\includegraphicsnowarning@jj[#1]}}
\def\includegraphicsnowarningstar@j#1 {\ActivateWarningFilters[warning_to_hide]\includegraphicsorig*{#1}\DeactivateWarningFilters[warning_to_hide]}
\def\includegraphicsnowarning@j#1 {\ActivateWarningFilters[warning_to_hide]\includegraphicsorig{#1}\DeactivateWarningFilters[warning_to_hide]}
\def\includegraphicsnowarningstar@jj[#1]#2 {\ActivateWarningFilters[warning_to_hide]\includegraphicsorig*[#1]{#2}\DeactivateWarningFilters[warning_to_hide]}
\def\includegraphicsnowarning@jj[#1]#2 {\ActivateWarningFilters[warning_to_hide]\includegraphicsorig[#1]{#2}\DeactivateWarningFilters[warning_to_hide]}
\def\includegraphicsnowarningstar@jjj[#1][#2]#3{\ActivateWarningFilters[warning_to_hide]\includegraphicsorig*[#1][#2]{#3}\DeactivateWarningFilters[warning_to_hide]}
\def\includegraphicsnowarning@jjj[#1][#2]#3 {\ActivateWarningFilters[warning_to_hide]\includegraphicsorig[#1][#2]{#3}\DeactivateWarningFilters[warning_to_hide]}
\renewcommand{\includegraphics}{%
\@ifstar
{\includegraphicsnowarningstar}
{\includegraphicsnowarning}}
\makeatother
\begin{document}
\begin{figure}[t]
\centering
\includegraphics{test.jpg}
\caption{This is a working test image.}
\end{figure}
\begin{figure}[t]
\centering
\subfloat[][First sub-figure]{\includegraphics{test.jpg}}\\
\subfloat[][Second sub-figure]{\includegraphics{test.jpg}}
\caption{Why doesn't this work?}
\end{figure}
\end{document}
This works fine except where the new \includegraphics is used in the \subfloat. I get the following errors in the \subfloat:
! Argument of \includegraphicsnowarning@j has an extra }.
<inserted text>
\par
l.52 ...st sub-figure]{\includegraphics{test.jpg}}
\\
Runaway argument?
{test.jpg}
! Paragraph ended before \includegraphicsnowarning@j was complete.
<to be read again>
\par
l.52 ...st sub-figure]{\includegraphics{test.jpg}}
\\
What am I missing here?
If there is a simpler way to perform the redefinition that would be great.

xparseinstead of all those manual argument parsing? – daleif Feb 16 '15 at 11:34xparse. – zelanix Feb 16 '15 at 11:46\subfloat. – zelanix Feb 16 '15 at 11:47\includegraphicsonly accepts one optional argument, so the part you're doing with two optional arguments is useless. – egreg Feb 16 '15 at 11:48\includegraphics*[100,100][300,300]{mypicture}is valid syntax? source. – zelanix Feb 16 '15 at 11:56graphics, notgraphicx; when you loadgraphicx, you specify the bounding box by\includegraphics*[bb=100 100 300 300]{mypicture}. – egreg Feb 16 '15 at 12:14viewportinstead? Doesbbwoth withpdflatex? – daleif Feb 16 '15 at 12:14\includegraphicsdoesn't work inside the\subfloatthough. Thanks for the answer though :) – zelanix Feb 16 '15 at 12:21