Heath Ledger’s zeal roused his co-stars to up their game in his final film, and his death inspired them – and three A-list friends who completed his role – to carry on with a story the late actor had wanted to see, director Terry Gilliam said Friday.

As Gilliam’s “The Imaginarium of Doctor Parnassus” premiered at the Cannes Film Festival, the filmmaker said Ledger almost co-directed the film.

“Heath was enjoying himself so much, and he was ad-libbing a lot, which I don’t normally allow … but Heath was just brilliant at it, and he got everybody else going,” Gilliam said. “Everybody was just energized by Heath. He was extraordinary. He was almost exhausting because he had so much energy.

“That just passed on to everyone else. Everyone’s part grew because they were full of Heath’s energy,” Gilliam said. “What I thought was interesting was to watch people filling the void that Heath left. Everybody was just growing to make sure that there was no void left in the space that Heath had left us.”

The movie closes with the dedication: “A film from Heath Ledger and friends.”

Ledger’s death by an accidental prescription drug overdose on Jan. 22, 2008, left Gilliam with some of the biggest hurdles he has faced in a career filled with tough breaks. Gilliam fought prolonged battles with studio executives over both “Brazil” (1985) and his previous Ledger collaboration “The Brothers Grimm” (2005).

In 2000, Gilliam saw his fantasy epic “The Man Who Killed Don Quixote” shut down after a few days of shooting because of a string of mishaps. On Friday, the director said he would soon restart production on Don Quixote, which will star Johnny Depp.

Gilliam said that, when Ledger died with only about half of his performance for “Doctor Parnassus” filmed, his first thought was to scrap the film.

“Fortunately, I was surrounded by really good people who insisted that I couldn’t be such a lazy bastard and that we had to go out and find a way of finishing the film for Heath,” Gilliam said.

His solution was to cast Johnny Depp, Jude Law and Colin Farrell to play incarnations of Ledger during otherworldly portions of the fantasy film.

Ledger’s character, Tony, is a slick-tongued fundraiser for children’s charities who crosses Russian mobsters and is left for dead, hanging under a London bridge. He’s rescued by a small theater troupe run by Parnassus (Christopher Plummer), an immortal monk who made a deal with the devil (Tom Waits) and now must find a way to keep the wily demon from taking the soul of his teenage daughter (Lily Cole).

Parnassus is overseer of a magic mirror that sends people to a world of imagination, and the script called for Tony to take three trips to the other side – portions of the film that had not been shot when Ledger died.

Depp, who worked with Gilliam on “Fear and Loathing in Las Vegas” (1998) and the Quixote picture before it was aborted, along with Law and Farrell stepped in to each play an incarnation of Tony on his through-the-looking-glass sequences.

“The real credit has got to go to Johnny, Colin and Jude, which was an extraordinary thing, to come in,” Gilliam said. “They’re all doing other films, they’re involved in other projects, and they came to the rescue of this thing.

“They did it solely, basically, for nothing. The money they would have been paid went to Matilda, Heath’s daughter. To me, they’re the real heroes.”

Ledger won the supporting-actor Academy Award in February for his last completed role as the maniacal Joker in “The Dark Knight.”

“Doctor Parnassus” includes allusions that eerily parallel the mythic aura that has grown around Ledger. The movie has references to unforeseen death, remaining forever young – even James Dean, to whom Ledger has been compared as another rising star who died before his time.

Gilliam said those parallels were in the script before Ledger died, and he decided they should stay because “this is the movie Heath wanted to see, and this is the movie that we will do. And I hope he would be pleased with it. I think he would be.”