core/vil/vil_copy.cxx

Go to the documentation of this file.
00001 // This is core/vil/vil_copy.cxx
00002 
00003 //:
00004 // \file
00005 // \author Ian Scott, ISBE, Manchester
00006 // \date   21 Aug 2003
00007 
00008 #include "vil_copy.h"
00009 #include <vcl_algorithm.h>
00010 #include <vil/vil_property.h>
00011 #include <vil/vil_image_resource.h>
00012 #include <vil/vil_blocked_image_resource.h>
00013 
00014 //: Copy images in blocks of roughly this size
00015 static const unsigned long large_image_limit = 1024ul * 1024ul * 8ul; //8M Pixels
00016 
00017 //If image resource is blocked then it makes sense to use the
00018 //blocks to copy image data. src and det are known to be blocked with
00019 //equal blocking parameters
00020 static bool copy_resource_by_blocks(const vil_image_resource_sptr& src,
00021                                     vil_image_resource_sptr& det)
00022 
00023                                   
00024 {
00025   //cast to blocked image resource
00026   vil_blocked_image_resource_sptr bsrc = blocked_image_resource(src);
00027   vil_blocked_image_resource_sptr bdet = blocked_image_resource(det);
00028   for(unsigned bi = 0; bi<bsrc->n_block_i(); ++bi)
00029     for(unsigned bj = 0; bj<bsrc->n_block_j(); ++bj)
00030       {
00031         vil_image_view_base_sptr blk = bsrc->get_block(bi, bj);
00032         if(!blk) return false;
00033         if(!bdet->put_block(bi, bj, *blk)) return false;
00034       }  
00035   return true;
00036 }
00037 
00038 //: Copy src to dest.
00039 // This is useful if you want to copy on image into a window on another image.
00040 // src and dest must have identical sizes, and pixel-types. Returns false if the copy
00041 // failed.
00042 //  O(size).
00043 // \relates vil_image_resource
00044 bool vil_copy_deep(const vil_image_resource_sptr &src, vil_image_resource_sptr &dest)
00045 {
00046   if (dest->ni() != src->ni() || dest->nj() != src->nj() ||
00047       dest->nplanes() != src->nplanes() || dest->pixel_format() != src->pixel_format() )
00048     return false;
00049 
00050   if (src->ni() == 0 || src->nj() == 0 || src->nplanes() == 0) return true;
00051 
00052   //Check for a blocked resource.  Copying will be more
00053   //efficient in blocks,  unless a block is too large
00054   unsigned src_sbi=0, src_sbj=0, dest_sbi=0, dest_sbj=0;
00055 
00056   src->get_property(vil_property_size_block_i, &src_sbi);
00057   src->get_property(vil_property_size_block_j, &src_sbj);
00058   dest->get_property(vil_property_size_block_i, &dest_sbi);
00059   dest->get_property(vil_property_size_block_j, &dest_sbj);
00060   //If the source or destination is blocked then use that structure
00061   //to copy images
00062   if(src_sbi>0&&src_sbj>0&&src_sbi==dest_sbi&&src_sbj==dest_sbj)
00063     return copy_resource_by_blocks(src, dest);
00064 
00065   if (src->ni() * src->nj() * src->nplanes() < large_image_limit)
00066   {
00067     vil_image_view_base_sptr view_ref = src->get_view();
00068     if (!view_ref) return false;
00069     return dest->put_view(*view_ref);
00070   }
00071   else
00072   {
00073     unsigned got_to_line =0;
00074     unsigned block_size = vcl_max(static_cast<unsigned>(large_image_limit / src->ni()),1u);
00075 
00076     while (got_to_line < src->nj())
00077     {
00078       vil_image_view_base_sptr view_ref = src->get_view(0, src->ni(), got_to_line,
00079                                                         vcl_min(block_size, src->nj()-got_to_line));
00080       if (!view_ref) return false;
00081       if (!dest->put_view(*view_ref,0,got_to_line)) return false;
00082       got_to_line += block_size;
00083     }
00084     return true;
00085   }
00086 }
00087 

Generated on Thu Aug 28 05:08:07 2008 for core/vil by  doxygen 1.5.1