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
69 const IWICBitmapDecoderVtbl *lpVtbl;
70 const IWICBitmapFrameDecodeVtbl *lpFrameVtbl;
75 CRITICAL_SECTION lock;
78 static inline TgaDecoder *decoder_from_frame(IWICBitmapFrameDecode *iface)
80 return CONTAINING_RECORD(iface, TgaDecoder, lpFrameVtbl);
83 static HRESULT WINAPI TgaDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
86 TgaDecoder *This = (TgaDecoder*)iface;
87 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
89 if (!ppv) return E_INVALIDARG;
91 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
101 IUnknown_AddRef((IUnknown*)*ppv);
105 static ULONG WINAPI TgaDecoder_AddRef(IWICBitmapDecoder *iface)
107 TgaDecoder *This = (TgaDecoder*)iface;
108 ULONG ref = InterlockedIncrement(&This->ref);
110 TRACE("(%p) refcount=%u\n", iface, ref);
115 static ULONG WINAPI TgaDecoder_Release(IWICBitmapDecoder *iface)
117 TgaDecoder *This = (TgaDecoder*)iface;
118 ULONG ref = InterlockedDecrement(&This->ref);
120 TRACE("(%p) refcount=%u\n", iface, ref);
124 This->lock.DebugInfo->Spare[0] = 0;
125 DeleteCriticalSection(&This->lock);
127 IStream_Release(This->stream);
128 HeapFree(GetProcessHeap(), 0, This);
134 static HRESULT WINAPI TgaDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
135 DWORD *pdwCapability)
137 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
141 static HRESULT WINAPI TgaDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
142 WICDecodeOptions cacheOptions)
144 TgaDecoder *This = (TgaDecoder*)iface;
149 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOptions);
151 EnterCriticalSection(&This->lock);
153 if (This->initialized)
155 hr = WINCODEC_ERR_WRONGSTATE;
160 hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
161 if (FAILED(hr)) goto end;
163 hr = IStream_Read(pIStream, &This->header, sizeof(tga_header), &bytesread);
164 if (SUCCEEDED(hr) && bytesread != sizeof(tga_header))
166 TRACE("got only %u bytes\n", bytesread);
169 if (FAILED(hr)) goto end;
171 TRACE("imagetype=%u, colormap type=%u, depth=%u, image descriptor=0x%x\n",
172 This->header.image_type, This->header.colormap_type,
173 This->header.depth, This->header.image_descriptor);
175 /* Sanity checking. Since TGA has no clear identifying markers, we need
176 * to be careful to not load a non-TGA image. */
177 switch (This->header.image_type)
179 case IMAGETYPE_COLORMAPPED:
180 case IMAGETYPE_COLORMAPPED|IMAGETYPE_RLE:
181 if (This->header.colormap_type != 1)
184 case IMAGETYPE_TRUECOLOR:
185 case IMAGETYPE_TRUECOLOR|IMAGETYPE_RLE:
186 if (This->header.colormap_type != 0 && This->header.colormap_type != 1)
189 case IMAGETYPE_GRAYSCALE:
190 case IMAGETYPE_GRAYSCALE|IMAGETYPE_RLE:
191 if (This->header.colormap_type != 0)
198 if (This->header.depth != 8 && This->header.depth != 16 &&
199 This->header.depth != 24 && This->header.depth != 32)
202 if ((This->header.image_descriptor & IMAGE_ATTRIBUTE_BITCOUNT_MASK) > 8 ||
203 (This->header.image_descriptor & 0xc0) != 0)
208 WARN("bad tga header\n");
212 /* FIXME: Read footer if there is one. */
214 IStream_AddRef(pIStream);
215 This->stream = pIStream;
216 This->initialized = TRUE;
219 LeaveCriticalSection(&This->lock);
223 static HRESULT WINAPI TgaDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
224 GUID *pguidContainerFormat)
226 memcpy(pguidContainerFormat, &GUID_WineContainerFormatTga, sizeof(GUID));
230 static HRESULT WINAPI TgaDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
231 IWICBitmapDecoderInfo **ppIDecoderInfo)
233 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
237 static HRESULT WINAPI TgaDecoder_CopyPalette(IWICBitmapDecoder *iface,
238 IWICPalette *pIPalette)
240 FIXME("(%p,%p): stub\n", iface, pIPalette);
244 static HRESULT WINAPI TgaDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
245 IWICMetadataQueryReader **ppIMetadataQueryReader)
247 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
251 static HRESULT WINAPI TgaDecoder_GetPreview(IWICBitmapDecoder *iface,
252 IWICBitmapSource **ppIBitmapSource)
254 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
255 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
258 static HRESULT WINAPI TgaDecoder_GetColorContexts(IWICBitmapDecoder *iface,
259 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
261 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
262 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
265 static HRESULT WINAPI TgaDecoder_GetThumbnail(IWICBitmapDecoder *iface,
266 IWICBitmapSource **ppIThumbnail)
268 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
269 return WINCODEC_ERR_CODECNOTHUMBNAIL;
272 static HRESULT WINAPI TgaDecoder_GetFrameCount(IWICBitmapDecoder *iface,
279 static HRESULT WINAPI TgaDecoder_GetFrame(IWICBitmapDecoder *iface,
280 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
282 FIXME("(%p,%p): stub\n", iface, ppIBitmapFrame);
286 static const IWICBitmapDecoderVtbl TgaDecoder_Vtbl = {
287 TgaDecoder_QueryInterface,
290 TgaDecoder_QueryCapability,
291 TgaDecoder_Initialize,
292 TgaDecoder_GetContainerFormat,
293 TgaDecoder_GetDecoderInfo,
294 TgaDecoder_CopyPalette,
295 TgaDecoder_GetMetadataQueryReader,
296 TgaDecoder_GetPreview,
297 TgaDecoder_GetColorContexts,
298 TgaDecoder_GetThumbnail,
299 TgaDecoder_GetFrameCount,
303 static HRESULT WINAPI TgaDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
306 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
308 if (!ppv) return E_INVALIDARG;
310 if (IsEqualIID(&IID_IUnknown, iid) ||
311 IsEqualIID(&IID_IWICBitmapSource, iid) ||
312 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
319 return E_NOINTERFACE;
322 IUnknown_AddRef((IUnknown*)*ppv);
326 static ULONG WINAPI TgaDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
328 TgaDecoder *This = decoder_from_frame(iface);
329 return IUnknown_AddRef((IUnknown*)This);
332 static ULONG WINAPI TgaDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
334 TgaDecoder *This = decoder_from_frame(iface);
335 return IUnknown_Release((IUnknown*)This);
338 static HRESULT WINAPI TgaDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
339 UINT *puiWidth, UINT *puiHeight)
341 FIXME("(%p)\n", iface);
345 static HRESULT WINAPI TgaDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
346 WICPixelFormatGUID *pPixelFormat)
348 TRACE("(%p,%p): stub\n", iface, pPixelFormat);
352 static HRESULT WINAPI TgaDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
353 double *pDpiX, double *pDpiY)
355 FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
359 static HRESULT WINAPI TgaDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
360 IWICPalette *pIPalette)
362 FIXME("(%p,%p): stub\n", iface, pIPalette);
366 static HRESULT WINAPI TgaDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
367 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
369 FIXME("(%p,%p,%u,%u,%p):stub\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
373 static HRESULT WINAPI TgaDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
374 IWICMetadataQueryReader **ppIMetadataQueryReader)
376 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
377 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
380 static HRESULT WINAPI TgaDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
381 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
383 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
384 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
387 static HRESULT WINAPI TgaDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
388 IWICBitmapSource **ppIThumbnail)
390 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
391 return WINCODEC_ERR_CODECNOTHUMBNAIL;
394 static const IWICBitmapFrameDecodeVtbl TgaDecoder_Frame_Vtbl = {
395 TgaDecoder_Frame_QueryInterface,
396 TgaDecoder_Frame_AddRef,
397 TgaDecoder_Frame_Release,
398 TgaDecoder_Frame_GetSize,
399 TgaDecoder_Frame_GetPixelFormat,
400 TgaDecoder_Frame_GetResolution,
401 TgaDecoder_Frame_CopyPalette,
402 TgaDecoder_Frame_CopyPixels,
403 TgaDecoder_Frame_GetMetadataQueryReader,
404 TgaDecoder_Frame_GetColorContexts,
405 TgaDecoder_Frame_GetThumbnail
408 HRESULT TgaDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
413 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
417 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
419 This = HeapAlloc(GetProcessHeap(), 0, sizeof(TgaDecoder));
420 if (!This) return E_OUTOFMEMORY;
422 This->lpVtbl = &TgaDecoder_Vtbl;
423 This->lpFrameVtbl = &TgaDecoder_Frame_Vtbl;
425 This->initialized = FALSE;
427 InitializeCriticalSection(&This->lock);
428 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TgaDecoder.lock");
430 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
431 IUnknown_Release((IUnknown*)This);