~ubuntu-branches/ubuntu/hardy/devil/hardy-updates

« back to all changes in this revision

Viewing changes to src-ILUT/src/ilut_win32.c

  • Committer: Bazaar Package Importer
  • Author(s): Marcelo E. Magallon
  • Date: 2005-01-03 19:57:42 UTC
  • Revision ID: james.westby@ubuntu.com-20050103195742-4ipkplcwygu3irv0
Tags: upstream-1.6.7
ImportĀ upstreamĀ versionĀ 1.6.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//-----------------------------------------------------------------------------
 
2
//
 
3
// ImageLib Utility Toolkit Sources
 
4
// Copyright (C) 2000-2002 by Denton Woods
 
5
// Last modified: 05/28/2001 <--Y2K Compliant! =]
 
6
//
 
7
// Filename: src-ILUT/src/ilut_win32.c
 
8
//
 
9
// Description: Windows functions for images
 
10
//
 
11
//-----------------------------------------------------------------------------
 
12
 
 
13
 
 
14
#include "ilut_internal.h"
 
15
#ifdef ILUT_USE_WIN32
 
16
#include <wininet.h>
 
17
#include <windows.h>
 
18
 
 
19
// For ilutWinLoadUrl().
 
20
//pragma comment(lib, "wininet.lib")
 
21
 
 
22
#if !_WIN32_WCE && (_WIN32 && __GNUC__)
 
23
        PRINTDLG        Pd;
 
24
#endif//_WIN32_WCE
 
25
 
 
26
ILboolean ilutWin32Init()
 
27
{
 
28
 
 
29
 
 
30
        return IL_TRUE;
 
31
}
 
32
 
 
33
 
 
34
ILAPI HBITMAP   ILAPIENTRY ilutConvertSliceToHBitmap(HDC hDC, ILuint slice)
 
35
{
 
36
        ILubyte         *Data, *DataBackup;
 
37
        HBITMAP         hBitmap;
 
38
        ILimage         *TempImage;
 
39
        ILuint          pad, i, j, k, l, m, n, DepthBackup;
 
40
        ILpal           *palImg;
 
41
        ILboolean       alloc_buffer;
 
42
 
 
43
        //reserve space for palette in every case...
 
44
        ILubyte         *buff[sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD)];
 
45
        BITMAPINFO      *info = (BITMAPINFO*)buff;
 
46
        RGBQUAD         *pal = info->bmiColors;
 
47
 
 
48
        ilutCurImage = ilutCurImage = ilGetCurImage();
 
49
        if (ilutCurImage == NULL) {
 
50
                ilSetError(ILUT_ILLEGAL_OPERATION);
 
51
                return NULL;
 
52
        }
 
53
 
 
54
        //check if the image has the wanted slice
 
55
        if (slice < 0 || slice >= ilutCurImage->Depth) {
 
56
                ilSetError(ILUT_INVALID_PARAM);
 
57
                return NULL;
 
58
        }
 
59
 
 
60
        //fool iConvertImage into thinking that the current image has
 
61
        //only one slice, the one we want:
 
62
        DepthBackup = ilutCurImage->Depth;
 
63
        DataBackup = ilutCurImage->Data;
 
64
        ilutCurImage->Depth = 1;
 
65
        ilutCurImage->Data += ilutCurImage->SizeOfPlane*slice;
 
66
 
 
67
        if (ilutCurImage->Type != IL_UNSIGNED_BYTE)
 
68
                TempImage = iConvertImage(ilutCurImage, ilutCurImage->Format, IL_UNSIGNED_BYTE);
 
69
        else
 
70
                TempImage = ilutCurImage;
 
71
        if (TempImage == NULL)
 
72
                return NULL;
 
73
 
 
74
        //changed 2003-09-09: use Temp!
 
75
        ilSetCurImage(TempImage);
 
76
 
 
77
        hBitmap = CreateCompatibleBitmap(hDC, ilutCurImage->Width, ilutCurImage->Height);
 
78
        if(hBitmap == NULL) {
 
79
                ilSetError(IL_UNKNOWN_ERROR);
 
80
                return NULL;
 
81
        }
 
82
 
 
83
        info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
 
84
        info->bmiHeader.biWidth = TempImage->Width;
 
85
        if(TempImage->Origin == IL_ORIGIN_UPPER_LEFT)
 
86
                info->bmiHeader.biHeight = -(ILint)TempImage->Height;
 
87
        else
 
88
                info->bmiHeader.biHeight = TempImage->Height;
 
89
        info->bmiHeader.biPlanes = 1;
 
90
        info->bmiHeader.biCompression = 0;
 
91
        info->bmiHeader.biSizeImage = 0;
 
92
        info->bmiHeader.biXPelsPerMeter = 0;
 
93
        info->bmiHeader.biYPelsPerMeter = 0;
 
94
        info->bmiHeader.biClrUsed = 0;
 
95
        info->bmiHeader.biClrImportant = 0;
 
96
 
 
97
        pad = (4 - TempImage->Bps%4)%4;
 
