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);
287 if (FAILED(hr)) goto end;
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);
295 if (FAILED(hr)) goto end;
297 if (memcmp(footer.magic, tga_footer_magic, sizeof(tga_footer_magic)) == 0)
299 This->extension_area_offset = footer.extension_area_offset;
300 This->developer_directory_offset = footer.developer_directory_offset;
304 This->extension_area_offset = 0;
305 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)
347 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
351 static HRESULT WINAPI TgaDecoder_CopyPalette(IWICBitmapDecoder *iface,
352 IWICPalette *pIPalette)
354 FIXME("(%p,%p): stub\n", iface, pIPalette);
358 static HRESULT WINAPI TgaDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
359 IWICMetadataQueryReader **ppIMetadataQueryReader)
361 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
365 static HRESULT WINAPI TgaDecoder_GetPreview(IWICBitmapDecoder *iface,
366 IWICBitmapSource **ppIBitmapSource)
368 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
369 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
372 static HRESULT WINAPI TgaDecoder_GetColorContexts(IWICBitmapDecoder *iface,
373 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
375 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
376 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
379 static HRESULT WINAPI TgaDecoder_GetThumbnail(IWICBitmapDecoder *iface,
380 IWICBitmapSource **ppIThumbnail)
382 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
383 return WINCODEC_ERR_CODECNOTHUMBNAIL;
386 static HRESULT WINAPI TgaDecoder_GetFrameCount(IWICBitmapDecoder *iface,
393 static HRESULT WINAPI TgaDecoder_GetFrame(IWICBitmapDecoder *iface,
394 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
396 TgaDecoder *This = impl_from_IWICBitmapDecoder(iface);
397 TRACE("(%p,%p)\n", iface, ppIBitmapFrame);
399 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
401 if (index != 0) return E_INVALIDARG;
403 IWICBitmapDecoder_AddRef(iface);
404 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
409 static const IWICBitmapDecoderVtbl TgaDecoder_Vtbl = {
410 TgaDecoder_QueryInterface,
413 TgaDecoder_QueryCapability,
414 TgaDecoder_Initialize,
415 TgaDecoder_GetContainerFormat,
416 TgaDecoder_GetDecoderInfo,
417 TgaDecoder_CopyPalette,
418 TgaDecoder_GetMetadataQueryReader,
419 TgaDecoder_GetPreview,
420 TgaDecoder_GetColorContexts,
421 TgaDecoder_GetThumbnail,
422 TgaDecoder_GetFrameCount,
426 static HRESULT WINAPI TgaDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
429 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
431 if (!ppv) return E_INVALIDARG;
433 if (IsEqualIID(&IID_IUnknown, iid) ||
434 IsEqualIID(&IID_IWICBitmapSource, iid) ||
435 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
442 return E_NOINTERFACE;
445 IUnknown_AddRef((IUnknown*)*ppv);
449 static ULONG WINAPI TgaDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
451 TgaDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
452 return IUnknown_AddRef((IUnknown*)This);
455 static ULONG WINAPI TgaDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
457 TgaDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
458 return IUnknown_Release((IUnknown*)This);
461 static HRESULT WINAPI TgaDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
462 UINT *puiWidth, UINT *puiHeight)
464 TgaDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
466 *puiWidth = This->header.width;
467 *puiHeight = This->header.height;
469 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
474 static HRESULT WINAPI TgaDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
475 WICPixelFormatGUID *pPixelFormat)
477 TgaDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
478 int attribute_bitcount;
481 TRACE("(%p,%p)\n", iface, pPixelFormat);
483 attribute_bitcount = This->header.image_descriptor & IMAGE_ATTRIBUTE_BITCOUNT_MASK;
485 if (attribute_bitcount && This->extension_area_offset)
486 attribute_type = This->extension_area.attributes_type;
487 else if (attribute_bitcount)
488 attribute_type = ATTRIBUTE_ALPHA;
490 attribute_type = ATTRIBUTE_NO_ALPHA;
492 switch (This->header.image_type & ~IMAGETYPE_RLE)
494 case IMAGETYPE_COLORMAPPED:
495 switch (This->header.depth)
498 memcpy(pPixelFormat, &GUID_WICPixelFormat8bppIndexed, sizeof(GUID));
501 FIXME("Unhandled indexed color depth %u\n", This->header.depth);
505 case IMAGETYPE_TRUECOLOR:
506 switch (This->header.depth)
509 switch (attribute_type)
511 case ATTRIBUTE_NO_ALPHA:
512 case ATTRIBUTE_UNDEFINED:
513 case ATTRIBUTE_UNDEFINED_PRESERVE:
514 memcpy(pPixelFormat, &GUID_WICPixelFormat16bppBGR555, sizeof(GUID));
516 case ATTRIBUTE_ALPHA:
517 case ATTRIBUTE_PALPHA:
518 memcpy(pPixelFormat, &GUID_WICPixelFormat16bppBGRA5551, sizeof(GUID));
521 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type);
526 memcpy(pPixelFormat, &GUID_WICPixelFormat24bppBGR, sizeof(GUID));
529 switch (attribute_type)
531 case ATTRIBUTE_NO_ALPHA:
532 case ATTRIBUTE_UNDEFINED:
533 case ATTRIBUTE_UNDEFINED_PRESERVE:
534 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppBGR, sizeof(GUID));
536 case ATTRIBUTE_ALPHA:
537 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
539 case ATTRIBUTE_PALPHA:
540 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppPBGRA, sizeof(GUID));
543 FIXME("Unhandled 32-bit attribute type %u\n", attribute_type);
548 FIXME("Unhandled truecolor depth %u\n", This->header.depth);
552 case IMAGETYPE_GRAYSCALE:
553 switch (This->header.depth)
556 memcpy(pPixelFormat, &GUID_WICPixelFormat8bppGray, sizeof(GUID));
559 memcpy(pPixelFormat, &GUID_WICPixelFormat16bppGray, sizeof(GUID));
562 FIXME("Unhandled grayscale depth %u\n", This->header.depth);
567 ERR("Unknown image type %u\n", This->header.image_type);
574 static HRESULT WINAPI TgaDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
575 double *pDpiX, double *pDpiY)
577 FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
581 static HRESULT WINAPI TgaDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
582 IWICPalette *pIPalette)
584 TgaDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
586 WICColor colors[256], *color;
588 WORD *wcolormap_data;
589 DWORD *dwcolormap_data;
592 int depth, attribute_bitcount, attribute_type;
595 TRACE("(%p,%p)\n", iface, pIPalette);
597 if (!This->colormap_length)
599 WARN("no colormap present in this file\n");
600 return WINCODEC_ERR_PALETTEUNAVAILABLE;
603 if (This->header.colormap_firstentry + This->header.colormap_length > 256)
605 FIXME("cannot read colormap with %i entries starting at %i\n",
606 This->header.colormap_firstentry + This->header.colormap_length,
607 This->header.colormap_firstentry);
611 colormap_data = HeapAlloc(GetProcessHeap(), 0, This->colormap_length);
612 if (!colormap_data) return E_OUTOFMEMORY;
614 wcolormap_data = (WORD*)colormap_data;
615 dwcolormap_data = (DWORD*)colormap_data;
617 EnterCriticalSection(&This->lock);
619 seek.QuadPart = This->colormap_offset;
620 hr = IStream_Seek(This->stream, seek, STREAM_SEEK_SET, NULL);
624 hr = IStream_Read(This->stream, colormap_data, This->colormap_length, &bytesread);
625 if (SUCCEEDED(hr) && bytesread != This->colormap_length)
627 WARN("expected %i bytes in colormap, got %i\n", This->colormap_length, bytesread);
632 LeaveCriticalSection(&This->lock);
636 attribute_bitcount = This->header.image_descriptor & IMAGE_ATTRIBUTE_BITCOUNT_MASK;
638 if (attribute_bitcount && This->extension_area_offset)
639 attribute_type = This->extension_area.attributes_type;
640 else if (attribute_bitcount)
641 attribute_type = ATTRIBUTE_ALPHA;
643 attribute_type = ATTRIBUTE_NO_ALPHA;
645 depth = This->header.colormap_entrysize;
649 attribute_type = ATTRIBUTE_NO_ALPHA;
652 memset(colors, 0, sizeof(colors));
654 color = &colors[This->header.colormap_firstentry];
656 /* Colormap entries can be in any truecolor format, and we have to convert them. */
660 switch (attribute_type)
662 case ATTRIBUTE_NO_ALPHA:
663 case ATTRIBUTE_UNDEFINED:
664 case ATTRIBUTE_UNDEFINED_PRESERVE:
665 for (i=0; i<This->header.colormap_length; i++)
667 WORD srcval = wcolormap_data[i];
668 *color++=0xff000000 | /* constant 255 alpha */
669 ((srcval << 9) & 0xf80000) | /* r */
670 ((srcval << 4) & 0x070000) | /* r - 3 bits */
671 ((srcval << 6) & 0x00f800) | /* g */
672 ((srcval << 1) & 0x000700) | /* g - 3 bits */
673 ((srcval << 3) & 0x0000f8) | /* b */
674 ((srcval >> 2) & 0x000007); /* b - 3 bits */
677 case ATTRIBUTE_ALPHA:
678 case ATTRIBUTE_PALPHA:
679 for (i=0; i<This->header.colormap_length; i++)
681 WORD srcval = wcolormap_data[i];
682 *color++=((srcval & 0x8000) ? 0xff000000 : 0) | /* 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 */
692 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type);
697 for (i=0; i<This->header.colormap_length; i++)
699 *color++=0xff000000 | /* alpha */
700 colormap_data[i*3+2] | /* red */
701 colormap_data[i*3+1] | /* green */
702 colormap_data[i*3]; /* blue */
706 switch (attribute_type)
708 case ATTRIBUTE_NO_ALPHA:
709 case ATTRIBUTE_UNDEFINED:
710 case ATTRIBUTE_UNDEFINED_PRESERVE:
711 for (i=0; i<This->header.colormap_length; i++)
712 *color++=dwcolormap_data[i]|0xff000000;
714 case ATTRIBUTE_ALPHA:
715 for (i=0; i<This->header.colormap_length; i++)
716 *color++=dwcolormap_data[i];
718 case ATTRIBUTE_PALPHA:
719 /* FIXME: Unpremultiply alpha */
721 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type);
726 FIXME("Unhandled truecolor depth %u\n", This->header.depth);
731 HeapFree(GetProcessHeap(), 0, colormap_data);
734 hr = IWICPalette_InitializeCustom(pIPalette, colors, 256);
739 static HRESULT TgaDecoder_ReadRLE(TgaDecoder *This, BYTE *imagebits, int datasize)
741 int i=0, j, bytesperpixel;
745 bytesperpixel = This->header.depth / 8;
753 hr = IStream_Read(This->stream, &rc, 1, &bytesread);
754 if (bytesread != 1) hr = E_FAIL;
755 if (FAILED(hr)) break;
758 size = count * bytesperpixel;
760 if (size + i > datasize)
762 WARN("RLE packet too large\n");
769 /* Run-length packet */
770 hr = IStream_Read(This->stream, pixeldata, bytesperpixel, &bytesread);
771 if (bytesread != bytesperpixel) hr = E_FAIL;
772 if (FAILED(hr)) break;
774 if (bytesperpixel == 1)
775 memset(&imagebits[i], pixeldata[0], count);
778 for (j=0; j<count; j++)
779 memcpy(&imagebits[i+j*bytesperpixel], pixeldata, bytesperpixel);
785 hr = IStream_Read(This->stream, &imagebits[i], size, &bytesread);
786 if (bytesread != size) hr = E_FAIL;
787 if (FAILED(hr)) break;
796 static HRESULT TgaDecoder_ReadImage(TgaDecoder *This)
806 EnterCriticalSection(&This->lock);
808 if (!This->imagebits)
810 if (This->header.image_descriptor & IMAGE_RIGHTTOLEFT)
812 FIXME("Right to left image reading not implemented\n");
818 datasize = This->header.width * This->header.height * (This->header.depth / 8);
819 This->imagebits = HeapAlloc(GetProcessHeap(), 0, datasize);
820 if (!This->imagebits) hr = E_OUTOFMEMORY;
825 seek.QuadPart = This->image_offset;
826 hr = IStream_Seek(This->stream, seek, STREAM_SEEK_SET, NULL);
831 if (This->header.image_type & IMAGETYPE_RLE)
833 hr = TgaDecoder_ReadRLE(This, This->imagebits, datasize);
837 hr = IStream_Read(This->stream, This->imagebits, datasize, &bytesread);
838 if (SUCCEEDED(hr) && bytesread != datasize)
845 if (This->header.image_descriptor & IMAGE_TOPTOBOTTOM)
847 This->origin = This->imagebits;
848 This->stride = This->header.width * (This->header.depth / 8);
852 This->stride = -This->header.width * (This->header.depth / 8);
853 This->origin = This->imagebits + This->header.width * (This->header.height - 1) * (This->header.depth / 8);
858 HeapFree(GetProcessHeap(), 0, This->imagebits);
859 This->imagebits = NULL;
863 LeaveCriticalSection(&This->lock);
868 static HRESULT WINAPI TgaDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
869 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
871 TgaDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
874 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
876 hr = TgaDecoder_ReadImage(This);
880 hr = copy_pixels(This->header.depth, This->origin,
881 This->header.width, This->header.height, This->stride,
882 prc, cbStride, cbBufferSize, pbBuffer);
888 static HRESULT WINAPI TgaDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
889 IWICMetadataQueryReader **ppIMetadataQueryReader)
891 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
892 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
895 static HRESULT WINAPI TgaDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
896 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
898 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
899 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
902 static HRESULT WINAPI TgaDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
903 IWICBitmapSource **ppIThumbnail)
905 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
906 return WINCODEC_ERR_CODECNOTHUMBNAIL;
909 static const IWICBitmapFrameDecodeVtbl TgaDecoder_Frame_Vtbl = {
910 TgaDecoder_Frame_QueryInterface,
911 TgaDecoder_Frame_AddRef,
912 TgaDecoder_Frame_Release,
913 TgaDecoder_Frame_GetSize,
914 TgaDecoder_Frame_GetPixelFormat,
915 TgaDecoder_Frame_GetResolution,
916 TgaDecoder_Frame_CopyPalette,
917 TgaDecoder_Frame_CopyPixels,
918 TgaDecoder_Frame_GetMetadataQueryReader,
919 TgaDecoder_Frame_GetColorContexts,
920 TgaDecoder_Frame_GetThumbnail
923 HRESULT TgaDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
928 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
932 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
934 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TgaDecoder));
935 if (!This) return E_OUTOFMEMORY;
937 This->IWICBitmapDecoder_iface.lpVtbl = &TgaDecoder_Vtbl;
938 This->IWICBitmapFrameDecode_iface.lpVtbl = &TgaDecoder_Frame_Vtbl;
940 This->initialized = FALSE;
942 This->imagebits = NULL;
943 InitializeCriticalSection(&This->lock);
944 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TgaDecoder.lock");
946 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
947 IUnknown_Release((IUnknown*)This);