xref: /haiku/src/tests/kits/interface/flatten_picture/PictureTest.cpp (revision b5c0078dc0013189eeb82ba7eef5a741230da3c6)
182afdb09SMichael Pfeiffer /*
282afdb09SMichael Pfeiffer  * Copyright 2007, Haiku. All rights reserved.
382afdb09SMichael Pfeiffer  * Distributed under the terms of the MIT License.
482afdb09SMichael Pfeiffer  *
582afdb09SMichael Pfeiffer  * Authors:
682afdb09SMichael Pfeiffer  *		Michael Pfeiffer
782afdb09SMichael Pfeiffer  */
882afdb09SMichael Pfeiffer 
92d201d5aSMichael Pfeiffer #include <GraphicsDefs.h>
1082afdb09SMichael Pfeiffer #include <InterfaceKit.h>
1182afdb09SMichael Pfeiffer #include <String.h>
1282afdb09SMichael Pfeiffer 
1382afdb09SMichael Pfeiffer #include <stdio.h>
1482afdb09SMichael Pfeiffer 
1582afdb09SMichael Pfeiffer #include "PictureTest.h"
1682afdb09SMichael Pfeiffer 
1782afdb09SMichael Pfeiffer #define TEST_AND_RETURN(condition, message, result) \
1882afdb09SMichael Pfeiffer 	{ \
1982afdb09SMichael Pfeiffer 		if (condition) { \
204f422578SMichael Pfeiffer 			SetErrorMessage(message); \
2182afdb09SMichael Pfeiffer 			return result; \
2282afdb09SMichael Pfeiffer 		} \
2382afdb09SMichael Pfeiffer 	}
2482afdb09SMichael Pfeiffer 
2582afdb09SMichael Pfeiffer template <class T>
2682afdb09SMichael Pfeiffer class AutoDelete
2782afdb09SMichael Pfeiffer {
2882afdb09SMichael Pfeiffer public:
AutoDelete(T * object)2982afdb09SMichael Pfeiffer 	AutoDelete(T *object) : fObject(object) { }
~AutoDelete()3082afdb09SMichael Pfeiffer 	~AutoDelete() { delete fObject; fObject = NULL; }
3182afdb09SMichael Pfeiffer 
Release()32acf1c6adSMichael Pfeiffer 	T* Release() { T* object = fObject; fObject = NULL; return object; }
3382afdb09SMichael Pfeiffer 
3482afdb09SMichael Pfeiffer private:
3582afdb09SMichael Pfeiffer 	T *fObject;
3682afdb09SMichael Pfeiffer };
3782afdb09SMichael Pfeiffer 
38acf1c6adSMichael Pfeiffer class OffscreenBitmap {
39acf1c6adSMichael Pfeiffer public:
40acf1c6adSMichael Pfeiffer 	OffscreenBitmap(BRect frame, color_space colorSpace);
41acf1c6adSMichael Pfeiffer 	virtual ~OffscreenBitmap();
InitCheck() const42acf1c6adSMichael Pfeiffer 	status_t InitCheck() const { return fStatus; }
4382afdb09SMichael Pfeiffer 
44acf1c6adSMichael Pfeiffer 	BView *View();
45acf1c6adSMichael Pfeiffer 	BBitmap *Copy();
46acf1c6adSMichael Pfeiffer 
47acf1c6adSMichael Pfeiffer private:
48acf1c6adSMichael Pfeiffer 	BRect fFrame;
49acf1c6adSMichael Pfeiffer 	color_space fColorSpace;
50acf1c6adSMichael Pfeiffer 	status_t fStatus;
51acf1c6adSMichael Pfeiffer 
52acf1c6adSMichael Pfeiffer 	BBitmap *fBitmap;
53acf1c6adSMichael Pfeiffer 	BView *fView;
54acf1c6adSMichael Pfeiffer };
55acf1c6adSMichael Pfeiffer 
OffscreenBitmap(BRect frame,color_space colorSpace)56acf1c6adSMichael Pfeiffer OffscreenBitmap::OffscreenBitmap(BRect frame, color_space colorSpace)
57acf1c6adSMichael Pfeiffer 	: fFrame(frame)
58acf1c6adSMichael Pfeiffer 	, fColorSpace(colorSpace)
59acf1c6adSMichael Pfeiffer 	, fStatus(B_ERROR)
60acf1c6adSMichael Pfeiffer 	, fBitmap(NULL)
61acf1c6adSMichael Pfeiffer 	, fView(NULL)
6282afdb09SMichael Pfeiffer {
63acf1c6adSMichael Pfeiffer 	BBitmap *bitmap = new BBitmap(frame, fColorSpace, true);
64acf1c6adSMichael Pfeiffer 	AutoDelete<BBitmap> _bitmap(bitmap);
65acf1c6adSMichael Pfeiffer 	if (bitmap == NULL || bitmap->IsValid() == false || bitmap->InitCheck() != B_OK)
66acf1c6adSMichael Pfeiffer 		return;
67acf1c6adSMichael Pfeiffer 
68acf1c6adSMichael Pfeiffer 	BView *view = new BView(frame, "offscreen", B_FOLLOW_ALL, B_WILL_DRAW);
69acf1c6adSMichael Pfeiffer 	AutoDelete<BView> _view(view);
70acf1c6adSMichael Pfeiffer 	if (view == NULL)
71acf1c6adSMichael Pfeiffer 		return;
72acf1c6adSMichael Pfeiffer 
73acf1c6adSMichael Pfeiffer 	bitmap->Lock();
74acf1c6adSMichael Pfeiffer 	bitmap->AddChild(view);
75acf1c6adSMichael Pfeiffer 	// bitmap is locked during the life time of this object
76acf1c6adSMichael Pfeiffer 
77acf1c6adSMichael Pfeiffer 	fBitmap = _bitmap.Release();
78acf1c6adSMichael Pfeiffer 	fView = _view.Release();
79acf1c6adSMichael Pfeiffer 	fStatus = B_OK;
80acf1c6adSMichael Pfeiffer }
81acf1c6adSMichael Pfeiffer 
~OffscreenBitmap()82acf1c6adSMichael Pfeiffer OffscreenBitmap::~OffscreenBitmap()
83acf1c6adSMichael Pfeiffer {
84acf1c6adSMichael Pfeiffer 	if (fStatus != B_OK)
85acf1c6adSMichael Pfeiffer 		return;
86acf1c6adSMichael Pfeiffer 
87acf1c6adSMichael Pfeiffer 	fView->RemoveSelf();
88acf1c6adSMichael Pfeiffer 	fBitmap->Unlock();
89acf1c6adSMichael Pfeiffer 	delete fView;
90acf1c6adSMichael Pfeiffer 	fView = NULL;
91acf1c6adSMichael Pfeiffer 
92acf1c6adSMichael Pfeiffer 	delete fBitmap;
93acf1c6adSMichael Pfeiffer 	fBitmap = NULL;
94acf1c6adSMichael Pfeiffer 
95acf1c6adSMichael Pfeiffer 	fStatus = B_ERROR;
96acf1c6adSMichael Pfeiffer }
97acf1c6adSMichael Pfeiffer 
98acf1c6adSMichael Pfeiffer BView *
View()99acf1c6adSMichael Pfeiffer OffscreenBitmap::View()
100acf1c6adSMichael Pfeiffer {
101acf1c6adSMichael Pfeiffer 	return fView;
10282afdb09SMichael Pfeiffer }
10382afdb09SMichael Pfeiffer 
10482afdb09SMichael Pfeiffer BBitmap*
Copy()105acf1c6adSMichael Pfeiffer OffscreenBitmap::Copy()
10682afdb09SMichael Pfeiffer {
107acf1c6adSMichael Pfeiffer 	// the result bitmap that does not accept views
108acf1c6adSMichael Pfeiffer 	// to save resources in the application server
109acf1c6adSMichael Pfeiffer 	BBitmap *copy = new BBitmap(fFrame, fColorSpace, false);
110acf1c6adSMichael Pfeiffer 	AutoDelete<BBitmap> _copy(copy);
111acf1c6adSMichael Pfeiffer 	if (copy == NULL || copy->IsValid() == false || copy->InitCheck() != B_OK)
112acf1c6adSMichael Pfeiffer 		return NULL;
113acf1c6adSMichael Pfeiffer 
114acf1c6adSMichael Pfeiffer 	fView->Sync();
115acf1c6adSMichael Pfeiffer 	fBitmap->Unlock();
116acf1c6adSMichael Pfeiffer 
117acf1c6adSMichael Pfeiffer 	memcpy(copy->Bits(), fBitmap->Bits(), fBitmap->BitsLength());
118acf1c6adSMichael Pfeiffer 
119acf1c6adSMichael Pfeiffer 	fBitmap->Lock();
120acf1c6adSMichael Pfeiffer 
121acf1c6adSMichael Pfeiffer 	return _copy.Release();
122acf1c6adSMichael Pfeiffer }
123acf1c6adSMichael Pfeiffer 
PictureTest()124acf1c6adSMichael Pfeiffer PictureTest::PictureTest()
125acf1c6adSMichael Pfeiffer 	: fColorSpace(B_RGBA32)
126acf1c6adSMichael Pfeiffer 	, fDirectBitmap(NULL)
127acf1c6adSMichael Pfeiffer 	, fBitmapFromPicture(NULL)
128acf1c6adSMichael Pfeiffer 	, fBitmapFromRestoredPicture(NULL)
129acf1c6adSMichael Pfeiffer {
130acf1c6adSMichael Pfeiffer }
131acf1c6adSMichael Pfeiffer 
132acf1c6adSMichael Pfeiffer 
133acf1c6adSMichael Pfeiffer BBitmap*
DirectBitmap(bool detach)134acf1c6adSMichael Pfeiffer PictureTest::DirectBitmap(bool detach)
135acf1c6adSMichael Pfeiffer {
136acf1c6adSMichael Pfeiffer 	BBitmap* bitmap = fDirectBitmap;
13782afdb09SMichael Pfeiffer 	if (detach)
138acf1c6adSMichael Pfeiffer 		fDirectBitmap = NULL;
139acf1c6adSMichael Pfeiffer 	return bitmap;
140acf1c6adSMichael Pfeiffer }
141acf1c6adSMichael Pfeiffer 
142acf1c6adSMichael Pfeiffer BBitmap*
BitmapFromPicture(bool detach)143acf1c6adSMichael Pfeiffer PictureTest::BitmapFromPicture(bool detach)
144acf1c6adSMichael Pfeiffer {
145acf1c6adSMichael Pfeiffer 	BBitmap* bitmap = fBitmapFromPicture;
146acf1c6adSMichael Pfeiffer 	if (detach)
147acf1c6adSMichael Pfeiffer 		fBitmapFromPicture = NULL;
14882afdb09SMichael Pfeiffer 	return bitmap;
14982afdb09SMichael Pfeiffer }
15082afdb09SMichael Pfeiffer 
15182afdb09SMichael Pfeiffer 
15282afdb09SMichael Pfeiffer BBitmap*
BitmapFromRestoredPicture(bool detach)153acf1c6adSMichael Pfeiffer PictureTest::BitmapFromRestoredPicture(bool detach)
15482afdb09SMichael Pfeiffer {
155acf1c6adSMichael Pfeiffer 	BBitmap* bitmap = fBitmapFromRestoredPicture;
15682afdb09SMichael Pfeiffer 	if (detach)
157acf1c6adSMichael Pfeiffer 		fBitmapFromRestoredPicture = NULL;
15882afdb09SMichael Pfeiffer 	return bitmap;
15982afdb09SMichael Pfeiffer }
16082afdb09SMichael Pfeiffer 
16182afdb09SMichael Pfeiffer 
~PictureTest()16282afdb09SMichael Pfeiffer PictureTest::~PictureTest()
16382afdb09SMichael Pfeiffer {
16482afdb09SMichael Pfeiffer 	CleanUp();
16582afdb09SMichael Pfeiffer }
16682afdb09SMichael Pfeiffer 
16782afdb09SMichael Pfeiffer void
CleanUp()16882afdb09SMichael Pfeiffer PictureTest::CleanUp()
16982afdb09SMichael Pfeiffer {
170acf1c6adSMichael Pfeiffer 	delete fBitmapFromPicture;
171acf1c6adSMichael Pfeiffer 	fBitmapFromPicture = NULL;
172acf1c6adSMichael Pfeiffer 	delete fBitmapFromRestoredPicture;
173acf1c6adSMichael Pfeiffer 	fBitmapFromRestoredPicture = NULL;
17482afdb09SMichael Pfeiffer 	fErrorMessage = "";
17582afdb09SMichael Pfeiffer }
17682afdb09SMichael Pfeiffer 
1774f422578SMichael Pfeiffer void
SetErrorMessage(const char * message)1784f422578SMichael Pfeiffer PictureTest::SetErrorMessage(const char *message)
1794f422578SMichael Pfeiffer {
1804f422578SMichael Pfeiffer 	if (fErrorMessage.Length() == 0)
1814f422578SMichael Pfeiffer 		fErrorMessage = message;
1824f422578SMichael Pfeiffer }
1834f422578SMichael Pfeiffer 
18482afdb09SMichael Pfeiffer bool
Test(draw_func * func,BRect frame)18582afdb09SMichael Pfeiffer PictureTest::Test(draw_func* func, BRect frame)
18682afdb09SMichael Pfeiffer {
18782afdb09SMichael Pfeiffer 	CleanUp();
18882afdb09SMichael Pfeiffer 
189acf1c6adSMichael Pfeiffer 	fDirectBitmap = CreateBitmap(func, frame);
190acf1c6adSMichael Pfeiffer 	TEST_AND_RETURN(fDirectBitmap == NULL, "Could not create direct draw bitmap!", false);
191acf1c6adSMichael Pfeiffer 
19282afdb09SMichael Pfeiffer 	BPicture *picture = RecordPicture(func, frame);
19382afdb09SMichael Pfeiffer 	AutoDelete<BPicture> _picture(picture);
19482afdb09SMichael Pfeiffer 	TEST_AND_RETURN(picture == NULL, "Picture could not be recorded!", false);
19582afdb09SMichael Pfeiffer 
19682afdb09SMichael Pfeiffer 	BPicture *archivedPicture = SaveAndRestore(picture);
19782afdb09SMichael Pfeiffer 	AutoDelete<BPicture> _archivedPicture(archivedPicture);
19882afdb09SMichael Pfeiffer 	TEST_AND_RETURN(picture == NULL, "Picture could not be flattened and unflattened!", false);
19982afdb09SMichael Pfeiffer 
200acf1c6adSMichael Pfeiffer 	fBitmapFromPicture = CreateBitmap(picture, frame);
201acf1c6adSMichael Pfeiffer 	TEST_AND_RETURN(fBitmapFromPicture == NULL, "Could not create bitmap from original picture!", false);
20282afdb09SMichael Pfeiffer 
203acf1c6adSMichael Pfeiffer 	fBitmapFromRestoredPicture = CreateBitmap(archivedPicture, frame);
204acf1c6adSMichael Pfeiffer 	TEST_AND_RETURN(fBitmapFromRestoredPicture == NULL, "Could not create bitmap from archived picture!", false);
20582afdb09SMichael Pfeiffer 
2062d201d5aSMichael Pfeiffer 	BString reason;
2072d201d5aSMichael Pfeiffer 	if (!IsSame(fDirectBitmap, fBitmapFromPicture, reason)) {
2082d201d5aSMichael Pfeiffer 		BString message("Bitmap from picture differs from direct drawing bitmap: ");
2092d201d5aSMichael Pfeiffer 		message += reason;
2102d201d5aSMichael Pfeiffer 		SetErrorMessage(message.String());
2112d201d5aSMichael Pfeiffer 		return false;
2122d201d5aSMichael Pfeiffer 	}
2132d201d5aSMichael Pfeiffer 	if (!IsSame(fDirectBitmap, fBitmapFromRestoredPicture, reason)) {
2142d201d5aSMichael Pfeiffer 		BString message("Bitmap from restored picture differs from direct drawing bitmap: ");
2152d201d5aSMichael Pfeiffer 		message += reason;
2162d201d5aSMichael Pfeiffer 		SetErrorMessage(message.String());
2172d201d5aSMichael Pfeiffer 		return false;
2182d201d5aSMichael Pfeiffer 	}
219acf1c6adSMichael Pfeiffer 	return true;
22082afdb09SMichael Pfeiffer }
22182afdb09SMichael Pfeiffer 
22282afdb09SMichael Pfeiffer 
223acf1c6adSMichael Pfeiffer BBitmap *
CreateBitmap(draw_func * func,BRect frame)224acf1c6adSMichael Pfeiffer PictureTest::CreateBitmap(draw_func* func, BRect frame)
225acf1c6adSMichael Pfeiffer {
226acf1c6adSMichael Pfeiffer 	OffscreenBitmap bitmap(frame, fColorSpace);
2274f422578SMichael Pfeiffer 	TEST_AND_RETURN(bitmap.InitCheck() != B_OK, "Offscreen bitmap for direct drawing could not be created!" , NULL);
228acf1c6adSMichael Pfeiffer 	func(bitmap.View(), frame);
229acf1c6adSMichael Pfeiffer 	return bitmap.Copy();
230acf1c6adSMichael Pfeiffer }
231acf1c6adSMichael Pfeiffer 
23282afdb09SMichael Pfeiffer BPicture *
RecordPicture(draw_func * func,BRect frame)23382afdb09SMichael Pfeiffer PictureTest::RecordPicture(draw_func* func, BRect frame)
23482afdb09SMichael Pfeiffer {
235acf1c6adSMichael Pfeiffer 	OffscreenBitmap bitmap(frame, fColorSpace);
2364f422578SMichael Pfeiffer 	TEST_AND_RETURN(bitmap.InitCheck() != B_OK, "Offscreen bitmap for picture recording could not be created!" , NULL);
23782afdb09SMichael Pfeiffer 
238acf1c6adSMichael Pfeiffer 	BView *view = bitmap.View();
23982afdb09SMichael Pfeiffer 	// record
24082afdb09SMichael Pfeiffer 	BPicture *picture = new BPicture();
24182afdb09SMichael Pfeiffer 	view->BeginPicture(picture);
24282afdb09SMichael Pfeiffer 	func(view, frame);
24382afdb09SMichael Pfeiffer 	picture = view->EndPicture();
24482afdb09SMichael Pfeiffer 
24582afdb09SMichael Pfeiffer 	return picture;
24682afdb09SMichael Pfeiffer }
24782afdb09SMichael Pfeiffer 
248acf1c6adSMichael Pfeiffer BBitmap *
CreateBitmap(BPicture * picture,BRect frame)249acf1c6adSMichael Pfeiffer PictureTest::CreateBitmap(BPicture *picture, BRect frame)
250acf1c6adSMichael Pfeiffer {
251acf1c6adSMichael Pfeiffer 	OffscreenBitmap bitmap(frame, fColorSpace);
2524f422578SMichael Pfeiffer 	TEST_AND_RETURN(bitmap.InitCheck() != B_OK, "Offscreen bitmap for picture drawing could not be created!" , NULL);
253acf1c6adSMichael Pfeiffer 
254acf1c6adSMichael Pfeiffer 	BView *view = bitmap.View();
255acf1c6adSMichael Pfeiffer 	view->DrawPicture(picture);
256acf1c6adSMichael Pfeiffer 	view->Sync();
257acf1c6adSMichael Pfeiffer 
258acf1c6adSMichael Pfeiffer 	return bitmap.Copy();
259acf1c6adSMichael Pfeiffer }
260acf1c6adSMichael Pfeiffer 
261acf1c6adSMichael Pfeiffer 
setMismatchReason(int32 x,int32 y,uint8 * pixel1,uint8 * pixel2,int32 bpp,BString & reason)2622d201d5aSMichael Pfeiffer static void setMismatchReason(int32 x, int32 y, uint8 *pixel1, uint8 *pixel2,
2632d201d5aSMichael Pfeiffer 	int32 bpp, BString &reason)
264acf1c6adSMichael Pfeiffer {
2652d201d5aSMichael Pfeiffer 	char buffer1[32];
2662d201d5aSMichael Pfeiffer 	char buffer2[32];
2672d201d5aSMichael Pfeiffer 	uint32 color1 = 0;
2682d201d5aSMichael Pfeiffer 	uint32 color2 = 0;
2692d201d5aSMichael Pfeiffer 	memcpy(&color1, pixel1, bpp);
2702d201d5aSMichael Pfeiffer 	memcpy(&color2, pixel2, bpp);
2712d201d5aSMichael Pfeiffer 	sprintf(buffer1, "0x%8.8x", (int)color1);
2722d201d5aSMichael Pfeiffer 	sprintf(buffer2, "0x%8.8x", (int)color2);
273acf1c6adSMichael Pfeiffer 
2742d201d5aSMichael Pfeiffer 	reason = "Pixel at ";
2752d201d5aSMichael Pfeiffer 	reason << x << ", " << y << " differs: " << buffer1 << " != " << buffer2;
2762d201d5aSMichael Pfeiffer }
277acf1c6adSMichael Pfeiffer 
2782d201d5aSMichael Pfeiffer 
2792d201d5aSMichael Pfeiffer bool
IsSame(BBitmap * bitmap1,BBitmap * bitmap2,BString & reason)2802d201d5aSMichael Pfeiffer PictureTest::IsSame(BBitmap *bitmap1, BBitmap *bitmap2, BString &reason)
2812d201d5aSMichael Pfeiffer {
2822d201d5aSMichael Pfeiffer 	if (bitmap1->ColorSpace() != bitmap2->ColorSpace()) {
2832d201d5aSMichael Pfeiffer 		reason = "ColorSpace() differs";
2842d201d5aSMichael Pfeiffer 		return false;
2852d201d5aSMichael Pfeiffer 	}
2862d201d5aSMichael Pfeiffer 
2872d201d5aSMichael Pfeiffer 	if (bitmap1->BitsLength() != bitmap2->BitsLength()) {
2882d201d5aSMichael Pfeiffer 		reason = "BitsLength() differs";
2892d201d5aSMichael Pfeiffer 		return false;
2902d201d5aSMichael Pfeiffer 	}
2912d201d5aSMichael Pfeiffer 
2922d201d5aSMichael Pfeiffer 	size_t rowAlignment;
2932d201d5aSMichael Pfeiffer 	size_t pixelChunk;
2942d201d5aSMichael Pfeiffer 	size_t pixelsPerChunk;
295*b5c0078dSMichael Pfeiffer 	if (get_pixel_size_for(bitmap1->ColorSpace(), &pixelChunk, &rowAlignment,
2962d201d5aSMichael Pfeiffer 		&pixelsPerChunk) != B_OK) {
2972d201d5aSMichael Pfeiffer 		reason = "get_pixel_size_for() not supported for this color space";
2982d201d5aSMichael Pfeiffer 		return false;
2992d201d5aSMichael Pfeiffer 	}
3002d201d5aSMichael Pfeiffer 	if (pixelsPerChunk != 1) {
3012d201d5aSMichael Pfeiffer 		reason = "Unsupported color_space; IsSame(...) supports 1 pixels per chunk only";
3022d201d5aSMichael Pfeiffer 		return false;
3032d201d5aSMichael Pfeiffer 	}
3042d201d5aSMichael Pfeiffer 	int32 bpp = (int32)pixelChunk;
3052d201d5aSMichael Pfeiffer 	uint8* row1 = (uint8*)bitmap1->Bits();
3062d201d5aSMichael Pfeiffer 	uint8* row2 = (uint8*)bitmap2->Bits();
3072d201d5aSMichael Pfeiffer 	int32 bpr = bitmap1->BytesPerRow();
3082d201d5aSMichael Pfeiffer 	int32 width = bitmap1->Bounds().IntegerWidth() + 1;
3092d201d5aSMichael Pfeiffer 	int32 height = bitmap1->Bounds().IntegerHeight() + 1;
3102d201d5aSMichael Pfeiffer 	for (int y = 0; y < height; y ++, row1 += bpr, row2 += bpr) {
3112d201d5aSMichael Pfeiffer 		uint8* pixel1 = row1;
3122d201d5aSMichael Pfeiffer 		uint8* pixel2 = row2;
3132d201d5aSMichael Pfeiffer 		for (int x = 0; x < width; x ++, pixel1 += bpp, pixel2 += bpp) {
3142d201d5aSMichael Pfeiffer 			if (memcmp(pixel1, pixel2, bpp) != 0) {
3152d201d5aSMichael Pfeiffer 				setMismatchReason(x, y, pixel1, pixel2, bpp, reason);
3162d201d5aSMichael Pfeiffer 				return false;
3172d201d5aSMichael Pfeiffer 			}
3182d201d5aSMichael Pfeiffer 		}
3192d201d5aSMichael Pfeiffer 	}
3202d201d5aSMichael Pfeiffer 
3212d201d5aSMichael Pfeiffer 	reason = "";
3222d201d5aSMichael Pfeiffer 	return true;
323acf1c6adSMichael Pfeiffer }
324acf1c6adSMichael Pfeiffer 
325acf1c6adSMichael Pfeiffer 
FlattenPictureTest()326acf1c6adSMichael Pfeiffer FlattenPictureTest::FlattenPictureTest()
327acf1c6adSMichael Pfeiffer {
328acf1c6adSMichael Pfeiffer }
329acf1c6adSMichael Pfeiffer 
33082afdb09SMichael Pfeiffer BPicture *
SaveAndRestore(BPicture * picture)331acf1c6adSMichael Pfeiffer FlattenPictureTest::SaveAndRestore(BPicture *picture)
33282afdb09SMichael Pfeiffer {
33382afdb09SMichael Pfeiffer 	BMallocIO *data = new BMallocIO();
33482afdb09SMichael Pfeiffer 	AutoDelete<BMallocIO> _data(data);
3354f422578SMichael Pfeiffer 	TEST_AND_RETURN(data == NULL, "BMallocIO could not be allocated for flattening the picture!" , NULL);
33682afdb09SMichael Pfeiffer 
33782afdb09SMichael Pfeiffer 	picture->Flatten(data);
33882afdb09SMichael Pfeiffer 
33982afdb09SMichael Pfeiffer 	data->Seek(0, SEEK_SET);
34082afdb09SMichael Pfeiffer 	BPicture *archivedPicture = new BPicture();
3414f422578SMichael Pfeiffer 	TEST_AND_RETURN(archivedPicture == NULL, "BPicture could not be allocated for unflattening the picture!" , NULL);
34282afdb09SMichael Pfeiffer 	archivedPicture->Unflatten(data);
34382afdb09SMichael Pfeiffer 
34482afdb09SMichael Pfeiffer 	return archivedPicture;
34582afdb09SMichael Pfeiffer }
34682afdb09SMichael Pfeiffer 
ArchivePictureTest()347acf1c6adSMichael Pfeiffer ArchivePictureTest::ArchivePictureTest()
34882afdb09SMichael Pfeiffer {
34982afdb09SMichael Pfeiffer }
35082afdb09SMichael Pfeiffer 
351acf1c6adSMichael Pfeiffer BPicture *
SaveAndRestore(BPicture * picture)352acf1c6adSMichael Pfeiffer ArchivePictureTest::SaveAndRestore(BPicture *picture)
35382afdb09SMichael Pfeiffer {
354acf1c6adSMichael Pfeiffer 	BMessage archive;
3554f422578SMichael Pfeiffer 	TEST_AND_RETURN(picture->Archive(&archive) != B_OK, "Picture could not be archived to BMessage", NULL);
35682afdb09SMichael Pfeiffer 
3574f422578SMichael Pfeiffer 	BArchivable *archivable = BPicture::Instantiate(&archive);
3584f422578SMichael Pfeiffer 	AutoDelete<BArchivable> _archivable(archivable);
3594f422578SMichael Pfeiffer 	TEST_AND_RETURN(archivable == NULL, "Picture could not be instantiated from BMessage", NULL);
36082afdb09SMichael Pfeiffer 
3614f422578SMichael Pfeiffer 	BPicture *archivedPicture = dynamic_cast<BPicture*>(archivable);
3624f422578SMichael Pfeiffer 	TEST_AND_RETURN(archivedPicture == NULL, "Picture could not be restored from BMessage", NULL);
3634f422578SMichael Pfeiffer 
3644f422578SMichael Pfeiffer 	_archivable.Release();
365acf1c6adSMichael Pfeiffer 	return archivedPicture;
36682afdb09SMichael Pfeiffer }
36762c7fd0fSMichael Pfeiffer 
368