98
        alloc_buffer = !(pad == 0 && TempImage->Format != IL_RGB
 
99
                && TempImage->Format != IL_RGBA && TempImage->Format != IL_LUMINANCE_ALPHA);
 
100
        if(!alloc_buffer) {
 
101
                Data = TempImage->Data;
 
102
        }
 
103
        else {
 
104
                if (TempImage->Format == IL_RGBA) {//strip alpha during byte swapping for faster upload to gdi
 
105
                        //recalculate pad because it changes when bpp change
 
106
                        pad = (4 - (3*TempImage->Width)%4)%4;
 
107
                        Data = ialloc((TempImage->Width + pad)*TempImage->Height*3);
 
108
                }
 
109
                //strip alpha channel from grayscale image
 
110
                else if(TempImage->Format == IL_LUMINANCE_ALPHA)
 
111
                        Data = ialloc((TempImage->Width + pad)*TempImage->Height);
 
112
                else
 
113
                        Data = ialloc((TempImage->Width + pad)*TempImage->Height*TempImage->Bpp);
 
114
 
 
115
                if (Data == NULL) {
 
116
                        ilSetCurImage(ilutCurImage);
 
117
                        ilCloseImage(TempImage);
 
118
                        ilSetError(IL_OUT_OF_MEMORY);
 
119
                        return hBitmap;
 
120
                }
 
121
 
 
122
                if (TempImage->Format == IL_RGB || TempImage->Format == IL_RGBA) {
 
123
                        //swap bytes
 
124
                        m = (TempImage->Format == IL_RGB)?3:4;
 
125
                        k = l = 0;
 
126
                        for (j = 0; j < TempImage->Height; j++) {
 
127
                                for (i = 0, n = 0; i < 3*TempImage->Width; i += 3, n += m) {
 
128
                                        Data[l + i] = TempImage->Data[k + n + 2];
 
129
                                        Data[l + i + 1] = TempImage->Data[k + n + 1];
 
130
                                        Data[l + i + 2] = TempImage->Data[k + n];
 
131
                                }
 
132
 
 
133
                                k += TempImage->Bps;
 
134
                                l += 3*TempImage->Width + pad;
 
135
                        }
 
136
                }
 
137
                else if(TempImage->Format == IL_LUMINANCE_ALPHA) {
 
138
                        //strip alpha channel
 
139
                        k = l = 0;
 
140
                        for (j = 0; j < TempImage->Height; j++) {
 
141
                                for (i = 0, n = 0; i < TempImage->Width; ++i, n += 2) {
 
142
                                        Data[l + i] = TempImage->Data[k + n];
 
143
                                }
 
144
                                k += TempImage->Bps;
 
145
                                l += TempImage->Width + pad;
 
146
                        }
 
147
                }
 
148
                else
 
149
                        for (i = 0; i < TempImage->Height; i++)
 
150
                                memcpy(Data + i*(TempImage->Bps + pad), TempImage->Data + i*TempImage->Bps, TempImage->Bps);
 
151
        }
 
152
 
 
153
        switch(TempImage->Format) {
 
154
 
 
155
                case IL_LUMINANCE:
 
156
                case IL_LUMINANCE_ALPHA:
 
157
                case IL_COLOUR_INDEX:
 
158
                        if (TempImage->Format != IL_COLOUR_INDEX) {
 
159
                                //generate greyscale palette
 
160
                                for (i = 0; i < 256; i++)
 
161
                                        pal[i].rgbRed = pal[i].rgbGreen = pal[i].rgbBlue = i;
 
162
                        }
 
163
                        else {
 
164
                                palImg = iConvertPal(&TempImage->Pal, IL_PAL_BGR32);
 
165
                                if (palImg != NULL) {
 
166
                                        memcpy(pal, palImg->Palette, palImg->PalSize);
 
167
                                        ilClosePal(palImg);
 
168
                                }
 
169
                                else {
 
170
                                        ilSetError(IL_INVALID_PARAM);
 
171
                                        //generate greyscale palette
 
172
                                        for (i = 0; i < 256; i++)
 
173
                                                pal[i].rgbRed = pal[i].rgbGreen = pal[i].rgbBlue = i;
 
174
                                }
 
175
                        }
 
176
                        info->bmiHeader.biBitCount = 8;
 
177
                        break;
 
178
 
 
179
                case IL_RGB:
 
180
                case IL_BGR:
 
181
                case IL_RGBA: //alpha is removed during byte swapping
 
182
                        info->bmiHeader.biBitCount = 24;
 
183
                        break;
 
184
 
 
185
                case IL_BGRA:
 
186
                        info->bmiHeader.biBitCount = 32;
 
187
                        break;
 
188
 
 
189
                /*default:
 
190
                        ilSetError(IL_FORMAT_NOT_SUPPORTED);
 
191
                        return hBitmap;*/
 
192
        }
 
193
 
 
194
        if (ilutCurImage != TempImage) {
 
195
                ilSetCurImage(ilutCurImage);
 
196
                ilCloseImage(TempImage);
 
197
        }
 
