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