user32: Merge create_icon_bitmaps with CURSORICON_CreateIconFromBMI.
[wine/multimedia.git] / dlls / windowscodecs / tgaformat.c
blob8db1cf173119b17964bf76e13740651495e6a743
1 /*
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 "config.h"
20 #include "wine/port.h"
22 #include <stdarg.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "objbase.h"
29 #include "wincodec.h"
31 #include "wincodecs_private.h"
33 #include "wine/debug.h"
34 #include "wine/library.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
38 #include "pshpack1.h"
40 typedef struct {
41 BYTE id_length;
42 BYTE colormap_type;
43 BYTE image_type;
44 /* Colormap Specification */
45 WORD colormap_firstentry;
46 WORD colormap_length;
47 BYTE colormap_entrysize;
48 /* Image Specification */
49 WORD xorigin;
50 WORD yorigin;
51 WORD width;
52 WORD height;
53 BYTE depth;
54 BYTE image_descriptor;
55 } tga_header;
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
66 typedef struct {
67 DWORD extension_area_offset;
68 DWORD developer_directory_offset;
69 char magic[18];
70 } tga_footer;
72 static const BYTE tga_footer_magic[18] = "TRUEVISION-XFILE.";
74 typedef struct {
75 WORD size;
76 char author_name[41];
77 char author_comments[324];
78 WORD timestamp[6];
79 char job_name[41];
80 WORD job_timestamp[6];
81 char software_id[41];
82 WORD software_version;
83 char software_version_letter;
84 DWORD key_color;
85 WORD pixel_width;
86 WORD pixel_height;
87 WORD gamma_numerator;
88 WORD gamma_denominator;
89 DWORD color_correction_offset;
90 DWORD thumbnail_offset;
91 DWORD scanline_offset;
92 BYTE attributes_type;
93 } tga_extension_area;
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
101 #include "poppack.h"
103 typedef struct {
104 IWICBitmapDecoder IWICBitmapDecoder_iface;
105 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
106 LONG ref;
107 BOOL initialized;
108 IStream *stream;
109 tga_header header;
110 tga_extension_area extension_area;
111 BYTE *imagebits;
112 BYTE *origin;
113 int stride;
114 ULONG id_offset;
115 ULONG colormap_length;
116 ULONG colormap_offset;
117 ULONG image_offset;
118 ULONG extension_area_offset;
119 ULONG developer_directory_offset;
120 CRITICAL_SECTION lock;
121 } TgaDecoder;
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,
134 void **ppv)
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;
145 else
147 *ppv = NULL;
148 return E_NOINTERFACE;
151 IUnknown_AddRef((IUnknown*)*ppv);
152 return S_OK;
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);
162 return 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);
172 if (ref == 0)
174 This->lock.DebugInfo->Spare[0] = 0;
175 DeleteCriticalSection(&This->lock);
176 if (This->stream)
177 IStream_Release(This->stream);
178 HeapFree(GetProcessHeap(), 0, This->imagebits);
179 HeapFree(GetProcessHeap(), 0, This);
182 return ref;
185 static HRESULT WINAPI TgaDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
186 DWORD *pdwCapability)
188 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
189 return E_NOTIMPL;
192 static HRESULT WINAPI TgaDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
193 WICDecodeOptions cacheOptions)
195 TgaDecoder *This = impl_from_IWICBitmapDecoder(iface);
196 HRESULT hr=S_OK;
197 DWORD bytesread;
198 LARGE_INTEGER seek;
199 tga_footer footer;
200 int attribute_bitcount;
201 int mapped_depth=0;
203 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOptions);
205 EnterCriticalSection(&This->lock);
207 if (This->initialized)
209 hr = WINCODEC_ERR_WRONGSTATE;
210 goto end;
213 seek.QuadPart = 0;
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);
221 hr = E_FAIL;
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)
236 hr = E_FAIL;
237 mapped_depth = This->header.colormap_entrysize;
238 break;
239 case IMAGETYPE_TRUECOLOR:
240 case IMAGETYPE_TRUECOLOR|IMAGETYPE_RLE:
241 if (This->header.colormap_type != 0 && This->header.colormap_type != 1)
242 hr = E_FAIL;
243 mapped_depth = This->header.depth;
244 break;
245 case IMAGETYPE_GRAYSCALE:
246 case IMAGETYPE_GRAYSCALE|IMAGETYPE_RLE:
247 if (This->header.colormap_type != 0)
248 hr = E_FAIL;
249 mapped_depth = 0;
250 break;
251 default:
252 hr = E_FAIL;
255 if (This->header.depth != 8 && This->header.depth != 16 &&
256 This->header.depth != 24 && This->header.depth != 32)
257 hr = E_FAIL;
259 if ((This->header.image_descriptor & 0xc0) != 0)
260 hr = E_FAIL;
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)))
267 hr = E_FAIL;
269 if (FAILED(hr))
271 WARN("bad tga header\n");
272 goto end;
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;
280 else
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);
288 if (SUCCEEDED(hr)) {
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);
293 hr = E_FAIL;
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;
301 else
303 This->extension_area_offset = 0;
304 This->developer_directory_offset = 0;
307 else
309 /* File is too small to have a footer. */
310 This->extension_area_offset = 0;
311 This->developer_directory_offset = 0;
312 hr = S_OK;
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);
325 hr = E_FAIL;
327 if (SUCCEEDED(hr) && This->extension_area.size < 495)
329 TRACE("extension area is only %u bytes long\n", This->extension_area.size);
330 hr = E_FAIL;
332 if (FAILED(hr)) goto end;
335 IStream_AddRef(pIStream);
336 This->stream = pIStream;
337 This->initialized = TRUE;
339 end:
340 LeaveCriticalSection(&This->lock);
341 return hr;
344 static HRESULT WINAPI TgaDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
345 GUID *pguidContainerFormat)
347 memcpy(pguidContainerFormat, &GUID_WineContainerFormatTga, sizeof(GUID));
348 return S_OK;
351 static HRESULT WINAPI TgaDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
352 IWICBitmapDecoderInfo **ppIDecoderInfo)
354 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
355 return E_NOTIMPL;
358 static HRESULT WINAPI TgaDecoder_CopyPalette(IWICBitmapDecoder *iface,
359 IWICPalette *pIPalette)
361 FIXME("(%p,%p): stub\n", iface, pIPalette);
362 return E_NOTIMPL;
365 static HRESULT WINAPI TgaDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
366 IWICMetadataQueryReader **ppIMetadataQueryReader)
368 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
369 return E_NOTIMPL;
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,
394 UINT *pCount)
396 *pCount = 1;
397 return S_OK;
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;
413 return S_OK;
416 static const IWICBitmapDecoderVtbl TgaDecoder_Vtbl = {
417 TgaDecoder_QueryInterface,
418 TgaDecoder_AddRef,
419 TgaDecoder_Release,
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,
430 TgaDecoder_GetFrame
433 static HRESULT WINAPI TgaDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
434 void **ppv)
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))
444 *ppv = iface;
446 else
448 *ppv = NULL;
449 return E_NOINTERFACE;
452 IUnknown_AddRef((IUnknown*)*ppv);
453 return S_OK;
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);
478 return S_OK;
481 static HRESULT WINAPI TgaDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
482 WICPixelFormatGUID *pPixelFormat)
484 TgaDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
485 int attribute_bitcount;
486 byte attribute_type;
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;
496 else
497 attribute_type = ATTRIBUTE_NO_ALPHA;
499 switch (This->header.image_type & ~IMAGETYPE_RLE)
501 case IMAGETYPE_COLORMAPPED:
502 switch (This->header.depth)
504 case 8:
505 memcpy(pPixelFormat, &GUID_WICPixelFormat8bppIndexed, sizeof(GUID));
506 break;
507 default:
508 FIXME("Unhandled indexed color depth %u\n", This->header.depth);
509 return E_NOTIMPL;
511 break;
512 case IMAGETYPE_TRUECOLOR:
513 switch (This->header.depth)
515 case 16:
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));
522 break;
523 case ATTRIBUTE_ALPHA:
524 case ATTRIBUTE_PALPHA:
525 memcpy(pPixelFormat, &GUID_WICPixelFormat16bppBGRA5551, sizeof(GUID));
526 break;
527 default:
528 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type);
529 return E_NOTIMPL;
531 break;
532 case 24:
533 memcpy(pPixelFormat, &GUID_WICPixelFormat24bppBGR, sizeof(GUID));
534 break;
535 case 32:
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));
542 break;
543 case ATTRIBUTE_ALPHA:
544 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
545 break;
546 case ATTRIBUTE_PALPHA:
547 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppPBGRA, sizeof(GUID));
548 break;
549 default:
550 FIXME("Unhandled 32-bit attribute type %u\n", attribute_type);
551 return E_NOTIMPL;
553 break;
554 default:
555 FIXME("Unhandled truecolor depth %u\n", This->header.depth);
556 return E_NOTIMPL;
558 break;
559 case IMAGETYPE_GRAYSCALE:
560 switch (This->header.depth)
562 case 8:
563 memcpy(pPixelFormat, &GUID_WICPixelFormat8bppGray, sizeof(GUID));
564 break;
565 case 16:
566 memcpy(pPixelFormat, &GUID_WICPixelFormat16bppGray, sizeof(GUID));
567 break;
568 default:
569 FIXME("Unhandled grayscale depth %u\n", This->header.depth);
570 return E_NOTIMPL;
572 break;
573 default:
574 ERR("Unknown image type %u\n", This->header.image_type);
575 return E_FAIL;
578 return S_OK;
581 static HRESULT WINAPI TgaDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
582 double *pDpiX, double *pDpiY)
584 FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
585 return E_NOTIMPL;
588 static HRESULT WINAPI TgaDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
589 IWICPalette *pIPalette)
591 TgaDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
592 HRESULT hr=S_OK;
593 WICColor colors[256], *color;
594 BYTE *colormap_data;
595 WORD *wcolormap_data;
596 DWORD *dwcolormap_data;
597 LARGE_INTEGER seek;
598 ULONG bytesread;
599 int depth, attribute_bitcount, attribute_type;
600 int i;
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);
615 return E_FAIL;
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);
629 if (SUCCEEDED(hr))
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);
635 hr = E_FAIL;
639 LeaveCriticalSection(&This->lock);
641 if (SUCCEEDED(hr))
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;
649 else
650 attribute_type = ATTRIBUTE_NO_ALPHA;
652 depth = This->header.colormap_entrysize;
653 if (depth == 15)
655 depth = 16;
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. */
664 switch (depth)
666 case 16:
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 */
683 break;
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 */
697 break;
698 default:
699 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type);
700 hr = E_NOTIMPL;
702 break;
703 case 24:
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 */
711 break;
712 case 32:
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;
720 break;
721 case ATTRIBUTE_ALPHA:
722 for (i=0; i<This->header.colormap_length; i++)
723 *color++=dwcolormap_data[i];
724 break;
725 case ATTRIBUTE_PALPHA:
726 /* FIXME: Unpremultiply alpha */
727 default:
728 FIXME("Unhandled 16-bit attribute type %u\n", attribute_type);
729 hr = E_NOTIMPL;
731 break;
732 default:
733 FIXME("Unhandled truecolor depth %u\n", This->header.depth);
734 hr = E_NOTIMPL;
738 HeapFree(GetProcessHeap(), 0, colormap_data);
740 if (SUCCEEDED(hr))
741 hr = IWICPalette_InitializeCustom(pIPalette, colors, 256);
743 return hr;
746 static HRESULT TgaDecoder_ReadRLE(TgaDecoder *This, BYTE *imagebits, int datasize)
748 int i=0, j, bytesperpixel;
749 ULONG bytesread;
750 HRESULT hr=S_OK;
752 bytesperpixel = This->header.depth / 8;
754 while (i<datasize)
756 BYTE rc;
757 int count, size;
758 BYTE pixeldata[4];
760 hr = IStream_Read(This->stream, &rc, 1, &bytesread);
761 if (bytesread != 1) hr = E_FAIL;
762 if (FAILED(hr)) break;
764 count = (rc&0x7f)+1;
765 size = count * bytesperpixel;
767 if (size + i > datasize)
769 WARN("RLE packet too large\n");
770 hr = E_FAIL;
771 break;
774 if (rc&0x80)
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);
783 else
785 for (j=0; j<count; j++)
786 memcpy(&imagebits[i+j*bytesperpixel], pixeldata, bytesperpixel);
789 else
791 /* Raw packet */
792 hr = IStream_Read(This->stream, &imagebits[i], size, &bytesread);
793 if (bytesread != size) hr = E_FAIL;
794 if (FAILED(hr)) break;
797 i += size;
800 return hr;
803 static HRESULT TgaDecoder_ReadImage(TgaDecoder *This)
805 HRESULT hr=S_OK;
806 int datasize;
807 LARGE_INTEGER seek;
808 ULONG bytesread;
810 if (This->imagebits)
811 return S_OK;
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");
820 hr = E_NOTIMPL;
823 if (SUCCEEDED(hr))
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;
830 if (SUCCEEDED(hr))
832 seek.QuadPart = This->image_offset;
833 hr = IStream_Seek(This->stream, seek, STREAM_SEEK_SET, NULL);
836 if (SUCCEEDED(hr))
838 if (This->header.image_type & IMAGETYPE_RLE)
840 hr = TgaDecoder_ReadRLE(This, This->imagebits, datasize);
842 else
844 hr = IStream_Read(This->stream, This->imagebits, datasize, &bytesread);
845 if (SUCCEEDED(hr) && bytesread != datasize)
846 hr = E_FAIL;
850 if (SUCCEEDED(hr))
852 if (This->header.image_descriptor & IMAGE_TOPTOBOTTOM)
854 This->origin = This->imagebits;
855 This->stride = This->header.width * (This->header.depth / 8);
857 else
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);
863 else
865 HeapFree(GetProcessHeap(), 0, This->imagebits);
866 This->imagebits = NULL;
870 LeaveCriticalSection(&This->lock);
872 return hr;
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);
879 HRESULT hr;
881 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
883 hr = TgaDecoder_ReadImage(This);
885 if (SUCCEEDED(hr))
887 hr = copy_pixels(This->header.depth, This->origin,
888 This->header.width, This->header.height, This->stride,
889 prc, cbStride, cbBufferSize, pbBuffer);
892 return hr;
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)
932 TgaDecoder *This;
933 HRESULT ret;
935 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
937 *ppv = NULL;
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;
946 This->ref = 1;
947 This->initialized = FALSE;
948 This->stream = NULL;
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);
956 return ret;