bfa7c70a227331ec1b36ac0e1a1e48b1a7a1722a
[reactos.git] / rostests / apitests / atl / CImage.cpp
1 /*
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)
6 */
7
8 #include <atlimage.h>
9 #include "resource.h"
10
11 #ifdef __REACTOS__
12 #include <apitest.h>
13 #else
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <stdarg.h>
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, ...)
20 {
21 va_list va;
22 va_start(va, fmt);
23 if (!value)
24 {
25 printf("%s (%d): ", file, line);
26 vprintf(fmt, va);
27 g_tests_failed++;
28 }
29 g_tests_executed++;
30 va_end(va);
31 }
32 #undef ok
33 #define ok(value, ...) ok_func(__FILE__, __LINE__, value, __VA_ARGS__)
34 #define START_TEST(x) int main(void)
35 #endif
36
37
38 const TCHAR* szFiles[] = {
39 TEXT("ant.png"),
40 TEXT("ant.tif"),
41 TEXT("ant.gif"),
42 TEXT("ant.jpg"),
43 TEXT("ant.bmp"),
44 };
45
46 static TCHAR szTempPath[MAX_PATH];
47 TCHAR* file_name(const TCHAR* file)
48 {
49 static TCHAR buffer[MAX_PATH];
50 lstrcpy(buffer, szTempPath);
51 lstrcat(buffer, TEXT("\\"));
52 lstrcat(buffer, file);
53 return buffer;
54 }
55
56 static void write_bitmap(HINSTANCE hInst, int id, TCHAR* file)
57 {
58 HRSRC rsrc;
59
60 rsrc = FindResource(hInst, MAKEINTRESOURCE(id), RT_BITMAP);
61 ok(rsrc != NULL, "Expected to find an image resource\n");
62 if (rsrc)
63 {
64 void *rsrc_data;
65 HANDLE hfile;
66 BOOL ret;
67 HGLOBAL glob = LoadResource(hInst, rsrc);
68 DWORD rsrc_size = SizeofResource(hInst, rsrc);
69
70 rsrc_data = LockResource(glob);
71
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)
75 {
76 BITMAPFILEHEADER bfh = { 0 };
77 DWORD dwWritten;
78
79 bfh.bfType = 'MB';
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());
87 CloseHandle(hfile);
88 }
89 UnlockResource(rsrc_data);
90 }
91 }
92
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 *);
98
99 static HINSTANCE hinstGdiPlus;
100 static ULONG_PTR gdiplusToken;
101
102 static STARTUP Startup;
103 static SHUTDOWN Shutdown;
104 static CREATEBITMAPFROMFILE CreateBitmapFromFile;
105 static GETPIXELFORMAT GetImagePixelFormat;
106 static DISPOSEIMAGE DisposeImage;
107
108 template <typename TYPE>
109 TYPE AddrOf(const char *name)
110 {
111 FARPROC proc = ::GetProcAddress(hinstGdiPlus, name);
112 return reinterpret_cast<TYPE>(proc);
113 }
114
115 static void init_gdip()
116 {
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");
123 }
124
125
126 static void determine_file_bpp(TCHAR* tfile, Gdiplus::PixelFormat expect_pf)
127 {
128 using namespace Gdiplus;
129 GpBitmap *pBitmap = NULL;
130
131 #ifdef UNICODE
132 WCHAR* file = tfile;
133 #else
134 WCHAR file[MAX_PATH];
135 ::MultiByteToWideChar(CP_ACP, 0, tfile, -1, file, MAX_PATH);
136 #endif
137
138 if (Startup == NULL)
139 init_gdip();
140
141 Gdiplus::GdiplusStartupInput gdiplusStartupInput;
142 Startup(&gdiplusToken, &gdiplusStartupInput, NULL);
143
144
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");
148 if (pBitmap)
149 {
150 PixelFormat pf;
151 GetImagePixelFormat(pBitmap, &pf);
152 ok(pf == expect_pf, "Expected PixelFormat to be 0x%x, was: 0x%x\n", (int)expect_pf, (int)pf);
153
154 DisposeImage(pBitmap);
155 }
156 Shutdown(gdiplusToken);
157 }
158
159
160 START_TEST(CImage)
161 {
162 HRESULT hr;
163 TCHAR* file;
164 BOOL bOK;
165 int width, height, bpp;
166 size_t n;
167 CImage image1, image2;
168 COLORREF color;
169 HDC hDC;
170
171 #if 0
172 width = image1.GetWidth();
173 height = image1.GetHeight();
174 bpp = image1.GetBPP();
175 #endif
176
177 HINSTANCE hInst = GetModuleHandle(NULL);
178 GetTempPath(MAX_PATH, szTempPath);
179
180 image1.LoadFromResource(hInst, IDB_ANT);
181 ok(!image1.IsNull(), "Expected image1 is not null\n");
182
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);
189
190
191 image2.LoadFromResource(hInst, IDB_CROSS);
192 ok(!image2.IsNull(), "Expected image2 is not null\n");
193 image2.SetTransparentColor(RGB(255, 255, 255));
194
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);
201
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));
204
205 hDC = image1.GetDC();
206 bOK = image2.Draw(hDC, 0, 0);
207 image1.ReleaseDC();
208 ok(bOK != FALSE, "Expected bDraw to be TRUE, was: %d\n", bOK);
209 image2.Destroy();
210
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));
213
214 file = file_name(TEXT("ant.bmp"));
215 write_bitmap(hInst, IDB_ANT, file);
216
217 init_gdip();
218
219 determine_file_bpp(file, PixelFormat8bppIndexed);
220
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);
226
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);
233
234 for (n = 0; n < _countof(szFiles); ++n)
235 {
236 file = file_name(szFiles[n]);
237 image2.Destroy();
238
239 if (n == 0)
240 hr = image1.Save(file, Gdiplus::ImageFormatPNG);
241 else
242 hr = image1.Save(file);
243 ok(hr == S_OK, "Expected hr to be S_OK, was: %08lx (for %i)\n", hr, n);
244
245 bOK = (GetFileAttributes(file) != 0xFFFFFFFF);
246 ok(bOK, "Expected bOK to be TRUE, was: %d (for %i)\n", bOK, n);
247
248 hr = image2.Load(file);
249 ok(hr == S_OK, "Expected hr to be S_OK, was: %08lx (for %i)\n", hr, n);
250
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();
256 if (n == 3)
257 {
258 ok(bpp == 24, "Expected bpp to be 24, was: %d (for %i)\n", bpp, n);
259 determine_file_bpp(file, PixelFormat24bppRGB);
260 }
261 else
262 {
263 ok(bpp == 8, "Expected bpp to be 8, was: %d (for %i)\n", bpp, n);
264 determine_file_bpp(file, PixelFormat8bppIndexed);
265 }
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);
268
269 bOK = DeleteFile(file);
270 ok(bOK, "Expected bOK to be TRUE, was: %d (for %i)\n", bOK, n);
271 }
272
273 #ifndef __REACTOS__
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;
276 #endif
277 }