quartz: Silence requests for ipin on filters.
[wine] / dlls / gdiplus / tests / image.c
1 /*
2  * Unit test suite for images
3  *
4  * Copyright (C) 2007 Google (Evan Stade)
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "windows.h"
22 #include "gdiplus.h"
23 #include "wine/test.h"
24 #include <math.h>
25
26 #define expect(expected, got) ok(((UINT)got) == ((UINT)expected), "Expected %.8x, got %.8x\n", (UINT)expected, (UINT)got)
27
28 static void test_Scan0(void)
29 {
30     GpBitmap *bm;
31     GpStatus stat;
32     BYTE buff[360];
33
34     bm = NULL;
35     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, NULL, &bm);
36     expect(Ok, stat);
37     ok(NULL != bm, "Expected bitmap to be initialized\n");
38     if (stat == Ok)
39         GdipDisposeImage((GpImage*)bm);
40
41     bm = (GpBitmap*)0xdeadbeef;
42     stat = GdipCreateBitmapFromScan0(10, -10, 10, PixelFormat24bppRGB, NULL, &bm);
43     expect(InvalidParameter, stat);
44
45     expect(NULL, bm);
46
47     bm = (GpBitmap*)0xdeadbeef;
48     stat = GdipCreateBitmapFromScan0(-10, 10, 10, PixelFormat24bppRGB, NULL, &bm);
49     expect(InvalidParameter, stat);
50
51     expect(NULL, bm);
52
53     bm = (GpBitmap*)0xdeadbeef;
54     stat = GdipCreateBitmapFromScan0(10, 0, 10, PixelFormat24bppRGB, NULL, &bm);
55     expect(InvalidParameter, stat);
56
57     expect(NULL, bm);
58
59     bm = NULL;
60     stat = GdipCreateBitmapFromScan0(10, 10, 12, PixelFormat24bppRGB, buff, &bm);
61     expect(Ok, stat);
62     ok(NULL != bm, "Expected bitmap to be initialized\n");
63     if (stat == Ok)
64         GdipDisposeImage((GpImage*)bm);
65
66     bm = (GpBitmap*) 0xdeadbeef;
67     stat = GdipCreateBitmapFromScan0(10, 10, 10, PixelFormat24bppRGB, buff, &bm);
68     expect(InvalidParameter, stat);
69     expect(NULL, bm);
70
71     bm = (GpBitmap*)0xdeadbeef;
72     stat = GdipCreateBitmapFromScan0(10, 10, 0, PixelFormat24bppRGB, buff, &bm);
73     expect(InvalidParameter, stat);
74     expect(0xdeadbeef, bm);
75 }
76
77 static void test_GetImageDimension(void)
78 {
79     GpBitmap *bm;
80     GpStatus stat;
81     const REAL WIDTH = 10.0, HEIGHT = 20.0;
82     REAL w,h;
83
84     bm = (GpBitmap*)0xdeadbeef;
85     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB,NULL, &bm);
86     expect(Ok,stat);
87     ok((GpBitmap*)0xdeadbeef != bm, "Expected bitmap to not be 0xdeadbeef\n");
88     ok(NULL != bm, "Expected bitmap to not be NULL\n");
89
90     stat = GdipGetImageDimension(NULL,&w,&h);
91     expect(InvalidParameter, stat);
92
93     stat = GdipGetImageDimension((GpImage*)bm,NULL,&h);
94     expect(InvalidParameter, stat);
95
96     stat = GdipGetImageDimension((GpImage*)bm,&w,NULL);
97     expect(InvalidParameter, stat);
98
99     w = -1;
100     h = -1;
101     stat = GdipGetImageDimension((GpImage*)bm,&w,&h);
102     expect(Ok, stat);
103     ok(fabs(WIDTH - w) < 0.0001, "Width wrong\n");
104     ok(fabs(HEIGHT - h) < 0.0001, "Height wrong\n");
105     GdipDisposeImage((GpImage*)bm);
106 }
107
108 static void test_LoadingImages(void)
109 {
110     GpStatus stat;
111
112     stat = GdipCreateBitmapFromFile(0, 0);
113     expect(InvalidParameter, stat);
114
115     stat = GdipCreateBitmapFromFile(0, (GpBitmap**)0xdeadbeef);
116     expect(InvalidParameter, stat);
117
118     stat = GdipLoadImageFromFile(0, 0);
119     expect(InvalidParameter, stat);
120
121     stat = GdipLoadImageFromFile(0, (GpImage**)0xdeadbeef);
122     expect(InvalidParameter, stat);
123 }
124
125 static void test_SavingImages(void)
126 {
127     GpStatus stat;
128     GpBitmap *bm;
129     UINT n;
130     UINT s;
131     const REAL WIDTH = 10.0, HEIGHT = 20.0;
132     REAL w, h;
133     ImageCodecInfo *codecs;
134     static const WCHAR filename[] = { 'a','.','b','m','p',0 };
135
136     codecs = NULL;
137
138     stat = GdipSaveImageToFile(0, 0, 0, 0);
139     expect(InvalidParameter, stat);
140
141     bm = NULL;
142     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
143     expect(Ok, stat);
144     if (!bm)
145         return;
146
147     /* invalid params */
148     stat = GdipSaveImageToFile((GpImage*)bm, 0, 0, 0);
149     expect(InvalidParameter, stat);
150
151     stat = GdipSaveImageToFile((GpImage*)bm, filename, 0, 0);
152     expect(InvalidParameter, stat);
153
154     /* encoder tests should succeed -- already tested */
155     stat = GdipGetImageEncodersSize(&n, &s);
156     if (stat != Ok || n == 0) goto cleanup;
157
158     codecs = GdipAlloc(s);
159     if (!codecs) goto cleanup;
160
161     stat = GdipGetImageEncoders(n, s, codecs);
162     if (stat != Ok) goto cleanup;
163
164     stat = GdipSaveImageToFile((GpImage*)bm, filename, &codecs[0].Clsid, 0);
165     expect(stat, Ok);
166
167     GdipDisposeImage((GpImage*)bm);
168     bm = 0;
169
170     /* re-load and check image stats */
171     stat = GdipLoadImageFromFile(filename, (GpImage**)&bm);
172     expect(stat, Ok);
173     if (stat != Ok) goto cleanup;
174
175     stat = GdipGetImageDimension((GpImage*)bm, &w, &h);
176     if (stat != Ok) goto cleanup;
177
178     ok((fabs(w - WIDTH) < 0.01) && (fabs(h - HEIGHT) < 0.01),
179        "Saved image dimensions are different!\n");
180
181  cleanup:
182     GdipFree(codecs);
183     if (bm)
184         GdipDisposeImage((GpImage*)bm);
185     ok(DeleteFileW(filename), "Delete failed.\n");
186 }
187
188 static void test_encoders(void)
189 {
190     GpStatus stat;
191     UINT n;
192     UINT s;
193     ImageCodecInfo *codecs;
194     int i;
195     int bmp_found;
196
197     static const WCHAR bmp_format[] = {'B', 'M', 'P', 0};
198
199     stat = GdipGetImageEncodersSize(&n, &s);
200     expect(stat, Ok);
201
202     codecs = GdipAlloc(s);
203     if (!codecs)
204         return;
205
206     stat = GdipGetImageEncoders(n, s, NULL);
207     expect(GenericError, stat);
208
209     stat = GdipGetImageEncoders(0, s, codecs);
210     expect(GenericError, stat);
211
212     stat = GdipGetImageEncoders(n, s-1, codecs);
213     expect(GenericError, stat);
214
215     stat = GdipGetImageEncoders(n, s+1, codecs);
216     expect(GenericError, stat);
217
218     stat = GdipGetImageEncoders(n, s, codecs);
219     expect(stat, Ok);
220
221     bmp_found = FALSE;
222     for (i = 0; i < n; i++)
223         {
224             if (CompareStringW(LOCALE_SYSTEM_DEFAULT, 0,
225                                codecs[i].FormatDescription, -1,
226                                bmp_format, -1) == CSTR_EQUAL) {
227                 bmp_found = TRUE;
228                 break;
229             }
230         }
231     if (!bmp_found)
232         ok(FALSE, "No BMP codec found.\n");
233
234     GdipFree(codecs);
235 }
236
237 static void test_LockBits(void)
238 {
239     GpStatus stat;
240     GpBitmap *bm;
241     GpRect rect;
242     BitmapData bd;
243     const REAL WIDTH = 10.0, HEIGHT = 20.0;
244
245     bm = NULL;
246     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
247     expect(Ok, stat);
248
249     rect.X = 2;
250     rect.Y = 3;
251     rect.Width = 4;
252     rect.Height = 5;
253
254     /* read-only */
255     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
256     expect(Ok, stat);
257
258     if (stat == Ok) {
259         stat = GdipBitmapUnlockBits(bm, &bd);
260         expect(Ok, stat);
261     }
262
263     /* read-only, consecutive */
264     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
265     expect(Ok, stat);
266
267     if (stat == Ok) {
268         stat = GdipBitmapUnlockBits(bm, &bd);
269         expect(Ok, stat);
270     }
271
272     stat = GdipDisposeImage((GpImage*)bm);
273     expect(Ok, stat);
274     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
275     expect(Ok, stat);
276
277     /* read x2 */
278     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
279     expect(Ok, stat);
280     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
281     expect(WrongState, stat);
282
283     stat = GdipBitmapUnlockBits(bm, &bd);
284     expect(Ok, stat);
285
286     stat = GdipDisposeImage((GpImage*)bm);
287     expect(Ok, stat);
288     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
289     expect(Ok, stat);
290
291     /* write, no modification */
292     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
293     expect(Ok, stat);
294
295     if (stat == Ok) {
296         stat = GdipBitmapUnlockBits(bm, &bd);
297         expect(Ok, stat);
298     }
299
300     /* write, consecutive */
301     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
302     expect(Ok, stat);
303
304     if (stat == Ok) {
305         stat = GdipBitmapUnlockBits(bm, &bd);
306         expect(Ok, stat);
307     }
308
309     stat = GdipDisposeImage((GpImage*)bm);
310     expect(Ok, stat);
311     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
312     expect(Ok, stat);
313
314     /* write, modify */
315     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeWrite, PixelFormat24bppRGB, &bd);
316     expect(Ok, stat);
317
318     if (stat == Ok) {
319         if (bd.Scan0)
320             ((char*)bd.Scan0)[2] = 0xff;
321
322         stat = GdipBitmapUnlockBits(bm, &bd);
323         expect(Ok, stat);
324     }
325
326     stat = GdipDisposeImage((GpImage*)bm);
327     expect(Ok, stat);
328
329     /* dispose locked */
330     stat = GdipCreateBitmapFromScan0(WIDTH, HEIGHT, 0, PixelFormat24bppRGB, NULL, &bm);
331     expect(Ok, stat);
332     stat = GdipBitmapLockBits(bm, &rect, ImageLockModeRead, PixelFormat24bppRGB, &bd);
333     expect(Ok, stat);
334     stat = GdipDisposeImage((GpImage*)bm);
335     expect(Ok, stat);
336 }
337
338 START_TEST(image)
339 {
340     struct GdiplusStartupInput gdiplusStartupInput;
341     ULONG_PTR gdiplusToken;
342
343     gdiplusStartupInput.GdiplusVersion              = 1;
344     gdiplusStartupInput.DebugEventCallback          = NULL;
345     gdiplusStartupInput.SuppressBackgroundThread    = 0;
346     gdiplusStartupInput.SuppressExternalCodecs      = 0;
347
348     GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
349
350     test_Scan0();
351     test_GetImageDimension();
352     test_LoadingImages();
353     test_SavingImages();
354     test_encoders();
355     test_LockBits();
356
357     GdiplusShutdown(gdiplusToken);
358 }