// Internal function used to get the Psd header from the current file. ILboolean iGetPsdHead(SIO* io, PSDHEAD *Header) { io->read(io, Header, 1, sizeof(PSDHEAD)); #ifdef IL_LITTLE_ENDIAN iSwapUShort(&Header->Version); iSwapUShort(&Header->Channels); iSwapUInt(&Header->Height); iSwapUInt(&Header->Width); iSwapUShort(&Header->Depth); iSwapUShort(&Header->Mode); #endif return IL_TRUE; }
// Internal function used to load the DICOM. ILboolean iLoadDicomInternal(void) { DICOMHEAD Header; ILuint i; ILushort TempS, *ShortPtr; ILfloat TempF, *FloatPtr; ILboolean Swizzle = IL_FALSE; if (iCurImage == NULL) { ilSetError(IL_ILLEGAL_OPERATION); return IL_FALSE; } // Clear the header to all 0s to make checks later easier. memset(&Header, 0, sizeof(DICOMHEAD)); if (!iGetDicomHead(&Header)) { ilSetError(IL_INVALID_FILE_HEADER); return IL_FALSE; } if (!iCheckDicom(&Header)) return IL_FALSE; if (!ilTexImage(Header.Width, Header.Height, Header.Depth, ilGetBppFormat(Header.Format), Header.Format, Header.Type, NULL)) return IL_FALSE; //@TODO: Find out if the origin is always in the upper left. iCurImage->Origin = IL_ORIGIN_UPPER_LEFT; // Header.DataLen may be larger than SizeOfData, since it has to be padded with a NULL if it is not an even length, // so we just test to make sure it is at least large enough. //@TODO: Do this check before ilTexImage call. if (Header.DataLen < iCurImage->SizeOfData) { ilSetError(IL_INVALID_FILE_HEADER); return IL_FALSE; } // We may have to swap the order of the data. #ifdef __BIG_ENDIAN__ if (!Header.BigEndian) { if (Header.Format == IL_RGB) Header.Format = IL_BGR; else if (Header.Format == IL_RGBA) Swizzle = IL_TRUE; } #else // Little endian if (Header.BigEndian) { if (Header.Format == IL_RGB) Header.Format = IL_BGR; if (Header.Format == IL_RGBA) Swizzle = IL_TRUE; } #endif switch (Header.Type) { case IL_UNSIGNED_BYTE: if (iread(iCurImage->Data, iCurImage->SizeOfData, 1) != 1) return IL_FALSE; // Swizzle the data from ABGR to RGBA. if (Swizzle) { for (i = 0; i < iCurImage->SizeOfData; i += 4) { iSwapUInt((ILuint*)(iCurImage->Data + i)); } } break; case IL_UNSIGNED_SHORT: for (i = 0; i < iCurImage->SizeOfData; i += 2) { *((ILushort*)(iCurImage->Data + i)) = GetShort(&Header, 0);//GetLittleUShort(); } // Swizzle the data from ABGR to RGBA. if (Swizzle) { ShortPtr = (ILushort*)iCurImage->Data; for (i = 0; i < iCurImage->SizeOfData / 2; i += 4) { TempS = ShortPtr[i]; ShortPtr[i] = ShortPtr[i+3]; ShortPtr[i+3] = TempS; } } break; case IL_FLOAT: for (i = 0; i < iCurImage->SizeOfData; i += 4) { *((ILfloat*)(iCurImage->Data + i)) = GetFloat(&Header, 0);//GetLittleFloat(); } // Swizzle the data from ABGR to RGBA. if (Swizzle) { FloatPtr = (ILfloat*)iCurImage->Data; for (i = 0; i < iCurImage->SizeOfData / 4; i += 4) { TempF = FloatPtr[i]; FloatPtr[i] = FloatPtr[i+3]; FloatPtr[i+3] = TempF; } } break; } return ilFixImage(); }
ILvoid EndianSwapData(void *_Image) { ILuint i; ILubyte *temp, *s, *d; ILushort *ShortS, *ShortD; ILuint *IntS, *IntD; ILfloat *FltS, *FltD; ILdouble *DblS, *DblD; ILimage *Image = (ILimage*)_Image; switch (Image->Type) { case IL_BYTE: case IL_UNSIGNED_BYTE: switch (Image->Bpp) { case 3: temp = (ILubyte*)ialloc(Image->SizeOfData); if (temp == NULL) return; s = Image->Data; d = temp; for( i = Image->Width * Image->Height; i > 0; i-- ) { *d++ = *(s+2); *d++ = *(s+1); *d++ = *s; s += 3; } ifree(Image->Data); Image->Data = temp; break; case 4: temp = (ILubyte*)ialloc(Image->SizeOfData); if (temp == NULL) return; s = Image->Data; d = temp; for (i = Image->Width * Image->Height; i > 0; i--) { *d++ = *(s+3); *d++ = *(s+2); *d++ = *(s+1); *d++ = *s; s += 4; } ifree(Image->Data); Image->Data = temp; break; } break; case IL_SHORT: case IL_UNSIGNED_SHORT: switch (Image->Bpp) { case 3: temp = (ILubyte*)ialloc(Image->SizeOfData); if (temp == NULL) return; ShortS = (ILushort*)Image->Data; ShortD = (ILushort*)temp; for (i = Image->Width * Image->Height; i > 0; i--) { *ShortD = *ShortS++; iSwapUShort(ShortD++); *ShortD = *ShortS++; iSwapUShort(ShortD++); *ShortD = *ShortS++; iSwapUShort(ShortD++); } ifree(Image->Data); Image->Data = temp; break; case 4: temp = (ILubyte*)ialloc(Image->SizeOfData); if (temp == NULL) return; ShortS = (ILushort*)Image->Data; ShortD = (ILushort*)temp; for (i = Image->Width * Image->Height; i > 0; i--) { *ShortD = *ShortS++; iSwapUShort(ShortD++); *ShortD = *ShortS++; iSwapUShort(ShortD++); *ShortD = *ShortS++; iSwapUShort(ShortD++); *ShortD = *ShortS++; iSwapUShort(ShortD++); } ifree(Image->Data); Image->Data = temp; break; } break; case IL_INT: case IL_UNSIGNED_INT: switch (Image->Bpp) { case 3: temp = (ILubyte*)ialloc(Image->SizeOfData); if (temp == NULL) return; IntS = (ILuint*)Image->Data; IntD = (ILuint*)temp; for (i = Image->Width * Image->Height; i > 0; i--) { *IntD = *IntS++; iSwapUInt(IntD++); *IntD = *IntS++; iSwapUInt(IntD++); *IntD = *IntS++; iSwapUInt(IntD++); } ifree(Image->Data); Image->Data = temp; break; case 4: temp = (ILubyte*)ialloc(Image->SizeOfData); if (temp == NULL) return; IntS = (ILuint*)Image->Data; IntD = (ILuint*)temp; for (i = Image->Width * Image->Height; i > 0; i--) { *IntD = *IntS++; iSwapUInt(IntD++); *IntD = *IntS++; iSwapUInt(IntD++); *IntD = *IntS++; iSwapUInt(IntD++); *IntD = *IntS++; iSwapUInt(IntD++); } ifree(Image->Data); Image->Data = temp; break; } break; case IL_FLOAT: switch (Image->Bpp) { case 3: temp = (ILubyte*)ialloc(Image->SizeOfData); if (temp == NULL) return; FltS = (ILfloat*)Image->Data; FltD = (ILfloat*)temp; for (i = Image->Width * Image->Height; i > 0; i--) { *FltD = *FltS++; iSwapFloat(FltD++); *FltD = *FltS++; iSwapFloat(FltD++); *FltD = *FltS++; iSwapFloat(FltD++); } ifree(Image->Data); Image->Data = temp; break; case 4: temp = (ILubyte*)ialloc(Image->SizeOfData); if (temp == NULL) return; FltS = (ILfloat*)Image->Data; FltD = (ILfloat*)temp; for (i = Image->Width * Image->Height; i > 0; i--) { *FltD = *FltS++; iSwapFloat(FltD++); *FltD = *FltS++; iSwapFloat(FltD++); *FltD = *FltS++; iSwapFloat(FltD++); *FltD = *FltS++; iSwapFloat(FltD++); } ifree(Image->Data); Image->Data = temp; break; } break; case IL_DOUBLE: switch (Image->Bpp) { case 3: temp = (ILubyte*)ialloc(Image->SizeOfData); if (temp == NULL) return; DblS = (ILdouble*)Image->Data; DblD = (ILdouble*)temp; for (i = Image->Width * Image->Height; i > 0; i--) { *DblD = *DblS++; iSwapDouble(DblD++); *DblD = *DblS++; iSwapDouble(DblD++); *DblD = *DblS++; iSwapDouble(DblD++); } ifree(Image->Data); Image->Data = temp; break; case 4: temp = (ILubyte*)ialloc(Image->SizeOfData); if (temp == NULL) return; DblS = (ILdouble*)Image->Data; DblD = (ILdouble*)temp; for (i = Image->Width * Image->Height; i > 0; i--) { *DblD = *DblS++; iSwapDouble(DblD++); *DblD = *DblS++; iSwapDouble(DblD++); *DblD = *DblS++; iSwapDouble(DblD++); *DblD = *DblS++; iSwapDouble(DblD++); } ifree(Image->Data); Image->Data = temp; break; } break; } if( iCurImage->Format == IL_COLOUR_INDEX ) { switch (iCurImage->Pal.PalType) { case IL_PAL_RGB24: case IL_PAL_BGR24: temp = (ILubyte*)ialloc(Image->Pal.PalSize); if (temp == NULL) return; s = Image->Pal.Palette; d = temp; for (i = Image->Pal.PalSize / 3; i > 0; i--) { *d++ = *(s+2); *d++ = *(s+1); *d++ = *s; s += 3; } ifree(Image->Pal.Palette); Image->Pal.Palette = temp; break; case IL_PAL_RGBA32: case IL_PAL_RGB32: case IL_PAL_BGRA32: case IL_PAL_BGR32: temp = (ILubyte*)ialloc(Image->Pal.PalSize); if (temp == NULL) return; s = Image->Pal.Palette; d = temp; for (i = Image->Pal.PalSize / 4; i > 0; i--) { *d++ = *(s+3); *d++ = *(s+2); *d++ = *(s+1); *d++ = *s; s += 4; } ifree(Image->Pal.Palette); Image->Pal.Palette = temp; break; } } return; }
ILboolean iSaveRleSgi(ILubyte *Data, ILuint w, ILuint h, ILuint numChannels, ILuint bps) { //works only for sgi files with only 1 bpc ILuint c, i, y, j; ILubyte *ScanLine = NULL, *CompLine = NULL; ILuint *StartTable = NULL, *LenTable = NULL; ILuint TableOff, DataOff = 0; ScanLine = (ILubyte*)ialloc(w); CompLine = (ILubyte*)ialloc(w * 2 + 1); // Absolute worst case. StartTable = (ILuint*)ialloc(h * numChannels * sizeof(ILuint)); LenTable = (ILuint*)icalloc(h * numChannels, sizeof(ILuint)); if (!ScanLine || !CompLine || !StartTable || !LenTable) { ifree(ScanLine); ifree(CompLine); ifree(StartTable); ifree(LenTable); return IL_FALSE; } // These just contain dummy values at this point. TableOff = itellw(); iwrite(StartTable, sizeof(ILuint), h * numChannels); iwrite(LenTable, sizeof(ILuint), h * numChannels); DataOff = itellw(); for (c = 0; c < numChannels; c++) { for (y = 0; y < h; y++) { i = y * bps + c; for (j = 0; j < w; j++, i += numChannels) { ScanLine[j] = Data[i]; } ilRleCompressLine(ScanLine, w, 1, CompLine, LenTable + h * c + y, IL_SGICOMP); iwrite(CompLine, 1, *(LenTable + h * c + y)); } } iseekw(TableOff, IL_SEEK_SET); j = h * numChannels; for (y = 0; y < j; y++) { StartTable[y] = DataOff; DataOff += LenTable[y]; #ifdef __LITTLE_ENDIAN__ iSwapUInt(&StartTable[y]); iSwapUInt(&LenTable[y]); #endif } iwrite(StartTable, sizeof(ILuint), h * numChannels); iwrite(LenTable, sizeof(ILuint), h * numChannels); ifree(ScanLine); ifree(CompLine); ifree(StartTable); ifree(LenTable); return IL_TRUE; }
ILboolean iReadRleSgi(iSgiHeader *Head) { #ifdef __LITTLE_ENDIAN__ ILuint ixTable; #endif ILuint ChanInt = 0; ILuint ixPlane, ixHeight,ixPixel, RleOff, RleLen; ILuint *OffTable=NULL, *LenTable=NULL, TableSize, Cur; ILubyte **TempData=NULL; if (!iNewSgi(Head)) return IL_FALSE; TableSize = Head->YSize * Head->ZSize; OffTable = (ILuint*)ialloc(TableSize * sizeof(ILuint)); LenTable = (ILuint*)ialloc(TableSize * sizeof(ILuint)); if (OffTable == NULL || LenTable == NULL) goto cleanup_error; if (iread(OffTable, TableSize * sizeof(ILuint), 1) != 1) goto cleanup_error; if (iread(LenTable, TableSize * sizeof(ILuint), 1) != 1) goto cleanup_error; #ifdef __LITTLE_ENDIAN__ // Fix the offset/len table (it's big endian format) for (ixTable = 0; ixTable < TableSize; ixTable++) { iSwapUInt(OffTable + ixTable); iSwapUInt(LenTable + ixTable); } #endif //__LITTLE_ENDIAN__ // We have to create a temporary buffer for the image, because SGI // images are plane-separated. */ TempData = (ILubyte**)ialloc(Head->ZSize * sizeof(ILubyte*)); if (TempData == NULL) goto cleanup_error; imemclear(TempData, Head->ZSize * sizeof(ILubyte*)); // Just in case ialloc fails then cleanup_error. for (ixPlane = 0; ixPlane < Head->ZSize; ixPlane++) { TempData[ixPlane] = (ILubyte*)ialloc(Head->XSize * Head->YSize * Head->Bpc); if (TempData[ixPlane] == NULL) goto cleanup_error; } // Read the Planes into the temporary memory for (ixPlane = 0; ixPlane < Head->ZSize; ixPlane++) { for (ixHeight = 0, Cur = 0; ixHeight < Head->YSize; ixHeight++, Cur += Head->XSize * Head->Bpc) { RleOff = OffTable[ixHeight + ixPlane * Head->YSize]; RleLen = LenTable[ixHeight + ixPlane * Head->YSize]; // Seeks to the offset table position iseek(RleOff, IL_SEEK_SET); if (iGetScanLine((TempData[ixPlane]) + (ixHeight * Head->XSize * Head->Bpc), Head, RleLen) != Head->XSize * Head->Bpc) { ilSetError(IL_ILLEGAL_FILE_VALUE); goto cleanup_error; } } } // DW: Removed on 05/25/2002. /*// Check if an alphaplane exists and invert it if (Head->ZSize == 4) { for (ixPixel=0; (ILint)ixPixel<Head->XSize * Head->YSize; ixPixel++) { TempData[3][ixPixel] = TempData[3][ixPixel] ^ 255; } }*/ // Assemble the image from its planes for (ixPixel = 0; ixPixel < iCurImage->SizeOfData; ixPixel += Head->ZSize * Head->Bpc, ChanInt += Head->Bpc) { for (ixPlane = 0; (ILint)ixPlane < Head->ZSize * Head->Bpc; ixPlane += Head->Bpc) { iCurImage->Data[ixPixel + ixPlane] = TempData[ixPlane][ChanInt]; if (Head->Bpc == 2) iCurImage->Data[ixPixel + ixPlane + 1] = TempData[ixPlane][ChanInt + 1]; } } #ifdef __LITTLE_ENDIAN__ if (Head->Bpc == 2) sgiSwitchData(iCurImage->Data, iCurImage->SizeOfData); #endif ifree(OffTable); ifree(LenTable); for (ixPlane = 0; ixPlane < Head->ZSize; ixPlane++) { ifree(TempData[ixPlane]); } ifree(TempData); return IL_TRUE; cleanup_error: ifree(OffTable); ifree(LenTable); if (TempData) { for (ixPlane = 0; ixPlane < Head->ZSize; ixPlane++) { ifree(TempData[ixPlane]); } ifree(TempData); } return IL_FALSE; }
static Uint32 iSDL_SwapBE32( Uint32 i ) { Uint32 foo = i; iSwapUInt(&foo); return foo; }