blob: 764fbf8d0499a85655b4acf2ccd01cf4b856ac84 [file] [log] [blame]
Alexandre Julliard401710d1993-09-04 10:09:32 +00001/*
Alexandre Julliard3a5816f1994-12-27 14:11:53 +00002 * GDI device-independent bitmaps
Alexandre Julliard401710d1993-09-04 10:09:32 +00003 *
Alexandre Julliard3a5816f1994-12-27 14:11:53 +00004 * Copyright 1993,1994 Alexandre Julliard
Huw D M Davies91d16081998-11-06 11:03:00 +00005 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00006 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexandre Julliard3a5816f1994-12-27 14:11:53 +000019 */
20
Alexandre Julliard908464d2000-11-01 03:11:12 +000021#include <stdlib.h>
James Juranf4d5fef2001-01-26 20:43:40 +000022#include <string.h>
Alexandre Julliard908464d2000-11-01 03:11:12 +000023
Michael Veksler92ae2191999-05-02 11:39:09 +000024#include "winbase.h"
Alexandre Julliard8d24ae61994-04-05 21:42:43 +000025#include "bitmap.h"
Alexandre Julliard6bbc7452001-07-22 23:13:08 +000026#include "selectors.h"
Alexandre Julliard2239abb2000-11-05 02:05:07 +000027#include "gdi.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000028#include "wine/debug.h"
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +000029#include "palette.h"
Alexandre Julliard7ff1c411997-05-25 13:58:18 +000030
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000031WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000032
Alexandre Julliard7ff1c411997-05-25 13:58:18 +000033/***********************************************************************
34 * DIB_GetDIBWidthBytes
35 *
36 * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
37 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
38 */
39int DIB_GetDIBWidthBytes( int width, int depth )
Alexandre Julliard234bc241994-12-10 13:02:28 +000040{
Alexandre Julliard3a5816f1994-12-27 14:11:53 +000041 int words;
42
43 switch(depth)
44 {
Alexandre Julliarddf2673b1997-03-29 17:20:20 +000045 case 1: words = (width + 31) / 32; break;
46 case 4: words = (width + 7) / 8; break;
47 case 8: words = (width + 3) / 4; break;
48 case 15:
49 case 16: words = (width + 1) / 2; break;
50 case 24: words = (width * 3 + 3)/4; break;
51
52 default:
Alexandre Julliard15657091999-05-23 10:25:25 +000053 WARN("(%d): Unsupported depth\n", depth );
Alexandre Julliarddf2673b1997-03-29 17:20:20 +000054 /* fall through */
55 case 32:
56 words = width;
Alexandre Julliard3a5816f1994-12-27 14:11:53 +000057 }
58 return 4 * words;
Alexandre Julliard234bc241994-12-10 13:02:28 +000059}
Alexandre Julliard401710d1993-09-04 10:09:32 +000060
Huw D M Davies608629b1999-04-18 12:07:00 +000061/***********************************************************************
62 * DIB_GetDIBImageBytes
63 *
64 * Return the number of bytes used to hold the image in a DIB bitmap.
65 */
66int DIB_GetDIBImageBytes( int width, int height, int depth )
67{
68 return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
69}
70
Alexandre Julliard3a5816f1994-12-27 14:11:53 +000071
Alexandre Julliard401710d1993-09-04 10:09:32 +000072/***********************************************************************
73 * DIB_BitmapInfoSize
74 *
Alexandre Julliard7ff1c411997-05-25 13:58:18 +000075 * Return the size of the bitmap info structure including color table.
Alexandre Julliard401710d1993-09-04 10:09:32 +000076 */
Huw D M Davies56166a61999-04-19 16:45:24 +000077int DIB_BitmapInfoSize( const BITMAPINFO * info, WORD coloruse )
Alexandre Julliard401710d1993-09-04 10:09:32 +000078{
Alexandre Julliardaf0bae51995-10-03 17:06:08 +000079 int colors;
80
81 if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
82 {
83 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
Alexandre Julliard23946ad1997-06-16 17:43:53 +000084 colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
Alexandre Julliardaf0bae51995-10-03 17:06:08 +000085 return sizeof(BITMAPCOREHEADER) + colors *
86 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
87 }
88 else /* assume BITMAPINFOHEADER */
89 {
90 colors = info->bmiHeader.biClrUsed;
Alexandre Julliard23946ad1997-06-16 17:43:53 +000091 if (!colors && (info->bmiHeader.biBitCount <= 8))
Alexandre Julliardaf0bae51995-10-03 17:06:08 +000092 colors = 1 << info->bmiHeader.biBitCount;
93 return sizeof(BITMAPINFOHEADER) + colors *
94 ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
95 }
Alexandre Julliard401710d1993-09-04 10:09:32 +000096}
97
98
99/***********************************************************************
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000100 * DIB_GetBitmapInfo
101 *
102 * Get the info from a bitmap header.
Andreas Mohr1d8ef192001-08-03 18:11:23 +0000103 * Return 1 for INFOHEADER, 0 for COREHEADER,
104 * 4 for V4HEADER, 5 for V5HEADER, -1 for error.
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000105 */
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000106int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
Alexandre Julliarda845b881998-06-01 10:44:35 +0000107 int *height, WORD *bpp, WORD *compr )
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000108{
109 if (header->biSize == sizeof(BITMAPINFOHEADER))
110 {
111 *width = header->biWidth;
112 *height = header->biHeight;
113 *bpp = header->biBitCount;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000114 *compr = header->biCompression;
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000115 return 1;
116 }
117 if (header->biSize == sizeof(BITMAPCOREHEADER))
118 {
119 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
120 *width = core->bcWidth;
121 *height = core->bcHeight;
122 *bpp = core->bcBitCount;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000123 *compr = 0;
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000124 return 0;
125 }
Andreas Mohr1d8ef192001-08-03 18:11:23 +0000126 if (header->biSize == sizeof(BITMAPV4HEADER))
127 {
128 BITMAPV4HEADER *v4hdr = (BITMAPV4HEADER *)header;
129 *width = v4hdr->bV4Width;
130 *height = v4hdr->bV4Height;
131 *bpp = v4hdr->bV4BitCount;
132 *compr = v4hdr->bV4Compression;
133 return 4;
134 }
135 if (header->biSize == sizeof(BITMAPV5HEADER))
136 {
137 BITMAPV5HEADER *v5hdr = (BITMAPV5HEADER *)header;
138 *width = v5hdr->bV5Width;
139 *height = v5hdr->bV5Height;
140 *bpp = v5hdr->bV5BitCount;
141 *compr = v5hdr->bV5Compression;
142 return 5;
143 }
144 ERR("(%ld): unknown/wrong size for header\n", header->biSize );
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000145 return -1;
146}
147
148
149/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000150 * StretchDIBits (GDI.439)
Alexandre Julliard2787be81995-05-22 18:23:01 +0000151 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000152INT16 WINAPI StretchDIBits16(HDC16 hdc, INT16 xDst, INT16 yDst, INT16 widthDst,
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000153 INT16 heightDst, INT16 xSrc, INT16 ySrc, INT16 widthSrc,
154 INT16 heightSrc, const VOID *bits,
155 const BITMAPINFO *info, UINT16 wUsage, DWORD dwRop )
Alexandre Julliard2787be81995-05-22 18:23:01 +0000156{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000157 return (INT16)StretchDIBits( hdc, xDst, yDst, widthDst, heightDst,
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000158 xSrc, ySrc, widthSrc, heightSrc, bits,
159 info, wUsage, dwRop );
160}
161
162
163/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000164 * StretchDIBits (GDI32.@)
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000165 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000166INT WINAPI StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst,
167 INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
168 INT heightSrc, const void *bits,
169 const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
Alexandre Julliard7e6ae4b1996-12-08 19:25:27 +0000170{
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000171 DC *dc = DC_GetDCUpdate( hdc );
Huw D M Davies14c99d01998-10-24 10:44:05 +0000172 if(!dc) return FALSE;
Alexandre Julliardded30381995-07-06 17:18:27 +0000173
Huw D M Davies14c99d01998-10-24 10:44:05 +0000174 if(dc->funcs->pStretchDIBits)
Alexandre Julliard658cdb42001-08-15 23:33:20 +0000175 {
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000176 heightSrc = dc->funcs->pStretchDIBits(dc->physDev, xDst, yDst, widthDst,
Alexandre Julliard658cdb42001-08-15 23:33:20 +0000177 heightDst, xSrc, ySrc, widthSrc,
178 heightSrc, bits, info, wUsage, dwRop);
179 GDI_ReleaseObj( hdc );
180 }
181 else /* use StretchBlt */
182 {
Alexandre Julliard54e47751999-10-13 16:16:23 +0000183 HBITMAP hBitmap, hOldBitmap;
184 HDC hdcMem;
Matthew J. Francised744e71999-10-24 17:28:23 +0000185
Alexandre Julliard658cdb42001-08-15 23:33:20 +0000186 GDI_ReleaseObj( hdc );
Alexandre Julliard54e47751999-10-13 16:16:23 +0000187 hdcMem = CreateCompatibleDC( hdc );
Eric Pouech26501592000-09-10 03:13:41 +0000188 if (info->bmiHeader.biCompression == BI_RLE4 ||
189 info->bmiHeader.biCompression == BI_RLE8) {
190
191 /* when RLE compression is used, there may be some gaps (ie the DIB doesn't
192 * contain all the rectangle described in bmiHeader, but only part of it.
193 * This mean that those undescribed pixels must be left untouched.
194 * So, we first copy on a memory bitmap the current content of the
195 * destination rectangle, blit the DIB bits on top of it - hence leaving
196 * the gaps untouched -, and blitting the rectangle back.
197 * This insure that gaps are untouched on the destination rectangle
198 * Not doing so leads to trashed images (the gaps contain what was on the
199 * memory bitmap => generally black or garbage)
200 * Unfortunately, RLE DIBs without gaps will be slowed down. But this is
201 * another speed vs correctness issue. Anyway, if speed is needed, then the
202 * pStretchDIBits function shall be implemented.
203 * ericP (2000/09/09)
204 */
205 hBitmap = CreateCompatibleBitmap(hdc, info->bmiHeader.biWidth,
206 info->bmiHeader.biHeight);
207 hOldBitmap = SelectObject( hdcMem, hBitmap );
208
209 /* copy existing bitmap from destination dc */
210 StretchBlt( hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
211 widthSrc, heightSrc, hdc, xDst, yDst, widthDst, heightDst,
212 dwRop );
213 SetDIBits(hdcMem, hBitmap, 0, info->bmiHeader.biHeight, bits,
214 info, DIB_RGB_COLORS);
215
216 } else {
217 hBitmap = CreateDIBitmap( hdc, &info->bmiHeader, CBM_INIT,
218 bits, info, wUsage );
219 hOldBitmap = SelectObject( hdcMem, hBitmap );
220 }
221
Matthew J. Francised744e71999-10-24 17:28:23 +0000222 /* Origin for DIBitmap may be bottom left (positive biHeight) or top
223 left (negative biHeight) */
Alexandre Julliard54e47751999-10-13 16:16:23 +0000224 StretchBlt( hdc, xDst, yDst, widthDst, heightDst,
Eric Pouech26501592000-09-10 03:13:41 +0000225 hdcMem, xSrc, abs(info->bmiHeader.biHeight) - heightSrc - ySrc,
226 widthSrc, heightSrc, dwRop );
Alexandre Julliard54e47751999-10-13 16:16:23 +0000227 SelectObject( hdcMem, hOldBitmap );
228 DeleteDC( hdcMem );
229 DeleteObject( hBitmap );
Huw D M Davies14c99d01998-10-24 10:44:05 +0000230 }
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000231 return heightSrc;
Alexandre Julliard2787be81995-05-22 18:23:01 +0000232}
233
Alexandre Julliard54e47751999-10-13 16:16:23 +0000234
Alexandre Julliard2787be81995-05-22 18:23:01 +0000235/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000236 * SetDIBits (GDI.440)
Alexandre Julliard401710d1993-09-04 10:09:32 +0000237 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000238INT16 WINAPI SetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
239 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
240 UINT16 coloruse )
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000241{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000242 return SetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000243}
244
245
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000246/******************************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000247 * SetDIBits [GDI32.@] Sets pixels in a bitmap using colors from DIB
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000248 *
249 * PARAMS
250 * hdc [I] Handle to device context
251 * hbitmap [I] Handle to bitmap
252 * startscan [I] Starting scan line
253 * lines [I] Number of scan lines
254 * bits [I] Array of bitmap bits
255 * info [I] Address of structure with data
256 * coloruse [I] Type of color indexes to use
257 *
258 * RETURNS
259 * Success: Number of scan lines copied
260 * Failure: 0
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000261 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000262INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000263 UINT lines, LPCVOID bits, const BITMAPINFO *info,
264 UINT coloruse )
Alexandre Julliard401710d1993-09-04 10:09:32 +0000265{
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000266 DC *dc;
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000267 INT result = 0;
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000268
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000269 /* Check parameters */
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000270 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000271
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000272 if (dc->funcs->pSetDIBits)
273 result = dc->funcs->pSetDIBits(dc->physDev, hbitmap, startscan, lines, bits, info, coloruse);
Alexandre Julliard401710d1993-09-04 10:09:32 +0000274
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000275 GDI_ReleaseObj( hdc );
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000276 return result;
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000277}
Alexandre Julliard401710d1993-09-04 10:09:32 +0000278
Alexandre Julliard401710d1993-09-04 10:09:32 +0000279
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000280/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000281 * SetDIBitsToDevice (GDI.443)
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000282 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000283INT16 WINAPI SetDIBitsToDevice16(HDC16 hdc, INT16 xDest, INT16 yDest, INT16 cx,
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000284 INT16 cy, INT16 xSrc, INT16 ySrc, UINT16 startscan,
285 UINT16 lines, LPCVOID bits, const BITMAPINFO *info,
286 UINT16 coloruse )
287{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000288 return SetDIBitsToDevice( hdc, xDest, yDest, cx, cy, xSrc, ySrc,
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000289 startscan, lines, bits, info, coloruse );
290}
291
292
293/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000294 * SetDIBitsToDevice (GDI32.@)
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000295 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000296INT WINAPI SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
297 DWORD cy, INT xSrc, INT ySrc, UINT startscan,
298 UINT lines, LPCVOID bits, const BITMAPINFO *info,
299 UINT coloruse )
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000300{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000301 INT ret;
Huw D M Davies91d16081998-11-06 11:03:00 +0000302 DC *dc;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000303
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000304 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000305
Huw D M Davies91d16081998-11-06 11:03:00 +0000306 if(dc->funcs->pSetDIBitsToDevice)
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000307 ret = dc->funcs->pSetDIBitsToDevice( dc->physDev, xDest, yDest, cx, cy, xSrc,
Huw D M Davies91d16081998-11-06 11:03:00 +0000308 ySrc, startscan, lines, bits,
309 info, coloruse );
310 else {
Alexandre Julliard15657091999-05-23 10:25:25 +0000311 FIXME("unimplemented on hdc %08x\n", hdc);
Huw D M Davies91d16081998-11-06 11:03:00 +0000312 ret = 0;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000313 }
Alexandre Julliard642d3131998-07-12 19:29:36 +0000314
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000315 GDI_ReleaseObj( hdc );
Huw D M Davies91d16081998-11-06 11:03:00 +0000316 return ret;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000317}
318
Alexandre Julliarde658d821997-11-30 17:45:40 +0000319/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000320 * SetDIBColorTable (GDI.602)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000321 */
322UINT16 WINAPI SetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
323 RGBQUAD *colors )
324{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000325 return SetDIBColorTable( hdc, startpos, entries, colors );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000326}
327
328/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000329 * SetDIBColorTable (GDI32.@)
Alexandre Julliarde658d821997-11-30 17:45:40 +0000330 */
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000331UINT WINAPI SetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
Alexandre Julliarde658d821997-11-30 17:45:40 +0000332{
333 DC * dc;
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000334 UINT result = 0;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000335
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000336 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
Alexandre Julliarde658d821997-11-30 17:45:40 +0000337
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000338 if (dc->funcs->pSetDIBColorTable)
339 result = dc->funcs->pSetDIBColorTable(dc->physDev, startpos, entries, colors);
Alexandre Julliarde658d821997-11-30 17:45:40 +0000340
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000341 GDI_ReleaseObj( hdc );
Ove Kaavena32ddc02000-11-25 21:42:00 +0000342 return result;
Alexandre Julliarde658d821997-11-30 17:45:40 +0000343}
344
345/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000346 * GetDIBColorTable (GDI.603)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000347 */
348UINT16 WINAPI GetDIBColorTable16( HDC16 hdc, UINT16 startpos, UINT16 entries,
349 RGBQUAD *colors )
350{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000351 return GetDIBColorTable( hdc, startpos, entries, colors );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000352}
353
354/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000355 * GetDIBColorTable (GDI32.@)
Alexandre Julliarde658d821997-11-30 17:45:40 +0000356 */
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000357UINT WINAPI GetDIBColorTable( HDC hdc, UINT startpos, UINT entries, RGBQUAD *colors )
Alexandre Julliarde658d821997-11-30 17:45:40 +0000358{
359 DC * dc;
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000360 UINT result = 0;
Alexandre Julliarde658d821997-11-30 17:45:40 +0000361
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000362 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
Alexandre Julliarde658d821997-11-30 17:45:40 +0000363
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000364 if (dc->funcs->pGetDIBColorTable)
365 result = dc->funcs->pGetDIBColorTable(dc->physDev, startpos, entries, colors);
Alexandre Julliarde658d821997-11-30 17:45:40 +0000366
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000367 GDI_ReleaseObj( hdc );
Ove Kaavena32ddc02000-11-25 21:42:00 +0000368 return result;
Alexandre Julliarde658d821997-11-30 17:45:40 +0000369}
Alexandre Julliard2787be81995-05-22 18:23:01 +0000370
Huw D M Daviescc3e5d71999-03-25 10:48:01 +0000371/* FIXME the following two structs should be combined with __sysPalTemplate in
372 objects/color.c - this should happen after de-X11-ing both of these
373 files.
Andreas Mohrf32f9182001-04-20 18:36:05 +0000374 NB. RGBQUAD and PALETTEENTRY have different orderings of red, green
Huw D M Daviescc3e5d71999-03-25 10:48:01 +0000375 and blue - sigh */
376
377static RGBQUAD EGAColors[16] = {
378/* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
379 { 0x00, 0x00, 0x00, 0x00 },
380 { 0x00, 0x00, 0x80, 0x00 },
381 { 0x00, 0x80, 0x00, 0x00 },
382 { 0x00, 0x80, 0x80, 0x00 },
383 { 0x80, 0x00, 0x00, 0x00 },
384 { 0x80, 0x00, 0x80, 0x00 },
385 { 0x80, 0x80, 0x00, 0x00 },
386 { 0x80, 0x80, 0x80, 0x00 },
387 { 0xc0, 0xc0, 0xc0, 0x00 },
388 { 0x00, 0x00, 0xff, 0x00 },
389 { 0x00, 0xff, 0x00, 0x00 },
390 { 0x00, 0xff, 0xff, 0x00 },
391 { 0xff, 0x00, 0x00, 0x00 },
392 { 0xff, 0x00, 0xff, 0x00 },
393 { 0xff, 0xff, 0x00, 0x00 },
394 { 0xff, 0xff, 0xff, 0x00 }
395};
396
397
398static RGBQUAD DefLogPalette[20] = { /* Copy of Default Logical Palette */
399/* rgbBlue, rgbGreen, rgbRed, rgbReserverd */
400 { 0x00, 0x00, 0x00, 0x00 },
401 { 0x00, 0x00, 0x80, 0x00 },
402 { 0x00, 0x80, 0x00, 0x00 },
403 { 0x00, 0x80, 0x80, 0x00 },
404 { 0x80, 0x00, 0x00, 0x00 },
405 { 0x80, 0x00, 0x80, 0x00 },
406 { 0x80, 0x80, 0x00, 0x00 },
407 { 0xc0, 0xc0, 0xc0, 0x00 },
408 { 0xc0, 0xdc, 0xc0, 0x00 },
409 { 0xf0, 0xca, 0xa6, 0x00 },
410 { 0xf0, 0xfb, 0xff, 0x00 },
411 { 0xa4, 0xa0, 0xa0, 0x00 },
412 { 0x80, 0x80, 0x80, 0x00 },
413 { 0x00, 0x00, 0xf0, 0x00 },
414 { 0x00, 0xff, 0x00, 0x00 },
415 { 0x00, 0xff, 0xff, 0x00 },
416 { 0xff, 0x00, 0x00, 0x00 },
417 { 0xff, 0x00, 0xff, 0x00 },
418 { 0xff, 0xff, 0x00, 0x00 },
419 { 0xff, 0xff, 0xff, 0x00 }
420};
421
Alexandre Julliard401710d1993-09-04 10:09:32 +0000422/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000423 * GetDIBits (GDI.441)
Alexandre Julliard401710d1993-09-04 10:09:32 +0000424 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000425INT16 WINAPI GetDIBits16( HDC16 hdc, HBITMAP16 hbitmap, UINT16 startscan,
Zygo Blaxell1084b2c1999-02-09 14:13:12 +0000426 UINT16 lines, LPVOID bits, BITMAPINFO * info,
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000427 UINT16 coloruse )
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000428{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000429 return GetDIBits( hdc, hbitmap, startscan, lines, bits, info, coloruse );
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000430}
431
432
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000433/******************************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000434 * GetDIBits [GDI32.@] Retrieves bits of bitmap and copies to buffer
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000435 *
436 * RETURNS
437 * Success: Number of scan lines copied from bitmap
438 * Failure: 0
Alexandre Julliard7ff1c411997-05-25 13:58:18 +0000439 *
440 * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/func/src/f30_14.htm
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000441 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000442INT WINAPI GetDIBits(
443 HDC hdc, /* [in] Handle to device context */
444 HBITMAP hbitmap, /* [in] Handle to bitmap */
445 UINT startscan, /* [in] First scan line to set in dest bitmap */
446 UINT lines, /* [in] Number of scan lines to copy */
Zygo Blaxell1084b2c1999-02-09 14:13:12 +0000447 LPVOID bits, /* [out] Address of array for bitmap bits */
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000448 BITMAPINFO * info, /* [out] Address of structure with bitmap data */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000449 UINT coloruse) /* [in] RGB or palette index */
Alexandre Julliard401710d1993-09-04 10:09:32 +0000450{
451 DC * dc;
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000452 BITMAPOBJ * bmp;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000453 PALETTEENTRY * palEntry;
454 PALETTEOBJ * palette;
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000455 int i;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000456
Huw D M Daviescc3e5d71999-03-25 10:48:01 +0000457 if (!info) return 0;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000458 if (!(dc = DC_GetDCUpdate( hdc ))) return 0;
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000459 if (!(bmp = (BITMAPOBJ *)GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000460 {
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000461 GDI_ReleaseObj( hdc );
Alexandre Julliard401710d1993-09-04 10:09:32 +0000462 return 0;
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000463 }
Alexandre Julliard2239abb2000-11-05 02:05:07 +0000464 if (!(palette = (PALETTEOBJ*)GDI_GetObjPtr( dc->hPalette, PALETTE_MAGIC )))
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000465 {
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000466 GDI_ReleaseObj( hdc );
467 GDI_ReleaseObj( hbitmap );
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000468 return 0;
469 }
Alexandre Julliard401710d1993-09-04 10:09:32 +0000470
Huw D M Daviescc3e5d71999-03-25 10:48:01 +0000471 /* Transfer color info */
472
473 if (info->bmiHeader.biBitCount <= 8 && info->bmiHeader.biBitCount > 0 ) {
474
Eric Kohlc0112671998-11-15 18:10:11 +0000475 info->bmiHeader.biClrUsed = 0;
Huw D M Daviescc3e5d71999-03-25 10:48:01 +0000476
477 if(info->bmiHeader.biBitCount >= bmp->bitmap.bmBitsPixel) {
478 palEntry = palette->logpalette.palPalEntry;
479 for (i = 0; i < (1 << bmp->bitmap.bmBitsPixel); i++, palEntry++) {
480 if (coloruse == DIB_RGB_COLORS) {
481 info->bmiColors[i].rgbRed = palEntry->peRed;
482 info->bmiColors[i].rgbGreen = palEntry->peGreen;
483 info->bmiColors[i].rgbBlue = palEntry->peBlue;
484 info->bmiColors[i].rgbReserved = 0;
485 }
486 else ((WORD *)info->bmiColors)[i] = (WORD)i;
Alexandre Julliardd37eb361997-07-20 16:23:21 +0000487 }
Huw D M Daviescc3e5d71999-03-25 10:48:01 +0000488 } else {
489 switch (info->bmiHeader.biBitCount) {
490 case 1:
491 info->bmiColors[0].rgbRed = info->bmiColors[0].rgbGreen =
492 info->bmiColors[0].rgbBlue = 0;
493 info->bmiColors[0].rgbReserved = 0;
494 info->bmiColors[1].rgbRed = info->bmiColors[1].rgbGreen =
495 info->bmiColors[1].rgbBlue = 0xff;
496 info->bmiColors[1].rgbReserved = 0;
497 break;
498
499 case 4:
500 memcpy(info->bmiColors, EGAColors, sizeof(EGAColors));
501 break;
502
503 case 8:
504 {
505 INT r, g, b;
506 RGBQUAD *color;
507
508 memcpy(info->bmiColors, DefLogPalette,
509 10 * sizeof(RGBQUAD));
510 memcpy(info->bmiColors + 246, DefLogPalette + 10,
511 10 * sizeof(RGBQUAD));
512 color = info->bmiColors + 10;
513 for(r = 0; r <= 5; r++) /* FIXME */
514 for(g = 0; g <= 5; g++)
515 for(b = 0; b <= 5; b++) {
516 color->rgbRed = (r * 0xff) / 5;
517 color->rgbGreen = (g * 0xff) / 5;
518 color->rgbBlue = (b * 0xff) / 5;
519 color->rgbReserved = 0;
520 color++;
521 }
522 }
523 }
Alexandre Julliard401710d1993-09-04 10:09:32 +0000524 }
Alexandre Julliard401710d1993-09-04 10:09:32 +0000525 }
Alexandre Julliarda845b881998-06-01 10:44:35 +0000526
Alexandre Julliard2239abb2000-11-05 02:05:07 +0000527 GDI_ReleaseObj( dc->hPalette );
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000528
Kai Morich9e9fc1b1999-09-13 15:13:24 +0000529 if (bits && lines)
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000530 {
Andreas Mohrf32f9182001-04-20 18:36:05 +0000531 /* If the bitmap object already have a dib section that contains image data, get the bits from it */
Karl Lessarddee464c1999-09-14 11:51:01 +0000532 if(bmp->dib && bmp->dib->dsBm.bmBitsPixel >= 15 && info->bmiHeader.biBitCount >= 15)
Karl Lessard41875791999-09-03 16:49:17 +0000533 {
534 /*FIXME: Only RGB dibs supported for now */
Joerg Mayer4d756402001-01-10 22:45:33 +0000535 unsigned int srcwidth = bmp->dib->dsBm.bmWidth, srcwidthb = bmp->dib->dsBm.bmWidthBytes;
Karl Lessard41875791999-09-03 16:49:17 +0000536 int dstwidthb = DIB_GetDIBWidthBytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
Patrik Stridvall0ee98cc2000-02-26 13:17:55 +0000537 LPBYTE dbits = bits, sbits = (LPBYTE) bmp->dib->dsBm.bmBits + (startscan * srcwidthb);
Joerg Mayer4d756402001-01-10 22:45:33 +0000538 unsigned int x, y;
Karl Lessard41875791999-09-03 16:49:17 +0000539
540 if ((info->bmiHeader.biHeight < 0) ^ (bmp->dib->dsBmih.biHeight < 0))
541 {
Huw D M Davies1bb98601999-09-19 12:04:17 +0000542 dbits = (LPBYTE)bits + (dstwidthb * (lines-1));
Karl Lessard41875791999-09-03 16:49:17 +0000543 dstwidthb = -dstwidthb;
544 }
545
546 switch( info->bmiHeader.biBitCount ) {
547
Karl Lessarddee464c1999-09-14 11:51:01 +0000548 case 15:
Karl Lessard41875791999-09-03 16:49:17 +0000549 case 16: /* 16 bpp dstDIB */
550 {
551 LPWORD dstbits = (LPWORD)dbits;
552 WORD rmask = 0x7c00, gmask= 0x03e0, bmask = 0x001f;
553
554 /* FIXME: BI_BITFIELDS not supported yet */
555
556 switch(bmp->dib->dsBm.bmBitsPixel) {
557
558 case 16: /* 16 bpp srcDIB -> 16 bpp dstDIB */
559 {
560 /* FIXME: BI_BITFIELDS not supported yet */
561 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
562 memcpy(dbits, sbits, srcwidthb);
563 }
564 break;
565
566 case 24: /* 24 bpp srcDIB -> 16 bpp dstDIB */
567 {
Huw D M Davies1bb98601999-09-19 12:04:17 +0000568 LPBYTE srcbits = sbits;
Karl Lessard41875791999-09-03 16:49:17 +0000569
570 for( y = 0; y < lines; y++) {
Marcus Meissner183eae92001-06-14 19:22:55 +0000571 for( x = 0; x < srcwidth; x++, srcbits += 3)
572 *dstbits++ = ((srcbits[0] >> 3) & bmask) |
573 (((WORD)srcbits[1] << 2) & gmask) |
574 (((WORD)srcbits[2] << 7) & rmask);
575
Karl Lessard41875791999-09-03 16:49:17 +0000576 dstbits = (LPWORD)(dbits+=dstwidthb);
Huw D M Davies1bb98601999-09-19 12:04:17 +0000577 srcbits = (sbits += srcwidthb);
Karl Lessard41875791999-09-03 16:49:17 +0000578 }
579 }
580 break;
581
582 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
583 {
584 LPDWORD srcbits = (LPDWORD)sbits;
585 DWORD val;
586
587 for( y = 0; y < lines; y++) {
588 for( x = 0; x < srcwidth; x++ ) {
589 val = *srcbits++;
Karl Lessardc73a1fd1999-09-19 14:15:41 +0000590 *dstbits++ = (WORD)(((val >> 3) & bmask) | ((val >> 6) & gmask) |
591 ((val >> 9) & rmask));
Karl Lessard41875791999-09-03 16:49:17 +0000592 }
Huw D M Davies1bb98601999-09-19 12:04:17 +0000593 dstbits = (LPWORD)(dbits+=dstwidthb);
594 srcbits = (LPDWORD)(sbits+=srcwidthb);
Karl Lessard41875791999-09-03 16:49:17 +0000595 }
596 }
597 break;
598
599 default: /* ? bit bmp -> 16 bit DIB */
600 FIXME("15/16 bit DIB %d bit bitmap\n",
601 bmp->bitmap.bmBitsPixel);
602 break;
603 }
604 }
605 break;
606
607 case 24: /* 24 bpp dstDIB */
608 {
Huw D M Davies1bb98601999-09-19 12:04:17 +0000609 LPBYTE dstbits = dbits;
Karl Lessard41875791999-09-03 16:49:17 +0000610
611 switch(bmp->dib->dsBm.bmBitsPixel) {
612
613 case 16: /* 16 bpp srcDIB -> 24 bpp dstDIB */
614 {
615 LPWORD srcbits = (LPWORD)sbits;
616 WORD val;
617
618 /* FIXME: BI_BITFIELDS not supported yet */
619 for( y = 0; y < lines; y++) {
620 for( x = 0; x < srcwidth; x++ ) {
621 val = *srcbits++;
Karl Lessard41875791999-09-03 16:49:17 +0000622 *dstbits++ = (BYTE)(((val << 3) & 0xf8) | ((val >> 2) & 0x07));
Karl Lessardc73a1fd1999-09-19 14:15:41 +0000623 *dstbits++ = (BYTE)(((val >> 2) & 0xf8) | ((val >> 7) & 0x07));
624 *dstbits++ = (BYTE)(((val >> 7) & 0xf8) | ((val >> 12) & 0x07));
Karl Lessard41875791999-09-03 16:49:17 +0000625 }
Huw D M Davies1bb98601999-09-19 12:04:17 +0000626 dstbits = (LPBYTE)(dbits+=dstwidthb);
Karl Lessard41875791999-09-03 16:49:17 +0000627 srcbits = (LPWORD)(sbits+=srcwidthb);
628 }
629 }
630 break;
631
632 case 24: /* 24 bpp srcDIB -> 24 bpp dstDIB */
633 {
634 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
635 memcpy(dbits, sbits, srcwidthb);
636 }
637 break;
638
639 case 32: /* 32 bpp srcDIB -> 24 bpp dstDIB */
640 {
641 LPBYTE srcbits = (LPBYTE)sbits;
642
643 for( y = 0; y < lines; y++) {
644 for( x = 0; x < srcwidth; x++, srcbits++ ) {
645 *dstbits++ = *srcbits++;
646 *dstbits++ = *srcbits++;
647 *dstbits++ = *srcbits++;
648 }
649 dstbits=(LPBYTE)(dbits+=dstwidthb);
650 srcbits = (LPBYTE)(sbits+=srcwidthb);
651 }
652 }
653 break;
654
655 default: /* ? bit bmp -> 24 bit DIB */
656 FIXME("24 bit DIB %d bit bitmap\n",
657 bmp->bitmap.bmBitsPixel);
658 break;
659 }
660 }
661 break;
662
663 case 32: /* 32 bpp dstDIB */
664 {
665 LPDWORD dstbits = (LPDWORD)dbits;
Karl Lessard41875791999-09-03 16:49:17 +0000666
667 /* FIXME: BI_BITFIELDS not supported yet */
668
669 switch(bmp->dib->dsBm.bmBitsPixel) {
670 case 16: /* 16 bpp srcDIB -> 32 bpp dstDIB */
671 {
672 LPWORD srcbits = (LPWORD)sbits;
673 DWORD val;
674
675 /* FIXME: BI_BITFIELDS not supported yet */
676 for( y = 0; y < lines; y++) {
677 for( x = 0; x < srcwidth; x++ ) {
678 val = (DWORD)*srcbits++;
Karl Lessardc73a1fd1999-09-19 14:15:41 +0000679 *dstbits++ = ((val << 3) & 0xf8) | ((val >> 2) & 0x07) |
Karl Lessard41875791999-09-03 16:49:17 +0000680 ((val << 6) & 0xf800) | ((val << 1) & 0x0700) |
Karl Lessardc73a1fd1999-09-19 14:15:41 +0000681 ((val << 9) & 0xf80000) | ((val << 4) & 0x070000);
Karl Lessard41875791999-09-03 16:49:17 +0000682 }
Huw D M Davies1bb98601999-09-19 12:04:17 +0000683 dstbits=(LPDWORD)(dbits+=dstwidthb);
684 srcbits=(LPWORD)(sbits+=srcwidthb);
Karl Lessard41875791999-09-03 16:49:17 +0000685 }
686 }
687 break;
688
689 case 24: /* 24 bpp srcDIB -> 32 bpp dstDIB */
690 {
Huw D M Davies1bb98601999-09-19 12:04:17 +0000691 LPBYTE srcbits = sbits;
Karl Lessard41875791999-09-03 16:49:17 +0000692
693 for( y = 0; y < lines; y++) {
Karl Lessardc73a1fd1999-09-19 14:15:41 +0000694 for( x = 0; x < srcwidth; x++, srcbits+=3 )
695 *dstbits++ = ((DWORD)*srcbits) & 0x00ffffff;
Huw D M Davies1bb98601999-09-19 12:04:17 +0000696 dstbits=(LPDWORD)(dbits+=dstwidthb);
Karl Lessard41875791999-09-03 16:49:17 +0000697 srcbits=(sbits+=srcwidthb);
698 }
699 }
700 break;
701
702 case 32: /* 32 bpp srcDIB -> 16 bpp dstDIB */
703 {
704 /* FIXME: BI_BITFIELDS not supported yet */
705 for (y = 0; y < lines; y++, dbits+=dstwidthb, sbits+=srcwidthb)
706 memcpy(dbits, sbits, srcwidthb);
707 }
708 break;
709
710 default: /* ? bit bmp -> 16 bit DIB */
711 FIXME("15/16 bit DIB %d bit bitmap\n",
712 bmp->bitmap.bmBitsPixel);
713 break;
714 }
715 }
716 break;
717
718 default: /* ? bit DIB */
719 FIXME("Unsupported DIB depth %d\n", info->bmiHeader.biBitCount);
720 break;
721 }
722 }
723 /* Otherwise, get bits from the XImage */
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000724 else if (!dc->funcs->pGetDIBits ||
725 !dc->funcs->pGetDIBits(dc->physDev, hbitmap, startscan, lines, bits, info, coloruse))
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000726 {
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000727 GDI_ReleaseObj( hdc );
728 GDI_ReleaseObj( hbitmap );
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000729
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000730 return 0;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000731 }
Alexandre Julliard401710d1993-09-04 10:09:32 +0000732 }
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000733 else if( info->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER) )
734 {
735 /* fill in struct members */
Huw D M Daviescc3e5d71999-03-25 10:48:01 +0000736
737 if( info->bmiHeader.biBitCount == 0)
738 {
739 info->bmiHeader.biWidth = bmp->bitmap.bmWidth;
740 info->bmiHeader.biHeight = bmp->bitmap.bmHeight;
741 info->bmiHeader.biPlanes = 1;
742 info->bmiHeader.biBitCount = bmp->bitmap.bmBitsPixel;
Huw D M Davies608629b1999-04-18 12:07:00 +0000743 info->bmiHeader.biSizeImage =
744 DIB_GetDIBImageBytes( bmp->bitmap.bmWidth,
745 bmp->bitmap.bmHeight,
746 bmp->bitmap.bmBitsPixel );
Huw D M Daviescc3e5d71999-03-25 10:48:01 +0000747 info->bmiHeader.biCompression = 0;
748 }
749 else
750 {
Huw D M Davies608629b1999-04-18 12:07:00 +0000751 info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(
752 info->bmiHeader.biWidth,
753 info->bmiHeader.biHeight,
754 info->bmiHeader.biBitCount );
Huw D M Daviescc3e5d71999-03-25 10:48:01 +0000755 }
Alexandre Julliarddf2673b1997-03-29 17:20:20 +0000756 }
Huw D M Daviescc3e5d71999-03-25 10:48:01 +0000757
Alexandre Julliard15657091999-05-23 10:25:25 +0000758 TRACE("biSizeImage = %ld, biWidth = %ld, biHeight = %ld\n",
Huw D M Davies3da9ff31999-02-24 09:47:37 +0000759 info->bmiHeader.biSizeImage, info->bmiHeader.biWidth,
760 info->bmiHeader.biHeight);
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000761
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000762 GDI_ReleaseObj( hdc );
763 GDI_ReleaseObj( hbitmap );
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000764
Alexandre Julliard401710d1993-09-04 10:09:32 +0000765 return lines;
766}
767
768
769/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000770 * CreateDIBitmap (GDI.442)
Alexandre Julliard401710d1993-09-04 10:09:32 +0000771 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000772HBITMAP16 WINAPI CreateDIBitmap16( HDC16 hdc, const BITMAPINFOHEADER * header,
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000773 DWORD init, LPCVOID bits, const BITMAPINFO * data,
774 UINT16 coloruse )
Alexandre Julliard401710d1993-09-04 10:09:32 +0000775{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000776 return CreateDIBitmap( hdc, header, init, bits, data, coloruse );
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000777}
778
779
780/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000781 * CreateDIBitmap (GDI32.@)
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000782 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000783HBITMAP WINAPI CreateDIBitmap( HDC hdc, const BITMAPINFOHEADER *header,
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000784 DWORD init, LPCVOID bits, const BITMAPINFO *data,
Alexandre Julliarda3960291999-02-26 11:11:13 +0000785 UINT coloruse )
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000786{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000787 HBITMAP handle;
788 BOOL fColor;
Alexandre Julliarde658d821997-11-30 17:45:40 +0000789 DWORD width;
790 int height;
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000791 WORD bpp;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000792 WORD compr;
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000793
Alexandre Julliarda845b881998-06-01 10:44:35 +0000794 if (DIB_GetBitmapInfo( header, &width, &height, &bpp, &compr ) == -1) return 0;
Alexandre Julliarde658d821997-11-30 17:45:40 +0000795 if (height < 0) height = -height;
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000796
797 /* Check if we should create a monochrome or color bitmap. */
798 /* We create a monochrome bitmap only if it has exactly 2 */
Francois Boisvert3d696d91999-09-23 11:40:38 +0000799 /* colors, which are black followed by white, nothing else. */
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000800 /* In all other cases, we create a color bitmap. */
801
802 if (bpp != 1) fColor = TRUE;
803 else if ((coloruse != DIB_RGB_COLORS) ||
804 (init != CBM_INIT) || !data) fColor = FALSE;
805 else
806 {
807 if (data->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
808 {
809 RGBQUAD *rgb = data->bmiColors;
810 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
Francois Boisvert3d696d91999-09-23 11:40:38 +0000811
812 /* Check if the first color of the colormap is black */
813 if ((col == RGB(0,0,0)))
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000814 {
815 rgb++;
816 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
Francois Boisvert3d696d91999-09-23 11:40:38 +0000817 /* If the second color is white, create a monochrome bitmap */
818 fColor = (col != RGB(0xff,0xff,0xff));
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000819 }
Francois Boisvert3d696d91999-09-23 11:40:38 +0000820 /* Note : If the first color of the colormap is white
821 followed by black, we have to create a color bitmap.
822 If we don't the white will be displayed in black later on!*/
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000823 else fColor = TRUE;
824 }
825 else if (data->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
826 {
827 RGBTRIPLE *rgb = ((BITMAPCOREINFO *)data)->bmciColors;
828 DWORD col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
Francois Boisvert3d696d91999-09-23 11:40:38 +0000829 if ((col == RGB(0,0,0)))
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000830 {
831 rgb++;
832 col = RGB( rgb->rgbtRed, rgb->rgbtGreen, rgb->rgbtBlue );
Francois Boisvert3d696d91999-09-23 11:40:38 +0000833 fColor = (col != RGB(0xff,0xff,0xff));
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000834 }
835 else fColor = TRUE;
836 }
Andreas Mohr1d8ef192001-08-03 18:11:23 +0000837 else if (data->bmiHeader.biSize == sizeof(BITMAPV4HEADER))
838 { /* FIXME: correct ? */
839 RGBQUAD *rgb = data->bmiColors;
840 DWORD col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
841
842 /* Check if the first color of the colormap is black */
843 if ((col == RGB(0,0,0)))
844 {
845 rgb++;
846 col = RGB( rgb->rgbRed, rgb->rgbGreen, rgb->rgbBlue );
847 /* If the second color is white, create a monochrome bitmap */
848 fColor = (col != RGB(0xff,0xff,0xff));
849 }
850 /* Note : If the first color of the colormap is white
851 followed by black, we have to create a color bitmap.
852 If we don't the white will be displayed in black later on!*/
853 else fColor = TRUE;
854 }
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000855 else
856 {
Andreas Mohr1d8ef192001-08-03 18:11:23 +0000857 ERR("(%ld): wrong/unknown size for data\n",
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000858 data->bmiHeader.biSize );
859 return 0;
860 }
861 }
862
863 /* Now create the bitmap */
864
Alexandre Julliard87abe2f2001-08-20 18:04:09 +0000865 if (fColor)
866 handle = CreateBitmap( width, height, GetDeviceCaps( hdc, PLANES ),
867 GetDeviceCaps( hdc, BITSPIXEL ), NULL );
Alexandre Julliard946a4442000-07-30 13:50:27 +0000868 else handle = CreateBitmap( width, height, 1, 1, NULL );
869
Alexandre Julliard401710d1993-09-04 10:09:32 +0000870 if (!handle) return 0;
Alexandre Julliardaf0bae51995-10-03 17:06:08 +0000871
872 if (init == CBM_INIT)
Alexandre Julliarda3960291999-02-26 11:11:13 +0000873 SetDIBits( hdc, handle, 0, height, bits, data, coloruse );
Alexandre Julliard401710d1993-09-04 10:09:32 +0000874 return handle;
875}
Alexandre Julliard77b99181997-09-14 17:17:23 +0000876
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000877/***********************************************************************
Patrik Stridvall17fd4e32001-06-28 18:04:41 +0000878 * CreateDIBSection (GDI.489)
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000879 */
880HBITMAP16 WINAPI CreateDIBSection16 (HDC16 hdc, BITMAPINFO *bmi, UINT16 usage,
Alexandre Julliard6bbc7452001-07-22 23:13:08 +0000881 SEGPTR *bits16, HANDLE section, DWORD offset)
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000882{
Alexandre Julliard6bbc7452001-07-22 23:13:08 +0000883 LPVOID bits32;
884 HBITMAP hbitmap;
Stephane Lussier23259ce2000-07-11 22:04:44 +0000885
Alexandre Julliard6bbc7452001-07-22 23:13:08 +0000886 hbitmap = CreateDIBSection( hdc, bmi, usage, &bits32, section, offset );
887 if (hbitmap)
Stephane Lussier23259ce2000-07-11 22:04:44 +0000888 {
Alexandre Julliard6bbc7452001-07-22 23:13:08 +0000889 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr(hbitmap, BITMAP_MAGIC);
890 if (bmp && bmp->dib && bits32)
891 {
892 BITMAPINFOHEADER *bi = &bmi->bmiHeader;
893 INT height = bi->biHeight >= 0 ? bi->biHeight : -bi->biHeight;
894 INT width_bytes = DIB_GetDIBWidthBytes(bi->biWidth, bi->biBitCount);
895 INT size = (bi->biSizeImage && bi->biCompression != BI_RGB) ?
896 bi->biSizeImage : width_bytes * height;
897
898 WORD sel = SELECTOR_AllocBlock( bits32, size, WINE_LDT_FLAGS_DATA );
899 bmp->segptr_bits = MAKESEGPTR( sel, 0 );
900 if (bits16) *bits16 = bmp->segptr_bits;
901 }
902 if (bmp) GDI_ReleaseObj( hbitmap );
Stephane Lussier23259ce2000-07-11 22:04:44 +0000903 }
Ove Kaaven8b9f3382000-04-29 16:47:07 +0000904 return hbitmap;
905}
906
907/***********************************************************************
908 * DIB_CreateDIBSection
909 */
910HBITMAP DIB_CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
911 LPVOID *bits, HANDLE section,
912 DWORD offset, DWORD ovr_pitch)
913{
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000914 HBITMAP hbitmap = 0;
Stephane Lussier23259ce2000-07-11 22:04:44 +0000915 DC *dc;
916 BOOL bDesktopDC = FALSE;
917
918 /* If the reference hdc is null, take the desktop dc */
919 if (hdc == 0)
920 {
921 hdc = CreateCompatibleDC(0);
922 bDesktopDC = TRUE;
923 }
924
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000925 if ((dc = DC_GetDCPtr( hdc )))
926 {
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000927 hbitmap = dc->funcs->pCreateDIBSection(dc->physDev, bmi, usage, bits, section, offset, ovr_pitch);
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000928 GDI_ReleaseObj(hdc);
929 }
Ulrich Weigand4f85bad1999-02-09 15:30:22 +0000930
Stephane Lussier23259ce2000-07-11 22:04:44 +0000931 if (bDesktopDC)
932 DeleteDC(hdc);
933
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000934 return hbitmap;
Alexandre Julliard77b99181997-09-14 17:17:23 +0000935}
936
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000937/***********************************************************************
Patrik Stridvalld0a41772001-02-14 23:11:17 +0000938 * CreateDIBSection (GDI32.@)
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000939 */
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000940HBITMAP WINAPI CreateDIBSection(HDC hdc, BITMAPINFO *bmi, UINT usage,
941 LPVOID *bits, HANDLE section,
942 DWORD offset)
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000943{
Ove Kaaven8b9f3382000-04-29 16:47:07 +0000944 return DIB_CreateDIBSection(hdc, bmi, usage, bits, section, offset, 0);
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000945}
Alexandre Julliarda845b881998-06-01 10:44:35 +0000946
Alexandre Julliard642d3131998-07-12 19:29:36 +0000947/***********************************************************************
Noel Borthwickd05b7be1999-09-20 15:42:47 +0000948 * DIB_CreateDIBFromBitmap
949 * Allocates a packed DIB and copies the bitmap data into it.
950 */
951HGLOBAL DIB_CreateDIBFromBitmap(HDC hdc, HBITMAP hBmp)
952{
953 BITMAPOBJ *pBmp = NULL;
954 HGLOBAL hPackedDIB = 0;
955 LPBYTE pPackedDIB = NULL;
956 LPBITMAPINFOHEADER pbmiHeader = NULL;
957 unsigned int width, height, depth, cDataSize = 0, cPackedSize = 0,
958 OffsetBits = 0, nLinesCopied = 0;
959
960 /* Get a pointer to the BITMAPOBJ structure */
961 pBmp = (BITMAPOBJ *)GDI_GetObjPtr( hBmp, BITMAP_MAGIC );
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000962 if (!pBmp) return hPackedDIB;
Noel Borthwickd05b7be1999-09-20 15:42:47 +0000963
964 /* Get the bitmap dimensions */
965 width = pBmp->bitmap.bmWidth;
966 height = pBmp->bitmap.bmHeight;
967 depth = pBmp->bitmap.bmBitsPixel;
968
969 /*
970 * A packed DIB contains a BITMAPINFO structure followed immediately by
971 * an optional color palette and the pixel data.
972 */
973
974 /* Calculate the size of the packed DIB */
975 cDataSize = DIB_GetDIBImageBytes( width, height, depth );
976 cPackedSize = sizeof(BITMAPINFOHEADER)
977 + ( (depth <= 8) ? (sizeof(RGBQUAD) * (1 << depth)) : 0 )
978 + cDataSize;
979 /* Get the offset to the bits */
980 OffsetBits = cPackedSize - cDataSize;
981
982 /* Allocate the packed DIB */
983 TRACE("\tAllocating packed DIB of size %d\n", cPackedSize);
984 hPackedDIB = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE /*| GMEM_ZEROINIT*/,
985 cPackedSize );
986 if ( !hPackedDIB )
987 {
988 WARN("Could not allocate packed DIB!\n");
989 goto END;
990 }
991
992 /* A packed DIB starts with a BITMAPINFOHEADER */
993 pPackedDIB = (LPBYTE)GlobalLock(hPackedDIB);
994 pbmiHeader = (LPBITMAPINFOHEADER)pPackedDIB;
995
996 /* Init the BITMAPINFOHEADER */
997 pbmiHeader->biSize = sizeof(BITMAPINFOHEADER);
998 pbmiHeader->biWidth = width;
999 pbmiHeader->biHeight = height;
1000 pbmiHeader->biPlanes = 1;
1001 pbmiHeader->biBitCount = depth;
1002 pbmiHeader->biCompression = BI_RGB;
1003 pbmiHeader->biSizeImage = 0;
1004 pbmiHeader->biXPelsPerMeter = pbmiHeader->biYPelsPerMeter = 0;
1005 pbmiHeader->biClrUsed = 0;
1006 pbmiHeader->biClrImportant = 0;
1007
1008 /* Retrieve the DIB bits from the bitmap and fill in the
1009 * DIB color table if present */
1010
1011 nLinesCopied = GetDIBits(hdc, /* Handle to device context */
1012 hBmp, /* Handle to bitmap */
1013 0, /* First scan line to set in dest bitmap */
1014 height, /* Number of scan lines to copy */
1015 pPackedDIB + OffsetBits, /* [out] Address of array for bitmap bits */
1016 (LPBITMAPINFO) pbmiHeader, /* [out] Address of BITMAPINFO structure */
1017 0); /* RGB or palette index */
1018 GlobalUnlock(hPackedDIB);
1019
1020 /* Cleanup if GetDIBits failed */
1021 if (nLinesCopied != height)
1022 {
1023 TRACE("\tGetDIBits returned %d. Actual lines=%d\n", nLinesCopied, height);
1024 GlobalFree(hPackedDIB);
1025 hPackedDIB = 0;
1026 }
1027
1028END:
Alexandre Julliard2a2321b2000-08-19 21:38:55 +00001029 GDI_ReleaseObj( hBmp );
Noel Borthwickd05b7be1999-09-20 15:42:47 +00001030 return hPackedDIB;
1031}