libopenjpegenc: add NULL check for img before accessing it