1 /* $Id: HBPictureWin.cpp,v 1.14 2003/08/24 20:50:49 titer Exp $ */
4 #include "HBPictureWin.h"
12 #include <ffmpeg/avcodec.h>
14 #define UPDATE_BITMAP 'upbi'
16 HBPictureView::HBPictureView( BRect rect, BBitmap * bitmap )
17 : BView( rect, NULL, B_FOLLOW_ALL, B_WILL_DRAW )
22 void HBPictureView::Draw( BRect rect )
26 DrawBitmap( fBitmap, Bounds() );
31 Log( "HBPictureView::Draw : LockLooper() failed" );
39 HBPictureWin::HBPictureWin( HBTitleInfo * titleInfo )
40 : BWindow( BRect( 50, 50, 60, 60 ), "Picture settings",
41 B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE )
43 fTitleInfo = titleInfo;
45 fMaxOutWidth = fTitleInfo->fInWidth;
46 fMaxOutHeight = MULTIPLE_16( fTitleInfo->fInHeight * fTitleInfo->fPixelHeight /
47 fTitleInfo->fPixelWidth );
48 fBitmap = new BBitmap( BRect( 0, 0, fMaxOutWidth, fMaxOutHeight ),
51 ResizeTo( fMaxOutWidth + 40, fMaxOutHeight + 330 );
55 /* Add a background view */
57 view = new BView( Bounds(), NULL, B_FOLLOW_ALL, B_WILL_DRAW );
58 view->SetViewColor( ui_color( B_PANEL_BACKGROUND_COLOR ) );
61 /* First box : picture + slider */
62 r = BRect( 10, 10, fMaxOutWidth + 30, fMaxOutHeight + 65 );
64 pictureBox = new BBox( r, NULL );
65 pictureBox->SetLabel( "Preview" );
68 r = BRect( 10, 15, fMaxOutWidth + 10, fMaxOutHeight + 15 );
69 fPictureView = new HBPictureView( r, fBitmap );
70 pictureBox->AddChild( fPictureView );
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 );
77 view->AddChild( pictureBox );
79 /* Second box : resize & crop settings */
80 r = BRect( 10, fMaxOutHeight + 75, fMaxOutWidth + 30, fMaxOutHeight + 320 );
82 settingsBox = new BBox( r, NULL );
83 settingsBox->SetLabel( "Settings" );
85 r = BRect( 10, 15, fMaxOutWidth + 10, 45 );
86 fWidthSlider = new BSlider( r, NULL, "Picture size",
87 new BMessage( UPDATE_BITMAP ),
90 fWidthSlider->SetValue( fMaxOutWidth / 16 );
91 settingsBox->AddChild( fWidthSlider );
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,
98 settingsBox->AddChild( fTopCropSlider );
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,
105 settingsBox->AddChild( fBottomCropSlider );
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,
112 settingsBox->AddChild( fLeftCropSlider );
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,
119 settingsBox->AddChild( fRightCropSlider );
121 r = BRect( 10, 215, fMaxOutWidth + 10, 235 );
122 fDeinterlaceCheck = new BCheckBox( r, NULL, "Deinterlace",
123 new BMessage( UPDATE_BITMAP ) );
124 settingsBox->AddChild( fDeinterlaceCheck );
126 view->AddChild( settingsBox );
137 bool HBPictureWin::QuitRequested()
146 Log( "HBPictureWin::QuitRequested : cannot Lock()" );
151 void HBPictureWin::MessageReceived( BMessage * message )
153 switch( message->what )
156 UpdateBitmap( fPictureSlider->Value() );
157 fPictureView->Draw( fPictureView->Bounds() );
161 BWindow::MessageReceived( message );
165 void HBPictureWin::UpdateBitmap( int which )
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
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 );
186 fOutWidth = MULTIPLE_16( 16 * fWidthSlider->Value() - fLeftCrop - fRightCrop );
187 fOutHeight = MULTIPLE_16( ( fInHeight - fTopCrop - fBottomCrop ) *
188 ( 16 * fWidthSlider->Value() * fPixelHeight ) /
189 ( fInWidth * fPixelWidth ) );
191 AVPicture pic1; /* original YUV picture */
192 avpicture_fill( &pic1, fPictures[which],
193 PIX_FMT_YUV420P, fInWidth, fInHeight );
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 );
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 );
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 );
207 ImgReSampleContext * resampleContext;
208 resampleContext = img_resample_full_init( fOutWidth, fOutHeight,
210 fTopCrop, fBottomCrop,
211 fLeftCrop, fRightCrop );
215 avpicture_deinterlace( &pic2, &pic1, PIX_FMT_YUV420P, fInWidth, fInHeight );
216 img_resample( resampleContext, &pic3, &pic2 );
220 img_resample( resampleContext, &pic3, &pic1 );
222 img_convert( &pic4, PIX_FMT_RGBA32, &pic3, PIX_FMT_YUV420P, fOutWidth, fOutHeight );
224 /* Blank the bitmap */
225 for( uint32_t i = 0; i < fMaxOutHeight; i++ )
227 memset( (uint8_t*) fBitmap->Bits() + i * fBitmap->BytesPerRow(),
228 0, fMaxOutWidth * 4 );
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++ )
236 memcpy( (uint8_t*) fBitmap->Bits() +
237 ( i + topOffset ) * fBitmap->BytesPerRow() +
239 buf4 + i * fOutWidth * 4,
243 /* Draw the cropping zone */
244 memset( (uint8_t*) fBitmap->Bits() +
245 topOffset * fBitmap->BytesPerRow() +
250 for( uint32_t i = 0; i < fOutHeight; i++ )
252 memset( (uint8_t*) fBitmap->Bits() +
253 ( i + topOffset ) * fBitmap->BytesPerRow() +
257 memset( (uint8_t*) fBitmap->Bits() +
258 ( i + topOffset ) * fBitmap->BytesPerRow() +
259 ( leftOffset + fOutWidth - 1 ) * 4,
264 memset( (uint8_t*) fBitmap->Bits() +
265 ( topOffset + fOutHeight - 1 ) * fBitmap->BytesPerRow() +
275 /* Show the output size */
278 Log( "HBPictureWin::UpdateBitmap() : cannot Lock()" );
282 char label[128]; memset( label, 0, 128 );
283 snprintf( label, 128, "Picture size : %d x %d",
284 fOutWidth, fOutHeight );
285 fWidthSlider->SetLabel( label );