2 * Copyright 2010 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
20 #include "wine/port.h"
31 #include "wincodecs_private.h"
33 #include "wine/debug.h"
34 #include "wine/library.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
44 /* Colormap Specification */
45 WORD colormap_firstentry
;
47 BYTE colormap_entrysize
;
48 /* Image Specification */
54 BYTE image_descriptor
;
57 #define IMAGETYPE_COLORMAPPED 1
58 #define IMAGETYPE_TRUECOLOR 2
59 #define IMAGETYPE_GRAYSCALE 3
60 #define IMAGETYPE_RLE 8
62 #define IMAGE_ATTRIBUTE_BITCOUNT_MASK 0xf
63 #define IMAGE_RIGHTTOLEFT 0x10
64 #define IMAGE_TOPTOBOTTOM 0x20
67 DWORD extension_area_offset
;
68 DWORD developer_directory_offset
;
72 static const BYTE tga_footer_magic
[18] = "TRUEVISION-XFILE.";
77 char author_comments
[324];
80 WORD job_timestamp
[6];
82 WORD software_version
;
83 char software_version_letter
;
88 WORD gamma_denominator
;
89 DWORD color_correction_offset
;
90 DWORD thumbnail_offset
;
91 DWORD scanline_offset
;
95 #define ATTRIBUTE_NO_ALPHA 0
96 #define ATTRIBUTE_UNDEFINED 1
97 #define ATTRIBUTE_UNDEFINED_PRESERVE 2
98 #define ATTRIBUTE_ALPHA 3
99 #define ATTRIBUTE_PALPHA 4
104 IWICBitmapDecoder IWICBitmapDecoder_iface
;
105 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
110 tga_extension_area extension_area
;
115 ULONG colormap_length
;
116 ULONG colormap_offset
;
118 ULONG extension_area_offset
;
119 ULONG developer_directory_offset
;
120 CRITICAL_SECTION lock
;
123 static inline TgaDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
125 return CONTAINING_RECORD(iface
, TgaDecoder
, IWICBitmapDecoder_iface
);
128 static inline TgaDecoder
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
130 return CONTAINING_RECORD(iface
, TgaDecoder
, IWICBitmapFrameDecode_iface
);
133 static HRESULT WINAPI
TgaDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
136 TgaDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
137 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
139 if (!ppv
) return E_INVALIDARG
;
141 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
143 *ppv
= &This
->IWICBitmapDecoder_iface
;
148 return E_NOINTERFACE
;
151 IUnknown_AddRef((IUnknown
*)*ppv
);
155 static ULONG WINAPI
TgaDecoder_AddRef(IWICBitmapDecoder
*iface
)
157 TgaDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
158 ULONG ref
= InterlockedIncrement(&This
->ref
);
160 TRACE("(%p) refcount=%u\n", iface
, ref
);
165 static ULONG WINAPI
TgaDecoder_Release(IWICBitmapDecoder
*iface
)
167 TgaDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
168 ULONG ref
= InterlockedDecrement(&This
->ref
);
170 TRACE("(%p) refcount=%u\n", iface
, ref
);
174 This
->lock
.DebugInfo
->Spare
[0] = 0;
175 DeleteCriticalSection(&This
->lock
);
177 IStream_Release(This
->stream
);
178 HeapFree(GetProcessHeap(), 0, This
->imagebits
);
179 HeapFree(GetProcessHeap(), 0, This
);
185 static HRESULT WINAPI
TgaDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
190 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
192 if (!stream
|| !capability
) return E_INVALIDARG
;
194 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
195 if (hr
!= S_OK
) return hr
;
197 *capability
= WICBitmapDecoderCapabilityCanDecodeAllImages
|
198 WICBitmapDecoderCapabilityCanDecodeSomeImages
;
202 static HRESULT WINAPI
TgaDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
203 WICDecodeOptions cacheOptions
)
205 TgaDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
210 int attribute_bitcount
;
213 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOptions
);
215 EnterCriticalSection(&This
->lock
);
217 if (This
->initialized
)
219 hr
= WINCODEC_ERR_WRONGSTATE
;
224 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
225 if (FAILED(hr
)) goto end
;
227 hr
= IStream_Read(pIStream
, &This
->header
, sizeof(tga_header
), &bytesread
);
228 if (SUCCEEDED(hr
) && bytesread
!= sizeof(tga_header
))
230 TRACE("got only %u bytes\n", bytesread
);
233 if (FAILED(hr
)) goto end
;
235 TRACE("imagetype=%u, colormap type=%u, depth=%u, image descriptor=0x%x\n",
236 This
->header
.image_type
, This
->header
.colormap_type
,
237 This
->header
.depth
, This
->header
.image_descriptor
);
239 /* Sanity checking. Since TGA has no clear identifying markers, we need
240 * to be careful to not load a non-TGA image. */
241 switch (This
->header
.image_type
)
243 case IMAGETYPE_COLORMAPPED
:
244 case IMAGETYPE_COLORMAPPED
|IMAGETYPE_RLE
:
245 if (This
->header
.colormap_type
!= 1)
247 mapped_depth
= This
->header
.colormap_entrysize
;
249 case IMAGETYPE_TRUECOLOR
:
250 case IMAGETYPE_TRUECOLOR
|IMAGETYPE_RLE
:
251 if (This
->header
.colormap_type
!= 0 && This
->header
.colormap_type
!= 1)
253 mapped_depth
= This
->header
.depth
;
255 case IMAGETYPE_GRAYSCALE
:
256 case IMAGETYPE_GRAYSCALE
|IMAGETYPE_RLE
:
257 if (This
->header
.colormap_type
!= 0)
265 if (This
->header
.depth
!= 8 && This
->header
.depth
!= 16 &&
266 This
->header
.depth
!= 24 && This
->header
.depth
!= 32)
269 if ((This
->header
.image_descriptor
& 0xc0) != 0)
272 attribute_bitcount
= This
->header
.image_descriptor
& IMAGE_ATTRIBUTE_BITCOUNT_MASK
;
274 if (attribute_bitcount
&&
275 !((mapped_depth
== 32 && attribute_bitcount
== 8) ||
276 (mapped_depth
== 16 && attribute_bitcount
== 1)))
281 WARN("bad tga header\n");
285 /* Locate data in the file based on the header. */
286 This
->id_offset
= sizeof(tga_header
);
287 This
->colormap_offset
= This
->id_offset
+ This
->header
.id_length
;
288 if (This
->header
.colormap_type
== 1)
289 This
->colormap_length
= ((This
->header
.colormap_entrysize
+7)/8) * This
->header
.colormap_length
;
291 This
->colormap_length
= 0;
292 This
->image_offset
= This
->colormap_offset
+ This
->colormap_length
;
294 /* Read footer if there is one */
295 seek
.QuadPart
= -(LONGLONG
)sizeof(tga_footer
);
296 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_END
, NULL
);
299 hr
= IStream_Read(pIStream
, &footer
, sizeof(tga_footer
), &bytesread
);
300 if (SUCCEEDED(hr
) && bytesread
!= sizeof(tga_footer
))
302 TRACE("got only %u footer bytes\n", bytesread
);
306 if (memcmp(footer
.magic
, tga_footer_magic
, sizeof(tga_footer_magic
)) == 0)
308 This
->extension_area_offset
= footer
.extension_area_offset
;
309 This
->developer_directory_offset
= footer
.developer_directory_offset
;
313 This
->extension_area_offset
= 0;
314 This
->developer_directory_offset
= 0;
319 /* File is too small to have a footer. */
320 This
->extension_area_offset
= 0;
321 This
->developer_directory_offset
= 0;
325 if (This
->extension_area_offset
)
327 seek
.QuadPart
= This
->extension_area_offset
;
328 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
329 if (FAILED(hr
)) goto end
;
331 hr
= IStream_Read(pIStream
, &This
->extension_area
, sizeof(tga_extension_area
), &bytesread
);
332 if (SUCCEEDED(hr
) && bytesread
!= sizeof(tga_extension_area
))
334 TRACE("got only %u extension area bytes\n", bytesread
);
337 if (SUCCEEDED(hr
) && This
->extension_area
.size
< 495)
339 TRACE("extension area is only %u bytes long\n", This
->extension_area
.size
);
342 if (FAILED(hr
)) goto end
;
345 IStream_AddRef(pIStream
);
346 This
->stream
= pIStream
;
347 This
->initialized
= TRUE
;
350 LeaveCriticalSection(&This
->lock
);
354 static HRESULT WINAPI
TgaDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
355 GUID
*pguidContainerFormat
)
357 memcpy(pguidContainerFormat
, &GUID_WineContainerFormatTga
, sizeof(GUID
));
361 static HRESULT WINAPI
TgaDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
362 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
365 IWICComponentInfo
*compinfo
;
367 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
369 hr
= CreateComponentInfo(&CLSID_WineTgaDecoder
, &compinfo
);
370 if (FAILED(hr
)) return hr
;
372 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
373 (void**)ppIDecoderInfo
);
375 IWICComponentInfo_Release(compinfo
);
380 static HRESULT WINAPI
TgaDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
381 IWICPalette
*pIPalette
)
383 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
387 static HRESULT WINAPI
TgaDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
388 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
390 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
394 static HRESULT WINAPI
TgaDecoder_GetPreview(IWICBitmapDecoder
*iface
,
395 IWICBitmapSource
**ppIBitmapSource
)
397 FIXME("(%p,%p): stub\n", iface
, ppIBitmapSource
);
398 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
401 static HRESULT WINAPI
TgaDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
402 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
404 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
405 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
408 static HRESULT WINAPI
TgaDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
409 IWICBitmapSource
**ppIThumbnail
)
411 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
412 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
415 static HRESULT WINAPI
TgaDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
418 if (!pCount
) return E_INVALIDARG
;
424 static HRESULT WINAPI
TgaDecoder_GetFrame(IWICBitmapDecoder
*iface
,
425 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
427 TgaDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
428 TRACE("(%p,%p)\n", iface
, ppIBitmapFrame
);
430 if (!This
->initialized
) return WINCODEC_ERR_FRAMEMISSING
;
432 if (index
!= 0) return E_INVALIDARG
;
434 IWICBitmapDecoder_AddRef(iface
);
435 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
440 static const IWICBitmapDecoderVtbl TgaDecoder_Vtbl
= {
441 TgaDecoder_QueryInterface
,
444 TgaDecoder_QueryCapability
,
445 TgaDecoder_Initialize
,
446 TgaDecoder_GetContainerFormat
,
447 TgaDecoder_GetDecoderInfo
,
448 TgaDecoder_CopyPalette
,
449 TgaDecoder_GetMetadataQueryReader
,
450 TgaDecoder_GetPreview
,
451 TgaDecoder_GetColorContexts
,
452 TgaDecoder_GetThumbnail
,
453 TgaDecoder_GetFrameCount
,
457 static HRESULT WINAPI
TgaDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
460 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
462 if (!ppv
) return E_INVALIDARG
;
464 if (IsEqualIID(&IID_IUnknown
, iid
) ||
465 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
466 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
473 return E_NOINTERFACE
;
476 IUnknown_AddRef((IUnknown
*)*ppv
);
480 static ULONG WINAPI
TgaDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
482 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
483 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
486 static ULONG WINAPI
TgaDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
488 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
489 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
492 static HRESULT WINAPI
TgaDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
493 UINT
*puiWidth
, UINT
*puiHeight
)
495 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
497 *puiWidth
= This
->header
.width
;
498 *puiHeight
= This
->header
.height
;
500 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
505 static HRESULT WINAPI
TgaDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
506 WICPixelFormatGUID
*pPixelFormat
)
508 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
509 int attribute_bitcount
;
512 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
514 attribute_bitcount
= This
->header
.image_descriptor
& IMAGE_ATTRIBUTE_BITCOUNT_MASK
;
516 if (attribute_bitcount
&& This
->extension_area_offset
)
517 attribute_type
= This
->extension_area
.attributes_type
;
518 else if (attribute_bitcount
)
519 attribute_type
= ATTRIBUTE_ALPHA
;
521 attribute_type
= ATTRIBUTE_NO_ALPHA
;
523 switch (This
->header
.image_type
& ~IMAGETYPE_RLE
)
525 case IMAGETYPE_COLORMAPPED
:
526 switch (This
->header
.depth
)
529 memcpy(pPixelFormat
, &GUID_WICPixelFormat8bppIndexed
, sizeof(GUID
));
532 FIXME("Unhandled indexed color depth %u\n", This
->header
.depth
);
536 case IMAGETYPE_TRUECOLOR
:
537 switch (This
->header
.depth
)
540 switch (attribute_type
)
542 case ATTRIBUTE_NO_ALPHA
:
543 case ATTRIBUTE_UNDEFINED
:
544 case ATTRIBUTE_UNDEFINED_PRESERVE
:
545 memcpy(pPixelFormat
, &GUID_WICPixelFormat16bppBGR555
, sizeof(GUID
));
547 case ATTRIBUTE_ALPHA
:
548 case ATTRIBUTE_PALPHA
:
549 memcpy(pPixelFormat
, &GUID_WICPixelFormat16bppBGRA5551
, sizeof(GUID
));
552 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type
);
557 memcpy(pPixelFormat
, &GUID_WICPixelFormat24bppBGR
, sizeof(GUID
));
560 switch (attribute_type
)
562 case ATTRIBUTE_NO_ALPHA
:
563 case ATTRIBUTE_UNDEFINED
:
564 case ATTRIBUTE_UNDEFINED_PRESERVE
:
565 memcpy(pPixelFormat
, &GUID_WICPixelFormat32bppBGR
, sizeof(GUID
));
567 case ATTRIBUTE_ALPHA
:
568 memcpy(pPixelFormat
, &GUID_WICPixelFormat32bppBGRA
, sizeof(GUID
));
570 case ATTRIBUTE_PALPHA
:
571 memcpy(pPixelFormat
, &GUID_WICPixelFormat32bppPBGRA
, sizeof(GUID
));
574 FIXME("Unhandled 32-bit attribute type %u\n", attribute_type
);
579 FIXME("Unhandled truecolor depth %u\n", This
->header
.depth
);
583 case IMAGETYPE_GRAYSCALE
:
584 switch (This
->header
.depth
)
587 memcpy(pPixelFormat
, &GUID_WICPixelFormat8bppGray
, sizeof(GUID
));
590 memcpy(pPixelFormat
, &GUID_WICPixelFormat16bppGray
, sizeof(GUID
));
593 FIXME("Unhandled grayscale depth %u\n", This
->header
.depth
);
598 ERR("Unknown image type %u\n", This
->header
.image_type
);
605 static HRESULT WINAPI
TgaDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
606 double *pDpiX
, double *pDpiY
)
608 FIXME("(%p,%p,%p): stub\n", iface
, pDpiX
, pDpiY
);
612 static HRESULT WINAPI
TgaDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
613 IWICPalette
*pIPalette
)
615 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
617 WICColor colors
[256], *color
;
619 WORD
*wcolormap_data
;
620 DWORD
*dwcolormap_data
;
623 int depth
, attribute_bitcount
, attribute_type
;
626 TRACE("(%p,%p)\n", iface
, pIPalette
);
628 if (!This
->colormap_length
)
630 WARN("no colormap present in this file\n");
631 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
634 if (This
->header
.colormap_firstentry
+ This
->header
.colormap_length
> 256)
636 FIXME("cannot read colormap with %i entries starting at %i\n",
637 This
->header
.colormap_firstentry
+ This
->header
.colormap_length
,
638 This
->header
.colormap_firstentry
);
642 colormap_data
= HeapAlloc(GetProcessHeap(), 0, This
->colormap_length
);
643 if (!colormap_data
) return E_OUTOFMEMORY
;
645 wcolormap_data
= (WORD
*)colormap_data
;
646 dwcolormap_data
= (DWORD
*)colormap_data
;
648 EnterCriticalSection(&This
->lock
);
650 seek
.QuadPart
= This
->colormap_offset
;
651 hr
= IStream_Seek(This
->stream
, seek
, STREAM_SEEK_SET
, NULL
);
655 hr
= IStream_Read(This
->stream
, colormap_data
, This
->colormap_length
, &bytesread
);
656 if (SUCCEEDED(hr
) && bytesread
!= This
->colormap_length
)
658 WARN("expected %i bytes in colormap, got %i\n", This
->colormap_length
, bytesread
);
663 LeaveCriticalSection(&This
->lock
);
667 attribute_bitcount
= This
->header
.image_descriptor
& IMAGE_ATTRIBUTE_BITCOUNT_MASK
;
669 if (attribute_bitcount
&& This
->extension_area_offset
)
670 attribute_type
= This
->extension_area
.attributes_type
;
671 else if (attribute_bitcount
)
672 attribute_type
= ATTRIBUTE_ALPHA
;
674 attribute_type
= ATTRIBUTE_NO_ALPHA
;
676 depth
= This
->header
.colormap_entrysize
;
680 attribute_type
= ATTRIBUTE_NO_ALPHA
;
683 memset(colors
, 0, sizeof(colors
));
685 color
= &colors
[This
->header
.colormap_firstentry
];
687 /* Colormap entries can be in any truecolor format, and we have to convert them. */
691 switch (attribute_type
)
693 case ATTRIBUTE_NO_ALPHA
:
694 case ATTRIBUTE_UNDEFINED
:
695 case ATTRIBUTE_UNDEFINED_PRESERVE
:
696 for (i
=0; i
<This
->header
.colormap_length
; i
++)
698 WORD srcval
= wcolormap_data
[i
];
699 *color
++=0xff000000 | /* constant 255 alpha */
700 ((srcval
<< 9) & 0xf80000) | /* r */
701 ((srcval
<< 4) & 0x070000) | /* r - 3 bits */
702 ((srcval
<< 6) & 0x00f800) | /* g */
703 ((srcval
<< 1) & 0x000700) | /* g - 3 bits */
704 ((srcval
<< 3) & 0x0000f8) | /* b */
705 ((srcval
>> 2) & 0x000007); /* b - 3 bits */
708 case ATTRIBUTE_ALPHA
:
709 case ATTRIBUTE_PALPHA
:
710 for (i
=0; i
<This
->header
.colormap_length
; i
++)
712 WORD srcval
= wcolormap_data
[i
];
713 *color
++=((srcval
& 0x8000) ? 0xff000000 : 0) | /* alpha */
714 ((srcval
<< 9) & 0xf80000) | /* r */
715 ((srcval
<< 4) & 0x070000) | /* r - 3 bits */
716 ((srcval
<< 6) & 0x00f800) | /* g */
717 ((srcval
<< 1) & 0x000700) | /* g - 3 bits */
718 ((srcval
<< 3) & 0x0000f8) | /* b */
719 ((srcval
>> 2) & 0x000007); /* b - 3 bits */
723 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type
);
728 for (i
=0; i
<This
->header
.colormap_length
; i
++)
730 *color
++=0xff000000 | /* alpha */
731 colormap_data
[i
*3+2] | /* red */
732 colormap_data
[i
*3+1] | /* green */
733 colormap_data
[i
*3]; /* blue */
737 switch (attribute_type
)
739 case ATTRIBUTE_NO_ALPHA
:
740 case ATTRIBUTE_UNDEFINED
:
741 case ATTRIBUTE_UNDEFINED_PRESERVE
:
742 for (i
=0; i
<This
->header
.colormap_length
; i
++)
743 *color
++=dwcolormap_data
[i
]|0xff000000;
745 case ATTRIBUTE_ALPHA
:
746 for (i
=0; i
<This
->header
.colormap_length
; i
++)
747 *color
++=dwcolormap_data
[i
];
749 case ATTRIBUTE_PALPHA
:
750 /* FIXME: Unpremultiply alpha */
752 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type
);
757 FIXME("Unhandled truecolor depth %u\n", This
->header
.depth
);
762 HeapFree(GetProcessHeap(), 0, colormap_data
);
765 hr
= IWICPalette_InitializeCustom(pIPalette
, colors
, 256);
770 static HRESULT
TgaDecoder_ReadRLE(TgaDecoder
*This
, BYTE
*imagebits
, int datasize
)
772 int i
=0, j
, bytesperpixel
;
776 bytesperpixel
= This
->header
.depth
/ 8;
784 hr
= IStream_Read(This
->stream
, &rc
, 1, &bytesread
);
785 if (bytesread
!= 1) hr
= E_FAIL
;
786 if (FAILED(hr
)) break;
789 size
= count
* bytesperpixel
;
791 if (size
+ i
> datasize
)
793 WARN("RLE packet too large\n");
800 /* Run-length packet */
801 hr
= IStream_Read(This
->stream
, pixeldata
, bytesperpixel
, &bytesread
);
802 if (bytesread
!= bytesperpixel
) hr
= E_FAIL
;
803 if (FAILED(hr
)) break;
805 if (bytesperpixel
== 1)
806 memset(&imagebits
[i
], pixeldata
[0], count
);
809 for (j
=0; j
<count
; j
++)
810 memcpy(&imagebits
[i
+j
*bytesperpixel
], pixeldata
, bytesperpixel
);
816 hr
= IStream_Read(This
->stream
, &imagebits
[i
], size
, &bytesread
);
817 if (bytesread
!= size
) hr
= E_FAIL
;
818 if (FAILED(hr
)) break;
827 static HRESULT
TgaDecoder_ReadImage(TgaDecoder
*This
)
837 EnterCriticalSection(&This
->lock
);
839 if (!This
->imagebits
)
841 if (This
->header
.image_descriptor
& IMAGE_RIGHTTOLEFT
)
843 FIXME("Right to left image reading not implemented\n");
849 datasize
= This
->header
.width
* This
->header
.height
* (This
->header
.depth
/ 8);
850 This
->imagebits
= HeapAlloc(GetProcessHeap(), 0, datasize
);
851 if (!This
->imagebits
) hr
= E_OUTOFMEMORY
;
856 seek
.QuadPart
= This
->image_offset
;
857 hr
= IStream_Seek(This
->stream
, seek
, STREAM_SEEK_SET
, NULL
);
862 if (This
->header
.image_type
& IMAGETYPE_RLE
)
864 hr
= TgaDecoder_ReadRLE(This
, This
->imagebits
, datasize
);
868 hr
= IStream_Read(This
->stream
, This
->imagebits
, datasize
, &bytesread
);
869 if (SUCCEEDED(hr
) && bytesread
!= datasize
)
876 if (This
->header
.image_descriptor
& IMAGE_TOPTOBOTTOM
)
878 This
->origin
= This
->imagebits
;
879 This
->stride
= This
->header
.width
* (This
->header
.depth
/ 8);
883 This
->stride
= -This
->header
.width
* (This
->header
.depth
/ 8);
884 This
->origin
= This
->imagebits
+ This
->header
.width
* (This
->header
.height
- 1) * (This
->header
.depth
/ 8);
889 HeapFree(GetProcessHeap(), 0, This
->imagebits
);
890 This
->imagebits
= NULL
;
894 LeaveCriticalSection(&This
->lock
);
899 static HRESULT WINAPI
TgaDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
900 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
902 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
905 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
907 hr
= TgaDecoder_ReadImage(This
);
911 hr
= copy_pixels(This
->header
.depth
, This
->origin
,
912 This
->header
.width
, This
->header
.height
, This
->stride
,
913 prc
, cbStride
, cbBufferSize
, pbBuffer
);
919 static HRESULT WINAPI
TgaDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
920 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
922 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
923 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
926 static HRESULT WINAPI
TgaDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
927 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
929 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
930 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
933 static HRESULT WINAPI
TgaDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
934 IWICBitmapSource
**ppIThumbnail
)
936 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
937 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
940 static const IWICBitmapFrameDecodeVtbl TgaDecoder_Frame_Vtbl
= {
941 TgaDecoder_Frame_QueryInterface
,
942 TgaDecoder_Frame_AddRef
,
943 TgaDecoder_Frame_Release
,
944 TgaDecoder_Frame_GetSize
,
945 TgaDecoder_Frame_GetPixelFormat
,
946 TgaDecoder_Frame_GetResolution
,
947 TgaDecoder_Frame_CopyPalette
,
948 TgaDecoder_Frame_CopyPixels
,
949 TgaDecoder_Frame_GetMetadataQueryReader
,
950 TgaDecoder_Frame_GetColorContexts
,
951 TgaDecoder_Frame_GetThumbnail
954 HRESULT
TgaDecoder_CreateInstance(REFIID iid
, void** ppv
)
959 TRACE("(%s,%p)\n", debugstr_guid(iid
), ppv
);
963 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(TgaDecoder
));
964 if (!This
) return E_OUTOFMEMORY
;
966 This
->IWICBitmapDecoder_iface
.lpVtbl
= &TgaDecoder_Vtbl
;
967 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &TgaDecoder_Frame_Vtbl
;
969 This
->initialized
= FALSE
;
971 This
->imagebits
= NULL
;
972 InitializeCriticalSection(&This
->lock
);
973 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": TgaDecoder.lock");
975 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
976 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);