core/vil1/vil1_memory_image_impl.cxx

Go to the documentation of this file.
00001 // This is core/vil1/vil1_memory_image_impl.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 
00008 #include "vil1_memory_image_impl.h"
00009 
00010 #include <vcl_cassert.h>
00011 #include <vcl_cstring.h>   // strcmp() and memcpy()
00012 #include <vcl_iostream.h>
00013 
00014 #include <vil1/vil1_image.h>
00015 #include <vil1/vil1_property.h>
00016 
00017 vil1_memory_image_impl::vil1_memory_image_impl(int planes, int w, int h,
00018                                                vil1_memory_image_format
00019                                                const& format)
00020 {
00021   init((void*)0, planes, w, h, format.components, format.bits_per_component,
00022        format.component_format);
00023 }
00024 
00025 vil1_memory_image_impl::vil1_memory_image_impl(int planes, int w, int h,
00026                                                int components,
00027                                                int bits_per_component,
00028                                                vil1_component_format component_format)
00029 {
00030   init((void*)0, planes, w, h, components, bits_per_component, component_format);
00031 }
00032 
00033 vil1_memory_image_impl::vil1_memory_image_impl(int planes, int w, int h,
00034                                                vil1_pixel_format_t pixel_format)
00035 {
00036   init((void*)0, planes, w, h, pixel_format);
00037 }
00038 
00039 vil1_memory_image_impl::vil1_memory_image_impl(int w, int h, int components,
00040                                                int bits_per_component,
00041                                                vil1_component_format component_format)
00042 {
00043   init((void*)0, 1, w, h, components, bits_per_component, component_format);
00044 }
00045 
00046 vil1_memory_image_impl::vil1_memory_image_impl(int w, int h,
00047                                                vil1_pixel_format_t pixel_format)
00048 {
00049   init((void*)0, 1, w, h, pixel_format);
00050 }
00051 
00052 vil1_memory_image_impl::vil1_memory_image_impl(vil1_memory_image_impl const& i)
00053   : vil1_image_impl()
00054 {
00055   init((void*)0, i.planes_, i.width_, i.height_, i.components_,
00056        i.bits_per_component_, i.component_format_);
00057 }
00058 
00059 void vil1_memory_image_impl::init(void *buf,
00060                                   int planes, int w, int h,
00061                                   int components, int bits_per_component,
00062                                   vil1_component_format component_format)
00063 {
00064   is_foreign_buf_ = (buf != 0); // true if buf is non-nil
00065   planes_ = planes;
00066   width_ = w;
00067   height_ = h;
00068   components_ = components;
00069   bits_per_component_ = bits_per_component;
00070   component_format_ = component_format;
00071 
00072   bytes_per_pixel_ = (bits_per_component_ * components_ + 7) / 8;
00073   int bytes_per_row = (width_ * bits_per_component_ * components_ + 7) / 8;
00074 
00075   int size = planes_ * height_ * bytes_per_row;
00076   if (size) {
00077     // non-empty image
00078     if (is_foreign_buf_)
00079       buf_ = (unsigned char *)buf;
00080     else
00081       buf_ = new unsigned char[size];
00082     rows_ = new void**[planes_];
00083 
00084     unsigned char* ptr = buf_;
00085     for (int p = 0; p < planes_; ++p) {
00086       rows_[p] = new void*[height_];
00087       for (int y = 0; y < height_; ++y) {
00088         rows_[p][y] = ptr;
00089         ptr += bytes_per_row;
00090       }
00091     }
00092   }
00093   else {
00094     // empty image
00095     buf_ = 0;
00096     rows_ = 0;
00097   }
00098 }
00099 
00100 void vil1_memory_image_impl::init(void *buf, int planes, int w, int h,
00101                                   vil1_pixel_format_t pixel_format)
00102 {
00103   switch (pixel_format) {
00104   case VIL1_BYTE:     init(buf, planes, w, h, 1,  8,
00105                            VIL1_COMPONENT_FORMAT_UNSIGNED_INT); break;
00106   case VIL1_RGB_BYTE: init(buf, planes, w, h, 3,  8,
00107                            VIL1_COMPONENT_FORMAT_UNSIGNED_INT); break;
00108   case VIL1_FLOAT:    init(buf, planes, w, h, 1, 32,
00109                            VIL1_COMPONENT_FORMAT_UNSIGNED_INT); break;
00110   default:
00111     vcl_cerr << "vil1_memory_image_impl: crazy format!\n";
00112   }
00113 }
00114 
00115 vil1_memory_image_impl::~vil1_memory_image_impl()
00116 {
00117   if (rows_) {
00118     for (int p = 0; p < planes_; ++p) delete[] rows_[p];
00119     delete [] rows_;
00120   }
00121   if (!is_foreign_buf_)
00122     delete [] buf_;
00123 }
00124 
00125 
00126 void vil1_memory_image_impl::resize(int planes, int width, int height,
00127                                     int components,
00128                                     int bits_per_component,
00129                                     vil1_component_format format)
00130 {
00131   components_ = components;
00132   bits_per_component_ = bits_per_component;
00133   component_format_ = format;
00134   resize(planes, width, height);
00135 }
00136 
00137 
00138 void vil1_memory_image_impl::resize(int planes, int width, int height)
00139 {
00140   // Resize is expensive, so only do it if the size changes.
00141   if (planes == planes_ && width == width_ && height == height_)
00142     return;
00143 
00144   if (rows_) {
00145     for (int p = 0; p < planes_; ++p) delete[] rows_[p];
00146     delete [] rows_;
00147   }
00148   if (!is_foreign_buf_)
00149     delete [] buf_;
00150   init((void*)0, planes, width, height, components_,
00151        bits_per_component_, component_format_);
00152 }
00153 
00154 bool vil1_memory_image_impl::get_section(void* obuf, int x0, int y0,
00155                                          int xs, int ys) const
00156 {
00157   int bytes_per_row = (width_ * bits_per_component_ * components_ + 7) / 8;
00158   for (int p=0; p<planes_; ++p) {
00159     int offset = y0 * bytes_per_row + x0 * bytes_per_pixel_;
00160     if (width_ != xs) {
00161       int byte_out_width = bytes_per_pixel_ * xs;
00162       int byte_width = bytes_per_pixel_ * width_;
00163 
00164       for (int y = 0; y < ys; ++y) {
00165         vcl_memcpy((unsigned char*)obuf + y * byte_out_width, buf_ + offset + y * byte_width, byte_out_width);
00166       }
00167     } else {
00168       vcl_memcpy((unsigned char*)obuf, buf_ + offset, bytes_per_row * ys);
00169     }
00170     obuf = (void*)((char*)obuf + bytes_per_row*height_);
00171   }
00172 
00173   return true;
00174 }
00175 
00176 bool vil1_memory_image_impl::put_section(void const* ibuf, int x0,
00177                                          int y0, int xs, int ys)
00178 {
00179   int bytes_per_row = (width_ * bits_per_component_ * components_ + 7) / 8;
00180   for (int p=0; p<planes_; ++p) {
00181     int offset = y0 * bytes_per_row + x0 * bytes_per_pixel_;
00182     if (width_ != xs) {
00183       int byte_in_width = bytes_per_pixel_ * xs;
00184       int byte_width = bytes_per_pixel_ * width_;
00185 
00186       for (int y = 0; y < ys; ++y) {
00187         vcl_memcpy(buf_ + offset + y * byte_width,
00188                    (unsigned char const*)ibuf + y*byte_in_width, byte_in_width);
00189       }
00190       ibuf = (void const*)((char const*)ibuf + byte_in_width * ys);
00191     } else {
00192       vcl_memcpy(buf_ + offset, (unsigned char const*)ibuf, bytes_per_row * ys);
00193       ibuf = (void const*)((char const*)ibuf + bytes_per_row * ys);
00194     }
00195   }
00196 
00197   return true;
00198 }
00199 
00200 vil1_image vil1_memory_image_impl::get_plane(unsigned int plane) const
00201 {
00202   assert(plane==0);
00203   return const_cast<vil1_memory_image_impl*>(this);
00204 }
00205 
00206 bool vil1_memory_image_impl::get_property(char const *tag,
00207                                           void *property_value) const
00208 {
00209   if (0==vcl_strcmp(tag, vil1_property_memory))
00210     return property_value ? (*(bool*)property_value) = true : true;
00211 
00212   if (0==vcl_strcmp(tag, vil1_property_top_row_first))
00213     return property_value ? (*(bool*)property_value) = true : true;
00214 
00215   if (0==vcl_strcmp(tag, vil1_property_left_first))
00216     return property_value ? (*(bool*)property_value) = true : true;
00217 
00218   return false;
00219 }
00220 
00221 
00222 // these functions are for creating a vil1_image from a section of
00223 // memory that has already been created and assigned to. This is
00224 // useful when interfacing with framegrabbers for example as no
00225 // copying of memory is needed. On the other hand, much care must be
00226 // taken with this method as the correct amount of memory must be
00227 // created prior to calling these functions.
00228 // Added by: Brendan McCane
00229 
00230 vil1_memory_image_impl::vil1_memory_image_impl(void *buf, int planes, int w, int h,
00231                                                vil1_memory_image_format const& format)
00232 {
00233   init(buf, planes, w, h, format.components, format.bits_per_component,
00234        format.component_format);
00235 }
00236 
00237 vil1_memory_image_impl::vil1_memory_image_impl(void *buf, int planes, int w, int h,
00238                                                int components, int bits_per_component,
00239                                                vil1_component_format component_format)
00240 {
00241   init(buf, planes, w, h, components, bits_per_component, component_format);
00242 }
00243 
00244 vil1_memory_image_impl::vil1_memory_image_impl(void *buf, int planes, int w, int h,
00245                                                vil1_pixel_format_t pixel_format)
00246 {
00247   init(buf, planes, w, h, pixel_format);
00248 }
00249 
00250 vil1_memory_image_impl::vil1_memory_image_impl(void *buf, int w, int h,
00251                                                int components, int bits_per_component,
00252                                                vil1_component_format component_format)
00253 {
00254   init(buf, 1, w, h, components, bits_per_component, component_format);
00255 }
00256 
00257 vil1_memory_image_impl::vil1_memory_image_impl(void *buf, int w, int h,
00258                                                vil1_pixel_format_t pixel_format)
00259 {
00260   init(buf, 1, w, h, pixel_format);
00261 }
00262 
00263 //: Return the name of the class;
00264 vcl_string vil1_memory_image_impl::is_a() const
00265 {
00266   static const vcl_string class_name_="vil1_memory_image_impl";
00267   return class_name_;
00268 }
00269 
00270 //: Return true if the name of the class matches the argument
00271 bool vil1_memory_image_impl::is_class(vcl_string const& s) const
00272 {
00273   return s==vil1_memory_image_impl::is_a() || vil1_image_impl::is_class(s);
00274 }

Generated on Mon Nov 23 05:09:19 2009 for core/vil1 by  doxygen 1.5.1