wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / windowscodecs / wincodecs_common.c
blob1b146908be926d7c40014b4a0d1362816c4666aa
1 /*
2 * wincodecs_common.c - Functions shared with other WIC libraries.
4 * Copyright 2020 Esme Povirk
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define COBJMACROS
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winternl.h"
28 #include "objbase.h"
30 #include "wincodecs_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
36 #include "wincodecs_common.h"
38 HRESULT configure_write_source(IWICBitmapFrameEncode *iface,
39 IWICBitmapSource *source, const WICRect *prc,
40 const WICPixelFormatGUID *format,
41 INT width, INT height, double xres, double yres)
43 UINT src_width, src_height;
44 HRESULT hr = S_OK;
46 if (width == 0 && height == 0)
48 if (prc)
50 if (prc->Width <= 0 || prc->Height <= 0) return E_INVALIDARG;
51 width = prc->Width;
52 height = prc->Height;
54 else
56 hr = IWICBitmapSource_GetSize(source, &src_width, &src_height);
57 if (FAILED(hr)) return hr;
58 if (src_width == 0 || src_height == 0) return E_INVALIDARG;
59 width = src_width;
60 height = src_height;
62 hr = IWICBitmapFrameEncode_SetSize(iface, (UINT)width, (UINT)height);
63 if (FAILED(hr)) return hr;
65 if (width == 0 || height == 0) return E_INVALIDARG;
67 if (!format)
69 WICPixelFormatGUID src_format;
71 hr = IWICBitmapSource_GetPixelFormat(source, &src_format);
72 if (FAILED(hr)) return hr;
74 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &src_format);
75 if (FAILED(hr)) return hr;
78 if (xres == 0.0 || yres == 0.0)
80 hr = IWICBitmapSource_GetResolution(source, &xres, &yres);
81 if (FAILED(hr)) return hr;
82 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
83 if (FAILED(hr)) return hr;
86 return hr;
89 HRESULT write_source(IWICBitmapFrameEncode *iface,
90 IWICBitmapSource *source, const WICRect *prc,
91 const WICPixelFormatGUID *format, UINT bpp, BOOL need_palette,
92 INT width, INT height)
94 IWICBitmapSource *converted_source;
95 HRESULT hr=S_OK;
96 WICRect rc;
97 UINT stride;
98 BYTE* pixeldata;
100 if (!prc)
102 UINT src_width, src_height;
103 hr = IWICBitmapSource_GetSize(source, &src_width, &src_height);
104 if (FAILED(hr)) return hr;
105 rc.X = 0;
106 rc.Y = 0;
107 rc.Width = src_width;
108 rc.Height = src_height;
109 prc = &rc;
112 if (prc->Width != width || prc->Height <= 0)
113 return E_INVALIDARG;
115 hr = WICConvertBitmapSource(format, source, &converted_source);
116 if (FAILED(hr))
118 ERR("Failed to convert source, target format %s, %#x\n", debugstr_guid(format), hr);
119 return E_NOTIMPL;
122 if (need_palette)
124 IWICImagingFactory *factory;
125 IWICPalette *palette;
127 hr = create_instance(&CLSID_WICImagingFactory, &IID_IWICImagingFactory, (void**)&factory);
129 if (SUCCEEDED(hr))
131 hr = IWICImagingFactory_CreatePalette(factory, &palette);
132 IWICImagingFactory_Release(factory);
135 if (SUCCEEDED(hr))
137 hr = IWICBitmapSource_CopyPalette(converted_source, palette);
139 if (SUCCEEDED(hr))
140 hr = IWICBitmapFrameEncode_SetPalette(iface, palette);
142 IWICPalette_Release(palette);
145 if (FAILED(hr))
147 IWICBitmapSource_Release(converted_source);
148 return hr;
152 stride = (bpp * width + 7)/8;
154 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
155 if (!pixeldata)
157 IWICBitmapSource_Release(converted_source);
158 return E_OUTOFMEMORY;
161 hr = IWICBitmapSource_CopyPixels(converted_source, prc, stride,
162 stride*prc->Height, pixeldata);
164 if (SUCCEEDED(hr))
166 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
167 stride*prc->Height, pixeldata);
170 HeapFree(GetProcessHeap(), 0, pixeldata);
171 IWICBitmapSource_Release(converted_source);
173 return hr;
176 HRESULT CDECL stream_getsize(IStream *stream, ULONGLONG *size)
178 STATSTG statstg;
179 HRESULT hr;
181 hr = IStream_Stat(stream, &statstg, STATFLAG_NONAME);
183 if (SUCCEEDED(hr))
184 *size = statstg.cbSize.QuadPart;
186 return hr;
189 HRESULT CDECL stream_read(IStream *stream, void *buffer, ULONG read, ULONG *bytes_read)
191 return IStream_Read(stream, buffer, read, bytes_read);
194 HRESULT CDECL stream_seek(IStream *stream, LONGLONG ofs, DWORD origin, ULONGLONG *new_position)
196 HRESULT hr;
197 LARGE_INTEGER ofs_large;
198 ULARGE_INTEGER pos_large;
200 ofs_large.QuadPart = ofs;
201 hr = IStream_Seek(stream, ofs_large, origin, &pos_large);
202 if (new_position)
203 *new_position = pos_large.QuadPart;
205 return hr;
208 HRESULT CDECL stream_write(IStream *stream, const void *buffer, ULONG write, ULONG *bytes_written)
210 return IStream_Write(stream, buffer, write, bytes_written);