core/vil1/file_formats/vil1_gen.cxx

Go to the documentation of this file.
00001 // This is core/vil1/file_formats/vil1_gen.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 
00006 #include "vil1_gen.h"
00007 
00008 #include <vcl_cassert.h>
00009 #include <vcl_cstdlib.h> // vcl_abort()
00010 #include <vcl_cstdio.h>  // sprintf()
00011 #include <vcl_cstring.h>
00012 #include <vcl_iostream.h>
00013 
00014 #include <vil1/vil1_stream.h>
00015 #include <vil1/vil1_image_impl.h>
00016 #include <vil1/vil1_image.h>
00017 #include <vil1/vil1_property.h>
00018 
00019 char const* vil1_gen_format_tag = "gen";
00020 
00021 vil1_image_impl* vil1_gen_file_format::make_input_image(vil1_stream* vs)
00022 {
00023   // Attempt to read header
00024   vcl_string s;
00025   for (;;) {
00026     char buf;
00027     if (vs->read(&buf, 1L) == 0L)
00028       return 0;
00029     if (buf == 0)
00030       break;
00031     s += buf;
00032   }
00033   vcl_cerr << "vil1_gen_file_format: s= [" << s << "]\n";
00034 
00035   bool ok = (s[0] == 'g' &&
00036              s[1] == 'e' &&
00037              s[2] == 'n' &&
00038              s[3] == ':');
00039 
00040   if (!ok)
00041     return 0;
00042 
00043   vcl_cerr << "vil1_gen_file_format: s= [" << s << "]\n";
00044 
00045   return new vil1_gen_generic_image(s);
00046 }
00047 
00048 char const* vil1_gen_file_format::tag() const
00049 {
00050   return vil1_gen_format_tag;
00051 }
00052 
00053 /////////////////////////////////////////////////////////////////////////////
00054 
00055 char const* vil1_gen_generic_image::file_format() const
00056 {
00057   return vil1_gen_format_tag;
00058 }
00059 
00060 vil1_gen_generic_image::vil1_gen_generic_image(vcl_string const& /*s*/,
00061                                                int /*planes*/,
00062                                                int /*width*/,
00063                                                int /*height*/,
00064                                                int /*components*/,
00065                                                int /*bits_per_component*/,
00066                                                vil1_component_format /*format*/)
00067 {
00068   vcl_abort();
00069 }
00070 
00071 static int read_int(char const** p_inout)
00072 {
00073   int val = 0;
00074   char const* p = *p_inout;
00075   while (*p >= '0' && *p <= '9') {
00076     int d = *p - '0';
00077     val = val * 10 + d;
00078     ++p;
00079   }
00080   *p_inout = p;
00081   return val;
00082 }
00083 
00084 void vil1_gen_generic_image::init(vcl_string const& s)
00085 {
00086   char const* p = s.c_str();
00087   // 1. Skip over "gen:"
00088   p += 4;
00089   // 2. Get size
00090   width_ = -1;
00091   height_ = -1;
00092   vcl_sscanf(p, "%dx%d:", &width_, &height_);
00093   if (height_ == -1) {
00094     vcl_cerr << "vil1_gen_generic_image: bad height, should be gen:WxH:\n";
00095     width_ = 0;
00096     height_ = 0;
00097     return;
00098   }
00099   // Skip to colon
00100   while (*p != ':' && *p != 0)
00101     ++p;
00102 
00103   assert(*p != 0);
00104 
00105   // Skip colon;
00106   ++p;
00107 
00108   // Read type
00109   vcl_string type;
00110   while (*p != 0 && *p != ',') {
00111     type += *p;
00112     ++p;
00113   }
00114 
00115   vcl_cerr << "vil1_gen_generic_image: type = ["<<type<<"]\n";
00116 
00117   if (type == "grey" || type == "gray")
00118   {
00119     if (*p == ',') {
00120       ++p;
00121       params_[0] = read_int(&p);
00122     }
00123     else
00124       params_[0] = 128;
00125     components_ = 1;
00126     bits_per_component_ = 8;
00127     type_ = vil1_gen_gray;
00128 
00129     vcl_cerr << "vil1_gen_generic_image: p0 = ["<<params_[0]<<"]\n";
00130   }
00131   else if (type == "rgb")
00132   {
00133     if (*p == ',') {
00134       ++p;
00135       params_[0] = read_int(&p);
00136       ++p;
00137       params_[1] = read_int(&p);
00138       ++p;
00139       params_[2] = read_int(&p);
00140     }
00141     else
00142       params_[0] = 128;
00143     components_ = 3;
00144     bits_per_component_ = 8;
00145     type_ = vil1_gen_rgb;
00146 
00147     vcl_cerr << "vil1_gen_generic_image: p0 = [" << params_[0] << "], p1 = ["
00148              << params_[1] << "], p2 = [" << params_[2] << "]\n";
00149   }
00150   else
00151     assert(!"type must be one of grey, gray or rgb");
00152 }
00153 
00154 bool vil1_gen_generic_image::get_property(char const *tag, void *prop) const
00155 {
00156   if (0==vcl_strcmp(tag, vil1_property_top_row_first))
00157     return prop ? (*(bool*)prop) = true : true;
00158 
00159   if (0==vcl_strcmp(tag, vil1_property_left_first))
00160     return prop ? (*(bool*)prop) = true : true;
00161 
00162   return false;
00163 }
00164 
00165 bool vil1_gen_generic_image::get_section(void* buf, int /*x0*/, int /*y0*/, int xs, int ys) const
00166 {
00167   // A constant (generic) image pixel value is independent of (x0,y0)
00168   if (type_ == vil1_gen_gray) {
00169     vcl_memset(buf, params_[0], xs*ys);
00170     return true;
00171   }
00172   else if (type_ == vil1_gen_rgb) {
00173     int n = xs*ys;
00174     unsigned char* p = (unsigned char*)buf;
00175     unsigned char r = (unsigned char)(params_[0]);
00176     unsigned char g = (unsigned char)(params_[1]);
00177     unsigned char b = (unsigned char)(params_[2]);
00178     while (n--) {
00179       *p++ = r;
00180       *p++ = g;
00181       *p++ = b;
00182     }
00183     return true;
00184   }
00185   else
00186     return false;
00187 }
00188 
00189 bool vil1_gen_generic_image::put_section(void const* /*buf*/, int /*x0*/, int /*y0*/, int /*xs*/, int /*ys*/)
00190 {
00191   vcl_abort();
00192   return false;
00193 }
00194 
00195 vil1_image vil1_gen_generic_image::get_plane(unsigned int plane) const
00196 {
00197   assert(plane == 0);
00198   return const_cast<vil1_gen_generic_image*>(this);
00199 }

Generated on Sat Nov 22 05:08:28 2008 for core/vil1 by  doxygen 1.5.1