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