00001
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005
00006
00007
00008 #include "vil1_flip_components_impl.h"
00009 #include <vcl_cstring.h>
00010
00011 vil1_image vil1_flip_components_impl::get_plane(unsigned int p) const
00012 {
00013 return new vil1_flip_components_impl(base.get_plane(p));
00014 }
00015
00016 static void do_swap(char* buf, int nr_bytes, int nr_swaps)
00017 {
00018 if (nr_swaps <= 0) return;
00019 while (nr_swaps--) {
00020 for (int i=0; i<nr_bytes; ++i) {
00021 char t = buf[i]; buf[i] = buf[i+2*nr_bytes]; buf[i+2*nr_bytes] = t; }
00022 buf += 3*nr_bytes;
00023 }
00024 }
00025
00026 bool vil1_flip_components_impl::get_section(void *buf, int x0, int y0, int w, int h) const
00027 {
00028 if (!base.get_section(buf, x0, y0, w, h))
00029 return false;
00030 if (base.components() != 3)
00031 return true;
00032 int bpc = base.bits_per_component();
00033 if (bpc&7) return false;
00034 do_swap(static_cast<char*>(buf), bpc/8, w*h);
00035 return true;
00036 }
00037
00038 bool vil1_flip_components_impl::put_section(void const *buf, int x0, int y0, int w, int h)
00039 {
00040 if (base.components() != 3)
00041 return base.put_section(buf, x0, y0, w, h);
00042 int bpc = base.bits_per_component();
00043 if (bpc&7) return false;
00044 char* b = new char[bpc/8 * w*h * 3];
00045 vcl_memcpy(b, buf, bpc/8 * w*h * 3);
00046 do_swap(b, bpc/8, w*h);
00047 bool r = base.put_section(b, x0, y0, w, h);
00048 delete[] b;
00049 return r;
00050 }
00051
00052
00053 vcl_string vil1_flip_components_impl::is_a() const
00054 {
00055 static const vcl_string class_name_="vil1_flip_components_impl";
00056 return class_name_;
00057 }
00058
00059
00060 bool vil1_flip_components_impl::is_class(vcl_string const& s) const
00061 {
00062 return s==vil1_flip_components_impl::is_a() || vil1_image_impl::is_class(s);
00063 }