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)
355 IWICComponentInfo *compinfo;
357 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
359 hr = CreateComponentInfo(&CLSID_WineTgaDecoder, &compinfo);
360 if (FAILED(hr)) return hr;
362 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
363 (void**)ppIDecoderInfo);
365 IWICComponentInfo_Release(compinfo);
370 static HRESULT WINAPI TgaDecoder_CopyPalette(IWICBitmapDecoder *iface,
371 IWICPalette *pIPalette)
373 FIXME("(%p,%p): stub\n", iface, pIPalette);
377 static HRESULT WINAPI TgaDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
378 IWICMetadataQueryReader **ppIMetadataQueryReader)
380 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
384 static HRESULT WINAPI TgaDecoder_GetPreview(IWICBitmapDecoder *iface,
385 IWICBitmapSource **ppIBitmapSource)
387 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
388 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
391 static HRESULT WINAPI TgaDecoder_GetColorContexts(IWICBitmapDecoder *iface,
392 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
394 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
395 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
398 static HRESULT WINAPI TgaDecoder_GetThumbnail(IWICBitmapDecoder *iface,
399 IWICBitmapSource **ppIThumbnail)
401 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
402 return WINCODEC_ERR_CODECNOTHUMBNAIL;
405 static HRESULT WINAPI TgaDecoder_GetFrameCount(IWICBitmapDecoder *iface,
412 static HRESULT WINAPI TgaDecoder_GetFrame(IWICBitmapDecoder *iface,
413 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
415 TgaDecoder *This = impl_from_IWICBitmapDecoder(iface);
416 TRACE("(%p,%p)\n", iface, ppIBitmapFrame);
418 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
420 if (index != 0) return E_INVALIDARG;
422 IWICBitmapDecoder_AddRef(iface);
423 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
428 static const IWICBitmapDecoderVtbl TgaDecoder_Vtbl = {
429 TgaDecoder_QueryInterface,
432 TgaDecoder_QueryCapability,
433 TgaDecoder_Initialize,
434 TgaDecoder_GetContainerFormat,
435 TgaDecoder_GetDecoderInfo,
436 TgaDecoder_CopyPalette,
437 TgaDecoder_GetMetadataQueryReader,
438 TgaDecoder_GetPreview,
439 TgaDecoder_GetColorContexts,
440 TgaDecoder_GetThumbnail,
441 TgaDecoder_GetFrameCount,
445 static HRESULT WINAPI TgaDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
448 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
450 if (!ppv) return E_INVALIDARG;
452 if (IsEqualIID(&IID_IUnknown, iid) ||
453 IsEqualIID(&IID_IWICBitmapSource, iid) ||
454 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
461 return E_NOINTERFACE;
464 IUnknown_AddRef((IUnknown*)*ppv);
468 static ULONG WINAPI TgaDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
470 TgaDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
471 return IUnknown_AddRef((IUnknown*)This);
474 static ULONG WINAPI TgaDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
476 TgaDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
477 return IUnknown_Release((IUnknown*)This);
480 static HRESULT WINAPI TgaDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
481 UINT *puiWidth, UINT *puiHeight)
483 TgaDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
485 *puiWidth = This->header.width;
486 *puiHeight = This->header.height;
488 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
493 static HRESULT WINAPI TgaDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
494 WICPixelFormatGUID *pPixelFormat)
496 TgaDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
497 int attribute_bitcount;
500 TRACE("(%p,%p)\n", iface, pPixelFormat);
502 attribute_bitcount = This->header.image_descriptor & IMAGE_ATTRIBUTE_BITCOUNT_MASK;
504 if (attribute_bitcount && This->extension_area_offset)
505 attribute_type = This->extension_area.attributes_type;
506 else if (attribute_bitcount)
507 attribute_type = ATTRIBUTE_ALPHA;
509 attribute_type = ATTRIBUTE_NO_ALPHA;
511 switch (This->header.image_type & ~IMAGETYPE_RLE)
513 case IMAGETYPE_COLORMAPPED:
514 switch (This->header.depth)
517 memcpy(pPixelFormat, &GUID_WICPixelFormat8bppIndexed, sizeof(GUID));
520 FIXME("Unhandled indexed color depth %u\n", This->header.depth);
524 case IMAGETYPE_TRUECOLOR:
525 switch (This->header.depth)
528 switch (attribute_type)
530 case ATTRIBUTE_NO_ALPHA:
531 case ATTRIBUTE_UNDEFINED:
532 case ATTRIBUTE_UNDEFINED_PRESERVE:
533 memcpy(pPixelFormat, &GUID_WICPixelFormat16bppBGR555, sizeof(GUID));
535 case ATTRIBUTE_ALPHA:
536 case ATTRIBUTE_PALPHA:
537 memcpy(pPixelFormat, &GUID_WICPixelFormat16bppBGRA5551, sizeof(GUID));
540 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type);
545 memcpy(pPixelFormat, &GUID_WICPixelFormat24bppBGR, sizeof(GUID));
548 switch (attribute_type)
550 case ATTRIBUTE_NO_ALPHA:
551 case ATTRIBUTE_UNDEFINED:
552 case ATTRIBUTE_UNDEFINED_PRESERVE:
553 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppBGR, sizeof(GUID));
555 case ATTRIBUTE_ALPHA:
556 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
558 case ATTRIBUTE_PALPHA:
559 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppPBGRA, sizeof(GUID));
562 FIXME("Unhandled 32-bit attribute type %u\n", attribute_type);
567 FIXME("Unhandled truecolor depth %u\n", This->header.depth);
571 case IMAGETYPE_GRAYSCALE:
572 switch (This->header.depth)
575 memcpy(pPixelFormat, &GUID_WICPixelFormat8bppGray, sizeof(GUID));
578 memcpy(pPixelFormat, &GUID_WICPixelFormat16bppGray, sizeof(GUID));
581 FIXME("Unhandled grayscale depth %u\n", This->header.depth);
586 ERR("Unknown image type %u\n", This->header.image_type);
593 static HRESULT WINAPI TgaDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
594 double *pDpiX, double *pDpiY)
596 FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
600 static HRESULT WINAPI TgaDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
601 IWICPalette *pIPalette)
603 TgaDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
605 WICColor colors[256], *color;
607 WORD *wcolormap_data;
608 DWORD *dwcolormap_data;
611 int depth, attribute_bitcount, attribute_type;
614 TRACE("(%p,%p)\n", iface, pIPalette);
616 if (!This->colormap_length)
618 WARN("no colormap present in this file\n");
619 return WINCODEC_ERR_PALETTEUNAVAILABLE;
622 if (This->header.colormap_firstentry + This->header.colormap_length > 256)
624 FIXME("cannot read colormap with %i entries starting at %i\n",
625 This->header.colormap_firstentry + This->header.colormap_length,
626 This->header.colormap_firstentry);
630 colormap_data = HeapAlloc(GetProcessHeap(), 0, This->colormap_length);
631 if (!colormap_data) return E_OUTOFMEMORY;
633 wcolormap_data = (WORD*)colormap_data;
634 dwcolormap_data = (DWORD*)colormap_data;
636 EnterCriticalSection(&This->lock);
638 seek.QuadPart = This->colormap_offset;
639 hr = IStream_Seek(This->stream, seek, STREAM_SEEK_SET, NULL);
643 hr = IStream_Read(This->stream, colormap_data, This->colormap_length, &bytesread);
644 if (SUCCEEDED(hr) && bytesread != This->colormap_length)
646 WARN("expected %i bytes in colormap, got %i\n", This->colormap_length, bytesread);
651 LeaveCriticalSection(&This->lock);
655 attribute_bitcount = This->header.image_descriptor & IMAGE_ATTRIBUTE_BITCOUNT_MASK;
657 if (attribute_bitcount && This->extension_area_offset)
658 attribute_type = This->extension_area.attributes_type;
659 else if (attribute_bitcount)
660 attribute_type = ATTRIBUTE_ALPHA;
662 attribute_type = ATTRIBUTE_NO_ALPHA;
664 depth = This->header.colormap_entrysize;
668 attribute_type = ATTRIBUTE_NO_ALPHA;
671 memset(colors, 0, sizeof(colors));
673 color = &colors[This->header.colormap_firstentry];
675 /* Colormap entries can be in any truecolor format, and we have to convert them. */
679 switch (attribute_type)
681 case ATTRIBUTE_NO_ALPHA:
682 case ATTRIBUTE_UNDEFINED:
683 case ATTRIBUTE_UNDEFINED_PRESERVE:
684 for (i=0; i<This->header.colormap_length; i++)
686 WORD srcval = wcolormap_data[i];
687 *color++=0xff000000 | /* constant 255 alpha */
688 ((srcval << 9) & 0xf80000) | /* r */
689 ((srcval << 4) & 0x070000) | /* r - 3 bits */
690 ((srcval << 6) & 0x00f800) | /* g */
691 ((srcval << 1) & 0x000700) | /* g - 3 bits */
692 ((srcval << 3) & 0x0000f8) | /* b */
693 ((srcval >> 2) & 0x000007); /* b - 3 bits */
696 case ATTRIBUTE_ALPHA:
697 case ATTRIBUTE_PALPHA:
698 for (i=0; i<This->header.colormap_length; i++)
700 WORD srcval = wcolormap_data[i];
701 *color++=((srcval & 0x8000) ? 0xff000000 : 0) | /* alpha */
702 ((srcval << 9) & 0xf80000) | /* r */
703 ((srcval << 4) & 0x070000) | /* r - 3 bits */
704 ((srcval << 6) & 0x00f800) | /* g */
705 ((srcval << 1) & 0x000700) | /* g - 3 bits */
706 ((srcval << 3) & 0x0000f8) | /* b */
707 ((srcval >> 2) & 0x000007); /* b - 3 bits */
711 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type);
716 for (i=0; i<This->header.colormap_length; i++)
718 *color++=0xff000000 | /* alpha */
719 colormap_data[i*3+2] | /* red */
720 colormap_data[i*3+1] | /* green */
721 colormap_data[i*3]; /* blue */
725 switch (attribute_type)
727 case ATTRIBUTE_NO_ALPHA:
728 case ATTRIBUTE_UNDEFINED:
729 case ATTRIBUTE_UNDEFINED_PRESERVE:
730 for (i=0; i<This->header.colormap_length; i++)
731 *color++=dwcolormap_data[i]|0xff000000;
733 case ATTRIBUTE_ALPHA:
734 for (i=0; i<This->header.colormap_length; i++)
735 *color++=dwcolormap_data[i];
737 case ATTRIBUTE_PALPHA:
738 /* FIXME: Unpremultiply alpha */
740 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type);
745 FIXME("Unhandled truecolor depth %u\n", This->header.depth);
750 HeapFree(GetProcessHeap(), 0, colormap_data);
753 hr = IWICPalette_InitializeCustom(pIPalette, colors, 256);
758 static HRESULT TgaDecoder_ReadRLE(TgaDecoder *This, BYTE *imagebits, int datasize)
760 int i=0, j, bytesperpixel;
764 bytesperpixel = This->header.depth / 8;
772 hr = IStream_Read(This->stream, &rc, 1, &bytesread);
773 if (bytesread != 1) hr = E_FAIL;
774 if (FAILED(hr)) break;
777 size = count * bytesperpixel;
779 if (size + i > datasize)
781 WARN("RLE packet too large\n");
788 /* Run-length packet */
789 hr = IStream_Read(This->stream, pixeldata, bytesperpixel, &bytesread);
790 if (bytesread != bytesperpixel) hr = E_FAIL;
791 if (FAILED(hr)) break;
793 if (bytesperpixel == 1)
794 memset(&imagebits[i], pixeldata[0], count);
797 for (j=0; j<count; j++)
798 memcpy(&imagebits[i+j*bytesperpixel], pixeldata, bytesperpixel);
804 hr = IStream_Read(This->stream, &imagebits[i], size, &bytesread);
805 if (bytesread != size) hr = E_FAIL;
806 if (FAILED(hr)) break;
815 static HRESULT TgaDecoder_ReadImage(TgaDecoder *This)
825 EnterCriticalSection(&This->lock);
827 if (!This->imagebits)
829 if (This->header.image_descriptor & IMAGE_RIGHTTOLEFT)
831 FIXME("Right to left image reading not implemented\n");
837 datasize = This->header.width * This->header.height * (This->header.depth / 8);
838 This->imagebits = HeapAlloc(GetProcessHeap(), 0, datasize);
839 if (!This->imagebits) hr = E_OUTOFMEMORY;
844 seek.QuadPart = This->image_offset;
845 hr = IStream_Seek(This->stream, seek, STREAM_SEEK_SET, NULL);
850 if (This->header.image_type & IMAGETYPE_RLE)
852 hr = TgaDecoder_ReadRLE(This, This->imagebits, datasize);
856 hr = IStream_Read(This->stream, This->imagebits, datasize, &bytesread);
857 if (SUCCEEDED(hr) && bytesread != datasize)
864 if (This->header.image_descriptor & IMAGE_TOPTOBOTTOM)
866 This->origin = This->imagebits;
867 This->stride = This->header.width * (This->header.depth / 8);
871 This->stride = -This->header.width * (This->header.depth / 8);
872 This->origin = This->imagebits + This->header.width * (This->header.height - 1) * (This->header.depth / 8);
877 HeapFree(GetProcessHeap(), 0, This->imagebits);
878 This->imagebits = NULL;
882 LeaveCriticalSection(&This->lock);
887 static HRESULT WINAPI TgaDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
888 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
890 TgaDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
893 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
895 hr = TgaDecoder_ReadImage(This);
899 hr = copy_pixels(This->header.depth, This->origin,
900 This->header.width, This->header.height, This->stride,
901 prc, cbStride, cbBufferSize, pbBuffer);
907 static HRESULT WINAPI TgaDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
908 IWICMetadataQueryReader **ppIMetadataQueryReader)
910 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
911 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
914 static HRESULT WINAPI TgaDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
915 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
917 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
918 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
921 static HRESULT WINAPI TgaDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
922 IWICBitmapSource **ppIThumbnail)
924 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
925 return WINCODEC_ERR_CODECNOTHUMBNAIL;
928 static const IWICBitmapFrameDecodeVtbl TgaDecoder_Frame_Vtbl = {
929 TgaDecoder_Frame_QueryInterface,
930 TgaDecoder_Frame_AddRef,
931 TgaDecoder_Frame_Release,
932 TgaDecoder_Frame_GetSize,
933 TgaDecoder_Frame_GetPixelFormat,
934 TgaDecoder_Frame_GetResolution,
935 TgaDecoder_Frame_CopyPalette,
936 TgaDecoder_Frame_CopyPixels,
937 TgaDecoder_Frame_GetMetadataQueryReader,
938 TgaDecoder_Frame_GetColorContexts,
939 TgaDecoder_Frame_GetThumbnail
942 HRESULT TgaDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
947 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
951 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
953 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TgaDecoder));
954 if (!This) return E_OUTOFMEMORY;
956 This->IWICBitmapDecoder_iface.lpVtbl = &TgaDecoder_Vtbl;
957 This->IWICBitmapFrameDecode_iface.lpVtbl = &TgaDecoder_Frame_Vtbl;
959 This->initialized = FALSE;
961 This->imagebits = NULL;
962 InitializeCriticalSection(&This->lock);
963 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TgaDecoder.lock");
965 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
966 IUnknown_Release((IUnknown*)This);