2 * PROJECT: ReactOS api tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test for CImage
5 * PROGRAMMER: Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
17 int g_tests_executed
= 0;
18 int g_tests_failed
= 0;
19 void ok_func(const char *file
, int line
, BOOL value
, const char *fmt
, ...)
25 printf("%s (%d): ", file
, line
);
33 #define ok(value, ...) ok_func(__FILE__, __LINE__, value, __VA_ARGS__)
34 #define START_TEST(x) int main(void)
38 const TCHAR
* szFiles
[] = {
46 static TCHAR szTempPath
[MAX_PATH
];
47 TCHAR
* file_name(const TCHAR
* file
)
49 static TCHAR buffer
[MAX_PATH
];
50 lstrcpy(buffer
, szTempPath
);
51 lstrcat(buffer
, TEXT("\\"));
52 lstrcat(buffer
, file
);
56 static void write_bitmap(HINSTANCE hInst
, int id
, TCHAR
* file
)
60 rsrc
= FindResource(hInst
, MAKEINTRESOURCE(id
), RT_BITMAP
);
61 ok(rsrc
!= NULL
, "Expected to find an image resource\n");
67 HGLOBAL glob
= LoadResource(hInst
, rsrc
);
68 DWORD rsrc_size
= SizeofResource(hInst
, rsrc
);
70 rsrc_data
= LockResource(glob
);
72 hfile
= CreateFile(file
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, 0);
73 ok(hfile
!= INVALID_HANDLE_VALUE
, "Unable to open temp file: %lu\n", GetLastError());
74 if (hfile
!= INVALID_HANDLE_VALUE
)
76 BITMAPFILEHEADER bfh
= { 0 };
80 bfh
.bfSize
= rsrc_size
+ sizeof(BITMAPFILEHEADER
);
81 bfh
.bfOffBits
= sizeof(BITMAPFILEHEADER
) + sizeof(BITMAPINFOHEADER
);
82 bfh
.bfReserved1
= bfh
.bfReserved2
= 0;
83 ret
= WriteFile(hfile
, &bfh
, sizeof(bfh
), &dwWritten
, NULL
);
84 ok(ret
, "Unable to write temp file: %lu\n", GetLastError());
85 ret
= WriteFile(hfile
, rsrc_data
, rsrc_size
, &dwWritten
, NULL
);
86 ok(ret
, "Unable to write temp file: %lu\n", GetLastError());
89 UnlockResource(rsrc_data
);
93 typedef Gdiplus::GpStatus (WINAPI
*STARTUP
)(ULONG_PTR
*, const Gdiplus::GdiplusStartupInput
*, Gdiplus::GdiplusStartupOutput
*);
94 typedef void (WINAPI
*SHUTDOWN
)(ULONG_PTR
);
95 typedef Gdiplus::GpStatus (WINGDIPAPI
*CREATEBITMAPFROMFILE
)(GDIPCONST WCHAR
*, Gdiplus::GpBitmap
**);
96 typedef Gdiplus::GpStatus (WINGDIPAPI
*GETPIXELFORMAT
)(Gdiplus::GpImage
*image
, Gdiplus::PixelFormat
*format
);
97 typedef Gdiplus::GpStatus (WINGDIPAPI
*DISPOSEIMAGE
)(Gdiplus::GpImage
*);
99 static HINSTANCE hinstGdiPlus
;
100 static ULONG_PTR gdiplusToken
;
102 static STARTUP Startup
;
103 static SHUTDOWN Shutdown
;
104 static CREATEBITMAPFROMFILE CreateBitmapFromFile
;
105 static GETPIXELFORMAT GetImagePixelFormat
;
106 static DISPOSEIMAGE DisposeImage
;
108 template <typename TYPE
>
109 TYPE
AddrOf(const char *name
)
111 FARPROC proc
= ::GetProcAddress(hinstGdiPlus
, name
);
112 return reinterpret_cast<TYPE
>(proc
);
115 static void init_gdip()
117 hinstGdiPlus
= ::LoadLibraryA("gdiplus.dll");
118 Startup
= AddrOf
<STARTUP
>("GdiplusStartup");
119 Shutdown
= AddrOf
<SHUTDOWN
>("GdiplusShutdown");
120 CreateBitmapFromFile
= AddrOf
<CREATEBITMAPFROMFILE
>("GdipCreateBitmapFromFile");
121 GetImagePixelFormat
= AddrOf
<GETPIXELFORMAT
>("GdipGetImagePixelFormat");
122 DisposeImage
= AddrOf
<DISPOSEIMAGE
>("GdipDisposeImage");
126 static void determine_file_bpp(TCHAR
* tfile
, Gdiplus::PixelFormat expect_pf
)
128 using namespace Gdiplus
;
129 GpBitmap
*pBitmap
= NULL
;
134 WCHAR file
[MAX_PATH
];
135 ::MultiByteToWideChar(CP_ACP
, 0, tfile
, -1, file
, MAX_PATH
);
141 Gdiplus::GdiplusStartupInput gdiplusStartupInput
;
142 Startup(&gdiplusToken
, &gdiplusStartupInput
, NULL
);
145 Gdiplus::GpStatus status
= CreateBitmapFromFile(file
, &pBitmap
);
146 ok(status
== Gdiplus::Ok
, "Expected status to be %i, was: %i\n", (int)Gdiplus::Ok
, (int)status
);
147 ok(pBitmap
!= NULL
, "Expected a valid bitmap\n");
151 GetImagePixelFormat(pBitmap
, &pf
);
152 ok(pf
== expect_pf
, "Expected PixelFormat to be 0x%x, was: 0x%x\n", (int)expect_pf
, (int)pf
);
154 DisposeImage(pBitmap
);
156 Shutdown(gdiplusToken
);
165 int width
, height
, bpp
;
167 CImage image1
, image2
;
172 width
= image1
.GetWidth();
173 height
= image1
.GetHeight();
174 bpp
= image1
.GetBPP();
177 HINSTANCE hInst
= GetModuleHandle(NULL
);
178 GetTempPath(MAX_PATH
, szTempPath
);
180 image1
.LoadFromResource(hInst
, IDB_ANT
);
181 ok(!image1
.IsNull(), "Expected image1 is not null\n");
183 width
= image1
.GetWidth();
184 ok(width
== 48, "Expected width to be 48, was: %d\n", width
);
185 height
= image1
.GetHeight();
186 ok(height
== 48, "Expected height to be 48, was: %d\n", height
);
187 bpp
= image1
.GetBPP();
188 ok(bpp
== 8, "Expected bpp to be 8, was: %d\n", bpp
);
191 image2
.LoadFromResource(hInst
, IDB_CROSS
);
192 ok(!image2
.IsNull(), "Expected image2 is not null\n");
193 image2
.SetTransparentColor(RGB(255, 255, 255));
195 width
= image2
.GetWidth();
196 ok(width
== 32, "Expected width to be 32, was: %d\n", width
);
197 height
= image2
.GetHeight();
198 ok(height
== 32, "Expected height to be 32, was: %d\n", height
);
199 bpp
= image2
.GetBPP();
200 ok(bpp
== 8, "Expected bpp to be 8, was: %d\n", bpp
);
202 color
= image1
.GetPixel(5, 5);
203 ok(color
== RGB(166, 202, 240), "Expected color to be 166, 202, 240; was: %i, %i, %i\n", GetRValue(color
), GetGValue(color
), GetBValue(color
));
205 hDC
= image1
.GetDC();
206 bOK
= image2
.Draw(hDC
, 0, 0);
208 ok(bOK
!= FALSE
, "Expected bDraw to be TRUE, was: %d\n", bOK
);
211 color
= image1
.GetPixel(5, 5);
212 ok(color
== RGB(255, 0,0), "Expected color to be 255, 0, 0; was: %i, %i, %i\n", GetRValue(color
), GetGValue(color
), GetBValue(color
));
214 file
= file_name(TEXT("ant.bmp"));
215 write_bitmap(hInst
, IDB_ANT
, file
);
219 determine_file_bpp(file
, PixelFormat8bppIndexed
);
221 hr
= image2
.Load(file
);
222 ok(hr
== S_OK
, "Expected hr to be S_OK, was: %08lx\n", hr
);
223 ok(!image2
.IsNull(), "Expected image1 is not null\n");
224 bOK
= DeleteFile(file
);
225 ok(bOK
, "Expected bOK to be TRUE, was: %d\n", bOK
);
227 width
= image2
.GetWidth();
228 ok(width
== 48, "Expected width to be 48, was: %d\n", width
);
229 height
= image2
.GetHeight();
230 ok(height
== 48, "Expected height to be 48, was: %d\n", height
);
231 bpp
= image2
.GetBPP();
232 ok(bpp
== 8, "Expected bpp to be 8, was: %d\n", bpp
);
234 for (n
= 0; n
< _countof(szFiles
); ++n
)
236 file
= file_name(szFiles
[n
]);
240 hr
= image1
.Save(file
, Gdiplus::ImageFormatPNG
);
242 hr
= image1
.Save(file
);
243 ok(hr
== S_OK
, "Expected hr to be S_OK, was: %08lx (for %i)\n", hr
, n
);
245 bOK
= (GetFileAttributes(file
) != 0xFFFFFFFF);
246 ok(bOK
, "Expected bOK to be TRUE, was: %d (for %i)\n", bOK
, n
);
248 hr
= image2
.Load(file
);
249 ok(hr
== S_OK
, "Expected hr to be S_OK, was: %08lx (for %i)\n", hr
, n
);
251 width
= image2
.GetWidth();
252 ok(width
== 48, "Expected width to be 48, was: %d (for %i)\n", width
, n
);
253 height
= image2
.GetHeight();
254 ok(height
== 48, "Expected height to be 48, was: %d (for %i)\n", height
, n
);
255 bpp
= image2
.GetBPP();
258 ok(bpp
== 24, "Expected bpp to be 24, was: %d (for %i)\n", bpp
, n
);
259 determine_file_bpp(file
, PixelFormat24bppRGB
);
263 ok(bpp
== 8, "Expected bpp to be 8, was: %d (for %i)\n", bpp
, n
);
264 determine_file_bpp(file
, PixelFormat8bppIndexed
);
266 color
= image1
.GetPixel(5, 5);
267 ok(color
== RGB(255, 0,0), "Expected color to be 255, 0, 0; was: %i, %i, %i (for %i)\n", GetRValue(color
), GetGValue(color
), GetBValue(color
), n
);
269 bOK
= DeleteFile(file
);
270 ok(bOK
, "Expected bOK to be TRUE, was: %d (for %i)\n", bOK
, n
);
274 printf("CImage: %i tests executed (0 marked as todo, %i failures), 0 skipped.\n", g_tests_executed
, g_tests_failed
);
275 return g_tests_failed
;