3#include <winpr/print.h>
4#include <winpr/image.h>
5#include <winpr/clipboard.h>
6#include <winpr/stream.h>
9static BOOL test_dib_to_bmp(
const BYTE* dib,
size_t dibSize,
size_t expectedOffset)
13 wClipboard* clipboard = ClipboardCreate();
17 const UINT32 bmpId = ClipboardRegisterFormat(clipboard,
"image/bmp");
18 if ((bmpId == 0) || (dibSize > UINT32_MAX) ||
19 !ClipboardSetData(clipboard, CF_DIB, dib, (UINT32)dibSize))
23 BYTE* bmp = ClipboardGetData(clipboard, bmpId, &bmpSize);
27 wStream bmpBuffer = WINPR_C_ARRAY_INIT;
28 wStream* s = Stream_StaticConstInit(&bmpBuffer, bmp, bmpSize);
33 UINT32 bitmapOffset = 0;
34 Stream_Read_UINT32(s, bitmapOffset);
35 if ((bitmapOffset != expectedOffset) ||
41 wImage* image = winpr_image_new();
44 rc = (winpr_image_read_buffer(image, bmp, bmpSize) > 0);
45 winpr_image_free(image, TRUE);
50 ClipboardDestroy(clipboard);
54static void write_dib_info_header(
wStream* s, UINT32 size, UINT16 bpp, UINT32 compression)
58 Stream_Write_UINT32(s, size);
59 Stream_Write_INT32(s, 1);
60 Stream_Write_INT32(s, 1);
61 Stream_Write_UINT16(s, 1);
62 Stream_Write_UINT16(s, bpp);
63 Stream_Write_UINT32(s, compression);
64 Stream_Write_UINT32(s, 4);
68static BOOL test_dib_offsets(
void)
71 wStream sbuffer = WINPR_C_ARRAY_INIT;
72 wStream* s = Stream_StaticInit(&sbuffer, v4,
sizeof(v4));
75 write_dib_info_header(s,
sizeof(
BITMAPV4HEADER), 32, BI_BITFIELDS);
77 Stream_Write_UINT32(s, 0x00FF0000);
78 Stream_Write_UINT32(s, 0x0000FF00);
79 Stream_Write_UINT32(s, 0x000000FF);
80 Stream_Write_UINT32(s, 0xFF000000);
82 Stream_Write_UINT32(s, 0xFF123456);
86 BYTE bitfields[
sizeof(
BITMAPINFOHEADER) + 3 *
sizeof(DWORD) + 4] = WINPR_C_ARRAY_INIT;
87 s = Stream_StaticInit(&sbuffer, bitfields,
sizeof(bitfields));
91 Stream_Write_UINT32(s, 0x00FF0000);
92 Stream_Write_UINT32(s, 0x0000FF00);
93 Stream_Write_UINT32(s, 0x000000FF);
94 Stream_Write_UINT32(s, 0xFF123456);
95 if (!test_dib_to_bmp(bitfields,
sizeof(bitfields),
101 s = Stream_StaticInit(&sbuffer, paletted,
sizeof(paletted));
105 Stream_Zero(s, 256 *
sizeof(
RGBQUAD));
106 Stream_Write_UINT32(s, 0);
107 return test_dib_to_bmp(paletted,
sizeof(paletted),
112int TestClipboardFormats(
int argc,
char* argv[])
116 UINT32* pFormatIds =
nullptr;
117 const char* formatName =
nullptr;
118 wClipboard* clipboard =
nullptr;
119 UINT32 utf8StringFormatId = 0;
124 clipboard = ClipboardCreate();
127 if (!test_dib_offsets())
130 const char* mime_types[] = {
"text/html",
"text/html",
"image/bmp",
131 "image/png",
"image/webp",
"image/jpeg" };
132 for (
size_t x = 0; x < ARRAYSIZE(mime_types); x++)
134 const char* mime = mime_types[x];
135 UINT32
id = ClipboardRegisterFormat(clipboard, mime);
136 (void)fprintf(stderr,
"ClipboardRegisterFormat(%s) -> 0x%08" PRIx32
"\n", mime,
id);
141 utf8StringFormatId = ClipboardRegisterFormat(clipboard,
"UTF8_STRING");
142 pFormatIds =
nullptr;
143 count = ClipboardGetRegisteredFormatIds(clipboard, &pFormatIds);
145 for (UINT32 index = 0; index < count; index++)
147 UINT32 formatId = pFormatIds[index];
148 formatName = ClipboardGetFormatName(clipboard, formatId);
149 (void)fprintf(stderr,
"Format: 0x%08" PRIX32
" %s\n", formatId, formatName);
159 const char pSrcData[] =
"this is a test string";
160 char* pDstData =
nullptr;
162 SrcSize = (UINT32)(strnlen(pSrcData, ARRAYSIZE(pSrcData)) + 1);
163 bSuccess = ClipboardSetData(clipboard, utf8StringFormatId, pSrcData, SrcSize);
164 (void)fprintf(stderr,
"ClipboardSetData: %" PRId32
"\n", bSuccess);
166 pDstData = (
char*)ClipboardGetData(clipboard, utf8StringFormatId, &DstSize);
167 (void)fprintf(stderr,
"ClipboardGetData: %s\n", pDstData);
174 char* pSrcData =
nullptr;
175 WCHAR* pDstData =
nullptr;
177 pDstData = (WCHAR*)ClipboardGetData(clipboard, CF_UNICODETEXT, &DstSize);
178 pSrcData = ConvertWCharNToUtf8Alloc(pDstData, DstSize /
sizeof(WCHAR),
nullptr);
180 (void)fprintf(stderr,
"ClipboardGetData (synthetic): %s\n", pSrcData);
185 pFormatIds =
nullptr;
186 count = ClipboardGetFormatIds(clipboard, &pFormatIds);
188 for (UINT32 index = 0; index < count; index++)
190 UINT32 formatId = pFormatIds[index];
191 formatName = ClipboardGetFormatName(clipboard, formatId);
192 (void)fprintf(stderr,
"Format: 0x%08" PRIX32
" %s\n", formatId, formatName);
197 const char* name = TEST_CLIP_BMP;
198 BOOL bSuccess = FALSE;
199 UINT32 idBmp = ClipboardRegisterFormat(clipboard,
"image/bmp");
201 wImage* img = winpr_image_new();
205 if (winpr_image_read(img, name) <= 0)
207 winpr_image_free(img, TRUE);
212 void* data = winpr_image_write_buffer(img, WINPR_IMAGE_BITMAP, &bmpsize);
213 bSuccess = ClipboardSetData(clipboard, idBmp, data, bmpsize);
214 (void)fprintf(stderr,
"ClipboardSetData: %" PRId32
"\n", bSuccess);
217 winpr_image_free(img, TRUE);
225 void* pDstData = ClipboardGetData(clipboard,
id, &DstSize);
226 (void)fprintf(stderr,
"ClipboardGetData: [CF_DIB] %p [%" PRIu32
"]\n", pDstData,
230 bSuccess = ClipboardSetData(clipboard,
id, pDstData, DstSize);
236 const uint32_t
id = ClipboardGetFormatId(clipboard,
"HTML Format");
238 void* pDstData = ClipboardGetData(clipboard,
id, &DstSize);
242 FILE* fp = fopen(
"test.html",
"w");
245 (void)fwrite(pDstData, 1, DstSize, fp);
252 UINT32
id = ClipboardRegisterFormat(clipboard,
"image/bmp");
255 void* pDstData = ClipboardGetData(clipboard,
id, &DstSize);
256 (void)fprintf(stderr,
"ClipboardGetData: [image/bmp] %p [%" PRIu32
"]\n", pDstData,
261 if (DstSize != bmpsize)
265#if defined(WINPR_UTILS_IMAGE_PNG)
267 UINT32
id = ClipboardRegisterFormat(clipboard,
"image/png");
270 void* pDstData = ClipboardGetData(clipboard,
id, &DstSize);
271 (void)fprintf(stderr,
"ClipboardGetData: [image/png] %p\n", pDstData);
277 const char* name = TEST_CLIP_PNG;
278 BOOL bSuccess = FALSE;
279 UINT32 idBmp = ClipboardRegisterFormat(clipboard,
"image/png");
281 wImage* img = winpr_image_new();
285 if (winpr_image_read(img, name) <= 0)
287 winpr_image_free(img, TRUE);
292 void* data = winpr_image_write_buffer(img, WINPR_IMAGE_PNG, &bmpsize);
293 bSuccess = ClipboardSetData(clipboard, idBmp, data, bmpsize);
294 (void)fprintf(stderr,
"ClipboardSetData: %" PRId32
"\n", bSuccess);
297 winpr_image_free(img, TRUE);
305 void* pDstData = ClipboardGetData(clipboard,
id, &DstSize);
306 (void)fprintf(stderr,
"ClipboardGetData: [CF_DIB] %p [%" PRIu32
"]\n", pDstData,
310 bSuccess = ClipboardSetData(clipboard,
id, pDstData, DstSize);
317#if defined(WINPR_UTILS_IMAGE_WEBP)
319 UINT32
id = ClipboardRegisterFormat(clipboard,
"image/webp");
322 void* pDstData = ClipboardGetData(clipboard,
id, &DstSize);
323 (void)fprintf(stderr,
"ClipboardGetData: [image/webp] %p\n", pDstData);
330#if defined(WINPR_UTILS_IMAGE_JPEG)
332 UINT32
id = ClipboardRegisterFormat(clipboard,
"image/jpeg");
335 void* pDstData = ClipboardGetData(clipboard,
id, &DstSize);
336 (void)fprintf(stderr,
"ClipboardGetData: [image/jpeg] %p\n", pDstData);
348 ClipboardDestroy(clipboard);