[WINDOWSCODECS]
[reactos.git] / reactos / dll / win32 / windowscodecs / bmpdecode.c
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "wincodecs_private.h"
20
21 #include <assert.h>
22 #include <wingdi.h>
23
24 typedef struct {
25 DWORD bc2Size;
26 DWORD bc2Width;
27 DWORD bc2Height;
28 WORD bc2Planes;
29 WORD bc2BitCount;
30 DWORD bc2Compression;
31 DWORD bc2SizeImage;
32 DWORD bc2XRes;
33 DWORD bc2YRes;
34 DWORD bc2ClrUsed;
35 DWORD bc2ClrImportant;
36 /* same as BITMAPINFOHEADER until this point */
37 WORD bc2ResUnit;
38 WORD bc2Reserved;
39 WORD bc2Orientation;
40 WORD bc2Halftoning;
41 DWORD bc2HalftoneSize1;
42 DWORD bc2HalftoneSize2;
43 DWORD bc2ColorSpace;
44 DWORD bc2AppData;
45 } BITMAPCOREHEADER2;
46
47 typedef HRESULT (*ReadDataFunc)(BmpDecoder* This);
48
49 struct BmpDecoder {
50 IWICBitmapDecoder IWICBitmapDecoder_iface;
51 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
52 LONG ref;
53 BOOL initialized;
54 IStream *stream;
55 ULONG palette_offset;
56 ULONG image_offset;
57 BITMAPV5HEADER bih;
58 const WICPixelFormatGUID *pixelformat;
59 int bitsperpixel;
60 ReadDataFunc read_data_func;
61 INT stride;
62 BYTE *imagedata;
63 BYTE *imagedatastart;
64 CRITICAL_SECTION lock; /* must be held when initialized/imagedata is set or stream is accessed */
65 int packed; /* If TRUE, don't look for a file header and assume a packed DIB. */
66 int icoframe; /* If TRUE, this is a frame of a .ico file. */
67 };
68
69 static inline BmpDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
70 {
71 return CONTAINING_RECORD(iface, BmpDecoder, IWICBitmapDecoder_iface);
72 }
73
74 static inline BmpDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
75 {
76 return CONTAINING_RECORD(iface, BmpDecoder, IWICBitmapFrameDecode_iface);
77 }
78
79 static HRESULT WINAPI BmpFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
80 void **ppv)
81 {
82 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
83
84 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
85
86 if (!ppv) return E_INVALIDARG;
87
88 if (IsEqualIID(&IID_IUnknown, iid) ||
89 IsEqualIID(&IID_IWICBitmapSource, iid) ||
90 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
91 {
92 *ppv = &This->IWICBitmapFrameDecode_iface;
93 }
94 else
95 {
96 *ppv = NULL;
97 return E_NOINTERFACE;
98 }
99
100 IUnknown_AddRef((IUnknown*)*ppv);
101 return S_OK;
102 }
103
104 static ULONG WINAPI BmpFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
105 {
106 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
107
108 return IWICBitmapDecoder_AddRef(&This->IWICBitmapDecoder_iface);
109 }
110
111 static ULONG WINAPI BmpFrameDecode_Release(IWICBitmapFrameDecode *iface)
112 {
113 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
114
115 return IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
116 }
117
118 static HRESULT WINAPI BmpFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
119 UINT *puiWidth, UINT *puiHeight)
120 {
121 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
122 TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
123
124 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
125 {
126 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
127 *puiWidth = bch->bcWidth;
128 *puiHeight = bch->bcHeight;
129 }
130 else
131 {
132 *puiWidth = This->bih.bV5Width;
133 *puiHeight = abs(This->bih.bV5Height);
134 }
135 return S_OK;
136 }
137
138 static HRESULT WINAPI BmpFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
139 WICPixelFormatGUID *pPixelFormat)
140 {
141 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
142 TRACE("(%p,%p)\n", iface, pPixelFormat);
143
144 memcpy(pPixelFormat, This->pixelformat, sizeof(GUID));
145
146 return S_OK;
147 }
148
149 static HRESULT BmpHeader_GetResolution(BITMAPV5HEADER *bih, double *pDpiX, double *pDpiY)
150 {
151 LONG resx = 0, resy = 0;
152
153 switch (bih->bV5Size)
154 {
155 default:
156 case sizeof(BITMAPCOREHEADER):
157 break;
158
159 case sizeof(BITMAPCOREHEADER2):
160 case sizeof(BITMAPINFOHEADER):
161 case sizeof(BITMAPV4HEADER):
162 case sizeof(BITMAPV5HEADER):
163 resx = bih->bV5XPelsPerMeter;
164 resy = bih->bV5YPelsPerMeter;
165 break;
166 }
167
168 if (!resx || !resy)
169 {
170 *pDpiX = 96.0;
171 *pDpiY = 96.0;
172 }
173 else
174 {
175 *pDpiX = resx * 0.0254;
176 *pDpiY = resy * 0.0254;
177 }
178
179 return S_OK;
180 }
181
182 static HRESULT WINAPI BmpFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
183 double *pDpiX, double *pDpiY)
184 {
185 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
186 TRACE("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
187
188 return BmpHeader_GetResolution(&This->bih, pDpiX, pDpiY);
189 }
190
191 static HRESULT WINAPI BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
192 IWICPalette *pIPalette)
193 {
194 HRESULT hr;
195 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
196 int count;
197 WICColor *wiccolors=NULL;
198 RGBTRIPLE *bgrcolors=NULL;
199
200 TRACE("(%p,%p)\n", iface, pIPalette);
201
202 EnterCriticalSection(&This->lock);
203
204 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
205 {
206 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
207 if (bch->bcBitCount <= 8)
208 {
209 /* 2**n colors in BGR format after the header */
210 ULONG tablesize, bytesread;
211 LARGE_INTEGER offset;
212 int i;
213
214 count = 1 << bch->bcBitCount;
215 wiccolors = HeapAlloc(GetProcessHeap(), 0, sizeof(WICColor) * count);
216 tablesize = sizeof(RGBTRIPLE) * count;
217 bgrcolors = HeapAlloc(GetProcessHeap(), 0, tablesize);
218 if (!wiccolors || !bgrcolors)
219 {
220 hr = E_OUTOFMEMORY;
221 goto end;
222 }
223
224 offset.QuadPart = This->palette_offset;
225 hr = IStream_Seek(This->stream, offset, STREAM_SEEK_SET, NULL);
226 if (FAILED(hr)) goto end;
227
228 hr = IStream_Read(This->stream, bgrcolors, tablesize, &bytesread);
229 if (FAILED(hr)) goto end;
230 if (bytesread != tablesize) {
231 hr = E_FAIL;
232 goto end;
233 }
234
235 for (i=0; i<count; i++)
236 {
237 wiccolors[i] = 0xff000000|
238 (bgrcolors[i].rgbtRed<<16)|
239 (bgrcolors[i].rgbtGreen<<8)|
240 bgrcolors[i].rgbtBlue;
241 }
242 }
243 else
244 {
245 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
246 goto end;
247 }
248 }
249 else
250 {
251 if (This->bih.bV5BitCount <= 8)
252 {
253 ULONG tablesize, bytesread;
254 LARGE_INTEGER offset;
255 int i;
256
257 if (This->bih.bV5ClrUsed == 0)
258 count = 1 << This->bih.bV5BitCount;
259 else
260 count = This->bih.bV5ClrUsed;
261
262 tablesize = sizeof(WICColor) * count;
263 wiccolors = HeapAlloc(GetProcessHeap(), 0, tablesize);
264 if (!wiccolors)
265 {
266 hr = E_OUTOFMEMORY;
267 goto end;
268 }
269
270 offset.QuadPart = This->palette_offset;
271 hr = IStream_Seek(This->stream, offset, STREAM_SEEK_SET, NULL);
272 if (FAILED(hr)) goto end;
273
274 hr = IStream_Read(This->stream, wiccolors, tablesize, &bytesread);
275 if (FAILED(hr)) goto end;
276 if (bytesread != tablesize) {
277 hr = E_FAIL;
278 goto end;
279 }
280
281 /* convert from BGR to BGRA by setting alpha to 100% */
282 for (i=0; i<count; i++)
283 wiccolors[i] |= 0xff000000;
284 }
285 else
286 {
287 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
288 goto end;
289 }
290 }
291
292 end:
293
294 LeaveCriticalSection(&This->lock);
295
296 if (SUCCEEDED(hr))
297 hr = IWICPalette_InitializeCustom(pIPalette, wiccolors, count);
298
299 HeapFree(GetProcessHeap(), 0, wiccolors);
300 HeapFree(GetProcessHeap(), 0, bgrcolors);
301 return hr;
302 }
303
304 static HRESULT WINAPI BmpFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
305 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
306 {
307 BmpDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
308 HRESULT hr=S_OK;
309 UINT width, height;
310 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
311
312 EnterCriticalSection(&This->lock);
313 if (!This->imagedata)
314 {
315 hr = This->read_data_func(This);
316 }
317 LeaveCriticalSection(&This->lock);
318 if (FAILED(hr)) return hr;
319
320 hr = BmpFrameDecode_GetSize(iface, &width, &height);
321 if (FAILED(hr)) return hr;
322
323 return copy_pixels(This->bitsperpixel, This->imagedatastart,
324 width, height, This->stride,
325 prc, cbStride, cbBufferSize, pbBuffer);
326 }
327
328 static HRESULT WINAPI BmpFrameDecode_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
329 IWICMetadataQueryReader **ppIMetadataQueryReader)
330 {
331 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
332 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
333 }
334
335 static HRESULT WINAPI BmpFrameDecode_GetColorContexts(IWICBitmapFrameDecode *iface,
336 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
337 {
338 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
339 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
340 }
341
342 static HRESULT WINAPI BmpFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
343 IWICBitmapSource **ppIThumbnail)
344 {
345 TRACE("(%p,%p)\n", iface, ppIThumbnail);
346 return WINCODEC_ERR_CODECNOTHUMBNAIL;
347 }
348
349 static HRESULT BmpFrameDecode_ReadUncompressed(BmpDecoder* This)
350 {
351 UINT bytesperrow;
352 UINT width, height;
353 UINT datasize;
354 int bottomup;
355 HRESULT hr;
356 LARGE_INTEGER offbits;
357 ULONG bytesread;
358
359 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
360 {
361 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
362 width = bch->bcWidth;
363 height = bch->bcHeight;
364 bottomup = 1;
365 }
366 else
367 {
368 width = This->bih.bV5Width;
369 height = abs(This->bih.bV5Height);
370 bottomup = (This->bih.bV5Height > 0);
371 }
372
373 /* row sizes in BMP files must be divisible by 4 bytes */
374 bytesperrow = (((width * This->bitsperpixel)+31)/32)*4;
375 datasize = bytesperrow * height;
376
377 This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
378 if (!This->imagedata) return E_OUTOFMEMORY;
379
380 offbits.QuadPart = This->image_offset;
381 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
382 if (FAILED(hr)) goto fail;
383
384 hr = IStream_Read(This->stream, This->imagedata, datasize, &bytesread);
385 if (FAILED(hr) || bytesread != datasize) goto fail;
386
387 if (bottomup)
388 {
389 This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
390 This->stride = -bytesperrow;
391 }
392 else
393 {
394 This->imagedatastart = This->imagedata;
395 This->stride = bytesperrow;
396 }
397 return S_OK;
398
399 fail:
400 HeapFree(GetProcessHeap(), 0, This->imagedata);
401 This->imagedata = NULL;
402 if (SUCCEEDED(hr)) hr = E_FAIL;
403 return hr;
404 }
405
406 static HRESULT BmpFrameDecode_ReadRGB8(BmpDecoder* This)
407 {
408 HRESULT hr;
409 UINT width, height;
410
411 hr = IWICBitmapFrameDecode_GetSize(&This->IWICBitmapFrameDecode_iface, &width, &height);
412
413 if (SUCCEEDED(hr))
414 {
415 hr = BmpFrameDecode_ReadUncompressed(This);
416 }
417
418 if (SUCCEEDED(hr))
419 {
420 reverse_bgr8(This->bitsperpixel/8, This->imagedatastart,
421 width, height, This->stride);
422 }
423
424 return hr;
425 }
426
427 static HRESULT ReadByte(IStream *stream, BYTE *buffer, ULONG buffer_size,
428 ULONG *cursor, ULONG *bytesread, BYTE *result)
429 {
430 HRESULT hr=S_OK;
431
432 if (*bytesread == 0 || *cursor == *bytesread)
433 {
434 hr = IStream_Read(stream, buffer, buffer_size, bytesread);
435 *cursor = 0;
436 }
437
438 if (SUCCEEDED(hr))
439 {
440 if (*cursor < *bytesread)
441 *result = buffer[(*cursor)++];
442 else
443 hr = E_FAIL;
444 }
445
446 return hr;
447 }
448
449 static HRESULT BmpFrameDecode_ReadRLE8(BmpDecoder* This)
450 {
451 UINT bytesperrow;
452 UINT width, height;
453 BYTE rledata[4096];
454 UINT datasize, palettesize;
455 DWORD palette[256];
456 UINT x, y;
457 DWORD *bgrdata;
458 HRESULT hr;
459 LARGE_INTEGER offbits;
460 ULONG cursor=0, bytesread=0;
461
462 width = This->bih.bV5Width;
463 height = abs(This->bih.bV5Height);
464 bytesperrow = width * 4;
465 datasize = bytesperrow * height;
466 if (This->bih.bV5ClrUsed && This->bih.bV5ClrUsed < 256)
467 palettesize = 4 * This->bih.bV5ClrUsed;
468 else
469 palettesize = 4 * 256;
470
471 This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
472 if (!This->imagedata)
473 {
474 hr = E_OUTOFMEMORY;
475 goto fail;
476 }
477
478 /* read palette */
479 offbits.QuadPart = This->palette_offset;
480 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
481 if (FAILED(hr)) goto fail;
482
483 hr = IStream_Read(This->stream, palette, palettesize, &bytesread);
484 if (FAILED(hr) || bytesread != palettesize) goto fail;
485
486 /* read RLE data */
487 offbits.QuadPart = This->image_offset;
488 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
489 if (FAILED(hr)) goto fail;
490
491 /* decode RLE */
492 bgrdata = (DWORD*)This->imagedata;
493 x = 0;
494 y = 0;
495 cursor = 0;
496 bytesread = 0;
497 while (y < height)
498 {
499 BYTE length;
500 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &length);
501
502 if (FAILED(hr))
503 goto fail;
504 else if (length == 0)
505 {
506 /* escape code */
507 BYTE escape;
508 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &escape);
509 if (FAILED(hr))
510 goto fail;
511 switch(escape)
512 {
513 case 0: /* end of line */
514 x = 0;
515 y++;
516 break;
517 case 1: /* end of bitmap */
518 goto end;
519 case 2: /* delta */
520 {
521 BYTE dx, dy;
522 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &dx);
523 if (SUCCEEDED(hr))
524 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &dy);
525 if (FAILED(hr))
526 goto fail;
527 x += dx;
528 y += dy;
529 break;
530 }
531 default: /* absolute mode */
532 length = escape;
533 while (length-- && x < width)
534 {
535 BYTE index;
536 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &index);
537 if (FAILED(hr))
538 goto fail;
539 bgrdata[y*width + x++] = palette[index];
540 }
541 if (escape & 1)
542 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &length); /* skip pad byte */
543 if (FAILED(hr))
544 goto fail;
545 }
546 }
547 else
548 {
549 BYTE index;
550 DWORD color;
551 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &index);
552 if (FAILED(hr))
553 goto fail;
554 color = palette[index];
555 while (length-- && x < width)
556 bgrdata[y*width + x++] = color;
557 }
558 }
559
560 end:
561 This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
562 This->stride = -bytesperrow;
563
564 return S_OK;
565
566 fail:
567 HeapFree(GetProcessHeap(), 0, This->imagedata);
568 This->imagedata = NULL;
569 if (SUCCEEDED(hr)) hr = E_FAIL;
570 return hr;
571 }
572
573 static HRESULT BmpFrameDecode_ReadRLE4(BmpDecoder* This)
574 {
575 UINT bytesperrow;
576 UINT width, height;
577 BYTE rledata[4096];
578 UINT datasize, palettesize;
579 DWORD palette[16];
580 UINT x, y;
581 DWORD *bgrdata;
582 HRESULT hr;
583 LARGE_INTEGER offbits;
584 ULONG cursor=0, bytesread=0;
585
586 width = This->bih.bV5Width;
587 height = abs(This->bih.bV5Height);
588 bytesperrow = width * 4;
589 datasize = bytesperrow * height;
590 if (This->bih.bV5ClrUsed && This->bih.bV5ClrUsed < 16)
591 palettesize = 4 * This->bih.bV5ClrUsed;
592 else
593 palettesize = 4 * 16;
594
595 This->imagedata = HeapAlloc(GetProcessHeap(), 0, datasize);
596 if (!This->imagedata)
597 {
598 hr = E_OUTOFMEMORY;
599 goto fail;
600 }
601
602 /* read palette */
603 offbits.QuadPart = This->palette_offset;
604 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
605 if (FAILED(hr)) goto fail;
606
607 hr = IStream_Read(This->stream, palette, palettesize, &bytesread);
608 if (FAILED(hr) || bytesread != palettesize) goto fail;
609
610 /* read RLE data */
611 offbits.QuadPart = This->image_offset;
612 hr = IStream_Seek(This->stream, offbits, STREAM_SEEK_SET, NULL);
613 if (FAILED(hr)) goto fail;
614
615 /* decode RLE */
616 bgrdata = (DWORD*)This->imagedata;
617 x = 0;
618 y = 0;
619 cursor = 0;
620 bytesread = 0;
621 while (y < height)
622 {
623 BYTE length;
624 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &length);
625
626 if (FAILED(hr))
627 goto fail;
628 else if (length == 0)
629 {
630 /* escape code */
631 BYTE escape;
632 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &escape);
633 if (FAILED(hr))
634 goto fail;
635 switch(escape)
636 {
637 case 0: /* end of line */
638 x = 0;
639 y++;
640 break;
641 case 1: /* end of bitmap */
642 goto end;
643 case 2: /* delta */
644 {
645 BYTE dx, dy;
646 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &dx);
647 if (SUCCEEDED(hr))
648 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &dy);
649 if (FAILED(hr))
650 goto fail;
651 x += dx;
652 y += dy;
653 break;
654 }
655 default: /* absolute mode */
656 {
657 BYTE realsize=0;
658 length = escape;
659 while (length-- && x < width)
660 {
661 BYTE colors;
662 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &colors);
663 realsize++;
664 if (FAILED(hr))
665 goto fail;
666 bgrdata[y*width + x++] = palette[colors>>4];
667 if (length-- && x < width)
668 bgrdata[y*width + x++] = palette[colors&0xf];
669 else
670 break;
671 }
672 if (realsize & 1)
673 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &length); /* skip pad byte */
674 if (FAILED(hr))
675 goto fail;
676 }
677 }
678 }
679 else
680 {
681 BYTE colors;
682 DWORD color1;
683 DWORD color2;
684 hr = ReadByte(This->stream, rledata, 4096, &cursor, &bytesread, &colors);
685 if (FAILED(hr))
686 goto fail;
687 color1 = palette[colors>>4];
688 color2 = palette[colors&0xf];
689 while (length-- && x < width)
690 {
691 bgrdata[y*width + x++] = color1;
692 if (length-- && x < width)
693 bgrdata[y*width + x++] = color2;
694 else
695 break;
696 }
697 }
698 }
699
700 end:
701 This->imagedatastart = This->imagedata + (height-1) * bytesperrow;
702 This->stride = -bytesperrow;
703
704 return S_OK;
705
706 fail:
707 HeapFree(GetProcessHeap(), 0, This->imagedata);
708 This->imagedata = NULL;
709 if (SUCCEEDED(hr)) hr = E_FAIL;
710 return hr;
711 }
712
713 static HRESULT BmpFrameDecode_ReadUnsupported(BmpDecoder* This)
714 {
715 return E_FAIL;
716 }
717
718 struct bitfields_format {
719 WORD bitcount; /* 0 for end of list */
720 DWORD redmask;
721 DWORD greenmask;
722 DWORD bluemask;
723 DWORD alphamask;
724 const WICPixelFormatGUID *pixelformat;
725 ReadDataFunc read_data_func;
726 };
727
728 static const struct bitfields_format bitfields_formats[] = {
729 {16,0x7c00,0x3e0,0x1f,0,&GUID_WICPixelFormat16bppBGR555,BmpFrameDecode_ReadUncompressed},
730 {16,0xf800,0x7e0,0x1f,0,&GUID_WICPixelFormat16bppBGR565,BmpFrameDecode_ReadUncompressed},
731 {32,0xff0000,0xff00,0xff,0,&GUID_WICPixelFormat32bppBGR,BmpFrameDecode_ReadUncompressed},
732 {32,0xff0000,0xff00,0xff,0xff000000,&GUID_WICPixelFormat32bppBGRA,BmpFrameDecode_ReadUncompressed},
733 {32,0xff,0xff00,0xff0000,0,&GUID_WICPixelFormat32bppBGR,BmpFrameDecode_ReadRGB8},
734 {0}
735 };
736
737 static const IWICBitmapFrameDecodeVtbl BmpDecoder_FrameVtbl = {
738 BmpFrameDecode_QueryInterface,
739 BmpFrameDecode_AddRef,
740 BmpFrameDecode_Release,
741 BmpFrameDecode_GetSize,
742 BmpFrameDecode_GetPixelFormat,
743 BmpFrameDecode_GetResolution,
744 BmpFrameDecode_CopyPalette,
745 BmpFrameDecode_CopyPixels,
746 BmpFrameDecode_GetMetadataQueryReader,
747 BmpFrameDecode_GetColorContexts,
748 BmpFrameDecode_GetThumbnail
749 };
750
751 static HRESULT BmpDecoder_ReadHeaders(BmpDecoder* This, IStream *stream)
752 {
753 HRESULT hr;
754 ULONG bytestoread, bytesread;
755 LARGE_INTEGER seek;
756
757 if (This->initialized) return WINCODEC_ERR_WRONGSTATE;
758
759 seek.QuadPart = 0;
760 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
761 if (FAILED(hr)) return hr;
762
763 if (!This->packed)
764 {
765 BITMAPFILEHEADER bfh;
766 hr = IStream_Read(stream, &bfh, sizeof(BITMAPFILEHEADER), &bytesread);
767 if (FAILED(hr)) return hr;
768 if (bytesread != sizeof(BITMAPFILEHEADER) ||
769 bfh.bfType != 0x4d42 /* "BM" */) return E_FAIL;
770 This->image_offset = bfh.bfOffBits;
771 }
772
773 hr = IStream_Read(stream, &This->bih.bV5Size, sizeof(DWORD), &bytesread);
774 if (FAILED(hr)) return hr;
775 if (bytesread != sizeof(DWORD) ||
776 (This->bih.bV5Size != sizeof(BITMAPCOREHEADER) &&
777 This->bih.bV5Size != sizeof(BITMAPCOREHEADER2) &&
778 This->bih.bV5Size != sizeof(BITMAPINFOHEADER) &&
779 This->bih.bV5Size != sizeof(BITMAPV4HEADER) &&
780 This->bih.bV5Size != sizeof(BITMAPV5HEADER))) return E_FAIL;
781
782 bytestoread = This->bih.bV5Size-sizeof(DWORD);
783 hr = IStream_Read(stream, &This->bih.bV5Width, bytestoread, &bytesread);
784 if (FAILED(hr)) return hr;
785 if (bytestoread != bytesread) return E_FAIL;
786
787 if (This->packed)
788 This->palette_offset = This->bih.bV5Size;
789 else
790 This->palette_offset = sizeof(BITMAPFILEHEADER) + This->bih.bV5Size;
791
792 if (This->icoframe)
793 {
794 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
795 {
796 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
797 bch->bcHeight /= 2;
798 }
799 else
800 {
801 This->bih.bV5Height /= 2;
802 }
803 }
804
805 /* if this is a BITMAPINFOHEADER with BI_BITFIELDS compression, we need to
806 read the extra fields */
807 if (This->bih.bV5Size == sizeof(BITMAPINFOHEADER) &&
808 This->bih.bV5Compression == BI_BITFIELDS)
809 {
810 hr = IStream_Read(stream, &This->bih.bV5RedMask, 12, &bytesread);
811 if (FAILED(hr)) return hr;
812 if (bytesread != 12) return E_FAIL;
813 This->bih.bV5AlphaMask = 0;
814 This->palette_offset += 12;
815 }
816
817 /* decide what kind of bitmap this is and how/if we can read it */
818 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
819 {
820 BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
821 TRACE("BITMAPCOREHEADER with depth=%i\n", bch->bcBitCount);
822 This->bitsperpixel = bch->bcBitCount;
823 This->read_data_func = BmpFrameDecode_ReadUncompressed;
824 switch(bch->bcBitCount)
825 {
826 case 1:
827 This->pixelformat = &GUID_WICPixelFormat1bppIndexed;
828 break;
829 case 2:
830 This->pixelformat = &GUID_WICPixelFormat2bppIndexed;
831 break;
832 case 4:
833 This->pixelformat = &GUID_WICPixelFormat4bppIndexed;
834 break;
835 case 8:
836 This->pixelformat = &GUID_WICPixelFormat8bppIndexed;
837 break;
838 case 24:
839 This->pixelformat = &GUID_WICPixelFormat24bppBGR;
840 break;
841 default:
842 This->pixelformat = &GUID_WICPixelFormatUndefined;
843 WARN("unsupported bit depth %i for BITMAPCOREHEADER\n", bch->bcBitCount);
844 break;
845 }
846 }
847 else /* struct is compatible with BITMAPINFOHEADER */
848 {
849 TRACE("bitmap header=%i compression=%i depth=%i\n", This->bih.bV5Size, This->bih.bV5Compression, This->bih.bV5BitCount);
850 switch(This->bih.bV5Compression)
851 {
852 case BI_RGB:
853 This->bitsperpixel = This->bih.bV5BitCount;
854 This->read_data_func = BmpFrameDecode_ReadUncompressed;
855 switch(This->bih.bV5BitCount)
856 {
857 case 1:
858 This->pixelformat = &GUID_WICPixelFormat1bppIndexed;
859 break;
860 case 2:
861 This->pixelformat = &GUID_WICPixelFormat2bppIndexed;
862 break;
863 case 4:
864 This->pixelformat = &GUID_WICPixelFormat4bppIndexed;
865 break;
866 case 8:
867 This->pixelformat = &GUID_WICPixelFormat8bppIndexed;
868 break;
869 case 16:
870 This->pixelformat = &GUID_WICPixelFormat16bppBGR555;
871 break;
872 case 24:
873 This->pixelformat = &GUID_WICPixelFormat24bppBGR;
874 break;
875 case 32:
876 This->pixelformat = &GUID_WICPixelFormat32bppBGR;
877 break;
878 default:
879 This->pixelformat = &GUID_WICPixelFormatUndefined;
880 FIXME("unsupported bit depth %i for uncompressed RGB\n", This->bih.bV5BitCount);
881 }
882 break;
883 case BI_RLE8:
884 This->bitsperpixel = 32;
885 This->read_data_func = BmpFrameDecode_ReadRLE8;
886 This->pixelformat = &GUID_WICPixelFormat32bppBGR;
887 break;
888 case BI_RLE4:
889 This->bitsperpixel = 32;
890 This->read_data_func = BmpFrameDecode_ReadRLE4;
891 This->pixelformat = &GUID_WICPixelFormat32bppBGR;
892 break;
893 case BI_BITFIELDS:
894 {
895 const struct bitfields_format *format;
896 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER2))
897 {
898 /* BCH2 doesn't support bitfields; this is Huffman 1D compression */
899 This->bitsperpixel = 0;
900 This->read_data_func = BmpFrameDecode_ReadUnsupported;
901 This->pixelformat = &GUID_WICPixelFormatUndefined;
902 FIXME("Huffman 1D compression is unsupported\n");
903 break;
904 }
905 This->bitsperpixel = This->bih.bV5BitCount;
906 for (format = bitfields_formats; format->bitcount; format++)
907 {
908 if ((format->bitcount == This->bih.bV5BitCount) &&
909 (format->redmask == This->bih.bV5RedMask) &&
910 (format->greenmask == This->bih.bV5GreenMask) &&
911 (format->bluemask == This->bih.bV5BlueMask) &&
912 (format->alphamask == This->bih.bV5AlphaMask))
913 {
914 This->read_data_func = format->read_data_func;
915 This->pixelformat = format->pixelformat;
916 break;
917 }
918 }
919 if (!format->bitcount)
920 {
921 This->read_data_func = BmpFrameDecode_ReadUncompressed;
922 This->pixelformat = &GUID_WICPixelFormatUndefined;
923 FIXME("unsupported bitfields type depth=%i red=%x green=%x blue=%x alpha=%x\n",
924 This->bih.bV5BitCount, This->bih.bV5RedMask, This->bih.bV5GreenMask, This->bih.bV5BlueMask, This->bih.bV5AlphaMask);
925 }
926 break;
927 }
928 default:
929 This->bitsperpixel = 0;
930 This->read_data_func = BmpFrameDecode_ReadUnsupported;
931 This->pixelformat = &GUID_WICPixelFormatUndefined;
932 FIXME("unsupported bitmap type header=%i compression=%i depth=%i\n", This->bih.bV5Size, This->bih.bV5Compression, This->bih.bV5BitCount);
933 break;
934 }
935 }
936
937 if (This->packed)
938 {
939 /* In a packed DIB, the image follows the palette. */
940 ULONG palette_count, palette_size;
941 if (This->bih.bV5ClrUsed)
942 palette_count = This->bih.bV5ClrUsed;
943 else if (This->bih.bV5BitCount <= 8)
944 palette_count = 1 << This->bih.bV5BitCount;
945 else
946 palette_count = 0;
947 if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
948 palette_size = sizeof(RGBTRIPLE) * palette_count;
949 else
950 palette_size = sizeof(RGBQUAD) * palette_count;
951 This->image_offset = This->palette_offset + palette_size;
952 }
953
954 This->initialized = TRUE;
955
956 return S_OK;
957 }
958
959 static HRESULT WINAPI BmpDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
960 void **ppv)
961 {
962 BmpDecoder *This = impl_from_IWICBitmapDecoder(iface);
963 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
964
965 if (!ppv) return E_INVALIDARG;
966
967 if (IsEqualIID(&IID_IUnknown, iid) ||
968 IsEqualIID(&IID_IWICBitmapDecoder, iid))
969 {
970 *ppv = &This->IWICBitmapDecoder_iface;
971 }
972 else
973 {
974 *ppv = NULL;
975 return E_NOINTERFACE;
976 }
977
978 IUnknown_AddRef((IUnknown*)*ppv);
979 return S_OK;
980 }
981
982 static ULONG WINAPI BmpDecoder_AddRef(IWICBitmapDecoder *iface)
983 {
984 BmpDecoder *This = impl_from_IWICBitmapDecoder(iface);
985 ULONG ref = InterlockedIncrement(&This->ref);
986
987 TRACE("(%p) refcount=%u\n", iface, ref);
988
989 return ref;
990 }
991
992 static ULONG WINAPI BmpDecoder_Release(IWICBitmapDecoder *iface)
993 {
994 BmpDecoder *This = impl_from_IWICBitmapDecoder(iface);
995 ULONG ref = InterlockedDecrement(&This->ref);
996
997 TRACE("(%p) refcount=%u\n", iface, ref);
998
999 if (ref == 0)
1000 {
1001 if (This->stream) IStream_Release(This->stream);
1002 HeapFree(GetProcessHeap(), 0, This->imagedata);
1003 This->lock.DebugInfo->Spare[0] = 0;
1004 DeleteCriticalSection(&This->lock);
1005 HeapFree(GetProcessHeap(), 0, This);
1006 }
1007
1008 return ref;
1009 }
1010
1011 static HRESULT WINAPI BmpDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *stream,
1012 DWORD *capability)
1013 {
1014 HRESULT hr;
1015 BmpDecoder *This = impl_from_IWICBitmapDecoder(iface);
1016
1017 TRACE("(%p,%p,%p)\n", iface, stream, capability);
1018
1019 if (!stream || !capability) return E_INVALIDARG;
1020
1021 hr = IWICBitmapDecoder_Initialize(iface, stream, WICDecodeMetadataCacheOnDemand);
1022 if (hr != S_OK) return hr;
1023
1024 *capability = This->read_data_func == BmpFrameDecode_ReadUnsupported ? 0 : WICBitmapDecoderCapabilityCanDecodeAllImages;
1025 return S_OK;
1026 }
1027
1028 static HRESULT WINAPI BmpDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
1029 WICDecodeOptions cacheOptions)
1030 {
1031 HRESULT hr;
1032 BmpDecoder *This = impl_from_IWICBitmapDecoder(iface);
1033
1034 EnterCriticalSection(&This->lock);
1035 hr = BmpDecoder_ReadHeaders(This, pIStream);
1036
1037 if (SUCCEEDED(hr))
1038 {
1039 This->stream = pIStream;
1040 IStream_AddRef(pIStream);
1041 }
1042 LeaveCriticalSection(&This->lock);
1043
1044 return hr;
1045 }
1046
1047 static HRESULT WINAPI BmpDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
1048 GUID *pguidContainerFormat)
1049 {
1050 memcpy(pguidContainerFormat, &GUID_ContainerFormatBmp, sizeof(GUID));
1051 return S_OK;
1052 }
1053
1054 static HRESULT WINAPI BmpDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
1055 IWICBitmapDecoderInfo **ppIDecoderInfo)
1056 {
1057 HRESULT hr;
1058 IWICComponentInfo *compinfo;
1059
1060 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
1061
1062 hr = CreateComponentInfo(&CLSID_WICBmpDecoder, &compinfo);
1063 if (FAILED(hr)) return hr;
1064
1065 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
1066 (void**)ppIDecoderInfo);
1067
1068 IWICComponentInfo_Release(compinfo);
1069
1070 return hr;
1071 }
1072
1073 static HRESULT WINAPI BmpDecoder_CopyPalette(IWICBitmapDecoder *iface,
1074 IWICPalette *pIPalette)
1075 {
1076 TRACE("(%p,%p)\n", iface, pIPalette);
1077
1078 return WINCODEC_ERR_PALETTEUNAVAILABLE;
1079 }
1080
1081 static HRESULT WINAPI BmpDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
1082 IWICMetadataQueryReader **ppIMetadataQueryReader)
1083 {
1084 TRACE("(%p,%p)\n", iface, ppIMetadataQueryReader);
1085 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1086 }
1087
1088 static HRESULT WINAPI BmpDecoder_GetPreview(IWICBitmapDecoder *iface,
1089 IWICBitmapSource **ppIBitmapSource)
1090 {
1091 TRACE("(%p,%p)\n", iface, ppIBitmapSource);
1092 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1093 }
1094
1095 static HRESULT WINAPI BmpDecoder_GetColorContexts(IWICBitmapDecoder *iface,
1096 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
1097 {
1098 TRACE("(%p,%u,%p,%p)\n", iface, cCount, ppIColorContexts, pcActualCount);
1099 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1100 }
1101
1102 static HRESULT WINAPI BmpDecoder_GetThumbnail(IWICBitmapDecoder *iface,
1103 IWICBitmapSource **ppIThumbnail)
1104 {
1105 TRACE("(%p,%p)\n", iface, ppIThumbnail);
1106 return WINCODEC_ERR_CODECNOTHUMBNAIL;
1107 }
1108
1109 static HRESULT WINAPI BmpDecoder_GetFrameCount(IWICBitmapDecoder *iface,
1110 UINT *pCount)
1111 {
1112 if (!pCount) return E_INVALIDARG;
1113
1114 *pCount = 1;
1115 return S_OK;
1116 }
1117
1118 static HRESULT WINAPI BmpDecoder_GetFrame(IWICBitmapDecoder *iface,
1119 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
1120 {
1121 BmpDecoder *This = impl_from_IWICBitmapDecoder(iface);
1122
1123 if (index != 0) return E_INVALIDARG;
1124
1125 if (!This->stream) return WINCODEC_ERR_FRAMEMISSING;
1126
1127 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
1128 IWICBitmapDecoder_AddRef(iface);
1129
1130 return S_OK;
1131 }
1132
1133 static const IWICBitmapDecoderVtbl BmpDecoder_Vtbl = {
1134 BmpDecoder_QueryInterface,
1135 BmpDecoder_AddRef,
1136 BmpDecoder_Release,
1137 BmpDecoder_QueryCapability,
1138 BmpDecoder_Initialize,
1139 BmpDecoder_GetContainerFormat,
1140 BmpDecoder_GetDecoderInfo,
1141 BmpDecoder_CopyPalette,
1142 BmpDecoder_GetMetadataQueryReader,
1143 BmpDecoder_GetPreview,
1144 BmpDecoder_GetColorContexts,
1145 BmpDecoder_GetThumbnail,
1146 BmpDecoder_GetFrameCount,
1147 BmpDecoder_GetFrame
1148 };
1149
1150 static HRESULT BmpDecoder_Create(int packed, int icoframe, BmpDecoder **ppDecoder)
1151 {
1152 BmpDecoder *This;
1153
1154 This = HeapAlloc(GetProcessHeap(), 0, sizeof(BmpDecoder));
1155 if (!This) return E_OUTOFMEMORY;
1156
1157 This->IWICBitmapDecoder_iface.lpVtbl = &BmpDecoder_Vtbl;
1158 This->IWICBitmapFrameDecode_iface.lpVtbl = &BmpDecoder_FrameVtbl;
1159 This->ref = 1;
1160 This->initialized = FALSE;
1161 This->stream = NULL;
1162 This->imagedata = NULL;
1163 InitializeCriticalSection(&This->lock);
1164 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": BmpDecoder.lock");
1165 This->packed = packed;
1166 This->icoframe = icoframe;
1167
1168 *ppDecoder = This;
1169
1170 return S_OK;
1171 }
1172
1173 static HRESULT BmpDecoder_Construct(int packed, int icoframe, IUnknown *pUnkOuter, REFIID iid, void** ppv)
1174 {
1175 BmpDecoder *This;
1176 HRESULT ret;
1177
1178 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1179
1180 *ppv = NULL;
1181
1182 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1183
1184 ret = BmpDecoder_Create(packed, icoframe, &This);
1185 if (FAILED(ret)) return ret;
1186
1187 ret = IWICBitmapDecoder_QueryInterface(&This->IWICBitmapDecoder_iface, iid, ppv);
1188 IWICBitmapDecoder_Release(&This->IWICBitmapDecoder_iface);
1189
1190 return ret;
1191 }
1192
1193 HRESULT BmpDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1194 {
1195 return BmpDecoder_Construct(FALSE, FALSE, pUnkOuter, iid, ppv);
1196 }
1197
1198 HRESULT DibDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1199 {
1200 return BmpDecoder_Construct(TRUE, FALSE, pUnkOuter, iid, ppv);
1201 }
1202
1203 HRESULT IcoDibDecoder_CreateInstance(BmpDecoder **ppDecoder)
1204 {
1205 return BmpDecoder_Create(TRUE, TRUE, ppDecoder);
1206 }
1207
1208 void BmpDecoder_GetWICDecoder(BmpDecoder *This, IWICBitmapDecoder **ppDecoder)
1209 {
1210 *ppDecoder = &This->IWICBitmapDecoder_iface;
1211 }
1212
1213 /* Return the offset where the mask of an icon might be, or 0 for failure. */
1214 void BmpDecoder_FindIconMask(BmpDecoder *This, ULONG *mask_offset, int *topdown)
1215 {
1216 assert(This->stream != NULL);
1217
1218 if (This->read_data_func == BmpFrameDecode_ReadUncompressed)
1219 {
1220 /* RGB or BITFIELDS data */
1221 ULONG width, height, 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;
1226 }
1227 else
1228 *mask_offset = 0;
1229
1230 *topdown = This->stride > 0;
1231 }