summaryrefslogtreecommitdiffstats
path: root/beos/PictureWin.cpp
blob: 23aabe7c6edf7fb27b2e93d3d43e1bef2e5d4811 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* $Id: PictureWin.cpp,v 1.5 2003/09/30 14:38:15 titer Exp $

   This file is part of the HandBrake source code.
   Homepage: <http://beos.titer.org/handbrake/>.
   It may be used under the terms of the GNU General Public License. */

#include <Bitmap.h>
#include <Box.h>
#include <Button.h>
#include <CheckBox.h>
#include <Screen.h>
#include <Slider.h>

#include "PictureWin.h"
#include "Manager.h"

#define UPDATE_BITMAP 'upbi'

/* Handy way to access HBTitle members */
#define fInWidth      fTitle->fInWidth
#define fInHeight     fTitle->fInHeight
#define fPixelWidth   fTitle->fPixelWidth
#define fPixelHeight  fTitle->fPixelHeight
#define fDeinterlace  fTitle->fDeinterlace
#define fOutWidth     fTitle->fOutWidth
#define fOutHeight    fTitle->fOutHeight
#define fOutWidthMax  fTitle->fOutWidthMax
#define fOutHeightMax fTitle->fOutHeightMax
#define fTopCrop      fTitle->fTopCrop
#define fBottomCrop   fTitle->fBottomCrop
#define fLeftCrop     fTitle->fLeftCrop
#define fRightCrop    fTitle->fRightCrop

HBPictureView::HBPictureView( BRect rect, BBitmap * bitmap )
    : BView( rect, NULL, B_FOLLOW_ALL, B_WILL_DRAW )
{
    fBitmap = bitmap;
}

void HBPictureView::Draw( BRect rect )
{
    if( LockLooper() )
    {
        DrawBitmap( fBitmap, Bounds() );
        UnlockLooper();
    }
    else
    {
        Log( "HBPictureView::Draw() : LockLooper() failed" );
    }

    BView::Draw( rect );
}


/* Constructor */
HBPictureWin::HBPictureWin( HBManager * manager, HBTitle * title )
    : BWindow( BRect( 0, 0, 0, 0 ), "Picture settings",
               B_FLOATING_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL,
               B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_NOT_CLOSABLE )
{
    fManager = manager;
    fTitle   = title;

    /* Resize & center */
    ResizeTo( fOutWidthMax + 40, fOutHeightMax + 280 );
    BScreen screen;
    MoveTo( ( screen.Frame().Width() - Frame().Width() ) / 2,
            ( screen.Frame().Height() - Frame().Height() ) / 2 );

    /* Build the GUI */
    BRect r;

    /* Add a background view */
    BView * view;
    view = new BView( Bounds(), NULL, B_FOLLOW_ALL, B_WILL_DRAW );
    view->SetViewColor( ui_color( B_PANEL_BACKGROUND_COLOR ) );
    AddChild( view );

    /* First box : picture + slider */
    r = BRect( 10, 10, fOutWidthMax + 31, fOutHeightMax + 60 );
    BBox * pictureBox;
    pictureBox = new BBox( r, NULL );
    pictureBox->SetLabel( "Preview" );

    /* Leave a one-pixel margin to draw the white line around the picture */
    fBitmap = new BBitmap( BRect( 0, 0, fOutWidthMax + 1,
                                  fOutHeightMax + 1 ), 0, B_RGB32 );

    /* Picture view */
    r = BRect( 10, 15, fOutWidthMax + 11, fOutHeightMax + 16 );
    fPictureView = new HBPictureView( r, fBitmap );
    pictureBox->AddChild( fPictureView );

    /* Slider */
    r = BRect( 10, fOutHeightMax + 25, fOutWidthMax + 11,
               fOutHeightMax + 55 );
    fPictureSlider = new BSlider( r, NULL, NULL,
                                  new BMessage( UPDATE_BITMAP ), 0, 9 );
    pictureBox->AddChild( fPictureSlider );

    view->AddChild( pictureBox );

    /* Second box : resize & crop settings */
    r = BRect( 10, fOutHeightMax + 75, fOutWidthMax + 31,
               fOutHeightMax + 235 );
    BBox * settingsBox;
    settingsBox = new BBox( r, NULL );
    settingsBox->SetLabel( "Settings" );

    r = BRect( 10, 15, fOutWidthMax + 11, 30 );
    fDeinterlaceCheck = new BCheckBox( r, NULL, "Deinterlace",
                                       new BMessage( UPDATE_BITMAP ) );
    fDeinterlaceCheck->SetValue( fDeinterlace ? 1 : 0 );
    settingsBox->AddChild( fDeinterlaceCheck );

    r = BRect( 10, 40, fOutWidthMax + 11, 70 );
    fWidthSlider = new BSlider( r, NULL, "Picture size",
                                new BMessage( UPDATE_BITMAP ),
                                1, fOutWidthMax / 16,
                                B_TRIANGLE_THUMB );
    fWidthSlider->SetValue( fOutWidth / 16 );
    settingsBox->AddChild( fWidthSlider );

    r = BRect( 10, 80, ( fOutWidthMax / 2 ) + 5, 110 );
    fTopCropSlider = new BSlider( r, NULL, "Top cropping",
                                  new BMessage( UPDATE_BITMAP ),
                                  0, fInHeight / 4,
                                  B_TRIANGLE_THUMB );
    fTopCropSlider->SetValue( fTopCrop / 2 );
    settingsBox->AddChild( fTopCropSlider );

    r = BRect( ( fOutWidthMax / 2 ) + 15, 80, fOutWidthMax + 11, 110 );
    fBottomCropSlider = new BSlider( r, NULL, "Bottom cropping",
                                     new BMessage( UPDATE_BITMAP ),
                                     0, fInHeight / 4,
                                     B_TRIANGLE_THUMB );
    fBottomCropSlider->SetValue( fBottomCrop / 2 );
    settingsBox->AddChild( fBottomCropSlider );

    r = BRect( 10, 120, ( fOutWidthMax / 2 ) + 5, 150 );
    fLeftCropSlider = new BSlider( r, NULL, "Left cropping",
                                   new BMessage( UPDATE_BITMAP ),
                                   0, fInWidth / 4,
                                   B_TRIANGLE_THUMB );
    fLeftCropSlider->SetValue( fLeftCrop / 2 );
    settingsBox->AddChild( fLeftCropSlider );

    r = BRect( ( fOutWidthMax / 2 ) + 15, 120, fOutWidthMax + 11, 150 );
    fRightCropSlider = new BSlider( r, NULL, "Right cropping",
                                    new BMessage( UPDATE_BITMAP ),
                                    0, fInWidth / 4,
                                    B_TRIANGLE_THUMB );
    fRightCropSlider->SetValue( fRightCrop / 2 );
    settingsBox->AddChild( fRightCropSlider );

    view->AddChild( settingsBox );

    /* "Close" button */
    r = BRect( fOutWidthMax - 49, fOutHeightMax + 245,
               fOutWidthMax + 31, fOutHeightMax + 270 );
    BButton * button = new BButton( r, NULL, "OK",
                                    new BMessage( B_QUIT_REQUESTED ) );
    view->AddChild( button );

    UpdateBitmap( 0 );
}

