2 * OLEPICTURE test program
4 * Copyright 2005 Marcus Meissner
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <wine/test.h>
43 static HMODULE hOleaut32;
45 static HRESULT (WINAPI *pOleLoadPicture)(LPSTREAM,LONG,BOOL,REFIID,LPVOID*);
48 static const unsigned char gifimage[35] = {
49 0x47,0x49,0x46,0x38,0x37,0x61,0x01,0x00,0x01,0x00,0x80,0x00,0x00,0xff,0xff,0xff,
50 0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x02,0x02,0x44,
55 static const unsigned char jpgimage[285] = {
56 0xff,0xd8,0xff,0xe0,0x00,0x10,0x4a,0x46,0x49,0x46,0x00,0x01,0x01,0x01,0x01,0x2c,
57 0x01,0x2c,0x00,0x00,0xff,0xdb,0x00,0x43,0x00,0x05,0x03,0x04,0x04,0x04,0x03,0x05,
58 0x04,0x04,0x04,0x05,0x05,0x05,0x06,0x07,0x0c,0x08,0x07,0x07,0x07,0x07,0x0f,0x0b,
59 0x0b,0x09,0x0c,0x11,0x0f,0x12,0x12,0x11,0x0f,0x11,0x11,0x13,0x16,0x1c,0x17,0x13,
60 0x14,0x1a,0x15,0x11,0x11,0x18,0x21,0x18,0x1a,0x1d,0x1d,0x1f,0x1f,0x1f,0x13,0x17,
61 0x22,0x24,0x22,0x1e,0x24,0x1c,0x1e,0x1f,0x1e,0xff,0xdb,0x00,0x43,0x01,0x05,0x05,
62 0x05,0x07,0x06,0x07,0x0e,0x08,0x08,0x0e,0x1e,0x14,0x11,0x14,0x1e,0x1e,0x1e,0x1e,
63 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
64 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,
65 0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0x1e,0xff,0xc0,
66 0x00,0x11,0x08,0x00,0x01,0x00,0x01,0x03,0x01,0x22,0x00,0x02,0x11,0x01,0x03,0x11,
67 0x01,0xff,0xc4,0x00,0x15,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
68 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0xff,0xc4,0x00,0x14,0x10,0x01,0x00,0x00,
69 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xc4,
70 0x00,0x14,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
71 0x00,0x00,0x00,0x00,0xff,0xc4,0x00,0x14,0x11,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
72 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xda,0x00,0x0c,0x03,0x01,
73 0x00,0x02,0x11,0x03,0x11,0x00,0x3f,0x00,0xb2,0xc0,0x07,0xff,0xd9
76 #if 0 /* no png support yet */
78 static const unsigned char pngimage[285] = {
79 0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
80 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x08,0x02,0x00,0x00,0x00,0x90,0x77,0x53,
81 0xde,0x00,0x00,0x00,0x09,0x70,0x48,0x59,0x73,0x00,0x00,0x0b,0x13,0x00,0x00,0x0b,
82 0x13,0x01,0x00,0x9a,0x9c,0x18,0x00,0x00,0x00,0x07,0x74,0x49,0x4d,0x45,0x07,0xd5,
83 0x06,0x03,0x0f,0x07,0x2d,0x12,0x10,0xf0,0xfd,0x00,0x00,0x00,0x0c,0x49,0x44,0x41,
84 0x54,0x08,0xd7,0x63,0xf8,0xff,0xff,0x3f,0x00,0x05,0xfe,0x02,0xfe,0xdc,0xcc,0x59,
85 0xe7,0x00,0x00,0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82
90 static const unsigned char bmpimage[66] = {
91 0x42,0x4d,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x28,0x00,
92 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
93 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
94 0x00,0x00,0x02,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0x00,0x00,
99 static const unsigned char gif4pixel[42] = {
100 0x47,0x49,0x46,0x38,0x37,0x61,0x02,0x00,0x02,0x00,0xa1,0x00,0x00,0x00,0x00,0x00,
101 0x39,0x62,0xfc,0xff,0x1a,0xe5,0xff,0xff,0xff,0x2c,0x00,0x00,0x00,0x00,0x02,0x00,
102 0x02,0x00,0x00,0x02,0x03,0x14,0x16,0x05,0x00,0x3b
105 struct NoStatStreamImpl
107 const IStreamVtbl *lpVtbl;
110 HGLOBAL supportHandle;
111 ULARGE_INTEGER streamSize;
112 ULARGE_INTEGER currentPosition;
114 typedef struct NoStatStreamImpl NoStatStreamImpl;
115 static NoStatStreamImpl* NoStatStreamImpl_Construct(HGLOBAL hGlobal);
118 test_pic_with_stream(LPSTREAM stream, unsigned int imgsize)
120 IPicture* pic = NULL;
123 OLE_HANDLE handle, hPal;
124 OLE_XSIZE_HIMETRIC width;
125 OLE_YSIZE_HIMETRIC height;
131 hres = pOleLoadPicture(stream, imgsize, TRUE, &IID_IPicture, &pvObj);
134 ok(hres == S_OK,"OLP (NULL,..) does not return 0, but 0x%08lx\n",hres);
135 ok(pic != NULL,"OLP (NULL,..) returns NULL, instead of !NULL\n");
140 hres = IPicture_QueryInterface (pic, &IID_IPicture, &pvObj);
142 ok(hres == S_OK,"IPicture_QI does not return S_OK, but 0x%08lx\n", hres);
143 ok(pvObj != NULL,"IPicture_QI does return NULL, instead of a ptr\n");
145 IPicture_Release ((IPicture*)pvObj);
148 hres = IPicture_get_Handle (pic, &handle);
149 ok(hres == S_OK,"IPicture_get_Handle does not return S_OK, but 0x%08lx\n", hres);
150 ok(handle != 0, "IPicture_get_Handle returns a NULL handle, but it should be non NULL\n");
153 hres = IPicture_get_Width (pic, &width);
154 ok(hres == S_OK,"IPicture_get_Width does not return S_OK, but 0x%08lx\n", hres);
155 ok(width != 0, "IPicture_get_Width returns 0, but it should not be 0.\n");
158 hres = IPicture_get_Height (pic, &height);
159 ok(hres == S_OK,"IPicture_get_Height does not return S_OK, but 0x%08lx\n", hres);
160 ok(height != 0, "IPicture_get_Height returns 0, but it should not be 0.\n");
163 hres = IPicture_get_Type (pic, &type);
164 ok(hres == S_OK,"IPicture_get_Type does not return S_OK, but 0x%08lx\n", hres);
165 ok(type == PICTYPE_BITMAP, "IPicture_get_Type returns %d, but it should be PICTYPE_BITMAP(%d).\n", type, PICTYPE_BITMAP);
168 hres = IPicture_get_Attributes (pic, &attr);
169 ok(hres == S_OK,"IPicture_get_Attributes does not return S_OK, but 0x%08lx\n", hres);
170 ok(attr == 0, "IPicture_get_Attributes returns %ld, but it should be 0.\n", attr);
173 hres = IPicture_get_hPal (pic, &hPal);
174 ok(hres == S_OK,"IPicture_get_hPal does not return S_OK, but 0x%08lx\n", hres);
175 /* a single pixel b/w image has no palette */
176 ok(hPal == 0, "IPicture_get_hPal returns %ld, but it should be 0.\n", (long)hPal);
178 res = IPicture_Release (pic);
179 ok (res == 0, "refcount after release is %ld, but should be 0?\n", res);
183 test_pic(const unsigned char *imgdata, unsigned int imgsize)
189 LARGE_INTEGER seekto;
190 ULARGE_INTEGER newpos1;
192 /* Let the fun begin */
193 hglob = GlobalAlloc (0, imgsize);
194 data = GlobalLock (hglob);
195 memcpy(data, imgdata, imgsize);
197 hres = CreateStreamOnHGlobal (hglob, FALSE, &stream);
198 ok (hres == S_OK, "createstreamonhglobal failed? doubt it... hres 0x%08lx\n", hres);
200 memset(&seekto,0,sizeof(seekto));
201 hres = IStream_Seek(stream,seekto,SEEK_CUR,&newpos1);
202 ok (hres == S_OK, "istream seek failed? doubt it... hres 0x%08lx\n", hres);
203 test_pic_with_stream(stream, imgsize);
205 /* again with Non Statable and Non Seekable stream */
206 stream = (LPSTREAM)NoStatStreamImpl_Construct(hglob);
207 test_pic_with_stream(stream, 0);
210 static void test_empty_image(void) {
213 IPicture* pic = NULL;
218 ULARGE_INTEGER newpos1;
219 LARGE_INTEGER seekto;
222 /* Empty image. Happens occasionally in VB programs. */
223 hglob = GlobalAlloc (0, 8);
224 data = GlobalLock (hglob);
225 memcpy(data,"lt\0\0",4);
226 ((DWORD*)data)[1] = 0;
227 hres = CreateStreamOnHGlobal (hglob, TRUE, &stream);
228 ok (hres == S_OK, "CreatestreamOnHGlobal failed? doubt it... hres 0x%08lx\n", hres);
230 memset(&seekto,0,sizeof(seekto));
231 hres = IStream_Seek(stream,seekto,SEEK_CUR,&newpos1);
232 ok (hres == S_OK, "istream seek failed? doubt it... hres 0x%08lx\n", hres);
235 hres = pOleLoadPicture(stream, 8, TRUE, &IID_IPicture, &pvObj);
237 ok(hres == S_OK,"empty picture not loaded, hres 0x%08lx\n", hres);
238 ok(pic != NULL,"empty picture not loaded, pic is NULL\n");
240 hres = IPicture_get_Type (pic, &type);
241 ok (hres == S_OK,"empty picture get type failed with hres 0x%08lx\n", hres);
242 ok (type == PICTYPE_NONE,"type is %d, but should be PICTYPE_NONE(0)\n", type);
244 hres = IPicture_get_Handle (pic, &handle);
245 ok (hres == S_OK,"empty picture get handle failed with hres 0x%08lx\n", hres);
246 ok (handle == 0, "empty picture get handle did not return 0, but 0x%08x\n", handle);
247 IPicture_Release (pic);
250 static void test_empty_image_2(void) {
253 IPicture* pic = NULL;
257 ULARGE_INTEGER newpos1;
258 LARGE_INTEGER seekto;
261 /* Empty image at random stream position. */
262 hglob = GlobalAlloc (0, 200);
263 data = GlobalLock (hglob);
265 memcpy(data,"lt\0\0",4);
266 ((DWORD*)data)[1] = 0;
267 hres = CreateStreamOnHGlobal (hglob, TRUE, &stream);
268 ok (hres == S_OK, "CreatestreamOnHGlobal failed? doubt it... hres 0x%08lx\n", hres);
270 memset(&seekto,0,sizeof(seekto));
271 seekto.u.LowPart = 42;
272 hres = IStream_Seek(stream,seekto,SEEK_CUR,&newpos1);
273 ok (hres == S_OK, "istream seek failed? doubt it... hres 0x%08lx\n", hres);
276 hres = pOleLoadPicture(stream, 8, TRUE, &IID_IPicture, &pvObj);
278 ok(hres == S_OK,"empty picture not loaded, hres 0x%08lx\n", hres);
279 ok(pic != NULL,"empty picture not loaded, pic is NULL\n");
281 hres = IPicture_get_Type (pic, &type);
282 ok (hres == S_OK,"empty picture get type failed with hres 0x%08lx\n", hres);
283 ok (type == PICTYPE_NONE,"type is %d, but should be PICTYPE_NONE(0)\n", type);
285 IPicture_Release (pic);
288 START_TEST(olepicture)
290 hOleaut32 = LoadLibraryA("oleaut32.dll");
291 pOleLoadPicture = (void*)GetProcAddress(hOleaut32, "OleLoadPicture");
292 if (!pOleLoadPicture)
295 /* Test regular 1x1 pixel images of gif, jpg, bmp type */
296 test_pic(gifimage, sizeof(gifimage));
297 test_pic(jpgimage, sizeof(jpgimage));
298 test_pic(bmpimage, sizeof(bmpimage));
299 test_pic(gif4pixel, sizeof(gif4pixel));
300 /* No PNG support yet here or in older Windows...
301 test_pic(pngimage, sizeof(pngimage));
304 test_empty_image_2();
308 /* Helper functions only ... */
311 static void NoStatStreamImpl_Destroy(NoStatStreamImpl* This)
313 GlobalFree(This->supportHandle);
314 This->supportHandle=0;
315 HeapFree(GetProcessHeap(), 0, This);
318 static ULONG WINAPI NoStatStreamImpl_AddRef(
321 NoStatStreamImpl* const This=(NoStatStreamImpl*)iface;
322 return InterlockedIncrement(&This->ref);
325 static HRESULT WINAPI NoStatStreamImpl_QueryInterface(
327 REFIID riid, /* [in] */
328 void** ppvObject) /* [iid_is][out] */
330 NoStatStreamImpl* const This=(NoStatStreamImpl*)iface;
331 if (ppvObject==0) return E_INVALIDARG;
333 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
335 *ppvObject = (IStream*)This;
337 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
339 *ppvObject = (IStream*)This;
343 return E_NOINTERFACE;
344 NoStatStreamImpl_AddRef(iface);
348 static ULONG WINAPI NoStatStreamImpl_Release(
351 NoStatStreamImpl* const This=(NoStatStreamImpl*)iface;
352 ULONG newRef = InterlockedDecrement(&This->ref);
354 NoStatStreamImpl_Destroy(This);
358 static HRESULT WINAPI NoStatStreamImpl_Read(
360 void* pv, /* [length_is][size_is][out] */
362 ULONG* pcbRead) /* [out] */
364 NoStatStreamImpl* const This=(NoStatStreamImpl*)iface;
366 ULONG bytesReadBuffer;
367 ULONG bytesToReadFromBuffer;
370 pcbRead = &bytesReadBuffer;
371 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
372 supportBuffer = GlobalLock(This->supportHandle);
373 memcpy(pv, (char *) supportBuffer+This->currentPosition.u.LowPart, bytesToReadFromBuffer);
374 This->currentPosition.u.LowPart+=bytesToReadFromBuffer;
375 *pcbRead = bytesToReadFromBuffer;
376 GlobalUnlock(This->supportHandle);
382 static HRESULT WINAPI NoStatStreamImpl_Write(
384 const void* pv, /* [size_is][in] */
386 ULONG* pcbWritten) /* [out] */
388 NoStatStreamImpl* const This=(NoStatStreamImpl*)iface;
390 ULARGE_INTEGER newSize;
391 ULONG bytesWritten = 0;
394 pcbWritten = &bytesWritten;
397 newSize.u.HighPart = 0;
398 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
399 if (newSize.u.LowPart > This->streamSize.u.LowPart)
400 IStream_SetSize(iface, newSize);
402 supportBuffer = GlobalLock(This->supportHandle);
403 memcpy((char *) supportBuffer+This->currentPosition.u.LowPart, pv, cb);
404 This->currentPosition.u.LowPart+=cb;
406 GlobalUnlock(This->supportHandle);
410 static HRESULT WINAPI NoStatStreamImpl_Seek(
412 LARGE_INTEGER dlibMove, /* [in] */
413 DWORD dwOrigin, /* [in] */
414 ULARGE_INTEGER* plibNewPosition) /* [out] */
416 NoStatStreamImpl* const This=(NoStatStreamImpl*)iface;
417 ULARGE_INTEGER newPosition;
420 case STREAM_SEEK_SET:
421 newPosition.u.HighPart = 0;
422 newPosition.u.LowPart = 0;
424 case STREAM_SEEK_CUR:
425 newPosition = This->currentPosition;
427 case STREAM_SEEK_END:
428 newPosition = This->streamSize;
431 return STG_E_INVALIDFUNCTION;
433 if (dlibMove.QuadPart < 0 && newPosition.QuadPart < -dlibMove.QuadPart)
434 return STG_E_INVALIDFUNCTION;
435 newPosition.QuadPart += dlibMove.QuadPart;
436 if (plibNewPosition) *plibNewPosition = newPosition;
437 This->currentPosition = newPosition;
441 static HRESULT WINAPI NoStatStreamImpl_SetSize(
443 ULARGE_INTEGER libNewSize) /* [in] */
445 NoStatStreamImpl* const This=(NoStatStreamImpl*)iface;
446 HGLOBAL supportHandle;
447 if (libNewSize.u.HighPart != 0)
448 return STG_E_INVALIDFUNCTION;
449 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
451 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
452 if (supportHandle == 0)
453 return STG_E_MEDIUMFULL;
454 This->supportHandle = supportHandle;
455 This->streamSize.u.LowPart = libNewSize.u.LowPart;
459 static HRESULT WINAPI NoStatStreamImpl_CopyTo(
461 IStream* pstm, /* [unique][in] */
462 ULARGE_INTEGER cb, /* [in] */
463 ULARGE_INTEGER* pcbRead, /* [out] */
464 ULARGE_INTEGER* pcbWritten) /* [out] */
468 ULONG bytesRead, bytesWritten, copySize;
469 ULARGE_INTEGER totalBytesRead;
470 ULARGE_INTEGER totalBytesWritten;
473 return STG_E_INVALIDPOINTER;
474 totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
475 totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
477 while ( cb.u.LowPart > 0 )
479 if ( cb.u.LowPart >= 128 )
482 copySize = cb.u.LowPart;
483 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
484 totalBytesRead.u.LowPart += bytesRead;
485 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
486 totalBytesWritten.u.LowPart += bytesWritten;
487 if (bytesRead != bytesWritten)
489 hr = STG_E_MEDIUMFULL;
492 if (bytesRead!=copySize)
495 cb.u.LowPart -= bytesRead;
499 pcbRead->u.LowPart = totalBytesRead.u.LowPart;
500 pcbRead->u.HighPart = totalBytesRead.u.HighPart;
505 pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
506 pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
511 static HRESULT WINAPI NoStatStreamImpl_Commit(IStream* iface,DWORD grfCommitFlags)
515 static HRESULT WINAPI NoStatStreamImpl_Revert(IStream* iface) { return S_OK; }
517 static HRESULT WINAPI NoStatStreamImpl_LockRegion(
519 ULARGE_INTEGER libOffset, /* [in] */
520 ULARGE_INTEGER cb, /* [in] */
521 DWORD dwLockType) /* [in] */
526 static HRESULT WINAPI NoStatStreamImpl_UnlockRegion(
528 ULARGE_INTEGER libOffset, /* [in] */
529 ULARGE_INTEGER cb, /* [in] */
530 DWORD dwLockType) /* [in] */
535 static HRESULT WINAPI NoStatStreamImpl_Stat(
537 STATSTG* pstatstg, /* [out] */
538 DWORD grfStatFlag) /* [in] */
543 static HRESULT WINAPI NoStatStreamImpl_Clone(
545 IStream** ppstm) /* [out] */
549 static const IStreamVtbl NoStatStreamImpl_Vtbl;
551 static NoStatStreamImpl* NoStatStreamImpl_Construct(HGLOBAL hGlobal)
553 NoStatStreamImpl* newStream;
555 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(NoStatStreamImpl));
558 newStream->lpVtbl = &NoStatStreamImpl_Vtbl;
560 newStream->supportHandle = hGlobal;
562 if (!newStream->supportHandle)
563 newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
565 newStream->currentPosition.u.HighPart = 0;
566 newStream->currentPosition.u.LowPart = 0;
567 newStream->streamSize.u.HighPart = 0;
568 newStream->streamSize.u.LowPart = GlobalSize(newStream->supportHandle);
574 static const IStreamVtbl NoStatStreamImpl_Vtbl =
576 NoStatStreamImpl_QueryInterface,
577 NoStatStreamImpl_AddRef,
578 NoStatStreamImpl_Release,
579 NoStatStreamImpl_Read,
580 NoStatStreamImpl_Write,
581 NoStatStreamImpl_Seek,
582 NoStatStreamImpl_SetSize,
583 NoStatStreamImpl_CopyTo,
584 NoStatStreamImpl_Commit,
585 NoStatStreamImpl_Revert,
586 NoStatStreamImpl_LockRegion,
587 NoStatStreamImpl_UnlockRegion,
588 NoStatStreamImpl_Stat,
589 NoStatStreamImpl_Clone