Synchronize with trunk r58528.
[reactos.git] / dll / win32 / windowscodecs / main.c
1 /*
2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #define WIN32_NO_STATUS
20 #define _INC_WINDOWS
21 #define COM_NO_WINDOWS_H
22
23 #define COBJMACROS
24 #include <config.h>
25
26 #include <stdarg.h>
27
28 #include <windef.h>
29 #include <winbase.h>
30 #include <objbase.h>
31 #include <wincodec.h>
32
33 #include "wincodecs_private.h"
34
35 #include <wine/debug.h>
36
37 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
38
39 extern BOOL WINAPI WIC_DllMain(HINSTANCE, DWORD, LPVOID) DECLSPEC_HIDDEN;
40
41 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
42 {
43
44 switch (fdwReason)
45 {
46 case DLL_PROCESS_ATTACH:
47 DisableThreadLibraryCalls(hinstDLL);
48 break;
49 case DLL_PROCESS_DETACH:
50 break;
51 }
52
53 return WIC_DllMain(hinstDLL, fdwReason, lpvReserved);
54 }
55
56 HRESULT WINAPI DllCanUnloadNow(void)
57 {
58 return S_FALSE;
59 }
60
61 HRESULT copy_pixels(UINT bpp, const BYTE *srcbuffer,
62 UINT srcwidth, UINT srcheight, INT srcstride,
63 const WICRect *rc, UINT dststride, UINT dstbuffersize, BYTE *dstbuffer)
64 {
65 UINT bytesperrow;
66 UINT row_offset; /* number of bits into the source rows where the data starts */
67 WICRect rect;
68
69 if (!rc)
70 {
71 rect.X = 0;
72 rect.Y = 0;
73 rect.Width = srcwidth;
74 rect.Height = srcheight;
75 rc = &rect;
76 }
77 else
78 {
79 if (rc->X < 0 || rc->Y < 0 || rc->X+rc->Width > srcwidth || rc->Y+rc->Height > srcheight)
80 return E_INVALIDARG;
81 }
82
83 bytesperrow = ((bpp * rc->Width)+7)/8;
84
85 if (dststride < bytesperrow)
86 return E_INVALIDARG;
87
88 if ((dststride * (rc->Height-1)) + ((rc->Width * bpp) + 7)/8 > dstbuffersize)
89 return E_INVALIDARG;
90
91 /* if the whole bitmap is copied and the buffer format matches then it's a matter of a single memcpy */
92 if (rc->X == 0 && rc->Y == 0 && rc->Width == srcwidth && rc->Height == srcheight && srcstride == dststride)
93 {
94 memcpy(dstbuffer, srcbuffer, srcstride * srcheight);
95 return S_OK;
96 }
97
98 row_offset = rc->X * bpp;
99
100 if (row_offset % 8 == 0)
101 {
102 /* everything lines up on a byte boundary */
103 UINT row;
104 const BYTE *src;
105 BYTE *dst;
106
107 src = srcbuffer + (row_offset / 8) + srcstride * rc->Y;
108 dst = dstbuffer;
109 for (row=0; row < rc->Height; row++)
110 {
111 memcpy(dst, src, bytesperrow);
112 src += srcstride;
113 dst += dststride;
114 }
115 return S_OK;
116 }
117 else
118 {
119 /* we have to do a weird bitwise copy. eww. */
120 FIXME("cannot reliably copy bitmap data if bpp < 8\n");
121 return E_FAIL;
122 }
123 }
124
125 void reverse_bgr8(UINT bytesperpixel, LPBYTE bits, UINT width, UINT height, INT stride)
126 {
127 UINT x, y;
128 BYTE *pixel, temp;
129
130 for (y=0; y<height; y++)
131 {
132 pixel = bits + stride * y;
133
134 for (x=0; x<width; x++)
135 {
136 temp = pixel[2];
137 pixel[2] = pixel[0];
138 pixel[0] = temp;
139 pixel += bytesperpixel;
140 }
141 }
142 }
143
144 HRESULT get_pixelformat_bpp(const GUID *pixelformat, UINT *bpp)
145 {
146 HRESULT hr;
147 IWICComponentInfo *info;
148 IWICPixelFormatInfo *formatinfo;
149
150 hr = CreateComponentInfo(pixelformat, &info);
151 if (SUCCEEDED(hr))
152 {
153 hr = IWICComponentInfo_QueryInterface(info, &IID_IWICPixelFormatInfo, (void**)&formatinfo);
154
155 if (SUCCEEDED(hr))
156 {
157 hr = IWICPixelFormatInfo_GetBitsPerPixel(formatinfo, bpp);
158
159 IWICPixelFormatInfo_Release(formatinfo);
160 }
161
162 IWICComponentInfo_Release(info);
163 }
164
165 return hr;
166 }