secur32/tests: Use importlib for functions available since Windows XP.
[wine.git] / dlls / windowscodecs / main.c
blob3bed56536c22de467605f30428cac80494cab36b
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
20 #define COBJMACROS
21 #include "config.h"
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "objbase.h"
29 #include "wincodecs_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
35 extern BOOL WINAPI WIC_DllMain(HINSTANCE, DWORD, LPVOID) DECLSPEC_HIDDEN;
37 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
40 switch (fdwReason)
42 case DLL_PROCESS_ATTACH:
43 DisableThreadLibraryCalls(hinstDLL);
44 break;
47 return WIC_DllMain(hinstDLL, fdwReason, lpvReserved);
50 HRESULT WINAPI DllCanUnloadNow(void)
52 return S_FALSE;
55 HRESULT copy_pixels(UINT bpp, const BYTE *srcbuffer,
56 UINT srcwidth, UINT srcheight, INT srcstride,
57 const WICRect *rc, UINT dststride, UINT dstbuffersize, BYTE *dstbuffer)
59 UINT bytesperrow;
60 UINT row_offset; /* number of bits into the source rows where the data starts */
61 WICRect rect;
63 if (!rc)
65 rect.X = 0;
66 rect.Y = 0;
67 rect.Width = srcwidth;
68 rect.Height = srcheight;
69 rc = &rect;
71 else
73 if (rc->X < 0 || rc->Y < 0 || rc->X+rc->Width > srcwidth || rc->Y+rc->Height > srcheight)
74 return E_INVALIDARG;
77 bytesperrow = ((bpp * rc->Width)+7)/8;
79 if (dststride < bytesperrow)
80 return E_INVALIDARG;
82 if ((dststride * (rc->Height-1)) + ((rc->Width * bpp) + 7)/8 > dstbuffersize)
83 return E_INVALIDARG;
85 /* if the whole bitmap is copied and the buffer format matches then it's a matter of a single memcpy */
86 if (rc->X == 0 && rc->Y == 0 && rc->Width == srcwidth && rc->Height == srcheight &&
87 srcstride == dststride && srcstride == bytesperrow)
89 memcpy(dstbuffer, srcbuffer, srcstride * srcheight);
90 return S_OK;
93 row_offset = rc->X * bpp;
95 if (row_offset % 8 == 0)
97 /* everything lines up on a byte boundary */
98 INT row;
99 const BYTE *src;
100 BYTE *dst;
102 src = srcbuffer + (row_offset / 8) + srcstride * rc->Y;
103 dst = dstbuffer;
104 for (row=0; row < rc->Height; row++)
106 memcpy(dst, src, bytesperrow);
107 src += srcstride;
108 dst += dststride;
110 return S_OK;
112 else
114 /* we have to do a weird bitwise copy. eww. */
115 FIXME("cannot reliably copy bitmap data if bpp < 8\n");
116 return E_FAIL;
120 HRESULT configure_write_source(IWICBitmapFrameEncode *iface,
121 IWICBitmapSource *source, const WICRect *prc,
122 const WICPixelFormatGUID *format,
123 INT width, INT height, double xres, double yres)
125 HRESULT hr = S_OK;
127 if (width == 0 || height == 0)
128 return WINCODEC_ERR_WRONGSTATE;
130 if (!format)
132 WICPixelFormatGUID src_format;
134 hr = IWICBitmapSource_GetPixelFormat(source, &src_format);
135 if (FAILED(hr)) return hr;
137 hr = IWICBitmapFrameEncode_SetPixelFormat(iface, &src_format);
138 if (FAILED(hr)) return hr;
141 if (xres == 0.0 || yres == 0.0)
143 hr = IWICBitmapSource_GetResolution(source, &xres, &yres);
144 if (FAILED(hr)) return hr;
145 hr = IWICBitmapFrameEncode_SetResolution(iface, xres, yres);
146 if (FAILED(hr)) return hr;
149 return hr;
152 HRESULT write_source(IWICBitmapFrameEncode *iface,
153 IWICBitmapSource *source, const WICRect *prc,
154 const WICPixelFormatGUID *format, UINT bpp,
155 INT width, INT height)
157 IWICBitmapSource *converted_source;
158 HRESULT hr=S_OK;
159 WICRect rc;
160 UINT stride;
161 BYTE* pixeldata;
163 if (!prc)
165 UINT src_width, src_height;
166 hr = IWICBitmapSource_GetSize(source, &src_width, &src_height);
167 if (FAILED(hr)) return hr;
168 rc.X = 0;
169 rc.Y = 0;
170 rc.Width = src_width;
171 rc.Height = src_height;
172 prc = &rc;
175 if (prc->Width != width || prc->Height <= 0)
176 return E_INVALIDARG;
178 hr = WICConvertBitmapSource(format, source, &converted_source);
179 if (FAILED(hr))
181 ERR("Failed to convert source, target format %s, %#x\n", debugstr_guid(format), hr);
182 return hr;
185 stride = (bpp * width + 7)/8;
187 pixeldata = HeapAlloc(GetProcessHeap(), 0, stride * prc->Height);
188 if (!pixeldata)
190 IWICBitmapSource_Release(converted_source);
191 return E_OUTOFMEMORY;
194 hr = IWICBitmapSource_CopyPixels(converted_source, prc, stride,
195 stride*prc->Height, pixeldata);
197 if (SUCCEEDED(hr))
199 hr = IWICBitmapFrameEncode_WritePixels(iface, prc->Height, stride,
200 stride*prc->Height, pixeldata);
203 HeapFree(GetProcessHeap(), 0, pixeldata);
204 IWICBitmapSource_Release(converted_source);
206 return hr;
209 void reverse_bgr8(UINT bytesperpixel, LPBYTE bits, UINT width, UINT height, INT stride)
211 UINT x, y;
212 BYTE *pixel, temp;
214 for (y=0; y<height; y++)
216 pixel = bits + stride * y;
218 for (x=0; x<width; x++)
220 temp = pixel[2];
221 pixel[2] = pixel[0];
222 pixel[0] = temp;
223 pixel += bytesperpixel;
228 HRESULT get_pixelformat_bpp(const GUID *pixelformat, UINT *bpp)
230 HRESULT hr;
231 IWICComponentInfo *info;
232 IWICPixelFormatInfo *formatinfo;
234 hr = CreateComponentInfo(pixelformat, &info);
235 if (SUCCEEDED(hr))
237 hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo, (void**)&formatinfo);
239 if (SUCCEEDED(hr))
241 hr = IWICPixelFormatInfo_GetBitsPerPixel(formatinfo, bpp);
243 IWICPixelFormatInfo_Release(formatinfo);
246 IWICComponentInfo_Release(info);
249 return hr;