1/**************************************************************************
2 *
3 * Copyright 2009 Artur Wyszynski <harakash@gmail.com>
4 * Copyright 2013 Alexander von Gluck IV <kallisti5@unixzen.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 **************************************************************************/
27
28
29#include <stdio.h>
30#include <interface/Bitmap.h>
31#include <storage/File.h>
32#include <support/String.h>
33#include <translation/BitmapStream.h>
34#include <translation/TranslatorRoster.h>
35
36#include "bitmap_wrapper.h"
37
38
39extern "C" {
40static int frameNo = 0;
41
42
43Bitmap*
44create_bitmap(int32 width, int32 height, color_space colorSpace)
45{
46	BBitmap *bb = new BBitmap(BRect(0, 0, width, height), colorSpace);
47	if (bb)
48		return (Bitmap*)bb;
49	return NULL;
50}
51
52
53void
54get_bitmap_size(const Bitmap* bitmap, int32* width, int32* height)
55{
56	BBitmap *bb = (BBitmap*)bitmap;
57	if (bb && width && height) {
58		uint32 w = bb->Bounds().IntegerWidth() + 1;
59		uint32 h = bb->Bounds().IntegerHeight() + 1;
60		*width = w;
61		*height = h;
62	}
63}
64
65
66color_space
67get_bitmap_color_space(const Bitmap* bitmap)
68{
69	BBitmap *bb = (BBitmap*)bitmap;
70	if (bb)
71		return bb->ColorSpace();
72	return B_NO_COLOR_SPACE;
73}
74
75
76void
77copy_bitmap_bits(const Bitmap* bitmap, void* data, int32 length)
78{
79	BBitmap *bb = (BBitmap*)bitmap;
80
81	// We assume the data is 1:1 the format of the bitmap
82	if (bb)
83		bb->ImportBits(data, length, bb->BytesPerRow(), 0, bb->ColorSpace());
84}
85
86
87void
88import_bitmap_bits(const Bitmap* bitmap, void* data, int32 length,
89	unsigned srcStride, color_space srcColorSpace)
90{
91	BBitmap *bb = (BBitmap*)bitmap;
92
93	// Import image and adjust image format from source to dest
94	if (bb)
95		bb->ImportBits(data, length, srcStride, 0, srcColorSpace);
96}
97
98
99void
100delete_bitmap(Bitmap* bitmap)
101{
102	BBitmap *bb = (BBitmap*)bitmap;
103	delete bb;
104}
105
106
107int32
108get_bitmap_bytes_per_row(const Bitmap* bitmap)
109{
110	BBitmap *bb = (BBitmap*)bitmap;
111	if (bb)
112		return bb->BytesPerRow();
113	return 0;
114}
115
116
117int32
118get_bitmap_bits_length(const Bitmap* bitmap)
119{
120	BBitmap *bb = (BBitmap*)bitmap;
121	if (bb)
122		return bb->BitsLength();
123	return 0;
124}
125
126
127void
128dump_bitmap(const Bitmap* bitmap)
129{
130	BBitmap *bb = (BBitmap*)bitmap;
131	if (!bb)
132		return;
133
134	BString filename("/boot/home/frame_");
135	filename << (int32)frameNo << ".png";
136
137	BTranslatorRoster *roster = BTranslatorRoster::Default();
138	BBitmapStream stream(bb);
139	BFile dump(filename, B_CREATE_FILE | B_WRITE_ONLY);
140
141	roster->Translate(&stream, NULL, NULL, &dump, 0);
142
143	frameNo++;
144}
145
146}
147