Changeset 3806 for OpenSceneGraph/trunk/examples/osgvolume/osgvolume.cpp
- Timestamp:
- 02/01/05 23:36:05 (8 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgvolume/osgvolume.cpp
r3805 r3806 26 26 typedef std::vector< osg::ref_ptr<osg::Image> > ImageList; 27 27 28 29 struct ReadOperator30 {31 inline void luminance(float l) const { rgba(l,l,l,1.0f); }32 inline void alpha(float a) const { rgba(1.0f,1.0f,1.0f,a); }33 inline void luminance_alpha(float l,float a) const { rgba(l,l,l,a); }34 inline void rgb(float r,float g,float b) const { rgba(r,g,b,1.0f); }35 inline void rgba(float r,float g,float b,float a) const { std::cout<<"pixel("<<r<<", "<<g<<", "<<b<<", "<<a<<")"<<std::endl; }36 };28 // example ReadOperator 29 // struct ReadOperator 30 // { 31 // inline void luminance(float l) const { rgba(l,l,l,1.0f); } 32 // inline void alpha(float a) const { rgba(1.0f,1.0f,1.0f,a); } 33 // inline void luminance_alpha(float l,float a) const { rgba(l,l,l,a); } 34 // inline void rgb(float r,float g,float b) const { rgba(r,g,b,1.0f); } 35 // inline void rgba(float r,float g,float b,float a) const { std::cout<<"pixel("<<r<<", "<<g<<", "<<b<<", "<<a<<")"<<std::endl; } 36 // }; 37 37 38 38 … … 47 47 case(GL_RGB): { for(unsigned int i=0;i<num;++i) { float r = float(*data++)*scale; float g = float(*data++)*scale; float b = float(*data++)*scale; operation.rgb(r,g,b); } } break; 48 48 case(GL_RGBA): { for(unsigned int i=0;i<num;++i) { float r = float(*data++)*scale; float g = float(*data++)*scale; float b = float(*data++)*scale; float a = float(*data++)*scale; operation.rgba(r,g,b,a); } } break; 49 case(GL_BGR): { for(unsigned int i=0;i<num;++i) { float b = float(*data++)*scale; float g = float(*data++)*scale; float r = float(*data++)*scale; operation.rgb(r,g,b); } } break; 50 case(GL_BGRA): { for(unsigned int i=0;i<num;++i) { float b = float(*data++)*scale; float g = float(*data++)*scale; float r = float(*data++)*scale; float a = float(*data++)*scale; operation.rgba(r,g,b,a); } } break; 49 51 } 50 52 } … … 65 67 } 66 68 67 68 69 struct ModifyOperator 70 { 71 inline void luminance(float& l) const {} 72 inline void alpha(float& a) const {} 73 inline void luminance_alpha(float& l,float& a) const {} 74 inline void rgb(float& r,float& g,float& b) const {} 75 inline void rgba(float& r,float& g,float& b,float& a) const {} 76 }; 69 template <class O> 70 void readImage(osg::Image* image, const O& operation) 71 { 72 if (!image) return; 73 74 for(int r=0;r<image->r();++r) 75 { 76 for(int t=0;t<image->t();++t) 77 { 78 readRow(image->s(), image->getPixelFormat(), image->getDataType(), image->data(0,t,r), operation); 79 } 80 } 81 } 82 83 // example ModifyOperator 84 // struct ModifyOperator 85 // { 86 // inline void luminance(float& l) const {} 87 // inline void alpha(float& a) const {} 88 // inline void luminance_alpha(float& l,float& a) const {} 89 // inline void rgb(float& r,float& g,float& b) const {} 90 // inline void rgba(float& r,float& g,float& b,float& a) const {} 91 // }; 77 92 78 93 … … 87 102 case(GL_LUMINANCE_ALPHA): { for(unsigned int i=0;i<num;++i) { float l = float(*data)*scale; float a = float(*(data+1))*scale; operation.luminance_alpha(l,a); *data++ = T(l*inv_scale); *data++ = T(a*inv_scale); } } break; 88 103 case(GL_RGB): { for(unsigned int i=0;i<num;++i) { float r = float(*data)*scale; float g = float(*(data+1))*scale; float b = float(*(data+2))*scale; operation.rgb(r,g,b); *data++ = T(r*inv_scale); *data++ = T(g*inv_scale); *data++ = T(b*inv_scale); } } break; 89 case(GL_RGBA): { for(unsigned int i=0;i<num;++i) { float r = float(*data)*scale; float g = float(*(data+1))*scale; float b = float(*(data+2))*scale; float a = float(*(data+3))*scale; operation.rgb (r,g,b); *data++ = T(r*inv_scale); *data++ = T(g*inv_scale); *data++ = T(g*inv_scale); *data++ = T(a*inv_scale); } } break;104 case(GL_RGBA): { for(unsigned int i=0;i<num;++i) { float r = float(*data)*scale; float g = float(*(data+1))*scale; float b = float(*(data+2))*scale; float a = float(*(data+3))*scale; operation.rgba(r,g,b,a); *data++ = T(r*inv_scale); *data++ = T(g*inv_scale); *data++ = T(g*inv_scale); *data++ = T(a*inv_scale); } } break; 90 105 case(GL_BGR): { for(unsigned int i=0;i<num;++i) { float b = float(*data)*scale; float g = float(*(data+1))*scale; float r = float(*(data+2))*scale; operation.rgb(r,g,b); *data++ = T(b*inv_scale); *data++ = T(g*inv_scale); *data++ = T(r*inv_scale); } } break; 91 case(GL_BGRA): { for(unsigned int i=0;i<num;++i) { float b = float(*data)*scale; float g = float(*(data+1))*scale; float r = float(*(data+2))*scale; float a = float(*(data+3))*scale; operation.rgb (r,g,b); *data++ = T(g*inv_scale); *data++ = T(b*inv_scale); *data++ = T(r*inv_scale); *data++ = T(a*inv_scale); } } break;106 case(GL_BGRA): { for(unsigned int i=0;i<num;++i) { float b = float(*data)*scale; float g = float(*(data+1))*scale; float r = float(*(data+2))*scale; float a = float(*(data+3))*scale; operation.rgba(r,g,b,a); *data++ = T(g*inv_scale); *data++ = T(b*inv_scale); *data++ = T(r*inv_scale); *data++ = T(a*inv_scale); } } break; 92 107 } 93 108 } … … 108 123 } 109 124 125 template <class M> 126 void modifyImage(osg::Image* image, const M& operation) 127 { 128 if (!image) return; 129 130 for(int r=0;r<image->r();++r) 131 { 132 for(int t=0;t<image->t();++t) 133 { 134 modifyRow(image->s(), image->getPixelFormat(), image->getDataType(), image->data(0,t,r), operation); 135 } 136 } 137 } 110 138 111 139 struct PassThroughTransformFunction … … 509 537 GL_RGBA,GL_UNSIGNED_BYTE); 510 538 539 if (osg::getCpuByteOrder()==osg::LittleEndian) alphaOffset = sourcePixelIncrement-alphaOffset-1; 511 540 512 541 for(int r=1;r<image_3d->r()-1;++r) … … 862 891 struct ScaleOperator 863 892 { 893 ScaleOperator():_scale(1.0f) {} 864 894 ScaleOperator(float scale):_scale(scale) {} 865 895 ScaleOperator(const ScaleOperator& so):_scale(so._scale) {} 896 897 ScaleOperator& operator = (const ScaleOperator& so) { _scale = so._scale; return *this; } 898 866 899 float _scale; 867 900 … … 942 975 943 976 944 bool endianSwap = (osg::getCpuByteOrder()==osg::BigEndian) ? (endian =="big") : (endian!="big");977 bool endianSwap = (osg::getCpuByteOrder()==osg::BigEndian) ? (endian!="big") : (endian=="big"); 945 978 946 979 unsigned int r_offset = (sizeZ<sizeR) ? sizeR/2 - sizeZ/2 : 0; … … 976 1009 // compute range of values 977 1010 FindRangeOperator rangeOp; 978 for(int r=0;r<sizeR;++r) 979 { 980 for(int t=0;t<sizeT;++t) 981 { 982 readRow(sizeS, pixelFormat, dataType, image->data(0,t,r), rangeOp); 983 } 984 } 985 986 // scale the values 987 for(int r=0;r<sizeR;++r) 988 { 989 for(int t=0;t<sizeT;++t) 990 { 991 modifyRow(sizeS, pixelFormat, dataType, image->data(0,t,r), ScaleOperator(1.0f/rangeOp._rmax)); 992 } 993 } 1011 readImage(image.get(), rangeOp); 1012 modifyImage(image.get(),ScaleOperator(1.0f/rangeOp._rmax)); 994 1013 } 995 1014 … … 1036 1055 } 1037 1056 1057 enum ColourSpaceOperation 1058 { 1059 NO_COLOUR_SPACE_OPERATION, 1060 MODULATE_ALPHA_BY_LUMINANCE, 1061 MODULATE_ALPHA_BY_COLOUR, 1062 REPLACE_ALPHA_WITH_LUMINACE 1063 }; 1064 1065 struct ModulatAlphaByLuminanceOperator 1066 { 1067 ModulatAlphaByLuminanceOperator() {} 1068 1069 inline void luminance(float&) const {} 1070 inline void alpha(float&) const {} 1071 inline void luminance_alpha(float& l,float& a) const { a*= l; } 1072 inline void rgb(float&,float&,float&) const {} 1073 inline void rgba(float& r,float& g,float& b,float& a) const { float l = (r+g+b)*0.3333333; a *= l;} 1074 }; 1075 1076 struct ModulatAlphaByColourOperator 1077 { 1078 ModulatAlphaByColourOperator(const osg::Vec4& colour):_colour(colour) { _lum = _colour.length(); } 1079 1080 osg::Vec4 _colour; 1081 float _lum; 1082 1083 inline void luminance(float&) const {} 1084 inline void alpha(float&) const {} 1085 inline void luminance_alpha(float& l,float& a) const { a*= l*_lum; } 1086 inline void rgb(float&,float&,float&) const {} 1087 inline void rgba(float& r,float& g,float& b,float& a) const { a = (r*_colour.red()+g*_colour.green()+b*_colour.blue()+a*_colour.alpha()); } 1088 }; 1089 1090 struct ReplaceAlphaWithLuminanceOperator 1091 { 1092 ReplaceAlphaWithLuminanceOperator() {} 1093 1094 inline void luminance(float&) const {} 1095 inline void alpha(float&) const {} 1096 inline void luminance_alpha(float& l,float& a) const { a= l; } 1097 inline void rgb(float&,float&,float&) const { } 1098 inline void rgba(float& r,float& g,float& b,float& a) const { float l = (r+g+b)*0.3333333; a = l; } 1099 }; 1100 1101 void doColourSpaceConversion(ColourSpaceOperation op, osg::Image* image, osg::Vec4& colour) 1102 { 1103 switch(op) 1104 { 1105 case (MODULATE_ALPHA_BY_LUMINANCE): 1106 std::cout<<"doing conversion MODULATE_ALPHA_BY_LUMINANCE"<<std::endl; 1107 modifyImage(image,ModulatAlphaByLuminanceOperator()); 1108 break; 1109 case (MODULATE_ALPHA_BY_COLOUR): 1110 std::cout<<"doing conversion MODULATE_ALPHA_BY_COLOUR"<<std::endl; 1111 modifyImage(image,ModulatAlphaByColourOperator(colour)); 1112 break; 1113 case (REPLACE_ALPHA_WITH_LUMINACE): 1114 std::cout<<"doing conversion REPLACE_ALPHA_WITH_LUMINACE"<<std::endl; 1115 modifyImage(image,ReplaceAlphaWithLuminanceOperator()); 1116 break; 1117 default: 1118 break; 1119 } 1120 } 1038 1121 1039 1122 int main( int argc, char **argv ) 1040 1123 { 1041 1042 1043 1124 // use an ArgumentParser object to manage the program arguments. 1044 1125 osg::ArgumentParser arguments(&argc,argv); … … 1066 1147 arguments.getApplicationUsage()->addCommandLineOption("--compressed-dxt3","Enable the usage of S3TC DXT3 compressed textures"); 1067 1148 arguments.getApplicationUsage()->addCommandLineOption("--compressed-dxt5","Enable the usage of S3TC DXT5 compressed textures"); 1149 arguments.getApplicationUsage()->addCommandLineOption("--modulate-alpha-by-luminance","For each pixel multiple the alpha value by the luminance."); 1150 arguments.getApplicationUsage()->addCommandLineOption("--replace-alpha-with-luminance","For each pixel mSet the alpha value to the luminance"); 1068 1151 // arguments.getApplicationUsage()->addCommandLineOption("--raw <sizeX> <sizeY> <sizeZ> <numberBytesPerComponent> <numberOfComponents> <endian> <filename>","read a raw image data"); 1069 1152 … … 1133 1216 while(arguments.read("--compressed-dxt5")) { internalFormatMode = osg::Texture::USE_S3TC_DXT5_COMPRESSION; } 1134 1217 1218 1219 // set up colour space operation. 1220 ColourSpaceOperation colourSpaceOperation = NO_COLOUR_SPACE_OPERATION; 1221 osg::Vec4 colourModulate(0.25f,0.25f,0.25f,0.25f); 1222 while(arguments.read("--modulate-alpha-by-luminance")) { colourSpaceOperation = MODULATE_ALPHA_BY_LUMINANCE; } 1223 while(arguments.read("--modulate-alpha-by-colour", colourModulate.x(),colourModulate.y(),colourModulate.z(),colourModulate.w() )) { colourSpaceOperation = MODULATE_ALPHA_BY_COLOUR; } 1224 while(arguments.read("--replace-alpha-with-luminance")) { colourSpaceOperation = REPLACE_ALPHA_WITH_LUMINACE; } 1225 1226 1135 1227 osg::ref_ptr<osg::Image> image_3d; 1136 1228 … … 1183 1275 1184 1276 if (!image_3d) return 0; 1277 1278 if (colourSpaceOperation!=NO_COLOUR_SPACE_OPERATION) 1279 { 1280 doColourSpaceConversion(colourSpaceOperation, image_3d.get(), colourModulate); 1281 } 1185 1282 1186 1283 osg::ref_ptr<osg::Image> normalmap_3d = createNormalMap ? createNormalMapTexture(image_3d.get()) : 0;
