ILboolean iLoadBlp1()
{
BLP1HEAD Header;
ILubyte *DataAndAlpha, *Palette;
ILuint i;
ILimage *Image = iCurImage;
ILboolean BaseCreated = IL_FALSE;
#ifndef IL_NO_JPG
ILubyte *JpegHeader, *JpegData;
ILuint JpegHeaderSize;
#endif//IL_NO_JPG
if (!iGetBlp1Head(&Header))
return IL_FALSE;
if (!iCheckBlp1(&Header)) {
ilSetError(IL_INVALID_FILE_HEADER);
return IL_FALSE;
}
//@TODO: Remove this.
i = 0;
switch (Header.Compression)
{
case BLP_TYPE_JPG:
#ifdef IL_NO_JPG
// We can only do the Jpeg decoding if we do not have IL_NO_JPEG defined.
return IL_FALSE;
#else
JpegHeaderSize = GetLittleUInt();
JpegHeader = (ILubyte*)ialloc(JpegHeaderSize);
if (JpegHeader == NULL)
return IL_FALSE;
// Read the shared Jpeg header.
if (iread(JpegHeader, 1, JpegHeaderSize) != JpegHeaderSize) {
ifree(JpegHeader);
return IL_FALSE;
}
//for (i = 0; i < 16; i++) { // Possible maximum of 16 mipmaps
//@TODO: Check return value?
iseek(Header.MipOffsets[i], IL_SEEK_SET);
JpegData = (ILubyte*)ialloc(JpegHeaderSize + Header.MipLengths[i]);
if (JpegData == NULL) {
ifree(JpegHeader);
return IL_FALSE;
}
memcpy(JpegData, JpegHeader, JpegHeaderSize);
if (iread(JpegData + JpegHeaderSize, Header.MipLengths[i], 1) != 1)
return IL_FALSE;
// Just send the data straight to the Jpeg loader.
if (!ilLoadJpegL(JpegData, JpegHeaderSize + Header.MipLengths[i]))
return IL_FALSE;
// The image data is in BGR(A) order, even though it is Jpeg-compressed.
if (Image->Format == IL_RGBA)
Image->Format = IL_BGRA;
if (Image->Format == IL_RGB)
Image->Format = IL_BGR;
ifree(JpegData);
//}
ifree(JpegHeader);
#endif//IL_NO_JPG
break;
case BLP_RAW:
switch (Header.PictureType)
{
// There is no alpha list, so we just read like a normal indexed image.
case BLP_RAW_NO_ALPHA:
for (i = 0; i < 16; i++) { // Possible maximum of 16 mipmaps
if (!BaseCreated) { // Have not created the base image yet, so use ilTexImage.
if (!ilTexImage(Header.Width, Header.Height, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL))
return IL_FALSE;
Image = iCurImage;
BaseCreated = IL_TRUE;
// We have a BGRA palette.
Image->Pal.Palette = (ILubyte*)ialloc(256 * 4);
if (Image->Pal.Palette == NULL)
return IL_FALSE;
Image->Pal.PalSize = 1024;
Image->Pal.PalType = IL_PAL_BGRA32;
// Read in the palette ...
if (iread(Image->Pal.Palette, 1, 1024) != 1024)
return IL_FALSE;
}
else {
if (Image->Width == 1 && Image->Height == 1) // Already at the smallest mipmap (1x1), so we are done.
break;
if (Header.MipOffsets[i] == 0 || Header.MipLengths[i] == 0) // No more mipmaps in the file.
break;
Image->Mipmaps = ilNewImageFull(Image->Width >> 1, Image->Height >> 1, 1, 1, IL_COLOR_INDEX, IL_UNSIGNED_BYTE, NULL);
if (Image->Mipmaps == NULL)
return IL_FALSE;
//.........这里部分代码省略.........
// Internal function used to load the BLP.
ILboolean iLoadBlpInternal(void)
{
BLP2HEAD Header;
ILubyte *CompData;
ILimage *Image;
ILuint Mip, j, x, CompSize, AlphaSize, AlphaOff;
ILint y;
ILboolean BaseCreated = IL_FALSE;
ILubyte *DataAndAlpha = NULL, *Palette = NULL, AlphaMask; //, *JpegHeader, *JpegData;
if (iCurImage == NULL) {
ilSetError(IL_ILLEGAL_OPERATION);
return IL_FALSE;
}
if (!iGetBlp2Head(&Header)) {
ilSetError(IL_INVALID_FILE_HEADER);
return IL_FALSE;
}
if (!iCheckBlp2(&Header)) {
goto check_blp1;
}
//@TODO: Remove this!
if (Header.Type != BLP_TYPE_DXTC_RAW)
return IL_FALSE;
switch (Header.Compression)
{
case BLP_RAW:
for (Mip = 0; Mip < 16; Mip++) { // Possible maximum of 16 mipmaps
if (BaseCreated) {
if (Header.HasMips == 0) // Does not have mipmaps, so we are done.
break;
if (Image->Width == 1 && Image->Height == 1) // Already at the smallest mipmap (1x1), so we are done.
break;
if (Header.MipOffsets[Mip] == 0 || Header.MipLengths == 0) // No more mipmaps in the file.
break;
}
switch (Header.AlphaBits)
{
case 0:
if (!BaseCreated) { // Have not created the base image yet, so use ilTexImage.
if (!ilTexImage(Header.Width, Header.Height, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL))
return IL_FALSE;
Image = iCurImage;
BaseCreated = IL_TRUE;
Image->Pal.Palette = (ILubyte*)ialloc(256 * 4); // 256 entries of ARGB8888 values (1024).
if (Image->Pal.Palette == NULL)
return IL_FALSE;
Image->Pal.PalSize = 1024;
Image->Pal.PalType = IL_PAL_BGRA32; //@TODO: Find out if this is really BGRA data.
if (iread(Image->Pal.Palette, 1, 1024) != 1024) // Read in the palette.
return IL_FALSE;
}
else {
Image->Mipmaps = ilNewImageFull(Image->Width >> 1, Image->Height >> 1, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL);
if (Image->Mipmaps == NULL)
return IL_FALSE;
// Copy the palette from the first image before we change our Image pointer.
iCopyPalette(&Image->Mipmaps->Pal, &Image->Pal);
// Move to the next mipmap in the linked list.
Image = Image->Mipmaps;
}
// The origin should be in the upper left.
Image->Origin = IL_ORIGIN_UPPER_LEFT;
// These two should be the same (tells us how much data is in the file for this mipmap level).
if (Header.MipLengths[Mip] != Image->SizeOfData) {
ilSetError(IL_INVALID_FILE_HEADER);
return IL_FALSE;
}
// Finally read in the image data.
iseek(Header.MipOffsets[Mip], IL_SEEK_SET);
if (iread(Image->Data, 1, Image->SizeOfData) != Image->SizeOfData)
return IL_FALSE;
break;
case 1:
if (!BaseCreated) { // Have not created the base image yet, so use ilTexImage.
if (!ilTexImage(Header.Width, Header.Height, 1, 4, IL_BGRA, IL_UNSIGNED_BYTE, NULL))
return IL_FALSE;
Image = iCurImage;
BaseCreated = IL_TRUE;
Palette = (ILubyte*)ialloc(256 * 4);
if (Palette == NULL)
return IL_FALSE;
// Read in the palette.
if (iread(Palette, 1, 1024) != 1024) {
ifree(Palette);
return IL_FALSE;
}
// We only allocate this once and reuse this buffer with every mipmap (since successive ones are smaller).
//.........这里部分代码省略.........
开发者ID:damucz,项目名称:devil,代码行数:101,代码来源:il_blp.cpp
示例13: iIcnsReadData
ILboolean iIcnsReadData(ILboolean *BaseCreated, ILboolean IsAlpha, ILint Width, ICNSDATA *Entry, ILimage **Image)
{
ILint Position = 0, RLEPos = 0, Channel, i;
ILubyte RLERead, *Data = NULL;
ILimage *TempImage = NULL;
ILboolean ImageAlreadyExists = IL_FALSE;
// The .icns format stores the alpha and RGB as two separate images, so this
// checks to see if one exists for that particular size. Unfortunately,
// there is no guarantee that they are in any particular order.
if (*BaseCreated && iCurImage != NULL)
{
TempImage = iCurImage;
while (TempImage != NULL)
{
if ((ILuint)Width == TempImage->Width)
{
ImageAlreadyExists = IL_TRUE;
break;
}
TempImage = TempImage->Next;
}
}
Data = ialloc(Entry->Size - 8);
if (Data == NULL)
return IL_FALSE;
if (!ImageAlreadyExists)
{
if (!*BaseCreated) // Create base image
{
ilTexImage(Width, Width, 1, 4, IL_RGBA, IL_UNSIGNED_BYTE, NULL);
iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
*Image = iCurImage;
*BaseCreated = IL_TRUE;
}
else // Create next image in list
{
(*Image)->Next = ilNewImage(Width, Width, 1, 4, 1);
*Image = (*Image)->Next;
(*Image)->Format = IL_RGBA;
(*Image)->Origin = IL_ORIGIN_UPPER_LEFT;
}
TempImage = *Image;
}
if (IsAlpha) // Alpha is never compressed.
{
iread(Data, Entry->Size - 8, 1); // Size includes the header
if (Entry->Size - 8 != Width * Width)
{
ifree(Data);
return IL_FALSE;
}
for (i = 0; i < Width * Width; i++)
{
TempImage->Data[(i * 4) + 3] = Data[i];
}
}
else if (Width == 256 || Width == 512) // JPEG2000 encoded - uses JasPer
{
#ifndef IL_NO_JP2
iread(Data, Entry->Size - 8, 1); // Size includes the header
if (ilLoadJp2LInternal(Data, Entry->Size - 8, TempImage) == IL_FALSE)
{
ifree(Data);
ilSetError(IL_LIB_JP2_ERROR);
return IL_TRUE;
}
#else // Cannot handle this size.
ilSetError(IL_LIB_JP2_ERROR); //@TODO: Handle this better...just skip the data.
return IL_FALSE;
#endif//IL_NO_JP2
}
else // RGB data
{
iread(Data, Entry->Size - 8, 1); // Size includes the header
if (Width == 128)
RLEPos += 4; // There are an extra 4 bytes here of zeros.
//@TODO: Should we check to make sure they are all 0?
if (Entry->Size - 8 == Width * Width * 4) // Uncompressed
{
//memcpy(TempImage->Data, Data, Entry->Size);
for (i = 0; i < Width * Width; i++, Position += 4)
{
TempImage->Data[i * 4 + 0] = Data[Position+1];
TempImage->Data[i * 4 + 1] = Data[Position+2];
TempImage->Data[i * 4 + 2] = Data[Position+3];
}
}
else // RLE decoding
{
for (Channel = 0; Channel < 3; Channel++)
{
Position = 0;
while (Position < Width * Width)
//.........这里部分代码省略.........
开发者ID:AlfiyaZi,项目名称:DevIL,代码行数:101,代码来源:il_icns.c
示例14: ilClearImage_
ILAPI ILboolean ILAPIENTRY ilClearImage_(ILimage *Image)
{
ILuint i, c, NumBytes;
ILubyte Colours[32]; // Maximum is sizeof(double) * 4 = 32
ILubyte *BytePtr;
ILushort *ShortPtr;
ILuint *IntPtr;
ILfloat *FloatPtr;
ILdouble *DblPtr;
NumBytes = Image->Bpp * Image->Bpc;
ilGetClear(Colours, Image->Format, Image->Type);
if (Image->Format != IL_COLOUR_INDEX) {
switch (Image->Type)
{
case IL_BYTE:
case IL_UNSIGNED_BYTE:
BytePtr = (ILubyte*)Colours;
for (c = 0; c < NumBytes; c += Image->Bpc) {
for (i = c; i < Image->SizeOfData; i += NumBytes) {
Image->Data[i] = BytePtr[c];
}
}
break;
case IL_SHORT:
case IL_UNSIGNED_SHORT:
ShortPtr = (ILushort*)Colours;
for (c = 0; c < NumBytes; c += Image->Bpc) {
for (i = c; i < Image->SizeOfData; i += NumBytes) {
*((ILushort*)(Image->Data + i)) = ShortPtr[c];
}
}
break;
case IL_INT:
case IL_UNSIGNED_INT:
IntPtr = (ILuint*)Colours;
for (c = 0; c < NumBytes; c += Image->Bpc) {
for (i = c; i < Image->SizeOfData; i += NumBytes) {
*((ILuint*)(Image->Data + i)) = IntPtr[c];
}
}
break;
case IL_FLOAT:
FloatPtr = (ILfloat*)Colours;
for (c = 0; c < NumBytes; c += Image->Bpc) {
for (i = c; i < Image->SizeOfData; i += NumBytes) {
*((ILfloat*)(Image->Data + i)) = FloatPtr[c];
}
}
break;
case IL_DOUBLE:
DblPtr = (ILdouble*)Colours;
for (c = 0; c < NumBytes; c += Image->Bpc) {
for (i = c; i < Image->SizeOfData; i += NumBytes) {
*((ILdouble*)(Image->Data + i)) = DblPtr[c];
}
}
break;
}
}
else {
imemclear(Image->Data, Image->SizeOfData);
if (Image->Pal.Palette)
ifree(Image->Pal.Palette);
Image->Pal.Palette = (ILubyte*)ialloc(4);
if (Image->Pal.Palette == NULL) {
return IL_FALSE;
}
Image->Pal.PalType = IL_PAL_RGBA32;
Image->Pal.PalSize = 4;
Image->Pal.Palette[0] = Colours[0] * UCHAR_MAX;
Image->Pal.Palette[1] = Colours[1] * UCHAR_MAX;
Image->Pal.Palette[2] = Colours[2] * UCHAR_MAX;
Image->Pal.Palette[3] = Colours[3] * UCHAR_MAX;
}
return IL_TRUE;
}
/*! \param Image Specifies the image. The function fails if this is zero.
\param Width Specifies the new image width. This cannot be 0.
\param Height Specifies the new image height. This cannot be 0.
\param Depth Specifies the new image depth. This cannot be 0.
\param Bpp Number of channels (ex. 3 for RGB)
\param Format Enum of the desired format. Any format values are accepted.
\param Type Enum of the desired type. Any type values are accepted.
\param Data Specifies data that should be copied to the new image. If this parameter is NULL, no data is copied, and the new image data consists of undefined values.
\exception IL_ILLEGAL_OPERATION No currently bound image.
\exception IL_INVALID_PARAM One of the parameters is incorrect, such as one of the dimensions being 0.
\exception IL_OUT_OF_MEMORY Could not allocate enough memory.
\return Boolean value of failure or success*/
ILAPI ILboolean ILAPIENTRY il2TexImage(ILimage *Image, ILuint Width,
ILuint Height, ILuint Depth, ILubyte Bpp, ILenum Format, ILenum Type,
void *Data)
{
if (Image == NULL) {
il2SetError(IL_ILLEGAL_OPERATION);
return IL_FALSE;
}
ILubyte BpcType = ilGetBpcType(Type);
if (BpcType == 0) {
il2SetError(IL_INVALID_PARAM);
return IL_FALSE;
}
// Reset palette
Image->Pal.clear();
ilCloseImage(Image->Mipmaps);
Image->Mipmaps = NULL;
ilCloseImage(Image->Next);
Image->Next = NULL;
ilCloseImage(Image->Faces);
Image->Faces = NULL;
ilCloseImage(Image->Layers);
Image->Layers = NULL;
if (Image->AnimList) ifree(Image->AnimList);
if (Image->Profile) ifree(Image->Profile);
if (Image->DxtcData) ifree(Image->DxtcData);
if (Image->Data) ifree(Image->Data);
////
//@TODO: Also check against format?
/*if (Width == 0 || Height == 0 || Depth == 0 || Bpp == 0) {
il2SetError(IL_INVALID_PARAM);
return IL_FALSE;
}*/
////
if (Width == 0) Width = 1;
if (Height == 0) Height = 1;
if (Depth == 0) Depth = 1;
Image->Width = Width;
Image->Height = Height;
Image->Depth = Depth;
Image->Bpp = Bpp;
Image->Bpc = BpcType;
Image->Bps = Width * Bpp * Image->Bpc;
Image->SizeOfPlane = Image->Bps * Height;
Image->SizeOfData = Image->SizeOfPlane * Depth;
Image->Format = Format;
Image->Type = Type;
Image->Data = (ILubyte*)ialloc(Image->SizeOfData);
if (Image->Data != NULL) {
if (Data != NULL) {
memcpy(Image->Data, Data, Image->SizeOfData);
}
return IL_TRUE;
} else {
return IL_FALSE;
}
}
请发表评论