FILE COMPARISON
Produced: 2/18/2011 1:51:45 AM
   
Mode:  All Lines  
Left base folder: C:\Documents and Settings\krollins\My Documents\Admin\OpenSource\eRCP\eSWT\eSWT-1.2.orig  
Right base folder: C:\Documents and Settings\krollins\My Documents\Admin\OpenSource\eRCP\eSWT\eSWT-1.2.today  
   
File: org.eclipse.ercp.swt.core.win\win-native\impl\Image.c  
1 /******************************************************************************* = 1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 IBM Corporation and others.   2 * Copyright (c) 2000, 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials   3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0   4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at   5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html   6 * http://www.eclipse.org/legal/epl-v10.html
7 *   7 *
8 * Contributors:   8 * Contributors:
9 *     IBM Corporation - initial API and implementation   9 *     IBM Corporation - initial API and implementation
10 *******************************************************************************/   10 *******************************************************************************/
11     11  
    <> 12 /*******************************************************************************
      13 * Additions/modifications to this source file by Oracle America, Inc. 2011
      14 *******************************************************************************/
      15  
12 #include "ugl_win32.h" = 16 #include "ugl_win32.h"
13 #include "Graphics.h"   17 #include "Graphics.h"
14 #include "UGL_Win32_DC.h"   18 #include "UGL_Win32_DC.h"
15     19  
16 #include "Color.h"   20 #include "Color.h"
17 #include "Image.h"   21 #include "Image.h"
18 #ifndef _WIN32_WCE   22 #ifndef _WIN32_WCE
19 #include "ImageLoader_GDI+.h"   23 #include "ImageLoader_GDI+.h"
20 #include "ImageLoader_DirectX.h"   24 #include "ImageLoader_DirectX.h"
21 #else   25 #else
22 // #include "ImageLoader_External.h"   26 // #include "ImageLoader_External.h"
23 #include "ImageLoader_Imaging.h"   27 #include "ImageLoader_Imaging.h"
24 #endif   28 #endif
25     29  
26 #ifdef _WIN32_WCE   30 #ifdef _WIN32_WCE
27 #include <winuser.h>   31 #include <winuser.h>
28 #endif   32 #endif
29     33  
30 #include <math.h>   34 #include <math.h>
31     35  
32 static HANDLE _createBMI(int width, int height, int depth, int paletteSize);   36 static HANDLE _createBMI(int width, int height, int depth, int paletteSize);
33 static UINT _Image_GetScanlineLength(UINT width, UINT depth);   37 static UINT _Image_GetScanlineLength(UINT width, UINT depth);
34     38  
35 //========================================================================================   39 //========================================================================================
36     40  
37 UINT _Image_GetScanlineLength(UINT width, UINT depth) {   41 UINT _Image_GetScanlineLength(UINT width, UINT depth) {
38     UINT bitsPerLine = width * depth;   42     UINT bitsPerLine = width * depth;
39     UINT bitAlignment = sizeof(LONG) * 8;   43     UINT bitAlignment = sizeof(LONG) * 8;
40     44  
41     UINT bitStride = bitAlignment * (bitsPerLine / bitAlignment);   // The image buffer is always padded to LONG boundaries   45     UINT bitStride = bitAlignment * (bitsPerLine / bitAlignment);   // The image buffer is always padded to LONG boundaries
42     if ((bitsPerLine % bitAlignment) != 0) bitStride += bitAlignment; // Add a bit more for the leftover values   46     if ((bitsPerLine % bitAlignment) != 0) bitStride += bitAlignment; // Add a bit more for the leftover values
43     return bitStride / 8;   47     return bitStride / 8;
44 }   48 }
45     49  
46 //========================================================================================   50 //========================================================================================
47     51  
48 void Image_Dispose(UGL_Int handle, UGL_Error uglError) {   52 void Image_Dispose(UGL_Int handle, UGL_Error uglError) {
49     PUGL_Image image = UGL_IMAGE(handle);   53     PUGL_Image image = UGL_IMAGE(handle);
50     HANDLE imageHandle = image->image_handle;   54     HANDLE imageHandle = image->image_handle;
51     55  
52     DeleteCriticalSection(&(image->dcSection));   56     DeleteCriticalSection(&(image->dcSection));
53     57  
54     free(image);   58     free(image);
55     59  
56     DeleteObject(imageHandle);   60     DeleteObject(imageHandle);
57       61    
58 }   62 }
59 //========================================================================================   63 //========================================================================================
60     64  
61 void _Image_GetSize(PUGL_Image ugl_image, UGL_Int* returnWidth, UGL_Int* returnHeight, UGL_Error uglError) {   65 void _Image_GetSize(PUGL_Image ugl_image, UGL_Int* returnWidth, UGL_Int* returnHeight, UGL_Error uglError) {
62     BITMAP bitmap;   66     BITMAP bitmap;
63     67  
64     GetObject(ugl_image->image_handle, sizeof(BITMAP), &bitmap);   68     GetObject(ugl_image->image_handle, sizeof(BITMAP), &bitmap);
65     *returnWidth = bitmap.bmWidth;   69     *returnWidth = bitmap.bmWidth;
66     *returnHeight = bitmap.bmHeight;   70     *returnHeight = bitmap.bmHeight;
67 }   71 }
68 //========================================================================================   72 //========================================================================================
69     73  
70 void Image_GetBounds(UGL_Int handle, UGL_Int* returnX, UGL_Int* returnY, UGL_Int* returnWidth, UGL_Int* returnHeight, UGL_Error uglError) {   74 void Image_GetBounds(UGL_Int handle, UGL_Int* returnX, UGL_Int* returnY, UGL_Int* returnWidth, UGL_Int* returnHeight, UGL_Error uglError) {
71     PUGL_Image ugl_image = UGL_IMAGE(handle);   75     PUGL_Image ugl_image = UGL_IMAGE(handle);
72     BITMAP bitmap;   76     BITMAP bitmap;
73     77  
74     GetObject(ugl_image->image_handle, sizeof(BITMAP), &bitmap);   78     GetObject(ugl_image->image_handle, sizeof(BITMAP), &bitmap);
75     *returnX = 0;   79     *returnX = 0;
76     *returnY = 0;   80     *returnY = 0;
77     *returnWidth = bitmap.bmWidth;   81     *returnWidth = bitmap.bmWidth;
78     *returnHeight = bitmap.bmHeight;   82     *returnHeight = bitmap.bmHeight;
79 }   83 }
80 //========================================================================================   84 //========================================================================================
81 UGL_Int Image_GetDepth(UGL_Int handle, UGL_Error uglError) {   85 UGL_Int Image_GetDepth(UGL_Int handle, UGL_Error uglError) {
82     PUGL_Image ugl_image = UGL_IMAGE(handle);   86     PUGL_Image ugl_image = UGL_IMAGE(handle);
83     BITMAP bitmap;   87     BITMAP bitmap;
84     88  
85     GetObject(ugl_image->image_handle, sizeof(BITMAP), &bitmap);   89     GetObject(ugl_image->image_handle, sizeof(BITMAP), &bitmap);
86     return bitmap.bmBitsPixel;   90     return bitmap.bmBitsPixel;
87 }   91 }
88 //========================================================================================   92 //========================================================================================
89     93  
90 void Image_GetRGB(UGL_Int handle, UGL_Int startX, UGL_Int startY, UGL_Int width, UGL_Int height, UGL_Int offset, UGL_Int scanSize, UGL_IntArray rgbs, UGL_Error uglError) {   94 void Image_GetRGB(UGL_Int handle, UGL_Int startX, UGL_Int startY, UGL_Int width, UGL_Int height, UGL_Int offset, UGL_Int scanSize, UGL_IntArray rgbs, UGL_Error uglError) {
91     PUGL_Image ugl_image = UGL_IMAGE(handle);   95     PUGL_Image ugl_image = UGL_IMAGE(handle);
92     96  
93     BITMAP bitmap;   97     BITMAP bitmap;
94     LPBYTE pvBits;   98     LPBYTE pvBits;
95     int xPos, yPos;   99     int xPos, yPos;
96     100  
97     int size = GetObject(ugl_image->image_handle, sizeof(BITMAP), &bitmap);   101     int size = GetObject(ugl_image->image_handle, sizeof(BITMAP), &bitmap);
98     102  
99     pvBits = (LPBYTE)bitmap.bmBits;   103     pvBits = (LPBYTE)bitmap.bmBits;
100     for (yPos = startY; yPos < (startY + height); yPos++) {   104     for (yPos = startY; yPos < (startY + height); yPos++) {
101         for (xPos = startX; xPos < (startX + width); xPos++) {   105         for (xPos = startX; xPos < (startX + width); xPos++) {
102             int rgbIndex = offset + (xPos - startX) + ((yPos - startY) * scanSize);   106             int rgbIndex = offset + (xPos - startX) + ((yPos - startY) * scanSize);
103             int bitsIndex = (xPos*3) + (yPos * bitmap.bmWidthBytes);   107             int bitsIndex = (xPos*3) + (yPos * bitmap.bmWidthBytes);
104     108  
105             rgbs[rgbIndex] = pvBits[bitsIndex] & 0x0000FF;   109             rgbs[rgbIndex] = pvBits[bitsIndex] & 0x0000FF;
106             rgbs[rgbIndex] |= (pvBits[bitsIndex+1] << 8) & 0x00FF00;   110             rgbs[rgbIndex] |= (pvBits[bitsIndex+1] << 8) & 0x00FF00;
107             rgbs[rgbIndex] |= (pvBits[bitsIndex+2] << 16);   111             rgbs[rgbIndex] |= (pvBits[bitsIndex+2] << 16);
108             rgbs[rgbIndex] |= 0xFF000000;   112             rgbs[rgbIndex] |= 0xFF000000;
109         }   113         }
110     }   114     }
111     115  
112     /*   116     /*
113     HDC imageHDC;   117     HDC imageHDC;
114     HGDIOBJ oldBitmap;   118     HGDIOBJ oldBitmap;
115     int xPos, yPos;   119     int xPos, yPos;
116     120  
117     if (ugl_image->selected_DC == NULL) {   121     if (ugl_image->selected_DC == NULL) {
118         HDC hdc = GetDC(NULL);   122         HDC hdc = GetDC(NULL);
119         imageHDC = CreateCompatibleDC(hdc);   123         imageHDC = CreateCompatibleDC(hdc);
120         ReleaseDC(NULL, hdc);   124         ReleaseDC(NULL, hdc);
121         oldBitmap = SelectObject(imageHDC, ugl_image->image_handle);   125         oldBitmap = SelectObject(imageHDC, ugl_image->image_handle);
122     } else {   126     } else {
123         imageHDC = ugl_image->selected_DC->hdc;   127         imageHDC = ugl_image->selected_DC->hdc;
124     }   128     }
125     129  
126     for (yPos = 0; yPos < height; yPos++) {   130     for (yPos = 0; yPos < height; yPos++) {
127         for (xPos = 0; xPos < width; xPos++) {   131         for (xPos = 0; xPos < width; xPos++) {
128             int x = startX + xPos;   132             int x = startX + xPos;
129             int y = startY + yPos;                    133             int y = startY + yPos;                 
130             int index = offset + yPos * scanSize + xPos;   134             int index = offset + yPos * scanSize + xPos;
131             COLORREF pixelRGB = GetPixel(imageHDC, x, y);   135             COLORREF pixelRGB = GetPixel(imageHDC, x, y);
132     136  
133             rgbs[index] = RGB_SWAP(pixelRGB);   137             rgbs[index] = RGB_SWAP(pixelRGB);
134         }   138         }
135     }   139     }
136     140  
137     if (ugl_image->selected_DC == NULL) {   141     if (ugl_image->selected_DC == NULL) {
138         SelectObject(imageHDC, oldBitmap);   142         SelectObject(imageHDC, oldBitmap);
139         DeleteDC(imageHDC);   143         DeleteDC(imageHDC);
140     }   144     }
141     */   145     */
142 }   146 }
143 //========================================================================================   147 //========================================================================================
144     148  
145 UGL_Int Image_GetSize(UGL_Int handle, UGL_Error uglError) {   149 UGL_Int Image_GetSize(UGL_Int handle, UGL_Error uglError) {
146     PUGL_Image ugl_image = UGL_IMAGE(handle);   150     PUGL_Image ugl_image = UGL_IMAGE(handle);
147     BITMAP bitmap;   151     BITMAP bitmap;
148     152  
149     GetObject(ugl_image->image_handle, sizeof(BITMAP), &bitmap);   153     GetObject(ugl_image->image_handle, sizeof(BITMAP), &bitmap);
150     154  
151     return _Image_GetScanlineLength(bitmap.bmWidth, bitmap.bmBitsPixel) * bitmap.bmHeight;   155     return _Image_GetScanlineLength(bitmap.bmWidth, bitmap.bmBitsPixel) * bitmap.bmHeight;
152 }   156 }
153     157  
154     158  
155 PUGL_Image _Image_New(HANDLE handle, BOOL is_direct, UCHAR transparent_pixel, INT32 transparent_color, UGL_Error error) {   159 PUGL_Image _Image_New(HANDLE handle, BOOL is_direct, UCHAR transparent_pixel, INT32 transparent_color, UGL_Error error) {
156     PUGL_Image image = (PUGL_Image)calloc(sizeof(UGL_Image),1);   160     PUGL_Image image = (PUGL_Image)calloc(sizeof(UGL_Image),1);
157     if (image == NULL) {   161     if (image == NULL) {
158         DeleteObject(handle);   162         DeleteObject(handle);
159         UGL_SetError(error, UGL_ERROR_OUT_OF_MEMORY);   163         UGL_SetError(error, UGL_ERROR_OUT_OF_MEMORY);
160     } else {   164     } else {
161         image->image_handle = handle;   165         image->image_handle = handle;
162         image->is_direct = is_direct;   166         image->is_direct = is_direct;
163         image->transparent_pixel = transparent_pixel;   167         image->transparent_pixel = transparent_pixel;
164         image->transparent_color = transparent_color;   168         image->transparent_color = transparent_color;
165         InitializeCriticalSection(&(image->dcSection));   169         InitializeCriticalSection(&(image->dcSection));
166     }   170     }
167     171  
168     return image;   172     return image;
169 }   173 }
170     174  
171 //========================================================================================   175 //========================================================================================
172     176  
173 UGL_Int Image_NewFromFileData(UGL_Int deviceHandle, UGL_ByteArray data, UGL_Int length, UGL_Error uglError) {   177 UGL_Int Image_NewFromFileData(UGL_Int deviceHandle, UGL_ByteArray data, UGL_Int length, UGL_Error uglError) {
174     BOOL direct = TRUE;   178     BOOL direct = TRUE;
175     BOOL hasAlpha = FALSE;   179     BOOL hasAlpha = FALSE;
176     int transparentPixel = -1;   180     int transparentPixel = -1;
177     int transparentColor = -1;   181     int transparentColor = -1;
178       182    
179     HBITMAP imageHandle;   183     HBITMAP imageHandle;
180     184  
181 #ifndef _WIN32_WCE   185 #ifndef _WIN32_WCE
182     imageHandle = GDIpImageLoader_LoadFromFileData(data, length, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);   186     imageHandle = GDIpImageLoader_LoadFromFileData(data, length, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);
183     if ((UGL_GetError(uglError) != 0) && (UGL_GetError(uglError) != UGL_ERROR_INVALID_IMAGE)) {   187     if ((UGL_GetError(uglError) != 0) && (UGL_GetError(uglError) != UGL_ERROR_INVALID_IMAGE)) {
184         BOOL direct = TRUE;   188         BOOL direct = TRUE;
185         BOOL hasAlpha = FALSE;   189         BOOL hasAlpha = FALSE;
186         int transparentPixel = -1;   190         int transparentPixel = -1;
187         int transparentColor = -1;   191         int transparentColor = -1;
188           192        
189         UGL_ClearError(uglError);   193         UGL_ClearError(uglError);
190         imageHandle = DXImageLoader_LoadFromFileData(data, length, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);   194         imageHandle = DXImageLoader_LoadFromFileData(data, length, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);
191         if (UGL_GetError(uglError) != 0) {   195         if (UGL_GetError(uglError) != 0) {
192             return NULL_HANDLE;   196             return NULL_HANDLE;
193         }   197         }
194     }   198     }
195 #else   199 #else
196     imageHandle = ImagingImageLoader_LoadFromFileData(data, length, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);   200     imageHandle = ImagingImageLoader_LoadFromFileData(data, length, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);
197     if (imageHandle == NULL_HANDLE) return NULL_HANDLE;   201     if (imageHandle == NULL_HANDLE) return NULL_HANDLE;
198     202  
199 //  imageHandle = ExternalImageLoader_LoadFromFileData(data, length, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);   203 //  imageHandle = ExternalImageLoader_LoadFromFileData(data, length, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);
200 //  if (imageHandle == NULL_HANDLE) return NULL_HANDLE;   204 //  if (imageHandle == NULL_HANDLE) return NULL_HANDLE;
201 #endif   205 #endif
202     206  
203     return (UGL_Int)_Image_New(imageHandle, direct, transparentPixel, transparentColor, uglError);   207     return (UGL_Int)_Image_New(imageHandle, direct, transparentPixel, transparentColor, uglError);
204 }   208 }
205 //========================================================================================   209 //========================================================================================
206     210  
207 /**   211 /**
208 * Creates a new, empty image with the given dimensions.   212 * Creates a new, empty image with the given dimensions.
209 * <p>   213 * <p>
210 * Image will be filled with the provided fill color which   214 * Image will be filled with the provided fill color which
211 * is in 0xAARRGGBB format.   215 * is in 0xAARRGGBB format.
212 * </p>   216 * </p>
213 *   217 *
214 * @ingroup Image   218 * @ingroup Image
215 *   219 *
216 * @param deviceHandle the device/display handle   220 * @param deviceHandle the device/display handle
217 * @param width width of the new image   221 * @param width width of the new image
218 * @param height height of the new image   222 * @param height height of the new image
219 * @param fillColor value with which to fill the image in 0xAARRGGBB format.   223 * @param fillColor value with which to fill the image in 0xAARRGGBB format.
220 * @param error the error status of the call   224 * @param error the error status of the call
221 *   225 *
222 * @return handle to the new image   226 * @return handle to the new image
223 */   227 */
224 UGL_Int Image_NewFromSize(UGL_Int deviceHandle, UGL_Int width, UGL_Int height, UGL_Int fillColor, UGL_Error error) {   228 UGL_Int Image_NewFromSize(UGL_Int deviceHandle, UGL_Int width, UGL_Int height, UGL_Int fillColor, UGL_Error error) {
225     PUGL_Image ugl_image = NULL;   229     PUGL_Image ugl_image = NULL;
226     BYTE* pBits = NULL;   230     BYTE* pBits = NULL;
227     HANDLE hDib;   231     HANDLE hDib;
228     BITMAPINFO bmi;   232     BITMAPINFO bmi;
229     BITMAPINFO *pbmi;   233     BITMAPINFO *pbmi;
230     BYTE* lastPixel;   234     BYTE* lastPixel;
231     UINT scanlineLength;   235     UINT scanlineLength;
232     236  
233     /*   237     /*
234      * Assumes 16-bit format data will be in 5-5-5, and converts   238      * Assumes 16-bit format data will be in 5-5-5, and converts
235      * green component proportionally (value / 2 ^ 5).   239      * green component proportionally (value / 2 ^ 5).
236      */   240      */
237     241  
238     242  
239     /*   243     /*
240     * Note on WinCE.  CreateDIBSection requires the biCompression   244     * Note on WinCE.  CreateDIBSection requires the biCompression
241     * field of the BITMAPINFOHEADER to be set to BI_BITFIELDS for   245     * field of the BITMAPINFOHEADER to be set to BI_BITFIELDS for
242     * 16 and 32 bit direct images (see MSDN for CreateDIBSection).   246     * 16 and 32 bit direct images (see MSDN for CreateDIBSection).
243     * In this case, the color mask can be set to any value.  For   247     * In this case, the color mask can be set to any value.  For
244     * consistency, it is set to the same mask used by non WinCE   248     * consistency, it is set to the same mask used by non WinCE
245     * platforms in BI_RGB mode.   249     * platforms in BI_RGB mode.
246     */   250     */
247     251  
248     /*   252     /*
249      * Windows supports 16-bit mask of 5-5-5 (0x7C00, 0x3E0, 0x1F),   253      * Windows supports 16-bit mask of 5-5-5 (0x7C00, 0x3E0, 0x1F),
250      * 16-bit 5-6-5 (0xF800, 0x07E0, 0x001F) (only using BI_BITFIELDS),   254      * 16-bit 5-6-5 (0xF800, 0x07E0, 0x001F) (only using BI_BITFIELDS),
251      * 24-bit mask of (0xFF0000, 0xFF00, 0xFF) and 32-bit mask   255      * 24-bit mask of (0xFF0000, 0xFF00, 0xFF) and 32-bit mask
252      * (0x00FF0000, 0x0000FF00, 0x000000FF) as documented in   256      * (0x00FF0000, 0x0000FF00, 0x000000FF) as documented in
253      * MSDN BITMAPINFOHEADER.  Make sure the image is   257      * MSDN BITMAPINFOHEADER.  Make sure the image is
254      * Windows-supported.   258      * Windows-supported.
255      */   259      */
256     260  
257     memset(&bmi, 0, sizeof(BITMAPINFOHEADER));   261     memset(&bmi, 0, sizeof(BITMAPINFOHEADER));
258     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);   262     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
259     bmi.bmiHeader.biWidth = width;   263     bmi.bmiHeader.biWidth = width;
260     bmi.bmiHeader.biHeight = -height;   264     bmi.bmiHeader.biHeight = -height;
261     bmi.bmiHeader.biPlanes = 1;   265     bmi.bmiHeader.biPlanes = 1;
262     bmi.bmiHeader.biBitCount = 24;   266     bmi.bmiHeader.biBitCount = 24;
263     bmi.bmiHeader.biClrUsed = 0;   267     bmi.bmiHeader.biClrUsed = 0;
264     bmi.bmiHeader.biCompression = BI_RGB;   268     bmi.bmiHeader.biCompression = BI_RGB;
265     pbmi = &bmi;   269     pbmi = &bmi;
266     270  
267     hDib = CreateDIBSection(NULL, pbmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0x0);   271     hDib = CreateDIBSection(NULL, pbmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0x0);
268     if (hDib == 0) {   272     if (hDib == 0) {
269         Win32Error_SetError(error, GetLastError(), _T("Unable to create device-independent image handle"));   273         Win32Error_SetError(error, GetLastError(), _T("Unable to create device-independent image handle"));
270         if (pbmi != (&bmi)) free(pbmi);   274         if (pbmi != (&bmi)) free(pbmi);
271         return NULL_HANDLE;   275         return NULL_HANDLE;
272     }   276     }
273     277  
274     if (pbmi != (&bmi)) free(pbmi);   278     if (pbmi != (&bmi)) free(pbmi);
275     279  
276     scanlineLength = _Image_GetScanlineLength(width, 24);   280     scanlineLength = _Image_GetScanlineLength(width, 24);
277     lastPixel = pBits + (scanlineLength * height);   281     lastPixel = pBits + (scanlineLength * height);
278     while (pBits < lastPixel) {   282     while (pBits < lastPixel) {
279         int x;   283         int x;
280         for (x=0;x<width;x++) {   284         for (x=0;x<width;x++) {
281             pBits[x*3] = (fillColor >> 0) & 0xFF;   285             pBits[x*3] = (fillColor >> 0) & 0xFF;
282             pBits[x*3+1] = (fillColor >> 8) & 0xFF;   286             pBits[x*3+1] = (fillColor >> 8) & 0xFF;
283             pBits[x*3+2] = (fillColor >> 16) & 0xFF;   287             pBits[x*3+2] = (fillColor >> 16) & 0xFF;
284         }   288         }
285         pBits = (BYTE*)pBits + scanlineLength;   289         pBits = (BYTE*)pBits + scanlineLength;
286     }   290     }
287     291  
288     return (UGL_Int)_Image_New(hDib, TRUE, -1, -1, error);   292     return (UGL_Int)_Image_New(hDib, TRUE, -1, -1, error);
289 }   293 }
290 //========================================================================================   294 //========================================================================================
291     295  
292 UGL_Int Image_NewFromIntData(UGL_Int deviceHandle, UGL_Int width, UGL_Int height, UGL_IntArray imageData, UGL_Int depth, UGL_Int transparentColor, UGL_Int globalAlpha, UGL_ByteArray alphaData, UGL_Error uglError) {   296 UGL_Int Image_NewFromIntData(UGL_Int deviceHandle, UGL_Int width, UGL_Int height, UGL_IntArray imageData, UGL_Int depth, UGL_Int transparentColor, UGL_Int globalAlpha, UGL_ByteArray alphaData, UGL_Error uglError) {
293     PUGL_Image ugl_image = NULL;   297     PUGL_Image ugl_image = NULL;
294     int pBits[1] = {0};   298     int pBits[1] = {0};
295     HANDLE hDib;   299     HANDLE hDib;
296     BITMAPINFO bmi;   300     BITMAPINFO bmi;
297     BITMAPINFO *pbmi;   301     BITMAPINFO *pbmi;
298     302  
299     /*   303     /*
300      * Assumes 16-bit format data will be in 5-5-5, and converts   304      * Assumes 16-bit format data will be in 5-5-5, and converts
301      * green component proportionally (value / 2 ^ 5).   305      * green component proportionally (value / 2 ^ 5).
302      */   306      */
303     307  
304     308  
305     /*   309     /*
306     * Note on WinCE.  CreateDIBSection requires the biCompression   310     * Note on WinCE.  CreateDIBSection requires the biCompression
307     * field of the BITMAPINFOHEADER to be set to BI_BITFIELDS for   311     * field of the BITMAPINFOHEADER to be set to BI_BITFIELDS for
308     * 16 and 32 bit direct images (see MSDN for CreateDIBSection).   312     * 16 and 32 bit direct images (see MSDN for CreateDIBSection).
309     * In this case, the color mask can be set to any value.  For   313     * In this case, the color mask can be set to any value.  For
310     * consistency, it is set to the same mask used by non WinCE   314     * consistency, it is set to the same mask used by non WinCE
311     * platforms in BI_RGB mode.   315     * platforms in BI_RGB mode.
312     */   316     */
313     317  
314     /*   318     /*
315      * Windows supports 16-bit mask of 5-5-5 (0x7C00, 0x3E0, 0x1F),   319      * Windows supports 16-bit mask of 5-5-5 (0x7C00, 0x3E0, 0x1F),
316      * 16-bit 5-6-5 (0xF800, 0x07E0, 0x001F) (only using BI_BITFIELDS),   320      * 16-bit 5-6-5 (0xF800, 0x07E0, 0x001F) (only using BI_BITFIELDS),
317      * 24-bit mask of (0xFF0000, 0xFF00, 0xFF) and 32-bit mask   321      * 24-bit mask of (0xFF0000, 0xFF00, 0xFF) and 32-bit mask
318      * (0x00FF0000, 0x0000FF00, 0x000000FF) as documented in   322      * (0x00FF0000, 0x0000FF00, 0x000000FF) as documented in
319      * MSDN BITMAPINFOHEADER.  Make sure the image is   323      * MSDN BITMAPINFOHEADER.  Make sure the image is
320      * Windows-supported.   324      * Windows-supported.
321      */   325      */
322     326  
323     memset(&bmi, 0, sizeof(BITMAPINFOHEADER));   327     memset(&bmi, 0, sizeof(BITMAPINFOHEADER));
324     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);   328     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
325     bmi.bmiHeader.biWidth = width;   329     bmi.bmiHeader.biWidth = width;
326     bmi.bmiHeader.biHeight = -height;   330     bmi.bmiHeader.biHeight = -height;
327     bmi.bmiHeader.biPlanes = 1;   331     bmi.bmiHeader.biPlanes = 1;
328     bmi.bmiHeader.biBitCount = depth;   332     bmi.bmiHeader.biBitCount = depth;
329     if ((alphaData != NULL) || (globalAlpha != -1)) {   333     if ((alphaData != NULL) || (globalAlpha != -1)) {
330         // Windows only supports aplha blending for 32-bpp   334         // Windows only supports aplha blending for 32-bpp
331         bmi.bmiHeader.biBitCount = 32;   335         bmi.bmiHeader.biBitCount = 32;
332     }   336     }
333     337  
334     bmi.bmiHeader.biClrUsed = 0;   338     bmi.bmiHeader.biClrUsed = 0;
335     339  
336 #if (_WIN32_WCE == 300)   340 #if (_WIN32_WCE == 300)
337     if ((depth == 16 || depth == 32)) {   341     if ((depth == 16 || depth == 32)) {
338         // investigate use of BI_BITFIELDS in WIN32 for 5-6-5 16-bit color,   342         // investigate use of BI_BITFIELDS in WIN32 for 5-6-5 16-bit color,
339         // using masks in above comment.   343         // using masks in above comment.
340     344  
341         bmi.bmiHeader.biCompression = BI_BITFIELDS;   345         bmi.bmiHeader.biCompression = BI_BITFIELDS;
342     346  
343         pbmi = (BITMAPINFO *)calloc(sizeof(BITMAPINFOHEADER) + 12, 1);   347         pbmi = (BITMAPINFO *)calloc(sizeof(BITMAPINFOHEADER) + 12, 1);
344         MoveMemory((void *)pbmi, (void *)&bmi.bmiHeader, sizeof(BITMAPINFOHEADER));   348         MoveMemory((void *)pbmi, (void *)&bmi.bmiHeader, sizeof(BITMAPINFOHEADER));
345         {   349         {
346             LPBYTE pbytebmi = (LPBYTE)pbmi;   350             LPBYTE pbytebmi = (LPBYTE)pbmi;
347             int offset = sizeof(BITMAPINFOHEADER);   351             int offset = sizeof(BITMAPINFOHEADER);
348             int redMask = (depth == 32 ? 0xFF0000 : 0xF800);   352             int redMask = (depth == 32 ? 0xFF0000 : 0xF800);
349             int greenMask = (depth == 32 ? 0xFF00 : 0x07E0);   353             int greenMask = (depth == 32 ? 0xFF00 : 0x07E0);
350             int blueMask = (depth == 32 ? 0xFF : 0x001F);   354             int blueMask = (depth == 32 ? 0xFF : 0x001F);
351           355        
352             pbytebmi[offset] = (BYTE)((redMask & 0xFF) >> 0);   356             pbytebmi[offset] = (BYTE)((redMask & 0xFF) >> 0);
353             pbytebmi[offset + 1] = (BYTE)((redMask & 0xFF00) >> 8);   357             pbytebmi[offset + 1] = (BYTE)((redMask & 0xFF00) >> 8);
354             pbytebmi[offset + 2] = (BYTE)((redMask & 0xFF0000) >> 16);   358             pbytebmi[offset + 2] = (BYTE)((redMask & 0xFF0000) >> 16);
355             pbytebmi[offset + 3] = (BYTE)((redMask & 0xFF000000) >> 24);   359             pbytebmi[offset + 3] = (BYTE)((redMask & 0xFF000000) >> 24);
356             pbytebmi[offset + 4] = (BYTE)((greenMask & 0xFF) >> 0);   360             pbytebmi[offset + 4] = (BYTE)((greenMask & 0xFF) >> 0);
357             pbytebmi[offset + 5] = (BYTE)((greenMask & 0xFF00) >> 8);   361             pbytebmi[offset + 5] = (BYTE)((greenMask & 0xFF00) >> 8);
358             pbytebmi[offset + 6] = (BYTE)((greenMask & 0xFF0000) >> 16);   362             pbytebmi[offset + 6] = (BYTE)((greenMask & 0xFF0000) >> 16);
359             pbytebmi[offset + 7] = (BYTE)((greenMask & 0xFF000000) >> 24);   363             pbytebmi[offset + 7] = (BYTE)((greenMask & 0xFF000000) >> 24);
360             pbytebmi[offset + 8] = (BYTE)((blueMask & 0xFF) >> 0);   364             pbytebmi[offset + 8] = (BYTE)((blueMask & 0xFF) >> 0);
361             pbytebmi[offset + 9] = (BYTE)((blueMask & 0xFF00) >> 8);   365             pbytebmi[offset + 9] = (BYTE)((blueMask & 0xFF00) >> 8);
362             pbytebmi[offset + 10] = (BYTE)((blueMask & 0xFF0000) >> 16);   366             pbytebmi[offset + 10] = (BYTE)((blueMask & 0xFF0000) >> 16);
363             pbytebmi[offset + 11] = (BYTE)((blueMask & 0xFF000000) >> 24);   367             pbytebmi[offset + 11] = (BYTE)((blueMask & 0xFF000000) >> 24);
364         }   368         }
365     }     369     }  
366 #else   370 #else
367     bmi.bmiHeader.biCompression = BI_RGB;   371     bmi.bmiHeader.biCompression = BI_RGB;
368     pbmi = &bmi;   372     pbmi = &bmi;
369 #endif   373 #endif
370     374  
371     hDib = CreateDIBSection(NULL, (CONST BITMAPINFO *)pbmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0x0);   375     hDib = CreateDIBSection(NULL, (CONST BITMAPINFO *)pbmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0x0);
372     if (hDib == 0) {   376     if (hDib == 0) {
373         Win32Error_SetError(uglError, GetLastError(), _T("Unable to create device-independent image handle"));   377         Win32Error_SetError(uglError, GetLastError(), _T("Unable to create device-independent image handle"));
374         if (pbmi != (&bmi)) free(pbmi);   378         if (pbmi != (&bmi)) free(pbmi);
375         return NULL_HANDLE;   379         return NULL_HANDLE;
376     }   380     }
377     381  
378     382  
379 //  if (depth == 32) {   383 //  if (depth == 32) {
380 //      MoveMemory((void *)pBits[0], imageData, (depth * width * height) / 8);   384 //      MoveMemory((void *)pBits[0], imageData, (depth * width * height) / 8);
381 //  } else {   385 //  } else {
382     {   386     {
383         int data_int_length = width * height;   387         int data_int_length = width * height;
384         int buffer_index, data_index;   388         int buffer_index, data_index;
385         LPBYTE image_buffer = (LPBYTE)pBits[0];   389         LPBYTE image_buffer = (LPBYTE)pBits[0];
386     390  
387         for (data_index = 0, buffer_index = 0; data_index < data_int_length; data_index++) {   391         for (data_index = 0, buffer_index = 0; data_index < data_int_length; data_index++) {
388             int pixel = imageData[data_index];   392             int pixel = imageData[data_index];
389     393  
390             int red;   394             int red;
391             int green;   395             int green;
392             int blue;   396             int blue;
393             if (depth == 16) {   397             if (depth == 16) {
394                 // 16-bpp arrives as 5-6-5 (0xF800, 0x07E0, 0x001F)   398                 // 16-bpp arrives as 5-6-5 (0xF800, 0x07E0, 0x001F)
395                 red = (pixel & 0xF800) >> 11;   399                 red = (pixel & 0xF800) >> 11;
396                 green = (pixel & 0x07E0) >> 5;   400                 green = (pixel & 0x07E0) >> 5;
397                 blue = (pixel & 0x001F);   401                 blue = (pixel & 0x001F);
398                 if (alphaData != NULL) {   402                 if (alphaData != NULL) {
399                     // with alpha, 16-bpp 5-6-5 must be converted to 32-bpp 8-8-8   403                     // with alpha, 16-bpp 5-6-5 must be converted to 32-bpp 8-8-8
400                     red = ((red * 0x0FF) / 0x01F) << 16;   404                     red = ((red * 0x0FF) / 0x01F) << 16;
401                     green = ((green * 0x0FF) / 0x03F) << 8;   405                     green = ((green * 0x0FF) / 0x03F) << 8;
402                     blue = (blue * 0x0FF) / 0x01F;   406                     blue = (blue * 0x0FF) / 0x01F;
403                 } else {   407                 } else {
404                     // No alpha, we only have to find max green in 16 bit 5-5-5   408                     // No alpha, we only have to find max green in 16 bit 5-5-5
405                     green = (green * 0x01F) / 0x03F;   409                     green = (green * 0x01F) / 0x03F;
406                 }   410                 }
407             } else {   411             } else {
408                 // 24-bpp and 32-bpp arrive as 8-8-8   412                 // 24-bpp and 32-bpp arrive as 8-8-8
409                 red = (pixel & 0x00FF0000);    413                 red = (pixel & 0x00FF0000); 
410                 green = (pixel & 0x0000FF00);    414                 green = (pixel & 0x0000FF00); 
411                 blue = (pixel & 0x000000FF);    415                 blue = (pixel & 0x000000FF); 
412             }   416             }
413     417  
414             if (bmi.bmiHeader.biBitCount == 32) {   418             if (bmi.bmiHeader.biBitCount == 32) {
415                 // 32-bpp (with or without alpha), 16-bpp with alpha or, 24-bpp with alpha   419                 // 32-bpp (with or without alpha), 16-bpp with alpha or, 24-bpp with alpha
416                 unsigned long *target_pixel = (unsigned long *)&image_buffer[buffer_index];   420                 unsigned long *target_pixel = (unsigned long *)&image_buffer[buffer_index];
417                 *target_pixel = 0;   421                 *target_pixel = 0;
418                 if (alphaData != NULL) {   422                 if (alphaData != NULL) {
419                     *target_pixel = (alphaData[data_index] << 24);   423                     *target_pixel = (alphaData[data_index] << 24);
420                 } else   424                 } else
421                 if (globalAlpha != -1) {   425                 if (globalAlpha != -1) {
422                     *target_pixel = (globalAlpha << 24) & 0xFF000000;   426                     *target_pixel = (globalAlpha << 24) & 0xFF000000;
423                 } else   427                 } else
424                 if (depth == 32) {   428                 if (depth == 32) {
425                     *target_pixel = (pixel & 0xFF000000);   429                     *target_pixel = (pixel & 0xFF000000);
426                 }   430                 }
427                 *target_pixel |= red;  // red   431                 *target_pixel |= red;  // red
428                 *target_pixel |= green;  //green   432                 *target_pixel |= green;  //green
429                 *target_pixel |= blue;  //blue   433                 *target_pixel |= blue;  //blue
430 //              printf("target_pixel= %d", *target_pixel);   434 //              printf("target_pixel= %d", *target_pixel);
431                 buffer_index += sizeof(unsigned long);   435                 buffer_index += sizeof(unsigned long);
432             } else if (depth == 24) {             436             } else if (depth == 24) {          
433                 image_buffer[buffer_index++] = blue;  //blue   437                 image_buffer[buffer_index++] = blue;  //blue
434                 image_buffer[buffer_index++] = green >> 8;  //green   438                 image_buffer[buffer_index++] = green >> 8;  //green
435                 image_buffer[buffer_index++] = red >> 16;  //red   439                 image_buffer[buffer_index++] = red >> 16;  //red
436             } else if (depth == 16) {   440             } else if (depth == 16) {
437                 unsigned short *target_pixel = (unsigned short *)&image_buffer[buffer_index];   441                 unsigned short *target_pixel = (unsigned short *)&image_buffer[buffer_index];
438                 // 16-bit comes in as 5-6-5 (0xF800, 0x07E0, 0x001F)   442                 // 16-bit comes in as 5-6-5 (0xF800, 0x07E0, 0x001F)
439 //              int red = (pixel & 0xF800) >> 11;   443 //              int red = (pixel & 0xF800) >> 11;
440 //              int green = (pixel & 0x07E0) >> 5;   444 //              int green = (pixel & 0x07E0) >> 5;
441 //              int blue = (pixel & 0x001F);   445 //              int blue = (pixel & 0x001F);
442 //              // we only have to find max green in 16 bit 5-5-5   446 //              // we only have to find max green in 16 bit 5-5-5
443 //              green = (green * 0x01F) / 0x03F;   447 //              green = (green * 0x01F) / 0x03F;
444     448  
445 //              unsigned short *target_pixel = (unsigned short *)&image_buffer[buffer_index];   449 //              unsigned short *target_pixel = (unsigned short *)&image_buffer[buffer_index];
446                 *target_pixel = ((red << 10)) | ((green  << 5)) | (blue);   450                 *target_pixel = ((red << 10)) | ((green  << 5)) | (blue);
447                 buffer_index += sizeof(unsigned short);   451                 buffer_index += sizeof(unsigned short);
448             }   452             }
449             if (((data_index + 1) % width) == 0){   453             if (((data_index + 1) % width) == 0){
450                 // at the end of one row of pixel data. Each scan   454                 // at the end of one row of pixel data. Each scan
451                 // line must be padded with zeroes to end on a LONG boundary   455                 // line must be padded with zeroes to end on a LONG boundary
452                 int remainder = ((buffer_index) % sizeof(long));   456                 int remainder = ((buffer_index) % sizeof(long));
453                 if (remainder != 0) {   457                 if (remainder != 0) {
454                     buffer_index += sizeof(long) - remainder;   458                     buffer_index += sizeof(long) - remainder;
455                 }   459                 }
456             }   460             }
457     461  
458         }   462         }
459     }   463     }
460     464  
461     465  
462     if (pbmi != (&bmi)) free(pbmi);   466     if (pbmi != (&bmi)) free(pbmi);
463     return (UGL_Int)_Image_New(hDib, TRUE, 0, transparentColor, uglError);   467     return (UGL_Int)_Image_New(hDib, TRUE, 0, transparentColor, uglError);
464 }   468 }
465 //========================================================================================   469 //========================================================================================
466     470  
    <> 471 UGL_Int Image_NewFromSystemResource(UGL_Int deviceHandle, UGL_Int identifier, UGL_Error uglError) {
      472     UGL_Int handle;
      473     PUGL_Image ugl_image;
      474     LPCWSTR lpName;
      475     HICON hIcon;
      476     HDC hDC;
      477     HBITMAP hBitmap;
      478     HBITMAP hOldBitmap;
      479  
      480 #ifndef _WIN32_WCE
      481     switch (identifier)
      482     {
      483     default:
      484     case UGL_IMAGE_ERROR:
      485         lpName = IDI_ERROR;
      486         break;
      487  
      488     case UGL_IMAGE_INFORMATION:
      489         lpName = IDI_INFORMATION;
      490         break;
      491  
      492     case UGL_IMAGE_QUESTION:
      493         lpName = IDI_QUESTION;
      494         break;
      495  
      496     case UGL_IMAGE_WARNING:
      497         lpName = IDI_WARNING;
      498         break;
      499     }
      500  
      501     hIcon = LoadIcon(NULL, lpName);
      502 #else
      503     HMODULE hGWES;
      504  
      505     switch (identifier)
      506     {
      507     default:
      508     case UGL_IMAGE_ERROR:
      509         lpName = MAKEINTRESOURCE(MB_ICONERROR);
      510         break;
      511  
      512     case UGL_IMAGE_INFORMATION:
      513         lpName = MAKEINTRESOURCE(MB_ICONINFORMATION);
      514         break;
      515  
      516     case UGL_IMAGE_QUESTION:
      517         lpName = MAKEINTRESOURCE(MB_ICONQUESTION);
      518         break;
      519  
      520     case UGL_IMAGE_WARNING:
      521         lpName = MAKEINTRESOURCE(MB_ICONWARNING);
      522         break;
      523     }
      524  
      525     hGWES = LoadLibraryEx(L"gwes.exe", NULL, LOAD_LIBRARY_AS_DATAFILE);
      526     hIcon = LoadIcon(hGWES, lpName);
      527 #endif
      528  
      529     if (hIcon == NULL)
      530     {
      531         UGL_SetError(uglError, UGL_ERROR_INVALID_IMAGE);
      532         return NULL_HANDLE;
      533     }
      534     else
      535     {
      536         // Though it defines an ICON constant as well as BITMAP, and tracks the type,
      537         // the Image class only implements BITMAP support, so we must convert.
      538         handle = Image_NewFromSize(deviceHandle, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON), 0x00FFFFFF, uglError);
      539         ugl_image = UGL_IMAGE(handle);
      540         hDC = CreateCompatibleDC(NULL);
      541         hOldBitmap = (HBITMAP)SelectObject(hDC, ugl_image->image_handle);
      542         DrawIcon(hDC, 0, 0, hIcon);
      543         SelectObject(hDC, hOldBitmap);
      544         DeleteDC(hDC);
      545         return handle;
      546     }
      547 }
      548 //========================================================================================
      549  
