windowscodecs: Implement Initialize for the TGA decoder.
[wine] / dlls / windowscodecs / tgaformat.c
1 /*
2  * Copyright 2010 Vincent Povirk for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include <stdarg.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "objbase.h"
29 #include "wincodec.h"
30
31 #include "wincodecs_private.h"
32
33 #include "wine/debug.h"
34 #include "wine/library.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
37
38 #include "pshpack1.h"
39
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;
56
57 #define IMAGETYPE_COLORMAPPED 1
58 #define IMAGETYPE_TRUECOLOR 2
59 #define IMAGETYPE_GRAYSCALE 3
60 #define IMAGETYPE_RLE 8
61
62 #define IMAGE_ATTRIBUTE_BITCOUNT_MASK 0xf
63 #define IMAGE_RIGHTTOLEFT 0x10
64 #define IMAGE_TOPTOBOTTOM 0x20
65
66 #include "poppack.h"
67
68 typedef struct {
69     const IWICBitmapDecoderVtbl *lpVtbl;
70     const IWICBitmapFrameDecodeVtbl *lpFrameVtbl;
71     LONG ref;
72     BOOL initialized;
73     IStream *stream;
74     tga_header header;
75     CRITICAL_SECTION lock;
76 } TgaDecoder;
77
78 static inline TgaDecoder *decoder_from_frame(IWICBitmapFrameDecode *iface)
79 {
80     return CONTAINING_RECORD(iface, TgaDecoder, lpFrameVtbl);
81 }
82
83 static HRESULT WINAPI TgaDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
84     void **ppv)
85 {
86     TgaDecoder *This = (TgaDecoder*)iface;
87     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
88
89     if (!ppv) return E_INVALIDARG;
90
91     if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
92     {
93         *ppv = This;
94     }
95     else
96     {
97         *ppv = NULL;
98         return E_NOINTERFACE;
99     }
100
101     IUnknown_AddRef((IUnknown*)*ppv);
102     return S_OK;
103 }
104
105 static ULONG WINAPI TgaDecoder_AddRef(IWICBitmapDecoder *iface)
106 {
107     TgaDecoder *This = (TgaDecoder*)iface;
108     ULONG ref = InterlockedIncrement(&This->ref);
109
110     TRACE("(%p) refcount=%u\n", iface, ref);
111
112     return ref;
113 }
114
115 static ULONG WINAPI TgaDecoder_Release(IWICBitmapDecoder *iface)
116 {
117     TgaDecoder *This = (TgaDecoder*)iface;
118     ULONG ref = InterlockedDecrement(&This->ref);
119
120     TRACE("(%p) refcount=%u\n", iface, ref);
121
122     if (ref == 0)
123     {
124         This->lock.DebugInfo->Spare[0] = 0;
125         DeleteCriticalSection(&This->lock);
126         if (This->stream)
127             IStream_Release(This->stream);
128         HeapFree(GetProcessHeap(), 0, This);
129     }
130
131     return ref;
132 }
133
134 static HRESULT WINAPI TgaDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
135     DWORD *pdwCapability)
136 {
137     FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
138     return E_NOTIMPL;
139 }
140
141 static HRESULT WINAPI TgaDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
142     WICDecodeOptions cacheOptions)
143 {
144     TgaDecoder *This = (TgaDecoder*)iface;
145     HRESULT hr=S_OK;
146     DWORD bytesread;
147     LARGE_INTEGER seek;
148
149     TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOptions);
150
151     EnterCriticalSection(&This->lock);
152
153     if (This->initialized)
154     {
155         hr = WINCODEC_ERR_WRONGSTATE;
156         goto end;
157     }
158
159     seek.QuadPart = 0;
160     hr = IStream_Seek(pIStream, seek, STREAM_SEEK_SET, NULL);
161     if (FAILED(hr)) goto end;
162
163     hr = IStream_Read(pIStream, &This->header, sizeof(tga_header), &bytesread);
164     if (SUCCEEDED(hr) && bytesread != sizeof(tga_header))
165     {
166         TRACE("got only %u bytes\n", bytesread);
167         hr = E_FAIL;
168     }
169     if (FAILED(hr)) goto end;
170
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);
174
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)
178     {
179     case IMAGETYPE_COLORMAPPED:
180     case IMAGETYPE_COLORMAPPED|IMAGETYPE_RLE:
181         if (This->header.colormap_type != 1)
182             hr = E_FAIL;
183         break;
184     case IMAGETYPE_TRUECOLOR:
185     case IMAGETYPE_TRUECOLOR|IMAGETYPE_RLE:
186         if (This->header.colormap_type != 0 && This->header.colormap_type != 1)
187             hr = E_FAIL;
188         break;
189     case IMAGETYPE_GRAYSCALE:
190     case IMAGETYPE_GRAYSCALE|IMAGETYPE_RLE:
191         if (This->header.colormap_type != 0)
192             hr = E_FAIL;
193         break;
194     default:
195         hr = E_FAIL;
196     }
197
198     if (This->header.depth != 8 && This->header.depth != 16 &&
199         This->header.depth != 24 && This->header.depth != 32)
200         hr = E_FAIL;
201
202     if ((This->header.image_descriptor & IMAGE_ATTRIBUTE_BITCOUNT_MASK) > 8 ||
203         (This->header.image_descriptor & 0xc0) != 0)
204         hr = E_FAIL;
205
206     if (FAILED(hr))
207     {
208         WARN("bad tga header\n");
209         goto end;
210     }
211
212     /* FIXME: Read footer if there is one. */
213
214     IStream_AddRef(pIStream);
215     This->stream = pIStream;
216     This->initialized = TRUE;
217
218 end:
219     LeaveCriticalSection(&This->lock);
220     return hr;
221 }
222
223 static HRESULT WINAPI TgaDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
224     GUID *pguidContainerFormat)
225 {
226     memcpy(pguidContainerFormat, &GUID_WineContainerFormatTga, sizeof(GUID));
227     return S_OK;
228 }
229
230 static HRESULT WINAPI TgaDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
231     IWICBitmapDecoderInfo **ppIDecoderInfo)
232 {
233     FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
234     return E_NOTIMPL;
235 }
236
237 static HRESULT WINAPI TgaDecoder_CopyPalette(IWICBitmapDecoder *iface,
238     IWICPalette *pIPalette)
239 {
240     FIXME("(%p,%p): stub\n", iface, pIPalette);
241     return E_NOTIMPL;
242 }
243
244 static HRESULT WINAPI TgaDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
245     IWICMetadataQueryReader **ppIMetadataQueryReader)
246 {
247     FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
248     return E_NOTIMPL;
249 }
250
251 static HRESULT WINAPI TgaDecoder_GetPreview(IWICBitmapDecoder *iface,
252     IWICBitmapSource **ppIBitmapSource)
253 {
254     FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
255     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
256 }
257
258 static HRESULT WINAPI TgaDecoder_GetColorContexts(IWICBitmapDecoder *iface,
259     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
260 {
261     FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
262     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
263 }
264
265 static HRESULT WINAPI TgaDecoder_GetThumbnail(IWICBitmapDecoder *iface,
266     IWICBitmapSource **ppIThumbnail)
267 {
268     FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
269     return WINCODEC_ERR_CODECNOTHUMBNAIL;
270 }
271
272 static HRESULT WINAPI TgaDecoder_GetFrameCount(IWICBitmapDecoder *iface,
273     UINT *pCount)
274 {
275     *pCount = 1;
276     return S_OK;
277 }
278
279 static HRESULT WINAPI TgaDecoder_GetFrame(IWICBitmapDecoder *iface,
280     UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
281 {
282     FIXME("(%p,%p): stub\n", iface, ppIBitmapFrame);
283     return E_NOTIMPL;
284 }
285
286 static const IWICBitmapDecoderVtbl TgaDecoder_Vtbl = {
287     TgaDecoder_QueryInterface,
288     TgaDecoder_AddRef,
289     TgaDecoder_Release,
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,
300     TgaDecoder_GetFrame
301 };
302
303 static HRESULT WINAPI TgaDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
304     void **ppv)
305 {
306     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
307
308     if (!ppv) return E_INVALIDARG;
309
310     if (IsEqualIID(&IID_IUnknown, iid) ||
311         IsEqualIID(&IID_IWICBitmapSource, iid) ||
312         IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
313     {
314         *ppv = iface;
315     }
316     else
317     {
318         *ppv = NULL;
319         return E_NOINTERFACE;
320     }
321
322     IUnknown_AddRef((IUnknown*)*ppv);
323     return S_OK;
324 }
325
326 static ULONG WINAPI TgaDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
327 {
328     TgaDecoder *This = decoder_from_frame(iface);
329     return IUnknown_AddRef((IUnknown*)This);
330 }
331
332 static ULONG WINAPI TgaDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
333 {
334     TgaDecoder *This = decoder_from_frame(iface);
335     return IUnknown_Release((IUnknown*)This);
336 }
337
338 static HRESULT WINAPI TgaDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
339     UINT *puiWidth, UINT *puiHeight)
340 {
341     FIXME("(%p)\n", iface);
342     return E_NOTIMPL;
343 }
344
345 static HRESULT WINAPI TgaDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
346     WICPixelFormatGUID *pPixelFormat)
347 {
348     TRACE("(%p,%p): stub\n", iface, pPixelFormat);
349     return E_NOTIMPL;
350 }
351
352 static HRESULT WINAPI TgaDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
353     double *pDpiX, double *pDpiY)
354 {
355     FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
356     return E_NOTIMPL;
357 }
358
359 static HRESULT WINAPI TgaDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
360     IWICPalette *pIPalette)
361 {
362     FIXME("(%p,%p): stub\n", iface, pIPalette);
363     return E_NOTIMPL;
364 }
365
366 static HRESULT WINAPI TgaDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
367     const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
368 {
369     FIXME("(%p,%p,%u,%u,%p):stub\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
370     return E_NOTIMPL;
371 }
372
373 static HRESULT WINAPI TgaDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
374     IWICMetadataQueryReader **ppIMetadataQueryReader)
375 {
376     FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
377     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
378 }
379
380 static HRESULT WINAPI TgaDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
381     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
382 {
383     FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
384     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
385 }
386
387 static HRESULT WINAPI TgaDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
388     IWICBitmapSource **ppIThumbnail)
389 {
390     FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
391     return WINCODEC_ERR_CODECNOTHUMBNAIL;
392 }
393
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
406 };
407
408 HRESULT TgaDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
409 {
410     TgaDecoder *This;
411     HRESULT ret;
412
413     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
414
415     *ppv = NULL;
416
417     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
418
419     This = HeapAlloc(GetProcessHeap(), 0, sizeof(TgaDecoder));
420     if (!This) return E_OUTOFMEMORY;
421
422     This->lpVtbl = &TgaDecoder_Vtbl;
423     This->lpFrameVtbl = &TgaDecoder_Frame_Vtbl;
424     This->ref = 1;
425     This->initialized = FALSE;
426     This->stream = NULL;
427     InitializeCriticalSection(&This->lock);
428     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": TgaDecoder.lock");
429
430     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
431     IUnknown_Release((IUnknown*)This);
432
433     return ret;
434 }