sync with trunk r46493
[reactos.git] / dll / win32 / 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 } JpegDecoder;
107
108 static inline JpegDecoder *decoder_from_decompress(j_decompress_ptr decompress)
109 {
110 return CONTAINING_RECORD(decompress, JpegDecoder, cinfo);
111 }
112
113 static inline JpegDecoder *decoder_from_frame(IWICBitmapFrameDecode *iface)
114 {
115 return CONTAINING_RECORD(iface, JpegDecoder, lpFrameVtbl);
116 }
117
118 static HRESULT WINAPI JpegDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
119 void **ppv)
120 {
121 JpegDecoder *This = (JpegDecoder*)iface;
122 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
123
124 if (!ppv) return E_INVALIDARG;
125
126 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
127 {
128 *ppv = This;
129 }
130 else
131 {
132 *ppv = NULL;
133 return E_NOINTERFACE;
134 }
135
136 IUnknown_AddRef((IUnknown*)*ppv);
137 return S_OK;
138 }
139
140 static ULONG WINAPI JpegDecoder_AddRef(IWICBitmapDecoder *iface)
141 {
142 JpegDecoder *This = (JpegDecoder*)iface;
143 ULONG ref = InterlockedIncrement(&This->ref);
144
145 TRACE("(%p) refcount=%u\n", iface, ref);
146
147 return ref;
148 }
149
150 static ULONG WINAPI JpegDecoder_Release(IWICBitmapDecoder *iface)
151 {
152 JpegDecoder *This = (JpegDecoder*)iface;
153 ULONG ref = InterlockedDecrement(&This->ref);
154
155 TRACE("(%p) refcount=%u\n", iface, ref);
156
157 if (ref == 0)
158 {
159 if (This->cinfo_initialized) pjpeg_destroy_decompress(&This->cinfo);
160 if (This->stream) IStream_Release(This->stream);
161 HeapFree(GetProcessHeap(), 0, This->image_data);
162 HeapFree(GetProcessHeap(), 0, This);
163 }
164
165 return ref;
166 }
167
168 static HRESULT WINAPI JpegDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
169 DWORD *pdwCapability)
170 {
171 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
172 return E_NOTIMPL;
173 }
174
175 static void source_mgr_init_source(j_decompress_ptr cinfo)
176 {
177 }
178
179 static jpeg_boolean source_mgr_fill_input_buffer(j_decompress_ptr cinfo)
180 {
181 JpegDecoder *This = decoder_from_decompress(cinfo);
182 HRESULT hr;
183 ULONG bytesread;
184
185 hr = IStream_Read(This->stream, This->source_buffer, 1024, &bytesread);
186
187 if (hr != S_OK || bytesread == 0)
188 {
189 return FALSE;
190 }
191 else
192 {
193 This->source_mgr.next_input_byte = This->source_buffer;
194 This->source_mgr.bytes_in_buffer = bytesread;
195 return TRUE;
196 }
197 }
198
199 static void source_mgr_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
200 {
201 JpegDecoder *This = decoder_from_decompress(cinfo);
202 LARGE_INTEGER seek;
203
204 if (num_bytes > This->source_mgr.bytes_in_buffer)
205 {
206 seek.QuadPart = num_bytes - This->source_mgr.bytes_in_buffer;
207 IStream_Seek(This->stream, seek, STREAM_SEEK_CUR, NULL);
208 This->source_mgr.bytes_in_buffer = 0;
209 }
210 else if (num_bytes > 0)
211 {
212 This->source_mgr.next_input_byte += num_bytes;
213 This->source_mgr.bytes_in_buffer -= num_bytes;
214 }
215 }
216
217 static void source_mgr_term_source(j_decompress_ptr cinfo)
218 {
219 }
220
221 static HRESULT WINAPI JpegDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
222 WICDecodeOptions cacheOptions)
223 {
224 JpegDecoder *This = (JpegDecoder*)iface;
225 int ret;
226 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOptions);
227
228 if (This->cinfo_initialized) return WINCODEC_ERR_WRONGSTATE;
229
230 This->cinfo.err = pjpeg_std_error(&This->jerr);
231
232 pjpeg_CreateDecompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_decompress_struct));
233
234 This->cinfo_initialized = TRUE;
235
236 This->stream = pIStream;
237 IStream_AddRef(pIStream);
238
239 This->source_mgr.bytes_in_buffer = 0;
240 This->source_mgr.init_source = source_mgr_init_source;
241 This->source_mgr.fill_input_buffer = source_mgr_fill_input_buffer;
242 This->source_mgr.skip_input_data = source_mgr_skip_input_data;
243 This->source_mgr.resync_to_restart = pjpeg_resync_to_restart;
244 This->source_mgr.term_source = source_mgr_term_source;
245
246 This->cinfo.src = &This->source_mgr;
247
248 ret = pjpeg_read_header(&This->cinfo, TRUE);
249
250 if (ret != JPEG_HEADER_OK) {
251 WARN("Jpeg image in stream has bad format, read header returned %d.\n",ret);
252 return E_FAIL;
253 }
254
255 if (This->cinfo.jpeg_color_space == JCS_GRAYSCALE)
256 This->cinfo.out_color_space = JCS_GRAYSCALE;
257 else
258 This->cinfo.out_color_space = JCS_RGB;
259
260 if (!pjpeg_start_decompress(&This->cinfo))
261 {
262 ERR("jpeg_start_decompress failed\n");
263 return E_FAIL;
264 }
265
266 This->initialized = TRUE;
267
268 return S_OK;
269 }
270
271 static HRESULT WINAPI JpegDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
272 GUID *pguidContainerFormat)
273 {
274 memcpy(pguidContainerFormat, &GUID_ContainerFormatJpeg, sizeof(GUID));
275 return S_OK;
276 }
277
278 static HRESULT WINAPI JpegDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
279 IWICBitmapDecoderInfo **ppIDecoderInfo)
280 {
281 FIXME("(%p,%p): stub\n", iface, ppIDecoderInfo);
282 return E_NOTIMPL;
283 }
284
285 static HRESULT WINAPI JpegDecoder_CopyPalette(IWICBitmapDecoder *iface,
286 IWICPalette *pIPalette)
287 {
288 TRACE("(%p,%p)\n", iface, pIPalette);
289
290 return WINCODEC_ERR_PALETTEUNAVAILABLE;
291 }
292
293 static HRESULT WINAPI JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
294 IWICMetadataQueryReader **ppIMetadataQueryReader)
295 {
296 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
297 return E_NOTIMPL;
298 }
299
300 static HRESULT WINAPI JpegDecoder_GetPreview(IWICBitmapDecoder *iface,
301 IWICBitmapSource **ppIBitmapSource)
302 {
303 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
304 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
305 }
306
307 static HRESULT WINAPI JpegDecoder_GetColorContexts(IWICBitmapDecoder *iface,
308 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
309 {
310 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
311 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
312 }
313
314 static HRESULT WINAPI JpegDecoder_GetThumbnail(IWICBitmapDecoder *iface,
315 IWICBitmapSource **ppIThumbnail)
316 {
317 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
318 return WINCODEC_ERR_CODECNOTHUMBNAIL;
319 }
320
321 static HRESULT WINAPI JpegDecoder_GetFrameCount(IWICBitmapDecoder *iface,
322 UINT *pCount)
323 {
324 *pCount = 1;
325 return S_OK;
326 }
327
328 static HRESULT WINAPI JpegDecoder_GetFrame(IWICBitmapDecoder *iface,
329 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
330 {
331 JpegDecoder *This = (JpegDecoder*)iface;
332 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
333
334 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
335
336 if (index != 0) return E_INVALIDARG;
337
338 IWICBitmapDecoder_AddRef(iface);
339 *ppIBitmapFrame = (IWICBitmapFrameDecode*)&This->lpFrameVtbl;
340
341 return S_OK;
342 }
343
344 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl = {
345 JpegDecoder_QueryInterface,
346 JpegDecoder_AddRef,
347 JpegDecoder_Release,
348 JpegDecoder_QueryCapability,
349 JpegDecoder_Initialize,
350 JpegDecoder_GetContainerFormat,
351 JpegDecoder_GetDecoderInfo,
352 JpegDecoder_CopyPalette,
353 JpegDecoder_GetMetadataQueryReader,
354 JpegDecoder_GetPreview,
355 JpegDecoder_GetColorContexts,
356 JpegDecoder_GetThumbnail,
357 JpegDecoder_GetFrameCount,
358 JpegDecoder_GetFrame
359 };
360
361 static HRESULT WINAPI JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
362 void **ppv)
363 {
364 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
365
366 if (!ppv) return E_INVALIDARG;
367
368 if (IsEqualIID(&IID_IUnknown, iid) ||
369 IsEqualIID(&IID_IWICBitmapSource, iid) ||
370 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
371 {
372 *ppv = iface;
373 }
374 else
375 {
376 *ppv = NULL;
377 return E_NOINTERFACE;
378 }
379
380 IUnknown_AddRef((IUnknown*)*ppv);
381 return S_OK;
382 }
383
384 static ULONG WINAPI JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
385 {
386 JpegDecoder *This = decoder_from_frame(iface);
387 return IUnknown_AddRef((IUnknown*)This);
388 }
389
390 static ULONG WINAPI JpegDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
391 {
392 JpegDecoder *This = decoder_from_frame(iface);
393 return IUnknown_Release((IUnknown*)This);
394 }
395
396 static HRESULT WINAPI JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
397 UINT *puiWidth, UINT *puiHeight)
398 {
399 JpegDecoder *This = decoder_from_frame(iface);
400 *puiWidth = This->cinfo.output_width;
401 *puiHeight = This->cinfo.output_height;
402 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
403 return S_OK;
404 }
405
406 static HRESULT WINAPI JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
407 WICPixelFormatGUID *pPixelFormat)
408 {
409 JpegDecoder *This = decoder_from_frame(iface);
410 TRACE("(%p,%p)\n", iface, pPixelFormat);
411 if (This->cinfo.out_color_space == JCS_RGB)
412 memcpy(pPixelFormat, &GUID_WICPixelFormat24bppBGR, sizeof(GUID));
413 else /* This->cinfo.out_color_space == JCS_GRAYSCALE */
414 memcpy(pPixelFormat, &GUID_WICPixelFormat8bppGray, sizeof(GUID));
415 return S_OK;
416 }
417
418 static HRESULT WINAPI JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
419 double *pDpiX, double *pDpiY)
420 {
421 FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
422 return E_NOTIMPL;
423 }
424
425 static HRESULT WINAPI JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
426 IWICPalette *pIPalette)
427 {
428 FIXME("(%p,%p): stub\n", iface, pIPalette);
429 return E_NOTIMPL;
430 }
431
432 static HRESULT WINAPI JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
433 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
434 {
435 JpegDecoder *This = decoder_from_frame(iface);
436 UINT bpp;
437 UINT stride;
438 UINT data_size;
439 UINT max_row_needed;
440 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
441
442 if (This->cinfo.out_color_space == JCS_GRAYSCALE) bpp = 8;
443 else bpp = 24;
444
445 stride = bpp * This->cinfo.output_width;
446 data_size = stride * This->cinfo.output_height;
447
448 max_row_needed = prc->Y + prc->Height;
449 if (max_row_needed > This->cinfo.output_height) return E_INVALIDARG;
450
451 if (!This->image_data)
452 {
453 This->image_data = HeapAlloc(GetProcessHeap(), 0, data_size);
454 if (!This->image_data) return E_OUTOFMEMORY;
455 }
456
457 while (max_row_needed > This->cinfo.output_scanline)
458 {
459 UINT first_scanline = This->cinfo.output_scanline;
460 UINT max_rows;
461 JSAMPROW out_rows[4];
462 UINT i, j;
463 JDIMENSION ret;
464
465 max_rows = min(This->cinfo.output_height-first_scanline, 4);
466 for (i=0; i<max_rows; i++)
467 out_rows[i] = This->image_data + stride * (first_scanline+i);
468
469 ret = pjpeg_read_scanlines(&This->cinfo, out_rows, max_rows);
470
471 if (ret == 0)
472 {
473 ERR("read_scanlines failed\n");
474 return E_FAIL;
475 }
476
477 if (bpp == 24)
478 {
479 /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
480 for (i=first_scanline; i<This->cinfo.output_scanline; i++)
481 {
482 BYTE *pixel = This->image_data + stride * i;
483 for (j=0; j<This->cinfo.output_width; j++)
484 {
485 BYTE red=pixel[0];
486 BYTE blue=pixel[2];
487 pixel[0]=blue;
488 pixel[2]=red;
489 pixel+=3;
490 }
491 }
492 }
493 }
494
495 return copy_pixels(bpp, This->image_data,
496 This->cinfo.output_width, This->cinfo.output_height, stride,
497 prc, cbStride, cbBufferSize, pbBuffer);
498 }
499
500 static HRESULT WINAPI JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
501 IWICMetadataQueryReader **ppIMetadataQueryReader)
502 {
503 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
504 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
505 }
506
507 static HRESULT WINAPI JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
508 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
509 {
510 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
511 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
512 }
513
514 static HRESULT WINAPI JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
515 IWICBitmapSource **ppIThumbnail)
516 {
517 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
518 return WINCODEC_ERR_CODECNOTHUMBNAIL;
519 }
520
521 static const IWICBitmapFrameDecodeVtbl JpegDecoder_Frame_Vtbl = {
522 JpegDecoder_Frame_QueryInterface,
523 JpegDecoder_Frame_AddRef,
524 JpegDecoder_Frame_Release,
525 JpegDecoder_Frame_GetSize,
526 JpegDecoder_Frame_GetPixelFormat,
527 JpegDecoder_Frame_GetResolution,
528 JpegDecoder_Frame_CopyPalette,
529 JpegDecoder_Frame_CopyPixels,
530 JpegDecoder_Frame_GetMetadataQueryReader,
531 JpegDecoder_Frame_GetColorContexts,
532 JpegDecoder_Frame_GetThumbnail
533 };
534
535 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
536 {
537 JpegDecoder *This;
538 HRESULT ret;
539
540 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
541
542 if (!libjpeg_handle && !load_libjpeg())
543 {
544 ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG);
545 return E_FAIL;
546 }
547
548 *ppv = NULL;
549
550 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
551
552 This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder));
553 if (!This) return E_OUTOFMEMORY;
554
555 This->lpVtbl = &JpegDecoder_Vtbl;
556 This->lpFrameVtbl = &JpegDecoder_Frame_Vtbl;
557 This->ref = 1;
558 This->initialized = FALSE;
559 This->cinfo_initialized = FALSE;
560 This->stream = NULL;
561 This->image_data = NULL;
562
563 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
564 IUnknown_Release((IUnknown*)This);
565
566 return ret;
567 }
568
569 #else /* !defined(SONAME_LIBJPEG) */
570
571 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
572 {
573 ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");
574 return E_FAIL;
575 }
576
577 #endif