windowscodecs: Implement JpegEncoder_Frame_Initialize.
[wine/multimedia.git] / dlls / windowscodecs / jpegformat.c
blob2b4b14c72b52a219e9d8751d78c843bb8f887974
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_read_header);
70 MAKE_FUNCPTR(jpeg_read_scanlines);
71 MAKE_FUNCPTR(jpeg_resync_to_restart);
72 MAKE_FUNCPTR(jpeg_start_decompress);
73 MAKE_FUNCPTR(jpeg_std_error);
74 #undef MAKE_FUNCPTR
76 static void *load_libjpeg(void)
78 if((libjpeg_handle = wine_dlopen(SONAME_LIBJPEG, RTLD_NOW, NULL, 0)) != NULL) {
80 #define LOAD_FUNCPTR(f) \
81 if((p##f = wine_dlsym(libjpeg_handle, #f, NULL, 0)) == NULL) { \
82 libjpeg_handle = NULL; \
83 return NULL; \
86 LOAD_FUNCPTR(jpeg_CreateCompress);
87 LOAD_FUNCPTR(jpeg_CreateDecompress);
88 LOAD_FUNCPTR(jpeg_destroy_compress);
89 LOAD_FUNCPTR(jpeg_destroy_decompress);
90 LOAD_FUNCPTR(jpeg_read_header);
91 LOAD_FUNCPTR(jpeg_read_scanlines);
92 LOAD_FUNCPTR(jpeg_resync_to_restart);
93 LOAD_FUNCPTR(jpeg_start_decompress);
94 LOAD_FUNCPTR(jpeg_std_error);
95 #undef LOAD_FUNCPTR
97 return libjpeg_handle;
100 static void error_exit_fn(j_common_ptr cinfo)
102 char message[JMSG_LENGTH_MAX];
103 if (ERR_ON(jpeg))
105 cinfo->err->format_message(cinfo, message);
106 ERR_(jpeg)("%s\n", message);
108 longjmp(*(jmp_buf*)cinfo->client_data, 1);
111 static void emit_message_fn(j_common_ptr cinfo, int msg_level)
113 char message[JMSG_LENGTH_MAX];
115 if (msg_level < 0 && ERR_ON(jpeg))
117 cinfo->err->format_message(cinfo, message);
118 ERR_(jpeg)("%s\n", message);
120 else if (msg_level == 0 && WARN_ON(jpeg))
122 cinfo->err->format_message(cinfo, message);
123 WARN_(jpeg)("%s\n", message);
125 else if (msg_level > 0 && TRACE_ON(jpeg))
127 cinfo->err->format_message(cinfo, message);
128 TRACE_(jpeg)("%s\n", message);
132 typedef struct {
133 IWICBitmapDecoder IWICBitmapDecoder_iface;
134 IWICBitmapFrameDecode IWICBitmapFrameDecode_iface;
135 LONG ref;
136 BOOL initialized;
137 BOOL cinfo_initialized;
138 IStream *stream;
139 struct jpeg_decompress_struct cinfo;
140 struct jpeg_error_mgr jerr;
141 struct jpeg_source_mgr source_mgr;
142 BYTE source_buffer[1024];
143 BYTE *image_data;
144 CRITICAL_SECTION lock;
145 } JpegDecoder;
147 static inline JpegDecoder *impl_from_IWICBitmapDecoder(IWICBitmapDecoder *iface)
149 return CONTAINING_RECORD(iface, JpegDecoder, IWICBitmapDecoder_iface);
152 static inline JpegDecoder *impl_from_IWICBitmapFrameDecode(IWICBitmapFrameDecode *iface)
154 return CONTAINING_RECORD(iface, JpegDecoder, IWICBitmapFrameDecode_iface);
157 static inline JpegDecoder *decoder_from_decompress(j_decompress_ptr decompress)
159 return CONTAINING_RECORD(decompress, JpegDecoder, cinfo);
162 static HRESULT WINAPI JpegDecoder_QueryInterface(IWICBitmapDecoder *iface, REFIID iid,
163 void **ppv)
165 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
166 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
168 if (!ppv) return E_INVALIDARG;
170 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IWICBitmapDecoder, iid))
172 *ppv = This;
174 else
176 *ppv = NULL;
177 return E_NOINTERFACE;
180 IUnknown_AddRef((IUnknown*)*ppv);
181 return S_OK;
184 static ULONG WINAPI JpegDecoder_AddRef(IWICBitmapDecoder *iface)
186 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
187 ULONG ref = InterlockedIncrement(&This->ref);
189 TRACE("(%p) refcount=%u\n", iface, ref);
191 return ref;
194 static ULONG WINAPI JpegDecoder_Release(IWICBitmapDecoder *iface)
196 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
197 ULONG ref = InterlockedDecrement(&This->ref);
199 TRACE("(%p) refcount=%u\n", iface, ref);
201 if (ref == 0)
203 This->lock.DebugInfo->Spare[0] = 0;
204 DeleteCriticalSection(&This->lock);
205 if (This->cinfo_initialized) pjpeg_destroy_decompress(&This->cinfo);
206 if (This->stream) IStream_Release(This->stream);
207 HeapFree(GetProcessHeap(), 0, This->image_data);
208 HeapFree(GetProcessHeap(), 0, This);
211 return ref;
214 static HRESULT WINAPI JpegDecoder_QueryCapability(IWICBitmapDecoder *iface, IStream *pIStream,
215 DWORD *pdwCapability)
217 FIXME("(%p,%p,%p): stub\n", iface, pIStream, pdwCapability);
218 return E_NOTIMPL;
221 static void source_mgr_init_source(j_decompress_ptr cinfo)
225 static jpeg_boolean source_mgr_fill_input_buffer(j_decompress_ptr cinfo)
227 JpegDecoder *This = decoder_from_decompress(cinfo);
228 HRESULT hr;
229 ULONG bytesread;
231 hr = IStream_Read(This->stream, This->source_buffer, 1024, &bytesread);
233 if (hr != S_OK || bytesread == 0)
235 return FALSE;
237 else
239 This->source_mgr.next_input_byte = This->source_buffer;
240 This->source_mgr.bytes_in_buffer = bytesread;
241 return TRUE;
245 static void source_mgr_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
247 JpegDecoder *This = decoder_from_decompress(cinfo);
248 LARGE_INTEGER seek;
250 if (num_bytes > This->source_mgr.bytes_in_buffer)
252 seek.QuadPart = num_bytes - This->source_mgr.bytes_in_buffer;
253 IStream_Seek(This->stream, seek, STREAM_SEEK_CUR, NULL);
254 This->source_mgr.bytes_in_buffer = 0;
256 else if (num_bytes > 0)
258 This->source_mgr.next_input_byte += num_bytes;
259 This->source_mgr.bytes_in_buffer -= num_bytes;
263 static void source_mgr_term_source(j_decompress_ptr cinfo)
267 static HRESULT WINAPI JpegDecoder_Initialize(IWICBitmapDecoder *iface, IStream *pIStream,
268 WICDecodeOptions cacheOptions)
270 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
271 int ret;
272 LARGE_INTEGER seek;
273 jmp_buf jmpbuf;
274 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOptions);
276 EnterCriticalSection(&This->lock);
278 if (This->cinfo_initialized)
280 LeaveCriticalSection(&This->lock);
281 return WINCODEC_ERR_WRONGSTATE;
284 pjpeg_std_error(&This->jerr);
286 This->jerr.error_exit = error_exit_fn;
287 This->jerr.emit_message = emit_message_fn;
289 This->cinfo.err = &This->jerr;
291 This->cinfo.client_data = jmpbuf;
293 if (setjmp(jmpbuf))
295 LeaveCriticalSection(&This->lock);
296 return E_FAIL;
299 pjpeg_CreateDecompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_decompress_struct));
301 This->cinfo_initialized = TRUE;
303 This->stream = pIStream;
304 IStream_AddRef(pIStream);
306 seek.QuadPart = 0;
307 IStream_Seek(This->stream, seek, STREAM_SEEK_SET, NULL);
309 This->source_mgr.bytes_in_buffer = 0;
310 This->source_mgr.init_source = source_mgr_init_source;
311 This->source_mgr.fill_input_buffer = source_mgr_fill_input_buffer;
312 This->source_mgr.skip_input_data = source_mgr_skip_input_data;
313 This->source_mgr.resync_to_restart = pjpeg_resync_to_restart;
314 This->source_mgr.term_source = source_mgr_term_source;
316 This->cinfo.src = &This->source_mgr;
318 ret = pjpeg_read_header(&This->cinfo, TRUE);
320 if (ret != JPEG_HEADER_OK) {
321 WARN("Jpeg image in stream has bad format, read header returned %d.\n",ret);
322 LeaveCriticalSection(&This->lock);
323 return E_FAIL;
326 switch (This->cinfo.jpeg_color_space)
328 case JCS_GRAYSCALE:
329 This->cinfo.out_color_space = JCS_GRAYSCALE;
330 break;
331 case JCS_RGB:
332 case JCS_YCbCr:
333 This->cinfo.out_color_space = JCS_RGB;
334 break;
335 case JCS_CMYK:
336 case JCS_YCCK:
337 This->cinfo.out_color_space = JCS_CMYK;
338 break;
339 default:
340 ERR("Unknown JPEG color space %i\n", This->cinfo.jpeg_color_space);
341 LeaveCriticalSection(&This->lock);
342 return E_FAIL;
345 if (!pjpeg_start_decompress(&This->cinfo))
347 ERR("jpeg_start_decompress failed\n");
348 LeaveCriticalSection(&This->lock);
349 return E_FAIL;
352 This->initialized = TRUE;
354 LeaveCriticalSection(&This->lock);
356 return S_OK;
359 static HRESULT WINAPI JpegDecoder_GetContainerFormat(IWICBitmapDecoder *iface,
360 GUID *pguidContainerFormat)
362 memcpy(pguidContainerFormat, &GUID_ContainerFormatJpeg, sizeof(GUID));
363 return S_OK;
366 static HRESULT WINAPI JpegDecoder_GetDecoderInfo(IWICBitmapDecoder *iface,
367 IWICBitmapDecoderInfo **ppIDecoderInfo)
369 HRESULT hr;
370 IWICComponentInfo *compinfo;
372 TRACE("(%p,%p)\n", iface, ppIDecoderInfo);
374 hr = CreateComponentInfo(&CLSID_WICJpegDecoder, &compinfo);
375 if (FAILED(hr)) return hr;
377 hr = IWICComponentInfo_QueryInterface(compinfo, &IID_IWICBitmapDecoderInfo,
378 (void**)ppIDecoderInfo);
380 IWICComponentInfo_Release(compinfo);
382 return hr;
385 static HRESULT WINAPI JpegDecoder_CopyPalette(IWICBitmapDecoder *iface,
386 IWICPalette *pIPalette)
388 TRACE("(%p,%p)\n", iface, pIPalette);
390 return WINCODEC_ERR_PALETTEUNAVAILABLE;
393 static HRESULT WINAPI JpegDecoder_GetMetadataQueryReader(IWICBitmapDecoder *iface,
394 IWICMetadataQueryReader **ppIMetadataQueryReader)
396 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
397 return E_NOTIMPL;
400 static HRESULT WINAPI JpegDecoder_GetPreview(IWICBitmapDecoder *iface,
401 IWICBitmapSource **ppIBitmapSource)
403 FIXME("(%p,%p): stub\n", iface, ppIBitmapSource);
404 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
407 static HRESULT WINAPI JpegDecoder_GetColorContexts(IWICBitmapDecoder *iface,
408 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
410 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
411 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
414 static HRESULT WINAPI JpegDecoder_GetThumbnail(IWICBitmapDecoder *iface,
415 IWICBitmapSource **ppIThumbnail)
417 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
418 return WINCODEC_ERR_CODECNOTHUMBNAIL;
421 static HRESULT WINAPI JpegDecoder_GetFrameCount(IWICBitmapDecoder *iface,
422 UINT *pCount)
424 *pCount = 1;
425 return S_OK;
428 static HRESULT WINAPI JpegDecoder_GetFrame(IWICBitmapDecoder *iface,
429 UINT index, IWICBitmapFrameDecode **ppIBitmapFrame)
431 JpegDecoder *This = impl_from_IWICBitmapDecoder(iface);
432 TRACE("(%p,%u,%p)\n", iface, index, ppIBitmapFrame);
434 if (!This->initialized) return WINCODEC_ERR_NOTINITIALIZED;
436 if (index != 0) return E_INVALIDARG;
438 IWICBitmapDecoder_AddRef(iface);
439 *ppIBitmapFrame = &This->IWICBitmapFrameDecode_iface;
441 return S_OK;
444 static const IWICBitmapDecoderVtbl JpegDecoder_Vtbl = {
445 JpegDecoder_QueryInterface,
446 JpegDecoder_AddRef,
447 JpegDecoder_Release,
448 JpegDecoder_QueryCapability,
449 JpegDecoder_Initialize,
450 JpegDecoder_GetContainerFormat,
451 JpegDecoder_GetDecoderInfo,
452 JpegDecoder_CopyPalette,
453 JpegDecoder_GetMetadataQueryReader,
454 JpegDecoder_GetPreview,
455 JpegDecoder_GetColorContexts,
456 JpegDecoder_GetThumbnail,
457 JpegDecoder_GetFrameCount,
458 JpegDecoder_GetFrame
461 static HRESULT WINAPI JpegDecoder_Frame_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
462 void **ppv)
464 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
466 if (!ppv) return E_INVALIDARG;
468 if (IsEqualIID(&IID_IUnknown, iid) ||
469 IsEqualIID(&IID_IWICBitmapSource, iid) ||
470 IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
472 *ppv = iface;
474 else
476 *ppv = NULL;
477 return E_NOINTERFACE;
480 IUnknown_AddRef((IUnknown*)*ppv);
481 return S_OK;
484 static ULONG WINAPI JpegDecoder_Frame_AddRef(IWICBitmapFrameDecode *iface)
486 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
487 return IUnknown_AddRef((IUnknown*)This);
490 static ULONG WINAPI JpegDecoder_Frame_Release(IWICBitmapFrameDecode *iface)
492 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
493 return IUnknown_Release((IUnknown*)This);
496 static HRESULT WINAPI JpegDecoder_Frame_GetSize(IWICBitmapFrameDecode *iface,
497 UINT *puiWidth, UINT *puiHeight)
499 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
500 *puiWidth = This->cinfo.output_width;
501 *puiHeight = This->cinfo.output_height;
502 TRACE("(%p)->(%u,%u)\n", iface, *puiWidth, *puiHeight);
503 return S_OK;
506 static HRESULT WINAPI JpegDecoder_Frame_GetPixelFormat(IWICBitmapFrameDecode *iface,
507 WICPixelFormatGUID *pPixelFormat)
509 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
510 TRACE("(%p,%p)\n", iface, pPixelFormat);
511 if (This->cinfo.out_color_space == JCS_RGB)
512 memcpy(pPixelFormat, &GUID_WICPixelFormat24bppBGR, sizeof(GUID));
513 else if (This->cinfo.out_color_space == JCS_CMYK)
514 memcpy(pPixelFormat, &GUID_WICPixelFormat32bppCMYK, sizeof(GUID));
515 else /* This->cinfo.out_color_space == JCS_GRAYSCALE */
516 memcpy(pPixelFormat, &GUID_WICPixelFormat8bppGray, sizeof(GUID));
517 return S_OK;
520 static HRESULT WINAPI JpegDecoder_Frame_GetResolution(IWICBitmapFrameDecode *iface,
521 double *pDpiX, double *pDpiY)
523 FIXME("(%p,%p,%p): stub\n", iface, pDpiX, pDpiY);
524 return E_NOTIMPL;
527 static HRESULT WINAPI JpegDecoder_Frame_CopyPalette(IWICBitmapFrameDecode *iface,
528 IWICPalette *pIPalette)
530 FIXME("(%p,%p): stub\n", iface, pIPalette);
531 return E_NOTIMPL;
534 static HRESULT WINAPI JpegDecoder_Frame_CopyPixels(IWICBitmapFrameDecode *iface,
535 const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
537 JpegDecoder *This = impl_from_IWICBitmapFrameDecode(iface);
538 UINT bpp;
539 UINT stride;
540 UINT data_size;
541 UINT max_row_needed;
542 jmp_buf jmpbuf;
543 WICRect rect;
544 TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
546 if (!prc)
548 rect.X = 0;
549 rect.Y = 0;
550 rect.Width = This->cinfo.output_width;
551 rect.Height = This->cinfo.output_height;
552 prc = &rect;
554 else
556 if (prc->X < 0 || prc->Y < 0 || prc->X+prc->Width > This->cinfo.output_width ||
557 prc->Y+prc->Height > This->cinfo.output_height)
558 return E_INVALIDARG;
561 if (This->cinfo.out_color_space == JCS_GRAYSCALE) bpp = 8;
562 else if (This->cinfo.out_color_space == JCS_CMYK) bpp = 32;
563 else bpp = 24;
565 stride = bpp * This->cinfo.output_width;
566 data_size = stride * This->cinfo.output_height;
568 max_row_needed = prc->Y + prc->Height;
569 if (max_row_needed > This->cinfo.output_height) return E_INVALIDARG;
571 EnterCriticalSection(&This->lock);
573 if (!This->image_data)
575 This->image_data = HeapAlloc(GetProcessHeap(), 0, data_size);
576 if (!This->image_data)
578 LeaveCriticalSection(&This->lock);
579 return E_OUTOFMEMORY;
583 This->cinfo.client_data = jmpbuf;
585 if (setjmp(jmpbuf))
587 LeaveCriticalSection(&This->lock);
588 return E_FAIL;
591 while (max_row_needed > This->cinfo.output_scanline)
593 UINT first_scanline = This->cinfo.output_scanline;
594 UINT max_rows;
595 JSAMPROW out_rows[4];
596 UINT i;
597 JDIMENSION ret;
599 max_rows = min(This->cinfo.output_height-first_scanline, 4);
600 for (i=0; i<max_rows; i++)
601 out_rows[i] = This->image_data + stride * (first_scanline+i);
603 ret = pjpeg_read_scanlines(&This->cinfo, out_rows, max_rows);
605 if (ret == 0)
607 ERR("read_scanlines failed\n");
608 LeaveCriticalSection(&This->lock);
609 return E_FAIL;
612 if (bpp == 24)
614 /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
615 reverse_bgr8(3, This->image_data + stride * first_scanline,
616 This->cinfo.output_width, This->cinfo.output_scanline - first_scanline,
617 stride);
620 if (This->cinfo.out_color_space == JCS_CMYK && This->cinfo.saw_Adobe_marker)
621 /* Adobe JPEG's have inverted CMYK data. */
622 for (i=0; i<data_size; i++)
623 This->image_data[i] ^= 0xff;
626 LeaveCriticalSection(&This->lock);
628 return copy_pixels(bpp, This->image_data,
629 This->cinfo.output_width, This->cinfo.output_height, stride,
630 prc, cbStride, cbBufferSize, pbBuffer);
633 static HRESULT WINAPI JpegDecoder_Frame_GetMetadataQueryReader(IWICBitmapFrameDecode *iface,
634 IWICMetadataQueryReader **ppIMetadataQueryReader)
636 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryReader);
637 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
640 static HRESULT WINAPI JpegDecoder_Frame_GetColorContexts(IWICBitmapFrameDecode *iface,
641 UINT cCount, IWICColorContext **ppIColorContexts, UINT *pcActualCount)
643 FIXME("(%p,%u,%p,%p): stub\n", iface, cCount, ppIColorContexts, pcActualCount);
644 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
647 static HRESULT WINAPI JpegDecoder_Frame_GetThumbnail(IWICBitmapFrameDecode *iface,
648 IWICBitmapSource **ppIThumbnail)
650 FIXME("(%p,%p): stub\n", iface, ppIThumbnail);
651 return WINCODEC_ERR_CODECNOTHUMBNAIL;
654 static const IWICBitmapFrameDecodeVtbl JpegDecoder_Frame_Vtbl = {
655 JpegDecoder_Frame_QueryInterface,
656 JpegDecoder_Frame_AddRef,
657 JpegDecoder_Frame_Release,
658 JpegDecoder_Frame_GetSize,
659 JpegDecoder_Frame_GetPixelFormat,
660 JpegDecoder_Frame_GetResolution,
661 JpegDecoder_Frame_CopyPalette,
662 JpegDecoder_Frame_CopyPixels,
663 JpegDecoder_Frame_GetMetadataQueryReader,
664 JpegDecoder_Frame_GetColorContexts,
665 JpegDecoder_Frame_GetThumbnail
668 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
670 JpegDecoder *This;
671 HRESULT ret;
673 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
675 if (!libjpeg_handle && !load_libjpeg())
677 ERR("Failed reading JPEG because unable to find %s\n", SONAME_LIBJPEG);
678 return E_FAIL;
681 *ppv = NULL;
683 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
685 This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegDecoder));
686 if (!This) return E_OUTOFMEMORY;
688 This->IWICBitmapDecoder_iface.lpVtbl = &JpegDecoder_Vtbl;
689 This->IWICBitmapFrameDecode_iface.lpVtbl = &JpegDecoder_Frame_Vtbl;
690 This->ref = 1;
691 This->initialized = FALSE;
692 This->cinfo_initialized = FALSE;
693 This->stream = NULL;
694 This->image_data = NULL;
695 InitializeCriticalSection(&This->lock);
696 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JpegDecoder.lock");
698 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
699 IUnknown_Release((IUnknown*)This);
701 return ret;
704 typedef struct JpegEncoder {
705 IWICBitmapEncoder IWICBitmapEncoder_iface;
706 IWICBitmapFrameEncode IWICBitmapFrameEncode_iface;
707 LONG ref;
708 struct jpeg_compress_struct cinfo;
709 struct jpeg_error_mgr jerr;
710 struct jpeg_destination_mgr dest_mgr;
711 int initialized;
712 int frame_count;
713 int frame_initialized;
714 IStream *stream;
715 CRITICAL_SECTION lock;
716 BYTE dest_buffer[1024];
717 } JpegEncoder;
719 static inline JpegEncoder *impl_from_IWICBitmapEncoder(IWICBitmapEncoder *iface)
721 return CONTAINING_RECORD(iface, JpegEncoder, IWICBitmapEncoder_iface);
724 static inline JpegEncoder *impl_from_IWICBitmapFrameEncode(IWICBitmapFrameEncode *iface)
726 return CONTAINING_RECORD(iface, JpegEncoder, IWICBitmapFrameEncode_iface);
729 static inline JpegEncoder *encoder_from_compress(j_compress_ptr compress)
731 return CONTAINING_RECORD(compress, JpegEncoder, cinfo);
734 static void dest_mgr_init_destination(j_compress_ptr cinfo)
736 JpegEncoder *This = encoder_from_compress(cinfo);
738 This->dest_mgr.next_output_byte = This->dest_buffer;
739 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
742 static jpeg_boolean dest_mgr_empty_output_buffer(j_compress_ptr cinfo)
744 JpegEncoder *This = encoder_from_compress(cinfo);
745 HRESULT hr;
746 ULONG byteswritten;
748 hr = IStream_Write(This->stream, This->dest_buffer,
749 sizeof(This->dest_buffer), &byteswritten);
751 if (hr != S_OK || byteswritten == 0)
753 ERR("Failed writing data, hr=%x\n", hr);
754 return FALSE;
757 This->dest_mgr.next_output_byte = This->dest_buffer;
758 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
759 return TRUE;
762 static void dest_mgr_term_destination(j_compress_ptr cinfo)
764 JpegEncoder *This = encoder_from_compress(cinfo);
765 ULONG byteswritten;
766 HRESULT hr;
768 if (This->dest_mgr.free_in_buffer != sizeof(This->dest_buffer))
770 hr = IStream_Write(This->stream, This->dest_buffer,
771 sizeof(This->dest_buffer) - This->dest_mgr.free_in_buffer, &byteswritten);
773 if (hr != S_OK || byteswritten == 0)
774 ERR("Failed writing data, hr=%x\n", hr);
778 static HRESULT WINAPI JpegEncoder_Frame_QueryInterface(IWICBitmapFrameEncode *iface, REFIID iid,
779 void **ppv)
781 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
782 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
784 if (!ppv) return E_INVALIDARG;
786 if (IsEqualIID(&IID_IUnknown, iid) ||
787 IsEqualIID(&IID_IWICBitmapFrameEncode, iid))
789 *ppv = &This->IWICBitmapFrameEncode_iface;
791 else
793 *ppv = NULL;
794 return E_NOINTERFACE;
797 IUnknown_AddRef((IUnknown*)*ppv);
798 return S_OK;
801 static ULONG WINAPI JpegEncoder_Frame_AddRef(IWICBitmapFrameEncode *iface)
803 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
804 return IWICBitmapEncoder_AddRef(&This->IWICBitmapEncoder_iface);
807 static ULONG WINAPI JpegEncoder_Frame_Release(IWICBitmapFrameEncode *iface)
809 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
810 return IWICBitmapEncoder_Release(&This->IWICBitmapEncoder_iface);
813 static HRESULT WINAPI JpegEncoder_Frame_Initialize(IWICBitmapFrameEncode *iface,
814 IPropertyBag2 *pIEncoderOptions)
816 JpegEncoder *This = impl_from_IWICBitmapFrameEncode(iface);
817 TRACE("(%p,%p)\n", iface, pIEncoderOptions);
819 EnterCriticalSection(&This->lock);
821 if (This->frame_initialized)
823 LeaveCriticalSection(&This->lock);
824 return WINCODEC_ERR_WRONGSTATE;
827 This->frame_initialized = TRUE;
829 LeaveCriticalSection(&This->lock);
831 return S_OK;
834 static HRESULT WINAPI JpegEncoder_Frame_SetSize(IWICBitmapFrameEncode *iface,
835 UINT uiWidth, UINT uiHeight)
837 FIXME("(%p,%u,%u): stub\n", iface, uiWidth, uiHeight);
838 return E_NOTIMPL;
841 static HRESULT WINAPI JpegEncoder_Frame_SetResolution(IWICBitmapFrameEncode *iface,
842 double dpiX, double dpiY)
844 FIXME("(%p,%0.2f,%0.2f): stub\n", iface, dpiX, dpiY);
845 return E_NOTIMPL;
848 static HRESULT WINAPI JpegEncoder_Frame_SetPixelFormat(IWICBitmapFrameEncode *iface,
849 WICPixelFormatGUID *pPixelFormat)
851 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pPixelFormat));
852 return E_NOTIMPL;
855 static HRESULT WINAPI JpegEncoder_Frame_SetColorContexts(IWICBitmapFrameEncode *iface,
856 UINT cCount, IWICColorContext **ppIColorContext)
858 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
859 return E_NOTIMPL;
862 static HRESULT WINAPI JpegEncoder_Frame_SetPalette(IWICBitmapFrameEncode *iface,
863 IWICPalette *pIPalette)
865 FIXME("(%p,%p): stub\n", iface, pIPalette);
866 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
869 static HRESULT WINAPI JpegEncoder_Frame_SetThumbnail(IWICBitmapFrameEncode *iface,
870 IWICBitmapSource *pIThumbnail)
872 FIXME("(%p,%p): stub\n", iface, pIThumbnail);
873 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
876 static HRESULT WINAPI JpegEncoder_Frame_WritePixels(IWICBitmapFrameEncode *iface,
877 UINT lineCount, UINT cbStride, UINT cbBufferSize, BYTE *pbPixels)
879 FIXME("(%p,%u,%u,%u,%p): stub\n", iface, lineCount, cbStride, cbBufferSize, pbPixels);
880 return E_NOTIMPL;
883 static HRESULT WINAPI JpegEncoder_Frame_WriteSource(IWICBitmapFrameEncode *iface,
884 IWICBitmapSource *pIBitmapSource, WICRect *prc)
886 FIXME("(%p,%p,%p): stub\n", iface, pIBitmapSource, prc);
887 return E_NOTIMPL;
890 static HRESULT WINAPI JpegEncoder_Frame_Commit(IWICBitmapFrameEncode *iface)
892 FIXME("(%p): stub\n", iface);
893 return E_NOTIMPL;
896 static HRESULT WINAPI JpegEncoder_Frame_GetMetadataQueryWriter(IWICBitmapFrameEncode *iface,
897 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
899 FIXME("(%p, %p): stub\n", iface, ppIMetadataQueryWriter);
900 return E_NOTIMPL;
903 static const IWICBitmapFrameEncodeVtbl JpegEncoder_FrameVtbl = {
904 JpegEncoder_Frame_QueryInterface,
905 JpegEncoder_Frame_AddRef,
906 JpegEncoder_Frame_Release,
907 JpegEncoder_Frame_Initialize,
908 JpegEncoder_Frame_SetSize,
909 JpegEncoder_Frame_SetResolution,
910 JpegEncoder_Frame_SetPixelFormat,
911 JpegEncoder_Frame_SetColorContexts,
912 JpegEncoder_Frame_SetPalette,
913 JpegEncoder_Frame_SetThumbnail,
914 JpegEncoder_Frame_WritePixels,
915 JpegEncoder_Frame_WriteSource,
916 JpegEncoder_Frame_Commit,
917 JpegEncoder_Frame_GetMetadataQueryWriter
920 static HRESULT WINAPI JpegEncoder_QueryInterface(IWICBitmapEncoder *iface, REFIID iid,
921 void **ppv)
923 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
924 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
926 if (!ppv) return E_INVALIDARG;
928 if (IsEqualIID(&IID_IUnknown, iid) ||
929 IsEqualIID(&IID_IWICBitmapEncoder, iid))
931 *ppv = This;
933 else
935 *ppv = NULL;
936 return E_NOINTERFACE;
939 IUnknown_AddRef((IUnknown*)*ppv);
940 return S_OK;
943 static ULONG WINAPI JpegEncoder_AddRef(IWICBitmapEncoder *iface)
945 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
946 ULONG ref = InterlockedIncrement(&This->ref);
948 TRACE("(%p) refcount=%u\n", iface, ref);
950 return ref;
953 static ULONG WINAPI JpegEncoder_Release(IWICBitmapEncoder *iface)
955 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
956 ULONG ref = InterlockedDecrement(&This->ref);
958 TRACE("(%p) refcount=%u\n", iface, ref);
960 if (ref == 0)
962 This->lock.DebugInfo->Spare[0] = 0;
963 DeleteCriticalSection(&This->lock);
964 if (This->initialized) pjpeg_destroy_compress(&This->cinfo);
965 if (This->stream) IStream_Release(This->stream);
966 HeapFree(GetProcessHeap(), 0, This);
969 return ref;
972 static HRESULT WINAPI JpegEncoder_Initialize(IWICBitmapEncoder *iface,
973 IStream *pIStream, WICBitmapEncoderCacheOption cacheOption)
975 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
976 jmp_buf jmpbuf;
978 TRACE("(%p,%p,%u)\n", iface, pIStream, cacheOption);
980 EnterCriticalSection(&This->lock);
982 if (This->initialized)
984 LeaveCriticalSection(&This->lock);
985 return WINCODEC_ERR_WRONGSTATE;
988 pjpeg_std_error(&This->jerr);
990 This->jerr.error_exit = error_exit_fn;
991 This->jerr.emit_message = emit_message_fn;
993 This->cinfo.err = &This->jerr;
995 This->cinfo.client_data = &jmpbuf;
997 if (setjmp(jmpbuf))
999 LeaveCriticalSection(&This->lock);
1000 return E_FAIL;
1003 pjpeg_CreateCompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_compress_struct));
1005 This->stream = pIStream;
1006 IStream_AddRef(pIStream);
1008 This->dest_mgr.next_output_byte = This->dest_buffer;
1009 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
1011 This->dest_mgr.init_destination = dest_mgr_init_destination;
1012 This->dest_mgr.empty_output_buffer = dest_mgr_empty_output_buffer;
1013 This->dest_mgr.term_destination = dest_mgr_term_destination;
1015 This->cinfo.dest = &This->dest_mgr;
1017 This->initialized = TRUE;
1019 LeaveCriticalSection(&This->lock);
1021 return S_OK;
1024 static HRESULT WINAPI JpegEncoder_GetContainerFormat(IWICBitmapEncoder *iface,
1025 GUID *pguidContainerFormat)
1027 FIXME("(%p,%s): stub\n", iface, debugstr_guid(pguidContainerFormat));
1028 return E_NOTIMPL;
1031 static HRESULT WINAPI JpegEncoder_GetEncoderInfo(IWICBitmapEncoder *iface,
1032 IWICBitmapEncoderInfo **ppIEncoderInfo)
1034 FIXME("(%p,%p): stub\n", iface, ppIEncoderInfo);
1035 return E_NOTIMPL;
1038 static HRESULT WINAPI JpegEncoder_SetColorContexts(IWICBitmapEncoder *iface,
1039 UINT cCount, IWICColorContext **ppIColorContext)
1041 FIXME("(%p,%u,%p): stub\n", iface, cCount, ppIColorContext);
1042 return E_NOTIMPL;
1045 static HRESULT WINAPI JpegEncoder_SetPalette(IWICBitmapEncoder *iface, IWICPalette *pIPalette)
1047 TRACE("(%p,%p)\n", iface, pIPalette);
1048 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1051 static HRESULT WINAPI JpegEncoder_SetThumbnail(IWICBitmapEncoder *iface, IWICBitmapSource *pIThumbnail)
1053 TRACE("(%p,%p)\n", iface, pIThumbnail);
1054 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1057 static HRESULT WINAPI JpegEncoder_SetPreview(IWICBitmapEncoder *iface, IWICBitmapSource *pIPreview)
1059 TRACE("(%p,%p)\n", iface, pIPreview);
1060 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1063 static HRESULT WINAPI JpegEncoder_CreateNewFrame(IWICBitmapEncoder *iface,
1064 IWICBitmapFrameEncode **ppIFrameEncode, IPropertyBag2 **ppIEncoderOptions)
1066 JpegEncoder *This = impl_from_IWICBitmapEncoder(iface);
1067 HRESULT hr;
1069 TRACE("(%p,%p,%p)\n", iface, ppIFrameEncode, ppIEncoderOptions);
1071 EnterCriticalSection(&This->lock);
1073 if (This->frame_count != 0)
1075 LeaveCriticalSection(&This->lock);
1076 return WINCODEC_ERR_UNSUPPORTEDOPERATION;
1079 if (!This->initialized)
1081 LeaveCriticalSection(&This->lock);
1082 return WINCODEC_ERR_NOTINITIALIZED;
1085 hr = CreatePropertyBag2(ppIEncoderOptions);
1086 if (FAILED(hr))
1088 LeaveCriticalSection(&This->lock);
1089 return hr;
1092 This->frame_count = 1;
1094 LeaveCriticalSection(&This->lock);
1096 IWICBitmapEncoder_AddRef(iface);
1097 *ppIFrameEncode = &This->IWICBitmapFrameEncode_iface;
1099 return S_OK;
1102 static HRESULT WINAPI JpegEncoder_Commit(IWICBitmapEncoder *iface)
1104 FIXME("(%p): stub\n", iface);
1105 return E_NOTIMPL;
1108 static HRESULT WINAPI JpegEncoder_GetMetadataQueryWriter(IWICBitmapEncoder *iface,
1109 IWICMetadataQueryWriter **ppIMetadataQueryWriter)
1111 FIXME("(%p,%p): stub\n", iface, ppIMetadataQueryWriter);
1112 return E_NOTIMPL;
1115 static const IWICBitmapEncoderVtbl JpegEncoder_Vtbl = {
1116 JpegEncoder_QueryInterface,
1117 JpegEncoder_AddRef,
1118 JpegEncoder_Release,
1119 JpegEncoder_Initialize,
1120 JpegEncoder_GetContainerFormat,
1121 JpegEncoder_GetEncoderInfo,
1122 JpegEncoder_SetColorContexts,
1123 JpegEncoder_SetPalette,
1124 JpegEncoder_SetThumbnail,
1125 JpegEncoder_SetPreview,
1126 JpegEncoder_CreateNewFrame,
1127 JpegEncoder_Commit,
1128 JpegEncoder_GetMetadataQueryWriter
1131 HRESULT JpegEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1133 JpegEncoder *This;
1134 HRESULT ret;
1136 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
1138 *ppv = NULL;
1140 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
1142 if (!libjpeg_handle && !load_libjpeg())
1144 ERR("Failed writing JPEG because unable to find %s\n",SONAME_LIBJPEG);
1145 return E_FAIL;
1148 This = HeapAlloc(GetProcessHeap(), 0, sizeof(JpegEncoder));
1149 if (!This) return E_OUTOFMEMORY;
1151 This->IWICBitmapEncoder_iface.lpVtbl = &JpegEncoder_Vtbl;
1152 This->IWICBitmapFrameEncode_iface.lpVtbl = &JpegEncoder_FrameVtbl;
1153 This->ref = 1;
1154 This->initialized = 0;
1155 This->frame_count = 0;
1156 This->frame_initialized = 0;
1157 This->stream = NULL;
1158 InitializeCriticalSection(&This->lock);
1159 This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JpegEncoder.lock");
1161 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
1162 IUnknown_Release((IUnknown*)This);
1164 return ret;
1167 #else /* !defined(SONAME_LIBJPEG) */
1169 HRESULT JpegDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1171 ERR("Trying to load JPEG picture, but JPEG support is not compiled in.\n");
1172 return E_FAIL;
1175 HRESULT JpegEncoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
1177 ERR("Trying to save JPEG picture, but JPEG support is not compiled in.\n");
1178 return E_FAIL;
1181 #endif