1/*
2 * Copyright 2006, Haiku. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Stephan A��mus <superstippi@gmx.de>
7 */
8
9#include "BitmapExporter.h"
10
11#include <Bitmap.h>
12#include <BitmapStream.h>
13#include <TranslatorFormats.h>
14#include <TranslatorRoster.h>
15
16#include "Icon.h"
17#include "IconRenderer.h"
18#include "ReferenceImage.h"
19
20
21// constructor
22BitmapExporter::BitmapExporter(uint32 size)
23	: Exporter(),
24	  fFormat(B_PNG_FORMAT),
25	  fSize(size)
26{
27}
28
29// destructor
30BitmapExporter::~BitmapExporter()
31{
32}
33
34// Export
35status_t
36BitmapExporter::Export(const Icon* icon, BPositionIO* stream)
37{
38	if (fSize == 0)
39		return B_NO_INIT;
40
41	// render icon into bitmap with given size and transparent background
42	uint32 bitmapFlags = 0;
43
44	#if __HAIKU__
45	bitmapFlags |= B_BITMAP_NO_SERVER_LINK;
46	#endif
47
48	BBitmap bitmap(BRect(0, 0, fSize - 1, fSize - 1),
49				   bitmapFlags, B_RGBA32);
50
51	status_t ret  = bitmap.InitCheck();
52	if (ret < B_OK)
53		return ret;
54
55	IconRenderer renderer(&bitmap);
56	renderer.SetIcon(icon);
57	renderer.SetScale(fSize / 64.0);
58	renderer.Render(false);
59//	renderer.Demultiply(&bitmap);
60
61	// save bitmap to translator
62	BTranslatorRoster* roster = BTranslatorRoster::Default();
63	if (!roster)
64		return B_ERROR;
65
66	BBitmapStream bitmapStream(&bitmap);
67	ret = roster->Translate(&bitmapStream, NULL, NULL, stream, fFormat, 0);
68
69	BBitmap* dummy;
70	bitmapStream.DetachBitmap(&dummy);
71
72	return ret;
73}
74
75// MIMEType
76const char*
77BitmapExporter::MIMEType()
78{
79	// TODO: ...
80	return "image/png";
81}
82
83