198
 
 
199
        //restore original data
 
200
        ilutCurImage->Data = DataBackup;
 
201
        ilutCurImage->Depth = DepthBackup;
 
202
 
 
203
        SetDIBits(hDC, hBitmap, 0, ilutCurImage->Height, Data, info, DIB_RGB_COLORS);
 
204
 
 
205
        if(alloc_buffer)
 
206
                ifree(Data);
 
207
 
 
208
        return hBitmap;
 
209
}
 
210
 
 
211
HBITMAP ILAPIENTRY ilutConvertToHBitmap(HDC hDC)
 
212
{
 
213
        return ilutConvertSliceToHBitmap(hDC, 0);
 
214
}
 
215
 
 
216
ILubyte* ILAPIENTRY iGetPaddedData(ILimage *Image)
 
217
{
 
218
        ILubyte *NewData = NULL, *TempBuff = NULL;
 
219
        ILuint  i, CurPos = 0, PadSize;
 
220
        ILubyte *TempData = NULL;
 
221
 
 
222
        if (Image == NULL) {
 
223
                ilSetError(ILUT_INVALID_PARAM);
 
224
                return NULL;
 
225
        }
 
226
 
 
227
        if (Image->Origin != IL_ORIGIN_LOWER_LEFT) {
 
228
                TempData = iGetFlipped(Image);
 
229
        }
 
230
        else {
 
231
                TempData = Image->Data;
 
232
        }
 
233
 
 
234
        if (Image->Format == IL_RGB || Image->Format == IL_RGBA) {
 
235
                TempBuff = (ILubyte*)ialloc(Image->SizeOfData);
 
236
                if (TempBuff == NULL) {
 
237
                        return NULL;
 
238
                }
 
239
                // Swap red and blue.
 
240
                for (i = 0; i < Image->SizeOfData; i += Image->Bpp) {
 
241
                        TempBuff[i] = TempData[i+2];
 
242
                        TempBuff[i+1] = TempData[i+1];
 
243
                        TempBuff[i+2] = TempData[i];
 
244
                }
 
245
        }
 
246
        else {
 
247
                TempBuff = TempData;
 
248
        }
 
249
 
 
250
        PadSize = (4 - (Image->Bps % 4)) % 4;
 
251
        NewData = (ILubyte*)ialloc((Image->Width + PadSize) * Image->Height * Image->Bpp);
 
252
        if (NewData == NULL) {
 
253
                return NULL;
 
254
        }
 
255
 
 
256
        for (i = 0; i < Image->Height; i++) {
 
257
                memcpy(NewData + CurPos, TempBuff + Image->Bps * i, Image->Bps);
 
258
                CurPos += Image->Bps;
 
259
                memset(NewData + CurPos, 0, PadSize);           
 
260
                CurPos += PadSize;
 
261
        }
 
262
 
 
263
        if (TempData != TempBuff && TempData != Image->Data)
 
264
                ifree(TempData);
 
265
        if (TempBuff != Image->Data)
 
266
                ifree(TempBuff);
 
267
 
 
268
        return NewData;
 
269
}
 
270
 
 
271
 
 
272
ILvoid ILAPIENTRY ilutFreePaddedData(ILubyte *Data)
 
273
{
 
274
        ifree(Data);
 
275
        return;
 
276
}
 
277
 
 
278
 
 
279
// DirectX/GDI insists that all scanlines end on a dword boundary. =(
 
280
ILubyte* ILAPIENTRY ilutGetPaddedData()
 
281
{
 
282
        return iGetPaddedData(ilGetCurImage());
 
283
}
 
284
 
 
285
 
 
286
// @TODO:  Figure how to mess with multiple bpc's!
 
287
ILvoid ILAPIENTRY ilutGetBmpInfo(BITMAPINFO *Info)
 
288
{
 
289
        ILuint NewBps, Padding;
 
290
 
 
291
        ilutCurImage = ilGetCurImage();
 
292
        if (ilutCurImage == NULL) {
 
293
                ilSetError(ILUT_ILLEGAL_OPERATION);
 
294
                return;
 
295
        }
 
296
 
 
297
        Padding = (4 - (ilutCurImage->Bps % 4)) % 4;
 
298
        NewBps = ilutCurImage->Bps/* + Padding*/;
 
299
 
 
300
        Info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
 
301
        Info->bmiHeader.biWidth = ilutCurImage->Width;
 
302
        Info->bmiHeader.biHeight = ilutCurImage->Height;
 
303
        Info->bmiHeader.biPlanes = 1;
 
304
        Info->bmiHeader.biBitCount = ilutCurImage->Bpp << 3;
 
305
        Info->bmiHeader.biCompression = BI_RGB;
 
306
        Info->bmiHeader.biSizeImage = NewBps * ilutCurImage->Height;
 
307
        Info->bmiHeader.biXPelsPerMeter = 0;
 
308
        Info->bmiHeader.biYPelsPerMeter = 0;
 
309
        Info->bmiHeader.biClrUsed = ilutCurImage->Bpp == 1 ? 255 : 0;
 
310
        if (Info->bmiHeader.biClrUsed < 24)
 
311
                Info->bmiHeader.biClrImportant = Info->bmiHeader.biClrUsed;
 
312
        else
 
313
                Info->bmiHeader.biClrImportant = 0;
 
314
 
 
315
        return;
 
316
}
 
