sapi: Add SpMMAudioOut stub.
[wine.git] / dlls / windowscodecs / libjpeg.c
blob8611269d04b26bd72c02ebee75e89cdbffa45d91
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 <stdarg.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <setjmp.h>
23 #include <basetsd.h>
24 #include <jpeglib.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winternl.h"
30 #include "winbase.h"
31 #include "objbase.h"
33 #include "wincodecs_private.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
38 WINE_DECLARE_DEBUG_CHANNEL(jpeg);
40 static void error_exit_fn(j_common_ptr cinfo)
42 char message[JMSG_LENGTH_MAX];
43 if (ERR_ON(jpeg))
45 cinfo->err->format_message(cinfo, message);
46 ERR_(jpeg)("%s\n", message);
48 longjmp(*(jmp_buf*)cinfo->client_data, 1);
51 static void emit_message_fn(j_common_ptr cinfo, int msg_level)
53 char message[JMSG_LENGTH_MAX];
55 if (msg_level < 0 && ERR_ON(jpeg))
57 cinfo->err->format_message(cinfo, message);
58 ERR_(jpeg)("%s\n", message);
60 else if (msg_level == 0 && WARN_ON(jpeg))
62 cinfo->err->format_message(cinfo, message);
63 WARN_(jpeg)("%s\n", message);
65 else if (msg_level > 0 && TRACE_ON(jpeg))
67 cinfo->err->format_message(cinfo, message);
68 TRACE_(jpeg)("%s\n", message);
72 struct jpeg_decoder {
73 struct decoder decoder;
74 struct decoder_frame frame;
75 BOOL cinfo_initialized;
76 IStream *stream;
77 struct jpeg_decompress_struct cinfo;
78 struct jpeg_error_mgr jerr;
79 struct jpeg_source_mgr source_mgr;
80 BYTE source_buffer[1024];
81 UINT stride;
82 BYTE *image_data;
85 static inline struct jpeg_decoder *impl_from_decoder(struct decoder* iface)
87 return CONTAINING_RECORD(iface, struct jpeg_decoder, decoder);
90 static inline struct jpeg_decoder *decoder_from_decompress(j_decompress_ptr decompress)
92 return CONTAINING_RECORD(decompress, struct jpeg_decoder, cinfo);
95 static void CDECL jpeg_decoder_destroy(struct decoder* iface)
97 struct jpeg_decoder *This = impl_from_decoder(iface);
99 if (This->cinfo_initialized) jpeg_destroy_decompress(&This->cinfo);
100 free(This->image_data);
101 RtlFreeHeap(GetProcessHeap(), 0, This);
104 static void source_mgr_init_source(j_decompress_ptr cinfo)
108 static boolean source_mgr_fill_input_buffer(j_decompress_ptr cinfo)
110 struct jpeg_decoder *This = decoder_from_decompress(cinfo);
111 HRESULT hr;
112 ULONG bytesread;
114 hr = stream_read(This->stream, This->source_buffer, 1024, &bytesread);
116 if (FAILED(hr) || bytesread == 0)
118 return FALSE;
120 else
122 This->source_mgr.next_input_byte = This->source_buffer;
123 This->source_mgr.bytes_in_buffer = bytesread;
124 return TRUE;
128 static void source_mgr_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
130 struct jpeg_decoder *This = decoder_from_decompress(cinfo);
132 if (num_bytes > This->source_mgr.bytes_in_buffer)
134 stream_seek(This->stream, num_bytes - This->source_mgr.bytes_in_buffer, STREAM_SEEK_CUR, NULL);
135 This->source_mgr.bytes_in_buffer = 0;
137 else if (num_bytes > 0)
139 This->source_mgr.next_input_byte += num_bytes;
140 This->source_mgr.bytes_in_buffer -= num_bytes;
144 static void source_mgr_term_source(j_decompress_ptr cinfo)
148 static HRESULT CDECL jpeg_decoder_initialize(struct decoder* iface, IStream *stream, struct decoder_stat *st)
150 struct jpeg_decoder *This = impl_from_decoder(iface);
151 int ret;
152 jmp_buf jmpbuf;
153 UINT data_size, i;
155 if (This->cinfo_initialized)
156 return WINCODEC_ERR_WRONGSTATE;
158 jpeg_std_error(&This->jerr);
160 This->jerr.error_exit = error_exit_fn;
161 This->jerr.emit_message = emit_message_fn;
163 This->cinfo.err = &This->jerr;
165 This->cinfo.client_data = jmpbuf;
167 if (setjmp(jmpbuf))
168 return E_FAIL;
170 jpeg_CreateDecompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_decompress_struct));
172 This->cinfo_initialized = TRUE;
174 This->stream = stream;
176 stream_seek(This->stream, 0, STREAM_SEEK_SET, NULL);
178 This->source_mgr.bytes_in_buffer = 0;
179 This->source_mgr.init_source = source_mgr_init_source;
180 This->source_mgr.fill_input_buffer = source_mgr_fill_input_buffer;
181 This->source_mgr.skip_input_data = source_mgr_skip_input_data;
182 This->source_mgr.resync_to_restart = jpeg_resync_to_restart;
183 This->source_mgr.term_source = source_mgr_term_source;
185 This->cinfo.src = &This->source_mgr;
187 ret = jpeg_read_header(&This->cinfo, TRUE);
189 if (ret != JPEG_HEADER_OK) {
190 WARN("Jpeg image in stream has bad format, read header returned %d.\n",ret);
191 return E_FAIL;
194 switch (This->cinfo.jpeg_color_space)
196 case JCS_GRAYSCALE:
197 This->cinfo.out_color_space = JCS_GRAYSCALE;
198 This->frame.bpp = 8;
199 This->frame.pixel_format = GUID_WICPixelFormat8bppGray;
200 break;
201 case JCS_RGB:
202 case JCS_YCbCr:
203 This->cinfo.out_color_space = JCS_RGB;
204 This->frame.bpp = 24;
205 This->frame.pixel_format = GUID_WICPixelFormat24bppBGR;
206 break;
207 case JCS_CMYK:
208 case JCS_YCCK:
209 This->cinfo.out_color_space = JCS_CMYK;
210 This->frame.bpp = 32;
211 This->frame.pixel_format = GUID_WICPixelFormat32bppCMYK;
212 break;
213 default:
214 ERR("Unknown JPEG color space %i\n", This->cinfo.jpeg_color_space);
215 return E_FAIL;
218 if (!jpeg_start_decompress(&This->cinfo))
220 ERR("jpeg_start_decompress failed\n");
221 return E_FAIL;
224 This->frame.width = This->cinfo.output_width;
225 This->frame.height = This->cinfo.output_height;
227 switch (This->cinfo.density_unit)
229 case 2: /* pixels per centimeter */
230 This->frame.dpix = This->cinfo.X_density * 2.54;
231 This->frame.dpiy = This->cinfo.Y_density * 2.54;
232 break;
234 case 1: /* pixels per inch */
235 This->frame.dpix = This->cinfo.X_density;
236 This->frame.dpiy = This->cinfo.Y_density;
237 break;
239 case 0: /* unknown */
240 default:
241 This->frame.dpix = This->frame.dpiy = 96.0;
242 break;
245 This->frame.num_color_contexts = 0;
246 This->frame.num_colors = 0;
248 This->stride = (This->frame.bpp * This->cinfo.output_width + 7) / 8;
249 data_size = This->stride * This->cinfo.output_height;
251 This->image_data = malloc(data_size);
252 if (!This->image_data)
253 return E_OUTOFMEMORY;
255 while (This->cinfo.output_scanline < This->cinfo.output_height)
257 UINT first_scanline = This->cinfo.output_scanline;
258 UINT max_rows;
259 JSAMPROW out_rows[4];
260 JDIMENSION ret;
262 max_rows = min(This->cinfo.output_height-first_scanline, 4);
263 for (i=0; i<max_rows; i++)
264 out_rows[i] = This->image_data + This->stride * (first_scanline+i);
266 ret = jpeg_read_scanlines(&This->cinfo, out_rows, max_rows);
267 if (ret == 0)
269 ERR("read_scanlines failed\n");
270 return E_FAIL;
274 if (This->frame.bpp == 24)
276 /* libjpeg gives us RGB data and we want BGR, so byteswap the data */
277 reverse_bgr8(3, This->image_data,
278 This->cinfo.output_width, This->cinfo.output_height,
279 This->stride);
282 if (This->cinfo.out_color_space == JCS_CMYK && This->cinfo.saw_Adobe_marker)
284 /* Adobe JPEG's have inverted CMYK data. */
285 for (i=0; i<data_size; i++)
286 This->image_data[i] ^= 0xff;
289 st->frame_count = 1;
290 st->flags = WICBitmapDecoderCapabilityCanDecodeAllImages |
291 WICBitmapDecoderCapabilityCanDecodeSomeImages |
292 WICBitmapDecoderCapabilityCanEnumerateMetadata |
293 DECODER_FLAGS_UNSUPPORTED_COLOR_CONTEXT;
294 return S_OK;
297 static HRESULT CDECL jpeg_decoder_get_frame_info(struct decoder* iface, UINT frame, struct decoder_frame *info)
299 struct jpeg_decoder *This = impl_from_decoder(iface);
300 *info = This->frame;
301 return S_OK;
304 static HRESULT CDECL jpeg_decoder_copy_pixels(struct decoder* iface, UINT frame,
305 const WICRect *prc, UINT stride, UINT buffersize, BYTE *buffer)
307 struct jpeg_decoder *This = impl_from_decoder(iface);
308 return copy_pixels(This->frame.bpp, This->image_data,
309 This->frame.width, This->frame.height, This->stride,
310 prc, stride, buffersize, buffer);
313 static HRESULT CDECL jpeg_decoder_get_metadata_blocks(struct decoder* iface, UINT frame,
314 UINT *count, struct decoder_block **blocks)
316 FIXME("stub\n");
317 *count = 0;
318 *blocks = NULL;
319 return S_OK;
322 static HRESULT CDECL jpeg_decoder_get_color_context(struct decoder* This, UINT frame, UINT num,
323 BYTE **data, DWORD *datasize)
325 /* This should never be called because we report 0 color contexts and the unsupported flag. */
326 FIXME("stub\n");
327 return E_NOTIMPL;
330 static const struct decoder_funcs jpeg_decoder_vtable = {
331 jpeg_decoder_initialize,
332 jpeg_decoder_get_frame_info,
333 jpeg_decoder_copy_pixels,
334 jpeg_decoder_get_metadata_blocks,
335 jpeg_decoder_get_color_context,
336 jpeg_decoder_destroy
339 HRESULT CDECL jpeg_decoder_create(struct decoder_info *info, struct decoder **result)
341 struct jpeg_decoder *This;
343 This = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(struct jpeg_decoder));
344 if (!This) return E_OUTOFMEMORY;
346 This->decoder.vtable = &jpeg_decoder_vtable;
347 This->cinfo_initialized = FALSE;
348 This->stream = NULL;
349 This->image_data = NULL;
350 *result = &This->decoder;
352 info->container_format = GUID_ContainerFormatJpeg;
353 info->block_format = GUID_ContainerFormatJpeg;
354 info->clsid = CLSID_WICJpegDecoder;
356 return S_OK;
359 typedef struct jpeg_compress_format {
360 const WICPixelFormatGUID *guid;
361 int bpp;
362 int num_components;
363 J_COLOR_SPACE color_space;
364 int swap_rgb;
365 } jpeg_compress_format;
367 static const jpeg_compress_format compress_formats[] = {
368 { &GUID_WICPixelFormat24bppBGR, 24, 3, JCS_RGB, 1 },
369 { &GUID_WICPixelFormat32bppCMYK, 32, 4, JCS_CMYK },
370 { &GUID_WICPixelFormat8bppGray, 8, 1, JCS_GRAYSCALE },
371 { 0 }
374 struct jpeg_encoder
376 struct encoder encoder;
377 IStream *stream;
378 BOOL cinfo_initialized;
379 struct jpeg_compress_struct cinfo;
380 struct jpeg_error_mgr jerr;
381 struct jpeg_destination_mgr dest_mgr;
382 struct encoder_frame encoder_frame;
383 const jpeg_compress_format *format;
384 BYTE dest_buffer[1024];
387 static inline struct jpeg_encoder *impl_from_encoder(struct encoder* iface)
389 return CONTAINING_RECORD(iface, struct jpeg_encoder, encoder);
392 static inline struct jpeg_encoder *encoder_from_compress(j_compress_ptr compress)
394 return CONTAINING_RECORD(compress, struct jpeg_encoder, cinfo);
397 static void dest_mgr_init_destination(j_compress_ptr cinfo)
399 struct jpeg_encoder *This = encoder_from_compress(cinfo);
401 This->dest_mgr.next_output_byte = This->dest_buffer;
402 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
405 static boolean dest_mgr_empty_output_buffer(j_compress_ptr cinfo)
407 struct jpeg_encoder *This = encoder_from_compress(cinfo);
408 HRESULT hr;
409 ULONG byteswritten;
411 hr = stream_write(This->stream, This->dest_buffer,
412 sizeof(This->dest_buffer), &byteswritten);
414 if (hr != S_OK || byteswritten == 0)
416 ERR("Failed writing data, hr=%lx\n", hr);
417 return FALSE;
420 This->dest_mgr.next_output_byte = This->dest_buffer;
421 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
422 return TRUE;
425 static void dest_mgr_term_destination(j_compress_ptr cinfo)
427 struct jpeg_encoder *This = encoder_from_compress(cinfo);
428 ULONG byteswritten;
429 HRESULT hr;
431 if (This->dest_mgr.free_in_buffer != sizeof(This->dest_buffer))
433 hr = stream_write(This->stream, This->dest_buffer,
434 sizeof(This->dest_buffer) - This->dest_mgr.free_in_buffer, &byteswritten);
436 if (hr != S_OK || byteswritten == 0)
437 ERR("Failed writing data, hr=%lx\n", hr);
441 static HRESULT CDECL jpeg_encoder_initialize(struct encoder* iface, IStream *stream)
443 struct jpeg_encoder *This = impl_from_encoder(iface);
444 jmp_buf jmpbuf;
446 jpeg_std_error(&This->jerr);
448 This->jerr.error_exit = error_exit_fn;
449 This->jerr.emit_message = emit_message_fn;
451 This->cinfo.err = &This->jerr;
453 This->cinfo.client_data = jmpbuf;
455 if (setjmp(jmpbuf))
456 return E_FAIL;
458 jpeg_CreateCompress(&This->cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_compress_struct));
460 This->stream = stream;
462 This->dest_mgr.next_output_byte = This->dest_buffer;
463 This->dest_mgr.free_in_buffer = sizeof(This->dest_buffer);
465 This->dest_mgr.init_destination = dest_mgr_init_destination;
466 This->dest_mgr.empty_output_buffer = dest_mgr_empty_output_buffer;
467 This->dest_mgr.term_destination = dest_mgr_term_destination;
469 This->cinfo.dest = &This->dest_mgr;
471 This->cinfo_initialized = TRUE;
473 return S_OK;
476 static HRESULT CDECL jpeg_encoder_get_supported_format(struct encoder* iface, GUID *pixel_format,
477 DWORD *bpp, BOOL *indexed)
479 int i;
481 for (i=0; compress_formats[i].guid; i++)
483 if (memcmp(compress_formats[i].guid, pixel_format, sizeof(GUID)) == 0)
484 break;
487 if (!compress_formats[i].guid) i = 0;
489 *pixel_format = *compress_formats[i].guid;
490 *bpp = compress_formats[i].bpp;
491 *indexed = FALSE;
493 return S_OK;
496 static HRESULT CDECL jpeg_encoder_create_frame(struct encoder* iface, const struct encoder_frame *frame)
498 struct jpeg_encoder *This = impl_from_encoder(iface);
499 jmp_buf jmpbuf;
500 int i;
502 This->encoder_frame = *frame;
504 if (setjmp(jmpbuf))
505 return E_FAIL;
507 This->cinfo.client_data = jmpbuf;
509 for (i=0; compress_formats[i].guid; i++)
511 if (memcmp(compress_formats[i].guid, &frame->pixel_format, sizeof(GUID)) == 0)
512 break;
514 This->format = &compress_formats[i];
516 This->cinfo.image_width = frame->width;
517 This->cinfo.image_height = frame->height;
518 This->cinfo.input_components = This->format->num_components;
519 This->cinfo.in_color_space = This->format->color_space;
521 jpeg_set_defaults(&This->cinfo);
523 if (frame->dpix != 0.0 && frame->dpiy != 0.0)
525 This->cinfo.density_unit = 1; /* dots per inch */
526 This->cinfo.X_density = frame->dpix;
527 This->cinfo.Y_density = frame->dpiy;
530 jpeg_start_compress(&This->cinfo, TRUE);
532 return S_OK;
535 static HRESULT CDECL jpeg_encoder_write_lines(struct encoder* iface, BYTE *data,
536 DWORD line_count, DWORD stride)
538 struct jpeg_encoder *This = impl_from_encoder(iface);
539 jmp_buf jmpbuf;
540 BYTE *swapped_data = NULL, *current_row;
541 UINT line;
542 int row_size;
544 if (setjmp(jmpbuf))
546 free(swapped_data);
547 return E_FAIL;
550 This->cinfo.client_data = jmpbuf;
552 row_size = This->format->bpp / 8 * This->encoder_frame.width;
554 if (This->format->swap_rgb)
556 swapped_data = malloc(row_size);
557 if (!swapped_data)
558 return E_OUTOFMEMORY;
561 for (line=0; line < line_count; line++)
563 if (This->format->swap_rgb)
565 UINT x;
567 memcpy(swapped_data, data + (stride * line), row_size);
569 for (x=0; x < This->encoder_frame.width; x++)
571 BYTE b;
573 b = swapped_data[x*3];
574 swapped_data[x*3] = swapped_data[x*3+2];
575 swapped_data[x*3+2] = b;
578 current_row = swapped_data;
580 else
581 current_row = data + (stride * line);
583 if (!jpeg_write_scanlines(&This->cinfo, &current_row, 1))
585 ERR("failed writing scanlines\n");
586 free(swapped_data);
587 return E_FAIL;
591 free(swapped_data);
593 return S_OK;
596 static HRESULT CDECL jpeg_encoder_commit_frame(struct encoder* iface)
598 struct jpeg_encoder *This = impl_from_encoder(iface);
599 jmp_buf jmpbuf;
601 if (setjmp(jmpbuf))
602 return E_FAIL;
604 This->cinfo.client_data = jmpbuf;
606 jpeg_finish_compress(&This->cinfo);
608 return S_OK;
611 static HRESULT CDECL jpeg_encoder_commit_file(struct encoder* iface)
613 return S_OK;
616 static void CDECL jpeg_encoder_destroy(struct encoder* iface)
618 struct jpeg_encoder *This = impl_from_encoder(iface);
619 if (This->cinfo_initialized)
620 jpeg_destroy_compress(&This->cinfo);
621 RtlFreeHeap(GetProcessHeap(), 0, This);
624 static const struct encoder_funcs jpeg_encoder_vtable = {
625 jpeg_encoder_initialize,
626 jpeg_encoder_get_supported_format,
627 jpeg_encoder_create_frame,
628 jpeg_encoder_write_lines,
629 jpeg_encoder_commit_frame,
630 jpeg_encoder_commit_file,
631 jpeg_encoder_destroy
634 HRESULT CDECL jpeg_encoder_create(struct encoder_info *info, struct encoder **result)
636 struct jpeg_encoder *This;
638 This = RtlAllocateHeap(GetProcessHeap(), 0, sizeof(struct jpeg_encoder));
639 if (!This) return E_OUTOFMEMORY;
641 This->encoder.vtable = &jpeg_encoder_vtable;
642 This->stream = NULL;
643 This->cinfo_initialized = FALSE;
644 *result = &This->encoder;
646 info->flags = ENCODER_FLAGS_SUPPORTS_METADATA;
647 info->container_format = GUID_ContainerFormatJpeg;
648 info->clsid = CLSID_WICJpegEncoder;
649 info->encoder_options[0] = ENCODER_OPTION_IMAGE_QUALITY;
650 info->encoder_options[1] = ENCODER_OPTION_BITMAP_TRANSFORM;
651 info->encoder_options[2] = ENCODER_OPTION_LUMINANCE;
652 info->encoder_options[3] = ENCODER_OPTION_CHROMINANCE;
653 info->encoder_options[4] = ENCODER_OPTION_YCRCB_SUBSAMPLING;
654 info->encoder_options[5] = ENCODER_OPTION_SUPPRESS_APP0;
655 info->encoder_options[6] = ENCODER_OPTION_END;
657 return S_OK;