Go to the documentation of this file.00001
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005
00006
00007
00008
00009
00010
00011 #include "vgui_vrml_texture_map.h"
00012 #include <vul/vul_file.h>
00013 #include <vil1/vil1_rgb.h>
00014 #include <vxl_config.h>
00015 #include <vil1/vil1_file_image.h>
00016 #include <vil1/vil1_memory_image_of.h>
00017 #include <vcl_iostream.h>
00018
00019 int VrmlDraw_TEX_MAX = 256;
00020
00021 vcl_string vgui_vrml_texture_map::vrml_dirname("");
00022
00023
00024 static inline int texmap_dimension(int in)
00025 {
00026 int MAX_SIZE = VrmlDraw_TEX_MAX;
00027 for (int w = 1; w < MAX_SIZE; w *= 2)
00028 if (w >= in)
00029 return w;
00030 return MAX_SIZE;
00031 }
00032
00033
00034 vgui_vrml_texture_map* vgui_vrml_texture_map::create(char const* filename)
00035 {
00036 if (filename == 0 || *filename == 0)
00037 return 0;
00038 #if 0
00039 if (gl_mode != MWinGLMode::textured)
00040 return 0;
00041 #endif // 0
00042
00043
00044 vcl_string fn;
00045 if (vul_file::exists(filename))
00046 fn = filename;
00047 else if (vrml_dirname.size() > 0) {
00048 fn = vrml_dirname; fn += "/"; fn += filename;
00049 if (!vul_file::exists(fn.c_str())) {
00050 vcl_cerr << "Can't find texture [tried "<< filename <<" and "<< fn.c_str() <<"]\n";
00051 return 0;
00052 }
00053 }
00054 filename = fn.c_str();
00055
00056 vcl_cerr << "Loading texture from ["<< filename <<"]... ";
00057 vil1_file_image fileimage(filename, vil1_file_image::laconic);
00058 if (!fileimage) {
00059 vcl_cerr << "Failed!";
00060 return 0;
00061 }
00062
00063 int w = fileimage.width();
00064 int h = fileimage.height();
00065
00066 int tex_w = texmap_dimension(w);
00067 int tex_h = texmap_dimension(h);
00068
00069 bool need_rescale = (tex_w != w) || (tex_h != h);
00070 if (need_rescale)
00071 vcl_cerr << "Rescale from "<<w<<'x'<<h<<" to "<<tex_w<<'x'<<tex_h<<", ";
00072
00073
00074 if (vil1_pixel_format(fileimage) == VIL1_RGB_BYTE) {
00075 vgui_vrml_texture_map* newmap = new vgui_vrml_texture_map(filename, tex_w, tex_h);
00076 vil1_memory_image_of<vil1_rgb<unsigned char> > rgb(fileimage.width(), fileimage.height());
00077 fileimage.get_section(rgb.get_buffer(), 0,0, fileimage.width(), fileimage.height());
00078 for (int y = 0; y < tex_h; ++y) {
00079 int orig_y = y * h / tex_h;
00080 for (int x = 0; x < tex_w; ++x)
00081 newmap->rgb(x,tex_h - y - 1) = rgb(x * w / tex_w, orig_y);
00082 }
00083 vcl_cerr << "Done.\n";
00084 return newmap;
00085 }
00086 else if (vil1_pixel_format(fileimage) == VIL1_BYTE) {
00087 vgui_vrml_texture_map* newmap = new vgui_vrml_texture_map(filename, tex_w, tex_h);
00088 #if 0
00089 vil1_memory_image_of<vxl_byte> gray(fileimage.get_image_ptr() );
00090 #endif // 0
00091 vil1_memory_image_of<vxl_byte> gray(fileimage.width(), fileimage.height());
00092 fileimage.get_section(gray.get_buffer(), 0,0, fileimage.width(), fileimage.height());
00093 for (int y = 0; y < tex_h; ++y) {
00094 int orig_y = y * h / tex_h;
00095 for (int x = 0; x < tex_w; ++x) {
00096 vxl_byte v = gray(x * w / tex_w, orig_y);
00097 newmap->rgb(x,tex_h - y - 1).r = v;
00098 newmap->rgb(x,tex_h - y - 1).g = v;
00099 newmap->rgb(x,tex_h - y - 1).b = v;
00100 }
00101 }
00102 vcl_cerr << "Done.\n";
00103 return newmap;
00104 }
00105
00106 vcl_cerr << " ignoring "<< fileimage.bits_per_component() <<" b x "<< fileimage.components() <<" comp image\n";
00107 return 0;
00108 }