317
 
 
318
 
 
319
//! Just a convenience function.
 
320
HBITMAP ILAPIENTRY ilutWinLoadImage(const ILstring FileName, HDC hDC)
 
321
{
 
322
        HBITMAP Bitmap;
 
323
 
 
324
        iBindImageTemp();
 
325
        if (!ilLoadImage(FileName))
 
326
                return 0;
 
327
 
 
328
        Bitmap = ilutConvertToHBitmap(hDC);
 
329
 
 
330
        return Bitmap;
 
331
}
 
332
 
 
333
 
 
334
#ifndef _WIN32_WCE
 
335
ILboolean ILAPIENTRY ilutWinSaveImage(const ILstring FileName, HBITMAP Bitmap)
 
336
{
 
337
        ILuint          CurName;
 
338
        ILboolean       Saved;
 
339
        
 
340
        CurName = ilGetCurName();
 
341
 
 
342
        iBindImageTemp();
 
343
 
 
344
        if (!ilutSetHBitmap(Bitmap)) {
 
345
                ilBindImage(CurName);
 
346
                return IL_FALSE;
 
347
        }
 
348
 
 
349
        Saved = ilSaveImage(FileName);
 
350
        ilBindImage(CurName);
 
351
 
 
352
        return Saved;
 
353
}
 
354
#endif//_WIN32_WCE
 
355
 
 
356
 
 
357
// @TODO:  Just create a copy of the palette!
 
358
// Credit for this goes to the OpenGL SuperBible.
 
359
HPALETTE ILAPIENTRY ilutGetHPal()
 
360
{
 
361
        HPALETTE        Palette;
 
362
        LOGPALETTE      *LogPal;
 
363
        ILuint          NumEntries, i;
 
364
        ILenum          CurPalType;
 
365
 
 
366
        ilutCurImage = ilGetCurImage();
 
367
        if (ilutCurImage == NULL) {
 
368
                ilSetError(ILUT_ILLEGAL_OPERATION);
 
369
                return NULL;
 
370
        }
 
371
 
 
372
        if (!ilutCurImage->Pal.Palette || !ilutCurImage->Pal.PalSize || ilutCurImage->Pal.PalType == IL_PAL_NONE) {
 
373
                //ilSetError(ILUT_ILLEGAL_OPERATION);
 
374
                return NULL;
 
375
        }
 
376
 
 
377
        CurPalType = ilutCurImage->Pal.PalType;
 
378
        if (!ilConvertPal(IL_PAL_RGB24)) {
 
379
                return NULL;  // ilConvertPal already sets the error
 
380
        }
 
381
        NumEntries = ilutCurImage->Pal.PalSize / 3;
 
382
 
 
383
        LogPal = (LOGPALETTE*)ialloc(sizeof(LOGPALETTE) + NumEntries * sizeof(PALETTEENTRY));
 
384
        if (!LogPal) {
 
385
                return NULL;
 
386
        }
 
387
 
 
388
        LogPal->palVersion = 0x300;
 
389
        LogPal->palNumEntries = NumEntries;
 
390
 
 
391
        for (i = 0; i < NumEntries; i++) {
 
392
                LogPal->palPalEntry[i].peRed   = ilutCurImage->Pal.Palette[i * 3];
 
393
                LogPal->palPalEntry[i].peGreen = ilutCurImage->Pal.Palette[i * 3 + 1];
 
394
                LogPal->palPalEntry[i].peBlue  = ilutCurImage->Pal.Palette[i * 3 + 2];
 
395
                LogPal->palPalEntry[i].peFlags = 0;
 
396
        }
 
397
 
 
398
        Palette = CreatePalette(LogPal);
 
399
        ifree(LogPal);
 
400
 
 
401
        ilConvertPal(CurPalType);  // Should we check the return value?
 
402
 
 
403
        return Palette;
 
404
}
 
405
 
 
406
 
 
407
ILboolean ILAPIENTRY ilutSetHBitmap(HBITMAP Bitmap)
 
