This may cause a re-allocation of memory, since you already assigned width and height.  Handle the two cases in an if/else so you never set the size more than once.