OSDN Git Service

HandBrake 0.1
[handbrake-jp/handbrake-jp-git.git] / HBPictureWin.cpp
1 /* $Id: HBPictureWin.cpp,v 1.14 2003/08/24 20:50:49 titer Exp $ */
2
3 #include "HBCommon.h"
4 #include "HBPictureWin.h"
5 #include "HBManager.h"
6
7 #include <Bitmap.h>
8 #include <Box.h>
9 #include <CheckBox.h>
10 #include <Slider.h>
11
12 #include <ffmpeg/avcodec.h>
13
14 #define UPDATE_BITMAP 'upbi'
15
16 HBPictureView::HBPictureView( BRect rect, BBitmap * bitmap )
17     : BView( rect, NULL, B_FOLLOW_ALL, B_WILL_DRAW )
18 {
19     fBitmap = bitmap;
20 }
21
22 void HBPictureView::Draw( BRect rect )
23 {
24     if( LockLooper() )
25     {
26         DrawBitmap( fBitmap, Bounds() );
27         UnlockLooper();
28     }
29     else
30     {
31         Log( "HBPictureView::Draw : LockLooper() failed" );
32     }
33     
34     BView::Draw( rect );
35 }
36
37
38 /* Constructor */
39 HBPictureWin::HBPictureWin( HBTitleInfo * titleInfo )
40     : BWindow( BRect( 50, 50, 60, 60 ), "Picture settings",
41                B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE )
42 {
43     fTitleInfo    = titleInfo;
44     
45     fMaxOutWidth  = fTitleInfo->fInWidth;
46     fMaxOutHeight = MULTIPLE_16( fTitleInfo->fInHeight * fTitleInfo->fPixelHeight /
47                                  fTitleInfo->fPixelWidth );
48     fBitmap       = new BBitmap( BRect( 0, 0, fMaxOutWidth, fMaxOutHeight ),
49                                  0, B_RGB32 );
50
51     ResizeTo( fMaxOutWidth + 40, fMaxOutHeight + 330 );
52
53     BRect r;
54
55     /* Add a background view */
56     BView * view;
57     view = new BView( Bounds(), NULL, B_FOLLOW_ALL, B_WILL_DRAW );
58     view->SetViewColor( ui_color( B_PANEL_BACKGROUND_COLOR ) );
59     AddChild( view );
60     
61     /* First box : picture + slider */
62     r = BRect( 10, 10, fMaxOutWidth + 30, fMaxOutHeight + 65 );
63     BBox * pictureBox;
64     pictureBox = new BBox( r, NULL );
65     pictureBox->SetLabel( "Preview" );
66     
67     /* Picture view */
68     r = BRect( 10, 15, fMaxOutWidth + 10, fMaxOutHeight + 15 );
69     fPictureView = new HBPictureView( r, fBitmap );
70     pictureBox->AddChild( fPictureView );
71     
72     /* Slider */
73     r = BRect( 10, fMaxOutHeight + 25, fMaxOutWidth + 10, fMaxOutHeight + 55 );
74     fPictureSlider = new BSlider( r, NULL, NULL, new BMessage( UPDATE_BITMAP ), 0, 9 );
75     pictureBox->AddChild( fPictureSlider );
76     
77     view->AddChild( pictureBox );
78     
79     /* Second box : resize & crop settings */
80     r = BRect( 10, fMaxOutHeight + 75, fMaxOutWidth + 30, fMaxOutHeight + 320 );
81     BBox * settingsBox;
82     settingsBox = new BBox( r, NULL );
83     settingsBox->SetLabel( "Settings" );
84
85     r = BRect( 10, 15, fMaxOutWidth + 10, 45 );
86     fWidthSlider = new BSlider( r, NULL, "Picture size",
87                                 new BMessage( UPDATE_BITMAP ),
88                                 1, fMaxOutWidth / 16,
89                                 B_TRIANGLE_THUMB );
90     fWidthSlider->SetValue( fMaxOutWidth / 16 );
91     settingsBox->AddChild( fWidthSlider );
92     
93     r = BRect( 10, 55, fMaxOutWidth + 10, 85 );
94     fTopCropSlider = new BSlider( r, NULL, "Top cropping",
95                                   new BMessage( UPDATE_BITMAP ),
96                                   0, fTitleInfo->fInHeight / 4,
97                                   B_TRIANGLE_THUMB );
98     settingsBox->AddChild( fTopCropSlider );
99
100     r = BRect( 10, 95, fMaxOutWidth + 10, 125 );
101     fBottomCropSlider = new BSlider( r, NULL, "Bottom cropping",
102                                      new BMessage( UPDATE_BITMAP ),
103                                      0, fTitleInfo->fInHeight / 4,
104                                      B_TRIANGLE_THUMB );
105     settingsBox->AddChild( fBottomCropSlider );
106
107     r = BRect( 10, 135, fMaxOutWidth + 10, 165 );
108     fLeftCropSlider = new BSlider( r, NULL, "Left cropping",
109                                    new BMessage( UPDATE_BITMAP ),
110                                    0, fTitleInfo->fInWidth / 4,
111                                    B_TRIANGLE_THUMB );
112     settingsBox->AddChild( fLeftCropSlider );
113
114     r = BRect( 10, 175, fMaxOutWidth + 10, 205 );
115     fRightCropSlider = new BSlider( r, NULL, "Right cropping",
116                                     new BMessage( UPDATE_BITMAP ),
117                                     0, fTitleInfo->fInWidth / 4,
118                                     B_TRIANGLE_THUMB );
119     settingsBox->AddChild( fRightCropSlider );
120     
121     r = BRect( 10, 215, fMaxOutWidth + 10, 235 );
122     fDeinterlaceCheck = new BCheckBox( r, NULL, "Deinterlace",
123                                        new BMessage( UPDATE_BITMAP ) );
124     settingsBox->AddChild( fDeinterlaceCheck );
125     
126     view->AddChild( settingsBox );
127     
128     /* Buttons */
129     
130     
131     Hide();
132     Show();
133     
134     UpdateBitmap( 0 );
135 }
136
137 bool HBPictureWin::QuitRequested()
138 {
139     if( Lock() )
140     {
141         Hide();
142         Unlock();
143     }
144     else
145     {
146         Log( "HBPictureWin::QuitRequested : cannot Lock()" );
147     }
148     return false;
149 }
150
151 void HBPictureWin::MessageReceived( BMessage * message )
152 {
153     switch( message->what )
154     {
155         case UPDATE_BITMAP:
156             UpdateBitmap( fPictureSlider->Value() );
157             fPictureView->Draw( fPictureView->Bounds() );
158             break;
159             
160         default:
161             BWindow::MessageReceived( message );
162     }
163 }
164
165 void HBPictureWin::UpdateBitmap( int which )
166 {
167 #define fInWidth     fTitleInfo->fInWidth
168 #define fInHeight    fTitleInfo->fInHeight
169 #define fPixelWidth  fTitleInfo->fPixelWidth
170 #define fPixelHeight fTitleInfo->fPixelHeight
171 #define fDeinterlace fTitleInfo->fDeinterlace
172 #define fOutWidth    fTitleInfo->fOutWidth
173 #define fOutHeight   fTitleInfo->fOutHeight
174 #define fTopCrop     fTitleInfo->fTopCrop
175 #define fBottomCrop  fTitleInfo->fBottomCrop
176 #define fLeftCrop    fTitleInfo->fLeftCrop
177 #define fRightCrop   fTitleInfo->fRightCrop
178 #define fPictures    fTitleInfo->fPictures
179
180     fTopCrop     = 2 * fTopCropSlider->Value();
181     fBottomCrop  = 2 * fBottomCropSlider->Value();
182     fLeftCrop    = 2 * fLeftCropSlider->Value();
183     fRightCrop   = 2 * fRightCropSlider->Value();
184     fDeinterlace = ( fDeinterlaceCheck->Value() != 0 );
185
186     fOutWidth    = MULTIPLE_16( 16 * fWidthSlider->Value() - fLeftCrop - fRightCrop );
187     fOutHeight   = MULTIPLE_16( ( fInHeight - fTopCrop - fBottomCrop ) *
188                                 ( 16 * fWidthSlider->Value() * fPixelHeight ) /
189                                 ( fInWidth * fPixelWidth ) );
190     
191     AVPicture pic1; /* original YUV picture */
192     avpicture_fill( &pic1, fPictures[which],
193                     PIX_FMT_YUV420P, fInWidth, fInHeight );
194
195     AVPicture pic2; /* deinterlaced YUV picture */
196     uint8_t * buf2 = (uint8_t*) malloc( 3 * fInWidth * fInHeight / 2 );
197     avpicture_fill( &pic2, buf2, PIX_FMT_YUV420P, fInWidth, fInHeight );
198
199     AVPicture pic3; /* resized YUV picture */
200     uint8_t * buf3 = (uint8_t*) malloc( 3 * fOutWidth * fOutHeight / 2 );
201     avpicture_fill( &pic3, buf3, PIX_FMT_YUV420P, fOutWidth, fOutHeight );
202
203     AVPicture pic4; /* resized RGB picture */
204     uint8_t * buf4 = (uint8_t*) malloc( 4 * fOutWidth * fOutHeight );
205     avpicture_fill( &pic4, buf4, PIX_FMT_RGBA32, fOutWidth, fOutHeight );
206
207     ImgReSampleContext * resampleContext;
208     resampleContext = img_resample_full_init( fOutWidth, fOutHeight,
209                                               fInWidth, fInHeight,
210                                               fTopCrop, fBottomCrop,
211                                               fLeftCrop, fRightCrop );
212     
213     if( fDeinterlace )
214     {
215         avpicture_deinterlace( &pic2, &pic1, PIX_FMT_YUV420P, fInWidth, fInHeight );
216         img_resample( resampleContext, &pic3, &pic2 );
217     }
218     else
219     {
220         img_resample( resampleContext, &pic3, &pic1 );
221     }
222     img_convert( &pic4, PIX_FMT_RGBA32, &pic3, PIX_FMT_YUV420P, fOutWidth, fOutHeight );
223
224     /* Blank the bitmap */
225     for( uint32_t i = 0; i < fMaxOutHeight; i++ )
226     {
227         memset( (uint8_t*) fBitmap->Bits() + i * fBitmap->BytesPerRow(),
228                 0, fMaxOutWidth * 4 );
229     }
230         
231     /* Draw the picture (centered) */
232     uint32_t leftOffset = ( fMaxOutWidth - fOutWidth ) / 2;
233     uint32_t topOffset  = ( fMaxOutHeight - fOutHeight ) / 2;
234     for( uint32_t i = 0; i < fOutHeight; i++ )
235     {
236         memcpy( (uint8_t*) fBitmap->Bits() +
237                     ( i + topOffset ) * fBitmap->BytesPerRow() +
238                     leftOffset * 4,
239                 buf4 + i * fOutWidth * 4,
240                 fOutWidth * 4 );
241     }
242
243     /* Draw the cropping zone */
244     memset( (uint8_t*) fBitmap->Bits() +
245                 topOffset * fBitmap->BytesPerRow() +
246                 leftOffset * 4,
247             0xFF,
248             fOutWidth * 4 );
249     
250     for( uint32_t i = 0; i < fOutHeight; i++ )
251     {
252         memset( (uint8_t*) fBitmap->Bits() +
253                     ( i + topOffset ) * fBitmap->BytesPerRow() +
254                     leftOffset * 4,
255                 0xFF,
256                 4 );
257         memset( (uint8_t*) fBitmap->Bits() +
258                    ( i + topOffset ) * fBitmap->BytesPerRow() +
259                    ( leftOffset + fOutWidth - 1 ) * 4,
260                 0xFF,
261                 4 );
262     }
263
264     memset( (uint8_t*) fBitmap->Bits() +
265                 ( topOffset + fOutHeight - 1 ) * fBitmap->BytesPerRow() +
266                 leftOffset * 4,
267             0xFF,
268             fOutWidth * 4 );
269
270     /* Clean up */
271     free( buf2 );
272     free( buf3 );
273     free( buf4 );
274     
275     /* Show the output size */
276     if( !Lock() )
277     {
278         Log( "HBPictureWin::UpdateBitmap() : cannot Lock()" );
279         return;
280     }
281     
282     char label[128]; memset( label, 0, 128 );
283     snprintf( label, 128, "Picture size : %d x %d",
284               fOutWidth, fOutHeight );
285     fWidthSlider->SetLabel( label );
286     
287     Unlock();
288 }