408
{
 
409
#ifndef _WIN32_WCE
 
410
        BITMAPINFO      Info[2];
 
411
        HWND            hWnd;
 
412
        HDC                     hDC;
 
413
        ILubyte         *Buffer1 = NULL, *Buffer2 = NULL;
 
414
        ILuint          i, j, PadSize, Bps;
 
415
 
 
416
        ilutCurImage = ilGetCurImage();
 
417
        if (ilutCurImage == NULL) {
 
418
                ilSetError(ILUT_ILLEGAL_OPERATION);
 
419
                return IL_FALSE;
 
420
        }
 
421
 
 
422
        hWnd = GetForegroundWindow();
 
423
        hDC = GetDC(hWnd);
 
424
 
 
425
        // Query the dimensions
 
426
        memset(&Info, 0, sizeof(BITMAPINFO));
 
427
        Info[0].bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
 
428
        GetDIBits(hDC, Bitmap, 0, 0, NULL, Info, DIB_RGB_COLORS);
 
429
 
 
430
        // @TODO:  Implement this shitz0rz!
 
431
        if (Info[0].bmiHeader.biBitCount < 24) {
 
432
                ReleaseDC(hWnd, hDC); //added 20040527
 
433
                return IL_FALSE;
 
434
        }
 
435
 
 
436
        Buffer1 = (ILubyte*)ialloc(Info[0].bmiHeader.biSizeImage);
 
437
        Buffer2 = (ILubyte*)ialloc(Info[0].bmiHeader.biSizeImage);
 
438
        if (Buffer1 == NULL || Buffer2 == NULL) {
 
439
                ReleaseDC(hWnd, hDC); //added 20040527
 
440
                ifree(Buffer1);
 
441
                ifree(Buffer2);
 
442
                return IL_FALSE;
 
443
        }
 
444
 
 
445
        //GetBitmapBits
 
446
        GetDIBits(hDC, Bitmap, 0, Info[0].bmiHeader.biHeight, Buffer1, Info, DIB_RGB_COLORS);
 
447
 
 
448
        Bps = Info[0].bmiHeader.biWidth * (Info[0].bmiHeader.biBitCount >> 3);
 
449
        PadSize = (4 - (Bps % 4)) % 4;
 
450
 
 
451
        // Remove the padding.
 
452
        for (i = 0, j = 0; i < Info[0].bmiHeader.biSizeImage; i += Bps + PadSize, j += Bps) {
 
453
                memcpy(Buffer2 + j, Buffer1 + i, Bps);
 
454
        }
 
455
 
 
456
        if (Info[0].bmiHeader.biBitCount == 24) {
 
457
                ilTexImage(Info[0].bmiHeader.biWidth, Info[0].bmiHeader.biHeight, 1, 
 
458
                        (ILubyte)(Info[0].bmiHeader.biBitCount >> 3), IL_BGR, IL_UNSIGNED_BYTE, Buffer2);
 
459
        }
 
460
        else if (Info[0].bmiHeader.biBitCount == 32) {
 
461
                ilTexImage(Info[0].bmiHeader.biWidth, Info[0].bmiHeader.biHeight, 1, 
 
462
                        (ILubyte)(Info[0].bmiHeader.biBitCount >> 3), IL_BGRA, IL_UNSIGNED_BYTE, Buffer2);
 
463
        }
 
464
        ilutCurImage->Origin = IL_ORIGIN_LOWER_LEFT;
 
465
 
 
466
        ReleaseDC(hWnd, hDC); //added 20040527
 
467
        ifree(Buffer1);
 
468
        ifree(Buffer2);
 
469
 
 
470
#endif//_WIN32_WCE
 
471
 
 
472
        return IL_TRUE;
 
473
}
 
474
 
 
475
 
 
476
ILboolean ILAPIENTRY ilutSetHPal(HPALETTE Pal)
 
477
{
 
478
        LPPALETTEENTRY  PalEntries;
 
479
        ILuint                  NumEntries, i;
 
480
        ILubyte                 *TempPal;
 
481
 
 
482
        ilutCurImage = ilGetCurImage();
 
483
        if (ilutCurImage == NULL) {
 
484
                ilSetError(ILUT_ILLEGAL_OPERATION);
 
485
                return IL_FALSE;
 
486
        }
 
487
 
 
488
        NumEntries = GetPaletteEntries(Pal, 0, 0, NULL);
 
489
        if (NumEntries == 0)
 
490
                return IL_TRUE;  // @TODO:  Determine if correct...
 
491
 
 
492
        PalEntries = (LPPALETTEENTRY)ialloc(NumEntries * sizeof(PALETTEENTRY));
 
493
        if (PalEntries == NULL) {
 
494
                return IL_FALSE;
 
495
        }
 
496
 
 
497
        NumEntries = GetPaletteEntries(Pal, 0, NumEntries, PalEntries);
 
498
 
 
499
        TempPal = (ILubyte*)ialloc(NumEntries * 3);
 
500
        if (TempPal == NULL) {
 
501
                ifree(PalEntries);
 
502
                return IL_FALSE;
 
503
        }
 
504
        if (ilutCurImage->Pal.Palette)
 
505
                ifree(ilutCurImage->Pal.Palette);
 
506
        ilutCurImage->Pal.Palette = TempPal;
 
507
        ilutCurImage->Pal.PalSize = NumEntries * 3;
 
508
        ilutCurImage->Pal.PalType = IL_PAL_RGB24;
 
509
 
 
510
        for (i = 0; i < NumEntries; i++) {
 
511
                *TempPal++ = PalEntries[i].peRed;
 
512
                *TempPal++ = PalEntries[i].peGreen;
 
513
                *TempPal++ = PalEntries[i].peBlue;
 
514
        }
 
515
 
 
516
        ifree(PalEntries);
 
517
 
 
518
        return IL_TRUE;
 
519
}
 
