A hospital and two prisons have been criticised after failing to detect that a prisoner who died was suffering from cancer.
Sheriff Derek O'Carroll found a "series of shortcomings" at Aberdeen Royal Infirmary, HMP Peterhead and HMP Glenochil following the death of rapist Giovanni Cocozza.
The 82-year-old died at Stirling Royal Infirmary in March 2011 from a brain haemorrhage he was susceptible to due to long-standing high blood pressure.
Cocozza, who at the time was serving a 10-year sentence, was found to be suffering from incurable lung cancer which had spread to his liver. It was first diagnosed less than 24 hours before his death.
Sheriff O'Carroll, who heard a Fatal Accident Inquiry (FAI) into the death at Alloa Sheriff Court, concluded that the cancer did not contribute to his death and could not have been avoided.
Despite this, the failure to detect it earlier denied Cocozza access to treatment that might have improved his quality of life as well as the chance of being considered for compassionate release, he said.
The sheriff said: "The failure to diagnose that cancer was attributable to the combined effect of a series of shortcomings, both personal and institutional, occurring within Aberdeen Royal Infirmary, HMP Peterhead and HMP Glenochil.
"Had it not been for those shortcomings, it is likely that the existence of the cancer would have been diagnosed earlier, quite possibly before the end of 2010.
"The absence of a more timely diagnosis resulted in Mr Cocozza losing two opportunities.
"The first was access to treatment, though any such treatment could only have been palliative and would not have extended life, though it might well have improved his quality of life.
"The second was the chance of being considered for compassionate release."
The inquiry found there was a failure to ensure that a CT scan ordered by a consultant was carried out at Aberdeen Royal Infirmary's radiology department.
Staff at Peterhead prison did not notice that there was an outstanding need for a CT scan.
When Cocozza was later transferred to Glenochil, that information was omitted from his transfer form and no-one at the prison noticed that investigations into his deteriorating health had not been completed.
The sheriff recommended a review of systems at the hospital's radiology department and improvements to medical recordkeeping and transfer procedures at the prisons.
He said: "I hope that the contents of this determination will assist those who wish to improve the shortcomings in the systems...so that the treatment of prisoners may approach the quality of care that is generally expected among those cared for in the community."
It was a "fundamental principle" that a prisoner be entitled to the same quality of care as others, he said.
The sheriff also criticised the Crown for the delay in bringing forward the inquiry, which was mandatory as Cocozza died in custody.
He said: "Around two years passed before the petition to fix the inquiry was lodged at this court. It is difficult to understand clearly the reason for that delay."
An NHS Grampian spokesman said: ''We have received the Sheriff's report and note the points raised in the determination.
"It is important that we have robust systems of work so that referring clinicians are informed if an investigation is not appropriate.
"Recently the radiology department has moved to electronic systems for referral and reporting of radiology investigations.
"We are working towards ensuring that the new referral system is used to send a message every time a request for an investigation has not been carried out by the radiology department because it was clinically inappropriate.
"The safety of our referral and result data management systems are under constant review."
A Scottish Prison Service spokeswoman said: "SPS is aware that the findings of the Sheriff have now been published. We will take time to ensure we consider fully any matters relevant to SPS."