home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / Crop.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  6.1 KB  |  269 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 "Crop.h"
  26. #include "Auxiliary.h"
  27. #include "Debug.h"
  28.  
  29.  
  30. int CropRGB32(CFrame *source, CFrame *dest, TCropConfig *config) {
  31.  
  32.   if(!source || !dest)
  33.     return 0;
  34.   
  35.  
  36.     ui8*        src = source->GetBuffer();
  37.     ui8*        dst = dest->GetBuffer();
  38.   ui8 *src_start;
  39.   ui32 src_w = source->GetWidth(), src_h = source->GetHeight();
  40.   ui32 dst_w = dest->GetWidth(), dst_h = dest->GetHeight();
  41.   ui32 nSrcPitch = source->GetPitch();
  42.   ui32 nDstPitch = dest->GetPitch();  
  43.   
  44.   ui32 crop_w = config->right - config->left;
  45.   ui32 crop_h = config->bottom - config->top;
  46.  
  47.  
  48.   // If the crop width is the same as the output
  49.   // height there, just copy the raw data.
  50.   if( crop_w == src_w )
  51.   {
  52.     src_start = src + config->top * nSrcPitch;
  53.     flmemcpy( dst, src_start, crop_h * nSrcPitch );
  54.   }
  55.   else // copy stride by stride
  56.   {
  57.     // We are dealing with bottom-up dibs
  58.     // so last row in memory is the first scanline
  59.     src_start = src + ( src_h - 1 - config->top ) * nSrcPitch
  60.       + config->left * sizeof(Pixel32) ;
  61.  
  62.     // Advance the destination to the last row
  63.     dst += (dst_h - 1) * nDstPitch;
  64.     
  65.     int h = crop_h; // number of strides to copy
  66.     do
  67.     {
  68.       flmemcpy( dst, src_start, crop_w * sizeof(Pixel32) );
  69.       src_start-= nSrcPitch;
  70.       dst -= nDstPitch;
  71.     }while(--h);
  72.   }
  73.  
  74.  
  75.     return 0;
  76. }
  77.  
  78.  
  79. void CropPlanar( Pixel8 *src, Pixel8 *dst, int src_w, int src_h, int dst_w, int dst_h,
  80.                 TCropConfig *config)
  81. {
  82.   if(!src || !dst)
  83.     return;
  84.   
  85.   ui32 crop_w = config->right - config->left;
  86.   ui32 crop_h = config->bottom - config->top;
  87.   
  88.   Pixel8 *src_start = src + config->top/2*2 * src_w + config->left/2*2;
  89.   
  90.   // If the crop width is the same as the output
  91.   // height there, just copy the raw data.
  92.   if( crop_w == src_w )
  93.   {
  94.     flmemcpy( dst, src_start, crop_h * src_w );
  95.   }
  96.   else // copy stride by stride
  97.   {
  98.     int h = crop_h; // number of strides to copy
  99.     do
  100.     {
  101.       flmemcpy( dst, src_start, crop_w  );
  102.       src_start+= src_w;
  103.       dst += dst_w;
  104.     }while(--h);
  105.   }
  106. }
  107.  
  108. int CropYV12(CFrame *source, CFrame *dest, TCropConfig *config) 
  109. {
  110.   TCropConfig sUVConfig;
  111.  
  112.   int sw = source->GetWidth();
  113.   int sh = source->GetHeight();
  114.  
  115.   int shw = sw>>1;
  116.   int shh = sh>>1;
  117.  
  118.   int dw = config->right - config->left;
  119.   int dh = config->bottom - config->top;
  120.  
  121.   int dhw = dw >> 1;
  122.   int dhh = dh >> 1;
  123.  
  124.   int sysize = sw*sh;
  125.   int scrsize = shw * shh;
  126.  
  127.   int dysize = dw*dh;
  128.   int dcrsize = dhw * dhh;
  129.  
  130.   sUVConfig.bottom = config->bottom >> 1;
  131.   sUVConfig.top    = config->top    >> 1;
  132.   sUVConfig.left   = config->left   >> 1;
  133.   sUVConfig.right  = config->right  >> 1;
  134.  
  135.   Pixel8 *pSrc = (Pixel8 *)source->GetBuffer();
  136.   Pixel8 *pDst = (Pixel8 *)dest->GetBuffer();
  137.  
  138.   CropPlanar( pSrc, pDst, sw, sh, dw, dh, config);
  139.   // V
  140.   CropPlanar( pSrc + sysize, 
  141.              pDst + dysize, shw, shh,  dhw, dhh, &sUVConfig);
  142.   // U
  143.   CropPlanar( pSrc + sysize + scrsize,
  144.               pDst + dysize + dcrsize, 
  145.               shw, shh, dhw , dhh, &sUVConfig);
  146.  
  147.  
  148.   return 0;
  149. }
  150.  
  151. int Crop(CFrame *source, CFrame *dest, TCropConfig *config) 
  152. {
  153.   if(!source || !dest)
  154.     return 0;
  155.  
  156.   int crop_w = config->right - config->left;
  157.   int crop_h = config->bottom - config->top;
  158.  
  159.   if( crop_w != dest->GetWidth()  ||
  160.       crop_h != dest->GetHeight() )
  161.   {
  162.     DBG_STR((str, "Crop - Invalid Parameters\n"))
  163.     return 0;
  164.   }
  165.  
  166.   switch( source->GetFormat() )
  167.   {
  168.   case FRAME_RGB32:
  169.     return CropRGB32(source, dest, config);
  170.     break;
  171.   case FRAME_YV12:
  172.     return CropYV12( source, dest, config);
  173.     break;
  174.   default:
  175.     dest->SetFrame( source );
  176.     break;
  177.   }
  178.   return 1;
  179. }
  180.  
  181.  
  182. FlCrop::FlCrop()
  183. {
  184.   memset( &m_cfg, 0, sizeof m_cfg );
  185.   m_bConfigured = false;
  186. }
  187.  
  188.  
  189. int FlCrop::StartSimple()
  190. {
  191.   return m_bConfigured == true ? 1 : 0;
  192. }
  193.  
  194. int FlCrop::Configure( void *conf, int confsize)
  195. {
  196.   int res;
  197.  
  198.   FLASSERT( confsize==sizeof(TCropConfig) )
  199.   TCropConfig cfg = *(TCropConfig *)conf;
  200.  
  201.  
  202.   // Validate configuration
  203.   if( (cfg.right-cfg.left)%16==0  &&
  204.       (cfg.bottom-cfg.top)%16==0  && 
  205.       (cfg.right-cfg.left)>0      &&
  206.       (cfg.bottom-cfg.top)>0 ) 
  207.   {
  208.     m_cfg = cfg;
  209.     m_bConfigured = true;
  210.     res = flfil_ok;
  211.   }
  212.   else {
  213.     m_bConfigured = false;
  214.     res = flfil_error;
  215.   }
  216.  
  217.   return res;
  218. }
  219.  
  220. int FlCrop::GetFilterConf( flfilter_conf *fc )
  221. {
  222.   if(!m_bConfigured)
  223.   {
  224.     DBG_STR((str, "FlCrop::GetConf - You need to configure first\n"))
  225.     return 0;
  226.   }
  227.  
  228.   if(!fc)
  229.     return 0;
  230.  
  231.   *fc = m_fc;
  232.  
  233.   return 1;
  234. }
  235.  
  236. int FlCrop::ValFilterConf( flfilter_conf *fc )
  237. {
  238.   if(!m_bConfigured)
  239.   {
  240.     DBG_STR((str, "FlCrop::Query - You need to configure first\n"))
  241.     return flfil_error;
  242.   }
  243.   
  244.   if( m_cfg.left < 0 || m_cfg.right>fc->iw   ||
  245.       m_cfg.top < 0  || m_cfg.bottom>fc->ih ) {
  246.     return flfil_error;
  247.   }
  248.   
  249.   fc->od = fc->id;
  250.   fc->ow = m_cfg.right - m_cfg.left;
  251.   fc->oh = m_cfg.bottom - m_cfg.top;
  252.   fc->oprovided = 0;
  253.   fc->op = 0;
  254.   fc->ocanmodify = 1;
  255.  
  256.   m_fc = *fc;
  257.  
  258.   return 1;
  259. }
  260.  
  261. int FlCrop::ProcessSimple( CFrame *in,  CFrame *out )
  262. {
  263.   return Crop( in, out, &m_cfg );
  264. }
  265.  
  266. int FlCrop::StopSimple()
  267. {
  268.   return 1;
  269. }