core/vidl/vidl_frame.cxx
Go to the documentation of this file.
00001 // This is core/vidl/vidl_frame.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author Matt Leotta
00008 // \date   13 Jan 2006
00009 //
00010 //-----------------------------------------------------------------------------
00011 
00012 #include "vidl_frame.h"
00013 #include <vcl_cassert.h>
00014 #include <vil/vil_image_view.h>
00015 
00016 //-----------------------------------------------------------------------------
00017 
00018 //: Decrement reference count
00019 void
00020 vidl_frame::unref()
00021 {
00022   assert (ref_count_ >0);
00023   ref_count_--;
00024   if (ref_count_==0)
00025   {
00026     delete this;
00027   }
00028 }
00029 
00030 //-----------------------------------------------------------------------------
00031 
00032 //: Constructor - from a vil_image_view
00033 // return an invalid frame if the image format can not be wrapped
00034 vidl_memory_chunk_frame::
00035 vidl_memory_chunk_frame(const vil_image_view_base& image,
00036                         vidl_pixel_format fmt)
00037   : vidl_frame(), memory_(NULL)
00038 {
00039   ni_ = image.ni();
00040   nj_ = image.nj();
00041   // use the pixel component format to account for 
00042   // images of type vil_rgb<T>, vil_rgba<T>, etc.
00043   vil_pixel_format cmp_format =
00044       vil_pixel_format_component_format(image.pixel_format());
00045   unsigned int num_cmp = vil_pixel_format_num_components(image.pixel_format());
00046   unsigned int num_channels = image.nplanes() * num_cmp;
00047 
00048   if (cmp_format == VIL_PIXEL_FORMAT_UINT_16 &&
00049       num_channels == 1)
00050   {
00051     vil_image_view<vxl_uint_16> img = image;
00052     if (!img.is_contiguous())
00053       return;
00054     memory_ = img.memory_chunk();
00055     format_ = VIDL_PIXEL_FORMAT_MONO_16;
00056   }
00057   else if (cmp_format == VIL_PIXEL_FORMAT_BYTE)
00058   {
00059     vil_image_view<vxl_byte> img = image;
00060     if (!img.is_contiguous())
00061       return;
00062     memory_ = img.memory_chunk();
00063     if (img.nplanes() == 1)
00064     {
00065       format_ = VIDL_PIXEL_FORMAT_MONO_8;
00066     }
00067     else if (img.nplanes() == 3)
00068     {
00069       if (img.planestep() == 1) {
00070         if (fmt == VIDL_PIXEL_FORMAT_UYV_444)
00071           format_ = VIDL_PIXEL_FORMAT_UYV_444;
00072         else
00073           format_ = VIDL_PIXEL_FORMAT_RGB_24;
00074       }
00075       else
00076       {
00077         if (fmt == VIDL_PIXEL_FORMAT_YUV_444P)
00078           format_ = VIDL_PIXEL_FORMAT_YUV_444P;
00079         else
00080           format_ = VIDL_PIXEL_FORMAT_RGB_24P;
00081       }
00082     }
00083     else if (img.nplanes() == 4)
00084     {
00085       if (img.planestep() == 1)
00086         format_ = VIDL_PIXEL_FORMAT_RGBA_32;
00087       else
00088         format_ = VIDL_PIXEL_FORMAT_RGBA_32P;
00089     }
00090   }
00091   else if ( cmp_format == VIL_PIXEL_FORMAT_FLOAT )
00092   {
00093     vil_image_view<vxl_ieee_32> img = image;
00094     if (!img.is_contiguous())
00095       return;
00096     memory_ = img.memory_chunk();
00097     if ( img.nplanes()==1 )
00098       format_ = VIDL_PIXEL_FORMAT_MONO_F32;
00099     else if ( img.nplanes()==3 ) {
00100       if (img.planestep() == 1)
00101         format_ = VIDL_PIXEL_FORMAT_RGB_F32;
00102       else
00103         format_ = VIDL_PIXEL_FORMAT_RGB_F32P;
00104     }
00105   }
00106 
00107   if (fmt != VIDL_PIXEL_FORMAT_UNKNOWN &&
00108       fmt != format_)
00109     format_ = VIDL_PIXEL_FORMAT_UNKNOWN;
00110 
00111   if (format_ == VIDL_PIXEL_FORMAT_UNKNOWN)
00112     memory_ = NULL;
00113 }
00114 
00115