520
 
 
521
 
 
522
ILboolean ILAPIENTRY ilutSetWinClipboard()
 
523
{
 
524
        HBITMAP Bitmap;
 
525
        HANDLE  Handle;
 
526
        HWND    hWnd;
 
527
        HDC             hDC;
 
528
        ILimage *TempImage, *CurImage;
 
529
 
 
530
        ilutCurImage = ilGetCurImage();
 
531
        if (ilutCurImage == NULL) {
 
532
                ilSetError(ILUT_ILLEGAL_OPERATION);
 
533
                return IL_FALSE;
 
534
        }
 
535
 
 
536
        if (ilutCurImage->Format != IL_BGR || ilutCurImage->Bps > 1) {
 
537
                TempImage = iConvertImage(ilutCurImage, IL_BGR, IL_UNSIGNED_BYTE);
 
538
                if (TempImage == NULL)
 
539
                        return IL_FALSE;
 
540
        }
 
541
        else
 
542
                TempImage = ilutCurImage;
 
543
 
 
544
        CurImage = ilutCurImage;
 
545
        ilSetCurImage(TempImage);
 
546
 
 
547
        hWnd = GetForegroundWindow();
 
548
        hDC = GetDC(hWnd);
 
549
 
 
550
        if (!OpenClipboard(NULL)) {
 
551
                if (TempImage != ilutCurImage)
 
552
                        ilCloseImage(TempImage);
 
553
                ilSetCurImage(CurImage);
 
554
                ilSetError(ILUT_ILLEGAL_OPERATION);  // Dunno if this is the correct error.
 
555
                ReleaseDC(hWnd, hDC); //added 20040604
 
556
                if (TempImage != ilutCurImage)
 
557
                        ilCloseImage(TempImage);
 
558
                ilSetCurImage(CurImage);
 
559
                return IL_FALSE;
 
560
        }
 
561
 
 
562
        //note that this is not the best method to put an image into the
 
563
        //clipboard, CF_DIB is much better because HBITMAPS are device-dependent.
 
564
        //TODO: eventually change that if there is a need
 
565
        Bitmap = ilutConvertToHBitmap(hDC);
 
566
        ReleaseDC(hWnd, hDC); //added 20040604
 
567
 
 
568
        EmptyClipboard();
 
569
        Handle = SetClipboardData(CF_BITMAP, Bitmap);
 
570
 
 
571
        CloseClipboard();
 
572
 
 
573
        //DeleteObject(Bitmap);  // Needed? No! Clipboard takes care of image.
 
574
 
 
575
        if (TempImage != ilutCurImage)
 
576
                ilCloseImage(TempImage);
 
577
        ilSetCurImage(CurImage);
 
578
 
 
579
        return IL_TRUE;
 
580
}
 
581
 
 
582
 
 
583
ILboolean ILAPIENTRY ilutGetWinClipboard()
 
