| 210 | /*----------------------------------------------------------------------------*/ |
| 211 | |
| 212 | ILboolean iReadRleSgi(iSgiHeader *Head) |
| 213 | { |
| 214 | #ifdef __LITTLE_ENDIAN__ |
| 215 | ILuint ixTable; |
| 216 | #endif |
| 217 | ILuint ChanInt = 0; |
| 218 | ILuint ixPlane, ixHeight,ixPixel, RleOff, RleLen; |
| 219 | ILuint *OffTable=NULL, *LenTable=NULL, TableSize, Cur; |
| 220 | ILubyte **TempData=NULL; |
| 221 | |
| 222 | if (!iNewSgi(Head)) |
| 223 | return IL_FALSE; |
| 224 | |
| 225 | TableSize = Head->YSize * Head->ZSize; |
| 226 | OffTable = (ILuint*)ialloc(TableSize * sizeof(ILuint)); |
| 227 | LenTable = (ILuint*)ialloc(TableSize * sizeof(ILuint)); |
| 228 | if (OffTable == NULL || LenTable == NULL) |
| 229 | goto cleanup_error; |
| 230 | if (iread(OffTable, TableSize * sizeof(ILuint), 1) != 1) |
| 231 | goto cleanup_error; |
| 232 | if (iread(LenTable, TableSize * sizeof(ILuint), 1) != 1) |
| 233 | goto cleanup_error; |
| 234 | |
| 235 | #ifdef __LITTLE_ENDIAN__ |
| 236 | // Fix the offset/len table (it's big endian format) |
| 237 | for (ixTable = 0; ixTable < TableSize; ixTable++) { |
| 238 | iSwapUInt(OffTable + ixTable); |
| 239 | iSwapUInt(LenTable + ixTable); |
| 240 | } |
| 241 | #endif //__LITTLE_ENDIAN__ |
| 242 | |
| 243 | // We have to create a temporary buffer for the image, because SGI |
| 244 | // images are plane-separated. |
| 245 | TempData = (ILubyte**)ialloc(Head->ZSize * sizeof(ILubyte*)); |
| 246 | if (TempData == NULL) |
| 247 | goto cleanup_error; |
| 248 | imemclear(TempData, Head->ZSize * sizeof(ILubyte*)); // Just in case ialloc fails then cleanup_error. |
| 249 | for (ixPlane = 0; ixPlane < Head->ZSize; ixPlane++) { |
| 250 | TempData[ixPlane] = (ILubyte*)ialloc(Head->XSize * Head->YSize * Head->Bpc); |
| 251 | if (TempData[ixPlane] == NULL) |
| 252 | goto cleanup_error; |
| 253 | } |
| 254 | |
| 255 | // Read the Planes into the temporary memory |
| 256 | for (ixPlane = 0; ixPlane < Head->ZSize; ixPlane++) { |
| 257 | for (ixHeight = 0, Cur = 0; ixHeight < Head->YSize; |
| 258 | ixHeight++, Cur += Head->XSize * Head->Bpc) { |
| 259 | |
| 260 | RleOff = OffTable[ixHeight + ixPlane * Head->YSize]; |
| 261 | RleLen = LenTable[ixHeight + ixPlane * Head->YSize]; |
| 262 | |
| 263 | // Seeks to the offset table position |
| 264 | iseek(RleOff, IL_SEEK_SET); |
| 265 | if (iGetScanLine((TempData[ixPlane]) + (ixHeight * Head->XSize * Head->Bpc), |
| 266 | Head, RleLen) != Head->XSize * Head->Bpc) { |
| 267 | ilSetError(IL_ILLEGAL_FILE_VALUE); |
| 268 | goto cleanup_error; |
| 269 | } |
no test coverage detected