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
19 #include "wincodecs_private.h"
27 /* Colormap Specification */
28 WORD colormap_firstentry
;
30 BYTE colormap_entrysize
;
31 /* Image Specification */
37 BYTE image_descriptor
;
40 #define IMAGETYPE_COLORMAPPED 1
41 #define IMAGETYPE_TRUECOLOR 2
42 #define IMAGETYPE_GRAYSCALE 3
43 #define IMAGETYPE_RLE 8
45 #define IMAGE_ATTRIBUTE_BITCOUNT_MASK 0xf
46 #define IMAGE_RIGHTTOLEFT 0x10
47 #define IMAGE_TOPTOBOTTOM 0x20
50 DWORD extension_area_offset
;
51 DWORD developer_directory_offset
;
55 static const BYTE tga_footer_magic
[18] = "TRUEVISION-XFILE.";
60 char author_comments
[324];
63 WORD job_timestamp
[6];
65 WORD software_version
;
66 char software_version_letter
;
71 WORD gamma_denominator
;
72 DWORD color_correction_offset
;
73 DWORD thumbnail_offset
;
74 DWORD scanline_offset
;
78 #define ATTRIBUTE_NO_ALPHA 0
79 #define ATTRIBUTE_UNDEFINED 1
80 #define ATTRIBUTE_UNDEFINED_PRESERVE 2
81 #define ATTRIBUTE_ALPHA 3
82 #define ATTRIBUTE_PALPHA 4
87 IWICBitmapDecoder IWICBitmapDecoder_iface
;
88 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface
;
93 tga_extension_area extension_area
;
98 ULONG colormap_length
;
99 ULONG colormap_offset
;
101 ULONG extension_area_offset
;
102 ULONG developer_directory_offset
;
103 CRITICAL_SECTION lock
;
106 static inline TgaDecoder
*impl_from_IWICBitmapDecoder(IWICBitmapDecoder
*iface
)
108 return CONTAINING_RECORD(iface
, TgaDecoder
, IWICBitmapDecoder_iface
);
111 static inline TgaDecoder
*impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode
*iface
)
113 return CONTAINING_RECORD(iface
, TgaDecoder
, IWICBitmapFrameDecode_iface
);
116 static HRESULT WINAPI
TgaDecoder_QueryInterface(IWICBitmapDecoder
*iface
, REFIID iid
,
119 TgaDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
120 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
122 if (!ppv
) return E_INVALIDARG
;
124 if (IsEqualIID(&IID_IUnknown
, iid
) || IsEqualIID(&IID_IWICBitmapDecoder
, iid
))
126 *ppv
= &This
->IWICBitmapDecoder_iface
;
131 return E_NOINTERFACE
;
134 IUnknown_AddRef((IUnknown
*)*ppv
);
138 static ULONG WINAPI
TgaDecoder_AddRef(IWICBitmapDecoder
*iface
)
140 TgaDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
141 ULONG ref
= InterlockedIncrement(&This
->ref
);
143 TRACE("(%p) refcount=%u\n", iface
, ref
);
148 static ULONG WINAPI
TgaDecoder_Release(IWICBitmapDecoder
*iface
)
150 TgaDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
151 ULONG ref
= InterlockedDecrement(&This
->ref
);
153 TRACE("(%p) refcount=%u\n", iface
, ref
);
157 This
->lock
.DebugInfo
->Spare
[0] = 0;
158 DeleteCriticalSection(&This
->lock
);
160 IStream_Release(This
->stream
);
161 HeapFree(GetProcessHeap(), 0, This
->imagebits
);
162 HeapFree(GetProcessHeap(), 0, This
);
168 static HRESULT WINAPI
TgaDecoder_QueryCapability(IWICBitmapDecoder
*iface
, IStream
*stream
,
173 TRACE("(%p,%p,%p)\n", iface
, stream
, capability
);
175 if (!stream
|| !capability
) return E_INVALIDARG
;
177 hr
= IWICBitmapDecoder_Initialize(iface
, stream
, WICDecodeMetadataCacheOnDemand
);
178 if (hr
!= S_OK
) return hr
;
180 *capability
= WICBitmapDecoderCapabilityCanDecodeAllImages
|
181 WICBitmapDecoderCapabilityCanDecodeSomeImages
;
185 static HRESULT WINAPI
TgaDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
186 WICDecodeOptions cacheOptions
)
188 TgaDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
193 int attribute_bitcount
;
196 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOptions
);
198 EnterCriticalSection(&This
->lock
);
200 if (This
->initialized
)
202 hr
= WINCODEC_ERR_WRONGSTATE
;
207 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
208 if (FAILED(hr
)) goto end
;
210 hr
= IStream_Read(pIStream
, &This
->header
, sizeof(tga_header
), &bytesread
);
211 if (SUCCEEDED(hr
) && bytesread
!= sizeof(tga_header
))
213 TRACE("got only %u bytes\n", bytesread
);
216 if (FAILED(hr
)) goto end
;
218 TRACE("imagetype=%u, colormap type=%u, depth=%u, image descriptor=0x%x\n",
219 This
->header
.image_type
, This
->header
.colormap_type
,
220 This
->header
.depth
, This
->header
.image_descriptor
);
222 /* Sanity checking. Since TGA has no clear identifying markers, we need
223 * to be careful to not load a non-TGA image. */
224 switch (This
->header
.image_type
)
226 case IMAGETYPE_COLORMAPPED
:
227 case IMAGETYPE_COLORMAPPED
|IMAGETYPE_RLE
:
228 if (This
->header
.colormap_type
!= 1)
230 mapped_depth
= This
->header
.colormap_entrysize
;
232 case IMAGETYPE_TRUECOLOR
:
233 case IMAGETYPE_TRUECOLOR
|IMAGETYPE_RLE
:
234 if (This
->header
.colormap_type
!= 0 && This
->header
.colormap_type
!= 1)
236 mapped_depth
= This
->header
.depth
;
238 case IMAGETYPE_GRAYSCALE
:
239 case IMAGETYPE_GRAYSCALE
|IMAGETYPE_RLE
:
240 if (This
->header
.colormap_type
!= 0)
248 if (This
->header
.depth
!= 8 && This
->header
.depth
!= 16 &&
249 This
->header
.depth
!= 24 && This
->header
.depth
!= 32)
252 if ((This
->header
.image_descriptor
& 0xc0) != 0)
255 attribute_bitcount
= This
->header
.image_descriptor
& IMAGE_ATTRIBUTE_BITCOUNT_MASK
;
257 if (attribute_bitcount
&&
258 !((mapped_depth
== 32 && attribute_bitcount
== 8) ||
259 (mapped_depth
== 16 && attribute_bitcount
== 1)))
264 WARN("bad tga header\n");
268 /* Locate data in the file based on the header. */
269 This
->id_offset
= sizeof(tga_header
);
270 This
->colormap_offset
= This
->id_offset
+ This
->header
.id_length
;
271 if (This
->header
.colormap_type
== 1)
272 This
->colormap_length
= ((This
->header
.colormap_entrysize
+7)/8) * This
->header
.colormap_length
;
274 This
->colormap_length
= 0;
275 This
->image_offset
= This
->colormap_offset
+ This
->colormap_length
;
277 /* Read footer if there is one */
278 seek
.QuadPart
= -sizeof(tga_footer
);
279 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_END
, NULL
);
282 hr
= IStream_Read(pIStream
, &footer
, sizeof(tga_footer
), &bytesread
);
283 if (SUCCEEDED(hr
) && bytesread
!= sizeof(tga_footer
))
285 TRACE("got only %u footer bytes\n", bytesread
);
289 if (memcmp(footer
.magic
, tga_footer_magic
, sizeof(tga_footer_magic
)) == 0)
291 This
->extension_area_offset
= footer
.extension_area_offset
;
292 This
->developer_directory_offset
= footer
.developer_directory_offset
;
296 This
->extension_area_offset
= 0;
297 This
->developer_directory_offset
= 0;
302 /* File is too small to have a footer. */
303 This
->extension_area_offset
= 0;
304 This
->developer_directory_offset
= 0;
308 if (This
->extension_area_offset
)
310 seek
.QuadPart
= This
->extension_area_offset
;
311 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
312 if (FAILED(hr
)) goto end
;
314 hr
= IStream_Read(pIStream
, &This
->extension_area
, sizeof(tga_extension_area
), &bytesread
);
315 if (SUCCEEDED(hr
) && bytesread
!= sizeof(tga_extension_area
))
317 TRACE("got only %u extension area bytes\n", bytesread
);
320 if (SUCCEEDED(hr
) && This
->extension_area
.size
< 495)
322 TRACE("extension area is only %u bytes long\n", This
->extension_area
.size
);
325 if (FAILED(hr
)) goto end
;
328 IStream_AddRef(pIStream
);
329 This
->stream
= pIStream
;
330 This
->initialized
= TRUE
;
333 LeaveCriticalSection(&This
->lock
);
337 static HRESULT WINAPI
TgaDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
338 GUID
*pguidContainerFormat
)
340 memcpy(pguidContainerFormat
, &GUID_WineContainerFormatTga
, sizeof(GUID
));
344 static HRESULT WINAPI
TgaDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
345 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
348 IWICComponentInfo
*compinfo
;
350 TRACE("(%p,%p)\n", iface
, ppIDecoderInfo
);
352 hr
= CreateComponentInfo(&CLSID_WineTgaDecoder
, &compinfo
);
353 if (FAILED(hr
)) return hr
;
355 hr
= IWICComponentInfo_QueryInterface(compinfo
, &IID_IWICBitmapDecoderInfo
,
356 (void**)ppIDecoderInfo
);
358 IWICComponentInfo_Release(compinfo
);
363 static HRESULT WINAPI
TgaDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
364 IWICPalette
*pIPalette
)
366 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
370 static HRESULT WINAPI
TgaDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
371 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
373 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
377 static HRESULT WINAPI
TgaDecoder_GetPreview(IWICBitmapDecoder
*iface
,
378 IWICBitmapSource
**ppIBitmapSource
)
380 FIXME("(%p,%p): stub\n", iface
, ppIBitmapSource
);
381 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
384 static HRESULT WINAPI
TgaDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
385 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
387 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
388 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
391 static HRESULT WINAPI
TgaDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
392 IWICBitmapSource
**ppIThumbnail
)
394 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
395 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
398 static HRESULT WINAPI
TgaDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
401 if (!pCount
) return E_INVALIDARG
;
407 static HRESULT WINAPI
TgaDecoder_GetFrame(IWICBitmapDecoder
*iface
,
408 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
410 TgaDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
411 TRACE("(%p,%p)\n", iface
, ppIBitmapFrame
);
413 if (!This
->initialized
) return WINCODEC_ERR_FRAMEMISSING
;
415 if (index
!= 0) return E_INVALIDARG
;
417 IWICBitmapDecoder_AddRef(iface
);
418 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
423 static const IWICBitmapDecoderVtbl TgaDecoder_Vtbl
= {
424 TgaDecoder_QueryInterface
,
427 TgaDecoder_QueryCapability
,
428 TgaDecoder_Initialize
,
429 TgaDecoder_GetContainerFormat
,
430 TgaDecoder_GetDecoderInfo
,
431 TgaDecoder_CopyPalette
,
432 TgaDecoder_GetMetadataQueryReader
,
433 TgaDecoder_GetPreview
,
434 TgaDecoder_GetColorContexts
,
435 TgaDecoder_GetThumbnail
,
436 TgaDecoder_GetFrameCount
,
440 static HRESULT WINAPI
TgaDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
443 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
445 if (!ppv
) return E_INVALIDARG
;
447 if (IsEqualIID(&IID_IUnknown
, iid
) ||
448 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
449 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
456 return E_NOINTERFACE
;
459 IUnknown_AddRef((IUnknown
*)*ppv
);
463 static ULONG WINAPI
TgaDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
465 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
466 return IWICBitmapDecoder_AddRef(&This
->IWICBitmapDecoder_iface
);
469 static ULONG WINAPI
TgaDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
471 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
472 return IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);
475 static HRESULT WINAPI
TgaDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
476 UINT
*puiWidth
, UINT
*puiHeight
)
478 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
480 *puiWidth
= This
->header
.width
;
481 *puiHeight
= This
->header
.height
;
483 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
488 static HRESULT WINAPI
TgaDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
489 WICPixelFormatGUID
*pPixelFormat
)
491 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
492 int attribute_bitcount
;
495 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
497 attribute_bitcount
= This
->header
.image_descriptor
& IMAGE_ATTRIBUTE_BITCOUNT_MASK
;
499 if (attribute_bitcount
&& This
->extension_area_offset
)
500 attribute_type
= This
->extension_area
.attributes_type
;
501 else if (attribute_bitcount
)
502 attribute_type
= ATTRIBUTE_ALPHA
;
504 attribute_type
= ATTRIBUTE_NO_ALPHA
;
506 switch (This
->header
.image_type
& ~IMAGETYPE_RLE
)
508 case IMAGETYPE_COLORMAPPED
:
509 switch (This
->header
.depth
)
512 memcpy(pPixelFormat
, &GUID_WICPixelFormat8bppIndexed
, sizeof(GUID
));
515 FIXME("Unhandled indexed color depth %u\n", This
->header
.depth
);
519 case IMAGETYPE_TRUECOLOR
:
520 switch (This
->header
.depth
)
523 switch (attribute_type
)
525 case ATTRIBUTE_NO_ALPHA
:
526 case ATTRIBUTE_UNDEFINED
:
527 case ATTRIBUTE_UNDEFINED_PRESERVE
:
528 memcpy(pPixelFormat
, &GUID_WICPixelFormat16bppBGR555
, sizeof(GUID
));
530 case ATTRIBUTE_ALPHA
:
531 case ATTRIBUTE_PALPHA
:
532 memcpy(pPixelFormat
, &GUID_WICPixelFormat16bppBGRA5551
, sizeof(GUID
));
535 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type
);
540 memcpy(pPixelFormat
, &GUID_WICPixelFormat24bppBGR
, sizeof(GUID
));
543 switch (attribute_type
)
545 case ATTRIBUTE_NO_ALPHA
:
546 case ATTRIBUTE_UNDEFINED
:
547 case ATTRIBUTE_UNDEFINED_PRESERVE
:
548 memcpy(pPixelFormat
, &GUID_WICPixelFormat32bppBGR
, sizeof(GUID
));
550 case ATTRIBUTE_ALPHA
:
551 memcpy(pPixelFormat
, &GUID_WICPixelFormat32bppBGRA
, sizeof(GUID
));
553 case ATTRIBUTE_PALPHA
:
554 memcpy(pPixelFormat
, &GUID_WICPixelFormat32bppPBGRA
, sizeof(GUID
));
557 FIXME("Unhandled 32-bit attribute type %u\n", attribute_type
);
562 FIXME("Unhandled truecolor depth %u\n", This
->header
.depth
);
566 case IMAGETYPE_GRAYSCALE
:
567 switch (This
->header
.depth
)
570 memcpy(pPixelFormat
, &GUID_WICPixelFormat8bppGray
, sizeof(GUID
));
573 memcpy(pPixelFormat
, &GUID_WICPixelFormat16bppGray
, sizeof(GUID
));
576 FIXME("Unhandled grayscale depth %u\n", This
->header
.depth
);
581 ERR("Unknown image type %u\n", This
->header
.image_type
);
588 static HRESULT WINAPI
TgaDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
589 double *pDpiX
, double *pDpiY
)
591 FIXME("(%p,%p,%p): stub\n", iface
, pDpiX
, pDpiY
);
595 static HRESULT WINAPI
TgaDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
596 IWICPalette
*pIPalette
)
598 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
600 WICColor colors
[256], *color
;
602 WORD
*wcolormap_data
;
603 DWORD
*dwcolormap_data
;
606 int depth
, attribute_bitcount
, attribute_type
;
609 TRACE("(%p,%p)\n", iface
, pIPalette
);
611 if (!This
->colormap_length
)
613 WARN("no colormap present in this file\n");
614 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
617 if (This
->header
.colormap_firstentry
+ This
->header
.colormap_length
> 256)
619 FIXME("cannot read colormap with %i entries starting at %i\n",
620 This
->header
.colormap_firstentry
+ This
->header
.colormap_length
,
621 This
->header
.colormap_firstentry
);
625 colormap_data
= HeapAlloc(GetProcessHeap(), 0, This
->colormap_length
);
626 if (!colormap_data
) return E_OUTOFMEMORY
;
628 wcolormap_data
= (WORD
*)colormap_data
;
629 dwcolormap_data
= (DWORD
*)colormap_data
;
631 EnterCriticalSection(&This
->lock
);
633 seek
.QuadPart
= This
->colormap_offset
;
634 hr
= IStream_Seek(This
->stream
, seek
, STREAM_SEEK_SET
, NULL
);
638 hr
= IStream_Read(This
->stream
, colormap_data
, This
->colormap_length
, &bytesread
);
639 if (SUCCEEDED(hr
) && bytesread
!= This
->colormap_length
)
641 WARN("expected %i bytes in colormap, got %i\n", This
->colormap_length
, bytesread
);
646 LeaveCriticalSection(&This
->lock
);
650 attribute_bitcount
= This
->header
.image_descriptor
& IMAGE_ATTRIBUTE_BITCOUNT_MASK
;
652 if (attribute_bitcount
&& This
->extension_area_offset
)
653 attribute_type
= This
->extension_area
.attributes_type
;
654 else if (attribute_bitcount
)
655 attribute_type
= ATTRIBUTE_ALPHA
;
657 attribute_type
= ATTRIBUTE_NO_ALPHA
;
659 depth
= This
->header
.colormap_entrysize
;
663 attribute_type
= ATTRIBUTE_NO_ALPHA
;
666 memset(colors
, 0, sizeof(colors
));
668 color
= &colors
[This
->header
.colormap_firstentry
];
670 /* Colormap entries can be in any truecolor format, and we have to convert them. */
674 switch (attribute_type
)
676 case ATTRIBUTE_NO_ALPHA
:
677 case ATTRIBUTE_UNDEFINED
:
678 case ATTRIBUTE_UNDEFINED_PRESERVE
:
679 for (i
=0; i
<This
->header
.colormap_length
; i
++)
681 WORD srcval
= wcolormap_data
[i
];
682 *color
++=0xff000000 | /* constant 255 alpha */
683 ((srcval
<< 9) & 0xf80000) | /* r */
684 ((srcval
<< 4) & 0x070000) | /* r - 3 bits */
685 ((srcval
<< 6) & 0x00f800) | /* g */
686 ((srcval
<< 1) & 0x000700) | /* g - 3 bits */
687 ((srcval
<< 3) & 0x0000f8) | /* b */
688 ((srcval
>> 2) & 0x000007); /* b - 3 bits */
691 case ATTRIBUTE_ALPHA
:
692 case ATTRIBUTE_PALPHA
:
693 for (i
=0; i
<This
->header
.colormap_length
; i
++)
695 WORD srcval
= wcolormap_data
[i
];
696 *color
++=((srcval
& 0x8000) ? 0xff000000 : 0) | /* alpha */
697 ((srcval
<< 9) & 0xf80000) | /* r */
698 ((srcval
<< 4) & 0x070000) | /* r - 3 bits */
699 ((srcval
<< 6) & 0x00f800) | /* g */
700 ((srcval
<< 1) & 0x000700) | /* g - 3 bits */
701 ((srcval
<< 3) & 0x0000f8) | /* b */
702 ((srcval
>> 2) & 0x000007); /* b - 3 bits */
706 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type
);
711 for (i
=0; i
<This
->header
.colormap_length
; i
++)
713 *color
++=0xff000000 | /* alpha */
714 colormap_data
[i
*3+2] | /* red */
715 colormap_data
[i
*3+1] | /* green */
716 colormap_data
[i
*3]; /* blue */
720 switch (attribute_type
)
722 case ATTRIBUTE_NO_ALPHA
:
723 case ATTRIBUTE_UNDEFINED
:
724 case ATTRIBUTE_UNDEFINED_PRESERVE
:
725 for (i
=0; i
<This
->header
.colormap_length
; i
++)
726 *color
++=dwcolormap_data
[i
]|0xff000000;
728 case ATTRIBUTE_ALPHA
:
729 for (i
=0; i
<This
->header
.colormap_length
; i
++)
730 *color
++=dwcolormap_data
[i
];
732 case ATTRIBUTE_PALPHA
:
733 /* FIXME: Unpremultiply alpha */
735 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type
);
740 FIXME("Unhandled truecolor depth %u\n", This
->header
.depth
);
745 HeapFree(GetProcessHeap(), 0, colormap_data
);
748 hr
= IWICPalette_InitializeCustom(pIPalette
, colors
, 256);
753 static HRESULT
TgaDecoder_ReadRLE(TgaDecoder
*This
, BYTE
*imagebits
, int datasize
)
755 int i
=0, j
, bytesperpixel
;
759 bytesperpixel
= This
->header
.depth
/ 8;
767 hr
= IStream_Read(This
->stream
, &rc
, 1, &bytesread
);
768 if (bytesread
!= 1) hr
= E_FAIL
;
769 if (FAILED(hr
)) break;
772 size
= count
* bytesperpixel
;
774 if (size
+ i
> datasize
)
776 WARN("RLE packet too large\n");
783 /* Run-length packet */
784 hr
= IStream_Read(This
->stream
, pixeldata
, bytesperpixel
, &bytesread
);
785 if (bytesread
!= bytesperpixel
) hr
= E_FAIL
;
786 if (FAILED(hr
)) break;
788 if (bytesperpixel
== 1)
789 memset(&imagebits
[i
], pixeldata
[0], count
);
792 for (j
=0; j
<count
; j
++)
793 memcpy(&imagebits
[i
+j
*bytesperpixel
], pixeldata
, bytesperpixel
);
799 hr
= IStream_Read(This
->stream
, &imagebits
[i
], size
, &bytesread
);
800 if (bytesread
!= size
) hr
= E_FAIL
;
801 if (FAILED(hr
)) break;
810 static HRESULT
TgaDecoder_ReadImage(TgaDecoder
*This
)
820 EnterCriticalSection(&This
->lock
);
822 if (!This
->imagebits
)
824 if (This
->header
.image_descriptor
& IMAGE_RIGHTTOLEFT
)
826 FIXME("Right to left image reading not implemented\n");
832 datasize
= This
->header
.width
* This
->header
.height
* (This
->header
.depth
/ 8);
833 This
->imagebits
= HeapAlloc(GetProcessHeap(), 0, datasize
);
834 if (!This
->imagebits
) hr
= E_OUTOFMEMORY
;
839 seek
.QuadPart
= This
->image_offset
;
840 hr
= IStream_Seek(This
->stream
, seek
, STREAM_SEEK_SET
, NULL
);
845 if (This
->header
.image_type
& IMAGETYPE_RLE
)
847 hr
= TgaDecoder_ReadRLE(This
, This
->imagebits
, datasize
);
851 hr
= IStream_Read(This
->stream
, This
->imagebits
, datasize
, &bytesread
);
852 if (SUCCEEDED(hr
) && bytesread
!= datasize
)
859 if (This
->header
.image_descriptor
& IMAGE_TOPTOBOTTOM
)
861 This
->origin
= This
->imagebits
;
862 This
->stride
= This
->header
.width
* (This
->header
.depth
/ 8);
866 This
->stride
= -This
->header
.width
* (This
->header
.depth
/ 8);
867 This
->origin
= This
->imagebits
+ This
->header
.width
* (This
->header
.height
- 1) * (This
->header
.depth
/ 8);
872 HeapFree(GetProcessHeap(), 0, This
->imagebits
);
873 This
->imagebits
= NULL
;
877 LeaveCriticalSection(&This
->lock
);
882 static HRESULT WINAPI
TgaDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
883 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
885 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
888 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
890 hr
= TgaDecoder_ReadImage(This
);
894 hr
= copy_pixels(This
->header
.depth
, This
->origin
,
895 This
->header
.width
, This
->header
.height
, This
->stride
,
896 prc
, cbStride
, cbBufferSize
, pbBuffer
);
902 static HRESULT WINAPI
TgaDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
903 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
905 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
906 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
909 static HRESULT WINAPI
TgaDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
910 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
912 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
913 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
916 static HRESULT WINAPI
TgaDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
917 IWICBitmapSource
**ppIThumbnail
)
919 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
920 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
923 static const IWICBitmapFrameDecodeVtbl TgaDecoder_Frame_Vtbl
= {
924 TgaDecoder_Frame_QueryInterface
,
925 TgaDecoder_Frame_AddRef
,
926 TgaDecoder_Frame_Release
,
927 TgaDecoder_Frame_GetSize
,
928 TgaDecoder_Frame_GetPixelFormat
,
929 TgaDecoder_Frame_GetResolution
,
930 TgaDecoder_Frame_CopyPalette
,
931 TgaDecoder_Frame_CopyPixels
,
932 TgaDecoder_Frame_GetMetadataQueryReader
,
933 TgaDecoder_Frame_GetColorContexts
,
934 TgaDecoder_Frame_GetThumbnail
937 HRESULT
TgaDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
942 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
946 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
948 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(TgaDecoder
));
949 if (!This
) return E_OUTOFMEMORY
;
951 This
->IWICBitmapDecoder_iface
.lpVtbl
= &TgaDecoder_Vtbl
;
952 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &TgaDecoder_Frame_Vtbl
;
954 This
->initialized
= FALSE
;
956 This
->imagebits
= NULL
;
957 InitializeCriticalSection(&This
->lock
);
958 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": TgaDecoder.lock");
960 ret
= IWICBitmapDecoder_QueryInterface(&This
->IWICBitmapDecoder_iface
, iid
, ppv
);
961 IWICBitmapDecoder_Release(&This
->IWICBitmapDecoder_iface
);