ole32: Don't hold a ref to the drop target in the wrapper. Apps tend to destroy the...
[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 #include "config.h"
21
22 #include <stdarg.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "objbase.h"
27 #include "wincodec.h"
28
29 #include "wincodecs_private.h"
30
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs);
34
35 extern BOOL WINAPI WIC_DllMain(HINSTANCE, DWORD, LPVOID) DECLSPEC_HIDDEN;
36
37 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
38 {
39
40     switch (fdwReason)
41     {
42         case DLL_PROCESS_ATTACH:
43             DisableThreadLibraryCalls(hinstDLL);
44             break;
45         case DLL_PROCESS_DETACH:
46             break;
47     }
48
49     return WIC_DllMain(hinstDLL, fdwReason, lpvReserved);
50 }
51
52 HRESULT copy_pixels(UINT bpp, const BYTE *srcbuffer,
53     UINT srcwidth, UINT srcheight, INT srcstride,
54     const WICRect *rc, UINT dststride, UINT dstbuffersize, BYTE *dstbuffer)
55 {
56     UINT bytesperrow;
57     UINT row_offset; /* number of bits into the source rows where the data starts */
58     WICRect rect;
59
60     if (!rc)
61     {
62         rect.X = 0;
63         rect.Y = 0;
64         rect.Width = srcwidth;
65         rect.Height = srcheight;
66         rc = &rect;
67     }
68     else
69     {
70         if (rc->X < 0 || rc->Y < 0 || rc->X+rc->Width > srcwidth || rc->Y+rc->Height > srcheight)
71             return E_INVALIDARG;
72     }
73
74     bytesperrow = ((bpp * rc->Width)+7)/8;
75
76     if (dststride < bytesperrow)
77         return E_INVALIDARG;
78
79     if ((dststride * rc->Height) > dstbuffersize)
80         return E_INVALIDARG;
81
82     /* if the whole bitmap is copied and the buffer format matches then it's a matter of a single memcpy */
83     if (rc->X == 0 && rc->Y == 0 && rc->Width == srcwidth && rc->Height == srcheight && srcstride == dststride)
84     {
85         memcpy(dstbuffer, srcbuffer, srcstride * srcheight);
86         return S_OK;
87     }
88
89     row_offset = rc->X * bpp;
90
91     if (row_offset % 8 == 0)
92     {
93         /* everything lines up on a byte boundary */
94         UINT row;
95         const BYTE *src;
96         BYTE *dst;
97
98         src = srcbuffer + (row_offset / 8) + srcstride * rc->Y;
99         dst = dstbuffer;
100         for (row=0; row < rc->Height; row++)
101         {
102             memcpy(dst, src, bytesperrow);
103             src += srcstride;
104             dst += dststride;
105         }
106         return S_OK;
107     }
108     else
109     {
110         /* we have to do a weird bitwise copy. eww. */
111         FIXME("cannot reliably copy bitmap data if bpp < 8\n");
112         return E_FAIL;
113     }
114 }
115
116 void reverse_bgr8(UINT bytesperpixel, LPBYTE bits, UINT width, UINT height, INT stride)
117 {
118     UINT x, y;
119     BYTE *pixel, temp;
120
121     for (y=0; y<height; y++)
122     {
123         pixel = bits + stride * y;
124
125         for (x=0; x<width; x++)
126         {
127             temp = pixel[2];
128             pixel[2] = pixel[0];
129             pixel[0] = temp;
130             pixel += bytesperpixel;
131         }
132     }
133 }