467 UGL_Int Image_NewGraphics(UGL_Int handle, UGL_Error uglError) { = 550 UGL_Int Image_NewGraphics(UGL_Int handle, UGL_Error uglError) {
468     PUGL_Image ugl_image = UGL_IMAGE(handle);   551     PUGL_Image ugl_image = UGL_IMAGE(handle);
469     PUGL_DC ugl_dc = NULL;   552     PUGL_DC ugl_dc = NULL;
470     HBITMAP dummyBitmap;   553     HBITMAP dummyBitmap;
471     554  
472     HDC imageHDC = CreateCompatibleDC(NULL);   555     HDC imageHDC = CreateCompatibleDC(NULL);
473     RECT minClip;   556     RECT minClip;
474     557  
475     dummyBitmap = CreateCompatibleBitmap(imageHDC, 0, 0);   558     dummyBitmap = CreateCompatibleBitmap(imageHDC, 0, 0);
476     ugl_image->null_bitmap = (HBITMAP)SelectObject(imageHDC, dummyBitmap);   559     ugl_image->null_bitmap = (HBITMAP)SelectObject(imageHDC, dummyBitmap);
477     DeleteObject(dummyBitmap);   560     DeleteObject(dummyBitmap);
478     561  
479     {   562     {
480         int x,y,width,height;   563         int x,y,width,height;
481         Image_GetBounds(handle, &x, &y, &width, &height, uglError);   564         Image_GetBounds(handle, &x, &y, &width, &height, uglError);
482     565  
483         minClip.top = y;   566         minClip.top = y;
484         minClip.left = x;   567         minClip.left = x;
485         minClip.right = x+width;   568         minClip.right = x+width;
486         minClip.bottom = y+height;   569         minClip.bottom = y+height;
487     }   570     }
488     571  
489     ugl_dc = _internal_new_DC(imageHDC, ugl_image, &minClip, uglError);   572     ugl_dc = _internal_new_DC(imageHDC, ugl_image, &minClip, uglError);
490     573  
491     return (UGL_Int)ugl_dc;   574     return (UGL_Int)ugl_dc;
492 }   575 }
493     576  
494 //========================================================================================   577 //========================================================================================
495     578  
496 HANDLE _createBMI(int width, int height, int depth, int paletteSize) {   579 HANDLE _createBMI(int width, int height, int depth, int paletteSize) {
497     BITMAPINFO *pbmi;   580     BITMAPINFO *pbmi;
498     581  
499     pbmi = (BITMAPINFO *)calloc(sizeof(BITMAPINFOHEADER) + paletteSize * sizeof(RGBQUAD), 1);   582     pbmi = (BITMAPINFO *)calloc(sizeof(BITMAPINFOHEADER) + paletteSize * sizeof(RGBQUAD), 1);
500     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);   583     pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
501     pbmi->bmiHeader.biWidth = width;   584     pbmi->bmiHeader.biWidth = width;
502     pbmi->bmiHeader.biHeight = -height;   585     pbmi->bmiHeader.biHeight = -height;
503     pbmi->bmiHeader.biPlanes = 1;   586     pbmi->bmiHeader.biPlanes = 1;
504     pbmi->bmiHeader.biBitCount = depth;   587     pbmi->bmiHeader.biBitCount = depth;
505     pbmi->bmiHeader.biCompression = BI_RGB;   588     pbmi->bmiHeader.biCompression = BI_RGB;
506     pbmi->bmiHeader.biClrUsed = paletteSize;   589     pbmi->bmiHeader.biClrUsed = paletteSize;
507     590  
508     return pbmi;   591     return pbmi;
509 }   592 }
510     593  
511 //========================================================================================   594 //========================================================================================
512     595  
513     596  
514 //========================================================================================   597 //========================================================================================
515     598  
516 void Image_CopyArea(UGL_Int handle, UGL_Int destImageHandle, UGL_Int x, UGL_Int y, UGL_Int width, UGL_Int height, UGL_Error uglError)  {   599 void Image_CopyArea(UGL_Int handle, UGL_Int destImageHandle, UGL_Int x, UGL_Int y, UGL_Int width, UGL_Int height, UGL_Error uglError)  {
517     PUGL_Image ugl_image = UGL_IMAGE(handle);   600     PUGL_Image ugl_image = UGL_IMAGE(handle);
518     PUGL_Image ugl_dest_image = UGL_IMAGE(destImageHandle);   601     PUGL_Image ugl_dest_image = UGL_IMAGE(destImageHandle);
519     PUGL_DC ugl_dest_dc = (PUGL_DC)Image_NewGraphics(destImageHandle, uglError);   602     PUGL_DC ugl_dest_dc = (PUGL_DC)Image_NewGraphics(destImageHandle, uglError);
520     603  
521     Graphics_DrawImageRegion((UGL_Int)ugl_dest_dc, (UGL_Int)ugl_image, x, y, width, height, 0, 0, width, height, uglError);   604     Graphics_DrawImageRegion((UGL_Int)ugl_dest_dc, (UGL_Int)ugl_image, x, y, width, height, 0, 0, width, height, uglError);
522     Graphics_Dispose((UGL_Int)ugl_dest_dc, uglError);   605     Graphics_Dispose((UGL_Int)ugl_dest_dc, uglError);
523 }   606 }
524 //========================================================================================   607 //========================================================================================
525     608  
526 UGL_Int Image_Flip(UGL_Int image_handle, UGL_Boolean hFlip, UGL_Boolean vFlip, UGL_Int x, UGL_Int y, UGL_Int width, UGL_Int height, UGL_Error uglError) {   609 UGL_Int Image_Flip(UGL_Int image_handle, UGL_Boolean hFlip, UGL_Boolean vFlip, UGL_Int x, UGL_Int y, UGL_Int width, UGL_Int height, UGL_Error uglError) {
527     PUGL_Image ugl_image = UGL_IMAGE(image_handle);   610     PUGL_Image ugl_image = UGL_IMAGE(image_handle);
528     PUGL_Image ugl_subimage = NULL;   611     PUGL_Image ugl_subimage = NULL;
529     612  
530     HDC srcHdc, subHdc;   613     HDC srcHdc, subHdc;
531     HBITMAP oldSubBitmap, oldSrcBitmap;   614     HBITMAP oldSubBitmap, oldSrcBitmap;
532     615  
533     int destX, destY, destWidth, destHeight;   616     int destX, destY, destWidth, destHeight;
534     617  
535     // create a memory DC   618     // create a memory DC
536     srcHdc = CreateCompatibleDC(NULL);   619     srcHdc = CreateCompatibleDC(NULL);
537     // select the image into the DC   620     // select the image into the DC
538     oldSrcBitmap = SelectObject(srcHdc, ugl_image->image_handle);   621     oldSrcBitmap = SelectObject(srcHdc, ugl_image->image_handle);
539     622  
540     // create the subimage the size of the area to flip   623     // create the subimage the size of the area to flip
541     ugl_subimage = UGL_IMAGE(Image_NewFromSize(NULL_HANDLE, width, height, 0xFF000000, uglError));   624     ugl_subimage = UGL_IMAGE(Image_NewFromSize(NULL_HANDLE, width, height, 0xFF000000, uglError));
542     // save all of the transparency and alpha information from the original image   625     // save all of the transparency and alpha information from the original image
543     ugl_subimage->transparent_color = ugl_image->transparent_color;   626     ugl_subimage->transparent_color = ugl_image->transparent_color;
544     ugl_subimage->transparent_pixel = ugl_image->transparent_pixel;   627     ugl_subimage->transparent_pixel = ugl_image->transparent_pixel;
545     628  
546     // create another memory DC   629     // create another memory DC
547     subHdc = CreateCompatibleDC(NULL);   630     subHdc = CreateCompatibleDC(NULL);
548     // select the new subimage bitmap into it   631     // select the new subimage bitmap into it
549     oldSubBitmap = SelectObject(subHdc, ugl_subimage->image_handle);   632     oldSubBitmap = SelectObject(subHdc, ugl_subimage->image_handle);
550     633  
551     destWidth = width;   634     destWidth = width;
552     destX = 0;   635     destX = 0;
553     if (hFlip) {   636     if (hFlip) {
554         // indicate image rotates around x-axis   637         // indicate image rotates around x-axis
555         destWidth = -width;   638         destWidth = -width;
556         // rotate the x coordinate also   639         // rotate the x coordinate also
557         destX = width - 1;   640         destX = width - 1;
558     }   641     }
559     642  
560     destHeight = height;   643     destHeight = height;
561     destY = 0;   644     destY = 0;
562     if (vFlip) {   645     if (vFlip) {
563         // indicate image rotates around y-axis   646         // indicate image rotates around y-axis
564         destHeight = -height;   647         destHeight = -height;
565         // rotate the y coordinate also   648         // rotate the y coordinate also
566         destY = height -1 ;   649         destY = height -1 ;
567     }   650     }
568     651  
569     // copy described portion of the original image into the new subimage   652     // copy described portion of the original image into the new subimage
570     // rotating as defined by the destination.   653     // rotating as defined by the destination.
571     StretchBlt(subHdc, destX, destY, destWidth, destHeight, srcHdc, x, y, width, height, SRCCOPY);   654     StretchBlt(subHdc, destX, destY, destWidth, destHeight, srcHdc, x, y, width, height, SRCCOPY);
572     655  
573     // free resources   656     // free resources
574     SelectObject(srcHdc, oldSrcBitmap);   657     SelectObject(srcHdc, oldSrcBitmap);
575     DeleteDC(srcHdc);   658     DeleteDC(srcHdc);
576     SelectObject(subHdc, oldSubBitmap);   659     SelectObject(subHdc, oldSubBitmap);
577     DeleteDC(subHdc);   660     DeleteDC(subHdc);
578     661  
579     return (UGL_Int)ugl_subimage;   662     return (UGL_Int)ugl_subimage;
580 }   663 }
581 //========================================================================================   664 //========================================================================================
582     665  
583 void Image_GetDirectPaletteMasks(UGL_Int handle, UGL_Int* returnRedMask, UGL_Int* returnGreenMask, UGL_Int* returnBlueMask, UGL_Int* returnAlphaMask, UGL_Error uglError) {   666 void Image_GetDirectPaletteMasks(UGL_Int handle, UGL_Int* returnRedMask, UGL_Int* returnGreenMask, UGL_Int* returnBlueMask, UGL_Int* returnAlphaMask, UGL_Error uglError) {
584     PUGL_Image ugl_image = UGL_IMAGE(handle);   667     PUGL_Image ugl_image = UGL_IMAGE(handle);
585     int depth = Image_GetDepth(handle, uglError);   668     int depth = Image_GetDepth(handle, uglError);
586     669  
587     /*   670     /*
588      * Windows supports 16-bit mask of 5-5-5 (0x7C00, 0x3E0, 0x1F),   671      * Windows supports 16-bit mask of 5-5-5 (0x7C00, 0x3E0, 0x1F),
589      * 16-bit 5-6-5 (0xF800, 0x07E0, 0x001F) (only using BI_BITFIELDS),   672      * 16-bit 5-6-5 (0xF800, 0x07E0, 0x001F) (only using BI_BITFIELDS),
590      * 24-bit mask of (0xFF0000, 0xFF00, 0xFF) and 32-bit mask   673      * 24-bit mask of (0xFF0000, 0xFF00, 0xFF) and 32-bit mask
591      * (0x00FF0000, 0x0000FF00, 0x000000FF) as documented in   674      * (0x00FF0000, 0x0000FF00, 0x000000FF) as documented in
592      * MSDN BITMAPINFOHEADER.  Make sure the image is   675      * MSDN BITMAPINFOHEADER.  Make sure the image is
593      * Windows-supported.   676      * Windows-supported.
594      */   677      */
595     678  
596     switch (depth) {   679     switch (depth) {
597         case 32: {   680         case 32: {
598             *returnRedMask = 0xFF0000;   681             *returnRedMask = 0xFF0000;
599             *returnGreenMask = 0xFF00;   682             *returnGreenMask = 0xFF00;
600             *returnBlueMask = 0xFF;   683             *returnBlueMask = 0xFF;
601             *returnAlphaMask = 0xFF000000;   684             *returnAlphaMask = 0xFF000000;
602                  } break;   685                  } break;
603         case 24: {   686         case 24: {
604             *returnRedMask = 0xFF0000;   687             *returnRedMask = 0xFF0000;
605             *returnGreenMask = 0xFF00;   688             *returnGreenMask = 0xFF00;
606             *returnBlueMask = 0xFF;   689             *returnBlueMask = 0xFF;
607             *returnAlphaMask = 0;   690             *returnAlphaMask = 0;
608                  } break;   691                  } break;
609         case 16: {   692         case 16: {
610             // 5-5-5 for now, must make 5-6-5 eventually   693             // 5-5-5 for now, must make 5-6-5 eventually
611             *returnRedMask = 0x7C00;   694             *returnRedMask = 0x7C00;
612             *returnGreenMask = 0x3E0;   695             *returnGreenMask = 0x3E0;
613             *returnBlueMask = 0x1F;   696             *returnBlueMask = 0x1F;
614             *returnAlphaMask = 0;   697             *returnAlphaMask = 0;
615     698  
616             // 5-6-5   699             // 5-6-5
617             //*returnRedMask = 0xF800;   700             //*returnRedMask = 0xF800;
618             //*returnGreenMask = 0x07E0;   701             //*returnGreenMask = 0x07E0;
619             //*returnBlueMask = 0x001F;   702             //*returnBlueMask = 0x001F;
620                  } break;   703                  } break;
621     704  
622         default: {   705         default: {
623             *returnRedMask = 0;   706             *returnRedMask = 0;
624             *returnGreenMask = 0;   707             *returnGreenMask = 0;
625             *returnBlueMask = 0;   708             *returnBlueMask = 0;
626             *returnAlphaMask = 0;   709             *returnAlphaMask = 0;
627     710  
628     711  
629                  } break;   712                  } break;
630     }   713     }
631     714  
632 }   715 }
633 //========================================================================================   716 //========================================================================================
634     717  
635 void Image_GetIndexedPalette(UGL_Int handle, UGL_ByteArray red, UGL_ByteArray green, UGL_ByteArray blue, UGL_Error uglError) {   718 void Image_GetIndexedPalette(UGL_Int handle, UGL_ByteArray red, UGL_ByteArray green, UGL_ByteArray blue, UGL_Error uglError) {
636     PUGL_Image ugl_image = UGL_IMAGE(handle);   719     PUGL_Image ugl_image = UGL_IMAGE(handle);
637     HDC imageHDC;   720     HDC imageHDC;
638     HGDIOBJ oldBitmap;   721     HGDIOBJ oldBitmap;
639     int depth = 0;   722     int depth = 0;
640     int paletteSize, index = 0;   723     int paletteSize, index = 0;
641     LPRGBQUAD palette = NULL;   724     LPRGBQUAD palette = NULL;
642     DIBSECTION ds;   725     DIBSECTION ds;
643     726  
644     727  
645     if (Image_IsDirect(handle, uglError))   {   728     if (Image_IsDirect(handle, uglError))   {
646         Win32Error_SetError(uglError, UGL_ERROR_OTHER, _T("Indexed palette not available for direct color image"));   729         Win32Error_SetError(uglError, UGL_ERROR_OTHER, _T("Indexed palette not available for direct color image"));
647         return;   730         return;
648     }   731     }
649     732  
650     // determine size of palette so we don't try to get more colors than we are using   733     // determine size of palette so we don't try to get more colors than we are using
651     GetObject(ugl_image->image_handle, sizeof(DIBSECTION), &ds);   734     GetObject(ugl_image->image_handle, sizeof(DIBSECTION), &ds);
652     paletteSize = ds.dsBmih.biClrUsed;   735     paletteSize = ds.dsBmih.biClrUsed;
653     if (paletteSize == 0) {   736     if (paletteSize == 0) {
654         // if biClrUsed equals 0, maximum palette for bit count is used   737         // if biClrUsed equals 0, maximum palette for bit count is used
655         paletteSize = 1 << ds.dsBm.bmBitsPixel;   738         paletteSize = 1 << ds.dsBm.bmBitsPixel;
656     }   739     }
657     740  
658     palette = (LPRGBQUAD)calloc(sizeof(RGBQUAD), paletteSize);   741     palette = (LPRGBQUAD)calloc(sizeof(RGBQUAD), paletteSize);
659       742    
660     EnterCriticalSection(&(ugl_image->dcSection));   743     EnterCriticalSection(&(ugl_image->dcSection));
661     if (ugl_image->selected_DC == NULL) {   744     if (ugl_image->selected_DC == NULL) {
662         imageHDC = CreateCompatibleDC(NULL);   745         imageHDC = CreateCompatibleDC(NULL);
663         oldBitmap = SelectObject(imageHDC, ugl_image->image_handle);   746         oldBitmap = SelectObject(imageHDC, ugl_image->image_handle);
664     } else {   747     } else {
665         imageHDC = ugl_image->selected_DC;   748         imageHDC = ugl_image->selected_DC;
666     }   749     }
667     750  
668     if (!GetDIBColorTable(imageHDC, 0, paletteSize, palette)) {   751     if (!GetDIBColorTable(imageHDC, 0, paletteSize, palette)) {
669         Win32Error_SetError(uglError, UGL_ERROR_OTHER, _T("Unable to get DIB color table"));   752         Win32Error_SetError(uglError, UGL_ERROR_OTHER, _T("Unable to get DIB color table"));
670     } else {   753     } else {
671         for (index = 0; index < paletteSize; index++) {   754         for (index = 0; index < paletteSize; index++) {
672             LPRGBQUAD palette_entry = (LPRGBQUAD)&palette[index];   755             LPRGBQUAD palette_entry = (LPRGBQUAD)&palette[index];
673     756  
674             red[index] = palette_entry->rgbRed;   757             red[index] = palette_entry->rgbRed;
675             green[index] = palette_entry->rgbGreen;   758             green[index] = palette_entry->rgbGreen;
676             blue[index] = palette_entry->rgbBlue;   759             blue[index] = palette_entry->rgbBlue;
677         }   760         }
678     }   761     }
679     762  
680     free(palette);   763     free(palette);
681     if (ugl_image->selected_DC == NULL) {   764     if (ugl_image->selected_DC == NULL) {
682         SelectObject(imageHDC, oldBitmap);   765         SelectObject(imageHDC, oldBitmap);
683         DeleteDC(imageHDC);   766         DeleteDC(imageHDC);
684     }   767     }
685     768  
686     LeaveCriticalSection(&(ugl_image->dcSection));   769     LeaveCriticalSection(&(ugl_image->dcSection));
687 }   770 }
688 //========================================================================================   771 //========================================================================================
689     772  
690 UGL_Int Image_NewFromHandle(UGL_Int deviceHandle, UGL_Int handle, UGL_Error uglError){   773 UGL_Int Image_NewFromHandle(UGL_Int deviceHandle, UGL_Int handle, UGL_Error uglError){
691   774
692     PUGL_Image ugl_image = NULL;   775     PUGL_Image ugl_image = NULL;
693     PUGL_Image originalImage = UGL_IMAGE(handle);   776     PUGL_Image originalImage = UGL_IMAGE(handle);
694     HBITMAP hBitmap = originalImage->image_handle;   777     HBITMAP hBitmap = originalImage->image_handle;
695     HBITMAP hBitmapNew, hbmSrcOld;   778     HBITMAP hBitmapNew, hbmSrcOld;
696     DIBSECTION ds;   779     DIBSECTION ds;
697     HDC hdcSrc;   780     HDC hdcSrc;
698     781  
699     int paletteSize, index;   782     int paletteSize, index;
700     BITMAPINFO *pBmi;   783     BITMAPINFO *pBmi;
701     LPBYTE *pBits;   784     LPBYTE *pBits;
702     785  
703     // there exists a DC that has this image selected, temporarily disable it   786     // there exists a DC that has this image selected, temporarily disable it
704     EnterCriticalSection(&(originalImage->dcSection));   787     EnterCriticalSection(&(originalImage->dcSection));
705     if (originalImage->selected_DC != NULL) {   788     if (originalImage->selected_DC != NULL) {
706         SelectObject(originalImage->selected_DC, originalImage->null_bitmap);   789         SelectObject(originalImage->selected_DC, originalImage->null_bitmap);
707     }   790     }
708     // get information about the bitmap   791     // get information about the bitmap
709     hdcSrc = CreateCompatibleDC(NULL);   792     hdcSrc = CreateCompatibleDC(NULL);
710     hbmSrcOld = SelectObject(hdcSrc, hBitmap);   793     hbmSrcOld = SelectObject(hdcSrc, hBitmap);
711     GetObject(hBitmap, sizeof(DIBSECTION), &ds);   794     GetObject(hBitmap, sizeof(DIBSECTION), &ds);
712     795  
713     // create a palette only for images which are not direct   796     // create a palette only for images which are not direct
714     paletteSize = ds.dsBmih.biClrUsed;   797     paletteSize = ds.dsBmih.biClrUsed;
715     if (!originalImage->is_direct && paletteSize == 0) {   798     if (!originalImage->is_direct && paletteSize == 0) {
716         // if biClrUsed equals 0, maximum palette for bit count is used   799         // if biClrUsed equals 0, maximum palette for bit count is used
717         paletteSize = 1 << ds.dsBm.bmBitsPixel;   800         paletteSize = 1 << ds.dsBm.bmBitsPixel;
718     }   801     }
719     pBmi = _createBMI(ds.dsBm.bmWidth, ds.dsBm.bmHeight, ds.dsBm.bmBitsPixel, paletteSize);   802     pBmi = _createBMI(ds.dsBm.bmWidth, ds.dsBm.bmHeight, ds.dsBm.bmBitsPixel, paletteSize);
720     803  
721     if (!originalImage->is_direct) {   804     if (!originalImage->is_direct) {
722         // for bitmaps which use indexed palettes we must copy the color   805         // for bitmaps which use indexed palettes we must copy the color
723         // table from the original bitmap into the new one. In win32 and   806         // table from the original bitmap into the new one. In win32 and
724         // newer versions of wince you can get the color table for DIBs with   807         // newer versions of wince you can get the color table for DIBs with
725         // GetDIBColorTable.   808         // GetDIBColorTable.
726         RGBQUAD *pBmiColors = (LPRGBQUAD)(&pBmi->bmiColors);   809         RGBQUAD *pBmiColors = (LPRGBQUAD)(&pBmi->bmiColors);
727         LPBYTE pBitsOrig = ds.dsBm.bmBits;   810         LPBYTE pBitsOrig = ds.dsBm.bmBits;
728         BYTE pBitsOld = pBitsOrig[0];   811         BYTE pBitsOld = pBitsOrig[0];
729         for (index = 0; index < paletteSize; index++) {   812         for (index = 0; index < paletteSize; index++) {
730             // iterate through the entire color table. Set byte 0 of the   813             // iterate through the entire color table. Set byte 0 of the
731             // image data to point to the index in the palette, and then   814             // image data to point to the index in the palette, and then
732             // get the color that is pointed to by that index.   815             // get the color that is pointed to by that index.
733             COLORREF rgbColor;   816             COLORREF rgbColor;
734             pBitsOrig[0] = (BYTE) index & 0xff;   817             pBitsOrig[0] = (BYTE) index & 0xff;
735             rgbColor = GetPixel(hdcSrc, 0, 0);   818             rgbColor = GetPixel(hdcSrc, 0, 0);
736             // store the color into the palette for the new bitmap   819             // store the color into the palette for the new bitmap
737             pBmiColors[index].rgbBlue = GetBValue(rgbColor);   820             pBmiColors[index].rgbBlue = GetBValue(rgbColor);
738             pBmiColors[index].rgbGreen = GetGValue(rgbColor);   821             pBmiColors[index].rgbGreen = GetGValue(rgbColor);
739             pBmiColors[index].rgbRed = GetRValue(rgbColor);   822             pBmiColors[index].rgbRed = GetRValue(rgbColor);
740             pBmiColors[index].rgbReserved = 0;   823             pBmiColors[index].rgbReserved = 0;
741         }   824         }
742         // restore byte 0 back to its original value   825         // restore byte 0 back to its original value
743         pBitsOrig[0] = pBitsOld;   826         pBitsOrig[0] = pBitsOld;
744     }   827     }
745     828  
746     // create the dib to match our original image   829     // create the dib to match our original image
747     hBitmapNew = CreateDIBSection(NULL, pBmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0x0);   830     hBitmapNew = CreateDIBSection(NULL, pBmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0x0);
748     if (hBitmapNew == 0) {   831     if (hBitmapNew == 0) {
749         free(pBmi);   832         free(pBmi);
750         Win32Error_SetError(uglError, GetLastError(), _T("Unable to create device-independent image handle"));   833         Win32Error_SetError(uglError, GetLastError(), _T("Unable to create device-independent image handle"));
751         if (originalImage->selected_DC != NULL) {   834         if (originalImage->selected_DC != NULL) {
752             SelectObject(originalImage->selected_DC, originalImage->image_handle);   835             SelectObject(originalImage->selected_DC, originalImage->image_handle);
753         }   836         }
754         LeaveCriticalSection(&(originalImage->dcSection));   837         LeaveCriticalSection(&(originalImage->dcSection));
755         return NULL_HANDLE;   838         return NULL_HANDLE;
756     }   839     }
757     840  
758     // copy the image data bits to the new bitmap   841     // copy the image data bits to the new bitmap
759     MoveMemory(pBits, ds.dsBm.bmBits, ds.dsBm.bmWidthBytes * ds.dsBm.bmHeight);   842     MoveMemory(pBits, ds.dsBm.bmBits, ds.dsBm.bmWidthBytes * ds.dsBm.bmHeight);
760     843  
761     // release the memory   844     // release the memory
762     free(pBmi);   845     free(pBmi);
763     SelectObject(hdcSrc, hbmSrcOld);   846     SelectObject(hdcSrc, hbmSrcOld);
764     DeleteDC(hdcSrc);   847     DeleteDC(hdcSrc);
765     if (originalImage->selected_DC != NULL) {   848     if (originalImage->selected_DC != NULL) {
766         SelectObject(originalImage->selected_DC, originalImage->image_handle);   849         SelectObject(originalImage->selected_DC, originalImage->image_handle);
767     }   850     }
768     LeaveCriticalSection(&(originalImage->dcSection));   851     LeaveCriticalSection(&(originalImage->dcSection));
769     852  
770     return (UGL_Int)_Image_New(hBitmapNew, originalImage->is_direct, originalImage->transparent_pixel, originalImage->transparent_color, uglError);   853     return (UGL_Int)_Image_New(hBitmapNew, originalImage->is_direct, originalImage->transparent_pixel, originalImage->transparent_color, uglError);
771 }   854 }
772 //========================================================================================   855 //========================================================================================
773     856  
774 UGL_Int Image_NewFromFile(UGL_Int deviceHandle, UGL_String fileName, UGL_Error uglError) {   857 UGL_Int Image_NewFromFile(UGL_Int deviceHandle, UGL_String fileName, UGL_Error uglError) {
775     BOOL direct = TRUE;   858     BOOL direct = TRUE;
776     BOOL hasAlpha = FALSE;   859     BOOL hasAlpha = FALSE;
777     int transparentPixel = -1;   860     int transparentPixel = -1;
778     int transparentColor = -1;   861     int transparentColor = -1;
779     HBITMAP imageHandle;   862     HBITMAP imageHandle;
780     863  
781 #ifndef _WIN32_WCE   864 #ifndef _WIN32_WCE
782     imageHandle = GDIpImageLoader_LoadFromFile(fileName, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);   865     imageHandle = GDIpImageLoader_LoadFromFile(fileName, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);
783     if ((UGL_GetError(uglError) != 0) && (UGL_GetError(uglError) != UGL_ERROR_INVALID_IMAGE)) {   866     if ((UGL_GetError(uglError) != 0) && (UGL_GetError(uglError) != UGL_ERROR_INVALID_IMAGE)) {
784         BOOL direct = TRUE;   867         BOOL direct = TRUE;
785         BOOL hasAlpha = FALSE;   868         BOOL hasAlpha = FALSE;
786         int transparentPixel = -1;   869         int transparentPixel = -1;
787         int transparentColor = -1;   870         int transparentColor = -1;
788           871        
789         UGL_ClearError(uglError);   872         UGL_ClearError(uglError);
790         imageHandle = DXImageLoader_LoadFromFile(fileName, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);   873         imageHandle = DXImageLoader_LoadFromFile(fileName, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);
791         if (UGL_GetError(uglError) != 0) {   874         if (UGL_GetError(uglError) != 0) {
792             return NULL_HANDLE;   875             return NULL_HANDLE;
793         }   876         }
794     }   877     }
795 #else   878 #else
796     imageHandle = ImagingImageLoader_LoadFromFile(fileName, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);   879     imageHandle = ImagingImageLoader_LoadFromFile(fileName, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);
797     if (imageHandle == NULL_HANDLE) return NULL_HANDLE;   880     if (imageHandle == NULL_HANDLE) return NULL_HANDLE;
798     881  
799 //  imageHandle = ExternalImageLoader_LoadFromFile(fileName, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);   882 //  imageHandle = ExternalImageLoader_LoadFromFile(fileName, &direct, &hasAlpha, &transparentPixel, &transparentColor, uglError);
800 //  if (imageHandle == NULL_HANDLE) return NULL_HANDLE;   883 //  if (imageHandle == NULL_HANDLE) return NULL_HANDLE;
801 #endif   884 #endif
802     885  
803     return (UGL_Int)_Image_New(imageHandle, direct, transparentPixel, transparentColor, uglError);   886     return (UGL_Int)_Image_New(imageHandle, direct, transparentPixel, transparentColor, uglError);
804 }   887 }
805 //========================================================================================   888 //========================================================================================
806     889  
807 void Image_SetRGB(UGL_Int handle, UGL_Int x, UGL_Int y, UGL_Int argb, UGL_Error uglError) {   890 void Image_SetRGB(UGL_Int handle, UGL_Int x, UGL_Int y, UGL_Int argb, UGL_Error uglError) {
808     PUGL_Image ugl_image = UGL_IMAGE(handle);   891     PUGL_Image ugl_image = UGL_IMAGE(handle);
809     892  
810     BITMAP bitmap;   893     BITMAP bitmap;
811     LPBYTE pvBits;   894     LPBYTE pvBits;
812     int index;   895     int index;
813     896  
814     int size = GetObject(ugl_image->image_handle, sizeof(BITMAP), &bitmap);   897     int size = GetObject(ugl_image->image_handle, sizeof(BITMAP), &bitmap);
815     898  
816     pvBits = (LPBYTE)bitmap.bmBits;   899     pvBits = (LPBYTE)bitmap.bmBits;
817     index = (x*3) + y * bitmap.bmWidthBytes;   900     index = (x*3) + y * bitmap.bmWidthBytes;
818     *(pvBits + index) = (argb >> 0) & 0xFF;   901     *(pvBits + index) = (argb >> 0) & 0xFF;
819     *(pvBits + index+1) = (argb >> 8) & 0xFF;   902     *(pvBits + index+1) = (argb >> 8) & 0xFF;
820     *(pvBits + index+2) = (argb >> 16) & 0xFF;   903     *(pvBits + index+2) = (argb >> 16) & 0xFF;
821     904  
822     /*   905     /*
823     HDC imageHDC;   906     HDC imageHDC;
824     HGDIOBJ oldBitmap;   907     HGDIOBJ oldBitmap;
825     COLORREF color;   908     COLORREF color;
826     int red, green, blue;   909     int red, green, blue;
827     910  
828     if (ugl_image->selected_DC == NULL) {   911     if (ugl_image->selected_DC == NULL) {
829         HDC hdc = GetDC(NULL);   912         HDC hdc = GetDC(NULL);
830         imageHDC = CreateCompatibleDC(hdc);   913         imageHDC = CreateCompatibleDC(hdc);
831         ReleaseDC(NULL, hdc);   914         ReleaseDC(NULL, hdc);
832         oldBitmap = SelectObject(imageHDC, ugl_image->image_handle);   915         oldBitmap = SelectObject(imageHDC, ugl_image->image_handle);
833     } else {   916     } else {
834         imageHDC = ugl_image->selected_DC->hdc;   917         imageHDC = ugl_image->selected_DC->hdc;
835     }   918     }
836       919    
837     red = (rgb >> 16) & 0xFF;   920     red = (rgb >> 16) & 0xFF;
838     green = (rgb >> 8) & 0xFF;   921     green = (rgb >> 8) & 0xFF;
839     blue = (rgb) & 0xFF;   922     blue = (rgb) & 0xFF;
840     923  
841     color = RGB(red, green, blue);   924     color = RGB(red, green, blue);
842     color = RGB_SWAP(color);   925     color = RGB_SWAP(color);
843     926  
844 #ifdef _WIN32_WCE   927 #ifdef _WIN32_WCE
845     SetPixel(imageHDC, x, y, color);   928     SetPixel(imageHDC, x, y, color);
846 #else   929 #else
847     SetPixelV(imageHDC, x, y, color);   930     SetPixelV(imageHDC, x, y, color);
848 #endif   931 #endif
849     932  
850     if (ugl_image->selected_DC == NULL) {   933     if (ugl_image->selected_DC == NULL) {
851         SelectObject(imageHDC, oldBitmap);   934         SelectObject(imageHDC, oldBitmap);
852         DeleteDC(imageHDC);   935         DeleteDC(imageHDC);
853     }   936     }
854     */   937     */
855 }   938 }
856 //========================================================================================   939 //========================================================================================
857     940  
858 void Image_GetPixelsHighColor(UGL_Int handle, UGL_IntArray pixels, UGL_Int length, UGL_Error uglError) {   941 void Image_GetPixelsHighColor(UGL_Int handle, UGL_IntArray pixels, UGL_Int length, UGL_Error uglError) {
859     PUGL_Image ugl_image = UGL_IMAGE(handle);   942     PUGL_Image ugl_image = UGL_IMAGE(handle);
860     HBITMAP hBitmap = ugl_image->image_handle;   943     HBITMAP hBitmap = ugl_image->image_handle;
861     BITMAP bitmapObject;   944     BITMAP bitmapObject;
862     int depth = 0;   945     int depth = 0;
863     946  
864     LPBYTE buffer = NULL;   947     LPBYTE buffer = NULL;
865     948  
866     /* Get the HDC for the device */   949     /* Get the HDC for the device */
867     950  
868     if (!Image_IsDirect(handle, uglError)) {   951     if (!Image_IsDirect(handle, uglError)) {
869         Win32Error_SetError(uglError, UGL_ERROR_OTHER, _T("Pixels High color invalid on indexed color image"));   952         Win32Error_SetError(uglError, UGL_ERROR_OTHER, _T("Pixels High color invalid on indexed color image"));
870         return;   953         return;
871     }   954     }
872     955  
873     956  
874 //  if (ugl_image->selected_DC != NULL) {   957 //  if (ugl_image->selected_DC != NULL) {
875 //      SelectObject(ugl_image->selected_DC->hdc, ugl_image->null_bitmap);   958 //      SelectObject(ugl_image->selected_DC->hdc, ugl_image->null_bitmap);
876 //  }   959 //  }
877   960
878     GetObject(hBitmap, sizeof(BITMAP), &bitmapObject);   961     GetObject(hBitmap, sizeof(BITMAP), &bitmapObject);
879     depth = bitmapObject.bmPlanes * bitmapObject.bmBitsPixel;   962     depth = bitmapObject.bmPlanes * bitmapObject.bmBitsPixel;
880     963  
881     964  
882 //  if (depth == 32) {   965 //  if (depth == 32) {
883         // for 32-bpp image the pixel data can be copied directly into the allocated pixels   966         // for 32-bpp image the pixel data can be copied directly into the allocated pixels
884         // as this image is not padded in Windows   967         // as this image is not padded in Windows
885 //      buffer = (LPBYTE)pixels;   968 //      buffer = (LPBYTE)pixels;
886 //  } else {   969 //  } else {
887         // allocate space for the pixel data   970         // allocate space for the pixel data
888 //      buffer = (LPBYTE)calloc(sizeof(BYTE), pbmi->bmiHeader.biSizeImage);   971 //      buffer = (LPBYTE)calloc(sizeof(BYTE), pbmi->bmiHeader.biSizeImage);
889         //if (buffer == NULL) {   972         //if (buffer == NULL) {
890             //Win32Error_SetError(uglError, UGL_ERROR_OTHER, _T("Unable to calloc memory"));   973             //Win32Error_SetError(uglError, UGL_ERROR_OTHER, _T("Unable to calloc memory"));
891             //free(pbmi);   974             //free(pbmi);
892             //return;   975             //return;
893         //}   976         //}
894 //  }   977 //  }
895     978  
896     // get the pixel data   979     // get the pixel data
897     buffer = bitmapObject.bmBits;   980     buffer = bitmapObject.bmBits;
898     981  
899     {   982     {
900         // For 24-bpp and 16-bpp images covert the windows pixel data.   983         // For 24-bpp and 16-bpp images covert the windows pixel data.
901         int output_index = 0;   984         int output_index = 0;
902         int buffer_index = 0;   985         int buffer_index = 0;
903     986  
904         for (output_index = 0; output_index < length; output_index++) {   987         for (output_index = 0; output_index < length; output_index++) {
905             UGL_Int *pixel = &pixels[output_index];   988             UGL_Int *pixel = &pixels[output_index];
906             if (depth == 32) {   989             if (depth == 32) {
907                 unsigned long *ppixel_32bit = (unsigned long *)&buffer[buffer_index];   990                 unsigned long *ppixel_32bit = (unsigned long *)&buffer[buffer_index];
908                 *pixel = *ppixel_32bit;   991                 *pixel = *ppixel_32bit;
909 //              printf("target_pixel= %d", *pixel);   992 //              printf("target_pixel= %d", *pixel);
910                 buffer_index += sizeof(unsigned long);   993                 buffer_index += sizeof(unsigned long);
911             }else if (depth == 24) {   994             }else if (depth == 24) {
912                 // For 24-bpp, each 3-byte triplet in the bitmap array represents the   995                 // For 24-bpp, each 3-byte triplet in the bitmap array represents the
913                 //relative intensities of blue, green, and red, respectively, for a pixel.   996                 //relative intensities of blue, green, and red, respectively, for a pixel.
914                 *pixel = (buffer[buffer_index++]);  //blue   997                 *pixel = (buffer[buffer_index++]);  //blue
915                 *pixel |= (buffer[buffer_index++]) << 8;  //green   998                 *pixel |= (buffer[buffer_index++]) << 8;  //green
916                 *pixel |= (buffer[buffer_index++]) << 16;  //red   999                 *pixel |= (buffer[buffer_index++]) << 16;  //red
917             } else {   1000             } else {
918                 // For 16-bpp, each WORD in the bitmap array represents a single pixel.   1001                 // For 16-bpp, each WORD in the bitmap array represents a single pixel.
919                 // The relative intensities of red, green, and blue are represented   1002                 // The relative intensities of red, green, and blue are represented
920                 // with five bits for each color component. The value for blue is in the   1003                 // with five bits for each color component. The value for blue is in the
921                 // least significant five bits, followed by five bits each for green and red.   1004                 // least significant five bits, followed by five bits each for green and red.
922                 // The most significant bit is not used.   1005                 // The most significant bit is not used.
923                 unsigned short *ppixel_16bit = (unsigned short *)&buffer[buffer_index];   1006                 unsigned short *ppixel_16bit = (unsigned short *)&buffer[buffer_index];
924                 int red = ((*ppixel_16bit) & 0x7C00) << 1;   1007                 int red = ((*ppixel_16bit) & 0x7C00) << 1;
925                 int green = ((*ppixel_16bit) & 0x3E0) >> 5;   1008                 int green = ((*ppixel_16bit) & 0x3E0) >> 5;
926                 int blue = (*ppixel_16bit) & 0x1F;   1009                 int blue = (*ppixel_16bit) & 0x1F;
927                 // we only have to convert max green in 16 bit 5-5-5 to the max   1010                 // we only have to convert max green in 16 bit 5-5-5 to the max
928                 // green in 5-6-5.   1011                 // green in 5-6-5.
929                 green = ((green * 0x03F) / 0x01F) << 5;   1012                 green = ((green * 0x03F) / 0x01F) << 5;
930     1013  
931                 *pixel = red | green | blue;   1014                 *pixel = red | green | blue;
932                 buffer_index += sizeof(unsigned short);   1015                 buffer_index += sizeof(unsigned short);
933             }   1016             }
934     1017  
935             if (((output_index + 1) % bitmapObject.bmWidth) == 0){   1018             if (((output_index + 1) % bitmapObject.bmWidth) == 0){
936                 // at the end of one row of pixel data. Each scan   1019                 // at the end of one row of pixel data. Each scan
937                 // line is padded with zeroes to end on a LONG boundary   1020                 // line is padded with zeroes to end on a LONG boundary
938                 int remainder = ((buffer_index) % sizeof(long));   1021                 int remainder = ((buffer_index) % sizeof(long));
939                 if (remainder != 0) {   1022                 if (remainder != 0) {
940                     buffer_index += sizeof(long) - remainder;   1023                     buffer_index += sizeof(long) - remainder;
941                 }   1024                 }
942             }   1025             }
943         }   1026         }
944     }   1027     }
945     1028  
946 //  if (ugl_image->selected_DC != NULL) {   1029 //  if (ugl_image->selected_DC != NULL) {
947 //      SelectObject(ugl_image->selected_DC->hdc, ugl_image->image_handle);   1030 //      SelectObject(ugl_image->selected_DC->hdc, ugl_image->image_handle);
948 //  }   1031 //  }
949 }   1032 }
950 //========================================================================================   1033 //========================================================================================
951     1034  
952 void Image_GetPixelsLowColor(UGL_Int handle, UGL_ByteArray pixels, UGL_Int length, UGL_Error uglError) {   1035 void Image_GetPixelsLowColor(UGL_Int handle, UGL_ByteArray pixels, UGL_Int length, UGL_Error uglError) {
953     PUGL_Image ugl_image = UGL_IMAGE(handle);   1036     PUGL_Image ugl_image = UGL_IMAGE(handle);
954     HBITMAP hBitmap = ugl_image->image_handle;   1037     HBITMAP hBitmap = ugl_image->image_handle;
955     BITMAP bitmapObject;   1038     BITMAP bitmapObject;
956     1039  
957     LPBYTE buffer = NULL;   1040     LPBYTE buffer = NULL;
958     1041  
959     if (Image_IsDirect(handle, uglError)) {   1042     if (Image_IsDirect(handle, uglError)) {
960         Win32Error_SetError(uglError, UGL_ERROR_OTHER, _T("Pixels Low color invalid on direct color image"));   1043         Win32Error_SetError(uglError, UGL_ERROR_OTHER, _T("Pixels Low color invalid on direct color image"));
961         return;   1044         return;
962     }   1045     }
963     1046  
964 //  if (ugl_image->selected_DC != NULL) {   1047 //  if (ugl_image->selected_DC != NULL) {
965 //      SelectObject(ugl_image->selected_DC->hdc, ugl_image->null_bitmap);   1048 //      SelectObject(ugl_image->selected_DC->hdc, ugl_image->null_bitmap);
966 //  }   1049 //  }
967   1050
968     GetObject(hBitmap, sizeof(BITMAP), &bitmapObject);   1051     GetObject(hBitmap, sizeof(BITMAP), &bitmapObject);
969     1052  
970     // get the pixel data from the bitmap   1053     // get the pixel data from the bitmap
971     buffer = bitmapObject.bmBits;   1054     buffer = bitmapObject.bmBits;
972     1055  
973     // destination, source, length   1056     // destination, source, length
974     MoveMemory(pixels, &buffer[0], length);   1057     MoveMemory(pixels, &buffer[0], length);
975     1058  
976 //  if (ugl_image->selected_DC != NULL) {   1059 //  if (ugl_image->selected_DC != NULL) {
977 //      SelectObject(ugl_image->selected_DC->hdc, ugl_image->image_handle);   1060 //      SelectObject(ugl_image->selected_DC->hdc, ugl_image->image_handle);
978 //  }   1061 //  }
979 }   1062 }
980 //========================================================================================   1063 //========================================================================================
981     1064  
982 UGL_Int Image_GetTransparentColor(UGL_Int handle, UGL_Error uglError) {   1065 UGL_Int Image_GetTransparentColor(UGL_Int handle, UGL_Error uglError) {
983     PUGL_Image ugl_image = UGL_IMAGE(handle);   1066     PUGL_Image ugl_image = UGL_IMAGE(handle);
984     1067  
985     if (ugl_image->is_direct)   1068     if (ugl_image->is_direct)
986         return ugl_image->transparent_color;   1069         return ugl_image->transparent_color;
987     // when the image in indexed, the transparent color indicates whether one   1070     // when the image in indexed, the transparent color indicates whether one
988     // of the pixels is transparent   1071     // of the pixels is transparent
989     if (ugl_image->transparent_color == -1)   1072     if (ugl_image->transparent_color == -1)
990         return -1;   1073         return -1;
991     // return the pixel which is transparent for indexed images   1074     // return the pixel which is transparent for indexed images
992     return ugl_image->transparent_pixel;   1075     return ugl_image->transparent_pixel;
993 }   1076 }
994 //========================================================================================   1077 //========================================================================================
995     1078  
996 UGL_Boolean Image_IsDirect(UGL_Int handle, UGL_Error uglError) {   1079 UGL_Boolean Image_IsDirect(UGL_Int handle, UGL_Error uglError) {
997     PUGL_Image ugl_image = UGL_IMAGE(handle);   1080     PUGL_Image ugl_image = UGL_IMAGE(handle);
998     return ugl_image->is_direct;   1081     return ugl_image->is_direct;
999 }   1082 }
1000 //========================================================================================   1083 //========================================================================================
1001 UGL_Int Image_NewFromByteData(UGL_Int deviceHandle, UGL_Int width, UGL_Int height, UGL_ByteArray imageData, UGL_Int bytesPerLine, UGL_Int depth, UGL_ByteArray indexedReds, UGL_ByteArray indexedGreens, UGL_ByteArray indexedBlues, UGL_Int paletteSize, UGL_Int transparentPixel, UGL_Error uglError) {   1084 UGL_Int Image_NewFromByteData(UGL_Int deviceHandle, UGL_Int width, UGL_Int height, UGL_ByteArray imageData, UGL_Int bytesPerLine, UGL_Int depth, UGL_ByteArray indexedReds, UGL_ByteArray indexedGreens, UGL_ByteArray indexedBlues, UGL_Int paletteSize, UGL_Int transparentPixel, UGL_Error uglError) {
1002     PUGL_Image ugl_image = NULL;   1085     PUGL_Image ugl_image = NULL;
1003     void *pBits[1];   1086     void *pBits[1];
1004     LPBYTE buffer;   1087     LPBYTE buffer;
1005     HBITMAP hDib;   1088     HBITMAP hDib;
1006     BITMAPINFO bmi;   1089     BITMAPINFO bmi;
1007     BITMAPINFO *pbmi;   1090     BITMAPINFO *pbmi;
1008     int imageData_length = bytesPerLine * height;   1091     int imageData_length = bytesPerLine * height;
1009     int offset, index, bufferIndex, transparentColor;   1092     int offset, index, bufferIndex, transparentColor;
1010     LPRGBQUAD pcolortable;   1093     LPRGBQUAD pcolortable;
1011     UINT windowsScanline;   1094     UINT windowsScanline;
1012     1095  
1013     /*   1096     /*
1014     * Note on WinCE.  CreateDIBSection requires the biCompression   1097     * Note on WinCE.  CreateDIBSection requires the biCompression
1015     * field of the BITMAPINFOHEADER to be set to BI_BITFIELDS for   1098     * field of the BITMAPINFOHEADER to be set to BI_BITFIELDS for
1016     * 16 and 32 bit direct images (see MSDN for CreateDIBSection).   1099     * 16 and 32 bit direct images (see MSDN for CreateDIBSection).
1017     * In this case, the color mask can be set to any value.  For   1100     * In this case, the color mask can be set to any value.  For
1018     * consistency, it is set to the same mask used by non WinCE   1101     * consistency, it is set to the same mask used by non WinCE
1019     * platforms in BI_RGB mode.   1102     * platforms in BI_RGB mode.
1020     */   1103     */
1021     1104  
1022     /*   1105     /*
1023      * BUG in Windows 98:   1106      * BUG in Windows 98:
1024      * A monochrome DIBSection will display as solid black   1107      * A monochrome DIBSection will display as solid black
1025      * on Windows 98 machines, even though it contains the   1108      * on Windows 98 machines, even though it contains the
1026      * correct data. The fix is to convert 1-bit ImageData   1109      * correct data. The fix is to convert 1-bit ImageData
1027      * into 4-bit ImageData before creating the image.   1110      * into 4-bit ImageData before creating the image.
1028      */   1111      */
1029     1112  
1030     /* Windows does not support 2-bit images. Convert to 4-bit image. */   1113     /* Windows does not support 2-bit images. Convert to 4-bit image. */
1031     1114  
1032     /*   1115     /*
1033     * Note on WinCE.  CreateDIBSection requires the biCompression   1116     * Note on WinCE.  CreateDIBSection requires the biCompression
1034     * field of the BITMAPINFOHEADER to be set to BI_BITFIELDS for   1117     * field of the BITMAPINFOHEADER to be set to BI_BITFIELDS for
1035     * 16 and 32 bit direct images (see MSDN for CreateDIBSection).   1118     * 16 and 32 bit direct images (see MSDN for CreateDIBSection).
1036     * In this case, the color mask can be set to any value.  For   1119     * In this case, the color mask can be set to any value.  For
1037     * consistency, it is set to the same mask used by non WinCE   1120     * consistency, it is set to the same mask used by non WinCE
1038     * platforms in BI_RGB mode.   1121     * platforms in BI_RGB mode.
1039     */   1122     */
1040     memset(&bmi, 0,sizeof(BITMAPINFOHEADER));   1123     memset(&bmi, 0,sizeof(BITMAPINFOHEADER));
1041     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);   1124     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1042     bmi.bmiHeader.biWidth = width;   1125     bmi.bmiHeader.biWidth = width;
1043     bmi.bmiHeader.biHeight = -height;   1126     bmi.bmiHeader.biHeight = -height;
1044     bmi.bmiHeader.biPlanes = 1;   1127     bmi.bmiHeader.biPlanes = 1;
1045     bmi.bmiHeader.biBitCount = depth;   1128     bmi.bmiHeader.biBitCount = depth;
1046     bmi.bmiHeader.biCompression = BI_RGB;   1129     bmi.bmiHeader.biCompression = BI_RGB;
1047     // don't create a palette any larger than necessary   1130     // don't create a palette any larger than necessary
1048     bmi.bmiHeader.biClrUsed = paletteSize;   1131     bmi.bmiHeader.biClrUsed = paletteSize;
1049     pbmi = (BITMAPINFO *)calloc(sizeof(BITMAPINFOHEADER) + (paletteSize * sizeof(RGBQUAD)), 1);   1132     pbmi = (BITMAPINFO *)calloc(sizeof(BITMAPINFOHEADER) + (paletteSize * sizeof(RGBQUAD)), 1);
1050     if (pbmi == NULL) {   1133     if (pbmi == NULL) {
1051         Win32Error_SetError(uglError, UGL_ERROR_OUT_OF_MEMORY, _T("Not enough memory to create image"));   1134         Win32Error_SetError(uglError, UGL_ERROR_OUT_OF_MEMORY, _T("Not enough memory to create image"));
1052         return 0;   1135         return 0;
1053     }   1136     }
1054     MoveMemory((void *)pbmi, (void *)&bmi.bmiHeader, sizeof(BITMAPINFOHEADER));   1137     MoveMemory((void *)pbmi, (void *)&bmi.bmiHeader, sizeof(BITMAPINFOHEADER));
1055     1138  
1056     offset = sizeof(BITMAPINFOHEADER);   1139     offset = sizeof(BITMAPINFOHEADER);
1057     pcolortable = (LPRGBQUAD)(&pbmi->bmiColors);   1140     pcolortable = (LPRGBQUAD)(&pbmi->bmiColors);
1058     1141  
1059     for (index = 0; index < paletteSize; index++) {   1142     for (index = 0; index < paletteSize; index++) {
1060         pcolortable[index].rgbRed = indexedReds[index];   1143         pcolortable[index].rgbRed = indexedReds[index];
1061         pcolortable[index].rgbBlue = indexedBlues[index];   1144         pcolortable[index].rgbBlue = indexedBlues[index];
1062         pcolortable[index].rgbGreen = indexedGreens[index];   1145         pcolortable[index].rgbGreen = indexedGreens[index];
1063         pcolortable[index].rgbReserved = 0;   1146         pcolortable[index].rgbReserved = 0;
1064     }   1147     }
1065     1148  
1066     hDib = CreateDIBSection(NULL, (CONST BITMAPINFO *)pbmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0x0);   1149     hDib = CreateDIBSection(NULL, (CONST BITMAPINFO *)pbmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0x0);
1067     if (hDib == 0) {   1150     if (hDib == 0) {
1068         Win32Error_SetError(uglError, UGL_ERROR_OTHER, _T("Unable to create device-independent image handle"));   1151         Win32Error_SetError(uglError, UGL_ERROR_OTHER, _T("Unable to create device-independent image handle"));
1069         free(pbmi);   1152         free(pbmi);
1070         return NULL_HANDLE;   1153         return NULL_HANDLE;
1071     }   1154     }
1072     1155  
1073     // since the bytesPerLine might not match the scanline pad required by Windows, copy   1156     // since the bytesPerLine might not match the scanline pad required by Windows, copy
1074     // the pixels to our bitmap a row at at time. At the end of each row of pixel data,   1157     // the pixels to our bitmap a row at at time. At the end of each row of pixel data,
1075     // pad with zeroes to end on a LONG boundary.   1158     // pad with zeroes to end on a LONG boundary.
1076     buffer = (LPBYTE)pBits[0];   1159     buffer = (LPBYTE)pBits[0];
1077     bufferIndex = 0;   1160     bufferIndex = 0;
1078     windowsScanline = _Image_GetScanlineLength(width,bmi.bmiHeader.biBitCount);   1161     windowsScanline = _Image_GetScanlineLength(width,bmi.bmiHeader.biBitCount);
1079     for (index = 0; index < imageData_length; index += bytesPerLine) {   1162     for (index = 0; index < imageData_length; index += bytesPerLine) {
1080         memcpy((void *) &buffer[bufferIndex], (void *) &imageData[index], bytesPerLine);   1163         memcpy((void *) &buffer[bufferIndex], (void *) &imageData[index], bytesPerLine);
1081         bufferIndex += windowsScanline;   1164         bufferIndex += windowsScanline;
1082     }   1165     }
1083     1166  
1084     free(pbmi);   1167     free(pbmi);
1085     1168  
1086     if (transparentPixel != -1) {   1169     if (transparentPixel != -1) {
1087         transparentColor = indexedBlues[transparentPixel] << 16 |   1170         transparentColor = indexedBlues[transparentPixel] << 16 |
1088                            indexedGreens[transparentPixel] << 8 |   1171                            indexedGreens[transparentPixel] << 8 |
1089                            indexedReds[transparentPixel];   1172                            indexedReds[transparentPixel];
1090     } else {   1173     } else {
1091         transparentColor = -1;   1174         transparentColor = -1;
1092     }   1175     }
1093     1176  
1094     return (UGL_Int)_Image_New(hDib, FALSE, transparentPixel, transparentColor, uglError);   1177     return (UGL_Int)_Image_New(hDib, FALSE, transparentPixel, transparentColor, uglError);
1095 }   1178 }
1096 //========================================================================================   1179 //========================================================================================