void HBPictureWin::MessageReceived( BMessage * message )
{
    switch( message->what )
    {
        case UPDATE_BITMAP:
            UpdateBitmap( fPictureSlider->Value() );
            fPictureView->Draw( fPictureView->Bounds() );
            break;

        default:
            BWindow::MessageReceived( message );
    }
}

void HBPictureWin::UpdateBitmap( int image )
{
    fOutWidth    = 16 * fWidthSlider->Value();
    fTopCrop     = 2 * fTopCropSlider->Value();
    fBottomCrop  = 2 * fBottomCropSlider->Value();
    fLeftCrop    = 2 * fLeftCropSlider->Value();
    fRightCrop   = 2 * fRightCropSlider->Value();
    fDeinterlace = ( fDeinterlaceCheck->Value() != 0 );

    uint8_t * preview = fManager->GetPreview( fTitle, image );
    for( uint32_t i = 0; i < fOutHeightMax + 2; i++ )
    {
        memcpy( ((uint8_t*) fBitmap->Bits()) +
                    i * fBitmap->BytesPerRow(),
                preview + 4 * ( fOutWidthMax + 2 ) * i,
                4 * ( fOutWidthMax + 2 ) );
    }
    free( preview );

    if( !Lock() )
    {
        Log( "HBPictureWin::UpdateBitmap() : cannot Lock()" );
        return;
    }

    char label[128];

    memset( label, 0, 128 );
    snprintf( label, 128, "Picture size : %d x %d",
              fOutWidth, fOutHeight );
    fWidthSlider->SetValue( fOutWidth / 16 );
    fWidthSlider->SetLabel( label );

    memset( label, 0, 128 );
    snprintf( label, 128, "Top cropping : %d", fTopCrop );
    fTopCropSlider->SetLabel( label );

    memset( label, 0, 128 );
    snprintf( label, 128, "Bottom cropping : %d", fBottomCrop );
    fBottomCropSlider->SetLabel( label );

    memset( label, 0, 128 );
    snprintf( label, 128, "Left cropping : %d", fLeftCrop );
    fLeftCropSlider->SetLabel( label );

    memset( label, 0, 128 );
    snprintf( label, 128, "Right cropping : %d", fRightCrop );
    fRightCropSlider->SetLabel( label );

    Unlock();
}