Chuckling and joking with his visitors last week, Ayatollah Kazem Shariat-Madari was the perfect portrait of a most happy fellow and the very antithesis of his drawn, embattled adversary, Shah Mohammad Reza Pahlavi.

As face value, the ayatollah, or leader in Shiite Islam, Iran's state religion, had little to be cheerful about.

The shah's British-built Chieftain heavy tanks controlled the entranced to the holy city - and indeed the very square in fron tof the ayatollah's compound.

Armed soldiers patrolled the streets while uniformed police and mysterious men in mufti checked identiry papers of visitors in the narrow allys near the ayatollah's modest house.

But Shariat-Madari, the leading ayatollah living in Iran, clearly thinks the slow motion revolution against the shah, fueled as it is by religous forces, is winning.

He was contemptuous of the three-week-old military government, saying he would have nothing to do with the generals. Nothing that hte government leaders have defined their mission as only temporary, he insisted that they should return their troops to the barracks.

Waxing alternately optimistic and pessimistic, he said. There are indications we may be heading for something [positive]. But the other side must act even if it does not like the facts.

"Otherwise no brick will remain on another," he said ominously, "and the other side knows it."

What would happen if the military government stayed on he was asked.

"If they do remain, then other incidents will happen," he said, apparently hinting about the armed struggle many analysts fear may be the next stage in the 10-month-old power struggle.

At 76, the black-turbanned white bearded patriarch had just-returned from donating a pint of blood at the local hospital.

It was a gesture symbolizing both the blood already spilled - more than 2,000 Iranians are believed to have died since the first civilians fell to army bullets here last January - and the knowledge that the struggle will claim more lives before it is resolved.

In the six months since last receiving his visitors, Shariat-Madari had gained in confidence - and public relations techniques.

He who once forbade cameras or tape recorders in his presence now has his own official photographer, records his own words and keeps careful track of his media visitors.

But if the clandestinely traded casettes of the religious leadership's sermons, the photocopying machines and the direct dial telephone have become formidable weapons against the shah, here in Qom, 90 miles south of the capital, life shows few other signs of change.

The ayatollah still receives visitors in a small, carpeted room, lit by two naked light bulbs. Mens walked in and out, interrupting his discussion, without undeu ceremony.

From time to time money was handed to him, or he handed bills to visitors. All these transactions were signed with a metal stamp, hanging around his neck, which he linked on a portable ink pad.

Outside, however, things have changed from what a few months ago was unquestionably the shah's - and nobody else's - Iran.

Then it was only in Qom and other holy cities that time was told not in terms of Greenwich Mean Time plus four hours, but in the old 3 1/2-hour tradition.

Then the country's calendar was not the Islamic model, but one the shah proposed three years ago and said was based on that of ancient Persia.

Among the many gestures intended to appease his till seething country-men, the shah has jettisoned both his time zone and his calendar.

Never mentioning the shah by name, Shariat-Madari disagreed with those who felt the situation was out of control and that extremists would be the only ones to benefit.

He blamed the country's troubles on government "shortcomings."

"Maybe they have not yet understood the significance of things," he said, "since what has happened so far is proof of their incompetence in managing the situation."

"Maybe they do not realize it," he said amid laughter, "or maybe they are just dishonest with themselves."

Asked about his differences with Ayatollah Ruhollah Khomeini, the Paris-based exile believed by many Iranians to call the main opposition tune, Shariat-Madari said of all the opposition abroad:

"They are outside. They have freedom we do not enjoy. But we agree on the heart of the problem even if there are always tactical differences between people on the outside and people on the inside."

Shariat-Madari and other Islamic and lay opposition leaders in Iran are known to have considered establishing a committee whose goals would include presenting a united front to moderate Khomeini's demands, which many consider extreme and destablizing for the country.

So far, however, personal and ideological differences have prevented setting up the committee and at this juncture no opposition leaders dare critize Khomeini publicly because of his immense popularity.

As for the home-based political opposition, which so far has sought unsuccessfully to persuade Moslem leaders to accept compromises short of overthrowing the shah, he said, "They must agree with us if they do not want to lose the protection of the people."

In other words, he was telling the political opposition that the religious leadership owned the troops that have brought the shah all but to his knees.

Asked about the fate of cooperation among the splintered opposition, he said all agreed on the neccessity of "government run by the people," but he admitted that different views existed on how to achieve that goal.

Asked if he was committed to driving the shah from his throne, he said delicately, "the question to which you refer," should be "decided by referendum."

Elections, which the religious leadership is less interested in than the political opposition, were fine in principle, he said.

"Free elections need stable government, trusted by the people, but a stable and trusted government can only be resolved through free elections," he said. "We have to find a solution."

He reserved his only angry outburst for Western critics who have charged that the religious movement is reactionary.

Tired of being described as "fanatics" opposed to "material prosperity technology, science, universities, even agriculture," he said the critics "want to maintain despotism and dictatorship."

"Who is reactionary?" They [the critics] want a government similar to those found in the jungle," he muttered.

Mindful of criticisms that Moslem mobs have singled out for punishment property owned by Jews, Armenians, Zoroastrians and followers of the syncretic Bahai faith, he said charges that "we might force other religions to turn Moslem is absolutely baseless."

"We respect all people of any religion and we are just to them," he said of the Shiite majority representing an estimated 93 percent of the population, "except if they face us in a war situation, or if they are treasonable."

Asked how he maintained his good humor in such trying times, he recalled that the prophet had said, "The believer keeps his sadness in his heart and his gladness on his face."