- Main Page
- Data Structures
- Files

- File List

# E:/DevIL/src-ILUT/src/ilut_win32.c

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Utility Toolkit Sources
    00004 // Copyright (C) 2000-2008 by Denton Woods
    00005 // Last modified: 12/14/2008
    00006 //
    00007 // Filename: src-ILUT/src/ilut_win32.c
    00008 //
    00009 // Description: Windows functions for images
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013
    00014 #include "ilut_internal.h"
    00015 #ifdef ILUT_USE_WIN32
    00016 #include <windows.h>
    00017 #include <wininet.h>
    00018
    00019 // For ilutWinLoadUrl().
    00020 #ifdef _MSC_VER
    00021 #pragma comment(lib, "wininet.lib")
    00022 #endif
    00023
    00024
    00025 #if !_WIN32_WCE && (_WIN32 && __GNUC__)
    00026         PRINTDLG        Pd;
    00027 #endif//_WIN32_WCE
    00028
    00029 ILboolean ilutWin32Init()
    00030 {
    00031
    00032
    00033         return IL_TRUE;
    00034 }
    00035
    00036
    00037 ILAPI HBITMAP ILAPIENTRY ilutConvertSliceToHBitmap(HDC hDC, ILuint slice)
    00038 {
    00039         ILubyte         *Data, *DataBackup;
    00040         HBITMAP         hBitmap = NULL;
    00041         ILimage         *TempImage = NULL;
    00042         ILuint          pad, i, j, k, l, m, n, DepthBackup;
    00043         ILpal           *palImg;
    00044         ILboolean       alloc_buffer;
    00045
    00046         //reserve space for palette in every case...
    00047         ILubyte         buff[sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD)];
    00048         BITMAPINFO      *info = (BITMAPINFO*)buff;
    00049         RGBQUAD         *pal = info->bmiColors;
    00050
    00051         ilutCurImage = ilGetCurImage();
    00052         if (ilutCurImage == NULL) {
    00053                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00054                 return NULL;
    00055         }
    00056
    00057         //check if the image has the wanted slice
    00058         if (slice < 0 || slice >= ilutCurImage->Depth) {
    00059                 ilSetError(ILUT_INVALID_PARAM);
    00060                 return NULL;
    00061         }
    00062
    00063         // Fool iConvertImage into thinking that the current image has
    00064         //   only one slice, the one we want:
    00065         DepthBackup = ilutCurImage->Depth;
    00066         DataBackup = ilutCurImage->Data;
    00067         ilutCurImage->Depth = 1;
    00068         ilutCurImage->Data += ilutCurImage->SizeOfPlane*slice;
    00069
    00070         if (ilutCurImage->Type != IL_UNSIGNED_BYTE)
    00071                 TempImage = iConvertImage(ilutCurImage, ilutCurImage->Format, IL_UNSIGNED_BYTE);
    00072         else
    00073                 TempImage = ilutCurImage;
    00074         if (TempImage == NULL) {
    00075                 goto error;
    00076         }
    00077
    00078         //changed 2003-09-09: use Temp!
    00079         ilSetCurImage(TempImage);
    00080
    00081         hBitmap = CreateCompatibleBitmap(hDC, ilutCurImage->Width, ilutCurImage->Height);
    00082         if (hBitmap == NULL) {
    00083                 ilSetError(IL_UNKNOWN_ERROR);
    00084                 goto error;
    00085         }
    00086
    00087         info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    00088         info->bmiHeader.biWidth = TempImage->Width;
    00089         if (TempImage->Origin == IL_ORIGIN_UPPER_LEFT)
    00090                 info->bmiHeader.biHeight = -(ILint)TempImage->Height;
    00091         else
    00092                 info->bmiHeader.biHeight = TempImage->Height;
    00093         info->bmiHeader.biPlanes = 1;
    00094         info->bmiHeader.biCompression = 0;
    00095         info->bmiHeader.biSizeImage = 0;
    00096         info->bmiHeader.biXPelsPerMeter = 0;
    00097         info->bmiHeader.biYPelsPerMeter = 0;
    00098         info->bmiHeader.biClrUsed = 0;
    00099         info->bmiHeader.biClrImportant = 0;
    00100
    00101         pad = (4 - TempImage->Bps%4)%4;
    00102         alloc_buffer = (ILboolean)!(pad == 0 && TempImage->Format != IL_RGB
    00103                 && TempImage->Format != IL_RGBA && TempImage->Format != IL_LUMINANCE_ALPHA);
    00104         if (!alloc_buffer) {
    00105                 Data = TempImage->Data;
    00106         }
    00107         else {
    00108                 ILsizei DataSize = 0;
    00109                 if (TempImage->Format == IL_RGBA) {  // Strip alpha during byte swapping for faster upload to GDI.
    00110                         // Recalculate pad, because it changes when bpp changes.
    00111                         pad = (4 - (3 * TempImage->Width) % 4) % 4;
    00112                         DataSize = (TempImage->Width + pad) * TempImage->Height * 3;
    00113                 }
    00114                 // Strip alpha channel from grayscale image.
    00115                 else if (TempImage->Format == IL_LUMINANCE_ALPHA) {
    00116                         // Added 01-09-2009: Recalculate pad.
    00117                         pad = (4 - TempImage->Width % 4) % 4;
    00118                         DataSize = (TempImage->Width + pad) * TempImage->Height;
    00119                 }
    00120                 else {
    00121                         DataSize = (TempImage->Width + pad) * TempImage->Height * TempImage->Bpp;
    00122                 }
    00123
    00124                 Data = (ILubyte*)ialloc(DataSize);
    00125                 if (Data == NULL) {
    00126                         goto error;
    00127                 }
    00128
    00129                 if (TempImage->Format == IL_RGB || TempImage->Format == IL_RGBA) {
    00130                         //swap bytes
    00131                         m = (TempImage->Format == IL_RGB)?3:4;
    00132                         k = l = 0;
    00133                         for (j = 0; j < TempImage->Height; j++) {
    00134                                 for (i = 0, n = 0; i < 3*TempImage->Width; i += 3, n += m) {
    00135                                         Data[l + i] = TempImage->Data[k + n + 2];
    00136                                         Data[l + i + 1] = TempImage->Data[k + n + 1];
    00137                                         Data[l + i + 2] = TempImage->Data[k + n];
    00138                                 }
    00139
    00140                                 k += TempImage->Bps;
    00141                                 l += 3*TempImage->Width + pad;
    00142                         }
    00143                 }
    00144                 else if (TempImage->Format == IL_LUMINANCE_ALPHA) {
    00145                         //strip alpha channel
    00146                         //recalculate pad because it included alpha channel info
    00147                         pad = (4 - TempImage->Width%4)%4;
    00148                         k = l = 0;
    00149                         for (j = 0; j < TempImage->Height; j++) {
    00150                                 for (i = 0, n = 0; i < TempImage->Width; ++i, n += 2) {
    00151                                         Data[l + i] = TempImage->Data[k + n];
    00152                                 }
    00153                                 k += TempImage->Bps;
    00154                                 l += TempImage->Width + pad;
    00155                         }
    00156                 }
    00157                 else
    00158                         for (i = 0; i < TempImage->Height; i++)
    00159                                 memcpy(Data + i*(TempImage->Bps + pad), TempImage->Data + i*TempImage->Bps, TempImage->Bps);
    00160         }
    00161
    00162         switch (TempImage->Format)
    00163         {
    00164                 case IL_LUMINANCE:
    00165                 case IL_LUMINANCE_ALPHA:
    00166                 case IL_COLOUR_INDEX:
    00167                         if (TempImage->Format != IL_COLOUR_INDEX) {
    00168                                 //generate greyscale palette
    00169                                 for (i = 0; i < 256; i++)
    00170                                         pal[i].rgbRed = pal[i].rgbGreen = pal[i].rgbBlue = (ILubyte)i;
    00171                         }
    00172                         else {
    00173                                 palImg = iConvertPal(&TempImage->Pal, IL_PAL_BGR32);
    00174                                 if (palImg != NULL) {
    00175                                         memcpy(pal, palImg->Palette, palImg->PalSize);
    00176                                         ilClosePal(palImg);
    00177                                 }
    00178                                 else {
    00179                                         //ilSetError(IL_INVALID_PARAM);
    00180                                         // Generate greyscale palette  <-- Why is this here?
    00181                                         for (i = 0; i < 256; i++)
    00182                                                 pal[i].rgbRed = pal[i].rgbGreen = pal[i].rgbBlue = (ILubyte)i;
    00183                                 }
    00184                         }
    00185                         info->bmiHeader.biBitCount = 8;
    00186                         break;
    00187
    00188                 case IL_RGB:
    00189                 case IL_BGR:
    00190                 case IL_RGBA: //alpha is removed during byte swapping
    00191                         info->bmiHeader.biBitCount = 24;
    00192                         break;
    00193
    00194                 case IL_BGRA:
    00195                         info->bmiHeader.biBitCount = 32;
    00196                         break;
    00197
    00198                 /*default:
    00199                         ilSetError(IL_FORMAT_NOT_SUPPORTED);
    00200                         return hBitmap;*/
    00201         }
    00202
    00203         // Restore original data
    00204         ilutCurImage->Data = DataBackup;
    00205         ilutCurImage->Depth = DepthBackup;
    00206
    00207         SetDIBits(hDC, hBitmap, 0, ilutCurImage->Height, Data, info, DIB_RGB_COLORS);
    00208
    00209         if (alloc_buffer)
    00210                 ifree(Data);
    00211
    00212         if (ilutCurImage != TempImage) {
    00213                 ilSetCurImage(ilutCurImage);
    00214                 ilCloseImage(TempImage);
    00215         }
    00216
    00217         return hBitmap;
    00218
    00219 error:
    00220         // Restore original data
    00221         ilutCurImage->Data = DataBackup;
    00222         ilutCurImage->Depth = DepthBackup;
    00223         if (ilutCurImage != TempImage) {
    00224                 ilSetCurImage(ilutCurImage);
    00225                 ilCloseImage(TempImage);
    00226         }
    00227         ilSetCurImage(ilutCurImage);
    00228         if (hBitmap)
    00229                 DeleteObject(hBitmap);
    00230
    00231         return NULL;
    00232 }
    00233
    00234 HBITMAP ILAPIENTRY ilutConvertToHBitmap(HDC hDC)
    00235 {
    00236         return ilutConvertSliceToHBitmap(hDC, 0);
    00237 }
    00238
    00239 ILubyte* ILAPIENTRY iGetPaddedData(ILimage *Image)
    00240 {
    00241         ILubyte *NewData = NULL, *TempBuff = NULL;
    00242         ILuint  i, CurPos = 0, PadSize;
    00243         ILubyte *TempData = NULL;
    00244
    00245         if (Image == NULL) {
    00246                 ilSetError(ILUT_INVALID_PARAM);
    00247                 return NULL;
    00248         }
    00249
    00250         if (Image->Origin != IL_ORIGIN_LOWER_LEFT) {
    00251                 TempData = iGetFlipped(Image);
    00252         }
    00253         else {
    00254                 TempData = Image->Data;
    00255         }
    00256
    00257         if (Image->Format == IL_RGB || Image->Format == IL_RGBA) {
    00258                 TempBuff = (ILubyte*)ialloc(Image->SizeOfData);
    00259
    00260                 if (TempBuff == NULL) {
    00261                         return NULL;
    00262                 }
    00263                 // Swap red and blue.
    00264                 for (i = 0; i < Image->SizeOfData; i += Image->Bpp) {
    00265                         TempBuff[i] = TempData[i+2];
    00266                         TempBuff[i+1] = TempData[i+1];
    00267                         TempBuff[i+2] = TempData[i];
    00268                         // Copy the alpha channel if present.
    00269                         if (Image->Bpp == 4)
    00270                                 TempBuff[i+3] = TempData[i+3];
    00271                 }
    00272         }
    00273         else {
    00274                 TempBuff = TempData;
    00275         }
    00276
    00277         PadSize = (4 - (Image->Bps % 4)) % 4;
    00278         NewData = (ILubyte*)ialloc((Image->Width + PadSize) * Image->Height * Image->Bpp);
    00279         if (NewData == NULL) {
    00280                 return NULL;
    00281         }
    00282
    00283         for (i = 0; i < Image->Height; i++) {
    00284                 memcpy(NewData + CurPos, TempBuff + Image->Bps * i, Image->Bps);
    00285                 CurPos += Image->Bps;
    00286                 memset(NewData + CurPos, 0, PadSize);
    00287                 CurPos += PadSize;
    00288         }
    00289
    00290         if (TempData != TempBuff && TempData != Image->Data)
    00291                 ifree(TempData);
    00292         if (TempBuff != Image->Data)
    00293                 ifree(TempBuff);
    00294
    00295         return NewData;
    00296 }
    00297
    00298
    00299
    00300 void ILAPIENTRY ilutFreePaddedData(ILubyte *Data)
    00301 {
    00302         ifree(Data);
    00303         return;
    00304 }
    00305
    00306
    00307 // DirectX/GDI insists that all scanlines end on a dword boundary. =(
    00308 ILubyte* ILAPIENTRY ilutGetPaddedData()
    00309 {
    00310         return iGetPaddedData(ilGetCurImage());
    00311 }
    00312
    00313
    00314 // @TODO:  Figure how to mess with multiple bpc's!
    00315 void ILAPIENTRY ilutGetBmpInfo(BITMAPINFO *Info)
    00316 {
    00317         ILuint NewBps, Padding;
    00318
    00319         ilutCurImage = ilGetCurImage();
    00320         if (ilutCurImage == NULL) {
    00321                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00322                 return;
    00323         }
    00324
    00325         Padding = (4 - (ilutCurImage->Bps % 4)) % 4;
    00326         NewBps = ilutCurImage->Bps/* + Padding*/;
    00327
    00328         Info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    00329         Info->bmiHeader.biWidth = ilutCurImage->Width;
    00330         Info->bmiHeader.biHeight = ilutCurImage->Height;
    00331         Info->bmiHeader.biPlanes = 1;
    00332         Info->bmiHeader.biBitCount = ilutCurImage->Bpp << 3;
    00333         Info->bmiHeader.biCompression = BI_RGB;
    00334         Info->bmiHeader.biSizeImage = NewBps * ilutCurImage->Height;
    00335         Info->bmiHeader.biXPelsPerMeter = 0;
    00336         Info->bmiHeader.biYPelsPerMeter = 0;
    00337         Info->bmiHeader.biClrUsed = ilutCurImage->Bpp == 1 ? 255 : 0;
    00338         if (Info->bmiHeader.biClrUsed < 24)
    00339                 Info->bmiHeader.biClrImportant = Info->bmiHeader.biClrUsed;
    00340         else
    00341                 Info->bmiHeader.biClrImportant = 0;
    00342
    00343         return;
    00344 }
    00345
    00346
    00348 HBITMAP ILAPIENTRY ilutWinLoadImage(ILstring FileName, HDC hDC)
    00349 {
    00350         HBITMAP Bitmap;
    00351
    00352         iBindImageTemp();
    00353         if (!ilLoadImage(FileName))
    00354                 return 0;
    00355
    00356         Bitmap = ilutConvertToHBitmap(hDC);
    00357
    00358         return Bitmap;
    00359 }
    00360
    00361
    00362 #ifndef _WIN32_WCE
    00363 ILboolean ILAPIENTRY ilutWinSaveImage(ILstring FileName, HBITMAP Bitmap)
    00364 {
    00365         ILuint          CurName;
    00366         ILboolean       Saved;
    00367
    00368         CurName = ilGetCurName();
    00369
    00370         iBindImageTemp();
    00371
    00372         if (!ilutSetHBitmap(Bitmap)) {
    00373                 ilBindImage(CurName);
    00374                 return IL_FALSE;
    00375         }
    00376
    00377         Saved = ilSaveImage(FileName);
    00378         ilBindImage(CurName);
    00379
    00380         return Saved;
    00381 }
    00382 #endif//_WIN32_WCE
    00383
    00384
    00385 // @TODO:  Just create a copy of the palette!
    00386 // Credit for this goes to the OpenGL SuperBible.
    00387 HPALETTE ILAPIENTRY ilutGetHPal()
    00388 {
    00389         HPALETTE        Palette;
    00390         LOGPALETTE      *LogPal;
    00391         ILuint          NumEntries, i;
    00392         ILenum          CurPalType;
    00393
    00394         ilutCurImage = ilGetCurImage();
    00395         if (ilutCurImage == NULL) {
    00396                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00397                 return NULL;
    00398         }
    00399
    00400         if (!ilutCurImage->Pal.Palette || !ilutCurImage->Pal.PalSize || ilutCurImage->Pal.PalType == IL_PAL_NONE) {
    00401                 //ilSetError(ILUT_ILLEGAL_OPERATION);
    00402                 return NULL;
    00403         }
    00404
    00405         CurPalType = ilutCurImage->Pal.PalType;
    00406         if (!ilConvertPal(IL_PAL_RGB24)) {
    00407                 return NULL;  // ilConvertPal already sets the error
    00408         }
    00409         NumEntries = ilutCurImage->Pal.PalSize / 3;
    00410
    00411         LogPal = (LOGPALETTE*)ialloc(sizeof(LOGPALETTE) + NumEntries * sizeof(PALETTEENTRY));
    00412         if (!LogPal) {
    00413                 return NULL;
    00414         }
    00415
    00416         LogPal->palVersion = 0x300;
    00417         LogPal->palNumEntries = (WORD)NumEntries;
    00418
    00419         for (i = 0; i < NumEntries; i++) {
    00420                 LogPal->palPalEntry[i].peRed   = ilutCurImage->Pal.Palette[i * 3];
    00421                 LogPal->palPalEntry[i].peGreen = ilutCurImage->Pal.Palette[i * 3 + 1];
    00422                 LogPal->palPalEntry[i].peBlue  = ilutCurImage->Pal.Palette[i * 3 + 2];
    00423                 LogPal->palPalEntry[i].peFlags = 0;
    00424         }
    00425
    00426         Palette = CreatePalette(LogPal);
    00427         ifree(LogPal);
    00428
    00429         ilConvertPal(CurPalType);  // Should we check the return value?
    00430
    00431         return Palette;
    00432 }
    00433
    00434
    00435 ILboolean ILAPIENTRY ilutSetHBitmap(HBITMAP Bitmap)
    00436 {
    00437 #ifndef _WIN32_WCE
    00438         BITMAPINFO      Info[2];
    00439         HWND            hWnd;
    00440         HDC                     hDC;
    00441         ILubyte         *Buffer1 = NULL, *Buffer2 = NULL;
    00442         ILuint          i, j, PadSize, Bps;
    00443
    00444         ilutCurImage = ilGetCurImage();
    00445         if (ilutCurImage == NULL) {
    00446                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00447                 return IL_FALSE;
    00448         }
    00449
    00450         hWnd = GetForegroundWindow();
    00451         hDC = GetDC(hWnd);
    00452
    00453         // Query the dimensions
    00454         memset(&Info, 0, sizeof(BITMAPINFO));
    00455         Info[0].bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    00456         GetDIBits(hDC, Bitmap, 0, 0, NULL, Info, DIB_RGB_COLORS);
    00457
    00458         // @TODO:  Implement this shitz0rz!
    00459         if (Info[0].bmiHeader.biBitCount < 24) {
    00460                 ReleaseDC(hWnd, hDC); //added 20040527
    00461                 return IL_FALSE;
    00462         }
    00463
    00464         Buffer1 = (ILubyte*)ialloc(Info[0].bmiHeader.biSizeImage);
    00465         Buffer2 = (ILubyte*)ialloc(Info[0].bmiHeader.biSizeImage);
    00466         if (Buffer1 == NULL || Buffer2 == NULL) {
    00467                 ReleaseDC(hWnd, hDC); //added 20040527
    00468                 ifree(Buffer1);
    00469                 ifree(Buffer2);
    00470                 return IL_FALSE;
    00471         }
    00472
    00473         //GetBitmapBits
    00474         GetDIBits(hDC, Bitmap, 0, Info[0].bmiHeader.biHeight, Buffer1, Info, DIB_RGB_COLORS);
    00475
    00476         Bps = Info[0].bmiHeader.biWidth * (Info[0].bmiHeader.biBitCount >> 3);
    00477         PadSize = (4 - (Bps % 4)) % 4;
    00478
    00479         // Remove the padding.
    00480         for (i = 0, j = 0; i < Info[0].bmiHeader.biSizeImage; i += Bps + PadSize, j += Bps) {
    00481                 memcpy(Buffer2 + j, Buffer1 + i, Bps);
    00482         }
    00483
    00484         if (Info[0].bmiHeader.biBitCount == 24) {
    00485                 ilTexImage(Info[0].bmiHeader.biWidth, Info[0].bmiHeader.biHeight, 1,
    00486                         (ILubyte)(Info[0].bmiHeader.biBitCount >> 3), IL_BGR, IL_UNSIGNED_BYTE, Buffer2);
    00487         }
    00488         else if (Info[0].bmiHeader.biBitCount == 32) {
    00489                 ilTexImage(Info[0].bmiHeader.biWidth, Info[0].bmiHeader.biHeight, 1,
    00490                         (ILubyte)(Info[0].bmiHeader.biBitCount >> 3), IL_BGRA, IL_UNSIGNED_BYTE, Buffer2);
    00491         }
    00492         ilutCurImage->Origin = IL_ORIGIN_LOWER_LEFT;
    00493
    00494         ReleaseDC(hWnd, hDC); //added 20040527
    00495         ifree(Buffer1);
    00496         ifree(Buffer2);
    00497
    00498 #endif//_WIN32_WCE
    00499
    00500         return IL_TRUE;
    00501 }
    00502
    00503
    00504 ILboolean ILAPIENTRY ilutSetHPal(HPALETTE Pal)
    00505 {
    00506         LPPALETTEENTRY  PalEntries;
    00507         ILuint                  NumEntries, i;
    00508         ILubyte                 *TempPal;
    00509
    00510         ilutCurImage = ilGetCurImage();
    00511         if (ilutCurImage == NULL) {
    00512                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00513                 return IL_FALSE;
    00514         }
    00515
    00516         NumEntries = GetPaletteEntries(Pal, 0, 0, NULL);
    00517         if (NumEntries == 0)
    00518                 return IL_TRUE;  // @TODO:  Determine if correct...
    00519
    00520         PalEntries = (LPPALETTEENTRY)ialloc(NumEntries * sizeof(PALETTEENTRY));
    00521         if (PalEntries == NULL) {
    00522                 return IL_FALSE;
    00523         }
    00524
    00525         NumEntries = GetPaletteEntries(Pal, 0, NumEntries, PalEntries);
    00526
    00527         TempPal = (ILubyte*)ialloc(NumEntries * 3);
    00528         if (TempPal == NULL) {
    00529                 ifree(PalEntries);
    00530                 return IL_FALSE;
    00531         }
    00532         if (ilutCurImage->Pal.Palette)
    00533                 ifree(ilutCurImage->Pal.Palette);
    00534         ilutCurImage->Pal.Palette = TempPal;
    00535         ilutCurImage->Pal.PalSize = NumEntries * 3;
    00536         ilutCurImage->Pal.PalType = IL_PAL_RGB24;
    00537
    00538         for (i = 0; i < NumEntries; i++) {
    00539                 *TempPal++ = PalEntries[i].peRed;
    00540                 *TempPal++ = PalEntries[i].peGreen;
    00541                 *TempPal++ = PalEntries[i].peBlue;
    00542         }
    00543
    00544         ifree(PalEntries);
    00545
    00546         return IL_TRUE;
    00547 }
    00548
    00549
    00550 ILboolean ILAPIENTRY ilutSetWinClipboard()
    00551 {
    00552         HBITMAP Bitmap;
    00553         HANDLE  Handle;
    00554         HWND    hWnd;
    00555         HDC             hDC;
    00556         ILimage *TempImage, *CurImage;
    00557
    00558         ilutCurImage = ilGetCurImage();
    00559         if (ilutCurImage == NULL) {
    00560                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00561                 return IL_FALSE;
    00562         }
    00563
    00564         if (ilutCurImage->Format != IL_BGR || ilutCurImage->Bps > 1) {
    00565                 TempImage = iConvertImage(ilutCurImage, IL_BGR, IL_UNSIGNED_BYTE);
    00566                 if (TempImage == NULL)
    00567                         return IL_FALSE;
    00568         }
    00569         else
    00570                 TempImage = ilutCurImage;
    00571
    00572         CurImage = ilutCurImage;
    00573         ilSetCurImage(TempImage);
    00574
    00575         hWnd = GetForegroundWindow();
    00576         hDC = GetDC(hWnd);
    00577
    00578         if (!OpenClipboard(NULL)) {
    00579                 if (TempImage != ilutCurImage)
    00580                         ilCloseImage(TempImage);
    00581                 ilSetCurImage(CurImage);
    00582                 ilSetError(ILUT_ILLEGAL_OPERATION);  // Dunno if this is the correct error.
    00583                 ReleaseDC(hWnd, hDC); //added 20040604
    00584                 if (TempImage != ilutCurImage)
    00585                         ilCloseImage(TempImage);
    00586                 ilSetCurImage(CurImage);
    00587                 return IL_FALSE;
    00588         }
    00589
    00590         //note that this is not the best method to put an image into the
    00591         //clipboard, CF_DIB is much better because HBITMAPS are device-dependent.
    00592         //TODO: eventually change that if there is a need
    00593         Bitmap = ilutConvertToHBitmap(hDC);
    00594         ReleaseDC(hWnd, hDC); //added 20040604
    00595
    00596         EmptyClipboard();
    00597         Handle = SetClipboardData(CF_BITMAP, Bitmap);
    00598
    00599         CloseClipboard();
    00600
    00601         //DeleteObject(Bitmap);  // Needed? No! Clipboard takes care of image.
    00602
    00603         if (TempImage != ilutCurImage)
    00604                 ilCloseImage(TempImage);
    00605         ilSetCurImage(CurImage);
    00606
    00607         return IL_TRUE;
    00608 }
    00609
    00610
    00611 ILboolean ILAPIENTRY ilutGetWinClipboard()
    00612 {
    00613         //HBITMAP               Bitmap;
    00614         HWND            hWnd;
    00615         HGLOBAL         hGlobal;
    00616         PTSTR           pGlobal, data;
    00617         BITMAPFILEHEADER        *BmpHeader;
    00618         BITMAPINFOHEADER        *InfoHeader;
    00619         SIZE_T          Size;
    00620
    00621         ilutCurImage = ilGetCurImage();
    00622         if (ilutCurImage == NULL) {
    00623                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00624                 return IL_FALSE;
    00625         }
    00626
    00627         if (IsClipboardFormatAvailable(CF_DIB)) {
    00628                 hWnd = GetForegroundWindow();
    00629
    00630                 if (!OpenClipboard(hWnd)) {
    00631                         ilSetError(ILUT_ILLEGAL_OPERATION);  // Dunno if this is the correct error.
    00632                         return IL_FALSE;
    00633                 }
    00634
    00635                 hGlobal = GetClipboardData(CF_DIB);
    00636                 if (!hGlobal) {
    00637                         CloseClipboard();
    00638                         return IL_FALSE;  // No error?
    00639                 }
    00640
    00641                 //copy DIB to buffer because windows delivers it without the
    00642                 //BITMAPFILEHEADER that DevIL needs to load the image
    00643                 Size = GlobalSize(hGlobal);
    00644                 //@TODO: Size should never be larger than an ILuint?
    00645                 data = (PTSTR)ialloc((ILuint)Size + sizeof(BITMAPFILEHEADER));
    00646                 pGlobal = (PTSTR)GlobalLock(hGlobal);
    00647                 if (!pGlobal || !data) {
    00648                         ifree(data);
    00649                         CloseClipboard();
    00650                         return IL_FALSE;  // No error?
    00651                 }
    00652                 memcpy(data + sizeof(BITMAPFILEHEADER), pGlobal, Size);
    00653                 GlobalUnlock(hGlobal);
    00654                 CloseClipboard();
    00655
    00656                 //create BITMAPFILEHEADER
    00657                 InfoHeader = (BITMAPINFOHEADER*)(data + sizeof(BITMAPFILEHEADER));
    00658                 BmpHeader = (BITMAPFILEHEADER*)data;
    00659                 BmpHeader->bfType = 'B' | ('M' << 8);
    00660                 //@TODO: Again, could it ever be larger than an unsigned integer (DWORD)?
    00661                 BmpHeader->bfSize = (DWORD)Size + sizeof(BITMAPFILEHEADER);
    00662                 BmpHeader->bfReserved1 = BmpHeader->bfReserved2 = 0;
    00663                 BmpHeader->bfOffBits = sizeof(BITMAPFILEHEADER) + InfoHeader->biSize + InfoHeader->biClrUsed*4;
    00664                 if (InfoHeader->biCompression == BI_BITFIELDS)
    00665                         BmpHeader->bfOffBits += 12;
    00666
    00667                 return ilLoadL(IL_BMP, data, BmpHeader->bfSize);
    00668         }
    00669         /*
    00670         //this is not required becaus CF_BITMAP is converted to CF_DIB automatically
    00671         //when needed. CF_DIB suffices.
    00672         else if (IsClipboardFormatAvailable(CF_BITMAP)) {
    00673                 hWnd = GetForegroundWindow();
    00674
    00675                 if (!OpenClipboard(hWnd)) {
    00676                         ilSetError(ILUT_ILLEGAL_OPERATION);  // Dunno if this is the correct error.
    00677                         return IL_FALSE;
    00678                 }
    00679
    00680                 Bitmap = (HBITMAP)GetClipboardData(CF_BITMAP);
    00681                 if (!Bitmap) {
    00682                         CloseClipboard();
    00683                         return IL_FALSE;  // No error?
    00684                 }
    00685
    00686                 if (!ilutSetHBitmap(Bitmap)) {
    00687                         CloseClipboard();
    00688                         return IL_FALSE;
    00689                 }
    00690
    00691                 CloseClipboard();
    00692         }*/
    00693
    00694         //no data in clipboard
    00695         ilSetError(ILUT_ILLEGAL_OPERATION);
    00696         return IL_FALSE;
    00697 }
    00698
    00699
    00700 ILboolean ILAPIENTRY ilutWinPrint(ILuint XPos, ILuint YPos, ILuint Width, ILuint Height, HDC hDC)
    00701 {
    00702 #if !defined(_WIN32_WCE) && !(defined(_WIN32) && defined(__GNUC__))
    00703         PRINTDLG        Pd;
    00704         DOCINFO         Di;
    00705         HBITMAP         Bitmap, hReplaced;
    00706         HDC                     hMemDC;
    00707
    00708         ilutCurImage = ilGetCurImage();
    00709         if (ilutCurImage == NULL) {
    00710                 ilSetError(ILUT_ILLEGAL_OPERATION);
    00711                 return IL_FALSE;
    00712         }
    00713
    00714         //@TODO: Needs error checking!
    00715         hMemDC = CreateCompatibleDC(hDC);
    00716         Bitmap = ilutConvertToHBitmap(hDC);
    00717         hReplaced = (HBITMAP)SelectObject(hMemDC, Bitmap);
    00718
    00719         memset(&Pd, 0, sizeof(PRINTDLG));
    00720         Pd.lStructSize = sizeof(PRINTDLG);
    00721         Pd.hwndOwner = GetForegroundWindow();
    00722         Pd.Flags = PD_RETURNDC;
    00723         Pd.nCopies = 1;
    00724         Pd.nFromPage = 0xFFFF;
    00725         Pd.nToPage = 0xFFFF;
    00726         Pd.nMinPage = 1;
    00727         Pd.nMaxPage = 0xFFFF;
    00728
    00729         if (!PrintDlg(&Pd))
    00730                 return (0L);
    00731
    00732         Di.cbSize = sizeof(DOCINFO);
    00733         Di.lpszDocName = IL_TEXT("DevIL Print Job");
    00734         Di.lpszOutput = NULL;
    00735         Di.lpszDatatype = NULL;
    00736         Di.fwType = 0;
    00737
    00738         StartDoc(Pd.hDC, &Di);
    00739         StartPage(Pd.hDC);
    00740
    00741         StretchBlt(Pd.hDC, XPos, YPos, Width, Height, hMemDC, 0, 0, ilutCurImage->Width, ilutCurImage->Height, SRCCOPY);
    00742
    00743         EndPage(Pd.hDC);
    00744         EndDoc(Pd.hDC);
    00745         DeleteObject(Bitmap);
    00746         DeleteObject(hReplaced);
    00747         DeleteDC(Pd.hDC);
    00748
    00749 #endif
    00750
    00751         return IL_TRUE;
    00752 }
    00753
    00754
    00755 ILboolean ILAPIENTRY ilutLoadResource(HINSTANCE hInst, ILint ID, ILstring ResourceType, ILenum Type)
    00756 {
    00757         HRSRC Resource = (HRSRC)LoadResource(hInst, FindResource(hInst, MAKEINTRESOURCE(ID), ResourceType));
    00758         ILubyte *Data = (ILubyte*)LockResource(Resource);
    00759
    00760         return ilLoadL(Type, Data, SizeofResource(hInst, FindResource(hInst, MAKEINTRESOURCE(ID), ResourceType)));
    00761 }
    00762
    00763
    00764 #if !defined(_WIN32_WCE) && !(defined(_WIN32) && defined(__GNUC__))
    00765 #define BUFFSIZE 8192  // Change to suit the efficiency.
    00766 ILboolean ILAPIENTRY ilutWinLoadUrl(ILstring Url)
    00767 {
    00768         HINTERNET       Handle, UrlHandle;
    00769         DWORD           BytesRead = 0, Context = 1;
    00770         ILubyte         Buff[BUFFSIZE], *Buffer, *TempBuff;
    00771         ILuint          BufferSize = 0, i;
    00772         ILboolean       Is404 = IL_TRUE;
    00773         char            Buffer404[] = { '<', 'h', 't', 'm', 'l', '>' };
    00774
    00775         Buffer = (ILubyte*)ialloc(0);
    00776         if (Buffer == NULL) {
    00777                 return IL_FALSE;
    00778         }
    00779
    00780         Handle = InternetOpen(IL_TEXT("Developer's Image Library"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    00781         if (Handle == NULL) {
    00782                 ifree(Buffer);
    00783                 ilSetError(ILUT_COULD_NOT_OPEN_FILE);
    00784                 return IL_FALSE;
    00785         }
    00786
    00787         // Try again if fails the first time, loading only from the cache.
    00788         UrlHandle = InternetOpenUrl(Handle, Url, NULL, 0, 0, Context);
    00789         if (UrlHandle == NULL) {
    00790                 InternetCloseHandle(Handle);
    00791                 Handle = InternetOpen(IL_TEXT("Developer's Image Library"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_FROM_CACHE);
    00792                 if (Handle == NULL) {
    00793                         ifree(Buffer);
    00794                         ilSetError(ILUT_COULD_NOT_OPEN_FILE);
    00795                         return IL_FALSE;
    00796                 }
    00797                 UrlHandle = InternetOpenUrl(Handle, Url, NULL, 0, 0, Context);
    00798                 if (UrlHandle == NULL) {
    00799                         ifree(Buffer);
    00800                         InternetCloseHandle(Handle);
    00801                         ilSetError(ILUT_COULD_NOT_OPEN_FILE);
    00802                         return IL_FALSE;
    00803                 }
    00804         }
    00805
    00806         do {
    00807                 if (!InternetReadFile(UrlHandle, Buff, BUFFSIZE, &BytesRead)) {
    00808                         InternetCloseHandle(UrlHandle);
    00809                         InternetCloseHandle(Handle);
    00810                         ifree(Buffer);
    00811                         ilSetError(ILUT_COULD_NOT_OPEN_FILE);
    00812                         return IL_FALSE;
    00813                 }
    00814
    00815                 TempBuff = (ILubyte*)ialloc(BufferSize + BytesRead);
    00816                 if (TempBuff == NULL) {
    00817                         ifree(Buffer);
    00818                         return IL_FALSE;
    00819                 }
    00820
    00821                 memcpy(TempBuff, Buffer, BufferSize);
    00822                 memcpy(TempBuff + BufferSize, Buff, BytesRead);
    00823                 ifree(Buffer);
    00824                 Buffer = TempBuff;
    00825
    00826                 BufferSize += BytesRead;
    00827         } while (BytesRead > 0);
    00828
    00829         InternetCloseHandle(UrlHandle);
    00830         InternetCloseHandle(Handle);
    00831
    00832         // If the image does not exist, the server usually returns a 404 HTML page.
    00833         for (i = 0; i < sizeof(Buffer404) && i < BufferSize; i++) {
    00834                 if (tolower(Buffer[i]) != Buffer404[i]) {
    00835                         Is404 = IL_FALSE;
    00836                         break;
    00837                 }
    00838         }
    00839
    00840         if (!Is404) {
    00841                 if (!ilLoadL(ilTypeFromExt(Url), Buffer, BufferSize)) {
    00842                         if (!ilLoadL(IL_TYPE_UNKNOWN, Buffer, BufferSize)) {
    00843                                 ifree(Buffer);
    00844                                 return IL_FALSE;
    00845                         }
    00846                 }
    00847         }
    00848
    00849         ifree(Buffer);
    00850
    00851         return IL_TRUE;
    00852 }
    00853 #endif
    00854
    00855
    00856 #endif//ILUT_USE_WIN32