00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "map2.h"
00024 #include "common/meshio/imeshloader.h"
00025 #include "common/misc/types.h"
00026
00027 #include <IL/il.h>
00028
00029 using namespace sheep;
00030 using namespace std;
00031 using namespace toxic;
00032
00033 namespace {
00034 inline
00035 Real convert_uint8_to_real(uint8 x) {
00036 return static_cast<Real>(x) / 255.0;
00037 }
00038 }
00039
00040 Map2::Map2(const string &filename) {
00041
00042
00043 ILuint image_id;
00044
00045 ilGenImages(1, &image_id);
00046 ilBindImage(image_id);
00047
00048 if(!LoadImage(filename))
00049 throw FileNotFoundException(filename);
00050
00051 m_width = ilGetInteger(IL_IMAGE_WIDTH);
00052 m_height = ilGetInteger(IL_IMAGE_HEIGHT);
00053
00054 const int size = m_width * m_height;
00055 uint8 *buffer = new uint8[size * 3];
00056
00057 ilCopyPixels(0, 0, 0, m_width, m_height, 1, IL_RGB, IL_UNSIGNED_BYTE, buffer);
00058 ilDeleteImages(1, &image_id);
00059
00060 m_texels.resize(size);
00061
00062 uint8 *b = buffer;
00063
00064 for(int i = 0; i < size; ++i) {
00065 m_texels[i].m_r = convert_uint8_to_real(*b++);
00066 m_texels[i].m_g = convert_uint8_to_real(*b++);
00067 m_texels[i].m_b = convert_uint8_to_real(*b++);
00068 }
00069
00070 delete [] buffer;
00071 }
00072
00073 Map2::Map2(int w, int h, const unsigned char *texels) {
00074 assert(w > 0 && h > 0);
00075 assert(texels);
00076
00077 m_width = w;
00078 m_height = h;
00079
00080 const int size = m_width * m_height;
00081
00082 m_texels.resize(size);
00083
00084 for(int i = 0; i < size; ++i) {
00085 m_texels[i].m_r = convert_uint8_to_real(*texels++);
00086 m_texels[i].m_g = convert_uint8_to_real(*texels++);
00087 m_texels[i].m_b = convert_uint8_to_real(*texels++);
00088 }
00089 }