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
))
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
*pIStream
,
186 DWORD
*pdwCapability
)
188 FIXME("(%p,%p,%p): stub\n", iface
, pIStream
, pdwCapability
);
192 static HRESULT WINAPI
TgaDecoder_Initialize(IWICBitmapDecoder
*iface
, IStream
*pIStream
,
193 WICDecodeOptions cacheOptions
)
195 TgaDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
200 int attribute_bitcount
;
203 TRACE("(%p,%p,%u)\n", iface
, pIStream
, cacheOptions
);
205 EnterCriticalSection(&This
->lock
);
207 if (This
->initialized
)
209 hr
= WINCODEC_ERR_WRONGSTATE
;
214 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
215 if (FAILED(hr
)) goto end
;
217 hr
= IStream_Read(pIStream
, &This
->header
, sizeof(tga_header
), &bytesread
);
218 if (SUCCEEDED(hr
) && bytesread
!= sizeof(tga_header
))
220 TRACE("got only %u bytes\n", bytesread
);
223 if (FAILED(hr
)) goto end
;
225 TRACE("imagetype=%u, colormap type=%u, depth=%u, image descriptor=0x%x\n",
226 This
->header
.image_type
, This
->header
.colormap_type
,
227 This
->header
.depth
, This
->header
.image_descriptor
);
229 /* Sanity checking. Since TGA has no clear identifying markers, we need
230 * to be careful to not load a non-TGA image. */
231 switch (This
->header
.image_type
)
233 case IMAGETYPE_COLORMAPPED
:
234 case IMAGETYPE_COLORMAPPED
|IMAGETYPE_RLE
:
235 if (This
->header
.colormap_type
!= 1)
237 mapped_depth
= This
->header
.colormap_entrysize
;
239 case IMAGETYPE_TRUECOLOR
:
240 case IMAGETYPE_TRUECOLOR
|IMAGETYPE_RLE
:
241 if (This
->header
.colormap_type
!= 0 && This
->header
.colormap_type
!= 1)
243 mapped_depth
= This
->header
.depth
;
245 case IMAGETYPE_GRAYSCALE
:
246 case IMAGETYPE_GRAYSCALE
|IMAGETYPE_RLE
:
247 if (This
->header
.colormap_type
!= 0)
255 if (This
->header
.depth
!= 8 && This
->header
.depth
!= 16 &&
256 This
->header
.depth
!= 24 && This
->header
.depth
!= 32)
259 if ((This
->header
.image_descriptor
& 0xc0) != 0)
262 attribute_bitcount
= This
->header
.image_descriptor
& IMAGE_ATTRIBUTE_BITCOUNT_MASK
;
264 if (attribute_bitcount
&&
265 !((mapped_depth
== 32 && attribute_bitcount
== 8) ||
266 (mapped_depth
== 16 && attribute_bitcount
== 1)))
271 WARN("bad tga header\n");
275 /* Locate data in the file based on the header. */
276 This
->id_offset
= sizeof(tga_header
);
277 This
->colormap_offset
= This
->id_offset
+ This
->header
.id_length
;
278 if (This
->header
.colormap_type
== 1)
279 This
->colormap_length
= ((This
->header
.colormap_entrysize
+7)/8) * This
->header
.colormap_length
;
281 This
->colormap_length
= 0;
282 This
->image_offset
= This
->colormap_offset
+ This
->colormap_length
;
284 /* Read footer if there is one */
285 seek
.QuadPart
= -sizeof(tga_footer
);
286 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_END
, NULL
);
289 hr
= IStream_Read(pIStream
, &footer
, sizeof(tga_footer
), &bytesread
);
290 if (SUCCEEDED(hr
) && bytesread
!= sizeof(tga_footer
))
292 TRACE("got only %u footer bytes\n", bytesread
);
296 if (memcmp(footer
.magic
, tga_footer_magic
, sizeof(tga_footer_magic
)) == 0)
298 This
->extension_area_offset
= footer
.extension_area_offset
;
299 This
->developer_directory_offset
= footer
.developer_directory_offset
;
303 This
->extension_area_offset
= 0;
304 This
->developer_directory_offset
= 0;
309 /* File is too small to have a footer. */
310 This
->extension_area_offset
= 0;
311 This
->developer_directory_offset
= 0;
315 if (This
->extension_area_offset
)
317 seek
.QuadPart
= This
->extension_area_offset
;
318 hr
= IStream_Seek(pIStream
, seek
, STREAM_SEEK_SET
, NULL
);
319 if (FAILED(hr
)) goto end
;
321 hr
= IStream_Read(pIStream
, &This
->extension_area
, sizeof(tga_extension_area
), &bytesread
);
322 if (SUCCEEDED(hr
) && bytesread
!= sizeof(tga_extension_area
))
324 TRACE("got only %u extension area bytes\n", bytesread
);
327 if (SUCCEEDED(hr
) && This
->extension_area
.size
< 495)
329 TRACE("extension area is only %u bytes long\n", This
->extension_area
.size
);
332 if (FAILED(hr
)) goto end
;
335 IStream_AddRef(pIStream
);
336 This
->stream
= pIStream
;
337 This
->initialized
= TRUE
;
340 LeaveCriticalSection(&This
->lock
);
344 static HRESULT WINAPI
TgaDecoder_GetContainerFormat(IWICBitmapDecoder
*iface
,
345 GUID
*pguidContainerFormat
)
347 memcpy(pguidContainerFormat
, &GUID_WineContainerFormatTga
, sizeof(GUID
));
351 static HRESULT WINAPI
TgaDecoder_GetDecoderInfo(IWICBitmapDecoder
*iface
,
352 IWICBitmapDecoderInfo
**ppIDecoderInfo
)
354 FIXME("(%p,%p): stub\n", iface
, ppIDecoderInfo
);
358 static HRESULT WINAPI
TgaDecoder_CopyPalette(IWICBitmapDecoder
*iface
,
359 IWICPalette
*pIPalette
)
361 FIXME("(%p,%p): stub\n", iface
, pIPalette
);
365 static HRESULT WINAPI
TgaDecoder_GetMetadataQueryReader(IWICBitmapDecoder
*iface
,
366 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
368 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
372 static HRESULT WINAPI
TgaDecoder_GetPreview(IWICBitmapDecoder
*iface
,
373 IWICBitmapSource
**ppIBitmapSource
)
375 FIXME("(%p,%p): stub\n", iface
, ppIBitmapSource
);
376 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
379 static HRESULT WINAPI
TgaDecoder_GetColorContexts(IWICBitmapDecoder
*iface
,
380 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
382 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
383 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
386 static HRESULT WINAPI
TgaDecoder_GetThumbnail(IWICBitmapDecoder
*iface
,
387 IWICBitmapSource
**ppIThumbnail
)
389 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
390 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
393 static HRESULT WINAPI
TgaDecoder_GetFrameCount(IWICBitmapDecoder
*iface
,
400 static HRESULT WINAPI
TgaDecoder_GetFrame(IWICBitmapDecoder
*iface
,
401 UINT index
, IWICBitmapFrameDecode
**ppIBitmapFrame
)
403 TgaDecoder
*This
= impl_from_IWICBitmapDecoder(iface
);
404 TRACE("(%p,%p)\n", iface
, ppIBitmapFrame
);
406 if (!This
->initialized
) return WINCODEC_ERR_NOTINITIALIZED
;
408 if (index
!= 0) return E_INVALIDARG
;
410 IWICBitmapDecoder_AddRef(iface
);
411 *ppIBitmapFrame
= &This
->IWICBitmapFrameDecode_iface
;
416 static const IWICBitmapDecoderVtbl TgaDecoder_Vtbl
= {
417 TgaDecoder_QueryInterface
,
420 TgaDecoder_QueryCapability
,
421 TgaDecoder_Initialize
,
422 TgaDecoder_GetContainerFormat
,
423 TgaDecoder_GetDecoderInfo
,
424 TgaDecoder_CopyPalette
,
425 TgaDecoder_GetMetadataQueryReader
,
426 TgaDecoder_GetPreview
,
427 TgaDecoder_GetColorContexts
,
428 TgaDecoder_GetThumbnail
,
429 TgaDecoder_GetFrameCount
,
433 static HRESULT WINAPI
TgaDecoder_Frame_QueryInterface(IWICBitmapFrameDecode
*iface
, REFIID iid
,
436 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
438 if (!ppv
) return E_INVALIDARG
;
440 if (IsEqualIID(&IID_IUnknown
, iid
) ||
441 IsEqualIID(&IID_IWICBitmapSource
, iid
) ||
442 IsEqualIID(&IID_IWICBitmapFrameDecode
, iid
))
449 return E_NOINTERFACE
;
452 IUnknown_AddRef((IUnknown
*)*ppv
);
456 static ULONG WINAPI
TgaDecoder_Frame_AddRef(IWICBitmapFrameDecode
*iface
)
458 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
459 return IUnknown_AddRef((IUnknown
*)This
);
462 static ULONG WINAPI
TgaDecoder_Frame_Release(IWICBitmapFrameDecode
*iface
)
464 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
465 return IUnknown_Release((IUnknown
*)This
);
468 static HRESULT WINAPI
TgaDecoder_Frame_GetSize(IWICBitmapFrameDecode
*iface
,
469 UINT
*puiWidth
, UINT
*puiHeight
)
471 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
473 *puiWidth
= This
->header
.width
;
474 *puiHeight
= This
->header
.height
;
476 TRACE("(%p)->(%u,%u)\n", iface
, *puiWidth
, *puiHeight
);
481 static HRESULT WINAPI
TgaDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode
*iface
,
482 WICPixelFormatGUID
*pPixelFormat
)
484 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
485 int attribute_bitcount
;
488 TRACE("(%p,%p)\n", iface
, pPixelFormat
);
490 attribute_bitcount
= This
->header
.image_descriptor
& IMAGE_ATTRIBUTE_BITCOUNT_MASK
;
492 if (attribute_bitcount
&& This
->extension_area_offset
)
493 attribute_type
= This
->extension_area
.attributes_type
;
494 else if (attribute_bitcount
)
495 attribute_type
= ATTRIBUTE_ALPHA
;
497 attribute_type
= ATTRIBUTE_NO_ALPHA
;
499 switch (This
->header
.image_type
& ~IMAGETYPE_RLE
)
501 case IMAGETYPE_COLORMAPPED
:
502 switch (This
->header
.depth
)
505 memcpy(pPixelFormat
, &GUID_WICPixelFormat8bppIndexed
, sizeof(GUID
));
508 FIXME("Unhandled indexed color depth %u\n", This
->header
.depth
);
512 case IMAGETYPE_TRUECOLOR
:
513 switch (This
->header
.depth
)
516 switch (attribute_type
)
518 case ATTRIBUTE_NO_ALPHA
:
519 case ATTRIBUTE_UNDEFINED
:
520 case ATTRIBUTE_UNDEFINED_PRESERVE
:
521 memcpy(pPixelFormat
, &GUID_WICPixelFormat16bppBGR555
, sizeof(GUID
));
523 case ATTRIBUTE_ALPHA
:
524 case ATTRIBUTE_PALPHA
:
525 memcpy(pPixelFormat
, &GUID_WICPixelFormat16bppBGRA5551
, sizeof(GUID
));
528 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type
);
533 memcpy(pPixelFormat
, &GUID_WICPixelFormat24bppBGR
, sizeof(GUID
));
536 switch (attribute_type
)
538 case ATTRIBUTE_NO_ALPHA
:
539 case ATTRIBUTE_UNDEFINED
:
540 case ATTRIBUTE_UNDEFINED_PRESERVE
:
541 memcpy(pPixelFormat
, &GUID_WICPixelFormat32bppBGR
, sizeof(GUID
));
543 case ATTRIBUTE_ALPHA
:
544 memcpy(pPixelFormat
, &GUID_WICPixelFormat32bppBGRA
, sizeof(GUID
));
546 case ATTRIBUTE_PALPHA
:
547 memcpy(pPixelFormat
, &GUID_WICPixelFormat32bppPBGRA
, sizeof(GUID
));
550 FIXME("Unhandled 32-bit attribute type %u\n", attribute_type
);
555 FIXME("Unhandled truecolor depth %u\n", This
->header
.depth
);
559 case IMAGETYPE_GRAYSCALE
:
560 switch (This
->header
.depth
)
563 memcpy(pPixelFormat
, &GUID_WICPixelFormat8bppGray
, sizeof(GUID
));
566 memcpy(pPixelFormat
, &GUID_WICPixelFormat16bppGray
, sizeof(GUID
));
569 FIXME("Unhandled grayscale depth %u\n", This
->header
.depth
);
574 ERR("Unknown image type %u\n", This
->header
.image_type
);
581 static HRESULT WINAPI
TgaDecoder_Frame_GetResolution(IWICBitmapFrameDecode
*iface
,
582 double *pDpiX
, double *pDpiY
)
584 FIXME("(%p,%p,%p): stub\n", iface
, pDpiX
, pDpiY
);
588 static HRESULT WINAPI
TgaDecoder_Frame_CopyPalette(IWICBitmapFrameDecode
*iface
,
589 IWICPalette
*pIPalette
)
591 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
593 WICColor colors
[256], *color
;
595 WORD
*wcolormap_data
;
596 DWORD
*dwcolormap_data
;
599 int depth
, attribute_bitcount
, attribute_type
;
602 TRACE("(%p,%p)\n", iface
, pIPalette
);
604 if (!This
->colormap_length
)
606 WARN("no colormap present in this file\n");
607 return WINCODEC_ERR_PALETTEUNAVAILABLE
;
610 if (This
->header
.colormap_firstentry
+ This
->header
.colormap_length
> 256)
612 FIXME("cannot read colormap with %i entries starting at %i\n",
613 This
->header
.colormap_firstentry
+ This
->header
.colormap_length
,
614 This
->header
.colormap_firstentry
);
618 colormap_data
= HeapAlloc(GetProcessHeap(), 0, This
->colormap_length
);
619 if (!colormap_data
) return E_OUTOFMEMORY
;
621 wcolormap_data
= (WORD
*)colormap_data
;
622 dwcolormap_data
= (DWORD
*)colormap_data
;
624 EnterCriticalSection(&This
->lock
);
626 seek
.QuadPart
= This
->colormap_offset
;
627 hr
= IStream_Seek(This
->stream
, seek
, STREAM_SEEK_SET
, NULL
);
631 hr
= IStream_Read(This
->stream
, colormap_data
, This
->colormap_length
, &bytesread
);
632 if (SUCCEEDED(hr
) && bytesread
!= This
->colormap_length
)
634 WARN("expected %i bytes in colormap, got %i\n", This
->colormap_length
, bytesread
);
639 LeaveCriticalSection(&This
->lock
);
643 attribute_bitcount
= This
->header
.image_descriptor
& IMAGE_ATTRIBUTE_BITCOUNT_MASK
;
645 if (attribute_bitcount
&& This
->extension_area_offset
)
646 attribute_type
= This
->extension_area
.attributes_type
;
647 else if (attribute_bitcount
)
648 attribute_type
= ATTRIBUTE_ALPHA
;
650 attribute_type
= ATTRIBUTE_NO_ALPHA
;
652 depth
= This
->header
.colormap_entrysize
;
656 attribute_type
= ATTRIBUTE_NO_ALPHA
;
659 memset(colors
, 0, sizeof(colors
));
661 color
= &colors
[This
->header
.colormap_firstentry
];
663 /* Colormap entries can be in any truecolor format, and we have to convert them. */
667 switch (attribute_type
)
669 case ATTRIBUTE_NO_ALPHA
:
670 case ATTRIBUTE_UNDEFINED
:
671 case ATTRIBUTE_UNDEFINED_PRESERVE
:
672 for (i
=0; i
<This
->header
.colormap_length
; i
++)
674 WORD srcval
= wcolormap_data
[i
];
675 *color
++=0xff000000 | /* constant 255 alpha */
676 ((srcval
<< 9) & 0xf80000) | /* r */
677 ((srcval
<< 4) & 0x070000) | /* r - 3 bits */
678 ((srcval
<< 6) & 0x00f800) | /* g */
679 ((srcval
<< 1) & 0x000700) | /* g - 3 bits */
680 ((srcval
<< 3) & 0x0000f8) | /* b */
681 ((srcval
>> 2) & 0x000007); /* b - 3 bits */
684 case ATTRIBUTE_ALPHA
:
685 case ATTRIBUTE_PALPHA
:
686 for (i
=0; i
<This
->header
.colormap_length
; i
++)
688 WORD srcval
= wcolormap_data
[i
];
689 *color
++=((srcval
& 0x8000) ? 0xff000000 : 0) | /* alpha */
690 ((srcval
<< 9) & 0xf80000) | /* r */
691 ((srcval
<< 4) & 0x070000) | /* r - 3 bits */
692 ((srcval
<< 6) & 0x00f800) | /* g */
693 ((srcval
<< 1) & 0x000700) | /* g - 3 bits */
694 ((srcval
<< 3) & 0x0000f8) | /* b */
695 ((srcval
>> 2) & 0x000007); /* b - 3 bits */
699 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type
);
704 for (i
=0; i
<This
->header
.colormap_length
; i
++)
706 *color
++=0xff000000 | /* alpha */
707 colormap_data
[i
*3+2] | /* red */
708 colormap_data
[i
*3+1] | /* green */
709 colormap_data
[i
*3]; /* blue */
713 switch (attribute_type
)
715 case ATTRIBUTE_NO_ALPHA
:
716 case ATTRIBUTE_UNDEFINED
:
717 case ATTRIBUTE_UNDEFINED_PRESERVE
:
718 for (i
=0; i
<This
->header
.colormap_length
; i
++)
719 *color
++=dwcolormap_data
[i
]|0xff000000;
721 case ATTRIBUTE_ALPHA
:
722 for (i
=0; i
<This
->header
.colormap_length
; i
++)
723 *color
++=dwcolormap_data
[i
];
725 case ATTRIBUTE_PALPHA
:
726 /* FIXME: Unpremultiply alpha */
728 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type
);
733 FIXME("Unhandled truecolor depth %u\n", This
->header
.depth
);
738 HeapFree(GetProcessHeap(), 0, colormap_data
);
741 hr
= IWICPalette_InitializeCustom(pIPalette
, colors
, 256);
746 static HRESULT
TgaDecoder_ReadRLE(TgaDecoder
*This
, BYTE
*imagebits
, int datasize
)
748 int i
=0, j
, bytesperpixel
;
752 bytesperpixel
= This
->header
.depth
/ 8;
760 hr
= IStream_Read(This
->stream
, &rc
, 1, &bytesread
);
761 if (bytesread
!= 1) hr
= E_FAIL
;
762 if (FAILED(hr
)) break;
765 size
= count
* bytesperpixel
;
767 if (size
+ i
> datasize
)
769 WARN("RLE packet too large\n");
776 /* Run-length packet */
777 hr
= IStream_Read(This
->stream
, pixeldata
, bytesperpixel
, &bytesread
);
778 if (bytesread
!= bytesperpixel
) hr
= E_FAIL
;
779 if (FAILED(hr
)) break;
781 if (bytesperpixel
== 1)
782 memset(&imagebits
[i
], pixeldata
[0], count
);
785 for (j
=0; j
<count
; j
++)
786 memcpy(&imagebits
[i
+j
*bytesperpixel
], pixeldata
, bytesperpixel
);
792 hr
= IStream_Read(This
->stream
, &imagebits
[i
], size
, &bytesread
);
793 if (bytesread
!= size
) hr
= E_FAIL
;
794 if (FAILED(hr
)) break;
803 static HRESULT
TgaDecoder_ReadImage(TgaDecoder
*This
)
813 EnterCriticalSection(&This
->lock
);
815 if (!This
->imagebits
)
817 if (This
->header
.image_descriptor
& IMAGE_RIGHTTOLEFT
)
819 FIXME("Right to left image reading not implemented\n");
825 datasize
= This
->header
.width
* This
->header
.height
* (This
->header
.depth
/ 8);
826 This
->imagebits
= HeapAlloc(GetProcessHeap(), 0, datasize
);
827 if (!This
->imagebits
) hr
= E_OUTOFMEMORY
;
832 seek
.QuadPart
= This
->image_offset
;
833 hr
= IStream_Seek(This
->stream
, seek
, STREAM_SEEK_SET
, NULL
);
838 if (This
->header
.image_type
& IMAGETYPE_RLE
)
840 hr
= TgaDecoder_ReadRLE(This
, This
->imagebits
, datasize
);
844 hr
= IStream_Read(This
->stream
, This
->imagebits
, datasize
, &bytesread
);
845 if (SUCCEEDED(hr
) && bytesread
!= datasize
)
852 if (This
->header
.image_descriptor
& IMAGE_TOPTOBOTTOM
)
854 This
->origin
= This
->imagebits
;
855 This
->stride
= This
->header
.width
* (This
->header
.depth
/ 8);
859 This
->stride
= -This
->header
.width
* (This
->header
.depth
/ 8);
860 This
->origin
= This
->imagebits
+ This
->header
.width
* (This
->header
.height
- 1) * (This
->header
.depth
/ 8);
865 HeapFree(GetProcessHeap(), 0, This
->imagebits
);
866 This
->imagebits
= NULL
;
870 LeaveCriticalSection(&This
->lock
);
875 static HRESULT WINAPI
TgaDecoder_Frame_CopyPixels(IWICBitmapFrameDecode
*iface
,
876 const WICRect
*prc
, UINT cbStride
, UINT cbBufferSize
, BYTE
*pbBuffer
)
878 TgaDecoder
*This
= impl_from_IWICBitmapFrameDecode(iface
);
881 TRACE("(%p,%p,%u,%u,%p)\n", iface
, prc
, cbStride
, cbBufferSize
, pbBuffer
);
883 hr
= TgaDecoder_ReadImage(This
);
887 hr
= copy_pixels(This
->header
.depth
, This
->origin
,
888 This
->header
.width
, This
->header
.height
, This
->stride
,
889 prc
, cbStride
, cbBufferSize
, pbBuffer
);
895 static HRESULT WINAPI
TgaDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode
*iface
,
896 IWICMetadataQueryReader
**ppIMetadataQueryReader
)
898 FIXME("(%p,%p): stub\n", iface
, ppIMetadataQueryReader
);
899 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
902 static HRESULT WINAPI
TgaDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode
*iface
,
903 UINT cCount
, IWICColorContext
**ppIColorContexts
, UINT
*pcActualCount
)
905 FIXME("(%p,%u,%p,%p): stub\n", iface
, cCount
, ppIColorContexts
, pcActualCount
);
906 return WINCODEC_ERR_UNSUPPORTEDOPERATION
;
909 static HRESULT WINAPI
TgaDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode
*iface
,
910 IWICBitmapSource
**ppIThumbnail
)
912 FIXME("(%p,%p): stub\n", iface
, ppIThumbnail
);
913 return WINCODEC_ERR_CODECNOTHUMBNAIL
;
916 static const IWICBitmapFrameDecodeVtbl TgaDecoder_Frame_Vtbl
= {
917 TgaDecoder_Frame_QueryInterface
,
918 TgaDecoder_Frame_AddRef
,
919 TgaDecoder_Frame_Release
,
920 TgaDecoder_Frame_GetSize
,
921 TgaDecoder_Frame_GetPixelFormat
,
922 TgaDecoder_Frame_GetResolution
,
923 TgaDecoder_Frame_CopyPalette
,
924 TgaDecoder_Frame_CopyPixels
,
925 TgaDecoder_Frame_GetMetadataQueryReader
,
926 TgaDecoder_Frame_GetColorContexts
,
927 TgaDecoder_Frame_GetThumbnail
930 HRESULT
TgaDecoder_CreateInstance(IUnknown
*pUnkOuter
, REFIID iid
, void** ppv
)
935 TRACE("(%p,%s,%p)\n", pUnkOuter
, debugstr_guid(iid
), ppv
);
939 if (pUnkOuter
) return CLASS_E_NOAGGREGATION
;
941 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(TgaDecoder
));
942 if (!This
) return E_OUTOFMEMORY
;
944 This
->IWICBitmapDecoder_iface
.lpVtbl
= &TgaDecoder_Vtbl
;
945 This
->IWICBitmapFrameDecode_iface
.lpVtbl
= &TgaDecoder_Frame_Vtbl
;
947 This
->initialized
= FALSE
;
949 This
->imagebits
= NULL
;
950 InitializeCriticalSection(&This
->lock
);
951 This
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": TgaDecoder.lock");
953 ret
= IUnknown_QueryInterface((IUnknown
*)This
, iid
, ppv
);
954 IUnknown_Release((IUnknown
*)This
);