added dependencies and build files (loosely inspired by Sebastian Dorda's love-native-android)

This commit is contained in:
Martin Felis
2013-12-05 17:58:37 +01:00
parent 9604528a96
commit 95a086a47f
3700 changed files with 1691119 additions and 0 deletions
+131
View File
@@ -0,0 +1,131 @@
//-----------------------------------------------------------------------------
//
// ImageLib Sources
// Copyright (C) 2000 by Denton Woods
// Last modified: 11/23/2000 <--Y2K Compliant! =]
//
// Filename: openilut/allegro.c
//
// Description: Allegro functions for images
//
//-----------------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif //HAVE_CONFIG_H
#ifdef ILUT_USE_ALLEGRO
#include "ilut_allegro.h"
#endif
#include "ilut_internal.h"
#ifdef ILUT_USE_ALLEGRO
ILboolean ilConvertPal(ILenum DestFormat);
// Does not account for converting luminance...
BITMAP* ILAPIENTRY ilutConvertToAlleg(PALETTE Pal)
{
BITMAP *Bitmap;
ILimage *TempImage;
ILuint i = 0, j = 0;
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return NULL;
}
// Should be IL_BGR(A), but Djgpp screws up somewhere along the line.
if (ilutCurImage->Format == IL_RGB || ilutCurImage->Format == IL_RGBA) {
iluSwapColours();
}
if (ilutCurImage->Origin == IL_ORIGIN_LOWER_LEFT)
iluFlipImage();
if (ilutCurImage->Type > IL_UNSIGNED_BYTE) {} // Can't do anything about this right now...
if (ilutCurImage->Type == IL_BYTE) {} // Can't do anything about this right now...
Bitmap = create_bitmap_ex(ilutCurImage->Bpp * 8, ilutCurImage->Width, ilutCurImage->Height);
if (Bitmap == NULL) {
return IL_FALSE;
}
memcpy(Bitmap->dat, ilutCurImage->Data, ilutCurImage->SizeOfData);
// Should we make this toggleable?
if (ilutCurImage->Bpp == 8 && ilutCurImage->Pal.PalType != IL_PAL_NONE) {
// Use the image's palette if there is one
// ilConvertPal is destructive to the original image
// @TODO: Use new ilCopyPal!!!
TempImage = ilNewImage(ilutCurImage->Width, ilutCurImage->Height, ilutCurImage->Depth, ilutCurImage->Bpp, 1);
ilCopyImageAttr(TempImage, ilutCurImage);
ilSetCurImage(TempImage);
if (!ilConvertPal(IL_PAL_RGB24)) {
destroy_bitmap(Bitmap);
ilSetError(ILUT_ILLEGAL_OPERATION);
return NULL;
}
for (; i < ilutCurImage->Pal.PalSize && i < 768; i += 3, j++) {
Pal[j].r = TempImage->Pal.Palette[i+0];
Pal[j].g = TempImage->Pal.Palette[i+1];
Pal[j].b = TempImage->Pal.Palette[i+2];
Pal[j].filler = 255;
}
ilCloseImage(TempImage);
ilSetCurImage(ilutCurImage);
}
return Bitmap;
}
#ifndef _WIN32_WCE
BITMAP* ILAPIENTRY ilutAllegLoadImage(ILstring FileName)
{
ILuint ImgId;
PALETTE Pal;
ilGenImages(1, &ImgId);
ilBindImage(ImgId);
if (!ilLoadImage(FileName)) {
ilDeleteImages(1, &ImgId);
return 0;
}
ilDeleteImages(1, &ImgId);
return ilutConvertToAlleg(Pal);
}
#endif//_WIN32_WCE
// Unfinished
ILboolean ILAPIENTRY ilutAllegFromBitmap(BITMAP *Bitmap)
{
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
if (Bitmap == NULL || Bitmap->w == 0 || Bitmap->h == 0) {
ilSetError(ILUT_INVALID_PARAM);
return IL_FALSE;
}
if (!ilTexImage(Bitmap->w, Bitmap->h, 1, 3, IL_RGB, IL_UNSIGNED_BYTE, NULL))
return IL_FALSE;
ilutCurImage->Origin = IL_ORIGIN_LOWER_LEFT; // I have no idea.
return IL_TRUE;
}
#endif//ILUT_USE_ALLEGRO
+542
View File
@@ -0,0 +1,542 @@
//-----------------------------------------------------------------------------
//
// ImageLib Utility Toolkit Sources
// Copyright (C) 2000-2008 by Denton Woods
// Last modified: 12/25/2001
//
// Filename: src-ILUT/src/ilut_directx.c
//
// Description: DirectX 8 functions for textures
//
//-----------------------------------------------------------------------------
#include "ilut_internal.h"
#ifdef ILUT_USE_DIRECTX8
#include <d3d8.h>
//#include <d3dx8tex.h>
//pragma comment(lib, "d3d8.lib")
//pragma comment(lib, "d3dx8.lib")
ILimage* MakeD3D8Compliant(IDirect3DDevice8 *Device, D3DFORMAT *DestFormat);
ILenum GetD3D8Compat(ILenum Format);
D3DFORMAT GetD3DFormat(ILenum Format);
ILboolean iD3D8CreateMipmaps(IDirect3DTexture8 *Texture, ILimage *Image);
ILboolean FormatsDX8Checked = IL_FALSE;
ILboolean FormatsDX8supported[6] =
{ IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE };
D3DFORMAT FormatsDX8[6] =
{ D3DFMT_R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_L8, D3DFMT_DXT1, D3DFMT_DXT3, D3DFMT_DXT5 };
ILboolean ilutD3D8Init()
{
return IL_TRUE;
}
ILvoid CheckFormatsDX8(IDirect3DDevice8 *Device)
{
D3DDISPLAYMODE DispMode;
HRESULT hr;
IDirect3D8 *TestD3D8;
ILuint i;
IDirect3DDevice8_GetDirect3D(Device, (IDirect3D8**)&TestD3D8);
IDirect3DDevice8_GetDisplayMode(Device, &DispMode);
for (i = 0; i < 6; i++) {
hr = IDirect3D8_CheckDeviceFormat(TestD3D8, D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, DispMode.Format, 0, D3DRTYPE_TEXTURE, FormatsDX8[i]);
FormatsDX8supported[i] = SUCCEEDED(hr);
}
IDirect3D8_Release(TestD3D8);
FormatsDX8Checked = IL_TRUE;
return;
}
#ifndef _WIN32_WCE
ILboolean ILAPIENTRY ilutD3D8TexFromFile(IDirect3DDevice8 *Device, char *FileName, IDirect3DTexture8 **Texture)
{
iBindImageTemp();
if (!ilLoadImage(FileName))
return IL_FALSE;
*Texture = ilutD3D8Texture(Device);
return IL_TRUE;
}
#endif//_WIN32_WCE
#ifndef _WIN32_WCE
ILboolean ILAPIENTRY ilutD3D8VolTexFromFile(IDirect3DDevice8 *Device, char *FileName, IDirect3DVolumeTexture8 **Texture)
{
iBindImageTemp();
if (!ilLoadImage(FileName))
return IL_FALSE;
*Texture = ilutD3D8VolumeTexture(Device);
return IL_TRUE;
}
#endif//_WIN32_WCE
ILboolean ILAPIENTRY ilutD3D8TexFromFileInMemory(IDirect3DDevice8 *Device, ILvoid *Lump, ILuint Size, IDirect3DTexture8 **Texture)
{
iBindImageTemp();
if (!ilLoadL(IL_TYPE_UNKNOWN, Lump, Size))
return IL_FALSE;
*Texture = ilutD3D8Texture(Device);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutD3D8VolTexFromFileInMemory(IDirect3DDevice8 *Device, ILvoid *Lump, ILuint Size, IDirect3DVolumeTexture8 **Texture)
{
iBindImageTemp();
if (!ilLoadL(IL_TYPE_UNKNOWN, Lump, Size))
return IL_FALSE;
*Texture = ilutD3D8VolumeTexture(Device);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutD3D8TexFromResource(IDirect3DDevice8 *Device, HMODULE SrcModule, char *SrcResource, IDirect3DTexture8 **Texture)
{
HRSRC Resource;
ILubyte *Data;
iBindImageTemp();
Resource = (HRSRC)LoadResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP));
Data = (ILubyte*)LockResource(Resource);
if (!ilLoadL(IL_TYPE_UNKNOWN, Data, SizeofResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP))))
return IL_FALSE;
*Texture = ilutD3D8Texture(Device);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutD3D8VolTexFromResource(IDirect3DDevice8 *Device, HMODULE SrcModule, char *SrcResource, IDirect3DVolumeTexture8 **Texture)
{
HRSRC Resource;
ILubyte *Data;
iBindImageTemp();
Resource = (HRSRC)LoadResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP));
Data = (ILubyte*)LockResource(Resource);
if (!ilLoadL(IL_TYPE_UNKNOWN, Data, SizeofResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP))))
return IL_FALSE;
*Texture = ilutD3D8VolumeTexture(Device);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutD3D8TexFromFileHandle(IDirect3DDevice8 *Device, ILHANDLE File, IDirect3DTexture8 **Texture)
{
iBindImageTemp();
if (!ilLoadF(IL_TYPE_UNKNOWN, File))
return IL_FALSE;
*Texture = ilutD3D8Texture(Device);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutD3D8VolTexFromFileHandle(IDirect3DDevice8 *Device, ILHANDLE File, IDirect3DVolumeTexture8 **Texture)
{
iBindImageTemp();
if (!ilLoadF(IL_TYPE_UNKNOWN, File))
return IL_FALSE;
*Texture = ilutD3D8VolumeTexture(Device);
return IL_TRUE;
}
D3DFORMAT D3DGetDXTCNumDX8(ILenum DXTCFormat)
{
switch (DXTCFormat)
{
case IL_DXT1:
return D3DFMT_DXT1;
case IL_DXT3:
return D3DFMT_DXT3;
case IL_DXT5:
return D3DFMT_DXT5;
}
return D3DFMT_UNKNOWN;
}
IDirect3DTexture8* ILAPIENTRY ilutD3D8Texture(IDirect3DDevice8 *Device)
{
IDirect3DTexture8 *Texture;
D3DLOCKED_RECT Rect;
D3DFORMAT Format;
ILimage *Image;
ILenum DXTCFormat;
ILuint Size;
ILubyte *Buffer;
Image = ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return NULL;
}
if (!FormatsDX8Checked)
CheckFormatsDX8(Device);
if (ilutGetBoolean(ILUT_D3D_USE_DXTC) && FormatsDX8supported[3] && FormatsDX8supported[4] && FormatsDX8supported[5]) {
if (ilutCurImage->DxtcData != NULL && ilutCurImage->DxtcSize != 0) {
Format = D3DGetDXTCNumDX8(ilutCurImage->DxtcFormat);
if (FAILED(IDirect3DDevice8_CreateTexture(Device, ilutCurImage->Width,
ilutCurImage->Height, ilutGetInteger(ILUT_D3D_MIPLEVELS), 0, Format,
(D3DPOOL)ilutGetInteger(ILUT_D3D_POOL), &Texture)))
return NULL;
if (FAILED(IDirect3DTexture8_LockRect(Texture, 0, &Rect, NULL, 0)))
return NULL;
memcpy(Rect.pBits, ilutCurImage->DxtcData, ilutCurImage->DxtcSize);
goto success;
}
if (ilutGetBoolean(ILUT_D3D_GEN_DXTC)) {
DXTCFormat = ilutGetInteger(ILUT_DXTC_FORMAT);
Size = ilGetDXTCData(NULL, 0, DXTCFormat);
if (Size != 0) {
Buffer = (ILubyte*)ialloc(Size);
if (Buffer == NULL)
return NULL;
Size = ilGetDXTCData(Buffer, Size, DXTCFormat);
if (Size == 0) {
ifree(Buffer);
return NULL;
}
Format = D3DGetDXTCNumDX8(DXTCFormat);
if (FAILED(IDirect3DDevice8_CreateTexture(Device, ilutCurImage->Width,
ilutCurImage->Height, ilutGetInteger(ILUT_D3D_MIPLEVELS), 0, Format,
(D3DPOOL)ilutGetInteger(ILUT_D3D_POOL), &Texture))) {
ifree(Buffer);
return NULL;
}
if (FAILED(IDirect3DTexture8_LockRect(Texture, 0, &Rect, NULL, 0))) {
ifree(Buffer);
return NULL;
}
memcpy(Rect.pBits, Buffer, Size);
ifree(Buffer);
goto success;
}
}
}
Image = MakeD3D8Compliant(Device, &Format);
if (Image == NULL) {
if (Image != ilutCurImage)
ilCloseImage(Image);
return NULL;
}
if (FAILED(IDirect3DDevice8_CreateTexture(Device, Image->Width, Image->Height,
ilutGetInteger(ILUT_D3D_MIPLEVELS), 0, Format, (D3DPOOL)ilutGetInteger(ILUT_D3D_POOL), &Texture))) {
if (Image != ilutCurImage)
ilCloseImage(Image);
return NULL;
}
if (FAILED(IDirect3DTexture8_LockRect(Texture, 0, &Rect, NULL, 0)))
return NULL;
memcpy(Rect.pBits, Image->Data, Image->SizeOfPlane);
success:
IDirect3DTexture8_UnlockRect(Texture, 0);
// Just let D3DX filter for us.
//D3DXFilterTexture(Texture, NULL, D3DX_DEFAULT, D3DX_FILTER_BOX);
iD3D8CreateMipmaps(Texture, Image);
if (Image != ilutCurImage)
ilCloseImage(Image);
return Texture;
}
IDirect3DVolumeTexture8* ILAPIENTRY ilutD3D8VolumeTexture(IDirect3DDevice8 *Device)
{
IDirect3DVolumeTexture8 *Texture;
D3DLOCKED_BOX Box;
D3DFORMAT Format;
ILimage *Image;
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return NULL;
}
if (!FormatsDX8Checked)
CheckFormatsDX8(Device);
Image = MakeD3D8Compliant(Device, &Format);
if (Image == NULL)
return NULL;
if (FAILED(IDirect3DDevice8_CreateVolumeTexture(Device, Image->Width, Image->Height,
Image->Depth, 1, 0, Format, (D3DPOOL)ilutGetInteger(ILUT_D3D_POOL), &Texture)))
return NULL;
if (FAILED(IDirect3DVolumeTexture8_LockBox(Texture, 0, &Box, NULL, 0)))
return NULL;
memcpy(Box.pBits, Image->Data, Image->SizeOfData);
if (!IDirect3DVolumeTexture8_UnlockBox(Texture, 0))
return IL_FALSE;
// We don't want to have mipmaps for such a large image.
if (Image != ilutCurImage)
ilCloseImage(Image);
return Texture;
}
ILimage *MakeD3D8Compliant(IDirect3DDevice8 *Device, D3DFORMAT *DestFormat)
{
ILimage *Converted, *Scaled, *CurImage;
*DestFormat = D3DFMT_A8R8G8B8;
// Images must be in BGRA format.
if (ilutCurImage->Format != IL_BGRA) {
Converted = iConvertImage(ilutCurImage, IL_BGRA, IL_UNSIGNED_BYTE);
if (Converted == NULL)
return NULL;
}
else {
Converted = ilutCurImage;
}
// Images must have their origin in the upper left.
if (Converted->Origin != IL_ORIGIN_UPPER_LEFT) {
CurImage = ilutCurImage;
ilSetCurImage(Converted);
iluFlipImage();
ilSetCurImage(CurImage);
}
// Images must have powers-of-2 dimensions.
if (ilNextPower2(ilutCurImage->Width) != ilutCurImage->Width ||
ilNextPower2(ilutCurImage->Height) != ilutCurImage->Height ||
ilNextPower2(ilutCurImage->Depth) != ilutCurImage->Depth) {
Scaled = iluScale_(Converted, ilNextPower2(ilutCurImage->Width),
ilNextPower2(ilutCurImage->Height), ilNextPower2(ilutCurImage->Depth));
if (Converted != ilutCurImage) {
ilCloseImage(Converted);
}
if (Scaled == NULL) {
return NULL;
}
Converted = Scaled;
}
return Converted;
}
ILboolean iD3D8CreateMipmaps(IDirect3DTexture8 *Texture, ILimage *Image)
{
D3DLOCKED_RECT Rect;
D3DSURFACE_DESC Desc;
ILuint NumMips, Width, Height, i;
ILimage *CurImage, *MipImage, *Temp;
NumMips = IDirect3DTexture8_GetLevelCount(Texture);
Width = Image->Width;
Height = Image->Height;
CurImage = ilGetCurImage();
MipImage = ilCopyImage_(CurImage);
ilSetCurImage(MipImage);
if (!iluBuildMipmaps()) {
ilCloseImage(MipImage);
ilSetCurImage(CurImage);
return IL_FALSE;
}
ilSetCurImage(CurImage);
Temp = MipImage->Mipmaps;
// Counts the base texture as 1.
for (i = 1; i < NumMips && Temp != NULL; i++) {
if (FAILED(IDirect3DTexture8_LockRect(Texture, i, &Rect, NULL, 0)))
return IL_FALSE;
Width = IL_MAX(1, Width / 2);
Height = IL_MAX(1, Height / 2);
IDirect3DTexture8_GetLevelDesc(Texture, i, &Desc);
if (Desc.Width != Width || Desc.Height != Height) {
IDirect3DTexture8_UnlockRect(Texture, i);
return IL_FALSE;
}
memcpy(Rect.pBits, Temp->Data, Temp->SizeOfData);
IDirect3DTexture8_UnlockRect(Texture, i);
Temp = Temp->Next;
}
ilCloseImage(MipImage);
return IL_TRUE;
}
//
// SaveSurfaceToFile.cpp
//
// Copyright (c) 2001 David Galeano
//
// Permission to use, copy, modify and distribute this software
// is hereby granted, provided that both the copyright notice and
// this permission notice appear in all copies of the software,
// derivative works or modified versions.
//
// THE AUTHOR ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
// CONDITION AND DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
// WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
//
ILAPI ILboolean ILAPIENTRY ilutD3D8LoadSurface(IDirect3DDevice8 *Device, IDirect3DSurface8 *Surface)
{
HRESULT hr;
D3DSURFACE_DESC d3dsd;
LPDIRECT3DSURFACE8 SurfaceCopy;
D3DLOCKED_RECT d3dLR;
ILboolean bHasAlpha;
ILubyte *Image, *ImageAux, *Data;
ILuint y, x;
ILushort dwColor;
IDirect3DSurface8_GetDesc(Surface, &d3dsd);
bHasAlpha = (d3dsd.Format == D3DFMT_A8R8G8B8 || d3dsd.Format == D3DFMT_A1R5G5B5);
if (bHasAlpha) {
if (!ilTexImage(d3dsd.Width, d3dsd.Height, 1, 4, IL_BGRA, IL_UNSIGNED_BYTE, NULL)) {
return IL_FALSE;
}
}
else {
if (!ilTexImage(d3dsd.Width, d3dsd.Height, 1, 3, IL_BGR, IL_UNSIGNED_BYTE, NULL)) {
return IL_FALSE;
}
}
hr = IDirect3DDevice8_CreateImageSurface(Device, d3dsd.Width, d3dsd.Height, d3dsd.Format, &SurfaceCopy);
if (FAILED(hr)) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
hr = IDirect3DDevice8_CopyRects(Device, Surface, NULL, 0, SurfaceCopy, NULL);
if (FAILED(hr)) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
hr = IDirect3DSurface8_LockRect(SurfaceCopy, &d3dLR, NULL, D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY);
if (FAILED(hr)) {
IDirect3DSurface8_Release(SurfaceCopy);
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
Image = (ILubyte*)d3dLR.pBits;
Data = ilutCurImage->Data;
for (y = 0; y < d3dsd.Height; y++) {
if (d3dsd.Format == D3DFMT_X8R8G8B8) {
ImageAux = Image;
for (x = 0; x < d3dsd.Width; x++) {
Data[0] = ImageAux[0];
Data[1] = ImageAux[1];
Data[2] = ImageAux[2];
Data += 3;
ImageAux += 4;
}
}
else if (d3dsd.Format == D3DFMT_A8R8G8B8) {
memcpy(Data, Image, d3dsd.Width * 4);
}
else if (d3dsd.Format == D3DFMT_R5G6B5) {
ImageAux = Image;
for (x = 0; x < d3dsd.Width; x++) {
dwColor = *((ILushort*)ImageAux);
Data[0] = (ILubyte)((dwColor&0x001f)<<3);
Data[1] = (ILubyte)(((dwColor&0x7e0)>>5)<<2);
Data[2] = (ILubyte)(((dwColor&0xf800)>>11)<<3);
Data += 3;
ImageAux += 2;
}
}
else if (d3dsd.Format == D3DFMT_X1R5G5B5) {
ImageAux = Image;
for (x = 0; x < d3dsd.Width; x++) {
dwColor = *((ILushort*)ImageAux);
Data[0] = (ILubyte)((dwColor&0x001f)<<3);
Data[1] = (ILubyte)(((dwColor&0x3e0)>>5)<<3);
Data[2] = (ILubyte)(((dwColor&0x7c00)>>10)<<3);
Data += 3;
ImageAux += 2;
}
}
else if (d3dsd.Format == D3DFMT_A1R5G5B5) {
ImageAux = Image;
for (x = 0; x < d3dsd.Width; x++) {
dwColor = *((ILushort*)ImageAux);
Data[0] = (ILubyte)((dwColor&0x001f)<<3);
Data[1] = (ILubyte)(((dwColor&0x3e0)>>5)<<3);
Data[2] = (ILubyte)(((dwColor&0x7c00)>>10)<<3);
Data[3] = (ILubyte)(((dwColor&0x8000)>>15)*255);
Data += 4;
ImageAux += 2;
}
}
Image += d3dLR.Pitch;
}
IDirect3DSurface8_UnlockRect(SurfaceCopy);
IDirect3DSurface8_Release(SurfaceCopy);
return IL_TRUE;
}
#endif//ILUT_USE_DIRECTX8
@@ -0,0 +1,886 @@
//-----------------------------------------------------------------------------
//
// ImageLib Utility Toolkit Sources
// Copyright (C) 2000-2009 by Denton Woods
// Last modified: 01/30/2009
//
// Filename: src-ILUT/src/ilut_directx9.c
//
// Description: DirectX 9 functions for textures
//
//-----------------------------------------------------------------------------
#include "ilut_internal.h"
#ifdef ILUT_USE_DIRECTX9
#include <d3d9.h>
//#include <d3dx9math.h>
//#include <d3dx9tex.h>
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
ILimage* MakeD3D9Compliant(IDirect3DDevice9 *Device, D3DFORMAT *DestFormat);
ILenum GetD3D9Compat(ILenum Format);
//D3DFORMAT GetD3DFormat(ILenum Format);
D3DFORMAT D3DGetDXTCNumDX9(ILenum DXTCFormat);
ILenum D3DGetDXTCFormat(D3DFORMAT DXTCNum);
ILboolean iD3D9CreateMipmaps(IDirect3DTexture9 *Texture, ILimage *Image);
IDirect3DTexture9* iD3DMakeTexture( IDirect3DDevice9 *Device, void *Data, ILuint DLen, ILuint Width, ILuint Height, D3DFORMAT Format, D3DPOOL Pool, ILuint Levels );
#define ILUT_TEXTUREFORMAT_D3D9_COUNT 7
ILboolean FormatsDX9Checked = IL_FALSE;
ILboolean FormatsDX9supported[ILUT_TEXTUREFORMAT_D3D9_COUNT] =
{ IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE, IL_FALSE };
D3DFORMAT FormatsDX9[ILUT_TEXTUREFORMAT_D3D9_COUNT] =
{ D3DFMT_R8G8B8, D3DFMT_A8R8G8B8, D3DFMT_L8, D3DFMT_DXT1, D3DFMT_DXT3, D3DFMT_DXT5, D3DFMT_A16B16G16R16F};
ILboolean ilutD3D9Init()
{
return IL_TRUE;
}
void CheckFormatsDX9(IDirect3DDevice9 *Device)
{
D3DDISPLAYMODE DispMode;
HRESULT hr;
IDirect3D9 *TestD3D9;
ILuint i;
IDirect3DDevice9_GetDirect3D(Device, (IDirect3D9**)&TestD3D9);
IDirect3DDevice9_GetDisplayMode(Device, 0, &DispMode);
for (i = 0; i < ILUT_TEXTUREFORMAT_D3D9_COUNT; i++) {
hr = IDirect3D9_CheckDeviceFormat(TestD3D9, D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, DispMode.Format, 0, D3DRTYPE_TEXTURE, FormatsDX9[i]);
FormatsDX9supported[i] = (ILboolean)SUCCEEDED(hr);
}
IDirect3D9_Release(TestD3D9);
FormatsDX9Checked = IL_TRUE;
return;
}
#ifndef _WIN32_WCE
ILboolean ILAPIENTRY ilutD3D9TexFromFile(IDirect3DDevice9 *Device, ILconst_string FileName, IDirect3DTexture9 **Texture)
{
iBindImageTemp();
if (!ilLoadImage(FileName))
return IL_FALSE;
*Texture = ilutD3D9Texture(Device);
return IL_TRUE;
}
#endif//_WIN32_WCE
#ifndef _WIN32_WCE
ILboolean ILAPIENTRY ilutD3D9CubeTexFromFile(IDirect3DDevice9 *Device,
ILconst_string FileName, IDirect3DCubeTexture9 **Texture)
{
iBindImageTemp();
if (!ilLoadImage(FileName))
return IL_FALSE;
*Texture = ilutD3D9CubeTexture(Device);
return IL_TRUE;
}
#endif//_WIN32_WCE
ILboolean ILAPIENTRY ilutD3D9CubeTexFromFileInMemory(IDirect3DDevice9 *Device,
void *Lump, ILuint Size, IDirect3DCubeTexture9 **Texture)
{
iBindImageTemp();
if( !ilLoadL(IL_TYPE_UNKNOWN, Lump, Size) )
return IL_FALSE;
*Texture = ilutD3D9CubeTexture(Device);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutD3D9CubeTexFromResource(IDirect3DDevice9 *Device,
HMODULE SrcModule, ILconst_string SrcResource, IDirect3DCubeTexture9 **Texture)
{
HRSRC Resource;
ILubyte *Data;
iBindImageTemp();
Resource = (HRSRC)LoadResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP));
Data = (ILubyte*)LockResource(Resource);
if (!ilLoadL(IL_TYPE_UNKNOWN, Data, SizeofResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP))))
return IL_FALSE;
*Texture = ilutD3D9CubeTexture(Device);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutD3D9CubeTexFromFileHandle(IDirect3DDevice9 *Device,
ILHANDLE File, IDirect3DCubeTexture9 **Texture)
{
iBindImageTemp();
if( !ilLoadF(IL_TYPE_UNKNOWN, File) )
return IL_FALSE;
*Texture = ilutD3D9CubeTexture(Device);
return IL_TRUE;
}
D3DCUBEMAP_FACES iToD3D9Cube(ILuint cube)
{
switch (cube)
{
case IL_CUBEMAP_POSITIVEX:
return D3DCUBEMAP_FACE_POSITIVE_X;
case IL_CUBEMAP_NEGATIVEX:
return D3DCUBEMAP_FACE_NEGATIVE_X;
case IL_CUBEMAP_POSITIVEY:
return D3DCUBEMAP_FACE_POSITIVE_Y;
case IL_CUBEMAP_NEGATIVEY:
return D3DCUBEMAP_FACE_NEGATIVE_Y;
case IL_CUBEMAP_POSITIVEZ:
return D3DCUBEMAP_FACE_POSITIVE_Z;
case IL_CUBEMAP_NEGATIVEZ:
return D3DCUBEMAP_FACE_NEGATIVE_Z;
default:
return D3DCUBEMAP_FACE_POSITIVE_X; //???
}
}
IDirect3DCubeTexture9* ILAPIENTRY ilutD3D9CubeTexture(IDirect3DDevice9 *Device)
{
IDirect3DCubeTexture9 *Texture;
D3DLOCKED_RECT Box;
D3DFORMAT Format;
ILimage *StartImage;
ILimage *Image;
int i;
Texture=NULL;
Image=NULL;
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return NULL;
}
if (!FormatsDX9Checked)
CheckFormatsDX9(Device);
Image = MakeD3D9Compliant(Device, &Format);
if (Image == NULL) {
if (Image != ilutCurImage)
ilCloseImage(Image);
return NULL;
}
StartImage = ilutCurImage;
if (FAILED(IDirect3DDevice9_CreateCubeTexture(Device, StartImage->Width,
ilutGetInteger(ILUT_D3D_MIPLEVELS),0,Format, (D3DPOOL)ilutGetInteger(ILUT_D3D_POOL), &Texture, NULL)))
return NULL;
for (i = 0; i < CUBEMAP_SIDES; i++) {
if (ilutCurImage==NULL || ilutCurImage->CubeFlags == 0) {
SAFE_RELEASE(Texture)
return NULL;
}
Image = ilutCurImage;
Image = MakeD3D9Compliant(Device, &Format);
if( Image == NULL ) {
SAFE_RELEASE(Texture)
return NULL;
}
if( FAILED(IDirect3DCubeTexture9_LockRect(Texture,iToD3D9Cube(Image->CubeFlags), 0, &Box, NULL, /*D3DLOCK_DISCARD*/0))) {
SAFE_RELEASE(Texture)
return NULL;
}
memcpy(Box.pBits, Image->Data, Image->SizeOfData);
if (IDirect3DCubeTexture9_UnlockRect(Texture,iToD3D9Cube(Image->CubeFlags), 0) != D3D_OK) {
SAFE_RELEASE(Texture)
return IL_FALSE;
}
ilutCurImage=ilutCurImage->Faces;
}
ilutCurImage = StartImage;
// We don't want to have mipmaps for such a large image.
//if (Image != ilutCurImage)
// ilCloseImage(Image);
return Texture;
}
#ifndef _WIN32_WCE
ILboolean ILAPIENTRY ilutD3D9VolTexFromFile(IDirect3DDevice9 *Device, ILconst_string FileName, IDirect3DVolumeTexture9 **Texture) {
iBindImageTemp();
if (!ilLoadImage(FileName))
return IL_FALSE;
*Texture = ilutD3D9VolumeTexture(Device);
return IL_TRUE;
}
#endif//_WIN32_WCE
ILboolean ILAPIENTRY ilutD3D9TexFromFileInMemory(IDirect3DDevice9 *Device, void *Lump, ILuint Size, IDirect3DTexture9 **Texture)
{
iBindImageTemp();
if (!ilLoadL(IL_TYPE_UNKNOWN, Lump, Size))
return IL_FALSE;
*Texture = ilutD3D9Texture(Device);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutD3D9VolTexFromFileInMemory(IDirect3DDevice9 *Device, void *Lump, ILuint Size, IDirect3DVolumeTexture9 **Texture)
{
iBindImageTemp();
if (!ilLoadL(IL_TYPE_UNKNOWN, Lump, Size))
return IL_FALSE;
*Texture = ilutD3D9VolumeTexture(Device);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutD3D9TexFromResource(IDirect3DDevice9 *Device, HMODULE SrcModule, ILconst_string SrcResource, IDirect3DTexture9 **Texture)
{
HRSRC Resource;
ILubyte *Data;
iBindImageTemp();
Resource = (HRSRC)LoadResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP));
Data = (ILubyte*)LockResource(Resource);
if (!ilLoadL(IL_TYPE_UNKNOWN, Data, SizeofResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP))))
return IL_FALSE;
*Texture = ilutD3D9Texture(Device);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutD3D9VolTexFromResource(IDirect3DDevice9 *Device, HMODULE SrcModule, ILconst_string SrcResource, IDirect3DVolumeTexture9 **Texture)
{
HRSRC Resource;
ILubyte *Data;
iBindImageTemp();
Resource = (HRSRC)LoadResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP));
Data = (ILubyte*)LockResource(Resource);
if (!ilLoadL(IL_TYPE_UNKNOWN, Data, SizeofResource(SrcModule, FindResource(SrcModule, SrcResource, RT_BITMAP))))
return IL_FALSE;
*Texture = ilutD3D9VolumeTexture(Device);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutD3D9TexFromFileHandle(IDirect3DDevice9 *Device, ILHANDLE File, IDirect3DTexture9 **Texture)
{
iBindImageTemp();
if (!ilLoadF(IL_TYPE_UNKNOWN, File))
return IL_FALSE;
*Texture = ilutD3D9Texture(Device);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutD3D9VolTexFromFileHandle(IDirect3DDevice9 *Device, ILHANDLE File, IDirect3DVolumeTexture9 **Texture)
{
iBindImageTemp();
if (!ilLoadF(IL_TYPE_UNKNOWN, File))
return IL_FALSE;
*Texture = ilutD3D9VolumeTexture(Device);
return IL_TRUE;
}
D3DFORMAT D3DGetDXTCNumDX9(ILenum DXTCFormat)
{
switch (DXTCFormat)
{
case IL_DXT1:
return D3DFMT_DXT1;
case IL_DXT3:
return D3DFMT_DXT3;
case IL_DXT5:
return D3DFMT_DXT5;
}
return D3DFMT_UNKNOWN;
}
ILenum D3DGetDXTCFormat(D3DFORMAT DXTCNum)
{
switch (DXTCNum)
{
case D3DFMT_DXT1:
return IL_DXT1;
case D3DFMT_DXT3:
return IL_DXT3;
case D3DFMT_DXT5:
return IL_DXT5;
}
return D3DFMT_UNKNOWN;
}
IDirect3DTexture9* iD3DMakeTexture( IDirect3DDevice9 *Device, void *Data, ILuint DLen, ILuint Width, ILuint Height, D3DFORMAT Format, D3DPOOL Pool, ILuint Levels )
{
IDirect3DTexture9 *Texture;
D3DLOCKED_RECT Rect;
if (FAILED(IDirect3DDevice9_CreateTexture(Device, Width, Height, Levels,
0, Format, Pool, &Texture, NULL)))
return NULL;
if (FAILED(IDirect3DTexture9_LockRect(Texture, 0, &Rect, NULL, 0)))
return NULL;
memcpy(Rect.pBits, Data, DLen);
IDirect3DTexture9_UnlockRect(Texture, 0);
return Texture;
}
IDirect3DTexture9* ILAPIENTRY ilutD3D9Texture(IDirect3DDevice9 *Device)
{
IDirect3DTexture9 *Texture;
// D3DLOCKED_RECT Rect;
D3DFORMAT Format;
ILimage *Image;
ILenum DXTCFormat;
ILuint Size;
ILubyte *Buffer;
Image = ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return NULL;
}
if (!FormatsDX9Checked)
CheckFormatsDX9(Device);
if (ilutGetBoolean(ILUT_D3D_USE_DXTC) && FormatsDX9supported[3] && FormatsDX9supported[4] && FormatsDX9supported[5]) {
if (ilutCurImage->DxtcData != NULL && ilutCurImage->DxtcSize != 0) {
ILuint dxtcFormat = ilutGetInteger(ILUT_DXTC_FORMAT);
Format = D3DGetDXTCNumDX9(ilutCurImage->DxtcFormat);
ilutSetInteger(ILUT_DXTC_FORMAT, ilutCurImage->DxtcFormat);
Texture = iD3DMakeTexture(Device, ilutCurImage->DxtcData, ilutCurImage->DxtcSize,
ilutCurImage->Width, ilutCurImage->Height, Format,
(D3DPOOL)(ilutGetInteger(ILUT_D3D_POOL) == D3DPOOL_DEFAULT ? D3DPOOL_SYSTEMMEM : ilutGetInteger(ILUT_D3D_POOL)), ilutGetInteger(ILUT_D3D_MIPLEVELS));
if (!Texture)
return NULL;
iD3D9CreateMipmaps(Texture, Image);
if (ilutGetInteger(ILUT_D3D_POOL) == D3DPOOL_DEFAULT) {
IDirect3DTexture9 *SysTex = Texture;
// copy texture to device memory
if (FAILED(IDirect3DDevice9_CreateTexture(Device, ilutCurImage->Width,
ilutCurImage->Height, ilutGetInteger(ILUT_D3D_MIPLEVELS), 0, Format,
D3DPOOL_DEFAULT, &Texture, NULL))) {
IDirect3DTexture9_Release(SysTex);
return NULL;
}
if (FAILED(IDirect3DDevice9_UpdateTexture(Device, (LPDIRECT3DBASETEXTURE9)SysTex, (LPDIRECT3DBASETEXTURE9)Texture))) {
IDirect3DTexture9_Release(SysTex);
return NULL;
}
IDirect3DTexture9_Release(SysTex);
}
ilutSetInteger(ILUT_DXTC_FORMAT, dxtcFormat);
goto success;
}
if (ilutGetBoolean(ILUT_D3D_GEN_DXTC)) {
DXTCFormat = ilutGetInteger(ILUT_DXTC_FORMAT);
/*
Image = MakeD3D9Compliant(Device, &Format);
if (Image == NULL) {
if (Image != ilutCurImage)
ilCloseImage(Image);
return NULL;
}
*/
Size = ilGetDXTCData(NULL, 0, DXTCFormat);
if (Size != 0) {
Buffer = (ILubyte*)ialloc(Size);
if (Buffer == NULL)
return NULL;
Size = ilGetDXTCData(Buffer, Size, DXTCFormat);
if (Size == 0) {
ifree(Buffer);
return NULL;
}
Format = D3DGetDXTCNumDX9(DXTCFormat);
Texture = iD3DMakeTexture(Device, Buffer, Size,
ilutCurImage->Width, ilutCurImage->Height, Format,
(D3DPOOL)(ilutGetInteger(ILUT_D3D_POOL) == D3DPOOL_DEFAULT ? D3DPOOL_SYSTEMMEM : ilutGetInteger(ILUT_D3D_POOL)), ilutGetInteger(ILUT_D3D_MIPLEVELS));
if (!Texture)
return NULL;
iD3D9CreateMipmaps(Texture, Image);
if (ilutGetInteger(ILUT_D3D_POOL) == D3DPOOL_DEFAULT) {
IDirect3DTexture9 *SysTex = Texture;
if (FAILED(IDirect3DDevice9_CreateTexture(Device, ilutCurImage->Width,
ilutCurImage->Height, ilutGetInteger(ILUT_D3D_MIPLEVELS), 0, Format,
D3DPOOL_DEFAULT, &Texture, NULL))) {
IDirect3DTexture9_Release(SysTex);
return NULL;
}
if (FAILED(IDirect3DDevice9_UpdateTexture(Device, (LPDIRECT3DBASETEXTURE9)SysTex, (LPDIRECT3DBASETEXTURE9)Texture))) {
IDirect3DTexture9_Release(SysTex);
return NULL;
}
IDirect3DTexture9_Release(SysTex);
}
goto success;
}
}
}
Image = MakeD3D9Compliant(Device, &Format);
if (Image == NULL) {
if (Image != ilutCurImage)
ilCloseImage(Image);
return NULL;
}
Texture = iD3DMakeTexture(Device, Image->Data, Image->SizeOfPlane,
Image->Width, Image->Height, Format,
(D3DPOOL)(ilutGetInteger(ILUT_D3D_POOL) == D3DPOOL_DEFAULT ? D3DPOOL_SYSTEMMEM : ilutGetInteger(ILUT_D3D_POOL)), ilutGetInteger(ILUT_D3D_MIPLEVELS));
if (!Texture)
return NULL;
iD3D9CreateMipmaps(Texture, Image);
if (ilutGetInteger(ILUT_D3D_POOL) == D3DPOOL_DEFAULT) {
IDirect3DTexture9 *SysTex = Texture;
// create texture in system memory
if (FAILED(IDirect3DDevice9_CreateTexture(Device, Image->Width,
Image->Height, ilutGetInteger(ILUT_D3D_MIPLEVELS), 0, Format,
(D3DPOOL)ilutGetInteger(ILUT_D3D_POOL), &Texture, NULL))) {
IDirect3DTexture9_Release(SysTex);
return NULL;
}
if (FAILED(IDirect3DDevice9_UpdateTexture(Device, (LPDIRECT3DBASETEXTURE9)SysTex, (LPDIRECT3DBASETEXTURE9)Texture))) {
IDirect3DTexture9_Release(SysTex);
return NULL;
}
IDirect3DTexture9_Release(SysTex);
}
// if (Image != ilutCurImage)
// ilCloseImage(Image);
success:
if (Image != ilutCurImage)
ilCloseImage(Image);
return Texture;
}
IDirect3DVolumeTexture9* ILAPIENTRY ilutD3D9VolumeTexture(IDirect3DDevice9 *Device)
{
IDirect3DVolumeTexture9 *Texture;
D3DLOCKED_BOX Box;
D3DFORMAT Format;
ILimage *Image;
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return NULL;
}
if (!FormatsDX9Checked)
CheckFormatsDX9(Device);
Image = MakeD3D9Compliant(Device, &Format);
if (Image == NULL)
return NULL;
if (FAILED(IDirect3DDevice9_CreateVolumeTexture(Device, Image->Width, Image->Height,
Image->Depth, 1, 0, Format, (D3DPOOL)ilutGetInteger(ILUT_D3D_POOL), &Texture, NULL)))
return NULL;
if (FAILED(IDirect3DVolumeTexture9_LockBox(Texture, 0, &Box, NULL, 0)))
return NULL;
memcpy(Box.pBits, Image->Data, Image->SizeOfData);
if (!IDirect3DVolumeTexture9_UnlockBox(Texture, 0))
return IL_FALSE;
// We don't want to have mipmaps for such a large image.
if (Image != ilutCurImage)
ilCloseImage(Image);
return Texture;
}
ILimage *MakeD3D9Compliant(IDirect3DDevice9 *Device, D3DFORMAT *DestFormat)
{
ILuint color;
ILimage *Converted, *Scaled, *CurImage;
ILuint nConversionType, ilutFormat;
ILboolean bForceIntegerFormat = ilutGetBoolean(ILUT_FORCE_INTEGER_FORMAT);
Device;
ilutFormat = ilutCurImage->Format;
nConversionType = ilutCurImage->Type;
if (!ilutCurImage)
return NULL;
switch (ilutCurImage->Type)
{
case IL_UNSIGNED_BYTE:
case IL_BYTE:
case IL_SHORT:
case IL_UNSIGNED_SHORT:
case IL_INT:
case IL_UNSIGNED_INT:
*DestFormat = D3DFMT_A8R8G8B8;
nConversionType = IL_UNSIGNED_BYTE;
ilutFormat = IL_BGRA;
break;
case IL_FLOAT:
case IL_DOUBLE:
case IL_HALF:
if (bForceIntegerFormat || (!FormatsDX9supported[6]))
{
*DestFormat = D3DFMT_A8R8G8B8;
nConversionType = IL_UNSIGNED_BYTE;
ilutFormat = IL_BGRA;
}
else
{
*DestFormat = D3DFMT_A16B16G16R16F;
nConversionType = IL_HALF;
ilutFormat = IL_RGBA;
//
}
break;
}
// Images must be in BGRA format.
if (((ilutCurImage->Format != ilutFormat))
|| (ilutCurImage->Type != nConversionType))
{
Converted = iConvertImage(ilutCurImage, ilutFormat, nConversionType);
if (Converted == NULL)
return NULL;
}
else
{
Converted = ilutCurImage;
}
// perform alpha key on images if requested
color=ilutGetInteger(ILUT_D3D_ALPHA_KEY_COLOR);
if((color>=0) && (nConversionType == IL_UNSIGNED_BYTE))
{
ILubyte *data;
ILubyte *maxdata;
ILuint t;
data=(Converted->Data);
maxdata=(Converted->Data+Converted->SizeOfData);
while(data<maxdata)
{
t= (data[2]<<16) + (data[1]<<8) + (data[0]) ;
if((t&0x00ffffff)==(color&0x00ffffff))
{
data[0]=0;
data[1]=0;
data[2]=0;
data[3]=0;
}
data+=4;
}
}
// Images must have their origin in the upper left.
if (Converted->Origin != IL_ORIGIN_UPPER_LEFT)
{
CurImage = ilutCurImage;
ilSetCurImage(Converted);
iluFlipImage();
ilSetCurImage(CurImage);
}
// Images must have powers-of-2 dimensions.
if (ilNextPower2(ilutCurImage->Width) != ilutCurImage->Width ||
ilNextPower2(ilutCurImage->Height) != ilutCurImage->Height ||
ilNextPower2(ilutCurImage->Depth) != ilutCurImage->Depth) {
Scaled = iluScale_(Converted, ilNextPower2(ilutCurImage->Width),
ilNextPower2(ilutCurImage->Height), ilNextPower2(ilutCurImage->Depth));
if (Converted != ilutCurImage) {
ilCloseImage(Converted);
}
if (Scaled == NULL) {
return NULL;
}
Converted = Scaled;
}
return Converted;
}
ILboolean iD3D9CreateMipmaps(IDirect3DTexture9 *Texture, ILimage *Image)
{
D3DLOCKED_RECT Rect;
D3DSURFACE_DESC Desc;
ILuint NumMips, srcMips, Width, Height, i;
ILimage *CurImage, *MipImage, *Temp;
ILenum DXTCFormat;
ILuint Size;
ILubyte *Buffer;
ILboolean useDXTC = IL_FALSE;
NumMips = IDirect3DTexture9_GetLevelCount(Texture);
Width = Image->Width;
Height = Image->Height;
if (NumMips == 1)
return IL_TRUE;
CurImage = ilGetCurImage();
MipImage = Image;
iGetIntegervImage(MipImage, IL_NUM_MIPMAPS, (ILint*) &srcMips);
if ( srcMips != NumMips-1) {
MipImage = ilCopyImage_(Image);
ilSetCurImage(MipImage);
if (!iluBuildMipmaps()) {
ilCloseImage(MipImage);
ilSetCurImage(CurImage);
return IL_FALSE;
}
}
// ilSetCurImage(CurImage);
Temp = MipImage->Mipmaps;
if (ilutGetBoolean(ILUT_D3D_USE_DXTC) && FormatsDX9supported[3] && FormatsDX9supported[4] && FormatsDX9supported[5])
useDXTC = IL_TRUE;
// Counts the base texture as 1.
for (i = 1; i < NumMips && Temp != NULL; i++) {
ilSetCurImage(Temp);
if (FAILED(IDirect3DTexture9_LockRect(Texture, i, &Rect, NULL, 0)))
return IL_FALSE;
Width = IL_MAX(1, Width / 2);
Height = IL_MAX(1, Height / 2);
IDirect3DTexture9_GetLevelDesc(Texture, i, &Desc);
if (Desc.Width != Width || Desc.Height != Height) {
IDirect3DTexture9_UnlockRect(Texture, i);
return IL_FALSE;
}
if (useDXTC) {
if (Temp->DxtcData != NULL && Temp->DxtcSize != 0) {
memcpy(Rect.pBits, Temp->DxtcData, Temp->DxtcSize);
} else if (ilutGetBoolean(ILUT_D3D_GEN_DXTC)) {
DXTCFormat = ilutGetInteger(ILUT_DXTC_FORMAT);
Size = ilGetDXTCData(NULL, 0, DXTCFormat);
if (Size != 0) {
Buffer = (ILubyte*)ialloc(Size);
if (Buffer == NULL) {
IDirect3DTexture9_UnlockRect(Texture, i);
return IL_FALSE;
}
Size = ilGetDXTCData(Buffer, Size, DXTCFormat);
if (Size == 0) {
ifree(Buffer);
IDirect3DTexture9_UnlockRect(Texture, i);
return IL_FALSE;
}
memcpy(Rect.pBits, Buffer, Size);
} else {
IDirect3DTexture9_UnlockRect(Texture, i);
return IL_FALSE;
}
} else {
IDirect3DTexture9_UnlockRect(Texture, i);
return IL_FALSE;
}
} else
memcpy(Rect.pBits, Temp->Data, Temp->SizeOfData);
IDirect3DTexture9_UnlockRect(Texture, i);
Temp = Temp->Mipmaps;
}
if (MipImage != Image)
ilCloseImage(MipImage);
ilSetCurImage(CurImage);
return IL_TRUE;
}
//
// SaveSurfaceToFile.cpp
//
// Copyright (c) 2001 David Galeano
//
// Permission to use, copy, modify and distribute this software
// is hereby granted, provided that both the copyright notice and
// this permission notice appear in all copies of the software,
// derivative works or modified versions.
//
// THE AUTHOR ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
// CONDITION AND DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
// WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
//
ILAPI ILboolean ILAPIENTRY ilutD3D9LoadSurface(IDirect3DDevice9 *Device, IDirect3DSurface9 *Surface)
{
HRESULT hr;
D3DSURFACE_DESC d3dsd;
LPDIRECT3DSURFACE9 SurfaceCopy;
D3DLOCKED_RECT d3dLR;
ILboolean bHasAlpha;
ILubyte *Image, *ImageAux, *Data;
ILuint y, x;
ILushort dwColor;
IDirect3DSurface9_GetDesc(Surface, &d3dsd);
bHasAlpha = (ILboolean)(d3dsd.Format == D3DFMT_A8R8G8B8 || d3dsd.Format == D3DFMT_A1R5G5B5);
if (bHasAlpha) {
if (!ilTexImage(d3dsd.Width, d3dsd.Height, 1, 4, IL_BGRA, IL_UNSIGNED_BYTE, NULL)) {
return IL_FALSE;
}
}
else {
if (!ilTexImage(d3dsd.Width, d3dsd.Height, 1, 3, IL_BGR, IL_UNSIGNED_BYTE, NULL)) {
return IL_FALSE;
}
}
hr = IDirect3DDevice9_CreateOffscreenPlainSurface(Device, d3dsd.Width, d3dsd.Height, d3dsd.Format, D3DPOOL_SCRATCH, &SurfaceCopy, NULL);
if (FAILED(hr)) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
hr = IDirect3DDevice9_StretchRect(Device, Surface, NULL, SurfaceCopy, NULL, D3DTEXF_NONE);
if (FAILED(hr)) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
hr = IDirect3DSurface9_LockRect(SurfaceCopy, &d3dLR, NULL, D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_NOSYSLOCK | D3DLOCK_READONLY);
if (FAILED(hr)) {
IDirect3DSurface9_Release(SurfaceCopy);
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
Image = (ILubyte*)d3dLR.pBits;
Data = ilutCurImage->Data;
for (y = 0; y < d3dsd.Height; y++) {
if (d3dsd.Format == D3DFMT_X8R8G8B8) {
ImageAux = Image;
for (x = 0; x < d3dsd.Width; x++) {
Data[0] = ImageAux[0];
Data[1] = ImageAux[1];
Data[2] = ImageAux[2];
Data += 3;
ImageAux += 4;
}
}
else if (d3dsd.Format == D3DFMT_A8R8G8B8) {
memcpy(Data, Image, d3dsd.Width * 4);
}
else if (d3dsd.Format == D3DFMT_R5G6B5) {
ImageAux = Image;
for (x = 0; x < d3dsd.Width; x++) {
dwColor = *((ILushort*)ImageAux);
Data[0] = (ILubyte)((dwColor&0x001f)<<3);
Data[1] = (ILubyte)(((dwColor&0x7e0)>>5)<<2);
Data[2] = (ILubyte)(((dwColor&0xf800)>>11)<<3);
Data += 3;
ImageAux += 2;
}
}
else if (d3dsd.Format == D3DFMT_X1R5G5B5) {
ImageAux = Image;
for (x = 0; x < d3dsd.Width; x++) {
dwColor = *((ILushort*)ImageAux);
Data[0] = (ILubyte)((dwColor&0x001f)<<3);
Data[1] = (ILubyte)(((dwColor&0x3e0)>>5)<<3);
Data[2] = (ILubyte)(((dwColor&0x7c00)>>10)<<3);
Data += 3;
ImageAux += 2;
}
}
else if (d3dsd.Format == D3DFMT_A1R5G5B5) {
ImageAux = Image;
for (x = 0; x < d3dsd.Width; x++) {
dwColor = *((ILushort*)ImageAux);
Data[0] = (ILubyte)((dwColor&0x001f)<<3);
Data[1] = (ILubyte)(((dwColor&0x3e0)>>5)<<3);
Data[2] = (ILubyte)(((dwColor&0x7c00)>>10)<<3);
Data[3] = (ILubyte)(((dwColor&0x8000)>>15)*255);
Data += 4;
ImageAux += 2;
}
}
Image += d3dLR.Pitch;
}
IDirect3DSurface9_UnlockRect(SurfaceCopy);
IDirect3DSurface9_Release(SurfaceCopy);
return IL_TRUE;
}
#endif//ILUT_USE_DIRECTX9
@@ -0,0 +1,18 @@
//-----------------------------------------------------------------------------
//
// ImageLib Utility Toolkit Sources
// Copyright (C) 2000-2002 by Denton Woods
// Last modified: 05/15/2002 <--Y2K Compliant! =]
//
// Filename: src-ILUT/src/ilut_internal.c
//
// Description: Internal stuff for ILUT
//
//-----------------------------------------------------------------------------
#include "ilut_internal.h"
//#if !defined(__APPLE__)
ILimage *ilutCurImage = NULL;
//#endif
+76
View File
@@ -0,0 +1,76 @@
//-----------------------------------------------------------------------------
//
// ImageLib Utility Toolkit Sources
// Copyright (C) 2000-2002 by Denton Woods
// Last modified: 05/28/2001 <--Y2K Compliant! =]
//
// Filename: src-ILUT/src/ilut_main.c
//
// Description: Startup functions
//
//-----------------------------------------------------------------------------
#include "ilut_internal.h"
#ifdef _WIN32
#ifndef IL_STATIC_LIB
//#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#ifdef _WIN32
#if (defined(IL_USE_PRAGMA_LIBS))
#if defined(_MSC_VER) || defined(__BORLANDC__)
#pragma comment(lib, "ILU.lib")
#endif
#endif
#endif
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
hModule; lpReserved;
// only initialize when attached to a new process. setup can cause errors in OpenIL
// when called on a per thread basis
if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
//ilutInit();
}
return TRUE;
}
#endif
#else // Should check if gcc?
// Should be able to condense this...
static void GccMain() __attribute__((constructor));
static void GccMain()
{
//ilutInit();
}
#endif
void ILAPIENTRY ilutInit()
{
ilutDefaultStates(); // Set states to their defaults
// Can cause crashes if DevIL is not initialized yet
#ifdef ILUT_USE_OPENGL
ilutGLInit(); // default renderer is OpenGL
#endif
#ifdef ILUT_USE_DIRECTX8
ilutD3D8Init();
#endif
#ifdef ILUT_USE_DIRECTX9
ilutD3D9Init();
#endif
return;
}
+923
View File
@@ -0,0 +1,923 @@
//-----------------------------------------------------------------------------
//
// ImageLib Utility Toolkit Sources
// Copyright (C) 2000-2002 by Denton Woods
// Last modified: 05/25/2002 <--Y2K Compliant! =]
//
// Filename: src-ILUT/src/ilut_opengl.c
//
// Description: OpenGL functions for images
//
//-----------------------------------------------------------------------------
#include "ilut_opengl.h"
#ifdef ILUT_USE_OPENGL
#include <stdio.h>
#include <string.h>
#ifdef __APPLE__
#include <OpenGL/glext.h>
#include <dlfcn.h>
void *aglGetProcAddress( const GLubyte *name ) {
// deprecated code! and wasn't working
/*NSSymbol symbol;
char* symbolName;
// prepend a '_' for the Unix C symbol mangling convention
int len = strlen((const char*)name);
symbolName = malloc(len + 2);
memcpy(symbolName+1, (const char*)name, len );
symbolName[0] = '_';
symbol = NULL;
if (NSIsSymbolNameDefined(symbolName))
symbol = NSLookupAndBindSymbol(symbolName);
free(symbolName);
return symbol ? NSAddressOfSymbol(symbol) : NULL;
*/
// not deprecated code! and isn't working :(
// now the address is directly known with glext.h include
const int len = strlen((const char*)name);
char *symbolName = calloc(len + 2,1);
memcpy(symbolName+1, (const char*)name, len );
symbolName[0] = '_';
printf("searching %s as %s ",name,symbolName);
void *image = dlopen(NULL,RTLD_LAZY); // brutal solution
if( image == NULL ) {
return NULL;
}
return dlsym(image,symbolName);
}
#endif
#ifdef _MSC_VER
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "Glu32.lib")
// #pragma comment(lib, "freeglut.lib")
#endif
#ifdef linux
// fix for glXGetProcAddressARB
#define GLX_GLXEXT_PROTOTYPES
#include <GL/glx.h>
#endif
//used for automatic texture target detection
#define ILGL_TEXTURE_CUBE_MAP 0x8513
#define ILGL_TEXTURE_BINDING_CUBE_MAP 0x8514
#define ILGL_TEXTURE_CUBE_MAP_POSITIVE_X 0x8515
#define ILGL_TEXTURE_CUBE_MAP_NEGATIVE_X 0x8516
#define ILGL_TEXTURE_CUBE_MAP_POSITIVE_Y 0x8517
#define ILGL_TEXTURE_CUBE_MAP_NEGATIVE_Y 0x8518
#define ILGL_TEXTURE_CUBE_MAP_POSITIVE_Z 0x8519
#define ILGL_TEXTURE_CUBE_MAP_NEGATIVE_Z 0x851A
#define ILGL_CLAMP_TO_EDGE 0x812F
#define ILGL_TEXTURE_WRAP_R 0x8072
// Not defined in OpenGL 1.1 headers.
#define ILGL_TEXTURE_DEPTH 0x8071
#define ILGL_TEXTURE_3D 0x806F
#define ILGL_MAX_3D_TEXTURE_SIZE 0x8073
static ILboolean HasCubemapHardware = IL_FALSE;
static ILboolean HasNonPowerOfTwoHardware = IL_FALSE;
#if defined(_WIN32) || defined(_WIN64) || defined(linux) || defined(__APPLE__)
ILGLTEXIMAGE3DARBPROC ilGLTexImage3D = NULL;
ILGLTEXSUBIMAGE3DARBPROC ilGLTexSubImage3D = NULL;
ILGLCOMPRESSEDTEXIMAGE2DARBPROC ilGLCompressed2D = NULL;
ILGLCOMPRESSEDTEXIMAGE3DARBPROC ilGLCompressed3D = NULL;
#endif
// Absolutely *have* to call this if planning on using the image library with OpenGL.
// Call this after OpenGL has initialized.
ILboolean ilutGLInit()
{
ILint MaxTexW, MaxTexH, MaxTexD = 1;
// Should we really be setting all this ourselves? Seems too much like a glu(t) approach...
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
//#ifndef GL_VERSION_1_3
#if (defined (_WIN32) || defined(_WIN64))
if (IsExtensionSupported("GL_ARB_texture_compression") &&
IsExtensionSupported("GL_EXT_texture_compression_s3tc")) {
ilGLCompressed2D = (ILGLCOMPRESSEDTEXIMAGE2DARBPROC)wglGetProcAddress("glCompressedTexImage2DARB");
}
if (IsExtensionSupported("GL_EXT_texture3D")) {
ilGLTexImage3D = (ILGLTEXIMAGE3DARBPROC)wglGetProcAddress("glTexImage3D");
ilGLTexImage3D = (ILGLTEXIMAGE3DARBPROC)wglGetProcAddress("glTexSubImage3D");
}
if (IsExtensionSupported("GL_ARB_texture_compression") &&
IsExtensionSupported("GL_EXT_texture_compression_s3tc") &&
IsExtensionSupported("GL_EXT_texture3D")) {
ilGLCompressed3D = (ILGLCOMPRESSEDTEXIMAGE3DARBPROC)wglGetProcAddress("glCompressedTexImage3DARB");
}
#elif linux
if (IsExtensionSupported("GL_ARB_texture_compression") &&
IsExtensionSupported("GL_EXT_texture_compression_s3tc")) {
ilGLCompressed2D = (ILGLCOMPRESSEDTEXIMAGE2DARBPROC)
glXGetProcAddressARB((const GLubyte*)"glCompressedTexImage2DARB");
}
if (IsExtensionSupported("GL_EXT_texture3D")) {
ilGLTexImage3D = (ILGLTEXIMAGE3DARBPROC)glXGetProcAddressARB((const GLubyte*)"glTexImage3D");
}
if (IsExtensionSupported("GL_ARB_texture_compression") &&
IsExtensionSupported("GL_EXT_texture_compression_s3tc") &&
IsExtensionSupported("GL_EXT_texture3D")) {
ilGLCompressed3D = (ILGLCOMPRESSEDTEXIMAGE3DARBPROC)glXGetProcAddressARB((const GLubyte*)"glCompressedTexImage3DARB");
}
#elif defined(__APPLE__)
// Mac OS X headers are OpenGL 1.4 compliant already.
ilGLCompressed2D = glCompressedTexImage2DARB;//(ILGLCOMPRESSEDTEXIMAGE2DARBPROC)aglGetProcAddress((const GLubyte *)"glCompressedTexImage2DARB");
ilGLTexImage3D = glTexImage3D;
ilGLCompressed3D = glCompressedTexImage3DARB;
#else
return IL_FALSE; // @TODO: Find any other systems that we could be on.
#endif
//#else
//#endif//GL_VERSION_1_3
// Use PROXY_TEXTURE_2D/3D with glTexImage2D/3D() to test more accurately...
glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint*)&MaxTexW);
glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint*)&MaxTexH);
if (ilGLTexImage3D != NULL)
glGetIntegerv(ILGL_MAX_3D_TEXTURE_SIZE, (GLint*)&MaxTexD);
if (MaxTexW <= 0 || MaxTexH <= 0 || MaxTexD <= 0) {
MaxTexW = MaxTexH = 256; // Trying this because of the VooDoo series of cards.
MaxTexD = 1;
}
ilutSetInteger(ILUT_MAXTEX_WIDTH, MaxTexW);
ilutSetInteger(ILUT_MAXTEX_HEIGHT, MaxTexH);
ilutSetInteger(ILUT_MAXTEX_DEPTH, MaxTexD);
if (IsExtensionSupported("GL_ARB_texture_cube_map"))
HasCubemapHardware = IL_TRUE;
if (IsExtensionSupported("GL_ARB_texture_non_power_of_two"))
HasNonPowerOfTwoHardware = IL_TRUE;
return IL_TRUE;
}
// @TODO: Check what dimensions an image has and use the appropriate IL_IMAGE_XD #define!
GLuint ILAPIENTRY ilutGLBindTexImage()
{
GLuint TexID = 0, Target = GL_TEXTURE_2D;
ILimage *Image;
Image = ilGetCurImage();
if (Image == NULL)
return 0;
if (ilutGetBoolean(ILUT_GL_AUTODETECT_TEXTURE_TARGET)) {
if (HasCubemapHardware && Image->CubeFlags != 0)
Target = ILGL_TEXTURE_CUBE_MAP;
}
glGenTextures(1, &TexID);
glBindTexture(Target, TexID);
if (Target == GL_TEXTURE_2D) {
glTexParameteri(Target, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(Target, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
else if (Target == ILGL_TEXTURE_CUBE_MAP) {
glTexParameteri(Target, GL_TEXTURE_WRAP_S, ILGL_CLAMP_TO_EDGE);
glTexParameteri(Target, GL_TEXTURE_WRAP_T, ILGL_CLAMP_TO_EDGE);
glTexParameteri(Target, ILGL_TEXTURE_WRAP_R, ILGL_CLAMP_TO_EDGE);
}
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(Target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(Target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SWAP_BYTES, IL_FALSE);
if (!ilutGLTexImage(0)) {
glDeleteTextures(1, &TexID);
return 0;
}
return TexID;
}
ILuint GLGetDXTCNum(ILenum DXTCFormat)
{
switch (DXTCFormat)
{
// Constants from glext.h.
case IL_DXT1:
DXTCFormat = 0x83F1;
break;
case IL_DXT3:
DXTCFormat = 0x83F2;
break;
case IL_DXT5:
DXTCFormat = 0x83F3;
break;
}
return DXTCFormat;
}
// We assume *all* states have been set by the user, including 2D texturing!
ILboolean ILAPIENTRY ilutGLTexImage_(GLuint Level, GLuint Target, ILimage *Image)
{
ILimage *ImageCopy, *OldImage;
#if defined (_MSC_VER) || defined (linux) || defined(__APPLE__)
ILenum DXTCFormat;
ILuint Size;
ILubyte *Buffer;
#endif
if (Image == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
OldImage = ilGetCurImage();
#if defined (_MSC_VER) || defined (linux) || defined(__APPLE__)
if (ilutGetBoolean(ILUT_GL_USE_S3TC) && ilGLCompressed2D != NULL) {
if (Image->DxtcData != NULL && Image->DxtcSize != 0) {
DXTCFormat = GLGetDXTCNum(Image->DxtcFormat);
ilGLCompressed2D(Target, Level, DXTCFormat, Image->Width,
Image->Height, 0, Image->DxtcSize, Image->DxtcData);
return IL_TRUE;
}
if (ilutGetBoolean(ILUT_GL_GEN_S3TC)) {
DXTCFormat = ilutGetInteger(ILUT_S3TC_FORMAT);
ilSetCurImage(Image);
Size = ilGetDXTCData(NULL, 0, DXTCFormat);
if (Size != 0) {
Buffer = (ILubyte*)ialloc(Size);
if (Buffer == NULL) {
ilSetCurImage(OldImage);
return IL_FALSE;
}
Size = ilGetDXTCData(Buffer, Size, DXTCFormat);
if (Size == 0) {
ilSetCurImage(OldImage);
ifree(Buffer);
return IL_FALSE;
}
DXTCFormat = GLGetDXTCNum(DXTCFormat);
ilGLCompressed2D(Target, Level, DXTCFormat, Image->Width,
Image->Height, 0, Size, Buffer);
ifree(Buffer);
ilSetCurImage(OldImage);
return IL_TRUE;
}
ilSetCurImage(OldImage);
}
}
#endif//_MSC_VER
ImageCopy = MakeGLCompliant2D(Image);
if (ImageCopy == NULL)
return IL_FALSE;
glTexImage2D(
Target,
Level,
ilutGLFormat(ImageCopy->Format, ImageCopy->Bpp),
ImageCopy->Width,
ImageCopy->Height,
0,
ImageCopy->Format,
ImageCopy->Type,
ImageCopy->Data);
if (Image != ImageCopy)
ilCloseImage(ImageCopy);
return IL_TRUE;
}
GLuint iToGLCube(ILuint cube)
{
switch (cube) {
case IL_CUBEMAP_POSITIVEX:
return ILGL_TEXTURE_CUBE_MAP_POSITIVE_X;
case IL_CUBEMAP_POSITIVEY:
return ILGL_TEXTURE_CUBE_MAP_POSITIVE_Y;
case IL_CUBEMAP_POSITIVEZ:
return ILGL_TEXTURE_CUBE_MAP_POSITIVE_Z;
case IL_CUBEMAP_NEGATIVEX:
return ILGL_TEXTURE_CUBE_MAP_NEGATIVE_X;
case IL_CUBEMAP_NEGATIVEY:
return ILGL_TEXTURE_CUBE_MAP_NEGATIVE_Y;
case IL_CUBEMAP_NEGATIVEZ:
return ILGL_TEXTURE_CUBE_MAP_NEGATIVE_Z;
default:
return ILGL_TEXTURE_CUBE_MAP_POSITIVE_X; //???
}
}
ILboolean ILAPIENTRY ilutGLTexImage(GLuint Level)
{
ILimage *Temp;
ilutCurImage = ilGetCurImage();
if (!ilutGetBoolean(ILUT_GL_AUTODETECT_TEXTURE_TARGET))
return ilutGLTexImage_(Level, GL_TEXTURE_2D, ilGetCurImage());
else {
// Autodetect texture target
// Cubemap
if (ilutCurImage->CubeFlags != 0 && HasCubemapHardware) { //bind to cubemap
Temp = ilutCurImage;
while (Temp != NULL && Temp->CubeFlags != 0) {
ilutGLTexImage_(Level, iToGLCube(Temp->CubeFlags), Temp);
Temp = Temp->Next;
}
return IL_TRUE; //@TODO: check for errors??
}
else // 2D texture
return ilutGLTexImage_(Level, GL_TEXTURE_2D, ilGetCurImage());
}
}
GLuint ILAPIENTRY ilutGLBindMipmaps()
{
GLuint TexID = 0;
// glPushAttrib(GL_ALL_ATTRIB_BITS);
glGenTextures(1, &TexID);
glBindTexture(GL_TEXTURE_2D, TexID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
if (!ilutGLBuildMipmaps()) {
glDeleteTextures(1, &TexID);
return 0;
}
// glPopAttrib();
return TexID;
}
ILboolean ILAPIENTRY ilutGLBuildMipmaps()
{
ILimage *Image;
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
Image = MakeGLCompliant2D(ilutCurImage);
if (Image == NULL)
return IL_FALSE;
gluBuild2DMipmaps(GL_TEXTURE_2D, ilutGLFormat(Image->Format, Image->Bpp), Image->Width,
Image->Height, Image->Format, Image->Type, Image->Data);
if (Image != ilutCurImage)
ilCloseImage(Image);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutGLSubTex(GLuint TexID, ILuint XOff, ILuint YOff)
{
return ilutGLSubTex2D(TexID, XOff, YOff);
}
ILboolean ILAPIENTRY ilutGLSubTex2D(GLuint TexID, ILuint XOff, ILuint YOff)
{
ILimage *Image;
ILint Width, Height;
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
Image = MakeGLCompliant2D(ilutCurImage);
if (Image == NULL)
return IL_FALSE;
glBindTexture(GL_TEXTURE_2D, TexID);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, (GLint*)&Width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, (GLint*)&Height);
if (Image->Width + XOff > (ILuint)Width || Image->Height + YOff > (ILuint)Height) {
ilSetError(ILUT_BAD_DIMENSIONS);
return IL_FALSE;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SWAP_BYTES, IL_FALSE);
glTexSubImage2D(GL_TEXTURE_2D, 0, XOff, YOff, Image->Width, Image->Height, Image->Format,
Image->Type, Image->Data);
if (Image != ilutCurImage)
ilCloseImage(Image);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutGLSubTex3D(GLuint TexID, ILuint XOff, ILuint YOff, ILuint ZOff)
{
ILimage *Image;
ILint Width, Height, Depth;
if (ilGLTexSubImage3D == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION); // Set a different error?
return IL_FALSE;
}
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
Image = MakeGLCompliant3D(ilutCurImage);
if (Image == NULL)
return IL_FALSE;
glBindTexture(ILGL_TEXTURE_3D, TexID);
glGetTexLevelParameteriv(ILGL_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, (GLint*)&Width);
glGetTexLevelParameteriv(ILGL_TEXTURE_3D, 0, GL_TEXTURE_HEIGHT, (GLint*)&Height);
glGetTexLevelParameteriv(ILGL_TEXTURE_3D, 0, ILGL_TEXTURE_DEPTH, (GLint*)&Depth);
if (Image->Width + XOff > (ILuint)Width || Image->Height + YOff > (ILuint)Height
|| Image->Depth + ZOff > (ILuint)Depth) {
ilSetError(ILUT_BAD_DIMENSIONS);
return IL_FALSE;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SWAP_BYTES, IL_FALSE);
ilGLTexSubImage3D(ILGL_TEXTURE_3D, 0, XOff, YOff, ZOff, Image->Width, Image->Height, Image->Depth,
Image->Format, Image->Type, Image->Data);
if (Image != ilutCurImage)
ilCloseImage(Image);
return IL_TRUE;
}
ILimage* MakeGLCompliant2D(ILimage *Src)
{
ILimage *Dest = Src, *Temp;
ILboolean Created = IL_FALSE;
ILenum Filter;
ILubyte *Flipped;
ILboolean need_resize = IL_FALSE;
ILint MaxTexW, MaxTexH;
MaxTexW = ilutGetInteger(ILUT_MAXTEX_WIDTH);
MaxTexH = ilutGetInteger(ILUT_MAXTEX_HEIGHT);
if (Src->Pal.Palette != NULL && Src->Pal.PalSize != 0 && Src->Pal.PalType != IL_PAL_NONE) {
//ilSetCurImage(Src);
Dest = iConvertImage(Src, ilGetPalBaseType(Src->Pal.PalType), IL_UNSIGNED_BYTE);
//Dest = iConvertImage(IL_BGR);
//ilSetCurImage(ilutCurImage);
if (Dest == NULL)
return NULL;
Created = IL_TRUE;
// Change here!
// Set Dest's palette stuff here
Dest->Pal.PalType = IL_PAL_NONE;
}
if (HasNonPowerOfTwoHardware == IL_FALSE &&
(Src->Width != ilNextPower2(Src->Width) ||
Src->Height != ilNextPower2(Src->Height) )) {
need_resize = IL_TRUE;
}
if ((ILint)Src->Width > MaxTexW || (ILint)Src->Height > MaxTexH)
need_resize = IL_TRUE;
if (need_resize == IL_TRUE) {
if (!Created) {
Dest = ilCopyImage_(Src);
if (Dest == NULL) {
return NULL;
}
Created = IL_TRUE;
}
Filter = iluGetInteger(ILU_FILTER);
if (Src->Format == IL_COLOUR_INDEX) {
iluImageParameter(ILU_FILTER, ILU_NEAREST);
Temp = HasNonPowerOfTwoHardware == IL_TRUE ?
iluScale_(Dest, min((ILuint)MaxTexW, Dest->Width), min((ILuint)MaxTexH, Dest->Height), 1)
: iluScale_(Dest, min((ILuint)MaxTexW, ilNextPower2(Dest->Width)),
min((ILuint)MaxTexH, ilNextPower2(Dest->Height)), 1);
iluImageParameter(ILU_FILTER, Filter);
} else {
iluImageParameter(ILU_FILTER, ILU_BILINEAR);
Temp = HasNonPowerOfTwoHardware == IL_TRUE ?
iluScale_(Dest, min((ILuint)MaxTexW, Dest->Width), min((ILuint)MaxTexH, Dest->Height), 1)
: iluScale_(Dest, min((ILuint)MaxTexW, (ILint)ilNextPower2(Dest->Width)),
min(MaxTexH, (ILint)ilNextPower2(Dest->Height)), 1);
iluImageParameter(ILU_FILTER, Filter);
}
ilCloseImage(Dest);
if (!Temp) {
return NULL;
}
Dest = Temp;
}
if (Dest->Origin != IL_ORIGIN_LOWER_LEFT) {
Flipped = iGetFlipped(Dest);
ifree(Dest->Data);
Dest->Data = Flipped;
Dest->Origin = IL_ORIGIN_LOWER_LEFT;
}
return Dest;
}
ILimage* MakeGLCompliant3D(ILimage *Src)
{
ILimage *Dest = Src, *Temp;
ILboolean Created = IL_FALSE;
ILenum Filter;
ILubyte *Flipped;
ILboolean need_resize = IL_FALSE;
ILint MaxTexW, MaxTexH, MaxTexD;
MaxTexW = ilutGetInteger(ILUT_MAXTEX_WIDTH);
MaxTexH = ilutGetInteger(ILUT_MAXTEX_HEIGHT);
MaxTexD = ilutGetInteger(ILUT_MAXTEX_DEPTH);
if (Src->Pal.Palette != NULL && Src->Pal.PalSize != 0 && Src->Pal.PalType != IL_PAL_NONE) {
//ilSetCurImage(Src);
Dest = iConvertImage(Src, ilGetPalBaseType(Src->Pal.PalType), IL_UNSIGNED_BYTE);
//Dest = iConvertImage(IL_BGR);
//ilSetCurImage(ilutCurImage);
if (Dest == NULL)
return NULL;
Created = IL_TRUE;
// Change here!
// Set Dest's palette stuff here
Dest->Pal.PalType = IL_PAL_NONE;
}
if (HasNonPowerOfTwoHardware == IL_FALSE &&
(Src->Width != ilNextPower2(Src->Width) ||
Src->Height != ilNextPower2(Src->Height) ||
Src->Depth != ilNextPower2(Src->Depth) )) {
need_resize = IL_TRUE;
}
if ((ILint)Src->Width > MaxTexW || (ILint)Src->Height > MaxTexH || (ILint)Src->Depth > MaxTexD)
need_resize = IL_TRUE;
if (need_resize == IL_TRUE) {
if (!Created) {
Dest = ilCopyImage_(Src);
if (Dest == NULL) {
return NULL;
}
Created = IL_TRUE;
}
Filter = iluGetInteger(ILU_FILTER);
if (Src->Format == IL_COLOUR_INDEX) {
iluImageParameter(ILU_FILTER, ILU_NEAREST);
Temp = HasNonPowerOfTwoHardware == IL_TRUE ?
iluScale_(Dest, min((ILuint)MaxTexW, Dest->Width), min((ILuint)MaxTexH, Dest->Height), min((ILuint)MaxTexD, Dest->Depth))
: iluScale_(Dest, min((ILuint)MaxTexW, ilNextPower2(Dest->Width)),
min((ILuint)MaxTexH, ilNextPower2(Dest->Height)),
min((ILuint)MaxTexD, ilNextPower2(Dest->Height)));
iluImageParameter(ILU_FILTER, Filter);
} else {
iluImageParameter(ILU_FILTER, ILU_BILINEAR);
Temp = HasNonPowerOfTwoHardware == IL_TRUE ?
iluScale_(Dest, min((ILuint)MaxTexW, Dest->Width), min((ILuint)MaxTexH, Dest->Height), min((ILuint)MaxTexD, Dest->Depth))
: iluScale_(Dest, min((ILuint)MaxTexW, (ILint)ilNextPower2(Dest->Width)),
min(MaxTexH, (ILint)ilNextPower2(Dest->Height)),
min(MaxTexD, (ILint)ilNextPower2(Dest->Depth)));
iluImageParameter(ILU_FILTER, Filter);
}
ilCloseImage(Dest);
if (!Temp) {
return NULL;
}
Dest = Temp;
}
if (Dest->Origin != IL_ORIGIN_LOWER_LEFT) {
Flipped = iGetFlipped(Dest);
ifree(Dest->Data);
Dest->Data = Flipped;
Dest->Origin = IL_ORIGIN_LOWER_LEFT;
}
return Dest;
}
//! Just a convenience function.
#ifndef _WIN32_WCE
GLuint ILAPIENTRY ilutGLLoadImage(ILstring FileName)
{
GLuint TexId;
//ILuint Id;
iBindImageTemp();
//ilGenImages(1, &Id);
//ilBindImage(Id);
if (!ilLoadImage(FileName))
return 0;
TexId = ilutGLBindTexImage();
//ilDeleteImages(1, &Id);
return TexId;
}
#endif//_WIN32_WCE
#ifndef _WIN32_WCE
ILboolean ILAPIENTRY ilutGLSaveImage(ILstring FileName, GLuint TexID)
{
ILuint CurName;
ILboolean Saved;
CurName = ilGetCurName();
iBindImageTemp();
if (!ilutGLSetTex(TexID)) {
ilBindImage(CurName);
return IL_FALSE;
}
Saved = ilSaveImage(FileName);
ilBindImage(CurName);
return Saved;
}
#endif//_WIN32_WCE
//! Takes a screenshot of the current OpenGL window.
ILboolean ILAPIENTRY ilutGLScreen()
{
ILuint ViewPort[4];
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
glGetIntegerv(GL_VIEWPORT, (GLint*)ViewPort);
if (!ilTexImage(ViewPort[2], ViewPort[3], 1, 3, IL_RGB, IL_UNSIGNED_BYTE, NULL))
return IL_FALSE; // Error already set.
ilutCurImage->Origin = IL_ORIGIN_LOWER_LEFT;
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, ViewPort[2], ViewPort[3], GL_RGB, GL_UNSIGNED_BYTE, ilutCurImage->Data);
return IL_TRUE;
}
#ifndef _WIN32_WCE
ILboolean ILAPIENTRY ilutGLScreenie()
{
FILE *File;
ILchar Buff[255];
ILuint i, CurName;
ILboolean ReturnVal = IL_TRUE;
CurName = ilGetCurName();
// Could go above 128 easily...
for (i = 0; i < 128; i++) {
#ifndef _UNICODE
sprintf(Buff, "screen%d.tga", i);
File = fopen(Buff, "rb");
#else
swprintf(Buff, 128, L"screen%d.tga", i);
// Windows has a different function, _wfopen, to open UTF16 files,
// whereas Linux just uses fopen.
#ifdef _WIN32
File = _wfopen(Buff, L"rb");
#else
File = fopen((char*)Buff, "rb");
#endif
#endif
if (!File)
break;
fclose(File);
}
if (i == 127) {
ilSetError(ILUT_COULD_NOT_OPEN_FILE);
return IL_FALSE;
}
iBindImageTemp();
if (!ilutGLScreen()) {
ReturnVal = IL_FALSE;
}
if (ReturnVal)
ilSave(IL_TGA, Buff);
ilBindImage(CurName);
return ReturnVal;
}
#endif//_WIN32_WCE
//! Deprecated - use ilutGLSetTex2D instead.
ILboolean ILAPIENTRY ilutGLSetTex(GLuint TexID)
{
return ilutGLSetTex2D(TexID);
}
ILboolean ILAPIENTRY ilutGLSetTex2D(GLuint TexID)
{
ILubyte *Data;
ILuint Width, Height;
glBindTexture(GL_TEXTURE_2D, TexID);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, (GLint*)&Width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, (GLint*)&Height);
Data = (ILubyte*)ialloc(Width * Height * 4);
if (Data == NULL) {
return IL_FALSE;
}
glGetTexImage(GL_TEXTURE_2D, 0, IL_BGRA, GL_UNSIGNED_BYTE, Data);
if (!ilTexImage(Width, Height, 1, 4, IL_BGRA, IL_UNSIGNED_BYTE, Data)) {
ifree(Data);
return IL_FALSE;
}
ilutCurImage->Origin = IL_ORIGIN_LOWER_LEFT;
ifree(Data);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutGLSetTex3D(GLuint TexID)
{
ILubyte *Data;
ILuint Width, Height, Depth;
if (ilGLTexImage3D == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION); // Set a different error?
return IL_FALSE;
}
glBindTexture(ILGL_TEXTURE_3D, TexID);
glGetTexLevelParameteriv(ILGL_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, (GLint*)&Width);
glGetTexLevelParameteriv(ILGL_TEXTURE_3D, 0, GL_TEXTURE_HEIGHT, (GLint*)&Height);
glGetTexLevelParameteriv(ILGL_TEXTURE_3D, 0, ILGL_TEXTURE_DEPTH, (GLint*)&Depth);
Data = (ILubyte*)ialloc(Width * Height * Depth * 4);
if (Data == NULL) {
return IL_FALSE;
}
glGetTexImage(ILGL_TEXTURE_3D, 0, IL_BGRA, GL_UNSIGNED_BYTE, Data);
if (!ilTexImage(Width, Height, Depth, 4, IL_BGRA, IL_UNSIGNED_BYTE, Data)) {
ifree(Data);
return IL_FALSE;
}
ilutCurImage->Origin = IL_ORIGIN_LOWER_LEFT;
ifree(Data);
return IL_TRUE;
}
ILenum ilutGLFormat(ILenum Format, ILubyte Bpp)
{
if (Format == IL_RGB || Format == IL_BGR) {
if (ilutIsEnabled(ILUT_OPENGL_CONV)) {
return GL_RGB8;
}
}
else if (Format == IL_RGBA || Format == IL_BGRA) {
if (ilutIsEnabled(ILUT_OPENGL_CONV)) {
return GL_RGBA8;
}
}
else if (Format == IL_ALPHA) {
if (ilutIsEnabled(ILUT_OPENGL_CONV)) {
return GL_ALPHA;
}
}
return Bpp;
}
// From http://www.opengl.org/News/Special/OGLextensions/OGLextensions.html
// Should we make this accessible outside the lib?
ILboolean IsExtensionSupported(const char *extension)
{
const GLubyte *extensions;// = NULL;
const GLubyte *start;
GLubyte *where, *terminator;
// Extension names should not have spaces.
where = (GLubyte *) strchr(extension, ' ');
if (where || *extension == '\0')
return IL_FALSE;
extensions = glGetString(GL_EXTENSIONS);
if (!extensions)
return IL_FALSE;
// It takes a bit of care to be fool-proof about parsing the
// OpenGL extensions string. Don't be fooled by sub-strings, etc.
start = extensions;
for (;;) {
where = (GLubyte *)strstr((const char *) start, extension);
if (!where)
break;
terminator = where + strlen(extension);
if (where == start || *(where - 1) == ' ')
if (*terminator == ' ' || *terminator == '\0')
return IL_TRUE;
start = terminator;
}
return IL_FALSE;
}
#endif//ILUT_USE_OPENGL
@@ -0,0 +1,207 @@
//-----------------------------------------------------------------------------
//
// ImageLib Sources
// Copyright (C) 2002 by Denton Woods
// Copyright (C) 2002 Nelson Rush.
// Last modified: 05/18/2002
//
// Filename: src-ILUT/src/ilut_sdlsurface.c
//
// Description: SDL Surface functions for images
//
//-----------------------------------------------------------------------------
#include "ilut_internal.h"
#ifdef ILUT_USE_SDL
#include <SDL.h>
//#include "endian.h"
#ifdef _MSC_VER
#pragma comment(lib, "sdl.lib")
#endif//_MSC_VER
int isBigEndian;
int rmask, gmask, bmask, amask;
void InitSDL()
{
//#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#ifdef __BIG_ENDIAN__
isBigEndian = 1;
rmask = 0xFF000000;
gmask = 0x00FF0000;
bmask = 0x0000FF00;
amask = 0x000000FF;
#else
isBigEndian = 0;
rmask = 0x000000FF;
gmask = 0x0000FF00;
bmask = 0x00FF0000;
amask = 0xFF000000;
#endif
return;
}
//ILboolean ilConvertPal(ILenum DestFormat);
//ILpal *Pal;
// Does not account for converting luminance...
SDL_Surface *ILAPIENTRY ilutConvertToSDLSurface(unsigned int flags)
{
SDL_Surface *Bitmap = NULL;
ILuint i = 0, Pad, BppPal;
ILubyte *Dest, *Data;
ILimage *Image;
Image = ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return NULL;
}
InitSDL();
// Should be IL_BGR(A).
if (ilutCurImage->Format == IL_RGB || ilutCurImage->Format == IL_RGBA) {
if (!isBigEndian) {
//iluSwapColours(); // No need to swap colors. Just use the bitmasks.
rmask = 0x00FF0000;
gmask = 0x0000FF00;
bmask = 0x000000FF;
}
}
else if (ilutCurImage->Format == IL_BGR || ilutCurImage->Format == IL_BGRA) {
if (isBigEndian) {
rmask = 0x0000FF00;
gmask = 0x00FF0000;
bmask = 0xFF000000;
}
}
else if (Image->Format != IL_COLOR_INDEX) { // We have to convert the image.
#ifdef __BIG_ENDIAN__
Image = iConvertImage(Image, IL_RGBA, IL_UNSIGNED_BYTE);
#else
Image = iConvertImage(Image, IL_BGRA, IL_UNSIGNED_BYTE);
#endif
if (Image == NULL)
return NULL;
}
if (Image->Type != IL_UNSIGNED_BYTE) {
// We do not have to worry about Image != iCurImage at this point, because if it was converted,
// it was converted to a type of unsigned byte.
Image = iConvertImage(Image, Image->Format, IL_UNSIGNED_BYTE);
if (Image == NULL)
return NULL;
}
Data = Image->Data;
if (Image->Origin == IL_ORIGIN_LOWER_LEFT) {
Data = iGetFlipped(Image);
if (Data == NULL)
goto done;
}
Bitmap = SDL_CreateRGBSurface(flags, Image->Width, Image->Height, Image->Bpp * 8,
rmask,gmask,bmask,amask);
if (Bitmap == NULL)
goto done;
if (SDL_MUSTLOCK(Bitmap))
SDL_LockSurface(Bitmap);
Pad = Bitmap->pitch - Image->Bps;
if (Pad == 0) {
memcpy(Bitmap->pixels, Data, Image->SizeOfData);
}
else { // Must pad the lines on some images.
Dest = Bitmap->pixels;
for (i = 0; i < Image->Height; i++) {
memcpy(Dest, Data + i * Image->Bps, Image->Bps);
imemclear(Dest + Image->Bps, Pad);
Dest += Bitmap->pitch;
}
}
if (SDL_MUSTLOCK(Bitmap))
SDL_UnlockSurface(Bitmap);
if (Image->Format == IL_COLOR_INDEX) {
BppPal = ilGetBppPal(Image->Pal.PalType);
switch (ilutCurImage->Pal.PalType)
{
case IL_PAL_RGB24:
case IL_PAL_RGB32:
case IL_PAL_RGBA32:
for (i = 0; i < ilutCurImage->Pal.PalSize / BppPal; i++) {
(Bitmap->format)->palette->colors[i].r = ilutCurImage->Pal.Palette[i*BppPal+0];
(Bitmap->format)->palette->colors[i].g = ilutCurImage->Pal.Palette[i*BppPal+1];
(Bitmap->format)->palette->colors[i].b = ilutCurImage->Pal.Palette[i*BppPal+2];
(Bitmap->format)->palette->colors[i].unused = 0xFF;
}
break;
case IL_PAL_BGR24:
case IL_PAL_BGR32:
case IL_PAL_BGRA32:
for (i = 0; i < ilutCurImage->Pal.PalSize / BppPal; i++) {
(Bitmap->format)->palette->colors[i].b = ilutCurImage->Pal.Palette[i*BppPal+0];
(Bitmap->format)->palette->colors[i].g = ilutCurImage->Pal.Palette[i*BppPal+1];
(Bitmap->format)->palette->colors[i].r = ilutCurImage->Pal.Palette[i*BppPal+2];
(Bitmap->format)->palette->colors[i].unused = 0xFF;
}
break;
default:
ilSetError(IL_INTERNAL_ERROR); // Do anything else?
}
}
done:
if (Data != Image->Data)
ifree(Data); // This is flipped data.
if (Image != ilutCurImage)
ilCloseImage(Image); // This is a converted image.
return Bitmap; // This is NULL if there was an error.
}
#ifndef _WIN32_WCE
SDL_Surface* ILAPIENTRY ilutSDLSurfaceLoadImage(ILstring FileName)
{
SDL_Surface *Surface;
iBindImageTemp();
if (!ilLoadImage(FileName)) {
return NULL;
}
Surface = ilutConvertToSDLSurface(SDL_SWSURFACE);
return Surface;
}
#endif//_WIN32_WCE
// Unfinished
ILboolean ILAPIENTRY ilutSDLSurfaceFromBitmap(SDL_Surface *Bitmap)
{
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
if (Bitmap == NULL || Bitmap->w == 0 || Bitmap->h == 0) {
ilSetError(ILUT_INVALID_PARAM);
return IL_FALSE;
}
if (!ilTexImage(Bitmap->w, Bitmap->h, 1, 3, IL_RGB, IL_UNSIGNED_BYTE, NULL))
return IL_FALSE;
return IL_TRUE;
}
#endif//ILUT_USE_SDL
+424
View File
@@ -0,0 +1,424 @@
//-----------------------------------------------------------------------------
//
// ImageLib Utility Toolkit Sources
// Copyright (C) 2000-2009 by Denton Woods
// Last modified: 03/07/2009
//
// Filename: src-ILUT/src/ilut_states.c
//
// Description: The state machine
//
//-----------------------------------------------------------------------------
#include "ilut_internal.h"
#include "ilut_states.h"
//#ifdef ILUT_USE_OPENGL
#include "ilut_opengl.h"
//#endif
ILconst_string _ilutVendor = IL_TEXT("Abysmal Software");
//ILconst_string _ilutVersion = IL_TEXT("Developer's Image Library Utility Toolkit (ILUT) 1.7.8 " IL_TEXT(__DATE__));
ILconst_string _ilutVersion = IL_TEXT("Developer's Image Library Utility Toolkit (ILUT) 1.7.8");
// Set all states to their defaults
void ilutDefaultStates()
{
ilutStates[ilutCurrentPos].ilutUsePalettes = IL_FALSE;
ilutStates[ilutCurrentPos].ilutForceIntegerFormat = IL_FALSE;
ilutStates[ilutCurrentPos].ilutOglConv = IL_FALSE; // IL_TRUE ?
ilutStates[ilutCurrentPos].ilutDXTCFormat = 0;
ilutStates[ilutCurrentPos].ilutUseS3TC = IL_FALSE;
ilutStates[ilutCurrentPos].ilutGenS3TC = IL_FALSE;
ilutStates[ilutCurrentPos].ilutAutodetectTextureTarget = IL_FALSE;
ilutStates[ilutCurrentPos].MaxTexW = 256;
ilutStates[ilutCurrentPos].MaxTexH = 256;
ilutStates[ilutCurrentPos].MaxTexD = 1;
ilutStates[ilutCurrentPos].D3DMipLevels = 0;
ilutStates[ilutCurrentPos].D3DPool = 0;
ilutStates[ilutCurrentPos].D3DAlphaKeyColor = -1;
}
void ILAPIENTRY ilutD3D8MipFunc(ILuint NumLevels)
{
ilutStates[ilutCurrentPos].D3DMipLevels = NumLevels;
return;
}
ILstring ILAPIENTRY ilutGetString(ILenum StringName)
{
switch (StringName)
{
case ILUT_VENDOR:
return (ILstring)_ilutVendor;
//changed 2003-09-04
case ILUT_VERSION_NUM:
return (ILstring)_ilutVersion;
default:
ilSetError(ILUT_INVALID_PARAM);
break;
}
return NULL;
}
ILboolean ILAPIENTRY ilutEnable(ILenum Mode)
{
return ilutAble(Mode, IL_TRUE);
}
ILboolean ILAPIENTRY ilutDisable(ILenum Mode)
{
return ilutAble(Mode, IL_FALSE);
}
ILboolean ilutAble(ILenum Mode, ILboolean Flag)
{
switch (Mode)
{
case ILUT_PALETTE_MODE:
ilutStates[ilutCurrentPos].ilutUsePalettes = Flag;
break;
case ILUT_FORCE_INTEGER_FORMAT:
ilutStates[ilutCurrentPos].ilutForceIntegerFormat = Flag;
break;
case ILUT_OPENGL_CONV:
ilutStates[ilutCurrentPos].ilutOglConv = Flag;
break;
case ILUT_GL_USE_S3TC:
ilutStates[ilutCurrentPos].ilutUseS3TC = Flag;
break;
case ILUT_GL_GEN_S3TC:
ilutStates[ilutCurrentPos].ilutGenS3TC = Flag;
break;
case ILUT_GL_AUTODETECT_TEXTURE_TARGET:
ilutStates[ilutCurrentPos].ilutAutodetectTextureTarget = Flag;
break;
default:
ilSetError(ILUT_INVALID_ENUM);
return IL_FALSE;
}
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutIsEnabled(ILenum Mode)
{
switch (Mode)
{
case ILUT_PALETTE_MODE:
return ilutStates[ilutCurrentPos].ilutUsePalettes;
case ILUT_FORCE_INTEGER_FORMAT:
return ilutStates[ilutCurrentPos].ilutForceIntegerFormat;
case ILUT_OPENGL_CONV:
return ilutStates[ilutCurrentPos].ilutOglConv;
case ILUT_GL_USE_S3TC:
return ilutStates[ilutCurrentPos].ilutUseS3TC;
case ILUT_GL_GEN_S3TC:
return ilutStates[ilutCurrentPos].ilutGenS3TC;
case ILUT_GL_AUTODETECT_TEXTURE_TARGET:
return ilutStates[ilutCurrentPos].ilutAutodetectTextureTarget;
default:
ilSetError(ILUT_INVALID_ENUM);
}
return IL_FALSE;
}
ILboolean ILAPIENTRY ilutIsDisabled(ILenum Mode)
{
return !ilutIsEnabled(Mode);
}
void ILAPIENTRY ilutGetBooleanv(ILenum Mode, ILboolean *Param)
{
switch (Mode)
{
case ILUT_PALETTE_MODE:
*Param = ilutStates[ilutCurrentPos].ilutUsePalettes;
break;
case ILUT_FORCE_INTEGER_FORMAT:
*Param = ilutStates[ilutCurrentPos].ilutForceIntegerFormat;
break;
case ILUT_OPENGL_CONV:
*Param = ilutStates[ilutCurrentPos].ilutOglConv;
break;
case ILUT_GL_USE_S3TC:
*Param = ilutStates[ilutCurrentPos].ilutUseS3TC;
break;
case ILUT_GL_GEN_S3TC:
*Param = ilutStates[ilutCurrentPos].ilutGenS3TC;
break;
case ILUT_GL_AUTODETECT_TEXTURE_TARGET:
*Param = ilutStates[ilutCurrentPos].ilutAutodetectTextureTarget;
break;
default:
ilSetError(ILUT_INVALID_ENUM);
}
return;
}
ILboolean ILAPIENTRY ilutGetBoolean(ILenum Mode)
{
ILboolean Temp = IL_FALSE;
ilutGetBooleanv(Mode, &Temp);
return Temp;
}
void ILAPIENTRY ilutGetIntegerv(ILenum Mode, ILint *Param)
{
switch (Mode)
{
/*case IL_ORIGIN_MODE:
*Param = ilutStates[ilutCurrentPos].ilutOriginMode;
break;*/
case ILUT_MAXTEX_WIDTH:
*Param = ilutStates[ilutCurrentPos].MaxTexW;
break;
case ILUT_MAXTEX_HEIGHT:
*Param = ilutStates[ilutCurrentPos].MaxTexH;
break;
case ILUT_MAXTEX_DEPTH:
*Param = ilutStates[ilutCurrentPos].MaxTexD;
break;
case ILUT_VERSION_NUM:
*Param = ILUT_VERSION;
break;
case ILUT_PALETTE_MODE:
*Param = ilutStates[ilutCurrentPos].ilutUsePalettes;
break;
case ILUT_FORCE_INTEGER_FORMAT:
*Param = ilutStates[ilutCurrentPos].ilutForceIntegerFormat;
break;
case ILUT_OPENGL_CONV:
*Param = ilutStates[ilutCurrentPos].ilutOglConv;
break;
case ILUT_GL_USE_S3TC:
*Param = ilutStates[ilutCurrentPos].ilutUseS3TC;
break;
case ILUT_GL_GEN_S3TC:
*Param = ilutStates[ilutCurrentPos].ilutUseS3TC;
break;
case ILUT_S3TC_FORMAT:
*Param = ilutStates[ilutCurrentPos].ilutDXTCFormat;
break;
case ILUT_GL_AUTODETECT_TEXTURE_TARGET:
*Param = ilutStates[ilutCurrentPos].ilutAutodetectTextureTarget;
break;
case ILUT_D3D_MIPLEVELS:
*Param = ilutStates[ilutCurrentPos].D3DMipLevels;
break;
case ILUT_D3D_ALPHA_KEY_COLOR:
*Param = ilutStates[ilutCurrentPos].D3DAlphaKeyColor;
break;
case ILUT_D3D_POOL:
*Param = ilutStates[ilutCurrentPos].D3DPool;
break;
default:
ilSetError(ILUT_INVALID_ENUM);
}
return;
}
ILint ILAPIENTRY ilutGetInteger(ILenum Mode)
{
ILint Temp = 0;
ilutGetIntegerv(Mode, &Temp);
return Temp;
}
void ILAPIENTRY ilutSetInteger(ILenum Mode, ILint Param)
{
switch (Mode)
{
case ILUT_S3TC_FORMAT:
if (Param >= IL_DXT1 && Param <= IL_DXT5) {
ilutStates[ilutCurrentPos].ilutDXTCFormat = Param;
return;
}
//#ifdef ILUT_USE_OPENGL
case ILUT_MAXTEX_WIDTH:
if (Param >= 1) {
ilutStates[ilutCurrentPos].MaxTexW = Param;
return;
}
break;
case ILUT_MAXTEX_HEIGHT:
if (Param >= 1) {
ilutStates[ilutCurrentPos].MaxTexH = Param;
return;
}
break;
case ILUT_MAXTEX_DEPTH:
if (Param >= 1) {
ilutStates[ilutCurrentPos].MaxTexD = Param;
return;
}
break;
case ILUT_GL_USE_S3TC:
if (Param == IL_TRUE || Param == IL_FALSE) {
ilutStates[ilutCurrentPos].ilutUseS3TC = (ILboolean)Param;
return;
}
break;
case ILUT_GL_GEN_S3TC:
if (Param == IL_TRUE || Param == IL_FALSE) {
ilutStates[ilutCurrentPos].ilutGenS3TC = (ILboolean)Param;
return;
}
break;
case ILUT_GL_AUTODETECT_TEXTURE_TARGET:
if (Param == IL_TRUE || Param == IL_FALSE) {
ilutStates[ilutCurrentPos].ilutAutodetectTextureTarget = (ILboolean)Param;
return;
}
break;
//#endif//ILUT_USE_OPENGL
//#ifdef ILUT_USE_DIRECTX8
case ILUT_D3D_MIPLEVELS:
if (Param >= 0) {
ilutStates[ilutCurrentPos].D3DMipLevels = Param;
return;
}
break;
case ILUT_D3D_ALPHA_KEY_COLOR:
ilutStates[ilutCurrentPos].D3DAlphaKeyColor = Param;
return;
break;
case ILUT_D3D_POOL:
if (Param >= 0 && Param <= 2) {
ilutStates[ilutCurrentPos].D3DPool = Param;
return;
}
break;
//#endif//ILUT_USE_DIRECTX8
default:
ilSetError(ILUT_INVALID_ENUM);
}
ilSetError(IL_INVALID_PARAM); // Parameter not in valid bounds.
return;
}
void ILAPIENTRY ilutPushAttrib(ILuint Bits)
{
// Should we check here to see if ilCurrentPos is negative?
if (ilutCurrentPos >= ILUT_ATTRIB_STACK_MAX - 1) {
ilutCurrentPos = ILUT_ATTRIB_STACK_MAX - 1;
ilSetError(ILUT_STACK_OVERFLOW);
return;
}
ilutCurrentPos++;
//memcpy(&ilutStates[ilutCurrentPos], &ilutStates[ilutCurrentPos - 1], sizeof(ILUT_STATES));
if (Bits & ILUT_OPENGL_BIT) {
ilutStates[ilutCurrentPos].ilutUsePalettes = ilutStates[ilutCurrentPos-1].ilutUsePalettes;
ilutStates[ilutCurrentPos].ilutOglConv = ilutStates[ilutCurrentPos-1].ilutOglConv;
}
if (Bits & ILUT_D3D_BIT) {
ilutStates[ilutCurrentPos].D3DMipLevels = ilutStates[ilutCurrentPos-1].D3DMipLevels;
ilutStates[ilutCurrentPos].D3DAlphaKeyColor = ilutStates[ilutCurrentPos-1].D3DAlphaKeyColor;
}
return;
}
void ILAPIENTRY ilutPopAttrib()
{
if (ilutCurrentPos <= 0) {
ilutCurrentPos = 0;
ilSetError(ILUT_STACK_UNDERFLOW);
return;
}
// Should we check here to see if ilutCurrentPos is too large?
ilutCurrentPos--;
return;
}
ILboolean ILAPIENTRY ilutRenderer(ILenum Renderer)
{
if (Renderer > ILUT_WIN32) {
ilSetError(ILUT_INVALID_VALUE);
return IL_FALSE;
}
switch (Renderer)
{
#ifdef ILUT_USE_OPENGL
case ILUT_OPENGL:
return ilutGLInit();
#endif
#ifdef ILUT_USE_WIN32
case ILUT_WIN32:
return ilutWin32Init();
#endif
#ifdef ILUT_USE_DIRECTX8
case ILUT_DIRECT3D8:
return ilutD3D8Init();
#endif
#ifdef ILUT_USE_DIRECTX9
case ILUT_DIRECT3D9:
return ilutD3D9Init();
#endif
#ifdef ILUT_USE_DIRECTX10
case ILUT_DIRECT3D10:
return ilutD3D10Init();
#endif
default:
ilSetError(ILUT_NOT_SUPPORTED);
}
return IL_FALSE;
}
+856
View File
@@ -0,0 +1,856 @@
//-----------------------------------------------------------------------------
//
// ImageLib Utility Toolkit Sources
// Copyright (C) 2000-2008 by Denton Woods
// Last modified: 12/14/2008
//
// Filename: src-ILUT/src/ilut_win32.c
//
// Description: Windows functions for images
//
//-----------------------------------------------------------------------------
#include "ilut_internal.h"
#ifdef ILUT_USE_WIN32
#include <windows.h>
#include <wininet.h>
// For ilutWinLoadUrl().
#ifdef _MSC_VER
#pragma comment(lib, "wininet.lib")
#endif
#if !_WIN32_WCE && (_WIN32 && __GNUC__)
PRINTDLG Pd;
#endif//_WIN32_WCE
ILboolean ilutWin32Init()
{
return IL_TRUE;
}
ILAPI HBITMAP ILAPIENTRY ilutConvertSliceToHBitmap(HDC hDC, ILuint slice)
{
ILubyte *Data, *DataBackup;
HBITMAP hBitmap = NULL;
ILimage *TempImage = NULL;
ILuint pad, i, j, k, l, m, n, DepthBackup;
ILpal *palImg;
ILboolean alloc_buffer;
//reserve space for palette in every case...
ILubyte buff[sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD)];
BITMAPINFO *info = (BITMAPINFO*)buff;
RGBQUAD *pal = info->bmiColors;
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return NULL;
}
//check if the image has the wanted slice
if (slice < 0 || slice >= ilutCurImage->Depth) {
ilSetError(ILUT_INVALID_PARAM);
return NULL;
}
// Fool iConvertImage into thinking that the current image has
// only one slice, the one we want:
DepthBackup = ilutCurImage->Depth;
DataBackup = ilutCurImage->Data;
ilutCurImage->Depth = 1;
ilutCurImage->Data += ilutCurImage->SizeOfPlane*slice;
if (ilutCurImage->Type != IL_UNSIGNED_BYTE)
TempImage = iConvertImage(ilutCurImage, ilutCurImage->Format, IL_UNSIGNED_BYTE);
else
TempImage = ilutCurImage;
if (TempImage == NULL) {
goto error;
}
//changed 2003-09-09: use Temp!
ilSetCurImage(TempImage);
hBitmap = CreateCompatibleBitmap(hDC, ilutCurImage->Width, ilutCurImage->Height);
if (hBitmap == NULL) {
ilSetError(IL_UNKNOWN_ERROR);
goto error;
}
info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info->bmiHeader.biWidth = TempImage->Width;
if (TempImage->Origin == IL_ORIGIN_UPPER_LEFT)
info->bmiHeader.biHeight = -(ILint)TempImage->Height;
else
info->bmiHeader.biHeight = TempImage->Height;
info->bmiHeader.biPlanes = 1;
info->bmiHeader.biCompression = 0;
info->bmiHeader.biSizeImage = 0;
info->bmiHeader.biXPelsPerMeter = 0;
info->bmiHeader.biYPelsPerMeter = 0;
info->bmiHeader.biClrUsed = 0;
info->bmiHeader.biClrImportant = 0;
pad = (4 - TempImage->Bps%4)%4;
alloc_buffer = (ILboolean)!(pad == 0 && TempImage->Format != IL_RGB
&& TempImage->Format != IL_RGBA && TempImage->Format != IL_LUMINANCE_ALPHA);
if (!alloc_buffer) {
Data = TempImage->Data;
}
else {
ILsizei DataSize = 0;
if (TempImage->Format == IL_RGBA) { // Strip alpha during byte swapping for faster upload to GDI.
// Recalculate pad, because it changes when bpp changes.
pad = (4 - (3 * TempImage->Width) % 4) % 4;
DataSize = (TempImage->Width + pad) * TempImage->Height * 3;
}
// Strip alpha channel from grayscale image.
else if (TempImage->Format == IL_LUMINANCE_ALPHA) {
// Added 01-09-2009: Recalculate pad.
pad = (4 - TempImage->Width % 4) % 4;
DataSize = (TempImage->Width + pad) * TempImage->Height;
}
else {
DataSize = (TempImage->Width + pad) * TempImage->Height * TempImage->Bpp;
}
Data = (ILubyte*)ialloc(DataSize);
if (Data == NULL) {
goto error;
}
if (TempImage->Format == IL_RGB || TempImage->Format == IL_RGBA) {
//swap bytes
m = (TempImage->Format == IL_RGB)?3:4;
k = l = 0;
for (j = 0; j < TempImage->Height; j++) {
for (i = 0, n = 0; i < 3*TempImage->Width; i += 3, n += m) {
Data[l + i] = TempImage->Data[k + n + 2];
Data[l + i + 1] = TempImage->Data[k + n + 1];
Data[l + i + 2] = TempImage->Data[k + n];
}
k += TempImage->Bps;
l += 3*TempImage->Width + pad;
}
}
else if (TempImage->Format == IL_LUMINANCE_ALPHA) {
//strip alpha channel
//recalculate pad because it included alpha channel info
pad = (4 - TempImage->Width%4)%4;
k = l = 0;
for (j = 0; j < TempImage->Height; j++) {
for (i = 0, n = 0; i < TempImage->Width; ++i, n += 2) {
Data[l + i] = TempImage->Data[k + n];
}
k += TempImage->Bps;
l += TempImage->Width + pad;
}
}
else
for (i = 0; i < TempImage->Height; i++)
memcpy(Data + i*(TempImage->Bps + pad), TempImage->Data + i*TempImage->Bps, TempImage->Bps);
}
switch (TempImage->Format)
{
case IL_LUMINANCE:
case IL_LUMINANCE_ALPHA:
case IL_COLOUR_INDEX:
if (TempImage->Format != IL_COLOUR_INDEX) {
//generate greyscale palette
for (i = 0; i < 256; i++)
pal[i].rgbRed = pal[i].rgbGreen = pal[i].rgbBlue = (ILubyte)i;
}
else {
palImg = iConvertPal(&TempImage->Pal, IL_PAL_BGR32);
if (palImg != NULL) {
memcpy(pal, palImg->Palette, palImg->PalSize);
ilClosePal(palImg);
}
else {
//ilSetError(IL_INVALID_PARAM);
// Generate greyscale palette <-- Why is this here?
for (i = 0; i < 256; i++)
pal[i].rgbRed = pal[i].rgbGreen = pal[i].rgbBlue = (ILubyte)i;
}
}
info->bmiHeader.biBitCount = 8;
break;
case IL_RGB:
case IL_BGR:
case IL_RGBA: //alpha is removed during byte swapping
info->bmiHeader.biBitCount = 24;
break;
case IL_BGRA:
info->bmiHeader.biBitCount = 32;
break;
/*default:
ilSetError(IL_FORMAT_NOT_SUPPORTED);
return hBitmap;*/
}
// Restore original data
ilutCurImage->Data = DataBackup;
ilutCurImage->Depth = DepthBackup;
SetDIBits(hDC, hBitmap, 0, ilutCurImage->Height, Data, info, DIB_RGB_COLORS);
if (alloc_buffer)
ifree(Data);
if (ilutCurImage != TempImage) {
ilSetCurImage(ilutCurImage);
ilCloseImage(TempImage);
}
return hBitmap;
error:
// Restore original data
ilutCurImage->Data = DataBackup;
ilutCurImage->Depth = DepthBackup;
if (ilutCurImage != TempImage) {
ilSetCurImage(ilutCurImage);
ilCloseImage(TempImage);
}
ilSetCurImage(ilutCurImage);
if (hBitmap)
DeleteObject(hBitmap);
return NULL;
}
HBITMAP ILAPIENTRY ilutConvertToHBitmap(HDC hDC)
{
return ilutConvertSliceToHBitmap(hDC, 0);
}
ILubyte* ILAPIENTRY iGetPaddedData(ILimage *Image)
{
ILubyte *NewData = NULL, *TempBuff = NULL;
ILuint i, CurPos = 0, PadSize;
ILubyte *TempData = NULL;
if (Image == NULL) {
ilSetError(ILUT_INVALID_PARAM);
return NULL;
}
if (Image->Origin != IL_ORIGIN_LOWER_LEFT) {
TempData = iGetFlipped(Image);
}
else {
TempData = Image->Data;
}
if (Image->Format == IL_RGB || Image->Format == IL_RGBA) {
TempBuff = (ILubyte*)ialloc(Image->SizeOfData);
if (TempBuff == NULL) {
return NULL;
}
// Swap red and blue.
for (i = 0; i < Image->SizeOfData; i += Image->Bpp) {
TempBuff[i] = TempData[i+2];
TempBuff[i+1] = TempData[i+1];
TempBuff[i+2] = TempData[i];
// Copy the alpha channel if present.
if (Image->Bpp == 4)
TempBuff[i+3] = TempData[i+3];
}
}
else {
TempBuff = TempData;
}
PadSize = (4 - (Image->Bps % 4)) % 4;
NewData = (ILubyte*)ialloc((Image->Width + PadSize) * Image->Height * Image->Bpp);
if (NewData == NULL) {
return NULL;
}
for (i = 0; i < Image->Height; i++) {
memcpy(NewData + CurPos, TempBuff + Image->Bps * i, Image->Bps);
CurPos += Image->Bps;
memset(NewData + CurPos, 0, PadSize);
CurPos += PadSize;
}
if (TempData != TempBuff && TempData != Image->Data)
ifree(TempData);
if (TempBuff != Image->Data)
ifree(TempBuff);
return NewData;
}
void ILAPIENTRY ilutFreePaddedData(ILubyte *Data)
{
ifree(Data);
return;
}
// DirectX/GDI insists that all scanlines end on a dword boundary. =(
ILubyte* ILAPIENTRY ilutGetPaddedData()
{
return iGetPaddedData(ilGetCurImage());
}
// @TODO: Figure how to mess with multiple bpc's!
void ILAPIENTRY ilutGetBmpInfo(BITMAPINFO *Info)
{
ILuint NewBps, Padding;
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return;
}
Padding = (4 - (ilutCurImage->Bps % 4)) % 4;
NewBps = ilutCurImage->Bps/* + Padding*/;
Info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
Info->bmiHeader.biWidth = ilutCurImage->Width;
Info->bmiHeader.biHeight = ilutCurImage->Height;
Info->bmiHeader.biPlanes = 1;
Info->bmiHeader.biBitCount = ilutCurImage->Bpp << 3;
Info->bmiHeader.biCompression = BI_RGB;
Info->bmiHeader.biSizeImage = NewBps * ilutCurImage->Height;
Info->bmiHeader.biXPelsPerMeter = 0;
Info->bmiHeader.biYPelsPerMeter = 0;
Info->bmiHeader.biClrUsed = ilutCurImage->Bpp == 1 ? 255 : 0;
if (Info->bmiHeader.biClrUsed < 24)
Info->bmiHeader.biClrImportant = Info->bmiHeader.biClrUsed;
else
Info->bmiHeader.biClrImportant = 0;
return;
}
//! Just a convenience function.
HBITMAP ILAPIENTRY ilutWinLoadImage(ILstring FileName, HDC hDC)
{
HBITMAP Bitmap;
iBindImageTemp();
if (!ilLoadImage(FileName))
return 0;
Bitmap = ilutConvertToHBitmap(hDC);
return Bitmap;
}
#ifndef _WIN32_WCE
ILboolean ILAPIENTRY ilutWinSaveImage(ILstring FileName, HBITMAP Bitmap)
{
ILuint CurName;
ILboolean Saved;
CurName = ilGetCurName();
iBindImageTemp();
if (!ilutSetHBitmap(Bitmap)) {
ilBindImage(CurName);
return IL_FALSE;
}
Saved = ilSaveImage(FileName);
ilBindImage(CurName);
return Saved;
}
#endif//_WIN32_WCE
// @TODO: Just create a copy of the palette!
// Credit for this goes to the OpenGL SuperBible.
HPALETTE ILAPIENTRY ilutGetHPal()
{
HPALETTE Palette;
LOGPALETTE *LogPal;
ILuint NumEntries, i;
ILenum CurPalType;
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return NULL;
}
if (!ilutCurImage->Pal.Palette || !ilutCurImage->Pal.PalSize || ilutCurImage->Pal.PalType == IL_PAL_NONE) {
//ilSetError(ILUT_ILLEGAL_OPERATION);
return NULL;
}
CurPalType = ilutCurImage->Pal.PalType;
if (!ilConvertPal(IL_PAL_RGB24)) {
return NULL; // ilConvertPal already sets the error
}
NumEntries = ilutCurImage->Pal.PalSize / 3;
LogPal = (LOGPALETTE*)ialloc(sizeof(LOGPALETTE) + NumEntries * sizeof(PALETTEENTRY));
if (!LogPal) {
return NULL;
}
LogPal->palVersion = 0x300;
LogPal->palNumEntries = (WORD)NumEntries;
for (i = 0; i < NumEntries; i++) {
LogPal->palPalEntry[i].peRed = ilutCurImage->Pal.Palette[i * 3];
LogPal->palPalEntry[i].peGreen = ilutCurImage->Pal.Palette[i * 3 + 1];
LogPal->palPalEntry[i].peBlue = ilutCurImage->Pal.Palette[i * 3 + 2];
LogPal->palPalEntry[i].peFlags = 0;
}
Palette = CreatePalette(LogPal);
ifree(LogPal);
ilConvertPal(CurPalType); // Should we check the return value?
return Palette;
}
ILboolean ILAPIENTRY ilutSetHBitmap(HBITMAP Bitmap)
{
#ifndef _WIN32_WCE
BITMAPINFO Info[2];
HWND hWnd;
HDC hDC;
ILubyte *Buffer1 = NULL, *Buffer2 = NULL;
ILuint i, j, PadSize, Bps;
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
hWnd = GetForegroundWindow();
hDC = GetDC(hWnd);
// Query the dimensions
memset(&Info, 0, sizeof(BITMAPINFO));
Info[0].bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
GetDIBits(hDC, Bitmap, 0, 0, NULL, Info, DIB_RGB_COLORS);
// @TODO: Implement this shitz0rz!
if (Info[0].bmiHeader.biBitCount < 24) {
ReleaseDC(hWnd, hDC); //added 20040527
return IL_FALSE;
}
Buffer1 = (ILubyte*)ialloc(Info[0].bmiHeader.biSizeImage);
Buffer2 = (ILubyte*)ialloc(Info[0].bmiHeader.biSizeImage);
if (Buffer1 == NULL || Buffer2 == NULL) {
ReleaseDC(hWnd, hDC); //added 20040527
ifree(Buffer1);
ifree(Buffer2);
return IL_FALSE;
}
//GetBitmapBits
GetDIBits(hDC, Bitmap, 0, Info[0].bmiHeader.biHeight, Buffer1, Info, DIB_RGB_COLORS);
Bps = Info[0].bmiHeader.biWidth * (Info[0].bmiHeader.biBitCount >> 3);
PadSize = (4 - (Bps % 4)) % 4;
// Remove the padding.
for (i = 0, j = 0; i < Info[0].bmiHeader.biSizeImage; i += Bps + PadSize, j += Bps) {
memcpy(Buffer2 + j, Buffer1 + i, Bps);
}
if (Info[0].bmiHeader.biBitCount == 24) {
ilTexImage(Info[0].bmiHeader.biWidth, Info[0].bmiHeader.biHeight, 1,
(ILubyte)(Info[0].bmiHeader.biBitCount >> 3), IL_BGR, IL_UNSIGNED_BYTE, Buffer2);
}
else if (Info[0].bmiHeader.biBitCount == 32) {
ilTexImage(Info[0].bmiHeader.biWidth, Info[0].bmiHeader.biHeight, 1,
(ILubyte)(Info[0].bmiHeader.biBitCount >> 3), IL_BGRA, IL_UNSIGNED_BYTE, Buffer2);
}
ilutCurImage->Origin = IL_ORIGIN_LOWER_LEFT;
ReleaseDC(hWnd, hDC); //added 20040527
ifree(Buffer1);
ifree(Buffer2);
#endif//_WIN32_WCE
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutSetHPal(HPALETTE Pal)
{
LPPALETTEENTRY PalEntries;
ILuint NumEntries, i;
ILubyte *TempPal;
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
NumEntries = GetPaletteEntries(Pal, 0, 0, NULL);
if (NumEntries == 0)
return IL_TRUE; // @TODO: Determine if correct...
PalEntries = (LPPALETTEENTRY)ialloc(NumEntries * sizeof(PALETTEENTRY));
if (PalEntries == NULL) {
return IL_FALSE;
}
NumEntries = GetPaletteEntries(Pal, 0, NumEntries, PalEntries);
TempPal = (ILubyte*)ialloc(NumEntries * 3);
if (TempPal == NULL) {
ifree(PalEntries);
return IL_FALSE;
}
if (ilutCurImage->Pal.Palette)
ifree(ilutCurImage->Pal.Palette);
ilutCurImage->Pal.Palette = TempPal;
ilutCurImage->Pal.PalSize = NumEntries * 3;
ilutCurImage->Pal.PalType = IL_PAL_RGB24;
for (i = 0; i < NumEntries; i++) {
*TempPal++ = PalEntries[i].peRed;
*TempPal++ = PalEntries[i].peGreen;
*TempPal++ = PalEntries[i].peBlue;
}
ifree(PalEntries);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutSetWinClipboard()
{
HBITMAP Bitmap;
HANDLE Handle;
HWND hWnd;
HDC hDC;
ILimage *TempImage, *CurImage;
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
if (ilutCurImage->Format != IL_BGR || ilutCurImage->Bps > 1) {
TempImage = iConvertImage(ilutCurImage, IL_BGR, IL_UNSIGNED_BYTE);
if (TempImage == NULL)
return IL_FALSE;
}
else
TempImage = ilutCurImage;
CurImage = ilutCurImage;
ilSetCurImage(TempImage);
hWnd = GetForegroundWindow();
hDC = GetDC(hWnd);
if (!OpenClipboard(NULL)) {
if (TempImage != ilutCurImage)
ilCloseImage(TempImage);
ilSetCurImage(CurImage);
ilSetError(ILUT_ILLEGAL_OPERATION); // Dunno if this is the correct error.
ReleaseDC(hWnd, hDC); //added 20040604
if (TempImage != ilutCurImage)
ilCloseImage(TempImage);
ilSetCurImage(CurImage);
return IL_FALSE;
}
//note that this is not the best method to put an image into the
//clipboard, CF_DIB is much better because HBITMAPS are device-dependent.
//TODO: eventually change that if there is a need
Bitmap = ilutConvertToHBitmap(hDC);
ReleaseDC(hWnd, hDC); //added 20040604
EmptyClipboard();
Handle = SetClipboardData(CF_BITMAP, Bitmap);
CloseClipboard();
//DeleteObject(Bitmap); // Needed? No! Clipboard takes care of image.
if (TempImage != ilutCurImage)
ilCloseImage(TempImage);
ilSetCurImage(CurImage);
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutGetWinClipboard()
{
//HBITMAP Bitmap;
HWND hWnd;
HGLOBAL hGlobal;
PTSTR pGlobal, data;
BITMAPFILEHEADER *BmpHeader;
BITMAPINFOHEADER *InfoHeader;
SIZE_T Size;
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
if (IsClipboardFormatAvailable(CF_DIB)) {
hWnd = GetForegroundWindow();
if (!OpenClipboard(hWnd)) {
ilSetError(ILUT_ILLEGAL_OPERATION); // Dunno if this is the correct error.
return IL_FALSE;
}
hGlobal = GetClipboardData(CF_DIB);
if (!hGlobal) {
CloseClipboard();
return IL_FALSE; // No error?
}
//copy DIB to buffer because windows delivers it without the
//BITMAPFILEHEADER that DevIL needs to load the image
Size = GlobalSize(hGlobal);
//@TODO: Size should never be larger than an ILuint?
data = (PTSTR)ialloc((ILuint)Size + sizeof(BITMAPFILEHEADER));
pGlobal = (PTSTR)GlobalLock(hGlobal);
if (!pGlobal || !data) {
ifree(data);
CloseClipboard();
return IL_FALSE; // No error?
}
memcpy(data + sizeof(BITMAPFILEHEADER), pGlobal, Size);
GlobalUnlock(hGlobal);
CloseClipboard();
//create BITMAPFILEHEADER
InfoHeader = (BITMAPINFOHEADER*)(data + sizeof(BITMAPFILEHEADER));
BmpHeader = (BITMAPFILEHEADER*)data;
BmpHeader->bfType = 'B' | ('M' << 8);
//@TODO: Again, could it ever be larger than an unsigned integer (DWORD)?
BmpHeader->bfSize = (DWORD)Size + sizeof(BITMAPFILEHEADER);
BmpHeader->bfReserved1 = BmpHeader->bfReserved2 = 0;
BmpHeader->bfOffBits = sizeof(BITMAPFILEHEADER) + InfoHeader->biSize + InfoHeader->biClrUsed*4;
if (InfoHeader->biCompression == BI_BITFIELDS)
BmpHeader->bfOffBits += 12;
return ilLoadL(IL_BMP, data, BmpHeader->bfSize);
}
/*
//this is not required becaus CF_BITMAP is converted to CF_DIB automatically
//when needed. CF_DIB suffices.
else if (IsClipboardFormatAvailable(CF_BITMAP)) {
hWnd = GetForegroundWindow();
if (!OpenClipboard(hWnd)) {
ilSetError(ILUT_ILLEGAL_OPERATION); // Dunno if this is the correct error.
return IL_FALSE;
}
Bitmap = (HBITMAP)GetClipboardData(CF_BITMAP);
if (!Bitmap) {
CloseClipboard();
return IL_FALSE; // No error?
}
if (!ilutSetHBitmap(Bitmap)) {
CloseClipboard();
return IL_FALSE;
}
CloseClipboard();
}*/
//no data in clipboard
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
ILboolean ILAPIENTRY ilutWinPrint(ILuint XPos, ILuint YPos, ILuint Width, ILuint Height, HDC hDC)
{
#if !defined(_WIN32_WCE) && !(defined(_WIN32) && defined(__GNUC__))
PRINTDLG Pd;
DOCINFO Di;
HBITMAP Bitmap, hReplaced;
HDC hMemDC;
ilutCurImage = ilGetCurImage();
if (ilutCurImage == NULL) {
ilSetError(ILUT_ILLEGAL_OPERATION);
return IL_FALSE;
}
//@TODO: Needs error checking!
hMemDC = CreateCompatibleDC(hDC);
Bitmap = ilutConvertToHBitmap(hDC);
hReplaced = (HBITMAP)SelectObject(hMemDC, Bitmap);
memset(&Pd, 0, sizeof(PRINTDLG));
Pd.lStructSize = sizeof(PRINTDLG);
Pd.hwndOwner = GetForegroundWindow();
Pd.Flags = PD_RETURNDC;
Pd.nCopies = 1;
Pd.nFromPage = 0xFFFF;
Pd.nToPage = 0xFFFF;
Pd.nMinPage = 1;
Pd.nMaxPage = 0xFFFF;
if (!PrintDlg(&Pd))
return (0L);
Di.cbSize = sizeof(DOCINFO);
Di.lpszDocName = IL_TEXT("DevIL Print Job");
Di.lpszOutput = NULL;
Di.lpszDatatype = NULL;
Di.fwType = 0;
StartDoc(Pd.hDC, &Di);
StartPage(Pd.hDC);
StretchBlt(Pd.hDC, XPos, YPos, Width, Height, hMemDC, 0, 0, ilutCurImage->Width, ilutCurImage->Height, SRCCOPY);
EndPage(Pd.hDC);
EndDoc(Pd.hDC);
DeleteObject(Bitmap);
DeleteObject(hReplaced);
DeleteDC(Pd.hDC);
#endif
return IL_TRUE;
}
ILboolean ILAPIENTRY ilutLoadResource(HINSTANCE hInst, ILint ID, ILstring ResourceType, ILenum Type)
{
HRSRC Resource = (HRSRC)LoadResource(hInst, FindResource(hInst, MAKEINTRESOURCE(ID), ResourceType));
ILubyte *Data = (ILubyte*)LockResource(Resource);
return ilLoadL(Type, Data, SizeofResource(hInst, FindResource(hInst, MAKEINTRESOURCE(ID), ResourceType)));
}
#if !defined(_WIN32_WCE) && !(defined(_WIN32) && defined(__GNUC__))
#define BUFFSIZE 8192 // Change to suit the efficiency.
ILboolean ILAPIENTRY ilutWinLoadUrl(ILstring Url)
{
HINTERNET Handle, UrlHandle;
DWORD BytesRead = 0, Context = 1;
ILubyte Buff[BUFFSIZE], *Buffer, *TempBuff;
ILuint BufferSize = 0, i;
ILboolean Is404 = IL_TRUE;
char Buffer404[] = { '<', 'h', 't', 'm', 'l', '>' };
Buffer = (ILubyte*)ialloc(0);
if (Buffer == NULL) {
return IL_FALSE;
}
Handle = InternetOpen(IL_TEXT("Developer's Image Library"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (Handle == NULL) {
ifree(Buffer);
ilSetError(ILUT_COULD_NOT_OPEN_FILE);
return IL_FALSE;
}
// Try again if fails the first time, loading only from the cache.
UrlHandle = InternetOpenUrl(Handle, Url, NULL, 0, 0, Context);
if (UrlHandle == NULL) {
InternetCloseHandle(Handle);
Handle = InternetOpen(IL_TEXT("Developer's Image Library"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_FROM_CACHE);
if (Handle == NULL) {
ifree(Buffer);
ilSetError(ILUT_COULD_NOT_OPEN_FILE);
return IL_FALSE;
}
UrlHandle = InternetOpenUrl(Handle, Url, NULL, 0, 0, Context);
if (UrlHandle == NULL) {
ifree(Buffer);
InternetCloseHandle(Handle);
ilSetError(ILUT_COULD_NOT_OPEN_FILE);
return IL_FALSE;
}
}
do {
if (!InternetReadFile(UrlHandle, Buff, BUFFSIZE, &BytesRead)) {
InternetCloseHandle(UrlHandle);
InternetCloseHandle(Handle);
ifree(Buffer);
ilSetError(ILUT_COULD_NOT_OPEN_FILE);
return IL_FALSE;
}
TempBuff = (ILubyte*)ialloc(BufferSize + BytesRead);
if (TempBuff == NULL) {
ifree(Buffer);
return IL_FALSE;
}
memcpy(TempBuff, Buffer, BufferSize);
memcpy(TempBuff + BufferSize, Buff, BytesRead);
ifree(Buffer);
Buffer = TempBuff;
BufferSize += BytesRead;
} while (BytesRead > 0);
InternetCloseHandle(UrlHandle);
InternetCloseHandle(Handle);
// If the image does not exist, the server usually returns a 404 HTML page.
for (i = 0; i < sizeof(Buffer404) && i < BufferSize; i++) {
if (tolower(Buffer[i]) != Buffer404[i]) {
Is404 = IL_FALSE;
break;
}
}
if (!Is404) {
if (!ilLoadL(ilTypeFromExt(Url), Buffer, BufferSize)) {
if (!ilLoadL(IL_TYPE_UNKNOWN, Buffer, BufferSize)) {
ifree(Buffer);
return IL_FALSE;
}
}
}
ifree(Buffer);
return IL_TRUE;
}
#endif
#endif//ILUT_USE_WIN32
+399
View File
@@ -0,0 +1,399 @@
//-----------------------------------------------------------------------------
//
// ImageLib Sources
// Copyright (C) 2002 by Denton Woods
// Copyright (C) 2002 Nelson Rush.
// Last modified: 05/18/2002
//
// Filename: src-ILUT/src/ilut_x11.c
//
// Description: X11 Pixmap and XImage binding functions (with XShm)
//
//-----------------------------------------------------------------------------
/*
** This file was created by Jesse Maurais Wed April 4, 2007
** Contact: www.jessemaurais@gmail.com
**
**
** This patch to the Devil Tookit binds to the X Windows System Version 11,
** using client-side XImages and server-side Pixmaps, with support for both
** ZPixmaps and the insane XYPixmap format. (Depends on the X server)
**
** If the XShm extension to X11 is present at the time of ./configure then
** support for shared memory XImages and Pixmaps is also compiled in. Note
** that "shared" does not mean Devil and X are sharing the same memory space.
** This is not possible, because both libraries make byte-for-byte copies
** of their data. It means that memory is part of an inter-process memory
** segment (see XShm spec).
**
** TODO
** 1) Assumed the display depth is 24 bits (the most common) but there should
** be a check, and iXConvertImage should handle this properly. I don't think
** this should be difficult to modify from whats here.
** 2) It would be nice to convert from an XImage back to a Devil image for
** saving changes. Would be seful for an interactive image editor.
** 3) Possibly some additional OpenGL bindings for GLX Pbuffers.
**
** FYI
** It was a long night figuring out the under-documented XYPixmap format.
*/
#include "ilut_internal.h"
#ifdef ILUT_USE_X11
int bits; // bits per pixel
int field; // bits per channel
int bytes; // bytes per pixel
int grain; // bytes per line
int width; // pixels per line
int height; // number of lines
ILpal* palette; // for indexed colors
char* data; // pointer to pixels
void iXGrabImage( ILimage * img )
{
bits = img->Bpp*8; // bits per pixel
field = img->Bpc; // bits per channel
bytes = img->Bpp; // bytes per pixel
grain = img->Bps; // bytes per line
width = img->Width;
height = img->Height;
palette = &img->Pal;
data = (char*)img->Data;
}
Bool iXGrabCurrentImage(void)
{
ilutCurImage = ilGetCurImage();
if (!ilutCurImage) {
return False;
}
iXGrabImage(ilutCurImage);
return True;
}
void iXConvertImage( Display * dpy, XImage * img )
{
int x,y,z;
int sX,dX;
int sY,dY;
int sZ,dZ;
int plane;
ILimage * tmp;
switch ( img->byte_order )
{
case LSBFirst:
tmp = iConvertImage( ilutCurImage,IL_BGR,IL_UNSIGNED_BYTE );
break;
case MSBFirst:
tmp = iConvertImage( ilutCurImage,IL_RGB,IL_UNSIGNED_BYTE );
break;
default:
return;
}
if ( !tmp ) return;
iXGrabImage( tmp );
switch ( img->format )
{
case ZPixmap:
for ( y = 0; y < height; y ++ )
{
dY = y * img->bytes_per_line;
sY = y * grain;
for ( x = 0; x < width; x ++ )
{
dX = x * img->bits_per_pixel / 8;
sX = x * bytes;
for ( z = 0; z < bytes; z ++ )
{
img->data[dX+dY+z] = data[sX+sY+z];
}
}
}
break;
case XYPixmap:
for ( y = 0; y < height; y ++ )
{
sY = y * grain;
for ( x = 0; x < width; x ++ )
{
sX = x * bytes;
for ( z = 0; z < bits; z ++ )
{
sZ = z / 8;
dZ = z % 8;
if ( data[sY+sX+sZ] & ( 1 << dZ ) )
{
plane = bits - z - 1;
sZ = x % 8;
dX = x / 8;
dY = y * img->bytes_per_line;
dZ = plane * img->bytes_per_line * height;
img->data[dZ+dY+dX] |= 1 << sZ;
}
}
}
}
break;
default:
ilSetError( ILUT_NOT_SUPPORTED );
}
ilCloseImage( tmp );
}
ILboolean ilutXInit(void) {
return IL_TRUE;
}
XImage * ILAPIENTRY ilutXCreateImage( Display * dpy )
{
Visual * vis;
XImage * img;
char * buffer;
if (!iXGrabCurrentImage()) {
return NULL;
}
buffer = malloc( width * height * 4 );
if (!buffer) {
return NULL;
}
vis = CopyFromParent;
img = XCreateImage( dpy,vis, 24,ZPixmap,0,buffer,width,height,8,0 );
if (!img) {
free(buffer);
return NULL;
}
iXConvertImage( dpy,img );
return img;
}
Pixmap ILAPIENTRY ilutXCreatePixmap( Display * dpy, Drawable draw )
{
XImage * img;
GC gc;
Pixmap pix;
img = ilutXCreateImage( dpy );
if (!img) {
return None;
}
gc = DefaultGC(dpy,DefaultScreen(dpy));
if (!gc) {
XDestroyImage( img );
return None;
}
pix = XCreatePixmap( dpy,draw, width,height,24 );
if (!pix ) {
XDestroyImage( img );
return None;
}
XPutImage( dpy,pix,gc,img, 0,0,0,0,width,height );
XDestroyImage( img );
return pix;
}
XImage * ILAPIENTRY ilutXLoadImage( Display * dpy, char * filename )
{
iBindImageTemp();
if (!ilLoadImage(filename)) {
return NULL;
}
return ilutXCreateImage( dpy );
}
Pixmap ILAPIENTRY ilutXLoadPixmap( Display * dpy, Drawable draw, char * filename )
{
iBindImageTemp();
if (!ilLoadImage(filename)) {
return None;
}
return ilutXCreatePixmap( dpy,draw );
}
#ifdef ILUT_USE_XSHM
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>
XImage * ILAPIENTRY ilutXShmCreateImage( Display * dpy, XShmSegmentInfo * info )
{
Visual * vis;
XImage * img;
// Get server supported format
int size,format = XShmPixmapFormat( dpy );
// Grab the current image
if (!iXGrabCurrentImage()) {
return NULL;
}
// Create a shared image
vis = CopyFromParent;
img = XShmCreateImage( dpy,vis, 24,format,NULL,info,width,height );
if (!img) {
return NULL;
}
// Create shared memory
size = img->bytes_per_line * img->height;
info->shmid = shmget( IPC_PRIVATE, size, IPC_CREAT | 0666 );
info->shmaddr = img->data = shmat( info->shmid, 0, 0 );
info->readOnly = False;
// Attach to server
XShmAttach( dpy,info );
// Copy image pixels to shared memory
iXConvertImage( dpy,img );
return img;
}
void ILAPIENTRY ilutXShmDestroyImage( Display * dpy, XImage * img, XShmSegmentInfo * info )
{
XShmDetach( dpy,info );
XDestroyImage( img );
XFlush( dpy );
shmdt( info->shmaddr );
shmctl( info->shmid, IPC_RMID, 0 );
}
Pixmap ILAPIENTRY ilutXShmCreatePixmap( Display * dpy, Drawable draw, XShmSegmentInfo * info )
{
Pixmap pix;
XImage*img;
// Create a dumby image
img = ilutXShmCreateImage( dpy,info );
if (!img) {
return None;
}
// Use the same memory segment in the pixmap
pix = XShmCreatePixmap( dpy,draw, info->shmaddr,info,width,height,24 );
if (!pix) {
ilutXShmDestroyImage( dpy,img,info );
return None;
}
// Riddance to the image
XDestroyImage( img );
return pix;
}
void ILAPIENTRY ilutXShmFreePixmap( Display * dpy, Pixmap pix, XShmSegmentInfo * info )
{
XShmDetach( dpy,info );
XFreePixmap( dpy,pix );
XFlush( dpy );
shmdt( info->shmaddr );
shmctl( info->shmid, IPC_RMID, 0 );
}
XImage * ILAPIENTRY ilutXShmLoadImage( Display * dpy, char* filename, XShmSegmentInfo * info )
{
iBindImageTemp();
if (!ilLoadImage(filename)) {
return NULL;
}
return ilutXShmCreateImage( dpy,info );
}
Pixmap ILAPIENTRY ilutXShmLoadPixmap( Display * dpy, Drawable draw, char* filename, XShmSegmentInfo * info )
{
iBindImageTemp();
if (!ilLoadImage(filename)) {
return None;
}
return ilutXShmCreatePixmap( dpy,draw,info );
}
#endif//ILUT_USE_XSHM
#endif//ILUT_USE_X11