Conditions for Castling
You cannot castle when
escaping the danger of a check;
your King has moved;
the Rook on the side in which you are castling, has already moved;
your King has to cross a square attacked by an enemy piece.
For example, in Diagram #3, the black King cannot castle Kingside because he would have to cross the diagonal of the white Bishop on a3. He also cannot castle to the Queenside because the Rook has already moved. For the white King, he can't castle on the Queenside because (1) his Queen's Rook has already moved and (2) the black Rook on d8 is firing cannon shots down the d-file. However, white can safely castle Kingside. Black can only castle Kingside after driving the white Bishop off the a3-f8 diagonal or blocking it.
In Diagram #4, the white King has only one castling option. His Knight on g1 still blocks the Kingside castling path so he would have to move it in order to castle in that direction. However, white CAN castle Queenside even though the black Bishop is slicing down the diagonal and attacking the Rook. Remember it is only the King that is prohibited from crossing the line of fire… not the Rook! Likewise, the Black King cannot castle Kingside because it would have to cross the f8-square controlled by the white Knight. It could however, castle Queenside if it so chose… the Rook can cross the b8-square completing the castling move. Lastly, if the black King now captured the knight on d7, it would not be able to castle for the entire game!