1 //-----------------------------------------------------------------------------
2 //
3 // DevIL GDI+ Sources
4 // Copyright (C) 2000-2002 by Denton Woods
5 // Last modified: 02/13/2002 <--Y2K Compliant! =]
6 //
7 // Filename: gdi+/devil-gdi+.cpp
8 //
9 // Description: GDI+ loading routines
10 //
11 //-----------------------------------------------------------------------------
12 
13 #include "stdafx.h"
14 #include "DevIL-GDI+.h"
15 
16 
17 // Basic Palette struct
18 typedef struct ILpal
19 {
20 	ILubyte	*Palette;		// the image palette (if any)
21 	ILuint	PalSize;		// size of the palette (in bytes)
22 	ILenum	PalType;		// the palette types below (0x0500 range)
23 } ILpal;
24 
25 
26 // The Fundamental Image struct
27 typedef struct ILimage
28 {
29 	ILuint	Width;				// the image's width
30 	ILuint	Height;				// the image's height
31 	ILuint	Depth;				// the image's depth
32 	ILubyte	Bpp;				// bytes per pixel (now number of channels)
33 	ILubyte	Bpc;				// bytes per channel
34 	ILuint	Bps;				// bytes per scanline (components for IL)
35 	ILubyte	*Data;				// the image data
36 	ILuint	SizeOfData;			// the total size of the data (in bytes)
37 	ILuint	SizeOfPlane;		// SizeOfData in a 2d image, size of each plane slice in a 3d image (in bytes)
38 	ILenum	Format;				// image format (in IL enum style)
39 	ILenum	Type;				// image type (in IL enum style)
40 	ILenum	Origin;				// origin of the image
41 	ILpal	Pal;				// palette details
42 	ILuint	Duration;			// length of the time to display this "frame"
43 	ILenum	CubeFlags;			// cube map flags for sides present in chain
44 	struct	ILimage *Mipmaps;	// mipmapped versions of this image terminated by a NULL - usu. NULL
45 	struct	ILimage *Next;		// next image in the chain - usu. NULL
46 	struct	ILimage *Layers;	// subsequent layers in the chain - usu. NULL
47 	ILuint	NumNext;			// number of images following this one (0 when not parent)
48 	ILuint	NumMips;			// number of mipmaps (0 when not parent)
49 	ILuint	NumLayers;			// number of layers (0 when not parent)
50 	ILuint	*AnimList;			// animation list
51 	ILuint	AnimSize;			// animation list size
52 	ILvoid	*Profile;			// colour profile
53 	ILuint	ProfileSize;		// colour profile size
54 	ILuint	OffX, OffY;			// offset of the image
55 } ILimage;
56 
57 ILAPI ILimage*	ILAPIENTRY iConvertImage(ILenum DestFormat, ILenum DestType);
58 ILAPI ILubyte*	ILAPIENTRY iGetPaddedData(ILimage *Image);
59 ILAPI ILimage*	ILAPIENTRY ilGetCurImage(void);
60 ILAPI ILvoid	ILAPIENTRY ilCloseImage(ILimage *Image);
61 ILAPI ILvoid	ILAPIENTRY iBindImageTemp(void);
62 
63 
64 // @TODO: Use 48-bit and 64-bit types.
ilutConvertToBitmap()65 Bitmap *ilutConvertToBitmap()
66 {
67 	Bitmap	*Bmp = NULL;
68 	ILubyte	*Data = NULL;
69 	ILimage	*Image = NULL, *GdiCurImage = NULL;
70 
71 	GdiCurImage = ilGetCurImage();
72 	if (GdiCurImage == NULL) {
73 		return NULL;
74 	}
75 
76 	if (GdiCurImage->Format == IL_BGRA && GdiCurImage->Format == IL_UNSIGNED_BYTE) {
77 		Image = GdiCurImage;
78 		return new Bitmap(Image->Width, Image->Height, Image->Bps, PixelFormat32bppARGB, Image->Data);
79 	}
80 	else if (GdiCurImage->Format == IL_BGRA || GdiCurImage->Format == IL_RGBA) {
81 		Image = iConvertImage(IL_BGRA, IL_UNSIGNED_BYTE);
82 		if (Image == NULL)
83 			return NULL;
84 		Bmp = new Bitmap(Image->Width, Image->Height, Image->Bps, PixelFormat32bppARGB, Image->Data);
85 		ilCloseImage(Image);
86 		return Bmp;
87 	}
88 	else if (GdiCurImage->Format == IL_LUMINANCE &&
89 		(GdiCurImage->Type == IL_UNSIGNED_SHORT || GdiCurImage->Type == IL_SHORT)) {
90 			Image = GdiCurImage;
91 			return new Bitmap(Image->Width, Image->Height, Image->Bps, PixelFormat16bppGrayScale, Image->Data);
92 	}
93 	else if (GdiCurImage->Format == IL_LUMINANCE) {
94 		Image = iConvertImage(IL_LUMINANCE, IL_UNSIGNED_SHORT);
95 		if (Image == NULL)
96 			return NULL;
97 		Bmp = new Bitmap(Image->Width, Image->Height, Image->Bps, PixelFormat16bppGrayScale, Image->Data);
98 		ilCloseImage(Image);
99 		return Bmp;
100 	}
101 	else {
102 		Image = iConvertImage(IL_BGR, IL_UNSIGNED_BYTE);
103 		if (Image == NULL)
104 			return NULL;
105 		Bmp = new Bitmap(Image->Width, Image->Height, Image->Bps, PixelFormat24bppRGB, Image->Data);
106 		ilCloseImage(Image);
107 		return Bmp;
108 	}
109 
110 	return NULL;  // Something really screwy has happened here...
111 }
112 
113 
ilutGDILoadImage(const ILstring FileName)114 Bitmap *ilutGDILoadImage(const ILstring FileName)
115 {
116 	Bitmap *Bmp;
117 
118 	iBindImageTemp();
119 	if (!ilLoadImage(FileName))
120 		return NULL;
121 
122 	Bmp = ilutConvertToBitmap();
123 
124 	return Bmp;
125 }
126 
127 
128