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
33 #include "wincodecs_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
50 DWORD bc2ClrImportant;
51 /* same as BITMAPINFOHEADER until this point */
56 DWORD bc2HalftoneSize1;
57 DWORD bc2HalftoneSize2;
63 typedef HRESULT (*ReadDataFunc)(struct BmpDecoder* This);
65 typedef struct BmpDecoder {
66 const IWICBitmapDecoderVtbl *lpVtbl;
67 const IWICBitmapFrameDecodeVtbl *lpFrameVtbl;
74 const WICPixelFormatGUID *pixelformat;
76 ReadDataFunc read_data_func;
80 CRITICAL_SECTION lock; /* must be held when initialized/imagedata is set or stream is accessed */
81 int packed; /* If TRUE, don't look for a file header and assume a packed DIB. */
82 int icoframe; /* If TRUE, this is a frame of a .ico file. */
85 static inline BmpDecoder *impl_from_frame(IWICBitmapFrameDecode *iface)
87 return CONTAINING_RECORD(iface, BmpDecoder, lpFrameVtbl);
90 static HRESULT WINAPI BmpFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
93 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
95 if (!ppv) return E_INVALIDARG;
97 if (IsEqualIID(&IID_IUnknown, iid) ||
98 IsEqualIID(&IID_IWICBitmapSource, iid) ||
99 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
106 return E_NOINTERFACE;
109 IUnknown_AddRef((IUnknown*)*ppv);
113 static ULONG WINAPI BmpFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
115 BmpDecoder *This = impl_from_frame(iface);
117 return IUnknown_AddRef((IUnknown*)This);
120 static ULONG WINAPI BmpFrameDecode_Release(IWICBitmapFrameDecode *iface)
122 BmpDecoder *This = impl_from_frame(iface);
124 return IUnknown_Release((IUnknown*)This);
127 static HRESULT WINAPI BmpFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
128 UINT *puiWidth, UINT *puiHeight)
130 BmpDecoder *This = impl_from_frame(iface);
131 TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
133 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
135 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
136 *puiWidth = bch->bcWidth;
137 *puiHeight = bch->bcHeight;
141 *puiWidth = This->bih.bV5Width;
142 *puiHeight = abs(This->bih.bV5Height);
147 static HRESULT WINAPI BmpFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
148 WICPixelFormatGUID *pPixelFormat)
150 BmpDecoder *This = impl_from_frame(iface);
151 TRACE("(%p,%p)\n", iface, pPixelFormat);
153 memcpy(pPixelFormat, This->pixelformat, sizeof(GUID));
158 static HRESULT BmpHeader_GetResolution(BITMAPV5HEADER *bih, double *pDpiX, double *pDpiY)
160 switch (bih->bV5Size)
162 case sizeof(BITMAPCOREHEADER):
166 case sizeof(BITMAPCOREHEADER2):
167 case sizeof(BITMAPINFOHEADER):
168 case sizeof(BITMAPV4HEADER):
169 case sizeof(BITMAPV5HEADER):
170 *pDpiX = bih->bV5XPelsPerMeter * 0.0254;
171 *pDpiY = bih->bV5YPelsPerMeter * 0.0254;
178 static HRESULT WINAPI BmpFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
179 double *pDpiX, double *pDpiY)
181 BmpDecoder *This = impl_from_frame(iface);
182 TRACE("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
184 return BmpHeader_GetResolution(&This->bih, pDpiX, pDpiY);
187 static HRESULT WINAPI BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
188 IWICPalette *pIPalette)
191 BmpDecoder *This = impl_from_frame(iface);
193 WICColor *wiccolors=NULL;
194 RGBTRIPLE *bgrcolors=NULL;
196 TRACE("(%p,%p)\n", iface, pIPalette);
198 EnterCriticalSection(&This->lock);
200 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
202 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
203 if (bch->bcBitCount <= 8)
205 /* 2**n colors in BGR format after the header */
206 ULONG tablesize, bytesread;
207 LARGE_INTEGER offset;
210 count = 1 << bch->bcBitCount;
211 wiccolors = HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor) * count);
212 tablesize = sizeof(RGBTRIPLE) * count;
213 bgrcolors = HeapAlloc(GetProcessHeap(), 0, tablesize);
214 if (!wiccolors || !bgrcolors)
220 offset.QuadPart = This->palette_offset;
221 hr = IStream_Seek(This->stream, offset, STREAM_SEEK_SET, NULL);
222 if (FAILED(hr)) goto end;
224 hr = IStream_Read(This->stream, bgrcolors, tablesize, &bytesread);
225 if (FAILED(hr)) goto end;
226 if (bytesread != tablesize) {
231 for (i=0; i<count; i++)
233 wiccolors[i] = 0xff000000|
234 (bgrcolors[i].rgbtRed<<16)|
235 (bgrcolors[i].rgbtGreen<<8)|
236 bgrcolors[i].rgbtBlue;
241 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
247 if (This->bih.bV5BitCount <= 8)
249 ULONG tablesize, bytesread;
250 LARGE_INTEGER offset;
253 if (This->bih.bV5ClrUsed == 0)
254 count = 1 << This->bih.bV5BitCount;
256 count = This->bih.bV5ClrUsed;
258 tablesize = sizeof(WICColor) * count;
259 wiccolors = HeapAlloc(GetProcessHeap(), 0, tablesize);
266 offset.QuadPart = This->palette_offset;
267 hr = IStream_Seek(This->stream, offset, STREAM_SEEK_SET, NULL);
268 if (FAILED(hr)) goto end;
270 hr = IStream_Read(This->stream, wiccolors, tablesize, &bytesread);
271 if (FAILED(hr)) goto end;
272 if (bytesread != tablesize) {
277 /* convert from BGR to BGRA by setting alpha to 100% */
278 for (i=0; i<count; i++)
279 wiccolors[i] |= 0xff000000;
283 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
290 LeaveCriticalSection(&This->lock);
293 hr = IWICPalette_InitializeCustom(pIPalette, wiccolors, count);
295 HeapFree(GetProcessHeap(), 0, wiccolors);
296 HeapFree(GetProcessHeap(), 0, bgrcolors);
300 static HRESULT WINAPI BmpFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
301 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
303 BmpDecoder *This = impl_from_frame(iface);
306 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
308 EnterCriticalSection(&This->lock);
309 if (!This->imagedata)
311 hr = This->read_data_func(This);
313 LeaveCriticalSection(&This->lock);
314 if (FAILED(hr)) return hr;
316 hr = BmpFrameDecode_GetSize(iface, &width, &height);
317 if (FAILED(hr)) return hr;
319 return copy_pixels(This->bitsperpixel, This->imagedatastart,
320 width, height, This->stride,
321 prc, cbStride, cbBufferSize, pbBuffer);
324 static HRESULT WINAPI BmpFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
325 IWICMetadataQueryReader **ppIMetadataQueryReader)
327 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
328 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
331 static HRESULT WINAPI BmpFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
332 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
334 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
335 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
338 static HRESULT WINAPI BmpFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
339 IWICBitmapSource **ppIThumbnail)
341 TRACE("(%p,%p)\n", iface, ppIThumbnail);
342 return WINCODEC_ERR_CODECNOTHUMBNAIL;
345 static HRESULT BmpFrameDecode_ReadUncompressed(BmpDecoder* This)
352 LARGE_INTEGER offbits;
355 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
357 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
358 width = bch->bcWidth;
359 height = bch->bcHeight;
364 width = This->bih.bV5Width;
365 height = abs(This->bih.bV5Height);
366 bottomup = (This->bih.bV5Height > 0);
369 /* row sizes in BMP files must be divisible by 4 bytes */
370 bytesperrow = (((width * This->bitsperpixel)+31)/32)*4;
371 datasize = bytesperrow * height;
373 This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
374 if (!This->imagedata) return E_OUTOFMEMORY;
376 offbits.QuadPart = This->image_offset;
377 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
378 if (FAILED(hr)) goto fail;
380 hr = IStream_Read(This->stream, This->imagedata, datasize, &bytesread);
381 if (FAILED(hr) || bytesread != datasize) goto fail;
385 This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
386 This->stride = -bytesperrow;
390 This->imagedatastart = This->imagedata;
391 This->stride = bytesperrow;
396 HeapFree(GetProcessHeap(), 0, This->imagedata);
397 This->imagedata = NULL;
398 if (SUCCEEDED(hr)) hr = E_FAIL;
402 static HRESULT BmpFrameDecode_ReadRGB8(BmpDecoder* This)
407 hr = IWICBitmapFrameDecode_GetSize((IWICBitmapFrameDecode*)&This->lpFrameVtbl, &width, &height);
411 hr = BmpFrameDecode_ReadUncompressed(This);
416 reverse_bgr8(This->bitsperpixel/8, This->imagedatastart,
417 width, height, This->stride);
423 static HRESULT ReadByte(IStream *stream, BYTE *buffer, ULONG buffer_size,
424 ULONG *cursor, ULONG *bytesread, BYTE *result)
428 if (*bytesread == 0 || *cursor == *bytesread)
430 hr = IStream_Read(stream, buffer, buffer_size, bytesread);
436 if (*cursor < *bytesread)
437 *result = buffer[(*cursor)++];
445 static HRESULT BmpFrameDecode_ReadRLE8(BmpDecoder* This)
450 UINT datasize, palettesize;
455 LARGE_INTEGER offbits;
456 ULONG cursor=0, bytesread=0;
458 width = This->bih.bV5Width;
459 height = abs(This->bih.bV5Height);
460 bytesperrow = width * 4;
461 datasize = bytesperrow * height;
462 if (This->bih.bV5ClrUsed && This->bih.bV5ClrUsed < 256)
463 palettesize = 4 * This->bih.bV5ClrUsed;
465 palettesize = 4 * 256;
467 This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
468 if (!This->imagedata)
475 offbits.QuadPart = This->palette_offset;
476 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
477 if (FAILED(hr)) goto fail;
479 hr = IStream_Read(This->stream, palette, palettesize, &bytesread);
480 if (FAILED(hr) || bytesread != palettesize) goto fail;
483 offbits.QuadPart = This->image_offset;
484 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
485 if (FAILED(hr)) goto fail;
488 bgrdata = (DWORD*)This->imagedata;
496 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &length);
500 else if (length == 0)
504 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &escape);
509 case 0: /* end of line */
513 case 1: /* end of bitmap */
518 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &dx);
520 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &dy);
527 default: /* absolute mode */
529 while (length-- && x < width)
532 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &index);
535 bgrdata[y*width + x++] = palette[index];
538 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &length); /* skip pad byte */
547 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &index);
550 color = palette[index];
551 while (length-- && x < width)
552 bgrdata[y*width + x++] = color;
557 This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
558 This->stride = -bytesperrow;
563 HeapFree(GetProcessHeap(), 0, This->imagedata);
564 This->imagedata = NULL;
565 if (SUCCEEDED(hr)) hr = E_FAIL;
569 static HRESULT BmpFrameDecode_ReadRLE4(BmpDecoder* This)
574 UINT datasize, palettesize;
579 LARGE_INTEGER offbits;
580 ULONG cursor=0, bytesread=0;
582 width = This->bih.bV5Width;
583 height = abs(This->bih.bV5Height);
584 bytesperrow = width * 4;
585 datasize = bytesperrow * height;
586 if (This->bih.bV5ClrUsed && This->bih.bV5ClrUsed < 16)
587 palettesize = 4 * This->bih.bV5ClrUsed;
589 palettesize = 4 * 16;
591 This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
592 if (!This->imagedata)
599 offbits.QuadPart = This->palette_offset;
600 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
601 if (FAILED(hr)) goto fail;
603 hr = IStream_Read(This->stream, palette, palettesize, &bytesread);
604 if (FAILED(hr) || bytesread != palettesize) goto fail;
607 offbits.QuadPart = This->image_offset;
608 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
609 if (FAILED(hr)) goto fail;
612 bgrdata = (DWORD*)This->imagedata;
620 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &length);
624 else if (length == 0)
628 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &escape);
633 case 0: /* end of line */
637 case 1: /* end of bitmap */
642 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &dx);
644 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &dy);
651 default: /* absolute mode */
655 while (length-- && x < width)
658 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &colors);
662 bgrdata[y*width + x++] = palette[colors>>4];
663 if (length-- && x < width)
664 bgrdata[y*width + x++] = palette[colors&0xf];
669 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &length); /* skip pad byte */
680 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &colors);
683 color1 = palette[colors>>4];
684 color2 = palette[colors&0xf];
685 while (length-- && x < width)
687 bgrdata[y*width + x++] = color1;
688 if (length-- && x < width)
689 bgrdata[y*width + x++] = color2;
697 This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
698 This->stride = -bytesperrow;
703 HeapFree(GetProcessHeap(), 0, This->imagedata);
704 This->imagedata = NULL;
705 if (SUCCEEDED(hr)) hr = E_FAIL;
709 static HRESULT BmpFrameDecode_ReadUnsupported(BmpDecoder* This)
714 struct bitfields_format {
715 WORD bitcount; /* 0 for end of list */
720 const WICPixelFormatGUID *pixelformat;
721 ReadDataFunc read_data_func;
724 static const struct bitfields_format bitfields_formats[] = {
725 {16,0x7c00,0x3e0,0x1f,0,&GUID_WICPixelFormat16bppBGR555,BmpFrameDecode_ReadUncompressed},
726 {16,0xf800,0x7e0,0x1f,0,&GUID_WICPixelFormat16bppBGR565,BmpFrameDecode_ReadUncompressed},
727 {32,0xff0000,0xff00,0xff,0,&GUID_WICPixelFormat32bppBGR,BmpFrameDecode_ReadUncompressed},
728 {32,0xff0000,0xff00,0xff,0xff000000,&GUID_WICPixelFormat32bppBGRA,BmpFrameDecode_ReadUncompressed},
729 {32,0xff,0xff00,0xff0000,0,&GUID_WICPixelFormat32bppBGR,BmpFrameDecode_ReadRGB8},
733 static const IWICBitmapFrameDecodeVtbl BmpDecoder_FrameVtbl = {
734 BmpFrameDecode_QueryInterface,
735 BmpFrameDecode_AddRef,
736 BmpFrameDecode_Release,
737 BmpFrameDecode_GetSize,
738 BmpFrameDecode_GetPixelFormat,
739 BmpFrameDecode_GetResolution,
740 BmpFrameDecode_CopyPalette,
741 BmpFrameDecode_CopyPixels,
742 BmpFrameDecode_GetMetadataQueryReader,
743 BmpFrameDecode_GetColorContexts,
744 BmpFrameDecode_GetThumbnail
747 static HRESULT BmpDecoder_ReadHeaders(BmpDecoder* This, IStream *stream)
750 ULONG bytestoread, bytesread;
753 if (This->initialized) return WINCODEC_ERR_WRONGSTATE;
756 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
757 if (FAILED(hr)) return hr;
761 BITMAPFILEHEADER bfh;
762 hr = IStream_Read(stream, &bfh, sizeof(BITMAPFILEHEADER), &bytesread);
763 if (FAILED(hr)) return hr;
764 if (bytesread != sizeof(BITMAPFILEHEADER) ||
765 bfh.bfType != 0x4d42 /* "BM" */) return E_FAIL;
766 This->image_offset = bfh.bfOffBits;
769 hr = IStream_Read(stream, &This->bih.bV5Size, sizeof(DWORD), &bytesread);
770 if (FAILED(hr)) return hr;
771 if (bytesread != sizeof(DWORD) ||
772 (This->bih.bV5Size != sizeof(BITMAPCOREHEADER) &&
773 This->bih.bV5Size != sizeof(BITMAPCOREHEADER2) &&
774 This->bih.bV5Size != sizeof(BITMAPINFOHEADER) &&
775 This->bih.bV5Size != sizeof(BITMAPV4HEADER) &&
776 This->bih.bV5Size != sizeof(BITMAPV5HEADER))) return E_FAIL;
778 bytestoread = This->bih.bV5Size-sizeof(DWORD);
779 hr = IStream_Read(stream, &This->bih.bV5Width, bytestoread, &bytesread);
780 if (FAILED(hr)) return hr;
781 if (bytestoread != bytesread) return E_FAIL;
784 This->palette_offset = This->bih.bV5Size;
786 This->palette_offset = sizeof(BITMAPFILEHEADER) + This->bih.bV5Size;
790 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
792 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
797 This->bih.bV5Height /= 2;
801 /* if this is a BITMAPINFOHEADER with BI_BITFIELDS compression, we need to
802 read the extra fields */
803 if (This->bih.bV5Size == sizeof(BITMAPINFOHEADER) &&
804 This->bih.bV5Compression == BI_BITFIELDS)
806 hr = IStream_Read(stream, &This->bih.bV5RedMask, 12, &bytesread);
807 if (FAILED(hr)) return hr;
808 if (bytesread != 12) return E_FAIL;
809 This->bih.bV5AlphaMask = 0;
810 This->palette_offset += 12;
813 /* decide what kind of bitmap this is and how/if we can read it */
814 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
816 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
817 TRACE("BITMAPCOREHEADER with depth=%i\n", bch->bcBitCount);
818 This->bitsperpixel = bch->bcBitCount;
819 This->read_data_func = BmpFrameDecode_ReadUncompressed;
820 switch(bch->bcBitCount)
823 This->pixelformat = &GUID_WICPixelFormat1bppIndexed;
826 This->pixelformat = &GUID_WICPixelFormat2bppIndexed;
829 This->pixelformat = &GUID_WICPixelFormat4bppIndexed;
832 This->pixelformat = &GUID_WICPixelFormat8bppIndexed;
835 This->pixelformat = &GUID_WICPixelFormat24bppBGR;
838 This->pixelformat = &GUID_WICPixelFormatUndefined;
839 WARN("unsupported bit depth %i for BITMAPCOREHEADER\n", bch->bcBitCount);
843 else /* struct is compatible with BITMAPINFOHEADER */
845 TRACE("bitmap header=%i compression=%i depth=%i\n", This->bih.bV5Size, This->bih.bV5Compression, This->bih.bV5BitCount);
846 switch(This->bih.bV5Compression)
849 This->bitsperpixel = This->bih.bV5BitCount;
850 This->read_data_func = BmpFrameDecode_ReadUncompressed;
851 switch(This->bih.bV5BitCount)
854 This->pixelformat = &GUID_WICPixelFormat1bppIndexed;
857 This->pixelformat = &GUID_WICPixelFormat2bppIndexed;
860 This->pixelformat = &GUID_WICPixelFormat4bppIndexed;
863 This->pixelformat = &GUID_WICPixelFormat8bppIndexed;
866 This->pixelformat = &GUID_WICPixelFormat16bppBGR555;
869 This->pixelformat = &GUID_WICPixelFormat24bppBGR;
872 This->pixelformat = &GUID_WICPixelFormat32bppBGR;
875 This->pixelformat = &GUID_WICPixelFormatUndefined;
876 FIXME("unsupported bit depth %i for uncompressed RGB\n", This->bih.bV5BitCount);
880 This->bitsperpixel = 32;
881 This->read_data_func = BmpFrameDecode_ReadRLE8;
882 This->pixelformat = &GUID_WICPixelFormat32bppBGR;
885 This->bitsperpixel = 32;
886 This->read_data_func = BmpFrameDecode_ReadRLE4;
887 This->pixelformat = &GUID_WICPixelFormat32bppBGR;
891 const struct bitfields_format *format;
892 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER2))
894 /* BCH2 doesn't support bitfields; this is Huffman 1D compression */
895 This->bitsperpixel = 0;
896 This->read_data_func = BmpFrameDecode_ReadUnsupported;
897 This->pixelformat = &GUID_WICPixelFormatUndefined;
898 FIXME("Huffman 1D compression is unsupported\n");
901 This->bitsperpixel = This->bih.bV5BitCount;
902 for (format = bitfields_formats; format->bitcount; format++)
904 if ((format->bitcount == This->bih.bV5BitCount) &&
905 (format->redmask == This->bih.bV5RedMask) &&
906 (format->greenmask == This->bih.bV5GreenMask) &&
907 (format->bluemask == This->bih.bV5BlueMask) &&
908 (format->alphamask == This->bih.bV5AlphaMask))
910 This->read_data_func = format->read_data_func;
911 This->pixelformat = format->pixelformat;
915 if (!format->bitcount)
917 This->read_data_func = BmpFrameDecode_ReadUncompressed;
918 This->pixelformat = &GUID_WICPixelFormatUndefined;
919 FIXME("unsupported bitfields type depth=%i red=%x green=%x blue=%x alpha=%x\n",
920 This->bih.bV5BitCount, This->bih.bV5RedMask, This->bih.bV5GreenMask, This->bih.bV5BlueMask, This->bih.bV5AlphaMask);
925 This->bitsperpixel = 0;
926 This->read_data_func = BmpFrameDecode_ReadUnsupported;
927 This->pixelformat = &GUID_WICPixelFormatUndefined;
928 FIXME("unsupported bitmap type header=%i compression=%i depth=%i\n", This->bih.bV5Size, This->bih.bV5Compression, This->bih.bV5BitCount);
935 /* In a packed DIB, the image follows the palette. */
936 ULONG palette_count, palette_size;
937 if (This->bih.bV5ClrUsed)
938 palette_count = This->bih.bV5ClrUsed;
939 else if (This->bih.bV5BitCount <= 8)
940 palette_count = 1 << This->bih.bV5BitCount;
943 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
944 palette_size = sizeof(RGBTRIPLE) * palette_count;
946 palette_size = sizeof(RGBQUAD) * palette_count;
947 This->image_offset = This->palette_offset + palette_size;
950 This->initialized = TRUE;
955 static HRESULT WINAPI BmpDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
958 BmpDecoder *This = (BmpDecoder*)iface;
959 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
961 if (!ppv) return E_INVALIDARG;
963 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
970 return E_NOINTERFACE;
973 IUnknown_AddRef((IUnknown*)*ppv);
977 static ULONG WINAPI BmpDecoder_AddRef(IWICBitmapDecoder *iface)
979 BmpDecoder *This = (BmpDecoder*)iface;
980 ULONG ref = InterlockedIncrement(&This->ref);
982 TRACE("(%p) refcount=%u\n", iface, ref);
987 static ULONG WINAPI BmpDecoder_Release(IWICBitmapDecoder *iface)
989 BmpDecoder *This = (BmpDecoder*)iface;
990 ULONG ref = InterlockedDecrement(&This->ref);
992 TRACE("(%p) refcount=%u\n", iface, ref);
996 if (This->stream) IStream_Release(This->stream);
997 HeapFree(GetProcessHeap(), 0, This->imagedata);
998 This->lock.DebugInfo->Spare[0] = 0;
999 DeleteCriticalSection(&This->lock);
1000 HeapFree(GetProcessHeap(), 0, This);
1006 static HRESULT WINAPI BmpDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
1007 DWORD *pdwCapability)
1010 BmpDecoder *This = (BmpDecoder*)iface;
1012 EnterCriticalSection(&This->lock);
1013 hr = BmpDecoder_ReadHeaders(This, pIStream);
1014 LeaveCriticalSection(&This->lock);
1015 if (FAILED(hr)) return hr;
1017 if (This->read_data_func == BmpFrameDecode_ReadUnsupported)
1020 *pdwCapability = WICBitmapDecoderCapabilityCanDecodeAllImages;
1025 static HRESULT WINAPI BmpDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
1026 WICDecodeOptions cacheOptions)
1029 BmpDecoder *This = (BmpDecoder*)iface;
1031 EnterCriticalSection(&This->lock);
1032 hr = BmpDecoder_ReadHeaders(This, pIStream);
1036 This->stream = pIStream;
1037 IStream_AddRef(pIStream);
1039 LeaveCriticalSection(&This->lock);
1044 static HRESULT WINAPI BmpDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
1045 GUID *pguidContainerFormat)
1047 memcpy(pguidContainerFormat, &GUID_ContainerFormatBmp, sizeof(GUID));
1051 static HRESULT WINAPI BmpDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
1052 IWICBitmapDecoderInfo **ppIDecoderInfo)
1055 IWICComponentInfo *compinfo;
1057 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
1059 hr = CreateComponentInfo(&CLSID_WICBmpDecoder, &compinfo);
1060 if (FAILED(hr)) return hr;
1062 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
1063 (void**)ppIDecoderInfo);
1065 IWICComponentInfo_Release(compinfo);
1070 static HRESULT WINAPI BmpDecoder_CopyPalette(IWICBitmapDecoder *iface,
1071 IWICPalette *pIPalette)
1073 TRACE("(%p,%p)\n", iface, pIPalette);
1075 return WINCODEC_ERR_PALETTEUNAVAILABLE;
1078 static HRESULT WINAPI BmpDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
1079 IWICMetadataQueryReader **ppIMetadataQueryReader)
1081 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
1082 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1085 static HRESULT WINAPI BmpDecoder_GetPreview(IWICBitmapDecoder *iface,
1086 IWICBitmapSource **ppIBitmapSource)
1088 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
1089 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1092 static HRESULT WINAPI BmpDecoder_GetColorContexts(IWICBitmapDecoder *iface,
1093 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1095 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
1096 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1099 static HRESULT WINAPI BmpDecoder_GetThumbnail(IWICBitmapDecoder *iface,
1100 IWICBitmapSource **ppIThumbnail)
1102 TRACE("(%p,%p)\n", iface, ppIThumbnail);
1103 return WINCODEC_ERR_CODECNOTHUMBNAIL;
1106 static HRESULT WINAPI BmpDecoder_GetFrameCount(IWICBitmapDecoder *iface,
1113 static HRESULT WINAPI BmpDecoder_GetFrame(IWICBitmapDecoder *iface,
1114 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
1116 BmpDecoder *This = (BmpDecoder*)iface;
1118 if (index != 0) return E_INVALIDARG;
1120 if (!This->stream) return WINCODEC_ERR_WRONGSTATE;
1122 *ppIBitmapFrame = (IWICBitmapFrameDecode*)&This->lpFrameVtbl;
1123 IWICBitmapDecoder_AddRef(iface);
1128 static const IWICBitmapDecoderVtbl BmpDecoder_Vtbl = {
1129 BmpDecoder_QueryInterface,
1132 BmpDecoder_QueryCapability,
1133 BmpDecoder_Initialize,
1134 BmpDecoder_GetContainerFormat,
1135 BmpDecoder_GetDecoderInfo,
1136 BmpDecoder_CopyPalette,
1137 BmpDecoder_GetMetadataQueryReader,
1138 BmpDecoder_GetPreview,
1139 BmpDecoder_GetColorContexts,
1140 BmpDecoder_GetThumbnail,
1141 BmpDecoder_GetFrameCount,
1145 static HRESULT BmpDecoder_Construct(int packed, int icoframe, IUnknown *pUnkOuter, REFIID iid, void** ppv)
1150 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1154 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1156 This = HeapAlloc(GetProcessHeap(), 0, sizeof(BmpDecoder));
1157 if (!This) return E_OUTOFMEMORY;
1159 This->lpVtbl = &BmpDecoder_Vtbl;
1160 This->lpFrameVtbl = &BmpDecoder_FrameVtbl;
1162 This->initialized = FALSE;
1163 This->stream = NULL;
1164 This->imagedata = NULL;
1165 InitializeCriticalSection(&This->lock);
1166 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": BmpDecoder.lock");
1167 This->packed = packed;
1168 This->icoframe = icoframe;
1170 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1171 IUnknown_Release((IUnknown*)This);
1176 HRESULT BmpDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1178 return BmpDecoder_Construct(FALSE, FALSE, pUnkOuter, iid, ppv);
1181 HRESULT DibDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1183 return BmpDecoder_Construct(TRUE, FALSE, pUnkOuter, iid, ppv);
1186 HRESULT IcoDibDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1188 return BmpDecoder_Construct(TRUE, TRUE, pUnkOuter, iid, ppv);
1191 /* Return the offset where the mask of an icon might be, or 0 for failure. */
1192 void BmpDecoder_FindIconMask(IWICBitmapDecoder *decoder, ULONG *mask_offset, int *topdown)
1194 BmpDecoder *This = (BmpDecoder*)decoder;
1196 assert(This->lpVtbl == &BmpDecoder_Vtbl);
1197 assert(This->stream != NULL);
1199 if (This->read_data_func == BmpFrameDecode_ReadUncompressed)
1201 /* RGB or BITFIELDS data */
1202 ULONG width, height, bytesperrow, datasize;
1203 IWICBitmapFrameDecode_GetSize((IWICBitmapFrameDecode*)&This->lpFrameVtbl, &width, &height);
1204 bytesperrow = (((width * This->bitsperpixel)+31)/32)*4;
1205 datasize = bytesperrow * height;
1206 *mask_offset = This->image_offset + datasize;
1211 *topdown = This->stride > 0;