1142425Snectar/*
2160814Ssimon * Copyright 2006, Haiku. All rights reserved.
3142425Snectar * Distributed under the terms of the MIT License.
4142425Snectar *
5142425Snectar * Authors:
6142425Snectar *		Stephan A��mus <superstippi@gmx.de>
7142425Snectar */
8142425Snectar
9142425Snectar#include "BitmapExporter.h"
10142425Snectar
11142425Snectar#include <Bitmap.h>
12142425Snectar#include <BitmapStream.h>
13142425Snectar#include <TranslatorFormats.h>
14142425Snectar#include <TranslatorRoster.h>
15142425Snectar
16142425Snectar#include "Icon.h"
17142425Snectar#include "IconRenderer.h"
18160814Ssimon
19142425Snectar// constructor
20142425SnectarBitmapExporter::BitmapExporter(uint32 size)
21160814Ssimon	: Exporter(),
22142425Snectar	  fFormat(B_PNG_FORMAT),
23142425Snectar	  fSize(size)
24142425Snectar{
25160814Ssimon}
26160814Ssimon
27142425Snectar// destructor
28142425SnectarBitmapExporter::~BitmapExporter()
29142425Snectar{
30142425Snectar}
31142425Snectar
32142425Snectar// Export
33142425Snectarstatus_t
34142425SnectarBitmapExporter::Export(const Icon* icon, BPositionIO* stream)
35142425Snectar{
36142425Snectar	if (fSize == 0)
37142425Snectar		return B_NO_INIT;
38142425Snectar
39142425Snectar	// render icon into bitmap with given size and transparent background
40142425Snectar	uint32 bitmapFlags = 0;
41194206Ssimon
42142425Snectar	#if __HAIKU__
43142425Snectar	bitmapFlags |= B_BITMAP_NO_SERVER_LINK;
44142425Snectar	#endif
45160814Ssimon
46160814Ssimon	BBitmap bitmap(BRect(0, 0, fSize - 1, fSize - 1),
47160814Ssimon				   bitmapFlags, B_RGBA32);
48160814Ssimon
49160814Ssimon	status_t ret  = bitmap.InitCheck();
50160814Ssimon	if (ret < B_OK)
51160814Ssimon		return ret;
52160814Ssimon
53160814Ssimon	IconRenderer renderer(&bitmap);
54160814Ssimon	renderer.SetIcon(icon);
55142425Snectar	renderer.SetScale(fSize / 64.0);
56160814Ssimon	renderer.Render();
57160814Ssimon//	renderer.Demultiply(&bitmap);
58160814Ssimon
59160814Ssimon	// save bitmap to translator
60142425Snectar	BTranslatorRoster* roster = BTranslatorRoster::Default();
61160814Ssimon	if (!roster)
62194206Ssimon		return B_ERROR;
63160814Ssimon
64160814Ssimon	BBitmapStream bitmapStream(&bitmap);
65160814Ssimon	ret = roster->Translate(&bitmapStream, NULL, NULL, stream, fFormat, 0);
66160814Ssimon
67142425Snectar	BBitmap* dummy;
68194206Ssimon	bitmapStream.DetachBitmap(&dummy);
69194206Ssimon
70194206Ssimon	return ret;
71194206Ssimon}
72194206Ssimon
73142425Snectar// MIMEType
74142425Snectarconst char*
75142425SnectarBitmapExporter::MIMEType()
76142425Snectar{
77142425Snectar	// TODO: ...
78142425Snectar	return "image/png";
79142425Snectar}
80142425Snectar
81142425Snectar