win32u: Move NtUserTranslateMessage implementation from user32.
[wine.git] / dlls / windowscodecs / bmpdecode.c
blobb7ea464d1250673a4e02473ce7376a9782ac4f83
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <assert.h>
20 #include <stdarg.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "wingdi.h"
28 #include "objbase.h"
30 #include "wincodecs_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
36 typedef struct {
37 DWORD bc2Size;
38 DWORD bc2Width;
39 DWORD bc2Height;
40 WORD bc2Planes;
41 WORD bc2BitCount;
42 DWORD bc2Compression;
43 DWORD bc2SizeImage;
44 DWORD bc2XRes;
45 DWORD bc2YRes;
46 DWORD bc2ClrUsed;
47 DWORD bc2ClrImportant;
48 /* same as BITMAPINFOHEADER until this point */
49 WORD bc2ResUnit;
50 WORD bc2Reserved;
51 WORD bc2Orientation;
52 WORD bc2Halftoning;
53 DWORD bc2HalftoneSize1;
54 DWORD bc2HalftoneSize2;
55 DWORD bc2ColorSpace;
56 DWORD bc2AppData;
57 } BITMAPCOREHEADER2;
59 typedef HRESULT (*ReadDataFunc)(BmpDecoder* This);
61 struct BmpDecoder {
62 IWICBitmapDecoder IWICBitmapDecoder_iface;
63 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
64 LONG ref;
65 BOOL initialized;
66 IStream *stream;
67 ULONG palette_offset;
68 ULONG image_offset;
69 BITMAPV5HEADER bih;
70 const WICPixelFormatGUID *pixelformat;
71 int bitsperpixel;
72 ReadDataFunc read_data_func;
73 INT stride;
74 BYTE *imagedata;
75 BYTE *imagedatastart;
76 CRITICAL_SECTION lock; /* must be held when initialized/imagedata is set or stream is accessed */
77 int packed; /* If TRUE, don't look for a file header and assume a packed DIB. */
78 int icoframe; /* If TRUE, this is a frame of a .ico file. */
81 static inline BmpDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
83 return CONTAINING_RECORD(iface, BmpDecoder, IWICBitmapDecoder_iface);
86 static inline BmpDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
88 return CONTAINING_RECORD(iface, BmpDecoder, IWICBitmapFrameDecode_iface);
91 static HRESULT WINAPI BmpFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
92 void **ppv)
94 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
96 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
98 if (!ppv) return E_INVALIDARG;
100 if (IsEqualIID(&IID_IUnknown, iid) ||
101 IsEqualIID(&IID_IWICBitmapSource, iid) ||
102 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
104 *ppv = &This->IWICBitmapFrameDecode_iface;
106 else
108 *ppv = NULL;
109 return E_NOINTERFACE;
112 IUnknown_AddRef((IUnknown*)*ppv);
113 return S_OK;
116 static ULONG WINAPI BmpFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
118 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
120 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
123 static ULONG WINAPI BmpFrameDecode_Release(IWICBitmapFrameDecode *iface)
125 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
127 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
130 static HRESULT WINAPI BmpFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
131 UINT *puiWidth, UINT *puiHeight)
133 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
134 TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
136 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
138 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
139 *puiWidth = bch->bcWidth;
140 *puiHeight = bch->bcHeight;
142 else
144 *puiWidth = This->bih.bV5Width;
145 *puiHeight = abs(This->bih.bV5Height);
147 return S_OK;
150 static HRESULT WINAPI BmpFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
151 WICPixelFormatGUID *pPixelFormat)
153 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
154 TRACE("(%p,%p)\n", iface, pPixelFormat);
156 memcpy(pPixelFormat, This->pixelformat, sizeof(GUID));
158 return S_OK;
161 static HRESULT BmpHeader_GetResolution(BITMAPV5HEADER *bih, double *pDpiX, double *pDpiY)
163 LONG resx = 0, resy = 0;
165 switch (bih->bV5Size)
167 default:
168 case sizeof(BITMAPCOREHEADER):
169 break;
171 case sizeof(BITMAPCOREHEADER2):
172 case sizeof(BITMAPINFOHEADER):
173 case sizeof(BITMAPV4HEADER):
174 case sizeof(BITMAPV5HEADER):
175 resx = bih->bV5XPelsPerMeter;
176 resy = bih->bV5YPelsPerMeter;
177 break;
180 if (!resx || !resy)
182 *pDpiX = 96.0;
183 *pDpiY = 96.0;
185 else
187 *pDpiX = resx * 0.0254;
188 *pDpiY = resy * 0.0254;
191 return S_OK;
194 static HRESULT WINAPI BmpFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
195 double *pDpiX, double *pDpiY)
197 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
198 TRACE("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
200 return BmpHeader_GetResolution(&This->bih, pDpiX, pDpiY);
203 static HRESULT WINAPI BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
204 IWICPalette *pIPalette)
206 HRESULT hr;
207 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
208 int count;
209 WICColor *wiccolors=NULL;
210 RGBTRIPLE *bgrcolors=NULL;
212 TRACE("(%p,%p)\n", iface, pIPalette);
214 EnterCriticalSection(&This->lock);
216 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
218 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
219 if (bch->bcBitCount <= 8)
221 /* 2**n colors in BGR format after the header */
222 ULONG tablesize, bytesread;
223 LARGE_INTEGER offset;
224 int i;
226 count = 1 << bch->bcBitCount;
227 wiccolors = HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor) * count);
228 tablesize = sizeof(RGBTRIPLE) * count;
229 bgrcolors = HeapAlloc(GetProcessHeap(), 0, tablesize);
230 if (!wiccolors || !bgrcolors)
232 hr = E_OUTOFMEMORY;
233 goto end;
236 offset.QuadPart = This->palette_offset;
237 hr = IStream_Seek(This->stream, offset, STREAM_SEEK_SET, NULL);
238 if (FAILED(hr)) goto end;
240 hr = IStream_Read(This->stream, bgrcolors, tablesize, &bytesread);
241 if (FAILED(hr)) goto end;
242 if (bytesread != tablesize) {
243 hr = E_FAIL;
244 goto end;
247 for (i=0; i<count; i++)
249 wiccolors[i] = 0xff000000|
250 (bgrcolors[i].rgbtRed<<16)|
251 (bgrcolors[i].rgbtGreen<<8)|
252 bgrcolors[i].rgbtBlue;
255 else
257 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
258 goto end;
261 else
263 if (This->bih.bV5BitCount <= 8)
265 ULONG tablesize, bytesread;
266 LARGE_INTEGER offset;
267 int i;
269 if (This->bih.bV5ClrUsed == 0)
270 count = 1 << This->bih.bV5BitCount;
271 else
272 count = min(This->bih.bV5ClrUsed, 1 << This->bih.bV5BitCount);
274 tablesize = sizeof(WICColor) * count;
275 wiccolors = HeapAlloc(GetProcessHeap(), 0, tablesize);
276 if (!wiccolors)
278 hr = E_OUTOFMEMORY;
279 goto end;
282 offset.QuadPart = This->palette_offset;
283 hr = IStream_Seek(This->stream, offset, STREAM_SEEK_SET, NULL);
284 if (FAILED(hr)) goto end;
286 hr = IStream_Read(This->stream, wiccolors, tablesize, &bytesread);
287 if (FAILED(hr)) goto end;
288 if (bytesread != tablesize) {
289 hr = E_FAIL;
290 goto end;
293 /* convert from BGR to BGRA by setting alpha to 100% */
294 for (i=0; i<count; i++)
295 wiccolors[i] |= 0xff000000;
297 else
299 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
300 goto end;
304 end:
306 LeaveCriticalSection(&This->lock);
308 if (SUCCEEDED(hr))
309 hr = IWICPalette_InitializeCustom(pIPalette, wiccolors, count);
311 HeapFree(GetProcessHeap(), 0, wiccolors);
312 HeapFree(GetProcessHeap(), 0, bgrcolors);
313 return hr;
316 static HRESULT WINAPI BmpFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
317 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
319 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
320 HRESULT hr=S_OK;
321 UINT width, height;
322 TRACE("(%p,%s,%u,%u,%p)\n", iface, debug_wic_rect(prc), cbStride, cbBufferSize, pbBuffer);
324 EnterCriticalSection(&This->lock);
325 if (!This->imagedata)
327 hr = This->read_data_func(This);
329 LeaveCriticalSection(&This->lock);
330 if (FAILED(hr)) return hr;
332 hr = BmpFrameDecode_GetSize(iface, &width, &height);
333 if (FAILED(hr)) return hr;
335 return copy_pixels(This->bitsperpixel, This->imagedatastart,
336 width, height, This->stride,
337 prc, cbStride, cbBufferSize, pbBuffer);
340 static HRESULT WINAPI BmpFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
341 IWICMetadataQueryReader **ppIMetadataQueryReader)
343 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
344 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
347 static HRESULT WINAPI BmpFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
348 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
350 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
351 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
354 static HRESULT WINAPI BmpFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
355 IWICBitmapSource **ppIThumbnail)
357 TRACE("(%p,%p)\n", iface, ppIThumbnail);
358 return WINCODEC_ERR_CODECNOTHUMBNAIL;
361 static HRESULT BmpFrameDecode_ReadUncompressed(BmpDecoder* This)
363 UINT bytesperrow;
364 UINT width, height;
365 UINT datasize;
366 int bottomup;
367 HRESULT hr;
368 LARGE_INTEGER offbits;
369 ULONG bytesread;
371 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
373 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
374 width = bch->bcWidth;
375 height = bch->bcHeight;
376 bottomup = 1;
378 else
380 width = This->bih.bV5Width;
381 height = abs(This->bih.bV5Height);
382 bottomup = (This->bih.bV5Height > 0);
385 /* row sizes in BMP files must be divisible by 4 bytes */
386 bytesperrow = (((width * This->bitsperpixel)+31)/32)*4;
387 datasize = bytesperrow * height;
389 This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
390 if (!This->imagedata) return E_OUTOFMEMORY;
392 offbits.QuadPart = This->image_offset;
393 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
394 if (FAILED(hr)) goto fail;
396 hr = IStream_Read(This->stream, This->imagedata, datasize, &bytesread);
397 if (FAILED(hr) || bytesread != datasize) goto fail;
399 if (bottomup)
401 This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
402 This->stride = -bytesperrow;
404 else
406 This->imagedatastart = This->imagedata;
407 This->stride = bytesperrow;
409 return S_OK;
411 fail:
412 HeapFree(GetProcessHeap(), 0, This->imagedata);
413 This->imagedata = NULL;
414 if (SUCCEEDED(hr)) hr = E_FAIL;
415 return hr;
418 static HRESULT BmpFrameDecode_ReadRGB8(BmpDecoder* This)
420 HRESULT hr;
421 UINT width, height;
423 hr = IWICBitmapFrameDecode_GetSize(&This->IWICBitmapFrameDecode_iface, &width, &height);
425 if (SUCCEEDED(hr))
427 hr = BmpFrameDecode_ReadUncompressed(This);
430 if (SUCCEEDED(hr))
432 reverse_bgr8(This->bitsperpixel/8, This->imagedatastart,
433 width, height, This->stride);
436 return hr;
439 static HRESULT ReadByte(IStream *stream, BYTE *buffer, ULONG buffer_size,
440 ULONG *cursor, ULONG *bytesread, BYTE *result)
442 HRESULT hr=S_OK;
444 if (*bytesread == 0 || *cursor == *bytesread)
446 hr = IStream_Read(stream, buffer, buffer_size, bytesread);
447 *cursor = 0;
450 if (SUCCEEDED(hr))
452 if (*cursor < *bytesread)
453 *result = buffer[(*cursor)++];
454 else
455 hr = E_FAIL;
458 return hr;
461 static HRESULT BmpFrameDecode_ReadRLE8(BmpDecoder* This)
463 UINT bytesperrow;
464 UINT width, height;
465 BYTE rledata[4096];
466 UINT datasize, palettesize;
467 DWORD palette[256];
468 UINT x, y;
469 DWORD *bgrdata;
470 HRESULT hr;
471 LARGE_INTEGER offbits;
472 ULONG cursor=0, bytesread=0;
474 width = This->bih.bV5Width;
475 height = abs(This->bih.bV5Height);
476 bytesperrow = width * 4;
477 datasize = bytesperrow * height;
478 if (This->bih.bV5ClrUsed && This->bih.bV5ClrUsed < 256)
479 palettesize = 4 * This->bih.bV5ClrUsed;
480 else
481 palettesize = 4 * 256;
483 This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
484 if (!This->imagedata)
486 hr = E_OUTOFMEMORY;
487 goto fail;
490 /* read palette */
491 offbits.QuadPart = This->palette_offset;
492 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
493 if (FAILED(hr)) goto fail;
495 hr = IStream_Read(This->stream, palette, palettesize, &bytesread);
496 if (FAILED(hr) || bytesread != palettesize) goto fail;
498 /* read RLE data */
499 offbits.QuadPart = This->image_offset;
500 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
501 if (FAILED(hr)) goto fail;
503 /* decode RLE */
504 bgrdata = (DWORD*)This->imagedata;
505 x = 0;
506 y = 0;
507 cursor = 0;
508 bytesread = 0;
509 while (y < height)
511 BYTE length;
512 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &length);
514 if (FAILED(hr))
515 goto fail;
516 else if (length == 0)
518 /* escape code */
519 BYTE escape;
520 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &escape);
521 if (FAILED(hr))
522 goto fail;
523 switch(escape)
525 case 0: /* end of line */
526 x = 0;
527 y++;
528 break;
529 case 1: /* end of bitmap */
530 goto end;
531 case 2: /* delta */
533 BYTE dx, dy;
534 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &dx);
535 if (SUCCEEDED(hr))
536 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &dy);
537 if (FAILED(hr))
538 goto fail;
539 x += dx;
540 y += dy;
541 break;
543 default: /* absolute mode */
544 length = escape;
545 while (length-- && x < width)
547 BYTE index;
548 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &index);
549 if (FAILED(hr))
550 goto fail;
551 bgrdata[y*width + x++] = palette[index];
553 if (escape & 1)
554 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &length); /* skip pad byte */
555 if (FAILED(hr))
556 goto fail;
559 else
561 BYTE index;
562 DWORD color;
563 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &index);
564 if (FAILED(hr))
565 goto fail;
566 color = palette[index];
567 while (length-- && x < width)
568 bgrdata[y*width + x++] = color;
572 end:
573 This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
574 This->stride = -bytesperrow;
576 return S_OK;
578 fail:
579 HeapFree(GetProcessHeap(), 0, This->imagedata);
580 This->imagedata = NULL;
581 if (SUCCEEDED(hr)) hr = E_FAIL;
582 return hr;
585 static HRESULT BmpFrameDecode_ReadRLE4(BmpDecoder* This)
587 UINT bytesperrow;
588 UINT width, height;
589 BYTE rledata[4096];
590 UINT datasize, palettesize;
591 DWORD palette[16];
592 UINT x, y;
593 DWORD *bgrdata;
594 HRESULT hr;
595 LARGE_INTEGER offbits;
596 ULONG cursor=0, bytesread=0;
598 width = This->bih.bV5Width;
599 height = abs(This->bih.bV5Height);
600 bytesperrow = width * 4;
601 datasize = bytesperrow * height;
602 if (This->bih.bV5ClrUsed && This->bih.bV5ClrUsed < 16)
603 palettesize = 4 * This->bih.bV5ClrUsed;
604 else
605 palettesize = 4 * 16;
607 This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
608 if (!This->imagedata)
610 hr = E_OUTOFMEMORY;
611 goto fail;
614 /* read palette */
615 offbits.QuadPart = This->palette_offset;
616 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
617 if (FAILED(hr)) goto fail;
619 hr = IStream_Read(This->stream, palette, palettesize, &bytesread);
620 if (FAILED(hr) || bytesread != palettesize) goto fail;
622 /* read RLE data */
623 offbits.QuadPart = This->image_offset;
624 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
625 if (FAILED(hr)) goto fail;
627 /* decode RLE */
628 bgrdata = (DWORD*)This->imagedata;
629 x = 0;
630 y = 0;
631 cursor = 0;
632 bytesread = 0;
633 while (y < height)
635 BYTE length;
636 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &length);
638 if (FAILED(hr))
639 goto fail;
640 else if (length == 0)
642 /* escape code */
643 BYTE escape;
644 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &escape);
645 if (FAILED(hr))
646 goto fail;
647 switch(escape)
649 case 0: /* end of line */
650 x = 0;
651 y++;
652 break;
653 case 1: /* end of bitmap */
654 goto end;
655 case 2: /* delta */
657 BYTE dx, dy;
658 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &dx);
659 if (SUCCEEDED(hr))
660 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &dy);
661 if (FAILED(hr))
662 goto fail;
663 x += dx;
664 y += dy;
665 break;
667 default: /* absolute mode */
669 BYTE realsize=0;
670 length = escape;
671 while (length-- && x < width)
673 BYTE colors;
674 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &colors);
675 realsize++;
676 if (FAILED(hr))
677 goto fail;
678 bgrdata[y*width + x++] = palette[colors>>4];
679 if (length-- && x < width)
680 bgrdata[y*width + x++] = palette[colors&0xf];
681 else
682 break;
684 if (realsize & 1)
685 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &length); /* skip pad byte */
686 if (FAILED(hr))
687 goto fail;
691 else
693 BYTE colors;
694 DWORD color1;
695 DWORD color2;
696 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &colors);
697 if (FAILED(hr))
698 goto fail;
699 color1 = palette[colors>>4];
700 color2 = palette[colors&0xf];
701 while (length-- && x < width)
703 bgrdata[y*width + x++] = color1;
704 if (length-- && x < width)
705 bgrdata[y*width + x++] = color2;
706 else
707 break;
712 end:
713 This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
714 This->stride = -bytesperrow;
716 return S_OK;
718 fail:
719 HeapFree(GetProcessHeap(), 0, This->imagedata);
720 This->imagedata = NULL;
721 if (SUCCEEDED(hr)) hr = E_FAIL;
722 return hr;
725 static HRESULT BmpFrameDecode_ReadUnsupported(BmpDecoder* This)
727 return E_FAIL;
730 struct bitfields_format {
731 WORD bitcount; /* 0 for end of list */
732 DWORD redmask;
733 DWORD greenmask;
734 DWORD bluemask;
735 DWORD alphamask;
736 const WICPixelFormatGUID *pixelformat;
737 ReadDataFunc read_data_func;
740 static const struct bitfields_format bitfields_formats[] = {
741 {16,0x7c00,0x3e0,0x1f,0,&GUID_WICPixelFormat16bppBGR555,BmpFrameDecode_ReadUncompressed},
742 {16,0xf800,0x7e0,0x1f,0,&GUID_WICPixelFormat16bppBGR565,BmpFrameDecode_ReadUncompressed},
743 {32,0xff0000,0xff00,0xff,0,&GUID_WICPixelFormat32bppBGR,BmpFrameDecode_ReadUncompressed},
744 {32,0xff0000,0xff00,0xff,0xff000000,&GUID_WICPixelFormat32bppBGRA,BmpFrameDecode_ReadUncompressed},
745 {32,0xff,0xff00,0xff0000,0,&GUID_WICPixelFormat32bppBGR,BmpFrameDecode_ReadRGB8},
749 static const IWICBitmapFrameDecodeVtbl BmpDecoder_FrameVtbl = {
750 BmpFrameDecode_QueryInterface,
751 BmpFrameDecode_AddRef,
752 BmpFrameDecode_Release,
753 BmpFrameDecode_GetSize,
754 BmpFrameDecode_GetPixelFormat,
755 BmpFrameDecode_GetResolution,
756 BmpFrameDecode_CopyPalette,
757 BmpFrameDecode_CopyPixels,
758 BmpFrameDecode_GetMetadataQueryReader,
759 BmpFrameDecode_GetColorContexts,
760 BmpFrameDecode_GetThumbnail
763 static HRESULT BmpDecoder_ReadHeaders(BmpDecoder* This, IStream *stream)
765 HRESULT hr;
766 ULONG bytestoread, bytesread;
767 LARGE_INTEGER seek;
769 if (This->initialized) return WINCODEC_ERR_WRONGSTATE;
771 seek.QuadPart = 0;
772 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
773 if (FAILED(hr)) return hr;
775 if (!This->packed)
777 BITMAPFILEHEADER bfh;
778 hr = IStream_Read(stream, &bfh, sizeof(BITMAPFILEHEADER), &bytesread);
779 if (FAILED(hr)) return hr;
780 if (bytesread != sizeof(BITMAPFILEHEADER) ||
781 bfh.bfType != 0x4d42 /* "BM" */) return E_FAIL;
782 This->image_offset = bfh.bfOffBits;
785 hr = IStream_Read(stream, &This->bih.bV5Size, sizeof(DWORD), &bytesread);
786 if (FAILED(hr)) return hr;
787 if (bytesread != sizeof(DWORD) ||
788 (This->bih.bV5Size != sizeof(BITMAPCOREHEADER) &&
789 This->bih.bV5Size != sizeof(BITMAPCOREHEADER2) &&
790 This->bih.bV5Size != sizeof(BITMAPINFOHEADER) &&
791 This->bih.bV5Size != sizeof(BITMAPV4HEADER) &&
792 This->bih.bV5Size != sizeof(BITMAPV5HEADER))) return E_FAIL;
794 bytestoread = This->bih.bV5Size-sizeof(DWORD);
795 hr = IStream_Read(stream, &This->bih.bV5Width, bytestoread, &bytesread);
796 if (FAILED(hr)) return hr;
797 if (bytestoread != bytesread) return E_FAIL;
799 if (This->packed)
800 This->palette_offset = This->bih.bV5Size;
801 else
802 This->palette_offset = sizeof(BITMAPFILEHEADER) + This->bih.bV5Size;
804 if (This->icoframe)
806 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
808 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
809 bch->bcHeight /= 2;
811 else
813 This->bih.bV5Height /= 2;
817 /* if this is a BITMAPINFOHEADER with BI_BITFIELDS compression, we need to
818 read the extra fields */
819 if (This->bih.bV5Size == sizeof(BITMAPINFOHEADER) &&
820 This->bih.bV5Compression == BI_BITFIELDS)
822 hr = IStream_Read(stream, &This->bih.bV5RedMask, 12, &bytesread);
823 if (FAILED(hr)) return hr;
824 if (bytesread != 12) return E_FAIL;
825 This->bih.bV5AlphaMask = 0;
826 This->palette_offset += 12;
829 /* decide what kind of bitmap this is and how/if we can read it */
830 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
832 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
833 TRACE("BITMAPCOREHEADER with depth=%i\n", bch->bcBitCount);
834 This->bitsperpixel = bch->bcBitCount;
835 This->read_data_func = BmpFrameDecode_ReadUncompressed;
836 switch(bch->bcBitCount)
838 case 1:
839 This->pixelformat = &GUID_WICPixelFormat1bppIndexed;
840 break;
841 case 2:
842 This->pixelformat = &GUID_WICPixelFormat2bppIndexed;
843 break;
844 case 4:
845 This->pixelformat = &GUID_WICPixelFormat4bppIndexed;
846 break;
847 case 8:
848 This->pixelformat = &GUID_WICPixelFormat8bppIndexed;
849 break;
850 case 24:
851 This->pixelformat = &GUID_WICPixelFormat24bppBGR;
852 break;
853 default:
854 This->pixelformat = &GUID_WICPixelFormatUndefined;
855 WARN("unsupported bit depth %i for BITMAPCOREHEADER\n", bch->bcBitCount);
856 break;
859 else /* struct is compatible with BITMAPINFOHEADER */
861 TRACE("bitmap header=%li compression=%li depth=%i\n", This->bih.bV5Size, This->bih.bV5Compression, This->bih.bV5BitCount);
862 switch(This->bih.bV5Compression)
864 case BI_RGB:
865 This->bitsperpixel = This->bih.bV5BitCount;
866 This->read_data_func = BmpFrameDecode_ReadUncompressed;
867 switch(This->bih.bV5BitCount)
869 case 1:
870 This->pixelformat = &GUID_WICPixelFormat1bppIndexed;
871 break;
872 case 2:
873 This->pixelformat = &GUID_WICPixelFormat2bppIndexed;
874 break;
875 case 4:
876 This->pixelformat = &GUID_WICPixelFormat4bppIndexed;
877 break;
878 case 8:
879 This->pixelformat = &GUID_WICPixelFormat8bppIndexed;
880 break;
881 case 16:
882 This->pixelformat = &GUID_WICPixelFormat16bppBGR555;
883 break;
884 case 24:
885 This->pixelformat = &GUID_WICPixelFormat24bppBGR;
886 break;
887 case 32:
888 This->pixelformat = &GUID_WICPixelFormat32bppBGR;
889 break;
890 default:
891 This->pixelformat = &GUID_WICPixelFormatUndefined;
892 FIXME("unsupported bit depth %i for uncompressed RGB\n", This->bih.bV5BitCount);
894 break;
895 case BI_RLE8:
896 This->bitsperpixel = 32;
897 This->read_data_func = BmpFrameDecode_ReadRLE8;
898 This->pixelformat = &GUID_WICPixelFormat32bppBGR;
899 break;
900 case BI_RLE4:
901 This->bitsperpixel = 32;
902 This->read_data_func = BmpFrameDecode_ReadRLE4;
903 This->pixelformat = &GUID_WICPixelFormat32bppBGR;
904 break;
905 case BI_BITFIELDS:
907 const struct bitfields_format *format;
908 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER2))
910 /* BCH2 doesn't support bitfields; this is Huffman 1D compression */
911 This->bitsperpixel = 0;
912 This->read_data_func = BmpFrameDecode_ReadUnsupported;
913 This->pixelformat = &GUID_WICPixelFormatUndefined;
914 FIXME("Huffman 1D compression is unsupported\n");
915 break;
917 This->bitsperpixel = This->bih.bV5BitCount;
918 for (format = bitfields_formats; format->bitcount; format++)
920 if ((format->bitcount == This->bih.bV5BitCount) &&
921 (format->redmask == This->bih.bV5RedMask) &&
922 (format->greenmask == This->bih.bV5GreenMask) &&
923 (format->bluemask == This->bih.bV5BlueMask) &&
924 (format->alphamask == This->bih.bV5AlphaMask))
926 This->read_data_func = format->read_data_func;
927 This->pixelformat = format->pixelformat;
928 break;
931 if (!format->bitcount)
933 This->read_data_func = BmpFrameDecode_ReadUncompressed;
934 This->pixelformat = &GUID_WICPixelFormatUndefined;
935 FIXME("unsupported bitfields type depth=%i red=%lx green=%lx blue=%lx alpha=%lx\n",
936 This->bih.bV5BitCount, This->bih.bV5RedMask, This->bih.bV5GreenMask, This->bih.bV5BlueMask, This->bih.bV5AlphaMask);
938 break;
940 default:
941 This->bitsperpixel = 0;
942 This->read_data_func = BmpFrameDecode_ReadUnsupported;
943 This->pixelformat = &GUID_WICPixelFormatUndefined;
944 FIXME("unsupported bitmap type header=%li compression=%li depth=%i\n", This->bih.bV5Size, This->bih.bV5Compression, This->bih.bV5BitCount);
945 break;
949 if (This->packed)
951 /* In a packed DIB, the image follows the palette. */
952 ULONG palette_count, palette_size;
953 if (This->bih.bV5ClrUsed)
954 palette_count = This->bih.bV5ClrUsed;
955 else if (This->bih.bV5BitCount <= 8)
956 palette_count = 1 << This->bih.bV5BitCount;
957 else
958 palette_count = 0;
959 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
960 palette_size = sizeof(RGBTRIPLE) * palette_count;
961 else
962 palette_size = sizeof(RGBQUAD) * palette_count;
963 This->image_offset = This->palette_offset + palette_size;
966 This->initialized = TRUE;
968 return S_OK;
971 static HRESULT WINAPI BmpDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
972 void **ppv)
974 BmpDecoder *This = impl_from_IWICBitmapDecoder(iface);
975 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
977 if (!ppv) return E_INVALIDARG;
979 if (IsEqualIID(&IID_IUnknown, iid) ||
980 IsEqualIID(&IID_IWICBitmapDecoder, iid))
982 *ppv = &This->IWICBitmapDecoder_iface;
984 else
986 *ppv = NULL;
987 return E_NOINTERFACE;
990 IUnknown_AddRef((IUnknown*)*ppv);
991 return S_OK;
994 static ULONG WINAPI BmpDecoder_AddRef(IWICBitmapDecoder *iface)
996 BmpDecoder *This = impl_from_IWICBitmapDecoder(iface);
997 ULONG ref = InterlockedIncrement(&This->ref);
999 TRACE("(%p) refcount=%lu\n", iface, ref);
1001 return ref;
1004 static ULONG WINAPI BmpDecoder_Release(IWICBitmapDecoder *iface)
1006 BmpDecoder *This = impl_from_IWICBitmapDecoder(iface);
1007 ULONG ref = InterlockedDecrement(&This->ref);
1009 TRACE("(%p) refcount=%lu\n", iface, ref);
1011 if (ref == 0)
1013 if (This->stream) IStream_Release(This->stream);
1014 HeapFree(GetProcessHeap(), 0, This->imagedata);
1015 This->lock.DebugInfo->Spare[0] = 0;
1016 DeleteCriticalSection(&This->lock);
1017 HeapFree(GetProcessHeap(), 0, This);
1020 return ref;
1023 static HRESULT WINAPI BmpDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
1024 DWORD *capability)
1026 HRESULT hr;
1027 BmpDecoder *This = impl_from_IWICBitmapDecoder(iface);
1029 TRACE("(%p,%p,%p)\n", iface, stream, capability);
1031 if (!stream || !capability) return E_INVALIDARG;
1033 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
1034 if (hr != S_OK) return hr;
1036 *capability = This->read_data_func == BmpFrameDecode_ReadUnsupported ? 0 : WICBitmapDecoderCapabilityCanDecodeAllImages;
1037 return S_OK;
1040 static HRESULT WINAPI BmpDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
1041 WICDecodeOptions cacheOptions)
1043 HRESULT hr;
1044 BmpDecoder *This = impl_from_IWICBitmapDecoder(iface);
1046 EnterCriticalSection(&This->lock);
1047 hr = BmpDecoder_ReadHeaders(This, pIStream);
1049 if (SUCCEEDED(hr))
1051 This->stream = pIStream;
1052 IStream_AddRef(pIStream);
1054 LeaveCriticalSection(&This->lock);
1056 return hr;
1059 static HRESULT WINAPI BmpDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
1060 GUID *pguidContainerFormat)
1062 memcpy(pguidContainerFormat, &GUID_ContainerFormatBmp, sizeof(GUID));
1063 return S_OK;
1066 static HRESULT WINAPI BmpDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
1067 IWICBitmapDecoderInfo **ppIDecoderInfo)
1069 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
1071 return get_decoder_info(&CLSID_WICBmpDecoder, ppIDecoderInfo);
1074 static HRESULT WINAPI BmpDecoder_CopyPalette(IWICBitmapDecoder *iface,
1075 IWICPalette *pIPalette)
1077 TRACE("(%p,%p)\n", iface, pIPalette);
1079 return WINCODEC_ERR_PALETTEUNAVAILABLE;
1082 static HRESULT WINAPI BmpDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
1083 IWICMetadataQueryReader **ppIMetadataQueryReader)
1085 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
1086 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1089 static HRESULT WINAPI BmpDecoder_GetPreview(IWICBitmapDecoder *iface,
1090 IWICBitmapSource **ppIBitmapSource)
1092 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
1093 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1096 static HRESULT WINAPI BmpDecoder_GetColorContexts(IWICBitmapDecoder *iface,
1097 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1099 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
1100 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1103 static HRESULT WINAPI BmpDecoder_GetThumbnail(IWICBitmapDecoder *iface,
1104 IWICBitmapSource **ppIThumbnail)
1106 TRACE("(%p,%p)\n", iface, ppIThumbnail);
1107 return WINCODEC_ERR_CODECNOTHUMBNAIL;
1110 static HRESULT WINAPI BmpDecoder_GetFrameCount(IWICBitmapDecoder *iface,
1111 UINT *pCount)
1113 if (!pCount) return E_INVALIDARG;
1115 *pCount = 1;
1116 return S_OK;
1119 static HRESULT WINAPI BmpDecoder_GetFrame(IWICBitmapDecoder *iface,
1120 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
1122 BmpDecoder *This = impl_from_IWICBitmapDecoder(iface);
1124 if (index != 0) return E_INVALIDARG;
1126 if (!This->stream) return WINCODEC_ERR_FRAMEMISSING;
1128 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
1129 IWICBitmapDecoder_AddRef(iface);
1131 return S_OK;
1134 static const IWICBitmapDecoderVtbl BmpDecoder_Vtbl = {
1135 BmpDecoder_QueryInterface,
1136 BmpDecoder_AddRef,
1137 BmpDecoder_Release,
1138 BmpDecoder_QueryCapability,
1139 BmpDecoder_Initialize,
1140 BmpDecoder_GetContainerFormat,
1141 BmpDecoder_GetDecoderInfo,
1142 BmpDecoder_CopyPalette,
1143 BmpDecoder_GetMetadataQueryReader,
1144 BmpDecoder_GetPreview,
1145 BmpDecoder_GetColorContexts,
1146 BmpDecoder_GetThumbnail,
1147 BmpDecoder_GetFrameCount,
1148 BmpDecoder_GetFrame
1151 static HRESULT BmpDecoder_Create(int packed, int icoframe, BmpDecoder **ppDecoder)
1153 BmpDecoder *This;
1155 This = HeapAlloc(GetProcessHeap(), 0, sizeof(BmpDecoder));
1156 if (!This) return E_OUTOFMEMORY;
1158 This->IWICBitmapDecoder_iface.lpVtbl = &BmpDecoder_Vtbl;
1159 This->IWICBitmapFrameDecode_iface.lpVtbl = &BmpDecoder_FrameVtbl;
1160 This->ref = 1;
1161 This->initialized = FALSE;
1162 This->stream = NULL;
1163 This->imagedata = NULL;
1164 InitializeCriticalSection(&This->lock);
1165 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": BmpDecoder.lock");
1166 This->packed = packed;
1167 This->icoframe = icoframe;
1169 *ppDecoder = This;
1171 return S_OK;
1174 static HRESULT BmpDecoder_Construct(int packed, int icoframe, REFIID iid, void** ppv)
1176 BmpDecoder *This;
1177 HRESULT ret;
1179 TRACE("(%s,%p)\n", debugstr_guid(iid), ppv);
1181 *ppv = NULL;
1183 ret = BmpDecoder_Create(packed, icoframe, &This);
1184 if (FAILED(ret)) return ret;
1186 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1187 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1189 return ret;
1192 HRESULT BmpDecoder_CreateInstance(REFIID iid, void** ppv)
1194 return BmpDecoder_Construct(FALSE, FALSE, iid, ppv);
1197 HRESULT DibDecoder_CreateInstance(REFIID iid, void** ppv)
1199 return BmpDecoder_Construct(TRUE, FALSE, iid, ppv);
1202 HRESULT IcoDibDecoder_CreateInstance(BmpDecoder **ppDecoder)
1204 return BmpDecoder_Create(TRUE, TRUE, ppDecoder);
1207 void BmpDecoder_GetWICDecoder(BmpDecoder *This, IWICBitmapDecoder **ppDecoder)
1209 *ppDecoder = &This->IWICBitmapDecoder_iface;
1212 /* Return the offset where the mask of an icon might be, or 0 for failure. */
1213 void BmpDecoder_FindIconMask(BmpDecoder *This, ULONG *mask_offset, int *topdown)
1215 assert(This->stream != NULL);
1217 if (This->read_data_func == BmpFrameDecode_ReadUncompressed)
1219 /* RGB or BITFIELDS data */
1220 UINT width, height;
1221 ULONG bytesperrow, datasize;
1222 IWICBitmapFrameDecode_GetSize(&This->IWICBitmapFrameDecode_iface, &width, &height);
1223 bytesperrow = (((width * This->bitsperpixel)+31)/32)*4;
1224 datasize = bytesperrow * height;
1225 *mask_offset = This->image_offset + datasize;
1227 else
1228 *mask_offset = 0;
1230 *topdown = This->stride > 0;