vil_copy.cxx
Go to the documentation of this file.
1 // This is core/vil/vil_copy.cxx
2 #include <algorithm>
3 #include "vil_copy.h"
4 //:
5 // \file
6 // \author Ian Scott, ISBE, Manchester
7 // \date 21 Aug 2003
8 
9 #ifdef _MSC_VER
10 # include <vcl_msvc_warnings.h>
11 #endif
12 #include <vil/vil_property.h>
13 #include <vil/vil_image_resource.h>
15 #include <vil/vil_new.h>
16 
17 //: Copy images in blocks of roughly this size
18 static const unsigned long large_image_limit_ = 1024ul * 1024ul * 8ul; //8M Pixels
19 
20 //If image resource is blocked then it makes sense to use the
21 //blocks to copy image data. src and det are known to be blocked with
22 //equal blocking parameters
23 static bool copy_resource_by_blocks(const vil_image_resource_sptr& src,
25 {
26  //cast to blocked image resource
29  for (unsigned bi = 0; bi<bsrc->n_block_i(); ++bi)
30  for (unsigned bj = 0; bj<bsrc->n_block_j(); ++bj)
31  {
32  vil_image_view_base_sptr blk = bsrc->get_block(bi, bj);
33  if (!blk) return false;
34  if (!bdet->put_block(bi, bj, *blk)) return false;
35  }
36  return true;
37 }
38 
39 //: Copy src to dest.
40 // This is useful if you want to copy on image into a window on another image.
41 // src and dest must have identical sizes, and pixel-types. Returns false if the copy
42 // failed.
43 // O(size).
44 // \relatesalso vil_image_resource
46 {
47  if (dest->ni() != src->ni() || dest->nj() != src->nj() ||
48  dest->nplanes() != src->nplanes() || dest->pixel_format() != src->pixel_format() )
49  return false;
50 
51  if (src->ni() == 0 || src->nj() == 0 || src->nplanes() == 0) return true;
52 
53  //Check for a blocked resource. Copying will be more
54  //efficient in blocks, unless a block is too large
55  unsigned src_sbi=0, src_sbj=0, dest_sbi=0, dest_sbj=0;
56 
57  src->get_property(vil_property_size_block_i, &src_sbi);
58  src->get_property(vil_property_size_block_j, &src_sbj);
59  dest->get_property(vil_property_size_block_i, &dest_sbi);
60  dest->get_property(vil_property_size_block_j, &dest_sbj);
61  //If the source or destination is blocked then use that structure
62  //to copy images
63  if (src_sbi>0&&src_sbj>0&&src_sbi==dest_sbi&&src_sbj==dest_sbj)
64  return copy_resource_by_blocks(src, dest);
65 
66  if (src->ni() * src->nj() * src->nplanes() < large_image_limit_)
67  {
68  vil_image_view_base_sptr view_ref = src->get_view();
69  if (!view_ref) return false;
70  return dest->put_view(*view_ref);
71  }
72  else
73  {
74  unsigned got_to_line =0;
75  unsigned block_size = std::max(static_cast<unsigned>(large_image_limit_ / src->ni()),1u);
76 
77  while (got_to_line < src->nj())
78  {
79  vil_image_view_base_sptr view_ref = src->get_view(0, src->ni(), got_to_line,
80  std::min(block_size, src->nj()-got_to_line));
81  if (!view_ref) return false;
82  if (!dest->put_view(*view_ref,0,got_to_line)) return false;
83  got_to_line += block_size;
84  }
85  return true;
86  }
87 }
88 
90 {
91  if(src == nullptr) return nullptr;
92  vil_image_resource_sptr result = vil_new_image_resource(src->ni(), src->nj(), src);
93  bool copy_r = vil_copy_deep(src, result);
94  if(!copy_r)
95  {
96  return nullptr;
97  }
98  return result;
99 }
vil_image_resource_sptr vil_new_image_resource(unsigned ni, unsigned nj, unsigned nplanes, vil_pixel_format format)
Make a new image of given format.
Definition: vil_new.cxx:32
A blocked representation of the image_resource.
vil_blocked_image_resource_sptr blocked_image_resource(const vil_image_resource_sptr &ir)
cast to blocked resource if possible.
#define vil_property_size_block_i
For unblocked images, the following properties are not implemented.
Definition: vil_property.h:76
Various image copying functions.
There is no class or function called vil_property.
Make a new image.
#define vil_property_size_block_j
Block size in rows.
Definition: vil_property.h:79
Representation of a generic image source or destination.
bool vil_copy_deep(const vil_image_resource_sptr &src, vil_image_resource_sptr &dest)
Copy src to dest.
Definition: vil_copy.cxx:45