File: testclip.c

package info (click to toggle)
libgdiplus 2.10-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 12,636 kB
  • sloc: ansic: 102,856; sh: 13,336; makefile: 870
file content (158 lines) | stat: -rw-r--r-- 3,948 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifdef WIN32
#ifndef __cplusplus
#error Please compile with a C++ compiler.
#endif
#include <windows.h>
#include <GdiPlus.h>
#else
#include <GdiPlusFlat.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define C(func) assert(func == Ok)

#ifdef WIN32
using namespace Gdiplus;
using namespace DllExports;
#endif


struct bm 
{
	BitmapData data;
	char spoil1[128]; // Space eaten at by the real BitmapData ...
};


static void
test_gdip_clip()
{
	GpBitmap *bitmap = 0;
	GpBitmap *other_bitmap = 0;
	GpGraphics *graphics;
	GpRegion *clip;
	GpPath *path;
	GpPath *other_path;
	static const int width = 100;
	static const int height = 100;
	BYTE *scan0 = (BYTE*) GdipAlloc (width * height * 4);
	BOOL is_infinite = 0;
	GpRectF rect;
	GpRect recti;

	struct bm bm;
	struct bm other_bm;

	rect.X = 0.;
	rect.Y = 0.;
	rect.Width = width;
	rect.Height = height;
	recti.X = 0;
	recti.Y = 0;
	recti.Width = width;
	recti.Height = height;

	// Create bitmap

	C (GdipCreateBitmapFromScan0 (width, height, width * 4, PixelFormat32bppARGB, scan0, &bitmap));

	// Get graphics

	C (GdipGetImageGraphicsContext (bitmap, &graphics));

	// Check the clipping region is infinite

	C (GdipCreateRegion(&clip));

	C (GdipGetClip (graphics, clip));
	C (GdipIsInfiniteRegion(clip, graphics, &is_infinite));
	assert(is_infinite);

	// Create a path

	C (GdipCreatePath (FillModeAlternate, &path));
	C (GdipAddPathEllipse (path, 0, 0, 3, 2));
	C (GdipAddPathBezier (path, 30, 30, 70, 175, 210, 220, 20, 60));
	C (GdipAddPathRectangle (path, 200, 200, 60, 60));
	C (GdipAddPathArc (path, 0, 0, 150, 120, 30, 120));
	C (GdipAddPathEllipse (path, 50, 50, 50, 100));

	// Save the infinite clip
	C (GdipGetClip (graphics, clip));
	// Set the path as the clipping region
	C (GdipSetClipPath (graphics, path, CombineModeReplace));
	// Clear the clipped image (foreground)
	C (GdipGraphicsClear (graphics, 0x8000ff00));
	// Reset the clip
	C (GdipResetClip(graphics));

	// Clone the image for future use
	C (GdipCloneBitmapArea (0., 0., (float)width, (float)height, PixelFormat32bppARGB, bitmap, &other_bitmap));

	// Set the path as the clipping region
	C (GdipSetClipPath (graphics, path, CombineModeExclude));
	C (GdipGetClip (graphics, clip));
	C (GdipIsInfiniteRegion (clip, graphics, &is_infinite));
	assert(!is_infinite);

	// Clear the clipped image (background)
	C (GdipGraphicsClear (graphics, 0x80ff0000));

	// Now onto the second image

	// Get the graphics
	C (GdipGetImageGraphicsContext (other_bitmap, &graphics));
	// Get the infinite clip
	C (GdipGetClip (graphics, clip));
	// Dup the path
	C (GdipClonePath (path, &other_path));
	// Add the bounds to it
	C (GdipGetRegionBounds (clip, graphics, &rect));
	C (GdipAddPathRectangles (other_path, &rect, 1));
	// Reverse the path
	C (GdipReversePath (other_path));

	// Clip using the reversed path
	C (GdipSetClipPath (graphics, other_path, CombineModeReplace));
	// Clear the clipped image (background)
	C (GdipGraphicsClear (graphics, 0x80ff0000));

	// Compare the two images
	C (GdipBitmapLockBits (bitmap, &recti, ImageLockModeRead, PixelFormat32bppARGB, &bm.data));
	C (GdipBitmapLockBits (other_bitmap, &recti, ImageLockModeRead, PixelFormat32bppARGB, &other_bm.data));

	assert (bm.data.Width == other_bm.data.Width);
	assert (bm.data.Height == other_bm.data.Height);
	{
		int *p = (int*)bm.data.Scan0;
		int *other_p = (int*)other_bm.data.Scan0;
		int i;
		for (i = 0; i < width * height; ++i)
			assert(*p++ == *other_p++);
	}

	C (GdipBitmapUnlockBits (bitmap, &bm.data));
	C (GdipBitmapUnlockBits (other_bitmap, &other_bm.data));

	C (GdipDeleteRegion (clip));
	C (GdipDeleteGraphics (graphics));
	C (GdipDisposeImage (bitmap));

	GdipFree (scan0);
}

int
main(int argc, char**argv)
{
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

	test_gdip_clip();

	GdiplusShutdown(gdiplusToken);
	return 0;
}