584
{
 
585
        //HBITMAP               Bitmap;
 
586
        HWND            hWnd;
 
587
        HGLOBAL         hGlobal;
 
588
        PTSTR           pGlobal, data;
 
589
        BITMAPFILEHEADER        *BmpHeader;
 
590
        BITMAPINFOHEADER        *InfoHeader;
 
591
        ILuint          Size;
 
592
 
 
593
        ilutCurImage = ilGetCurImage();
 
594
        if (ilutCurImage == NULL) {
 
595
                ilSetError(ILUT_ILLEGAL_OPERATION);
 
596
                return IL_FALSE;
 
597
        }
 
598
 
 
599
        if (IsClipboardFormatAvailable(CF_DIB)) {
 
600
                hWnd = GetForegroundWindow();
 
601
 
 
602
                if (!OpenClipboard(hWnd)) {
 
603
                        ilSetError(ILUT_ILLEGAL_OPERATION);  // Dunno if this is the correct error.
 
604
                        return IL_FALSE;
 
605
                }
 
606
 
 
607
                hGlobal = GetClipboardData(CF_DIB);
 
608
                if (!hGlobal) {
 
609
                        CloseClipboard();
 
610
                        return IL_FALSE;  // No error?
 
611
                }
 
612
 
 
613
                //copy DIB to buffer because windows delivers it without the
 
614
                //BITMAPFILEHEADER that DevIL needs to load the image
 
615
                Size = GlobalSize(hGlobal);
 
616
                data = ialloc(Size + sizeof(BITMAPFILEHEADER));
 
617
                pGlobal = GlobalLock(hGlobal);
 
618
                if (!pGlobal || !data) {
 
619
                        ifree(data);
 
620
                        CloseClipboard();
 
621
                        return IL_FALSE;  // No error?
 
622
                }
 
623
                memcpy(data + sizeof(BITMAPFILEHEADER), pGlobal, Size);
 
624
                GlobalUnlock(hGlobal);
 
625
                CloseClipboard();
 
626
 
 
627
                //create BITMAPFILEHEADER
 
628
                InfoHeader = (BITMAPINFOHEADER*)(data + sizeof(BITMAPFILEHEADER));
 
629
                BmpHeader = (BITMAPFILEHEADER*)data;
 
630
                BmpHeader->bfType = 'B' | ('M' << 8);
 
631
                BmpHeader->bfSize = Size + sizeof(BITMAPFILEHEADER);
 
632
                BmpHeader->bfReserved1 = BmpHeader->bfReserved2 = 0;
 
633
                BmpHeader->bfOffBits = sizeof(BITMAPFILEHEADER) + InfoHeader->biSize + InfoHeader->biClrUsed*4;
 
634
                if (InfoHeader->biCompression == BI_BITFIELDS)
 
635
                        BmpHeader->bfOffBits += 12;
 
636
 
 
637
                return ilLoadL(IL_BMP, data, BmpHeader->bfSize);
 
638
        }
 
639
        /*
 
640
        //this is not required becaus CF_BITMAP is converted to CF_DIB automatically
 
641
        //when needed. CF_DIB suffices.
 
642
        else if (IsClipboardFormatAvailable(CF_BITMAP)) {
 
643
                hWnd = GetForegroundWindow();
 
644
 
 
645
                if (!OpenClipboard(hWnd)) {
 
646
                        ilSetError(ILUT_ILLEGAL_OPERATION);  // Dunno if this is the correct error.
 
647
                        return IL_FALSE;
 
648
                }
 
649
 
 
650
                Bitmap = (HBITMAP)GetClipboardData(CF_BITMAP);
 
651
                if (!Bitmap) {
 
652
                        CloseClipboard();
 
653
                        return IL_FALSE;  // No error?
 
654
                }
 
655
 
 
656
                if (!ilutSetHBitmap(Bitmap)) {
 
657
                        CloseClipboard();
 
658
                        return IL_FALSE;
 
659
                }
 
660
 
 
661
                CloseClipboard();
 
662
        }*/
 
663
        else { //no data in clipboard
 
664
                ilSetError(ILUT_ILLEGAL_OPERATION);
 
665
                return IL_FALSE;
 
666
        }
 
667
 
 
668
        return IL_TRUE;
 
669
}
 
670
 
 
671
 
 
672
ILboolean ILAPIENTRY ilutWinPrint(ILuint XPos, ILuint YPos, ILuint Width, ILuint Height, HDC hDC)
 
673
{
 
674
#if !defined(_WIN32_WCE) && !(defined(_WIN32) && defined(__GNUC__))
 
675
        PRINTDLG        Pd;
 
676
        DOCINFO         Di;
 
677
        HBITMAP         Bitmap, hReplaced;
 
678
        HDC                     hMemDC;
 
679
 
 
680
        ilutCurImage = ilGetCurImage();
 
681
        if (ilutCurImage == NULL) {
 
682
                ilSetError(ILUT_ILLEGAL_OPERATION);
 
683
                return IL_FALSE;
 
684
        }
 
685
 
 
686
        // Needs error checking!
 
687
        hMemDC = CreateCompatibleDC(hDC);
 
688
        Bitmap = ilutConvertToHBitmap(hDC);
 
689
        hReplaced = (HBITMAP)SelectObject(hMemDC, Bitmap);
 
690
 
 
691
        memset(&Pd, 0, sizeof(PRINTDLG));
 
692
        Pd.lStructSize = sizeof(PRINTDLG);
 
693
        Pd.hwndOwner = GetForegroundWindow();
 
694
        Pd.Flags = PD_RETURNDC;
 
695
        Pd.nCopies = 1;
 
696
        Pd.nFromPage = 0xFFFF;
 
697
        Pd.nToPage = 0xFFFF;
 
698
        Pd.nMinPage = 1;
 
699
        Pd.nMaxPage = 0xFFFF;
 
700
 
 
701
        if (!PrintDlg(&Pd))
 
702
                return (0L);
 
703
 
 
704
        Di.cbSize = sizeof(DOCINFO);
 
705
        Di.lpszDocName = "DevIL Print Job";
 
706
        Di.lpszOutput = NULL;
 
707
        Di.lpszDatatype = NULL;
 
708
        Di.fwType = 0;
 
709
 
 
710
        StartDoc(Pd.hDC, &Di);
 
711
        StartPage(Pd.hDC);
 
712
 
 
713
        StretchBlt(Pd.hDC, XPos, YPos, Width, Height, hMemDC, 0, 0, ilutCurImage->Width, ilutCurImage->Height, SRCCOPY);
 
714
 
 
715
        EndPage(Pd.hDC);
 
716
        EndDoc(Pd.hDC);
 
717
        DeleteObject(Bitmap);
 
718
        DeleteObject(hReplaced);
 
719
        DeleteDC(Pd.hDC);
 
720
 
 
721
#endif
 
722
 
 
723
        return IL_TRUE;
 
724
}
 
