windowscodecs: Add wrapper functions for IWICBitmap methods.
[wine/multimedia.git] / dlls / windowscodecs / jpegformat.c
blob53d7ec471d0a5a758d666501b30579899fd04253
1 /*
2 * Copyright 2009 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 #ifdef HAVE_UNISTD_H
23 # include <unistd.h>
24 #endif
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <setjmp.h>
30 #ifdef SONAME_LIBJPEG
31 /* This is a hack, so jpeglib.h does not redefine INT32 and the like*/
32 #define XMD_H
33 #define UINT8 JPEG_UINT8
34 #define UINT16 JPEG_UINT16
35 #define boolean jpeg_boolean
36 #undef HAVE_STDLIB_H
37 # include <jpeglib.h>
38 #undef HAVE_STDLIB_H
39 #define HAVE_STDLIB_H 1
40 #undef UINT8
41 #undef UINT16
42 #undef boolean
43 #endif
45 #define COBJMACROS
47 #include "windef.h"
48 #include "winbase.h"
49 #include "objbase.h"
50 #include "wincodec.h"
52 #include "wincodecs_private.h"
54 #include "wine/debug.h"
55 #include "wine/library.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
59 #ifdef SONAME_LIBJPEG
60 WINE_DECLARE_DEBUG_CHANNEL(jpeg);
62 static void *libjpeg_handle;
64 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
65 MAKE_FUNCPTR(jpeg_CreateCompress);
66 MAKE_FUNCPTR(jpeg_CreateDecompress);
67 MAKE_FUNCPTR(jpeg_destroy_compress);
68 MAKE_FUNCPTR(jpeg_destroy_decompress);
69 MAKE_FUNCPTR(jpeg_finish_compress);
70 MAKE_FUNCPTR(jpeg_read_header);
71 MAKE_FUNCPTR(jpeg_read_scanlines);
72 MAKE_FUNCPTR(jpeg_resync_to_restart);
73 MAKE_FUNCPTR(jpeg_set_defaults);
74 MAKE_FUNCPTR(jpeg_start_compress);
75 MAKE_FUNCPTR(jpeg_start_decompress);
76 MAKE_FUNCPTR(jpeg_std_error);
77 MAKE_FUNCPTR(jpeg_write_scanlines);
78 #undef MAKE_FUNCPTR
80 static void *load_libjpeg(void)
82 if((libjpeg_handle = wine_dlopen(SONAME_LIBJPEG, RTLD_NOW, NULL, 0)) != NULL) {
84 #define LOAD_FUNCPTR(f) \
85 if((p##f = wine_dlsym(libjpeg_handle, #f, NULL, 0)) == NULL) { \
86 libjpeg_handle = NULL; \
87 return NULL; \
90 LOAD_FUNCPTR(jpeg_CreateCompress);
91 LOAD_FUNCPTR(jpeg_CreateDecompress);
92 LOAD_FUNCPTR(jpeg_destroy_compress);
93 LOAD_FUNCPTR(jpeg_destroy_decompress);
94 LOAD_FUNCPTR(jpeg_finish_compress);
95 LOAD_FUNCPTR(jpeg_read_header);
96 LOAD_FUNCPTR(jpeg_read_scanlines);
97 LOAD_FUNCPTR(jpeg_resync_to_restart);
98 LOAD_FUNCPTR(jpeg_set_defaults);
99 LOAD_FUNCPTR(jpeg_start_compress);
100 LOAD_FUNCPTR(jpeg_start_decompress);
101 LOAD_FUNCPTR(jpeg_std_error);
102 LOAD_FUNCPTR(jpeg_write_scanlines);
103 #undef LOAD_FUNCPTR
105 return libjpeg_handle;
108 static void error_exit_fn(j_common_ptr cinfo)
110 char message[JMSG_LENGTH_MAX];
111 if (ERR_ON(jpeg))
113 cinfo->err->format_message(cinfo, message);
114 ERR_(jpeg)("%s\n", message);
116 longjmp(*(jmp_buf*)cinfo->client_data, 1);
119 static void emit_message_fn(j_common_ptr cinfo, int msg_level)
121 char message[JMSG_LENGTH_MAX];
123 if (msg_level < 0 && ERR_ON(jpeg))
125 cinfo->err->format_message(cinfo, message);
126 ERR_(jpeg)("%s\n", message);
128 else if (msg_level == 0 && WARN_ON(jpeg))
130 cinfo->err->format_message(cinfo, message);
131 WARN_(jpeg)("%s\n", message);
133 else if (msg_level > 0 && TRACE_ON(jpeg))
135 cinfo->err->format_message(cinfo, message);
136 TRACE_(jpeg)("%s\n", message);
140 typedef struct {
141 IWICBitmapDecoder IWICBitmapDecoder_iface;
142 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
143 LONG ref;
144 BOOL initialized;
145 BOOL cinfo_initialized;
146 IStream *stream;
147 struct jpeg_decompress_struct cinfo;
148 struct jpeg_error_mgr jerr;
149 struct jpeg_source_mgr source_mgr;
150 BYTE source_buffer[1024];
151 BYTE *image_data;
152 CRITICAL_SECTION lock;
153 } JpegDecoder;
155 static inline JpegDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
157 return CONTAINING_RECORD(iface, JpegDecoder, IWICBitmapDecoder_iface);
160 static inline JpegDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
162 return CONTAINING_RECORD(iface, JpegDecoder, IWICBitmapFrameDecode_iface);
165 static inline JpegDecoder *decoder_from_decompress(j_decompress_ptr decompress)
167 return CONTAINING_RECORD(decompress, JpegDecoder, cinfo);
170 static HRESULT WINAPI JpegDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
171 void **ppv)
173 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
174 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
176 if (!ppv) return E_INVALIDARG;
178 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
180 *ppv = This;
182 else
184 *ppv = NULL;
185 return E_NOINTERFACE;
188 IUnknown_AddRef((IUnknown*)*ppv);
189 return S_OK;
192 static ULONG WINAPI JpegDecoder_AddRef(IWICBitmapDecoder *iface)
194 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
195 ULONG ref = InterlockedIncrement(&This->ref);
197 TRACE("(%p) refcount=%u\n", iface, ref);
199 return ref;
202 static ULONG WINAPI JpegDecoder_Release(IWICBitmapDecoder *iface)
204 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
205 ULONG ref = InterlockedDecrement(&This->ref);
207 TRACE("(%p) refcount=%u\n", iface, ref);
209 if (ref == 0)
211 This->lock.DebugInfo->Spare[0] = 0;
212 DeleteCriticalSection(&This->lock);
213 if (This->cinfo_initialized) pjpeg_destroy_decompress(&This->cinfo);
214 if (This->stream) IStream_Release(This->stream);
215 HeapFree(GetProcessHeap(), 0, This->image_data);
216 HeapFree(GetProcessHeap(), 0, This);
219 return ref;
222 static HRESULT WINAPI JpegDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
223 DWORD *pdwCapability)
225 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
226 return E_NOTIMPL;
229 static void source_mgr_init_source(j_decompress_ptr cinfo)
233 static jpeg_boolean source_mgr_fill_input_buffer(j_decompress_ptr cinfo)
235 JpegDecoder *This = decoder_from_decompress(cinfo);
236 HRESULT hr;
237 ULONG bytesread;
239 hr = IStream_Read(This->stream, This->source_buffer, 1024, &bytesread);
241 if (hr != S_OK || bytesread == 0)
243 return FALSE;
245 else
247 This->source_mgr.next_input_byte = This->source_buffer;
248 This->source_mgr.bytes_in_buffer = bytesread;
249 return TRUE;
253 static void source_mgr_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
255 JpegDecoder *This = decoder_from_decompress(cinfo);
256 LARGE_INTEGER seek;
258 if (num_bytes > This->source_mgr.bytes_in_buffer)
260 seek.QuadPart = num_bytes - This->source_mgr.bytes_in_buffer;
261 IStream_Seek(This->stream, seek, STREAM_SEEK_CUR, NULL);
262 This->source_mgr.bytes_in_buffer = 0;
264 else if (num_bytes > 0)
266 This->source_mgr.next_input_byte += num_bytes;
267 This->source_mgr.bytes_in_buffer -= num_bytes;
271 static void source_mgr_term_source(j_decompress_ptr cinfo)
275 static HRESULT WINAPI JpegDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
276 WICDecodeOptions cacheOptions)
278 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
279 int ret;
280 LARGE_INTEGER seek;
281 jmp_buf jmpbuf;
282 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOptions);
284 EnterCriticalSection(&This->lock);
286 if (This->cinfo_initialized)
288 LeaveCriticalSection(&This->lock);
289 return WINCODEC_ERR_WRONGSTATE;
292 pjpeg_std_error(&This->jerr);
294 This->jerr.error_exit = error_exit_fn;
295 This->jerr.emit_message = emit_message_fn;
297 This->cinfo.err = &This->jerr;
299 This->cinfo.client_data = jmpbuf;
301 if (setjmp(jmpbuf))
303 LeaveCriticalSection(&This->lock);
304 return E_FAIL;
307 pjpeg_CreateDecompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_decompress_struct));
309 This->cinfo_initialized = TRUE;
311 This->stream = pIStream;
312 IStream_AddRef(pIStream);
314 seek.QuadPart = 0;
315 IStream_Seek(This->stream, seek, STREAM_SEEK_SET, NULL);
317 This->source_mgr.bytes_in_buffer = 0;
318 This->source_mgr.init_source = source_mgr_init_source;
319 This->source_mgr.fill_input_buffer = source_mgr_fill_input_buffer;
320 This->source_mgr.skip_input_data = source_mgr_skip_input_data;
321 This->source_mgr.resync_to_restart = pjpeg_resync_to_restart;
322 This->source_mgr.term_source = source_mgr_term_source;
324 This->cinfo.src = &This->source_mgr;
326 ret = pjpeg_read_header(&This->cinfo, TRUE);
328 if (ret != JPEG_HEADER_OK) {
329 WARN("Jpeg image in stream has bad format, read header returned %d.\n",ret);
330 LeaveCriticalSection(&This->lock);
331 return E_FAIL;
334 switch (This->cinfo.jpeg_color_space)
336 case JCS_GRAYSCALE:
337 This->cinfo.out_color_space = JCS_GRAYSCALE;
338 break;
339 case JCS_RGB:
340 case JCS_YCbCr:
341 This->cinfo.out_color_space = JCS_RGB;
342 break;
343 case JCS_CMYK:
344 case JCS_YCCK:
345 This->cinfo.out_color_space = JCS_CMYK;
346 break;
347 default:
348 ERR("Unknown JPEG color space %i\n", This->cinfo.jpeg_color_space);
349 LeaveCriticalSection(&This->lock);
350 return E_FAIL;
353 if (!pjpeg_start_decompress(&This->cinfo))
355 ERR("jpeg_start_decompress failed\n");
356 LeaveCriticalSection(&This->lock);
357 return E_FAIL;
360 This->initialized = TRUE;
362 LeaveCriticalSection(&This->lock);
364 return S_OK;
367 static HRESULT WINAPI JpegDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
368 GUID *pguidContainerFormat)
370 memcpy(pguidContainerFormat, &GUID_ContainerFormatJpeg, sizeof(GUID));
371 return S_OK;
374 static HRESULT WINAPI JpegDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
375 IWICBitmapDecoderInfo **ppIDecoderInfo)
377 HRESULT hr;
378 IWICComponentInfo *compinfo;
380 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
382 hr = CreateComponentInfo(&CLSID_WICJpegDecoder, &compinfo);
383 if (FAILED(hr)) return hr;
385 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
386 (void**)ppIDecoderInfo);
388 IWICComponentInfo_Release(compinfo);
390 return hr;
393 static HRESULT WINAPI JpegDecoder_CopyPalette(IWICBitmapDecoder *iface,
394 IWICPalette *pIPalette)
396 TRACE("(%p,%p)\n", iface, pIPalette);
398 return WINCODEC_ERR_PALETTEUNAVAILABLE;
401 static HRESULT WINAPI JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
402 IWICMetadataQueryReader **ppIMetadataQueryReader)
404 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
405 return E_NOTIMPL;
408 static HRESULT WINAPI JpegDecoder_GetPreview(IWICBitmapDecoder *iface,
409 IWICBitmapSource **ppIBitmapSource)
411 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
412 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
415 static HRESULT WINAPI JpegDecoder_GetColorContexts(IWICBitmapDecoder *iface,
416 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
418 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
419 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
422 static HRESULT WINAPI JpegDecoder_GetThumbnail(IWICBitmapDecoder *iface,
423 IWICBitmapSource **ppIThumbnail)
425 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
426 return WINCODEC_ERR_CODECNOTHUMBNAIL;
429 static HRESULT WINAPI JpegDecoder_GetFrameCount(IWICBitmapDecoder *iface,
430 UINT *pCount)
432 *pCount = 1;
433 return S_OK;
436 static HRESULT WINAPI JpegDecoder_GetFrame(IWICBitmapDecoder *iface,
437 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
439 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
440 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
442 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
444 if (index != 0) return E_INVALIDARG;
446 IWICBitmapDecoder_AddRef(iface);
447 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
449 return S_OK;
452 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl = {
453 JpegDecoder_QueryInterface,
454 JpegDecoder_AddRef,
455 JpegDecoder_Release,
456 JpegDecoder_QueryCapability,
457 JpegDecoder_Initialize,
458 JpegDecoder_GetContainerFormat,
459 JpegDecoder_GetDecoderInfo,
460 JpegDecoder_CopyPalette,
461 JpegDecoder_GetMetadataQueryReader,
462 JpegDecoder_GetPreview,
463 JpegDecoder_GetColorContexts,
464 JpegDecoder_GetThumbnail,
465 JpegDecoder_GetFrameCount,
466 JpegDecoder_GetFrame
469 static HRESULT WINAPI JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
470 void **ppv)
472 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
474 if (!ppv) return E_INVALIDARG;
476 if (IsEqualIID(&IID_IUnknown, iid) ||
477 IsEqualIID(&IID_IWICBitmapSource, iid) ||
478 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
480 *ppv = iface;
482 else
484 *ppv = NULL;
485 return E_NOINTERFACE;
488 IUnknown_AddRef((IUnknown*)*ppv);
489 return S_OK;
492 static ULONG WINAPI JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
494 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
495 return IUnknown_AddRef((IUnknown*)This);
498 static ULONG WINAPI JpegDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
500 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
501 return IUnknown_Release((IUnknown*)This);
504 static HRESULT WINAPI JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
505 UINT *puiWidth, UINT *puiHeight)
507 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
508 *puiWidth = This->cinfo.output_width;
509 *puiHeight = This->cinfo.output_height;
510 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
511 return S_OK;
514 static HRESULT WINAPI JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
515 WICPixelFormatGUID *pPixelFormat)
517 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
518 TRACE("(%p,%p)\n", iface, pPixelFormat);
519 if (This->cinfo.out_color_space == JCS_RGB)
520 memcpy(pPixelFormat, &GUID_WICPixelFormat24bppBGR, sizeof(GUID));
521 else if (This->cinfo.out_color_space == JCS_CMYK)
522 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppCMYK, sizeof(GUID));
523 else /* This->cinfo.out_color_space == JCS_GRAYSCALE */
524 memcpy(pPixelFormat, &GUID_WICPixelFormat8bppGray, sizeof(GUID));
525 return S_OK;
528 static HRESULT WINAPI JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
529 double *pDpiX, double *pDpiY)
531 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
533 EnterCriticalSection(&This->lock);
535 if (This->cinfo.density_unit == 2) /* pixels per centimeter */
537 *pDpiX = This->cinfo.X_density * 2.54;
538 *pDpiY = This->cinfo.Y_density * 2.54;
540 else
542 /* 1 = pixels per inch, 0 = unknown */
543 *pDpiX = This->cinfo.X_density;
544 *pDpiY = This->cinfo.Y_density;
547 LeaveCriticalSection(&This->lock);
549 TRACE("(%p)->(%0.2f,%0.2f)\n", iface, *pDpiX, *pDpiY);
551 return S_OK;
554 static HRESULT WINAPI JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
555 IWICPalette *pIPalette)
557 FIXME("(%p,%p): stub\n", iface, pIPalette);
558 return E_NOTIMPL;
561 static HRESULT WINAPI JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
562 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
564 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
565 UINT bpp;
566 UINT stride;
567 UINT data_size;
568 UINT max_row_needed;
569 jmp_buf jmpbuf;
570 WICRect rect;
571 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
573 if (!prc)
575 rect.X = 0;
576 rect.Y = 0;
577 rect.Width = This->cinfo.output_width;
578 rect.Height = This->cinfo.output_height;
579 prc = &rect;
581 else
583 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->cinfo.output_width ||
584 prc->Y+prc->Height > This->cinfo.output_height)
585 return E_INVALIDARG;
588 if (This->cinfo.out_color_space == JCS_GRAYSCALE) bpp = 8;
589 else if (This->cinfo.out_color_space == JCS_CMYK) bpp = 32;
590 else bpp = 24;
592 stride = bpp * This->cinfo.output_width;
593 data_size = stride * This->cinfo.output_height;
595 max_row_needed = prc->Y + prc->Height;
596 if (max_row_needed > This->cinfo.output_height) return E_INVALIDARG;
598 EnterCriticalSection(&This->lock);
600 if (!This->image_data)
602 This->image_data = HeapAlloc(GetProcessHeap(), 0, data_size);
603 if (!This->image_data)
605 LeaveCriticalSection(&This->lock);
606 return E_OUTOFMEMORY;
610 This->cinfo.client_data = jmpbuf;
612 if (setjmp(jmpbuf))
614 LeaveCriticalSection(&This->lock);
615 return E_FAIL;
618 while (max_row_needed > This->cinfo.output_scanline)
620 UINT first_scanline = This->cinfo.output_scanline;
621 UINT max_rows;
622 JSAMPROW out_rows[4];
623 UINT i;
624 JDIMENSION ret;
626 max_rows = min(This->cinfo.output_height-first_scanline, 4);
627 for (i=0; i<max_rows; i++)
628 out_rows[i] = This->image_data + stride * (first_scanline+i);
630 ret = pjpeg_read_scanlines(&This->cinfo, out_rows, max_rows);
632 if (ret == 0)
634 ERR("read_scanlines failed\n");
635 LeaveCriticalSection(&This->lock);
636 return E_FAIL;
639 if (bpp == 24)
641 /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
642 reverse_bgr8(3, This->image_data + stride * first_scanline,
643 This->cinfo.output_width, This->cinfo.output_scanline - first_scanline,
644 stride);
647 if (This->cinfo.out_color_space == JCS_CMYK && This->cinfo.saw_Adobe_marker)
648 /* Adobe JPEG's have inverted CMYK data. */
649 for (i=0; i<data_size; i++)
650 This->image_data[i] ^= 0xff;
653 LeaveCriticalSection(&This->lock);
655 return copy_pixels(bpp, This->image_data,
656 This->cinfo.output_width, This->cinfo.output_height, stride,
657 prc, cbStride, cbBufferSize, pbBuffer);
660 static HRESULT WINAPI JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
661 IWICMetadataQueryReader **ppIMetadataQueryReader)
663 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
664 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
667 static HRESULT WINAPI JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
668 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
670 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
671 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
674 static HRESULT WINAPI JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
675 IWICBitmapSource **ppIThumbnail)
677 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
678 return WINCODEC_ERR_CODECNOTHUMBNAIL;
681 static const IWICBitmapFrameDecodeVtbl JpegDecoder_Frame_Vtbl = {
682 JpegDecoder_Frame_QueryInterface,
683 JpegDecoder_Frame_AddRef,
684 JpegDecoder_Frame_Release,
685 JpegDecoder_Frame_GetSize,
686 JpegDecoder_Frame_GetPixelFormat,
687 JpegDecoder_Frame_GetResolution,
688 JpegDecoder_Frame_CopyPalette,
689 JpegDecoder_Frame_CopyPixels,
690 JpegDecoder_Frame_GetMetadataQueryReader,
691 JpegDecoder_Frame_GetColorContexts,
692 JpegDecoder_Frame_GetThumbnail
695 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
697 JpegDecoder *This;
698 HRESULT ret;
700 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
702 if (!libjpeg_handle && !load_libjpeg())
704 ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG);
705 return E_FAIL;
708 *ppv = NULL;
710 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
712 This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder));
713 if (!This) return E_OUTOFMEMORY;
715 This->IWICBitmapDecoder_iface.lpVtbl = &JpegDecoder_Vtbl;
716 This->IWICBitmapFrameDecode_iface.lpVtbl = &JpegDecoder_Frame_Vtbl;
717 This->ref = 1;
718 This->initialized = FALSE;
719 This->cinfo_initialized = FALSE;
720 This->stream = NULL;
721 This->image_data = NULL;
722 InitializeCriticalSection(&This->lock);
723 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JpegDecoder.lock");
725 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
726 IUnknown_Release((IUnknown*)This);
728 return ret;
731 typedef struct jpeg_compress_format {
732 const WICPixelFormatGUID *guid;
733 int bpp;
734 int num_components;
735 J_COLOR_SPACE color_space;
736 int swap_rgb;
737 } jpeg_compress_format;
739 static const jpeg_compress_format compress_formats[] = {
740 { &GUID_WICPixelFormat24bppBGR, 24, 3, JCS_RGB, 1 },
741 { &GUID_WICPixelFormat32bppCMYK, 32, 4, JCS_CMYK },
742 { &GUID_WICPixelFormat8bppGray, 8, 1, JCS_GRAYSCALE },
743 { 0 }
746 typedef struct JpegEncoder {
747 IWICBitmapEncoder IWICBitmapEncoder_iface;
748 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
749 LONG ref;
750 struct jpeg_compress_struct cinfo;
751 struct jpeg_error_mgr jerr;
752 struct jpeg_destination_mgr dest_mgr;
753 int initialized;
754 int frame_count;
755 int frame_initialized;
756 int started_compress;
757 int lines_written;
758 int frame_committed;
759 int committed;
760 UINT width, height;
761 double xres, yres;
762 const jpeg_compress_format *format;
763 IStream *stream;
764 CRITICAL_SECTION lock;
765 BYTE dest_buffer[1024];
766 } JpegEncoder;
768 static inline JpegEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
770 return CONTAINING_RECORD(iface, JpegEncoder, IWICBitmapEncoder_iface);
773 static inline JpegEncoder *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
775 return CONTAINING_RECORD(iface, JpegEncoder, IWICBitmapFrameEncode_iface);
778 static inline JpegEncoder *encoder_from_compress(j_compress_ptr compress)
780 return CONTAINING_RECORD(compress, JpegEncoder, cinfo);
783 static void dest_mgr_init_destination(j_compress_ptr cinfo)
785 JpegEncoder *This = encoder_from_compress(cinfo);
787 This->dest_mgr.next_output_byte = This->dest_buffer;
788 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
791 static jpeg_boolean dest_mgr_empty_output_buffer(j_compress_ptr cinfo)
793 JpegEncoder *This = encoder_from_compress(cinfo);
794 HRESULT hr;
795 ULONG byteswritten;
797 hr = IStream_Write(This->stream, This->dest_buffer,
798 sizeof(This->dest_buffer), &byteswritten);
800 if (hr != S_OK || byteswritten == 0)
802 ERR("Failed writing data, hr=%x\n", hr);
803 return FALSE;
806 This->dest_mgr.next_output_byte = This->dest_buffer;
807 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
808 return TRUE;
811 static void dest_mgr_term_destination(j_compress_ptr cinfo)
813 JpegEncoder *This = encoder_from_compress(cinfo);
814 ULONG byteswritten;
815 HRESULT hr;
817 if (This->dest_mgr.free_in_buffer != sizeof(This->dest_buffer))
819 hr = IStream_Write(This->stream, This->dest_buffer,
820 sizeof(This->dest_buffer) - This->dest_mgr.free_in_buffer, &byteswritten);
822 if (hr != S_OK || byteswritten == 0)
823 ERR("Failed writing data, hr=%x\n", hr);
827 static HRESULT WINAPI JpegEncoder_Frame_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
828 void **ppv)
830 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
831 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
833 if (!ppv) return E_INVALIDARG;
835 if (IsEqualIID(&IID_IUnknown, iid) ||
836 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
838 *ppv = &This->IWICBitmapFrameEncode_iface;
840 else
842 *ppv = NULL;
843 return E_NOINTERFACE;
846 IUnknown_AddRef((IUnknown*)*ppv);
847 return S_OK;
850 static ULONG WINAPI JpegEncoder_Frame_AddRef(IWICBitmapFrameEncode *iface)
852 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
853 return IWICBitmapEncoder_AddRef(&This->IWICBitmapEncoder_iface);
856 static ULONG WINAPI JpegEncoder_Frame_Release(IWICBitmapFrameEncode *iface)
858 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
859 return IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
862 static HRESULT WINAPI JpegEncoder_Frame_Initialize(IWICBitmapFrameEncode *iface,
863 IPropertyBag2 *pIEncoderOptions)
865 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
866 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
868 EnterCriticalSection(&This->lock);
870 if (This->frame_initialized)
872 LeaveCriticalSection(&This->lock);
873 return WINCODEC_ERR_WRONGSTATE;
876 This->frame_initialized = TRUE;
878 LeaveCriticalSection(&This->lock);
880 return S_OK;
883 static HRESULT WINAPI JpegEncoder_Frame_SetSize(IWICBitmapFrameEncode *iface,
884 UINT uiWidth, UINT uiHeight)
886 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
887 TRACE("(%p,%u,%u)\n", iface, uiWidth, uiHeight);
889 EnterCriticalSection(&This->lock);
891 if (!This->frame_initialized || This->started_compress)
893 LeaveCriticalSection(&This->lock);
894 return WINCODEC_ERR_WRONGSTATE;
897 This->width = uiWidth;
898 This->height = uiHeight;
900 LeaveCriticalSection(&This->lock);
902 return S_OK;
905 static HRESULT WINAPI JpegEncoder_Frame_SetResolution(IWICBitmapFrameEncode *iface,
906 double dpiX, double dpiY)
908 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
909 TRACE("(%p,%0.2f,%0.2f)\n", iface, dpiX, dpiY);
911 EnterCriticalSection(&This->lock);
913 if (!This->frame_initialized || This->started_compress)
915 LeaveCriticalSection(&This->lock);
916 return WINCODEC_ERR_WRONGSTATE;
919 This->xres = dpiX;
920 This->yres = dpiY;
922 LeaveCriticalSection(&This->lock);
924 return S_OK;
927 static HRESULT WINAPI JpegEncoder_Frame_SetPixelFormat(IWICBitmapFrameEncode *iface,
928 WICPixelFormatGUID *pPixelFormat)
930 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
931 int i;
932 TRACE("(%p,%s)\n", iface, debugstr_guid(pPixelFormat));
934 EnterCriticalSection(&This->lock);
936 if (!This->frame_initialized || This->started_compress)
938 LeaveCriticalSection(&This->lock);
939 return WINCODEC_ERR_WRONGSTATE;
942 for (i=0; compress_formats[i].guid; i++)
944 if (memcmp(compress_formats[i].guid, pPixelFormat, sizeof(GUID)) == 0)
945 break;
948 if (!compress_formats[i].guid) i = 0;
950 This->format = &compress_formats[i];
951 memcpy(pPixelFormat, This->format->guid, sizeof(GUID));
953 LeaveCriticalSection(&This->lock);
955 return S_OK;
958 static HRESULT WINAPI JpegEncoder_Frame_SetColorContexts(IWICBitmapFrameEncode *iface,
959 UINT cCount, IWICColorContext **ppIColorContext)
961 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
962 return E_NOTIMPL;
965 static HRESULT WINAPI JpegEncoder_Frame_SetPalette(IWICBitmapFrameEncode *iface,
966 IWICPalette *pIPalette)
968 FIXME("(%p,%p): stub\n", iface, pIPalette);
969 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
972 static HRESULT WINAPI JpegEncoder_Frame_SetThumbnail(IWICBitmapFrameEncode *iface,
973 IWICBitmapSource *pIThumbnail)
975 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
976 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
979 static HRESULT WINAPI JpegEncoder_Frame_WritePixels(IWICBitmapFrameEncode *iface,
980 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
982 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
983 jmp_buf jmpbuf;
984 BYTE *swapped_data = NULL, *current_row;
985 int line, row_size;
986 TRACE("(%p,%u,%u,%u,%p)\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
988 EnterCriticalSection(&This->lock);
990 if (!This->frame_initialized || !This->width || !This->height || !This->format)
992 LeaveCriticalSection(&This->lock);
993 return WINCODEC_ERR_WRONGSTATE;
996 if (lineCount == 0 || lineCount + This->lines_written > This->height)
998 LeaveCriticalSection(&This->lock);
999 return E_INVALIDARG;
1002 /* set up setjmp/longjmp error handling */
1003 if (setjmp(jmpbuf))
1005 LeaveCriticalSection(&This->lock);
1006 HeapFree(GetProcessHeap(), 0, swapped_data);
1007 return E_FAIL;
1009 This->cinfo.client_data = &jmpbuf;
1011 if (!This->started_compress)
1013 This->cinfo.image_width = This->width;
1014 This->cinfo.image_height = This->height;
1015 This->cinfo.input_components = This->format->num_components;
1016 This->cinfo.in_color_space = This->format->color_space;
1018 pjpeg_set_defaults(&This->cinfo);
1020 if (This->xres != 0.0 && This->yres != 0.0)
1022 This->cinfo.density_unit = 1; /* dots per inch */
1023 This->cinfo.X_density = This->xres;
1024 This->cinfo.Y_density = This->yres;
1027 pjpeg_start_compress(&This->cinfo, TRUE);
1029 This->started_compress = 1;
1032 row_size = This->format->bpp / 8 * This->width;
1034 if (This->format->swap_rgb)
1036 swapped_data = HeapAlloc(GetProcessHeap(), 0, row_size);
1037 if (!swapped_data)
1039 LeaveCriticalSection(&This->lock);
1040 return E_OUTOFMEMORY;
1044 for (line=0; line < lineCount; line++)
1046 if (This->format->swap_rgb)
1048 int x;
1050 memcpy(swapped_data, pbPixels + (cbStride * line), row_size);
1052 for (x=0; x < This->width; x++)
1054 BYTE b;
1056 b = swapped_data[x*3];
1057 swapped_data[x*3] = swapped_data[x*3+2];
1058 swapped_data[x*3+2] = b;
1061 current_row = swapped_data;
1063 else
1064 current_row = pbPixels + (cbStride * line);
1066 if (!pjpeg_write_scanlines(&This->cinfo, &current_row, 1))
1068 ERR("failed writing scanlines\n");
1069 LeaveCriticalSection(&This->lock);
1070 HeapFree(GetProcessHeap(), 0, swapped_data);
1071 return E_FAIL;
1074 This->lines_written++;
1077 LeaveCriticalSection(&This->lock);
1078 HeapFree(GetProcessHeap(), 0, swapped_data);
1080 return S_OK;
1083 static HRESULT WINAPI JpegEncoder_Frame_WriteSource(IWICBitmapFrameEncode *iface,
1084 IWICBitmapSource *pIBitmapSource, WICRect *prc)
1086 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1087 HRESULT hr;
1088 WICRect rc;
1089 WICPixelFormatGUID guid;
1090 UINT stride;
1091 BYTE *pixeldata;
1092 TRACE("(%p,%p,%p)\n", iface, pIBitmapSource, prc);
1094 if (!This->frame_initialized || !This->width || !This->height)
1095 return WINCODEC_ERR_WRONGSTATE;
1097 if (!This->format)
1099 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1100 if (FAILED(hr)) return hr;
1101 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &guid);
1102 if (FAILED(hr)) return hr;
1105 hr = IWICBitmapSource_GetPixelFormat(pIBitmapSource, &guid);
1106 if (FAILED(hr)) return hr;
1107 if (memcmp(&guid, This->format->guid, sizeof(GUID)) != 0)
1109 /* FIXME: should use WICConvertBitmapSource to convert */
1110 ERR("format %s unsupported\n", debugstr_guid(&guid));
1111 return E_FAIL;
1114 if (This->xres == 0.0 || This->yres == 0.0)
1116 double xres, yres;
1117 hr = IWICBitmapSource_GetResolution(pIBitmapSource, &xres, &yres);
1118 if (FAILED(hr)) return hr;
1119 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
1120 if (FAILED(hr)) return hr;
1123 if (!prc)
1125 UINT width, height;
1126 hr = IWICBitmapSource_GetSize(pIBitmapSource, &width, &height);
1127 if (FAILED(hr)) return hr;
1128 rc.X = 0;
1129 rc.Y = 0;
1130 rc.Width = width;
1131 rc.Height = height;
1132 prc = &rc;
1135 if (prc->Width != This->width) return E_INVALIDARG;
1137 stride = (This->format->bpp * This->width + 7)/8;
1139 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
1140 if (!pixeldata) return E_OUTOFMEMORY;
1142 hr = IWICBitmapSource_CopyPixels(pIBitmapSource, prc, stride,
1143 stride*prc->Height, pixeldata);
1145 if (SUCCEEDED(hr))
1147 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
1148 stride*prc->Height, pixeldata);
1151 HeapFree(GetProcessHeap(), 0, pixeldata);
1153 return hr;
1156 static HRESULT WINAPI JpegEncoder_Frame_Commit(IWICBitmapFrameEncode *iface)
1158 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
1159 jmp_buf jmpbuf;
1160 TRACE("(%p)\n", iface);
1162 EnterCriticalSection(&This->lock);
1164 if (!This->started_compress || This->lines_written != This->height || This->frame_committed)
1166 LeaveCriticalSection(&This->lock);
1167 return WINCODEC_ERR_WRONGSTATE;
1170 /* set up setjmp/longjmp error handling */
1171 if (setjmp(jmpbuf))
1173 LeaveCriticalSection(&This->lock);
1174 return E_FAIL;
1176 This->cinfo.client_data = &jmpbuf;
1178 pjpeg_finish_compress(&This->cinfo);
1180 This->frame_committed = TRUE;
1182 LeaveCriticalSection(&This->lock);
1184 return S_OK;
1187 static HRESULT WINAPI JpegEncoder_Frame_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
1188 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1190 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
1191 return E_NOTIMPL;
1194 static const IWICBitmapFrameEncodeVtbl JpegEncoder_FrameVtbl = {
1195 JpegEncoder_Frame_QueryInterface,
1196 JpegEncoder_Frame_AddRef,
1197 JpegEncoder_Frame_Release,
1198 JpegEncoder_Frame_Initialize,
1199 JpegEncoder_Frame_SetSize,
1200 JpegEncoder_Frame_SetResolution,
1201 JpegEncoder_Frame_SetPixelFormat,
1202 JpegEncoder_Frame_SetColorContexts,
1203 JpegEncoder_Frame_SetPalette,
1204 JpegEncoder_Frame_SetThumbnail,
1205 JpegEncoder_Frame_WritePixels,
1206 JpegEncoder_Frame_WriteSource,
1207 JpegEncoder_Frame_Commit,
1208 JpegEncoder_Frame_GetMetadataQueryWriter
1211 static HRESULT WINAPI JpegEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
1212 void **ppv)
1214 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1215 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
1217 if (!ppv) return E_INVALIDARG;
1219 if (IsEqualIID(&IID_IUnknown, iid) ||
1220 IsEqualIID(&IID_IWICBitmapEncoder, iid))
1222 *ppv = This;
1224 else
1226 *ppv = NULL;
1227 return E_NOINTERFACE;
1230 IUnknown_AddRef((IUnknown*)*ppv);
1231 return S_OK;
1234 static ULONG WINAPI JpegEncoder_AddRef(IWICBitmapEncoder *iface)
1236 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1237 ULONG ref = InterlockedIncrement(&This->ref);
1239 TRACE("(%p) refcount=%u\n", iface, ref);
1241 return ref;
1244 static ULONG WINAPI JpegEncoder_Release(IWICBitmapEncoder *iface)
1246 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1247 ULONG ref = InterlockedDecrement(&This->ref);
1249 TRACE("(%p) refcount=%u\n", iface, ref);
1251 if (ref == 0)
1253 This->lock.DebugInfo->Spare[0] = 0;
1254 DeleteCriticalSection(&This->lock);
1255 if (This->initialized) pjpeg_destroy_compress(&This->cinfo);
1256 if (This->stream) IStream_Release(This->stream);
1257 HeapFree(GetProcessHeap(), 0, This);
1260 return ref;
1263 static HRESULT WINAPI JpegEncoder_Initialize(IWICBitmapEncoder *iface,
1264 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
1266 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1267 jmp_buf jmpbuf;
1269 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
1271 EnterCriticalSection(&This->lock);
1273 if (This->initialized)
1275 LeaveCriticalSection(&This->lock);
1276 return WINCODEC_ERR_WRONGSTATE;
1279 pjpeg_std_error(&This->jerr);
1281 This->jerr.error_exit = error_exit_fn;
1282 This->jerr.emit_message = emit_message_fn;
1284 This->cinfo.err = &This->jerr;
1286 This->cinfo.client_data = &jmpbuf;
1288 if (setjmp(jmpbuf))
1290 LeaveCriticalSection(&This->lock);
1291 return E_FAIL;
1294 pjpeg_CreateCompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_compress_struct));
1296 This->stream = pIStream;
1297 IStream_AddRef(pIStream);
1299 This->dest_mgr.next_output_byte = This->dest_buffer;
1300 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
1302 This->dest_mgr.init_destination = dest_mgr_init_destination;
1303 This->dest_mgr.empty_output_buffer = dest_mgr_empty_output_buffer;
1304 This->dest_mgr.term_destination = dest_mgr_term_destination;
1306 This->cinfo.dest = &This->dest_mgr;
1308 This->initialized = TRUE;
1310 LeaveCriticalSection(&This->lock);
1312 return S_OK;
1315 static HRESULT WINAPI JpegEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1316 GUID *pguidContainerFormat)
1318 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1319 return E_NOTIMPL;
1322 static HRESULT WINAPI JpegEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1323 IWICBitmapEncoderInfo **ppIEncoderInfo)
1325 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1326 return E_NOTIMPL;
1329 static HRESULT WINAPI JpegEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1330 UINT cCount, IWICColorContext **ppIColorContext)
1332 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1333 return E_NOTIMPL;
1336 static HRESULT WINAPI JpegEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1338 TRACE("(%p,%p)\n", iface, pIPalette);
1339 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1342 static HRESULT WINAPI JpegEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1344 TRACE("(%p,%p)\n", iface, pIThumbnail);
1345 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1348 static HRESULT WINAPI JpegEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1350 TRACE("(%p,%p)\n", iface, pIPreview);
1351 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1354 static HRESULT WINAPI JpegEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1355 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1357 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1358 HRESULT hr;
1360 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1362 EnterCriticalSection(&This->lock);
1364 if (This->frame_count != 0)
1366 LeaveCriticalSection(&This->lock);
1367 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1370 if (!This->initialized)
1372 LeaveCriticalSection(&This->lock);
1373 return WINCODEC_ERR_NOTINITIALIZED;
1376 hr = CreatePropertyBag2(ppIEncoderOptions);
1377 if (FAILED(hr))
1379 LeaveCriticalSection(&This->lock);
1380 return hr;
1383 This->frame_count = 1;
1385 LeaveCriticalSection(&This->lock);
1387 IWICBitmapEncoder_AddRef(iface);
1388 *ppIFrameEncode = &This->IWICBitmapFrameEncode_iface;
1390 return S_OK;
1393 static HRESULT WINAPI JpegEncoder_Commit(IWICBitmapEncoder *iface)
1395 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1396 TRACE("(%p)\n", iface);
1398 EnterCriticalSection(&This->lock);
1400 if (!This->frame_committed || This->committed)
1402 LeaveCriticalSection(&This->lock);
1403 return WINCODEC_ERR_WRONGSTATE;
1406 This->committed = TRUE;
1408 LeaveCriticalSection(&This->lock);
1410 return S_OK;
1413 static HRESULT WINAPI JpegEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1414 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1416 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1417 return E_NOTIMPL;
1420 static const IWICBitmapEncoderVtbl JpegEncoder_Vtbl = {
1421 JpegEncoder_QueryInterface,
1422 JpegEncoder_AddRef,
1423 JpegEncoder_Release,
1424 JpegEncoder_Initialize,
1425 JpegEncoder_GetContainerFormat,
1426 JpegEncoder_GetEncoderInfo,
1427 JpegEncoder_SetColorContexts,
1428 JpegEncoder_SetPalette,
1429 JpegEncoder_SetThumbnail,
1430 JpegEncoder_SetPreview,
1431 JpegEncoder_CreateNewFrame,
1432 JpegEncoder_Commit,
1433 JpegEncoder_GetMetadataQueryWriter
1436 HRESULT JpegEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1438 JpegEncoder *This;
1439 HRESULT ret;
1441 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1443 *ppv = NULL;
1445 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1447 if (!libjpeg_handle && !load_libjpeg())
1449 ERR("Failed writing JPEG because unable to find %s\n",SONAME_LIBJPEG);
1450 return E_FAIL;
1453 This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegEncoder));
1454 if (!This) return E_OUTOFMEMORY;
1456 This->IWICBitmapEncoder_iface.lpVtbl = &JpegEncoder_Vtbl;
1457 This->IWICBitmapFrameEncode_iface.lpVtbl = &JpegEncoder_FrameVtbl;
1458 This->ref = 1;
1459 This->initialized = 0;
1460 This->frame_count = 0;
1461 This->frame_initialized = 0;
1462 This->started_compress = 0;
1463 This->lines_written = 0;
1464 This->frame_committed = 0;
1465 This->committed = 0;
1466 This->width = This->height = 0;
1467 This->xres = This->yres = 0.0;
1468 This->format = NULL;
1469 This->stream = NULL;
1470 InitializeCriticalSection(&This->lock);
1471 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JpegEncoder.lock");
1473 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1474 IUnknown_Release((IUnknown*)This);
1476 return ret;
1479 #else /* !defined(SONAME_LIBJPEG) */
1481 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1483 ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");
1484 return E_FAIL;
1487 HRESULT JpegEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1489 ERR("Trying to save JPEG picture, but JPEG support is not compiled in.\n");
1490 return E_FAIL;
1493 #endif