home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / letterbox.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  4.9 KB  |  235 lines

  1. /* 
  2.  *  Crop.cpp 
  3.  *                                     
  4.  *
  5.  *    Copyright (C) Alberto Vigata - January 2000
  6.  *
  7.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  8.  *    
  9.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  10.  *  it under the terms of the GNU General Public License as published by
  11.  *  the Free Software Foundation; either version 2, or (at your option)
  12.  *  any later version.
  13.  *   
  14.  *  FlasKMPEG is distributed in the hope that it will be useful,
  15.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *  GNU General Public License for more details.
  18.  *   
  19.  *  You should have received a copy of the GNU General Public License
  20.  *  along with GNU Make; see the file COPYING.  If not, write to
  21.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  22.  *
  23.  */
  24.  
  25. #include "letterbox.h"
  26. #include "Auxiliary.h"
  27. #include "Debug.h"
  28.  
  29.  
  30. void LetterboxPlanar( Pixel8 *dst, int width, int height, TLetterboxConfig *config, Pixel8 color)
  31. {
  32.   if(!dst)
  33.     return;
  34.   
  35.   Pixel8  *buf;
  36.   Pixel32 *buf4;
  37.   Pixel32 color32 = color<<24 | color<<16 | color<<8 | color;
  38.  
  39.   int ww, wr;
  40.  
  41.   if(config->top)
  42.   {
  43.     memset( dst, color, config->top * width );
  44.   }
  45.  
  46.   if( config->bottom )
  47.   {
  48.     memset( dst+width *(height - config->bottom), color, config->bottom*width);
  49.   }
  50.   if(config->left)
  51.   {
  52.     int h = height; 
  53.     int mod = width - config->left;
  54.  
  55.     buf4 = (Pixel32 *)dst;
  56.     while(h--)
  57.     {
  58.       ww = config->left/4; // 4 byte chunks
  59.       wr = config->left%4; // remainder
  60.       // 4 byte
  61.       while(ww--)
  62.         *buf4++ = color32;
  63.  
  64.       // single byte
  65.       buf = (Pixel8 *)buf4;
  66.       while(wr--)
  67.         *buf++ = color;
  68.       buf4 = (Pixel32 *)(buf + mod);
  69.     }
  70.   }
  71.   if(config->right)
  72.   {
  73.     int h = height; 
  74.     int mod = width - config->right;
  75.     
  76.     buf = dst + mod;
  77.     while(h--)
  78.     {
  79.       ww = config->right/4; // 4 byte chunks
  80.       wr = config->right%4; // remainder
  81.  
  82.       // single byte
  83.       while(wr--)
  84.         *buf++ = color;
  85.  
  86.       buf4 = (Pixel32 *)buf;
  87.       // 4 byte
  88.       while(ww--)
  89.         *buf4++ = color32;
  90.  
  91.       buf = (Pixel8 *)(buf4) + mod;
  92.     }
  93.   }
  94. }
  95.  
  96.  
  97. int LetterboxYV12(CFrame *fr, TLetterboxConfig *config) 
  98. {
  99.   TLetterboxConfig sUVConfig;
  100.  
  101.   int sw = fr->GetWidth();
  102.   int sh = fr->GetHeight();
  103.  
  104.   int shw = sw>>1;
  105.   int shh = sh>>1;
  106.  
  107.   int sysize = sw*sh;
  108.   int scrsize = shw * shh;
  109.  
  110.   sUVConfig.bottom = config->bottom >> 1;
  111.   sUVConfig.top    = config->top    >> 1;
  112.   sUVConfig.left   = config->left   >> 1;
  113.   sUVConfig.right  = config->right  >> 1;
  114.  
  115.   Pixel8 *pSrc = (Pixel8 *)fr->GetBuffer();
  116.  
  117.  
  118.   LetterboxPlanar( pSrc, sw, sh,  config, 0);
  119.   // V
  120.   LetterboxPlanar( pSrc + sysize, 
  121.                    shw, shh,  &sUVConfig, 128 );
  122.   // U
  123.   LetterboxPlanar( pSrc + sysize + scrsize,
  124.                    shw, shh,  &sUVConfig, 128 );
  125.  
  126.  
  127.   return 0;
  128. }
  129.  
  130. int Letterbox(CFrame *source, TLetterboxConfig *config) 
  131. {
  132.   if(!source )
  133.     return 0;
  134.  
  135.   switch( source->GetFormat() )
  136.   {
  137.   case FRAME_YV12:
  138.     return LetterboxYV12( source, config);
  139.     break;
  140.   default:
  141.     break;
  142.   }
  143.   return 1;
  144. }
  145.  
  146.  
  147. int StopCrop(TLetterboxConfig *config) {
  148.  
  149.     return 1;
  150. }
  151.  
  152. FlLetterbox::FlLetterbox()
  153. {
  154.   memset( &m_cfg, 0, sizeof m_cfg );
  155.   m_bConfigured = false;
  156. }
  157.  
  158. int FlLetterbox::Configure( void *conf, int confsize)
  159. {
  160.   
  161.   FLASSERT( confsize==sizeof(TLetterboxConfig) )
  162.   TLetterboxConfig *cf = (TLetterboxConfig *)conf;
  163.   
  164.   m_cfg = *cf;
  165.   m_bConfigured = true;
  166.   
  167.   return flfil_ok;
  168. }
  169.  
  170. int FlLetterbox::GetFilterConf( flfilter_conf *fc )
  171. {
  172.   if(!m_bConfigured)
  173.   {
  174.     DBG_STR((str, "FlLetterbox::GetFilterConf - You need to configure first\n"))
  175.       return 0;
  176.   }
  177.   
  178.   if(!fc)
  179.     return 0;
  180.   
  181.   *fc = m_fc;
  182.   
  183.   return 1;
  184. }
  185.  
  186. int FlLetterbox::ValFilterConf( flfilter_conf *fc )
  187. {
  188.   if(!m_bConfigured)
  189.   {
  190.     DBG_STR((str, "FlLetterbox::ValFilterConf - You need to configure first\n"))
  191.       return flfil_error;
  192.   }
  193.   
  194.   if( m_cfg.left  > fc->iw/2  ||
  195.       m_cfg.right > fc->iw/2  ||
  196.       m_cfg.bottom > fc->ih/2 ||
  197.       m_cfg.top    > fc->ih/2 ||
  198.       m_cfg.left  < 0         ||
  199.       m_cfg.right < 0         ||
  200.       m_cfg.bottom< 0         ||
  201.       m_cfg.top   < 0 ) {
  202.     DBG_STR((str, "FlLetterbox::ValFilterConf - Bad Configuration\n"))
  203.     return flfil_error;
  204.   }
  205.   fc->olag = 0;
  206.   fc->op = 1;
  207.  
  208.   fc->od = fc->id;
  209.   fc->ow = fc->iw;
  210.   fc->oh = fc->ih;
  211.   fc->oprovided = 0;
  212.  
  213.   fc->ocanmodify = 1;
  214.   
  215.   m_fc = *fc;
  216.   
  217.   return flfil_ok;
  218. }
  219.  
  220. int FlLetterbox::StartSimple()
  221. {
  222.   return m_bConfigured == true ? 1 : 0;
  223. }
  224.  
  225. int FlLetterbox::ProcessSimple( CFrame *in,  CFrame *out )
  226. {
  227.   FLASSERT( out==NULL )
  228.   return Letterbox( in, &m_cfg );
  229. }
  230.  
  231. int FlLetterbox::StopSimple()
  232. {
  233.   return 1;
  234. }
  235.