725
 
 
726
 
 
727
ILboolean ILAPIENTRY ilutLoadResource(HINSTANCE hInst, ILint ID, const ILstring ResourceType, ILenum Type)
 
728
{
 
729
        HRSRC Resource = (HRSRC)LoadResource(hInst, FindResource(hInst, MAKEINTRESOURCE(ID), ResourceType));
 
730
        ILubyte *Data = (ILubyte*)LockResource(Resource);
 
731
 
 
732
        return ilLoadL(Type, Data, SizeofResource(hInst, FindResource(hInst, MAKEINTRESOURCE(ID), ResourceType)));
 
733
}
 
734
 
 
735
 
 
736
#if !defined(_WIN32_WCE) && !(defined(_WIN32) && defined(__GNUC__))
 
737
#define BUFFSIZE 8192  // Change to suit the efficiency.
 
738
ILboolean ILAPIENTRY ilutWinLoadUrl(const ILstring Url)
 
739
{
 
740
        HINTERNET       Handle, UrlHandle;
 
741
        DWORD           BytesRead = 0, Context = 1;
 
742
        ILubyte         Buff[BUFFSIZE], *Buffer, *TempBuff;
 
743
        ILuint          BufferSize = 0, i;
 
744
        ILboolean       Is404 = IL_TRUE;
 
745
        char            Buffer404[] = { '<', 'h', 't', 'm', 'l', '>' };
 
746
 
 
747
        Buffer = (ILubyte*)ialloc(0);
 
748
        if (Buffer == NULL) {
 
749
                return IL_FALSE;
 
750
        }
 
751
 
 
752
        Handle = InternetOpen("Developer's Image Library", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
 
753
        if (Handle == NULL) {
 
754
                ifree(Buffer);
 
755
                ilSetError(ILUT_COULD_NOT_OPEN_FILE);
 
756
                return IL_FALSE;
 
757
        }
 
758
 
 
759
        // Try again if fails the first time, loading only from the cache.
 
760
        UrlHandle = InternetOpenUrl(Handle, Url, NULL, 0, 0, Context);
 
761
        if (UrlHandle == NULL) {
 
762
                InternetCloseHandle(Handle);
 
763
                Handle = InternetOpen("Developer's Image Library", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_FROM_CACHE);
 
764
                if (Handle == NULL) {
 
765
                        ifree(Buffer);
 
766
                        ilSetError(ILUT_COULD_NOT_OPEN_FILE);
 
767
                        return IL_FALSE;
 
768
                }
 
769
                UrlHandle = InternetOpenUrl(Handle, Url, NULL, 0, 0, Context);
 
770
                if (UrlHandle == NULL) {
 
771
                        ifree(Buffer);
 
772
                        InternetCloseHandle(Handle);
 
773
                        ilSetError(ILUT_COULD_NOT_OPEN_FILE);
 
774
                        return IL_FALSE;
 
775
                }
 
776
        }
 
777
 
 
778
        do {
 
779
                if (!InternetReadFile(UrlHandle, Buff, BUFFSIZE, &BytesRead)) {
 
780
                        InternetCloseHandle(UrlHandle);
 
781
                        InternetCloseHandle(Handle);
 
782
                        ifree(Buffer);
 
783
                        ilSetError(ILUT_COULD_NOT_OPEN_FILE);
 
784
                        return IL_FALSE;
 
785
                }
 
786
 
 
787
                TempBuff = (ILubyte*)ialloc(BufferSize + BytesRead);
 
788
                if (TempBuff == NULL) {
 
789
                        ifree(Buffer);
 
790
                        return IL_FALSE;
 
791
                }
 
792
 
 
793
                memcpy(TempBuff, Buffer, BufferSize);
 
794
                memcpy(TempBuff + BufferSize, Buff, BytesRead);
 
795
                ifree(Buffer);
 
796
                Buffer = TempBuff;
 
797
 
 
798
                BufferSize += BytesRead;
 
799
        } while (BytesRead > 0);
 
800
 
 
801
        InternetCloseHandle(UrlHandle);
 
802
        InternetCloseHandle(Handle);
 
803
 
 
804
        // If the image does not exist, the server usually returns a 404 HTML page.
 
805
        for (i = 0; i < sizeof(Buffer404) && i < BufferSize; i++) {
 
806
                if (tolower(Buffer[i]) != Buffer404[i]) {
 
807
                        Is404 = IL_FALSE;
 
808
                        break;
 
809
                }
 
810
        }
 
811
 
 
812
        if (!Is404) {
 
813
                if (!ilLoadL(ilTypeFromExt(Url), Buffer, BufferSize)) {
 
814
                        if (!ilLoadL(IL_TYPE_UNKNOWN, Buffer, BufferSize)) {
 
815
                                ifree(Buffer);
 
816
                                return IL_FALSE;
 
817
                        }
 
818
                }
 
819
        }
 
820
 
 
821
        ifree(Buffer);
 
822
 
 
823
        return IL_TRUE;
 
824
}
 
825
#endif
 
826
 
 
827
 
 
828
#endif//ILUT_USE_WIN32