wined3d: Don't crash during initialization if the adapter doesn't have full GL info.
[wine] / dlls / windowscodecs / jpegformat.c
1 /*
2  * Copyright 2009 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 #ifdef HAVE_UNISTD_H
23 # include <unistd.h>
24 #endif
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #ifdef SONAME_LIBJPEG
30 /* This is a hack, so jpeglib.h does not redefine INT32 and the like*/
31 #define XMD_H
32 #define UINT8 JPEG_UINT8
33 #define UINT16 JPEG_UINT16
34 #define boolean jpeg_boolean
35 #undef HAVE_STDLIB_H
36 # include <jpeglib.h>
37 #undef HAVE_STDLIB_H
38 #define HAVE_STDLIB_H 1
39 #undef UINT8
40 #undef UINT16
41 #undef boolean
42 #endif
43
44 #define COBJMACROS
45
46 #include "windef.h"
47 #include "winbase.h"
48 #include "objbase.h"
49 #include "wincodec.h"
50
51 #include "wincodecs_private.h"
52
53 #include "wine/debug.h"
54 #include "wine/library.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
57
58 #ifdef SONAME_LIBJPEG
59
60 static void *libjpeg_handle;
61
62 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
63 MAKE_FUNCPTR(jpeg_CreateDecompress);
64 MAKE_FUNCPTR(jpeg_destroy_decompress);
65 MAKE_FUNCPTR(jpeg_read_header);
66 MAKE_FUNCPTR(jpeg_read_scanlines);
67 MAKE_FUNCPTR(jpeg_resync_to_restart);
68 MAKE_FUNCPTR(jpeg_start_decompress);
69 MAKE_FUNCPTR(jpeg_std_error);
70 #undef MAKE_FUNCPTR
71
72 static void *load_libjpeg(void)
73 {
74     if((libjpeg_handle = wine_dlopen(SONAME_LIBJPEG, RTLD_NOW, NULL, 0)) != NULL) {
75
76 #define LOAD_FUNCPTR(f) \
77     if((p##f = wine_dlsym(libjpeg_handle, #f, NULL, 0)) == NULL) { \
78         libjpeg_handle = NULL; \
79         return NULL; \
80     }
81
82         LOAD_FUNCPTR(jpeg_CreateDecompress);
83         LOAD_FUNCPTR(jpeg_destroy_decompress);
84         LOAD_FUNCPTR(jpeg_read_header);
85         LOAD_FUNCPTR(jpeg_read_scanlines);
86         LOAD_FUNCPTR(jpeg_resync_to_restart);
87         LOAD_FUNCPTR(jpeg_start_decompress);
88         LOAD_FUNCPTR(jpeg_std_error);
89 #undef LOAD_FUNCPTR
90     }
91     return libjpeg_handle;
92 }
93
94 typedef struct {
95     const IWICBitmapDecoderVtbl *lpVtbl;
96     const IWICBitmapFrameDecodeVtbl *lpFrameVtbl;
97     LONG ref;
98     BOOL initialized;
99     BOOL cinfo_initialized;
100     IStream *stream;
101     struct jpeg_decompress_struct cinfo;
102     struct jpeg_error_mgr jerr;
103     struct jpeg_source_mgr source_mgr;
104     BYTE source_buffer[1024];
105     BYTE *image_data;
106     CRITICAL_SECTION lock;
107 } JpegDecoder;
108
109 static inline JpegDecoder *decoder_from_decompress(j_decompress_ptr decompress)
110 {
111     return CONTAINING_RECORD(decompress, JpegDecoder, cinfo);
112 }
113
114 static inline JpegDecoder *decoder_from_frame(IWICBitmapFrameDecode *iface)
115 {
116     return CONTAINING_RECORD(iface, JpegDecoder, lpFrameVtbl);
117 }
118
119 static HRESULT WINAPI JpegDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
120     void **ppv)
121 {
122     JpegDecoder *This = (JpegDecoder*)iface;
123     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
124
125     if (!ppv) return E_INVALIDARG;
126
127     if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
128     {
129         *ppv = This;
130     }
131     else
132     {
133         *ppv = NULL;
134         return E_NOINTERFACE;
135     }
136
137     IUnknown_AddRef((IUnknown*)*ppv);
138     return S_OK;
139 }
140
141 static ULONG WINAPI JpegDecoder_AddRef(IWICBitmapDecoder *iface)
142 {
143     JpegDecoder *This = (JpegDecoder*)iface;
144     ULONG ref = InterlockedIncrement(&This->ref);
145
146     TRACE("(%p) refcount=%u\n", iface, ref);
147
148     return ref;
149 }
150
151 static ULONG WINAPI JpegDecoder_Release(IWICBitmapDecoder *iface)
152 {
153     JpegDecoder *This = (JpegDecoder*)iface;
154     ULONG ref = InterlockedDecrement(&This->ref);
155
156     TRACE("(%p) refcount=%u\n", iface, ref);
157
158     if (ref == 0)
159     {
160         This->lock.DebugInfo->Spare[0] = 0;
161         DeleteCriticalSection(&This->lock);
162         if (This->cinfo_initialized) pjpeg_destroy_decompress(&This->cinfo);
163         if (This->stream) IStream_Release(This->stream);
164         HeapFree(GetProcessHeap(), 0, This->image_data);
165         HeapFree(GetProcessHeap(), 0, This);
166     }
167
168     return ref;
169 }
170
171 static HRESULT WINAPI JpegDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
172     DWORD *pdwCapability)
173 {
174     FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
175     return E_NOTIMPL;
176 }
177
178 static void source_mgr_init_source(j_decompress_ptr cinfo)
179 {
180 }
181
182 static jpeg_boolean source_mgr_fill_input_buffer(j_decompress_ptr cinfo)
183 {
184     JpegDecoder *This = decoder_from_decompress(cinfo);
185     HRESULT hr;
186     ULONG bytesread;
187
188     hr = IStream_Read(This->stream, This->source_buffer, 1024, &bytesread);
189
190     if (hr != S_OK || bytesread == 0)
191     {
192         return FALSE;
193     }
194     else
195     {
196         This->source_mgr.next_input_byte = This->source_buffer;
197         This->source_mgr.bytes_in_buffer = bytesread;
198         return TRUE;
199     }
200 }
201
202 static void source_mgr_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
203 {
204     JpegDecoder *This = decoder_from_decompress(cinfo);
205     LARGE_INTEGER seek;
206
207     if (num_bytes > This->source_mgr.bytes_in_buffer)
208     {
209         seek.QuadPart = num_bytes - This->source_mgr.bytes_in_buffer;
210         IStream_Seek(This->stream, seek, STREAM_SEEK_CUR, NULL);
211         This->source_mgr.bytes_in_buffer = 0;
212     }
213     else if (num_bytes > 0)
214     {
215         This->source_mgr.next_input_byte += num_bytes;
216         This->source_mgr.bytes_in_buffer -= num_bytes;
217     }
218 }
219
220 static void source_mgr_term_source(j_decompress_ptr cinfo)
221 {
222 }
223
224 static HRESULT WINAPI JpegDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
225     WICDecodeOptions cacheOptions)
226 {
227     JpegDecoder *This = (JpegDecoder*)iface;
228     int ret;
229     TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOptions);
230
231     EnterCriticalSection(&This->lock);
232
233     if (This->cinfo_initialized)
234     {
235         LeaveCriticalSection(&This->lock);
236         return WINCODEC_ERR_WRONGSTATE;
237     }
238
239     This->cinfo.err = pjpeg_std_error(&This->jerr);
240
241     pjpeg_CreateDecompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_decompress_struct));
242
243     This->cinfo_initialized = TRUE;
244
245     This->stream = pIStream;
246     IStream_AddRef(pIStream);
247
248     This->source_mgr.bytes_in_buffer = 0;
249     This->source_mgr.init_source = source_mgr_init_source;
250     This->source_mgr.fill_input_buffer = source_mgr_fill_input_buffer;
251     This->source_mgr.skip_input_data = source_mgr_skip_input_data;
252     This->source_mgr.resync_to_restart = pjpeg_resync_to_restart;
253     This->source_mgr.term_source = source_mgr_term_source;
254
255     This->cinfo.src = &This->source_mgr;
256
257     ret = pjpeg_read_header(&This->cinfo, TRUE);
258
259     if (ret != JPEG_HEADER_OK) {
260         WARN("Jpeg image in stream has bad format, read header returned %d.\n",ret);
261         LeaveCriticalSection(&This->lock);
262         return E_FAIL;
263     }
264
265     if (This->cinfo.jpeg_color_space == JCS_GRAYSCALE)
266         This->cinfo.out_color_space = JCS_GRAYSCALE;
267     else
268         This->cinfo.out_color_space = JCS_RGB;
269
270     if (!pjpeg_start_decompress(&This->cinfo))
271     {
272         ERR("jpeg_start_decompress failed\n");
273         LeaveCriticalSection(&This->lock);
274         return E_FAIL;
275     }
276
277     This->initialized = TRUE;
278
279     LeaveCriticalSection(&This->lock);
280
281     return S_OK;
282 }
283
284 static HRESULT WINAPI JpegDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
285     GUID *pguidContainerFormat)
286 {
287     memcpy(pguidContainerFormat, &GUID_ContainerFormatJpeg, sizeof(GUID));
288     return S_OK;
289 }
290
291 static HRESULT WINAPI JpegDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
292     IWICBitmapDecoderInfo **ppIDecoderInfo)
293 {
294     FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
295     return E_NOTIMPL;
296 }
297
298 static HRESULT WINAPI JpegDecoder_CopyPalette(IWICBitmapDecoder *iface,
299     IWICPalette *pIPalette)
300 {
301     TRACE("(%p,%p)\n", iface, pIPalette);
302
303     return WINCODEC_ERR_PALETTEUNAVAILABLE;
304 }
305
306 static HRESULT WINAPI JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
307     IWICMetadataQueryReader **ppIMetadataQueryReader)
308 {
309     FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
310     return E_NOTIMPL;
311 }
312
313 static HRESULT WINAPI JpegDecoder_GetPreview(IWICBitmapDecoder *iface,
314     IWICBitmapSource **ppIBitmapSource)
315 {
316     FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
317     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
318 }
319
320 static HRESULT WINAPI JpegDecoder_GetColorContexts(IWICBitmapDecoder *iface,
321     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
322 {
323     FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
324     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
325 }
326
327 static HRESULT WINAPI JpegDecoder_GetThumbnail(IWICBitmapDecoder *iface,
328     IWICBitmapSource **ppIThumbnail)
329 {
330     FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
331     return WINCODEC_ERR_CODECNOTHUMBNAIL;
332 }
333
334 static HRESULT WINAPI JpegDecoder_GetFrameCount(IWICBitmapDecoder *iface,
335     UINT *pCount)
336 {
337     *pCount = 1;
338     return S_OK;
339 }
340
341 static HRESULT WINAPI JpegDecoder_GetFrame(IWICBitmapDecoder *iface,
342     UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
343 {
344     JpegDecoder *This = (JpegDecoder*)iface;
345     TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
346
347     if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
348
349     if (index != 0) return E_INVALIDARG;
350
351     IWICBitmapDecoder_AddRef(iface);
352     *ppIBitmapFrame = (IWICBitmapFrameDecode*)&This->lpFrameVtbl;
353
354     return S_OK;
355 }
356
357 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl = {
358     JpegDecoder_QueryInterface,
359     JpegDecoder_AddRef,
360     JpegDecoder_Release,
361     JpegDecoder_QueryCapability,
362     JpegDecoder_Initialize,
363     JpegDecoder_GetContainerFormat,
364     JpegDecoder_GetDecoderInfo,
365     JpegDecoder_CopyPalette,
366     JpegDecoder_GetMetadataQueryReader,
367     JpegDecoder_GetPreview,
368     JpegDecoder_GetColorContexts,
369     JpegDecoder_GetThumbnail,
370     JpegDecoder_GetFrameCount,
371     JpegDecoder_GetFrame
372 };
373
374 static HRESULT WINAPI JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
375     void **ppv)
376 {
377     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
378
379     if (!ppv) return E_INVALIDARG;
380
381     if (IsEqualIID(&IID_IUnknown, iid) ||
382         IsEqualIID(&IID_IWICBitmapSource, iid) ||
383         IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
384     {
385         *ppv = iface;
386     }
387     else
388     {
389         *ppv = NULL;
390         return E_NOINTERFACE;
391     }
392
393     IUnknown_AddRef((IUnknown*)*ppv);
394     return S_OK;
395 }
396
397 static ULONG WINAPI JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
398 {
399     JpegDecoder *This = decoder_from_frame(iface);
400     return IUnknown_AddRef((IUnknown*)This);
401 }
402
403 static ULONG WINAPI JpegDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
404 {
405     JpegDecoder *This = decoder_from_frame(iface);
406     return IUnknown_Release((IUnknown*)This);
407 }
408
409 static HRESULT WINAPI JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
410     UINT *puiWidth, UINT *puiHeight)
411 {
412     JpegDecoder *This = decoder_from_frame(iface);
413     *puiWidth = This->cinfo.output_width;
414     *puiHeight = This->cinfo.output_height;
415     TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
416     return S_OK;
417 }
418
419 static HRESULT WINAPI JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
420     WICPixelFormatGUID *pPixelFormat)
421 {
422     JpegDecoder *This = decoder_from_frame(iface);
423     TRACE("(%p,%p)\n", iface, pPixelFormat);
424     if (This->cinfo.out_color_space == JCS_RGB)
425         memcpy(pPixelFormat, &GUID_WICPixelFormat24bppBGR, sizeof(GUID));
426     else /* This->cinfo.out_color_space == JCS_GRAYSCALE */
427         memcpy(pPixelFormat, &GUID_WICPixelFormat8bppGray, sizeof(GUID));
428     return S_OK;
429 }
430
431 static HRESULT WINAPI JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
432     double *pDpiX, double *pDpiY)
433 {
434     FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
435     return E_NOTIMPL;
436 }
437
438 static HRESULT WINAPI JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
439     IWICPalette *pIPalette)
440 {
441     FIXME("(%p,%p): stub\n", iface, pIPalette);
442     return E_NOTIMPL;
443 }
444
445 static HRESULT WINAPI JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
446     const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
447 {
448     JpegDecoder *This = decoder_from_frame(iface);
449     UINT bpp;
450     UINT stride;
451     UINT data_size;
452     UINT max_row_needed;
453     TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
454
455     if (This->cinfo.out_color_space == JCS_GRAYSCALE) bpp = 8;
456     else bpp = 24;
457
458     stride = bpp * This->cinfo.output_width;
459     data_size = stride * This->cinfo.output_height;
460
461     max_row_needed = prc->Y + prc->Height;
462     if (max_row_needed > This->cinfo.output_height) return E_INVALIDARG;
463
464     EnterCriticalSection(&This->lock);
465
466     if (!This->image_data)
467     {
468         This->image_data = HeapAlloc(GetProcessHeap(), 0, data_size);
469         if (!This->image_data)
470         {
471             LeaveCriticalSection(&This->lock);
472             return E_OUTOFMEMORY;
473         }
474     }
475
476     while (max_row_needed > This->cinfo.output_scanline)
477     {
478         UINT first_scanline = This->cinfo.output_scanline;
479         UINT max_rows;
480         JSAMPROW out_rows[4];
481         UINT i, j;
482         JDIMENSION ret;
483
484         max_rows = min(This->cinfo.output_height-first_scanline, 4);
485         for (i=0; i<max_rows; i++)
486             out_rows[i] = This->image_data + stride * (first_scanline+i);
487
488         ret = pjpeg_read_scanlines(&This->cinfo, out_rows, max_rows);
489
490         if (ret == 0)
491         {
492             ERR("read_scanlines failed\n");
493             LeaveCriticalSection(&This->lock);
494             return E_FAIL;
495         }
496
497         if (bpp == 24)
498         {
499             /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
500             for (i=first_scanline; i<This->cinfo.output_scanline; i++)
501             {
502                 BYTE *pixel = This->image_data + stride * i;
503                 for (j=0; j<This->cinfo.output_width; j++)
504                 {
505                     BYTE red=pixel[0];
506                     BYTE blue=pixel[2];
507                     pixel[0]=blue;
508                     pixel[2]=red;
509                     pixel+=3;
510                 }
511             }
512         }
513     }
514
515     LeaveCriticalSection(&This->lock);
516
517     return copy_pixels(bpp, This->image_data,
518         This->cinfo.output_width, This->cinfo.output_height, stride,
519         prc, cbStride, cbBufferSize, pbBuffer);
520 }
521
522 static HRESULT WINAPI JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
523     IWICMetadataQueryReader **ppIMetadataQueryReader)
524 {
525     FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
526     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
527 }
528
529 static HRESULT WINAPI JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
530     UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
531 {
532     FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
533     return WINCODEC_ERR_UNSUPPORTEDOPERATION;
534 }
535
536 static HRESULT WINAPI JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
537     IWICBitmapSource **ppIThumbnail)
538 {
539     FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
540     return WINCODEC_ERR_CODECNOTHUMBNAIL;
541 }
542
543 static const IWICBitmapFrameDecodeVtbl JpegDecoder_Frame_Vtbl = {
544     JpegDecoder_Frame_QueryInterface,
545     JpegDecoder_Frame_AddRef,
546     JpegDecoder_Frame_Release,
547     JpegDecoder_Frame_GetSize,
548     JpegDecoder_Frame_GetPixelFormat,
549     JpegDecoder_Frame_GetResolution,
550     JpegDecoder_Frame_CopyPalette,
551     JpegDecoder_Frame_CopyPixels,
552     JpegDecoder_Frame_GetMetadataQueryReader,
553     JpegDecoder_Frame_GetColorContexts,
554     JpegDecoder_Frame_GetThumbnail
555 };
556
557 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
558 {
559     JpegDecoder *This;
560     HRESULT ret;
561
562     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
563
564     if (!libjpeg_handle && !load_libjpeg())
565     {
566         ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG);
567         return E_FAIL;
568     }
569
570     *ppv = NULL;
571
572     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
573
574     This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder));
575     if (!This) return E_OUTOFMEMORY;
576
577     This->lpVtbl = &JpegDecoder_Vtbl;
578     This->lpFrameVtbl = &JpegDecoder_Frame_Vtbl;
579     This->ref = 1;
580     This->initialized = FALSE;
581     This->cinfo_initialized = FALSE;
582     This->stream = NULL;
583     This->image_data = NULL;
584     InitializeCriticalSection(&This->lock);
585     This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JpegDecoder.lock");
586
587     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
588     IUnknown_Release((IUnknown*)This);
589
590     return ret;
591 }
592
593 #else /* !defined(SONAME_LIBJPEG) */
594
595 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
596 {
597     ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");
598     return E_FAIL;
599 }
600
601 #endif