1*e89985ccSAdrien Destugues /*
2*e89985ccSAdrien Destugues * Copyright 2019, Adrien Destugues <pulkomandy@pulkomandy.tk>
3*e89985ccSAdrien Destugues * Distributed under terms of the MIT license.
4*e89985ccSAdrien Destugues */
5*e89985ccSAdrien Destugues
6*e89985ccSAdrien Destugues
7*e89985ccSAdrien Destugues #include <Application.h>
8*e89985ccSAdrien Destugues #include <Bitmap.h>
9*e89985ccSAdrien Destugues #include <View.h>
10*e89985ccSAdrien Destugues #include <Window.h>
11*e89985ccSAdrien Destugues
12*e89985ccSAdrien Destugues #include <assert.h>
13*e89985ccSAdrien Destugues
14*e89985ccSAdrien Destugues
15*e89985ccSAdrien Destugues class BitmapView: public BView
16*e89985ccSAdrien Destugues {
17*e89985ccSAdrien Destugues public:
BitmapView(BBitmap * bitmap)18*e89985ccSAdrien Destugues BitmapView(BBitmap* bitmap)
19*e89985ccSAdrien Destugues : BView(bitmap->Bounds(), "test view", B_FOLLOW_LEFT_TOP, B_WILL_DRAW)
20*e89985ccSAdrien Destugues , fBitmap(bitmap)
21*e89985ccSAdrien Destugues {
22*e89985ccSAdrien Destugues }
23*e89985ccSAdrien Destugues
~BitmapView()24*e89985ccSAdrien Destugues ~BitmapView()
25*e89985ccSAdrien Destugues {
26*e89985ccSAdrien Destugues delete fBitmap;
27*e89985ccSAdrien Destugues }
28*e89985ccSAdrien Destugues
Draw(BRect updateRect)29*e89985ccSAdrien Destugues void Draw(BRect updateRect)
30*e89985ccSAdrien Destugues {
31*e89985ccSAdrien Destugues DrawBitmap(fBitmap);
32*e89985ccSAdrien Destugues }
33*e89985ccSAdrien Destugues
34*e89985ccSAdrien Destugues private:
35*e89985ccSAdrien Destugues BBitmap* fBitmap;
36*e89985ccSAdrien Destugues };
37*e89985ccSAdrien Destugues
38*e89985ccSAdrien Destugues
39*e89985ccSAdrien Destugues int
main(void)40*e89985ccSAdrien Destugues main(void)
41*e89985ccSAdrien Destugues {
42*e89985ccSAdrien Destugues BApplication app("application/Haiku-BitmapTest");
43*e89985ccSAdrien Destugues
44*e89985ccSAdrien Destugues BWindow* window = new BWindow(BRect(10, 10, 100, 100),
45*e89985ccSAdrien Destugues "Bitmap drawing test", B_DOCUMENT_WINDOW, B_QUIT_ON_WINDOW_CLOSE);
46*e89985ccSAdrien Destugues window->Show();
47*e89985ccSAdrien Destugues
48*e89985ccSAdrien Destugues BBitmap* bitmap = new BBitmap(BRect(0, 0, 24, 24), B_GRAY1);
49*e89985ccSAdrien Destugues
50*e89985ccSAdrien Destugues // Bitmap is 25 pixels wide, which rounds up to 4 pixels
51*e89985ccSAdrien Destugues // The last byte only has one bit used, and 7 bits of padding
52*e89985ccSAdrien Destugues assert(bitmap->BytesPerRow() == 4);
53*e89985ccSAdrien Destugues
54*e89985ccSAdrien Destugues // This was extracted from letter_a.pbm and should look mostly like a
55*e89985ccSAdrien Destugues // black "A" letter on a white background (confirmed BeOS behavior)
56*e89985ccSAdrien Destugues const unsigned char data[] = {
57*e89985ccSAdrien Destugues 0, 0, 0, 0,
58*e89985ccSAdrien Destugues 0, 8, 0, 0,
59*e89985ccSAdrien Destugues 0, 0x1c, 0, 0,
60*e89985ccSAdrien Destugues 0, 0x3e, 0, 0,
61*e89985ccSAdrien Destugues 0, 0x7e, 0, 0,
62*e89985ccSAdrien Destugues 0, 0xFF, 0, 0,
63*e89985ccSAdrien Destugues 0, 0xE7, 0, 0,
64*e89985ccSAdrien Destugues 0, 0xC3, 0, 0,
65*e89985ccSAdrien Destugues 1, 0xC3, 0x80, 0,
66*e89985ccSAdrien Destugues 1, 0x81, 0x80, 0,
67*e89985ccSAdrien Destugues 3, 0x81, 0xC0, 0,
68*e89985ccSAdrien Destugues 3, 0xFF, 0xC0, 0,
69*e89985ccSAdrien Destugues 7, 0xFF, 0xE0, 0,
70*e89985ccSAdrien Destugues 7, 0xFF, 0xE0, 0,
71*e89985ccSAdrien Destugues 7, 0x81, 0xE0, 0,
72*e89985ccSAdrien Destugues 0x0F, 0, 0x0F, 0,
73*e89985ccSAdrien Destugues 0x0F, 0, 0x0F, 0,
74*e89985ccSAdrien Destugues 0x1F, 0, 0xF8, 0,
75*e89985ccSAdrien Destugues 0x1E, 0, 0x78, 0,
76*e89985ccSAdrien Destugues 0x1C, 0, 0x38, 0,
77*e89985ccSAdrien Destugues 0x3C, 0, 0x3C, 0,
78*e89985ccSAdrien Destugues 0x3C, 0, 0x3C, 0,
79*e89985ccSAdrien Destugues 0x38, 0, 0x0E, 0,
80*e89985ccSAdrien Destugues 0x78, 0, 0x0F, 0,
81*e89985ccSAdrien Destugues 0, 0, 0, 0
82*e89985ccSAdrien Destugues };
83*e89985ccSAdrien Destugues bitmap->SetBits(data, sizeof(data), 0, B_GRAY1);
84*e89985ccSAdrien Destugues
85*e89985ccSAdrien Destugues BView* view = new BitmapView(bitmap);
86*e89985ccSAdrien Destugues window->AddChild(view);
87*e89985ccSAdrien Destugues
88*e89985ccSAdrien Destugues app.Run();
89*e89985ccSAdrien Destugues return 0;
90*e89985ccSAdrien Destugues }
91